コード例 #1
0
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()
コード例 #2
0
ファイル: oasisunit.py プロジェクト: janredl/turhouse
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)
コード例 #3
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()
コード例 #4
0
ファイル: doors.py プロジェクト: mhaken/OpeningDoors
"""
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")
コード例 #5
0
ファイル: oasisunit.py プロジェクト: janredl/turhouse
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)
コード例 #6
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:
     print("SENDING:", c)
 device.send_command(c)
 r = reader.next()
コード例 #7
0
to clear the current memory and then enter ID of first empty slot.

Auto-detects RC-86K and calculates its second address.

End with pressing Ctrl+C or by entering any non-number during programming.
"""

from device import Device

if __name__ == "__main__":
    device = Device(device="/dev/ttyUSB0")
    reader = device.gen_lines()

    # print current status
    for i in range(0, 32):
        device.send_command("GET SLOT:%02d" % i)
        print reader.next()

    slot_id = 0
    # optional erasing
    erase_all = raw_input("Erase all slots first? [y/N] ")
    if erase_all.lower() == "y":
        print "Erasing all slots..."
        device.send_command("ERASE ALL SLOTS")
        print reader.next()
    else:
        # custom first slot ID
        start = raw_input("Where do you want to start programming?\n"
                          "Enter number of the first slot or anything else "
                          "for programming from slot #00: ")
        try: