Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import sys
- import time
- import mysql.connector
- from Phidget22.Devices.DigitalInput import *
- from Phidget22.PhidgetException import *
- from Phidget22.Phidget import *
- from Phidget22.Net import *
- try:
- file = open("/var/log/get_light.log", "a")
- ch0 = DigitalInput()
- ch1 = DigitalInput()
- ch2 = DigitalInput()
- ch3 = DigitalInput()
- ch4 = DigitalInput()
- ch5 = DigitalInput()
- ch6 = DigitalInput()
- ch7 = DigitalInput()
- time.sleep(15)
- cnx = mysql.connector.connect(user='user', password='pass', host='host', database='db')
- cursor = cnx.cursor(buffered=True)
- except RuntimeError as e:
- file.write("Runtime Exception %s" % e.details)
- file.write("Press Enter to Exit...\n")
- readin = sys.stdin.read(1)
- exit(1)
- def DigitalInputAttached(e):
- try:
- attached = e
- except PhidgetException as e:
- file.write("Phidget Exception %i: %s" % (e.code, e.details))
- file.write("Press Enter to Exit...\n")
- readin = sys.stdin.read(1)
- exit(1)
- def DigitalInputDetached(e):
- detached = e
- try:
- file.write("\nDetach event on Port %d Channel %d" % (detached.getHubPort(), detached.getChannel()))
- except PhidgetException as e:
- file.write("Phidget Exception %i: %s" % (e.code, e.details))
- file.write("Press Enter to Exit...\n")
- readin = sys.stdin.read(1)
- exit(1)
- def ErrorEvent(e, eCode, description):
- file.write("Error %i : %s" % (eCode, description))
- def StateChangeHandler(e, state):
- file.write("Channel %d : %f" % (e.getChannel(), state))
- cursor.execute("UPDATE object SET Valeur=%i WHERE Link=%i" % (int(state), int(e.getChannel())))
- cnx.commit()
- try:
- ch0.setOnAttachHandler(DigitalInputAttached)
- ch0.setOnDetachHandler(DigitalInputDetached)
- ch0.setOnErrorHandler(ErrorEvent)
- ch0.setOnStateChangeHandler(StateChangeHandler)
- ch0.setChannel(0)
- ch0.openWaitForAttachment(5000)
- ch1.setOnAttachHandler(DigitalInputAttached)
- ch1.setOnDetachHandler(DigitalInputDetached)
- ch1.setOnErrorHandler(ErrorEvent)
- ch1.setOnStateChangeHandler(StateChangeHandler)
- ch1.setChannel(1)
- ch1.openWaitForAttachment(5000)
- ch2.setOnAttachHandler(DigitalInputAttached)
- ch2.setOnDetachHandler(DigitalInputDetached)
- ch2.setOnErrorHandler(ErrorEvent)
- ch2.setOnStateChangeHandler(StateChangeHandler)
- ch2.setChannel(2)
- ch2.openWaitForAttachment(5000)
- ch3.setOnAttachHandler(DigitalInputAttached)
- ch3.setOnDetachHandler(DigitalInputDetached)
- ch3.setOnErrorHandler(ErrorEvent)
- ch3.setOnStateChangeHandler(StateChangeHandler)
- ch3.setChannel(3)
- ch3.openWaitForAttachment(5000)
- ch4.setOnAttachHandler(DigitalInputAttached)
- ch4.setOnDetachHandler(DigitalInputDetached)
- ch4.setOnErrorHandler(ErrorEvent)
- ch4.setOnStateChangeHandler(StateChangeHandler)
- ch4.setChannel(4)
- ch4.openWaitForAttachment(5000)
- ch5.setOnAttachHandler(DigitalInputAttached)
- ch5.setOnDetachHandler(DigitalInputDetached)
- ch5.setOnErrorHandler(ErrorEvent)
- ch5.setOnStateChangeHandler(StateChangeHandler)
- ch5.setChannel(5)
- ch5.openWaitForAttachment(5000)
- ch6.setOnAttachHandler(DigitalInputAttached)
- ch6.setOnDetachHandler(DigitalInputDetached)
- ch6.setOnErrorHandler(ErrorEvent)
- ch6.setOnStateChangeHandler(StateChangeHandler)
- ch6.setChannel(6)
- ch6.openWaitForAttachment(5000)
- ch7.setOnAttachHandler(DigitalInputAttached)
- ch7.setOnDetachHandler(DigitalInputDetached)
- ch7.setOnErrorHandler(ErrorEvent)
- ch7.setOnStateChangeHandler(StateChangeHandler)
- ch7.setChannel(7)
- ch7.openWaitForAttachment(5000)
- except PhidgetException as e:
- file.write("Phidget Exception %i: %s" % (e.code, e.details))
- file.write("Press Enter to Exit...\n")
- readin = sys.stdin.read(1)
- exit(1)
- while 1:
- time.sleep(10)
- try:
- ch0.close()
- ch1.close()
- ch2.close()
- ch3.close()
- ch4.close()
- ch5.close()
- ch6.close()
- ch7.close()
- cnx.close()
- except PhidgetException as e:
- file.write("Phidget Exception %i: %s" % (e.code, e.details))
- file.write("Press Enter to Exit...\n")
- readin = sys.stdin.read(1)
- exit(1)
- file.write("Closed DigitalInput device")
- exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement