Advertisement
Guest User

Untitled

a guest
Apr 27th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.18 KB | None | 0 0
  1. #
  2. # Check https://www.hjemmeautomasjon.no/forums/topic/2737-multireg-termostat-se-n%C3%A5r-den-er-%C2%ABaktiv%C2%BB-i-homeseer/?tab=comments#comment-33740
  3.  
  4. import os, sys
  5. import time
  6. import openhab
  7.  
  8. openhab_url = 'http://foobar:8090/rest'
  9. openhab_client = openhab.openHAB(openhab_url)
  10. openhab_logfile = '/var/log/openhab2/openhab.log'
  11.  
  12. multiregs = {
  13. # z-wave node, room name
  14.     '22': 'Rom1',
  15.     '23': 'Rom2',
  16.     '11': 'Rom3',
  17.     '14': 'Rom4'
  18. }
  19.  
  20. #
  21. # Tail a logfile that can be rotated at any time:
  22. def tail(theFile, filename, inode):
  23.     theFile.seek(0,2)   # Go to the end of the file
  24.     while True:
  25.         line = theFile.readline()
  26.         if not line:
  27.             time.sleep(2)    # Sleep briefly for 10sec
  28.             if os.stat(filename).st_ino != inode:
  29.                 # Wait for new file:
  30.                 while not os.path.isfile(filename):
  31.                     time.sleep(1)
  32.                 theFile = open(filename, 'r')
  33.                 inode = os.stat(filename).st_ino
  34.                 print "LOGFILE ROTATED"
  35.             continue
  36.         yield line
  37.  
  38. if __name__ == '__main__':
  39.     linecounter = 0
  40.     lastbasicset = 0
  41.     fd = open(openhab_logfile, 'r')
  42.     current_inode = os.fstat(fd.fileno()).st_ino
  43.     for line in tail(fd, openhab_logfile, current_inode):
  44.         linecounter += 1
  45.         if line.find('NODE') > 0 and line.find('Basic Set sent to the controller') > 0:
  46.             lastbasicset = linecounter
  47.         if line.find('NODE') > 0 and line.find('Basic report') > 0 and linecounter < lastbasicset + 4:
  48.             elements = line[line.find('NODE'):].split()
  49.             nodeno = elements[1].replace(':', '')
  50.             if nodeno in multiregs and elements[6] == "0x00":
  51.                 print multiregs[nodeno]  + " was turned off"
  52.                 openhab_client.get_item('Termostat_' + multiregs[nodeno] + "_bryter").state = "OFF"
  53.             elif nodeno in multiregs and elements[6] == "0xFF":
  54.                 print multiregs[nodeno] + " was turned ON"
  55.                 openhab_client.get_item('Termostat_' + multiregs[nodeno] + "_bryter").state = "ON"
  56.             else:
  57.                 print "Not multireg node:"
  58.                 print elements
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement