コード例 #1
0
ファイル: terrariumSwitch.py プロジェクト: h1kkan/TerrariumPI
  def set_state(self, state, force = False):
    if self.get_state() is not state or force:
      if self.get_hardware_type() == 'ftdi':
        try:
          if 'BitBang' == self.device_type:
            with BitBangDevice(self.device) as device:
              device.baudrate = 9600
              if state is terrariumSwitch.ON:
                device.port |= int(terrariumSwitch.BITBANG_ADDRESSES[str(self.get_address())], 16)
              else:
                device.port &= ~int(terrariumSwitch.BITBANG_ADDRESSES[str(self.get_address())], 16)
              device.close()

          elif 'Serial' == self.device_type:
            with SerialDevice(self.device) as device:
              device.baudrate = 9600
              cmd = chr(0xff) + chr(0x0 + int(self.get_address())) + chr(0x0 + (1 if state is terrariumSwitch.ON else 0))
              device.write(cmd)
              device.close()

        except Exception, err:
          # Ignore for now
          pass

      elif self.get_hardware_type() == 'eg-pm-usb':
        address = int(self.sensor_address) % 4
        if address == 0:
          address = 4

        logger.debug('Change remote Energenie USB power switch nr %s, on device nr %s, to state %s' % (address,self.device,state))
        subprocess.call(['/usr/bin/sispmctl', '-d',str(self.device),('-o' if state is terrariumSwitch.ON else '-f'),str(address)],stdout=open(os.devnull, 'w'), stderr=subprocess.STDOUT)

      elif self.get_hardware_type() == 'eg-pm-lan':
        if self.device is None:
          logger.error('Energenie LAN device is not connected. Cannot trigger power switch')
        else:
          data = re.match(r"^http:\/\/((?P<passwd>[^@]+)@)?(?P<host>[^#\/]+)(\/)?#(?P<switch>[1-4])$",self.sensor_address)
          if data:
            address = int(data.group('switch')) % 4
            if address == 0:
              address = 4

            logger.debug('Change remote Energenie LAN power switch nr %s to state %s' % (address,state))

            try:
              webstatus = self.device.getstatus()
              if webstatus['login'] == 1:
                logger.debug('Logged in at remote Energenie LAN power switch  %s' % (self.sensor_address,))
                if self.device.login():
                  webstatus = self.device.getstatus()

              if webstatus['login'] == 0:
                self.device.changesocket(address, ( 1 if state is terrariumSwitch.ON else 0 ))
                self.device.logout()
              else:
                logger.error('Could not login to the Energenie LAN device %s at location %s. Error status %s(%s)' % (self.get_name(),self.sensor_address,webstatus['logintxt'],webstatus['login']))
            except Exception, ex:
              logger.exception('Could not login to the Energenie LAN device %s at location %s. Error status %s' % (self.get_name(),self.sensor_address,ex))
コード例 #2
0
  def set_hardware_state(self, state, force = False):
    if 'BitBang' == self.__device_type:
      with BitBangDevice(self.__device) as device:
        device.baudrate = 9600
        if state is terrariumPowerSwitch.ON:
          device.port |= int(terrariumPowerSwitchFTDI.BITBANG_ADDRESSES[str(self.get_address())], 16)
        else:
          device.port &= ~int(terrariumPowerSwitchFTDI.BITBANG_ADDRESSES[str(self.get_address())], 16)
        device.close()

    elif 'Serial' == self.__device_type:
      with SerialDevice(self.__device) as device:
        device.baudrate = 9600
        cmd = chr(0xff) + chr(0x0 + int(self.get_address())) + chr(0x0 + (1 if state is terrariumPowerSwitch.ON else 0))
        device.write(cmd)
        device.close()
コード例 #3
0
  def _set_hardware_value(self, state):
    (device, device_type) = self.device

    if device_type == terrariumRelayFTDI.SERIAL:
      with SerialDevice(device) as device:
        device.baudrate = 9600
        cmd = chr(0xff) + chr(0x0 + self._address[0]) + chr(0x0 + (1 if state == self.ON else 0))
        device.write(cmd)

    elif device_type == terrariumRelayFTDI.BITBANG:
      with BitBangDevice(device) as device:
        device.baudrate = 9600
        if state == self.ON:
          device.port |=  int(terrariumRelayFTDI.BITBANG_ADDRESSES[str(self._address[0])], 16)
        else:
          device.port &= ~int(terrariumRelayFTDI.BITBANG_ADDRESSES[str(self._address[0])], 16)

    return True
コード例 #4
0
    def set_state(self, state, force=False):
        if self.get_state() is not state or force:
            if self.get_hardware_type() == 'ftdi':
                try:
                    if 'BitBang' == self.device_type:
                        with BitBangDevice(self.device) as device:
                            device.baudrate = 9600
                            if state is terrariumSwitch.ON:
                                device.port |= int(
                                    terrariumSwitch.bitbang_addresses[str(
                                        self.get_address())], 16)
                            else:
                                device.port &= ~int(
                                    terrariumSwitch.bitbang_addresses[str(
                                        self.get_address())], 16)
                            device.close()

                    elif 'Serial' == self.device_type:
                        with SerialDevice(self.device) as device:
                            device.baudrate = 9600
                            cmd = chr(0xff) + chr(
                                0x0 + int(self.get_address())) + chr(0x0 + (
                                    1 if state is terrariumSwitch.ON else 0))
                            device.write(cmd)
                            device.close()

                except Exception, err:
                    # Ignore for now
                    print err
                    pass

            elif self.get_hardware_type() == 'gpio':
                GPIO.output(
                    int(self.get_address()),
                    (GPIO.HIGH if state is terrariumSwitch.ON else GPIO.LOW))

            self.state = state
            logger.info('Toggle switch \'%s\' from %s', self.get_name(),
                        ('off to on' if self.is_on() else 'on to off'))
            if self.callback is not None:
                self.callback(self.get_data())
コード例 #5
0
class LoopbackTester(object):
    def __init__(self):
        self.device = SerialDevice(chunk_size=16)

    def test_loopback(self, l):
        test_str = test_string(l)
        if self.device.write(test_str) != len(test_str):
            sys.stdout.write('*')
        time.sleep(0.1)
        result = ''
        for _ in range(5):
            result = self.device.read(l)
            time.sleep(0.1)
            if result:
                break
        if result != test_str:
            self.device.flush()
            time.sleep(0.25)
        return result == test_str

    def test_iter(self, lengths):
        self.device.flush()
        time.sleep(0.1)
        for l in lengths:
            yield self.test_loopback(l)

    def bisect(self):
        xmin, xmax = 1, 5000
        last_test = None
        while True:
            test = (xmin + xmax) // 2
            if test == last_test:
                break
            if self.test_loopback(test):
                xmin = test
            else:
                xmax = test
            last_test = test
        return test

    def main(self):
        print("Determining largest non-streamed buffer size")
        for bd in [9600, 31250, 115200, 1152000]:
            print("Baudrate: {}".format(bd))
            self.device.baudrate = bd
            result = self.bisect()
            print("Buffer size: {}".format(result))
コード例 #6
0
 def __init__(self):
     self.device = SerialDevice(chunk_size=16)
コード例 #7
0
  def set_state(self, state, force = False):
    if self.get_state() is not state or force:
      if self.get_hardware_type() == 'ftdi':
        try:
          if 'BitBang' == self.device_type:
            with BitBangDevice(self.device) as device:
              device.baudrate = 9600
              if state is terrariumSwitch.ON:
                device.port |= int(terrariumSwitch.BITBANG_ADDRESSES[str(self.get_address())], 16)
              else:
                device.port &= ~int(terrariumSwitch.BITBANG_ADDRESSES[str(self.get_address())], 16)
              device.close()

          elif 'Serial' == self.device_type:
            with SerialDevice(self.device) as device:
              device.baudrate = 9600
              cmd = chr(0xff) + chr(0x0 + int(self.get_address())) + chr(0x0 + (1 if state is terrariumSwitch.ON else 0))
              device.write(cmd)
              device.close()

        except Exception, err:
          # Ignore for now
          pass

      elif self.get_hardware_type() == 'eg-pm-usb':
        address = int(self.sensor_address) % 4
        if address == 0:
          address = 4

        logger.debug('Change remote Energenie USB power switch nr %s, on device nr %s, to state %s' % (address,self.device,state))
        subprocess.call(['/usr/bin/sispmctl', '-d',str(self.device),('-o' if state is terrariumSwitch.ON else '-f'),str(address)],stdout=open(os.devnull, 'w'), stderr=subprocess.STDOUT)

      elif self.get_hardware_type() == 'eg-pm-lan':
        if self.device is None:
          logger.error('Energenie LAN device is not connected. Cannot trigger power switch')
        else:
          data = re.match(r"^http:\/\/((?P<passwd>[^@]+)@)?(?P<host>[^#\/]+)(\/)?#(?P<switch>[1-4])$",self.sensor_address)
          if data:
            address = int(data.group('switch')) % 4
            if address == 0:
              address = 4

            logger.debug('Change remote Energenie LAN power switch nr %s to state %s' % (address,state))

            webstatus = self.device.getstatus()
            if webstatus['login'] == 1:
              logger.debug('Logged in at remote Energenie LAN power switch  %s' % (self.sensor_address,))
              if self.device.login():
                webstatus = self.device.getstatus()

            if webstatus['login'] == 0:
              self.device.changesocket(address, ( 1 if state is terrariumSwitch.ON else 0 ))
              self.device.logout()
            else:
              logger.error('Could not login to the Energenie LAN device %s at location %s. Error status %s(%s)' % (self.get_name(),self.sensor_address,webstatus['logintxt'],webstatus['login']))

      elif self.get_hardware_type() == 'gpio':
        GPIO.output(terrariumUtils.to_BCM_port_number(self.get_address()), ( GPIO.HIGH if state is terrariumSwitch.ON else GPIO.LOW ))

      elif self.get_hardware_type() == 'gpio-inverse':
        GPIO.output(terrariumUtils.to_BCM_port_number(self.get_address()), ( GPIO.LOW if state is terrariumSwitch.ON else GPIO.HIGH ))

      elif self.get_hardware_type() == 'pwm-dimmer' and self.__pigpio is not False:
        duration = self.get_dimmer_duration()
        # State 100 = full on which means 0 dim.
        # State is inverse of dim
        if state is terrariumSwitch.ON:
          state = self.get_dimmer_on_percentage()
          duration = self.get_dimmer_on_duration()
        elif state is terrariumSwitch.OFF or not (0 <= state <= 100):
          state = self.get_dimmer_off_percentage()
          duration = self.get_dimmer_off_duration()

        thread.start_new_thread(self.__dim_switch, (self.state,state,duration))
      elif 'remote' in self.get_hardware_type():
        # Not yet implemented
        pass

      self.state = state
      if self.get_hardware_type() != 'pwm-dimmer':
        logger.info('Toggle switch \'%s\' from %s',self.get_name(),('off to on' if self.is_on() else 'on to off'))
      if self.callback is not None:
        data = self.get_data()
        self.callback(data)
コード例 #8
0
ファイル: pyl08.py プロジェクト: Noah1989/pyl08
from pylibftdi import USB_PID_LIST, SerialDevice
from ctypes import byref

USB_PID_LIST.append(0x6015)
BITMODE_CBUS = 0x20


dev = SerialDevice()

dev.baudrate = 46875

# programming voltage
dev.rts = 1

# reset
dev.ftdi_fn.ftdi_set_bitmode(0x40, BITMODE_CBUS)

dev.close()
コード例 #9
0
    def set_state(self, state, force=False):
        if self.get_state() is not state or force:
            if self.get_hardware_type() == 'ftdi':
                try:
                    if 'BitBang' == self.device_type:
                        with BitBangDevice(self.device) as device:
                            device.baudrate = 9600
                            if state is terrariumSwitch.ON:
                                device.port |= int(
                                    terrariumSwitch.BITBANG_ADDRESSES[str(
                                        self.get_address())], 16)
                            else:
                                device.port &= ~int(
                                    terrariumSwitch.BITBANG_ADDRESSES[str(
                                        self.get_address())], 16)
                            device.close()

                    elif 'Serial' == self.device_type:
                        with SerialDevice(self.device) as device:
                            device.baudrate = 9600
                            cmd = chr(0xff) + chr(
                                0x0 + int(self.get_address())) + chr(0x0 + (
                                    1 if state is terrariumSwitch.ON else 0))
                            device.write(cmd)
                            device.close()

                except Exception, err:
                    # Ignore for now
                    pass

            elif self.get_hardware_type() == 'gpio':
                GPIO.output(
                    int(self.get_address()),
                    (GPIO.HIGH if state is terrariumSwitch.ON else GPIO.LOW))

            elif self.get_hardware_type() == 'gpio-inverse':
                GPIO.output(
                    int(self.get_address()),
                    (GPIO.LOW if state is terrariumSwitch.ON else GPIO.HIGH))

            elif self.get_hardware_type(
            ) == 'pwm-dimmer' and self.__pigpio is not False:
                duration = self.get_dimmer_duration()
                # State 100 = full on which means 0 dim.
                # State is inverse of dim
                if state is terrariumSwitch.ON:
                    state = self.get_dimmer_on_percentage()
                    duration = self.get_dimmer_on_duration()
                elif state is terrariumSwitch.OFF or not (0 <= state <= 100):
                    state = self.get_dimmer_off_percentage()
                    duration = self.get_dimmer_off_duration()

                thread.start_new_thread(self.__dim_switch,
                                        (self.state, state, duration))
            elif 'remote' in self.get_hardware_type():
                # Not yet implemented
                pass

            self.state = state
            if self.get_hardware_type() != 'pwm-dimmer':
                logger.info('Toggle switch \'%s\' from %s', self.get_name(),
                            ('off to on' if self.is_on() else 'on to off'))
            if self.callback is not None:
                data = self.get_data()
                self.callback(data)