def remote_set_channel(remote,chan): global this_node_id log.debug("remote_set_channel(nodeid %d @ chan %d)",remote,chan) control = 0x21 mesh.send([control,mesh.pid["exec_cmd"],this_node_id,remote,mesh.exec_cmd["set_channel"],remote,chan]) loop(2) return
def ping(target_node): global this_node_id log.debug("msg > ping %d -> %d ",this_node_id,target_node) control = 0x70 mesh.send([control,mesh.pid["ping"],this_node_id,target_node]) loop(2) return
def rov_bldc(alpha, norm): global this_node_id target_node = int(config["bldc"]["nodeid"]) log.debug(f"msg > bldc from {this_node_id} -> {target_node} set alpha = {alpha} ; norm = {norm}") control = 0x70 if(norm > 1): norm = 1 byte_alpha = int(alpha) byte_norm = int(norm * 255) mesh.send([control,mesh.pid["bldc"],this_node_id,target_node,byte_alpha,byte_norm]) sleep(0.002)#workaround to slow down as UART dongle can't handle 2 successive messages return
def mqtt_on_message(client, userdata, msg): log.debug(f"mqtt> topic {msg.topic}") topics = msg.topic.split("/") if((len(topics) == 3) and (topics[0] == "mesh")): linejson = "" json_payload = json.loads(msg.payload) for key,val in json_payload.items(): linejson = linejson + str(key) + ':' + str(val) + ';' message = topics[1]+'/'+topics[2]+'>'+linejson+"\r\n" print("message >>>>> ", message) mesh.send(message) return
def serial_send_brightness_all(brightness): bri = int(brightness) size = 0x07 control = 0x71 pid = 0x0D src = config["light"]["node_src"] dst = config["light"]["node_dst"] brightness_high = bri / 256 brightness_low = bri % 256 mesh.send([ size,control,mesh.pid["dimmer"],src,dst,int(brightness_high),int(brightness_low)]) log.info("send brightness to all channels %d ",bri) return
def mesh_do_action(cmd,remote,params): control = 0x71 try: if(cmd == "dimmer"): #TODO log.info("action> dimmer TODO") elif(cmd == "ping"): mesh.send([ control,mesh.pid["ping"],this_node_id,int(remote)]) except KeyError: log.error("mqtt_remote_req > KeyError Exception for %s",cmd) except ValueError: log.error("mqtt_remote_req > ValueError Exception for %s , '%s'",cmd, remote) return
def off_callback(): size = 0x07 control = 0x72 pid = 0x09 heat = 0 duration = 0 src = config["heat"]["node_src"] dst = config["heat"]["node_dst"] mesh.send([ size,control,mesh.pid["heat"],src,dst,int(heat),int(duration)]) if(config["mqtt"]["publish"]): topic = "Nodes/"+str(config["heat"]["node_src"])+"/heat" payload = int(heat) clientMQTT.publish(topic,payload) return
def serial_send_heat_duration(heat,duration_mn): size = 0x07 control = 0x72 pid = 0x09 src = config["heat"]["node_src"] dst = config["heat"]["node_dst"] mesh.send([ size,control,mesh.pid["heat"],src,dst,int(heat),int(duration_mn)]) if(config["mqtt"]["publish"]): topic = "Nodes/"+str(config["heat"]["node_src"])+"/heat" payload = int(heat) clientMQTT.publish(topic,payload) threading.Timer(duration_mn*60, off_callback).start() log.info("send heat %s during %s minutes",heat, duration_mn) return
def remote_execute_command(cmd,params): control = 0x21 try: if(cmd == "set_channel"): mesh.send([ control,mesh.pid["exec_cmd"],this_node_id,int(params["remote"]), mesh.exec_cmd[cmd],int(params["channel"])] ) elif(cmd == "get_channel"): mesh.send([ control,mesh.pid["exec_cmd"],this_node_id,int(params["remote"]), mesh.exec_cmd[cmd]] ) else: return False except (KeyError,TypeError): log.error("mqtt_remote_req > Error Exception for %s",cmd) return True
def time_sync(): mesh.send([0x80,mesh.pid["sync-prepare"],this_node_id]) sleep(0.020)#allow main to execute and set is_expecting_sync mesh.send([0x80,mesh.pid["sync"],this_node_id]) return