How to poll a file in /sys

Posted by Bjoern on Stack Overflow See other posts from Stack Overflow or by Bjoern
Published on 2010-06-08T08:12:15Z Indexed on 2010/06/08 8:32 UTC
Read the original article Hit count: 318

Filed under:
|
|
|

Hi, I am stuck reading a file in /sys/ which contains the light intensity in Lux of the ambient light sensor on my Nokia N900 phone.

See thread on talk.maemo.org here

I tried to use pyinotify to poll the file but this looks some kind of wrong to me since the file is alway "process_IN_OPEN", "process_IN_ACCESS" and "process_IN_CLOSE_NOWRITE"

I basically want to get the changes ASAP and if something changed trigger an event, execute a class...

Here's the code I tried, which works, but not as I expected (I was hoping for process_IN_MODIFY to be triggered):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#

import os, time, pyinotify
import pyinotify

ambient_sensor = '/sys/class/i2c-adapter/i2c-2/2-0029/lux'

wm = pyinotify.WatchManager()  # Watch Manager
mask = pyinotify.ALL_EVENTS

def action(self, the_event):
    value = open(the_event.pathname, 'r').read().strip()
    return value

class EventHandler(pyinotify.ProcessEvent):
    def process_IN_ACCESS(self, event):
     print "ACCESS event:", action(self, event)

    def process_IN_ATTRIB(self, event):
     print "ATTRIB event:", action(self, event)

    def process_IN_CLOSE_NOWRITE(self, event):
     print "CLOSE_NOWRITE event:", action(self, event)

    def process_IN_CLOSE_WRITE(self, event):
     print "CLOSE_WRITE event:", action(self, event)

    def process_IN_CREATE(self, event):
     print "CREATE event:", action(self, event)

    def process_IN_DELETE(self, event):
     print "DELETE event:", action(self, event)

    def process_IN_MODIFY(self, event):
     print "MODIFY event:", action(self, event)

    def process_IN_OPEN(self, event):
     print "OPEN event:", action(self, event)


#log.setLevel(10)
notifier = pyinotify.ThreadedNotifier(wm, EventHandler())
notifier.start()

wdd = wm.add_watch(ambient_sensor, mask)
wdd

time.sleep(5)

notifier.stop()

Thanks for any hints, suggestions and examples

Kind regards
Bjoern

© Stack Overflow or respective owner

Related posts about python

Related posts about poll