Skip to content

Commit

Permalink
Merge pull request #33 from wangshub/master
Browse files Browse the repository at this point in the history
feat(aelosedu): auto detect port and redefine topic name
  • Loading branch information
wwj718 committed Jul 5, 2019
2 parents 55330a2 + ca3bba3 commit da61563
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions extension_leju_aelosedu.py
Expand Up @@ -6,20 +6,28 @@
import logging
logger = logging.getLogger(__name__)

SERIAL_DEVICE = '/dev/tty.wchusbserial14110'
CHANNEL = 0x42


def say(word):
subprocess.call("say {}".format(word), shell=True)


class Dongle2401:
def __init__(self, port, channel):
self.port = port
self.channel = channel
def __init__(self):
self.id = '1A86:7523'
self.port = self.auto_detect()
self.dongle = self.open_port(self.port)
# self.set_channel(channel)

def auto_detect(self):
ports = serial.tools.list_ports.comports()
for port, desc, hwid in sorted(ports):
# print("port = {}; desc = {} ;hwid = {}".format(port, desc, hwid))
if self.id in hwid:
try:
with serial.Serial(port, 9600) as ser:
ser.close()
print('found port {}'.format(port))
return port
except Exception as err:
pass
# print('open port failed', port, err)

assert False, 'Aelos usb dongle not found!'

def open_port(self, port):
return serial.Serial(port, 9600)
Expand All @@ -28,41 +36,46 @@ def send(self, data):
self.dongle.write(bytes(data))

def set_channel(self, channel):
self.send([0xcc, 0xcc, 0xcc, 0xcc, 0xcc])
time.sleep(0.5)
self.send([0x29, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, channel])


def is_positive_int(s):
def is_positive_valid(s):
try:
num = int(s)
return True if num > 0 else False
if 0 < num < 255:
return True
else:
return False
except ValueError:
return False


def parse_cmd(payload):
cmd = 0
if payload.startswith('leju_aelos_cmd_'):
msg = payload.split('_')
if is_positive_int(msg[-1]):
cmd = int(msg[-1])
if is_positive_valid(payload):
cmd = int(payload)
return cmd


dongle = Dongle2401(SERIAL_DEVICE, CHANNEL)


class LejuAelosRobotExtention(Extension):
def __init__(self):
name = type(self).__name__
super().__init__(name)

def run(self):
dongle = Dongle2401()
while True:
message = self.read()
if message["topic"] == "eim":
if message["topic"] == "leju/aelos/action":
action_num = parse_cmd(message.get('payload'))
logger.info(action_num)
dongle.send([action_num])
if message["topic"] == "leju/aelos/channel":
action_num = parse_cmd(message.get('payload'))
logger.info(action_num)
dongle.set_channel(action_num)


export = LejuAelosRobotExtention

0 comments on commit da61563

Please sign in to comment.