class GadgetsConnector(Daemon): ''' daemonized token connector ''' def __init__(self, log_file=None): Daemon.__init__(self, '/tmp/turris-gadgets.pid') self.log = self.open_file(log_file) self.token = Device(device="/dev/ttyUSB0") self.reader = self.token.gen_lines(timeout=5) def open_file(self, filename): ''' file helper''' if filename: import os.path if os.path.exists(filename): file_mode = "a" else: file_mode = "w" return file(filename, file_mode) return None def send_raw(self, command): ''' send raw command to token ''' self.token.send_command(command) return self.reader.next() def whoami(self): ''' token info''' return self.send_raw('WHO AM I?') def getslot(self, index): ''' get gadget address registered on slot ''' str_i = str(index).zfill(2) reply = self.send_raw('GET SLOT:%s' % str_i) slot = re.search("^SLOT:%s \[(\d{8}|-{8})\]" % str_i, reply) return slot.group(1) def cmd_read(self, line): m = re.search("^\[(\d{8})\]\s([a-zA-Z0-9_-]+)\s(.+)", line) if m.group(): return [m.group(1), m.group(2), m.group(3)] return None def run(self): ''' only method called by daemon (start, restart) ''' if self.log: self.log.write("%s\n" % self.whoami()) for i in range(0, 32): self.log.write("SLOT: %s - %s\n" % (i, self.getslot(i))) self.log.flush()
""" Simple example receiving messages from the dongle and passing them to a ZeroMQ for deliver. May be used to distribute messages to several subscribers. """ import datetime import sys import zmq # external dependency from device import Device if __name__ == "__main__": context = zmq.Context() publisher = context.socket(zmq.PUB) publisher.bind("tcp://127.0.0.1:33224") if len(sys.argv) <= 1: device_name = "/dev/ttyUSB0" else: device_name = sys.argv[1] device = Device(device_name) reader = device.gen_lines() while True: line = reader.next().decode( 'iso-8859-2') # there might be the degree symbol date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") publisher.send_json((date, line)) print line.encode('utf-8')
class OasisUnit(object): ''' class representing one oasis unit (turris dongle) ''' def __init__(self, unitName): ''' init ''' self.unitName = unitName self.queue = list() # pgx device self.pgx = None # pgy device self.pgy = None # beeper status self.beeper = 'NONE' self.device = None self.reader = None unitDevice = Glob.config.getUnitParams(unitName)['device'] try: self.device = Device(device=unitDevice) self.reader = self.device.gen_lines(0.05) except Exception as e: Glob.logger.error( 'Unable to open device %s (unit: %s): %s' % (unitDevice, unitName, str(e))) def alarm(self): self.beeper = 'ALARM' self.sendStatus() def beepSlow(self): self.beeper = 'SLOW' self.sendStatus() def beepFast(self): self.beeper = 'FAST' self.sendStatus() def alarmStop(self): self.beeper = 'NONE' self.sendStatus() def sendStatus(self, repeat=1, delay=0): pgx = 0 pgy = 0 if self.pgx: pgx = self.pgx.getStatus() if self.pgy: pgy = self.pgy.getStatus() if self.beeper == 'ALARM': alarm = 1 beep = 'NONE' else: alarm = 0 beep = self.beeper cmd = 'TX ENROLL:0 PGX:%s PGY:%s ALARM:%s BEEP:%s' % ( pgx, pgy, alarm, beep) timestamp = time.time() + delay while True: self.queueCommand(cmd, timestamp) timestamp += 0.2 repeat -= 1 if repeat <= 0: break def queueCommand(self, cmd, timestamp): self.queue.append((cmd, timestamp)) def sendCommand(self, cmd): Glob.loggerOasis.info(cmd) self.device.send_command(cmd) def beep(self): st = self.beeper self.beeper = 'SLOW' self.sendStatus(0) self.beeper = st self.sendStatus(1, 0.4) def checkTimeout(self): ''' check if there is some timeouted command in the queue ''' if self.queue: cmd, timestamp = self.queue[0] if timestamp < time.time(): self.sendCommand(cmd) self.queue.pop(0)
""" Simple demo for sending commands to the TURRIS:DONGLE. It can be used for sensor registration, etc.:: >> python gadget_command.py "GET SLOT:01" SENDING: GET SLOT:01 REPLY: SLOT:01 [07473725] >> python gadget_command.py SET SLOT:03 [07439975] SENDING: SET SLOT:03 [07439975] REPLY: OK """ import sys from device import Device if __name__ == "__main__": device = Device(device="/dev/ttyUSB0") reader = device.gen_lines(timeout=20) for c in sys.argv[1:]: print "SENDING:", c device.send_command(c) print "REPLY:", reader.next()
""" Turris """ from __future__ import print_function import sys import datetime import time from device import Device if __name__ == "__main__": device_name = "/dev/ttyUSB0" device = Device(device="/dev/ttyUSB0") reader = device.gen_lines() dvere_open="[XXXXXXXX] JA-81M SENSOR LB:0 ACT:1" device = Device(device=device_name) reader = device.gen_lines() while True: line = reader.next() if line == dvere_open: device.send_command("TX ENROLL:0 PGX:0 PGY:1 ALARM:0 BEEP:NONE") time.sleep(5) device.send_command("TX ENROLL:0 PGX:0 PGY:0 ALARM:0 BEEP:NONE")
class OasisUnit(object): ''' class representing one oasis unit (turris dongle) ''' def __init__(self, unitName): ''' init ''' self.unitName = unitName self.queue = list() # pgx device self.pgx = None # pgy device self.pgy = None # beeper status self.beeper = 'NONE' self.device = None self.reader = None unitDevice = Glob.config.getUnitParams(unitName)['device'] try: self.device = Device(device=unitDevice) self.reader = self.device.gen_lines(0.05) except Exception as e: Glob.logger.error('Unable to open device %s (unit: %s): %s' % (unitDevice, unitName, str(e))) def alarm(self): self.beeper = 'ALARM' self.sendStatus() def beepSlow(self): self.beeper = 'SLOW' self.sendStatus() def beepFast(self): self.beeper = 'FAST' self.sendStatus() def alarmStop(self): self.beeper = 'NONE' self.sendStatus() def sendStatus(self, repeat=1, delay=0): pgx = 0 pgy = 0 if self.pgx: pgx = self.pgx.getStatus() if self.pgy: pgy = self.pgy.getStatus() if self.beeper == 'ALARM': alarm = 1 beep = 'NONE' else: alarm = 0 beep = self.beeper cmd = 'TX ENROLL:0 PGX:%s PGY:%s ALARM:%s BEEP:%s' % (pgx, pgy, alarm, beep) timestamp = time.time() + delay while True: self.queueCommand(cmd, timestamp) timestamp += 0.2 repeat -= 1 if repeat <= 0: break def queueCommand(self, cmd, timestamp): self.queue.append((cmd, timestamp)) def sendCommand(self, cmd): Glob.loggerOasis.info(cmd) self.device.send_command(cmd) def beep(self): st = self.beeper self.beeper = 'SLOW' self.sendStatus(0) self.beeper = st self.sendStatus(1, 0.4) def checkTimeout(self): ''' check if there is some timeouted command in the queue ''' if self.queue: cmd, timestamp = self.queue[0] if timestamp < time.time(): self.sendCommand(cmd) self.queue.pop(0)
else: cmd2 = "PGX:0" if pc2 == 0 or pc3 == 0: cmd3 = "PGY:1" else: cmd3 = "PGY:0" #-------------------------------------------------------------- if prx == 1: print("Zasuvky:",cmd2,cmd3) #------- sestaveni cmd line --------------------------------------------- c = cmd1 + " " + cmd2 + " " + cmd3 + " " + cmd4 #------- odeslani cmd line --------------------------------------------- device = Device(device="/dev/ttyUSB0") reader = device.gen_lines(timeout=20) if prx == 1: print("SENDING:", c) device.send_command(c) r = reader.next() if prx == 1: print("REPLY:",r) time.sleep(0.3) if prx == 1: print("SENDING:", c) device.send_command(c) r = reader.next() if prx == 1: print ("REPLY:",r) time.sleep(0.4) if prx == 1: