Advertisement
DSX42

InputHandler

Dec 19th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.75 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import time
  5. import mysql.connector
  6. from Phidget22.Devices.DigitalInput import *
  7. from Phidget22.PhidgetException import *
  8. from Phidget22.Phidget import *
  9. from Phidget22.Net import *
  10.  
  11. try:
  12.         file = open("/var/log/get_light.log", "a")
  13.         ch0 = DigitalInput()
  14.         ch1 = DigitalInput()
  15.         ch2 = DigitalInput()
  16.         ch3 = DigitalInput()
  17.         ch4 = DigitalInput()
  18.         ch5 = DigitalInput()
  19.         ch6 = DigitalInput()
  20.         ch7 = DigitalInput()
  21.         time.sleep(15)
  22.         cnx = mysql.connector.connect(user='user', password='pass', host='host', database='db')
  23.         cursor = cnx.cursor(buffered=True)
  24. except RuntimeError as e:
  25.         file.write("Runtime Exception %s" % e.details)
  26.         file.write("Press Enter to Exit...\n")
  27.         readin = sys.stdin.read(1)
  28.         exit(1)
  29.  
  30. def DigitalInputAttached(e):
  31.         try:
  32.                 attached = e
  33.  
  34.         except PhidgetException as e:
  35.                 file.write("Phidget Exception %i: %s" % (e.code, e.details))
  36.                 file.write("Press Enter to Exit...\n")
  37.                 readin = sys.stdin.read(1)
  38.                 exit(1)
  39.  
  40. def DigitalInputDetached(e):
  41.         detached = e
  42.         try:
  43.                 file.write("\nDetach event on Port %d Channel %d" % (detached.getHubPort(), detached.getChannel()))
  44.         except PhidgetException as e:
  45.                 file.write("Phidget Exception %i: %s" % (e.code, e.details))
  46.                 file.write("Press Enter to Exit...\n")
  47.                 readin = sys.stdin.read(1)
  48.                 exit(1)
  49.  
  50. def ErrorEvent(e, eCode, description):
  51.         file.write("Error %i : %s" % (eCode, description))
  52.  
  53. def StateChangeHandler(e, state):
  54.         file.write("Channel %d : %f" % (e.getChannel(), state))
  55.         cursor.execute("UPDATE object SET Valeur=%i WHERE Link=%i" % (int(state), int(e.getChannel())))
  56.         cnx.commit()
  57.  
  58. try:
  59.         ch0.setOnAttachHandler(DigitalInputAttached)
  60.         ch0.setOnDetachHandler(DigitalInputDetached)
  61.         ch0.setOnErrorHandler(ErrorEvent)
  62.         ch0.setOnStateChangeHandler(StateChangeHandler)
  63.         ch0.setChannel(0)
  64.         ch0.openWaitForAttachment(5000)
  65.  
  66.         ch1.setOnAttachHandler(DigitalInputAttached)
  67.         ch1.setOnDetachHandler(DigitalInputDetached)
  68.         ch1.setOnErrorHandler(ErrorEvent)
  69.         ch1.setOnStateChangeHandler(StateChangeHandler)
  70.         ch1.setChannel(1)
  71.         ch1.openWaitForAttachment(5000)
  72.  
  73.         ch2.setOnAttachHandler(DigitalInputAttached)
  74.         ch2.setOnDetachHandler(DigitalInputDetached)
  75.         ch2.setOnErrorHandler(ErrorEvent)
  76.         ch2.setOnStateChangeHandler(StateChangeHandler)
  77.         ch2.setChannel(2)
  78.         ch2.openWaitForAttachment(5000)
  79.  
  80.         ch3.setOnAttachHandler(DigitalInputAttached)
  81.         ch3.setOnDetachHandler(DigitalInputDetached)
  82.         ch3.setOnErrorHandler(ErrorEvent)
  83.         ch3.setOnStateChangeHandler(StateChangeHandler)
  84.         ch3.setChannel(3)
  85.         ch3.openWaitForAttachment(5000)
  86.  
  87.         ch4.setOnAttachHandler(DigitalInputAttached)
  88.         ch4.setOnDetachHandler(DigitalInputDetached)
  89.         ch4.setOnErrorHandler(ErrorEvent)
  90.         ch4.setOnStateChangeHandler(StateChangeHandler)
  91.         ch4.setChannel(4)
  92.         ch4.openWaitForAttachment(5000)
  93.  
  94.         ch5.setOnAttachHandler(DigitalInputAttached)
  95.         ch5.setOnDetachHandler(DigitalInputDetached)
  96.         ch5.setOnErrorHandler(ErrorEvent)
  97.         ch5.setOnStateChangeHandler(StateChangeHandler)
  98.         ch5.setChannel(5)
  99.         ch5.openWaitForAttachment(5000)
  100.  
  101.         ch6.setOnAttachHandler(DigitalInputAttached)
  102.         ch6.setOnDetachHandler(DigitalInputDetached)
  103.         ch6.setOnErrorHandler(ErrorEvent)
  104.         ch6.setOnStateChangeHandler(StateChangeHandler)
  105.         ch6.setChannel(6)
  106.         ch6.openWaitForAttachment(5000)
  107.  
  108.         ch7.setOnAttachHandler(DigitalInputAttached)
  109.         ch7.setOnDetachHandler(DigitalInputDetached)
  110.         ch7.setOnErrorHandler(ErrorEvent)
  111.         ch7.setOnStateChangeHandler(StateChangeHandler)
  112.         ch7.setChannel(7)
  113.         ch7.openWaitForAttachment(5000)
  114.  
  115. except PhidgetException as e:
  116.         file.write("Phidget Exception %i: %s" % (e.code, e.details))
  117.         file.write("Press Enter to Exit...\n")
  118.         readin = sys.stdin.read(1)
  119.         exit(1)
  120.  
  121. while 1:
  122.         time.sleep(10)
  123.  
  124. try:
  125.         ch0.close()
  126.         ch1.close()
  127.         ch2.close()
  128.         ch3.close()
  129.         ch4.close()
  130.         ch5.close()
  131.         ch6.close()
  132.         ch7.close()
  133.         cnx.close()
  134. except PhidgetException as e:
  135.         file.write("Phidget Exception %i: %s" % (e.code, e.details))
  136.         file.write("Press Enter to Exit...\n")
  137.         readin = sys.stdin.read(1)
  138.         exit(1)
  139. file.write("Closed DigitalInput device")
  140. exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement