def run(self): while self._running: env_is_valid = check_env() # 等待用户连接microbit if not env_is_valid: self.logger.info("错误信息: %s", "请插入microbit") ui_error("错误信息", "请插入microbit") time.sleep(5) else: port = find_microbit() self.ser = serial.Serial(port, 115200, timeout=1) break def get_response_from_microbit(): try: data = self.ser.readline() # self.logger.info("in response") if data: data = data.decode() try: data = eval(data) except (ValueError, SyntaxError): pass else: # self.logger.info(data) return data except UnicodeDecodeError: pass return None lock = threading.Lock() def request(): while self._running: lock.acquire() self.scratch3_message = self.read() lock.release() bg_task = threading.Thread(target=request) self.logger.debug("thread start") bg_task.daemon = True bg_task.start() while self._running: # 一读一写 比较稳定 scratch3_message = self.scratch3_message self.logger.debug("scratch3_message {}".format(scratch3_message)) self.scratch3_message = {} if scratch3_message == {}: scratch3_message = {"topic": self.TOPIC, "data": ""} scratch3_message = json.dumps(scratch3_message) + "\r\n" scratch3_message_bytes = scratch3_message.encode('utf-8') self.logger.debug(scratch3_message_bytes) self.ser.write(scratch3_message_bytes) # response response_from_microbit = get_response_from_microbit() if response_from_microbit: # message = {"topic":self.TOPIC, "data":} # todo 不要在microbit中构建消息 self.publish(response_from_microbit) time.sleep(0.05)
def run(self): # 建立与arduino的连接 ser = self.connect_to_arduino() if not ser: ui_error("error", "无法连接arduino") while self._running: message = self.read( ) # todo 停止插件后,这里仍可能处于阻塞,以至于ser.close()无法释放(需要前端再多发一条消息才能跳出while) self.logger.debug(message) topic = message.get("topic") if topic == self.TOPIC: python_code = message.get('data') self.logger.debug("run python code:{}".format(message)) try: # 单一入口,传递源码 # 为了安全性, 做一些能力的牺牲, 放弃使用exec output = eval(python_code) except Exception as e: output = e message = { "topic": self.TOPIC, "message": str(output).rstrip() } self.publish(message) ser.close()
def run(self): try: from eim_script import handle # reload except Exception as e: ui_error('eim_script error', str(e)) return module = sys.modules["eim_script"] importlib.reload(module) while self._running: message = self.read() data = message.get('data') if data: # self.logger.debug("message:%s",str(message)) response = handle(data, self.logger) message = {"topic": "eim", "message": response} self.publish(message)
def run(self): try: from eim_monitor import monitor except Exception as e: ui_error('eim_script error', str(e)) return module = sys.modules["eim_monitor"] importlib.reload(module) while self._running: # monitor返回值被pub # rate = 10 # time.sleep(1/rate) # 默认频率是每秒运行十次这个函数 try: response = monitor() if response: message = {"topic": "eim", "message": response} self.publish(message) except: ui_error('eim_script error', str(e))
def run(self): while self._running: ''' 连接硬件 串口 ''' env_is_valid = check_env() # 等待用户连接microbit if not env_is_valid: self.logger.info("错误信息: %s", "请插入ubtrobot") ui_error("错误信息", "请连接ubtrobot") time.sleep(5) else: try: port = find_ubtrobot() self.ser = serial.Serial(port, 115200, timeout=1) # 9600 break except OSError: self.logger.info("错误信息: %s", "请插入ubtrobot") ui_error("错误信息", "请连接ubtrobot") time.sleep(5) while self._running: ''' 订阅到的是什么消息 消息体是怎样的 ''' message = self.read() cmd_map = { "forward": [0xFB, 0xBF, 0x09, 0x03, 0xc7, 0xb0, 0xbd, 0xf8, 0x38, 0xED], "backward": [0xFB, 0xBF, 0x09, 0x03, 0xba, 0xf3, 0xcd, 0xcb, 0x51, 0xED], "left": [0xFB, 0xBF, 0x09, 0x03, 0xd7, 0xf3, 0xd7, 0xaa, 0x57, 0xED], "right": [ 0xFB, 0xBF, 0x09, 0x03, 0xd3, 0xd2, 0xd7, 0xaa, 0x32, 0xED ], "push ups": [ 0xFB, 0xBF, 0x0b, 0x03, 0xb8, 0xa9, 0xce, 0xd4, 0xb3, 0xc5, 0x89, 0xED ], "stop": [0xFB, 0xBF, 0x06, 0x05, 0x00, 0x0b, 0xED], "init": [ 0xFB, 0xBF, 0x0b, 0x03, 0xb3, 0xf5, 0xca, 0xbc, 0xbb, 0xaf, 0xa6, 0xED ], "happy birthday": [ 0xFB, 0xBF, 0x13, 0x03, 0x48, 0x61, 0x70, 0x70, 0x79, 0x20, 0x42, 0x69, 0x72, 0x74, 0x68, 0x64, 0x61, 0x79, 0x6f, 0xED ], "left punch": [ 0xFB, 0xBF, 0x0b, 0x03, 0xd7, 0xf3, 0xb3, 0xf6, 0xc8, 0xad, 0xf6, 0xED ] } self.logger.debug("message {}".format(message)) if message.get("topic") == "eim": cmd = cmd_map.get(message.get("data")) if cmd: self.logger.debug("cmd:{}".format(cmd)) # 如果是linux每次都要重连 if platform.system() == "Linux": port = find_linux_port() self.ser = serial.Serial(port, 115200, timeout=1) self.ser.write(cmd)