class cycleForever(threading.Thread): def __init__( self, behavior_setup_name, interfaceKitIPAddress, pumpIPAddress, interfaceKitPort=5001, pumpPort=100, pump_channels=["01", "02"] ): threading.Thread.__init__(self) self.setup_name = behavior_setup_name self.interfaceKitIPAddress = interfaceKitIPAddress self.interfaceKitPort = interfaceKitPort self.controller = InterfaceKit() self.controller.openRemoteIP(self.interfaceKitIPAddress, self.interfaceKitPort) self.pumpIPAddress = pumpIPAddress self.pumpPort = pumpPort self.pumpsConn = newPumpConnection(self.pumpIPAddress, self.pumpPort) self.pump_channels = pump_channels def run(self): while True: infuse = infuseFully(self.setup_name, self.controller, self.pumpsConn, pump_channels=self.pump_channels) infuse.start() infuse.join() withdraw = withdrawFully(self.setup_name, self.controller, self.pumpsConn, pump_channels=self.pump_channels) withdraw.start() withdraw.join()
def instanciarIK (self, nroSerie): """ Método utilizado para instanciar un objeto de tipo InterfaceKit de la API de Phidgets, que permite interactuar con la placa controladora y sus puertos""" try: ik= InterfaceKit() ik.openRemoteIP(self.__ipWS, self.__puertoWS, nroSerie) ik.waitForAttach(5000) return ik except : ik.closePhidget() return None
def setup_interfaceKit(): #Create an interfacekit object try: interfaceKit = InterfaceKit() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) try: interfaceKit.setOnAttachHandler(inferfaceKitAttached) interfaceKit.setOnDetachHandler(interfaceKitDetached) interfaceKit.setOnErrorhandler(interfaceKitError) interfaceKit.setOnInputChangeHandler(interfaceKitInputChanged) interfaceKit.setOnOutputChangeHandler(interfaceKitOutputChanged) interfaceKit.setOnSensorChangeHandler(interfaceKitSensorChanged) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Opening phidget object....") try: #interfaceKit.openPhidget() interfaceKit.openRemoteIP(IP, port=5001) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: interfaceKit.waitForAttach(10000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: interfaceKit.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1) return interfaceKit
def setup_interfaceKit(): #Create an interfacekit object try: interfaceKit = InterfaceKit() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) try: interfaceKit.setOnAttachHandler(inferfaceKitAttached) interfaceKit.setOnDetachHandler(interfaceKitDetached) interfaceKit.setOnErrorhandler(interfaceKitError) interfaceKit.setOnInputChangeHandler(interfaceKitInputChanged) interfaceKit.setOnOutputChangeHandler(interfaceKitOutputChanged) interfaceKit.setOnSensorChangeHandler(interfaceKitSensorChanged) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Opening phidget object....") try: #interfaceKit.openPhidget() interfaceKit.openRemoteIP(IP, port=5001) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: interfaceKit.waitForAttach(10000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: interfaceKit.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1) return interfaceKit
class cycleFor(threading.Thread): def __init__( self, numCycles, behavior_setup_name, interfaceKitIPAddress, pumpIPAddress, interfaceKitPort=5001, primeForBehavior=True, pumpPort=100, pump_channels=["01", "02"] ): threading.Thread.__init__(self) self.controller = InterfaceKit() self.controller.openRemoteIP(interfaceKitIPAddress, interfaceKitPort) self.pumpsConn = newPumpConnection(pumpIPAddress, pumpPort) self.numCycles = numCycles self.behavior_setup_name = behavior_setup_name self.interfaceKitIPAddress = interfaceKitIPAddress self.pumpIPAddress = pumpIPAddress self.interfaceKitPort = interfaceKitPort self.primeForBehavior = primeForBehavior self.pumpPort = pumpPort self.pump_channels = pump_channels def run(self): #infuse fully then withdraw fully for numCycles for i in xrange(self.numCycles): infuse = infuseFully(self.behavior_setup_name, self.controller, self.pumpsConn, pump_channels=self.pump_channels) infuse.start() infuse.join() withdraw = withdrawFully(self.behavior_setup_name, self.controller, self.pumpsConn, pump_channels=self.pump_channels) withdraw.start() withdraw.join() if self.primeForBehavior: prime = primeForBehaviorSession(self.behavior_setup_name, self.controller, self.pumpsConn, pump_channels=self.pump_channels) prime.start() prime.join() self.pumpsConn.close()
def main(): args = parseCommandLineArgs() config = getSetupInfoFromConfig() if args["add"]: saveNewSetup( args["<setupName>"][0], args["<phidgetWebServiceIPaddress>"], args["<StartechAdaptorIPaddress>"], phidget_webservice_listen_port=int(args["--phidgetPort"]), pump_telnet_listen_port=int(args["--startechPort"]), ) elif args["rm"]: for setup in args["<setupName>"]: deleteSetup(setup) elif args["config"] and args["channels"]: updateSetupField(args["<setupName>"][0], "pump_channels", [chan for chan in args["<chans>"]]) elif args["run"]: if args["cycle"]: threads = [] for setup in args["<setupName>"]: #run pump routine for each setup in its own thread pump_routine = cycleFor( int(args["-n"]), setup, config["setups"][setup]["setupIPaddr"], config["setups"][setup]["pumpIPaddr"], interfaceKitPort=config["setups"][setup]["phidget_webservice_listen_port"], primeForBehavior=args["--primePumps"], pumpPort=config["setups"][setup]["pump_telnet_listen_port"], pump_channels=config["setups"][setup]["pump_channels"] ) pump_routine.setDaemon(True) pump_routine.start() threads.append(pump_routine) print "Starting infuse/withdraw for", args["-n"], "cycles. Ctrl+c to stop pumping..." try: while threading.activeCount() > 1: #keep main thread running so it can raise KeyboardInterrupt time.sleep(1) except KeyboardInterrupt: for pump_routine in threads: #TODO make this thread safe with threading.Lock in case main thread tries to access pumpConns concurrently if pump_routine.isAlive(): for ch in pump_routine.pump_channels: ch = str(ch) pump_routine.pumpsConn.write(ch + " STP\r\n") pump_routine.pumpsConn.read_until(ch, timeout=5) pump_routine.pumpsConn.close() if args["forever"]: threads = [] for setup in args["<setupName>"]: pump_routine = cycleForever( setup, config["setups"][setup]["setupIPaddr"], config["setups"][setup]["pumpIPaddr"], interfaceKitPort=config["setups"][setup]["phidget_webservice_listen_port"], pumpPort=config["setups"][setup]["pump_telnet_listen_port"], pump_channels=config["setups"][setup]["pump_channels"] ) pump_routine.setDaemon(True) pump_routine.start() threads.append(pump_routine) print "Withdraw/Infuse cycling forever...happy cleaning. :-)" print "Protip: ctrl+c will exit the program after finishing the current cycle" try: while threading.activeCount() > 1: time.sleep(1) except KeyboardInterrupt: for pump_routine in threads: if pump_routine.isAlive(): for ch in pump_routine.pump_channels: ch = str(ch) pump_routine.pumpsConn.write(ch + " STP\r\n") pump_routine.pumpsConn.read_until(ch, timeout=5) pump_routine.pumpsConn.close() elif args["inf"]: threads = [] for setup in args["<setupName>"]: controller = InterfaceKit() controller.openRemoteIP(config["setups"][setup]["setupIPaddr"], config["setups"][setup]["phidget_webservice_listen_port"]) pumpsConn = newPumpConnection(config["setups"][setup]["pumpIPaddr"], port=config["setups"][setup]["pump_telnet_listen_port"]) time.sleep(0.2) #give time to establish connections; multiple threads need extra time pump_routine = infuseFully( setup, controller, pumpsConn, pump_channels=config["setups"][setup]["pump_channels"] ) pump_routine.setDaemon(True) pump_routine.start() threads.append(pump_routine) print "Infusing...press ctrl+c to cancel." try: while threading.activeCount() > 1: time.sleep(1) except KeyboardInterrupt: for pump_routine in threads: if pump_routine.isAlive(): for ch in pump_routine.pump_channels: ch = str(ch) pump_routine.pump_conn.write(ch + " STP\r\n") pump_routine.pump_conn.read_until(ch, timeout=5) pump_routine.pump_conn.close() elif args["wdr"]: threads = [] for setup in args["<setupName>"]: controller = InterfaceKit() controller.openRemoteIP(config["setups"][setup]["setupIPaddr"], config["setups"][setup]["phidget_webservice_listen_port"]) pumpsConn = newPumpConnection(config["setups"][setup]["pumpIPaddr"], port=config["setups"][setup]["pump_telnet_listen_port"]) time.sleep(0.2) #give time to establish connections; multiple threads need extra time pump_routine = withdrawFully( setup, controller, pumpsConn, pump_channels=config["setups"][setup]["pump_channels"] ) pump_routine.setDaemon(True) pump_routine.start() threads.append(pump_routine) print "Withdrawing...press ctrl+c to cancel." try: while threading.activeCount() > 1: time.sleep(1) except KeyboardInterrupt: for pump_routine in threads: if pump_routine.isAlive(): for ch in pump_routine.pump_channels: ch = str(ch) pump_routine.pump_conn.write(ch + " STP\r\n") pump_routine.pump_conn.read_until(ch, timeout=5) pump_routine.pump_conn.close() elif args["prime"]: threads = [] for setup in args["<setupName>"]: controller = InterfaceKit() controller.openRemoteIP(config["setups"][setup]["setupIPaddr"], config["setups"][setup]["phidget_webservice_listen_port"]) pumpsConn = newPumpConnection(config["setups"][setup]["pumpIPaddr"], port=config["setups"][setup]["pump_telnet_listen_port"]) time.sleep(0.2) #give time to establish connections; multiple threads need extra time pump_routine = primeForBehaviorSession( setup, controller, pumpsConn, pump_channels=config["setups"][setup]["pump_channels"] ) pump_routine.setDaemon(True) pump_routine.start() threads.append(pump_routine) print "Infusing 1 mL to get water flowing for a behavior session...press ctrl+c to cancel." try: while threading.activeCount() > 1: time.sleep(1) except KeyboardInterrupt: for pump_routine in threads: if pump_routine.isAlive(): for ch in pump_routine.pump_channels: ch = str(ch) pump_routine.pump_conn.write(ch + " STP\r\n") pump_routine.pump_conn.read_until(ch, timeout=5) pump_routine.pump_conn.close() else: pass
try: interfaceKit.setOnAttachHandler(inferfaceKitAttached) interfaceKit.setOnDetachHandler(interfaceKitDetached) interfaceKit.setOnErrorhandler(interfaceKitError) interfaceKit.setOnInputChangeHandler(interfaceKitInputChanged) interfaceKit.setOnOutputChangeHandler(interfaceKitOutputChanged) interfaceKit.setOnSensorChangeHandler(interfaceKitSensorChanged) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Opening phidget relay board....") try: interfaceKit.openRemoteIP("192.168.128.2", port=5001, serial=SERIAL_NUM) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: interfaceKit.waitForAttach(10000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: interfaceKit.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details))
#try: #interfaceKit.setOnAttachHandler(inferfaceKitAttached) #interfaceKit.setOnDetachHandler(interfaceKitDetached) #interfaceKit.setOnErrorhandler(interfaceKitError) #interfaceKit.setOnInputChangeHandler(interfaceKitInputChanged) #interfaceKit.setOnOutputChangeHandler(interfaceKitOutputChanged) #interfaceKit.setOnSensorChangeHandler(interfaceKitSensorChanged) #except PhidgetException as e: #print("Phidget Exception %i: %s" % (e.code, e.details)) #print("Exiting....") #exit(1) print("Opening phidget object....") try: interfaceKit.openRemoteIP("192.168.128.250", port=5001, serial=178346) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: interfaceKit.waitForAttach(10000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: interfaceKit.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details))
class LTCPhidget(object): # TODO: can the remote specific events find a disconnected usb cable? devserial = 0 IP = "0.0.0.0" port = 0 input = {} output = {} sensor = {} callback = { 'attach': [], 'detach': [], 'error': [], 'output': [], 'input': [], 'sensor': [] } def __init__(self, **kwargs): log.debug("Acquiring InterfaceKit") self.ik = InterfaceKit() log.debug("Registering Handlers") self.ik.setOnAttachHandler(self._onAttach) self.ik.setOnDetachHandler(self._onDetach) self.ik.setOnErrorhandler(self._onError) self.ik.setOnOutputChangeHandler(self._onOutput) self.ik.setOnInputChangeHandler(self._onInput) self.ik.setOnSensorChangeHandler(self._onSensor) def start(self): log.verbose("Opening remote IP") self.ik.openRemoteIP(self.IP, self.port, self.devserial) log.debug("Remote IP opened") def close(self): log.verbose("Closing InterfaceKit") self.ik.closePhidget() log.debug("Interfac kit closed") def add_callback(self, cb, type): log.debug("Adding a {} type callback".format(type)) self.callback[type].append(cb) def remove_callback(self, cb, type): log.debug("Removing a {} type callback".format(type)) self.callback[type].remove(cb) def _genericCB(self, event, type): log.verbose("{} event received".format(type)) for cb in self.callback[type]: cb(event) for dev in self.input.itervalues(): for cb in dev.callback[type]: cb(event) for dev in self.output.itervalues(): for cb in dev.callback[type]: cb(event) for dev in self.sensor.itervalues(): for cb in dev.callback[type]: cb(event) def _onAttach(self, event): self._genericCB(event, 'attach') def _onDetach(self, event): self._genericCB(event, 'detach') def _onError(self, event): log.debug(event.description) log.verbose("{} event received".format(type)) for cb in self.callback['error']: cb(event) def _onOutput(self, event): log.verbose("Output event received") for cb in self.callback['output']: cb(event) try: for cb in self.output[event.index].callback['value']: cb(event) except KeyError: pass def _onInput(self, event): log.verbose("Input event received") for cb in self.callback['input']: cb(event) try: for cb in self.input[event.index].callback['value']: cb(event) except KeyError: pass def _onSensor(self, event): log.verbose("Sensor event received") for cb in self.callback['sensor']: cb(event) try: for cb in self.sensor[event.index].callback['value']: cb(event) except KeyError: pass
from django.contrib.auth.decorators import login_required from django.core.mail import send_mail, BadHeaderError from django.views.decorators.cache import cache_page from django.utils import timezone from django.conf import settings from .models import Plant, Photo import json from Phidgets.PhidgetException import PhidgetException from Phidgets.Devices.InterfaceKit import InterfaceKit # Create your views here. is_phidget_connected = False interfaceKit = InterfaceKit() light_val = 0 try: interfaceKit.openRemoteIP('169.254.4.87', 5001, -1, "greenspy") except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") # try: # light_val = interfaceKit.getSensorValue(0) # except PhidgetException as e: # print("Phidget Exception %i: %s" % (e.code, e.details)) def main_page(request): # Create an interface kit object return render(request, 'GreenSpy/index.html') # def live_page(request):
from django.contrib.auth.decorators import login_required from django.core.mail import send_mail, BadHeaderError from django.views.decorators.cache import cache_page from django.utils import timezone from django.conf import settings from .models import Plant, Photo import json from Phidgets.PhidgetException import PhidgetException from Phidgets.Devices.InterfaceKit import InterfaceKit # Create your views here. is_phidget_connected = False interfaceKit = InterfaceKit() light_val = 0 try: interfaceKit.openRemoteIP('169.254.4.87', 5001, -1, "greenspy") except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") # try: # light_val = interfaceKit.getSensorValue(0) # except PhidgetException as e: # print("Phidget Exception %i: %s" % (e.code, e.details)) def main_page(request): # Create an interface kit object return render(request, 'GreenSpy/index.html') # def live_page(request):
#try: #interfaceKit.setOnAttachHandler(inferfaceKitAttached) #interfaceKit.setOnDetachHandler(interfaceKitDetached) #interfaceKit.setOnErrorhandler(interfaceKitError) #interfaceKit.setOnInputChangeHandler(interfaceKitInputChanged) #interfaceKit.setOnOutputChangeHandler(interfaceKitOutputChanged) #interfaceKit.setOnSensorChangeHandler(interfaceKitSensorChanged) #except PhidgetException as e: #print("Phidget Exception %i: %s" % (e.code, e.details)) #print("Exiting....") #exit(1) print("Opening phidget object....") try: interfaceKit.openRemoteIP("192.168.128.250", port=5001, serial=178346) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: interfaceKit.waitForAttach(10000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: interfaceKit.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details))
class LTCPhidget(object): # TODO: can the remote specific events find a disconnected usb cable? devserial = 0 IP = "0.0.0.0" port = 0 input = {} output = {} sensor = {} callback = {'attach': [], 'detach': [], 'error': [], 'output': [], 'input': [], 'sensor': []} def __init__(self, **kwargs): log.debug("Acquiring InterfaceKit") self.ik = InterfaceKit() log.debug("Registering Handlers") self.ik.setOnAttachHandler(self._onAttach) self.ik.setOnDetachHandler(self._onDetach) self.ik.setOnErrorhandler(self._onError) self.ik.setOnOutputChangeHandler(self._onOutput) self.ik.setOnInputChangeHandler(self._onInput) self.ik.setOnSensorChangeHandler(self._onSensor) def start(self): log.verbose("Opening remote IP") self.ik.openRemoteIP(self.IP, self.port, self.devserial) log.debug("Remote IP opened") def close(self): log.verbose("Closing InterfaceKit") self.ik.closePhidget() log.debug("Interfac kit closed") def add_callback(self, cb, type): log.debug("Adding a {} type callback".format(type)) self.callback[type].append(cb) def remove_callback(self, cb, type): log.debug("Removing a {} type callback".format(type)) self.callback[type].remove(cb) def _genericCB(self, event, type): log.verbose("{} event received".format(type)) for cb in self.callback[type]: cb(event) for dev in self.input.itervalues(): for cb in dev.callback[type]: cb(event) for dev in self.output.itervalues(): for cb in dev.callback[type]: cb(event) for dev in self.sensor.itervalues(): for cb in dev.callback[type]: cb(event) def _onAttach(self, event): self._genericCB(event, 'attach') def _onDetach(self, event): self._genericCB(event, 'detach') def _onError(self, event): log.debug(event.description) log.verbose("{} event received".format(type)) for cb in self.callback['error']: cb(event) def _onOutput(self, event): log.verbose("Output event received") for cb in self.callback['output']: cb(event) try: for cb in self.output[event.index].callback['value']: cb(event) except KeyError: pass def _onInput(self, event): log.verbose("Input event received") for cb in self.callback['input']: cb(event) try: for cb in self.input[event.index].callback['value']: cb(event) except KeyError: pass def _onSensor(self, event): log.verbose("Sensor event received") for cb in self.callback['sensor']: cb(event) try: for cb in self.sensor[event.index].callback['value']: cb(event) except KeyError: pass
interfaceKit.setOnAttachHandler(inferfaceKitAttached) interfaceKit.setOnDetachHandler(interfaceKitDetached) interfaceKit.setOnErrorhandler(interfaceKitError) interfaceKit.setOnInputChangeHandler(interfaceKitInputChanged) interfaceKit.setOnOutputChangeHandler(interfaceKitOutputChanged) interfaceKit.setOnSensorChangeHandler(interfaceKitSensorChanged) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Opening phidget relay board....") try: interfaceKit.openRemoteIP("192.168.128.2", port=5001, serial=SERIAL_NUM) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: interfaceKit.waitForAttach(10000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: interfaceKit.closePhidget() except PhidgetException as e: