def senddata(self, idx, sensortype, value, userssi=-1, usebattery=-1, tasknum=-1, changedvalue=-1): # called by plugin if self.enabled and self.initialized and self.enablesend: if tasknum is None: return False if int(idx) > 0: if Settings.Tasks[ tasknum].remotefeed == False: # do not republish received values dp2 = p2pbuffer.data_packet() dp2.sensordata["sunit"] = Settings.Settings["Unit"] dp2.sensordata["dunit"] = self.defaultunit dp2.sensordata["idx"] = idx if tasknum > -1: dp2.sensordata["pluginid"] = Settings.Tasks[ tasknum].pluginid else: dp2.sensordata["pluginid"] = 33 dp2.sensordata["valuecount"] = Settings.Tasks[ tasknum].valuecount for u in range(Settings.Tasks[tasknum].valuecount): dp2.sensordata["values"][u] = Settings.Tasks[ tasknum].uservar[u] dp2.encode(5) return self.lora.lorasend(dp2.buffer)
def timer_thirty_second(self): if self.enabled and self.initialized: if self.defaultdestination != "" and ( (time.time() - self.lastsysinfo) > self.sysinfoperiod): dp = p2pbuffer.data_packet() try: dp.infopacket["mac"] = self.bleserv.getaddress() except Exception as e: dp.infopacket["mac"] = "00:00:00:00:00:00" dp.infopacket["unitno"] = int(Settings.Settings["Unit"]) dp.infopacket["build"] = int(rpieGlobals.BUILD) dp.infopacket["name"] = Settings.Settings["Name"] dp.infopacket["type"] = int( rpieGlobals.NODE_TYPE_ID_RPI_EASY_STD) # CAPABILITIES byte: first bit 1 if able to send, second bit 1 if able to receive dp.infopacket["cap"] = self.getmode() dp.encode(1) self.bleclient.setdestination(self.defaultdestination) misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG, "Sending infopacket") success = False if self.bleclient.connect(): success = self.bleclient.writepayload(dp.buffer) reply = self.bleclient.readpayload() # read remote infos self.bleclient.disconnect() if reply: # print("repl",reply) # debug self.pkt_receiver(reply) # handle received infos self.lastsysinfo = time.time() # if success: # self.lastsysinfo = time.time() # else: # self.lastsysinfo = (time.time()-self.sysinfoperiod)+10 # retry in 10sec return True
def sendcommand(self,unitno,commandstr): if self.enabled and self.initialized and self.enablesend: dpc = p2pbuffer.data_packet() dpc.cmdpacket["sunit"] = Settings.Settings["Unit"] dpc.cmdpacket["dunit"] = unitno dpc.cmdpacket["cmdline"] = commandstr dpc.encode(7) return self.lora.lorasend(dpc.buffer)
def senddata(self, idx, sensortype, value, userssi=-1, usebattery=-1, tasknum=-1, changedvalue=-1): # called by plugin if self.enabled and self.initialized: # print(idx,value) # debug if tasknum is None: return False if int(idx) > 0: if Settings.Tasks[tasknum].remotefeed == False or Settings.Tasks[ tasknum].remotefeed == -1: # do not republish received values dp2 = p2pbuffer.data_packet() dp2.sensordata["sunit"] = Settings.Settings["Unit"] dp2.sensordata["dunit"] = self.defaultunit dp2.sensordata["idx"] = idx if tasknum > -1: dp2.sensordata["pluginid"] = Settings.Tasks[ tasknum].pluginid else: dp2.sensordata["pluginid"] = 33 dp2.sensordata["valuecount"] = Settings.Tasks[ tasknum].valuecount for u in range(Settings.Tasks[tasknum].valuecount): try: dp2.sensordata["values"][u] = Settings.Tasks[ tasknum].uservar[u] except: dp2.sensordata["values"].append( Settings.Tasks[tasknum].uservar[u]) dp2.encode(5) # print(dp2.buffer) # debug if self.directsend: un = getunitordfromnum( self.defaultunit ) # try direct send only if instructed to do so else: un = -1 self.bleclient.setdestination(self.defaultdestination) if un > -1: if (int(Settings.p2plist[un]["cap"]) & 2 ) == 2: # try only if endpoint is able to receive self.bleclient.setdestination( Settings.p2plist[un]["mac"]) # print("a2:",Settings.p2plist[un]["mac"]) success = self.bleclient.send(dp2.buffer) if success == False and un > -1: # print("retry",success,un) # debug self.bleclient.setdestination(self.defaultdestination) success = self.bleclient.send(dp2.buffer) return success
def sendcommand(self,unitno,commandstr): if self.enabled and self.initialized and self.enablesend: dpc = p2pbuffer.data_packet() dpc.cmdpacket["sunit"] = Settings.Settings["Unit"] dpc.cmdpacket["dunit"] = unitno # print("CMD:",commandstr) dpc.cmdpacket["cmdline"] = commandstr dpc.encode(7) # print("RPIEasy buffer:",dpc.buffer) # debug if self.serdev is not None: try: self.serdev.write(dpc.buffer) except Exception as e: print(e)
def sendsysinfo(self): if self.enabled and self.initialized and self.enablesend: dp = p2pbuffer.data_packet() dp.infopacket["mac"] = self.mac dp.infopacket["unitno"] = int(Settings.Settings["Unit"]) dp.infopacket["build"] = int(rpieGlobals.BUILD) dp.infopacket["name"] = Settings.Settings["Name"] dp.infopacket["type"] = int(rpieGlobals.NODE_TYPE_ID_RPI_EASY_STD) # CAPABILITIES byte: first bit 1 if able to send, second bit 1 if able to receive dp.infopacket["cap"] = int(CAPABILITY_BYTE) dp.encode(1) # print(dp.buffer) # debug self.lastsysinfo = time.time() return True return False
def sendcommand(self, unitno, commandstr): dpc = p2pbuffer.data_packet() dpc.cmdpacket["sunit"] = Settings.Settings["Unit"] dpc.cmdpacket["dunit"] = unitno dpc.cmdpacket["cmdline"] = commandstr dpc.encode(7) un = getunitordfromnum(unitno) # try direct send anyway if un == -1: self.bleclient.setdestination(self.defaultdestination) else: self.bleclient.setdestination(Settings.p2plist[un]["mac"]) success = self.bleclient.send(dpc.buffer) if success == False and un > -1: self.bleclient.setdestination(self.defaultdestination) success = self.bleclient.send(dpc.buffer) return success
def onReadRequest(self, offset, callback): dp = p2pbuffer.data_packet() try: dp.infopacket["mac"] = self.addressfunc() except Exception as e: dp.infopacket["mac"] = "00:00:00:00:00:00" dp.infopacket["unitno"] = int(Settings.Settings["Unit"]) dp.infopacket["build"] = int(rpieGlobals.BUILD) dp.infopacket["name"] = Settings.Settings["Name"] dp.infopacket["type"] = int(rpieGlobals.NODE_TYPE_ID_RPI_EASY_STD) # CAPABILITIES byte: first bit 1 if able to send, second bit 1 if able to receive dp.infopacket["cap"] = self.modefunc() dp.encode(1) # data = array.array('B',[0]*64) data = list(base64.b64encode(dp.buffer[offset:])) # print(offset,data[offset:]) callback(Characteristic.RESULT_SUCCESS, data)
def sendsysinfo(self): if self.enabled and self.initialized and self.enablesend: dp = p2pbuffer.data_packet() try: defdev = Settings.NetMan.getprimarydevice() except: defdev = -1 if defdev != -1: dp.infopacket["mac"] = Settings.NetworkDevices[defdev].mac else: dp.infopacket["mac"] = "00:00:00:00:00:00" dp.infopacket["unitno"] = int(Settings.Settings["Unit"]) dp.infopacket["build"] = int(rpieGlobals.BUILD) dp.infopacket["name"] = Settings.Settings["Name"] dp.infopacket["type"] = int(rpieGlobals.NODE_TYPE_ID_RPI_EASY_STD) # CAPABILITIES byte: first bit 1 if able to send, second bit 1 if able to receive dp.infopacket["cap"] = int(CAPABILITY_BYTE) dp.encode(1) return self.lora.lorasend(dp.buffer) return False
def senddata(self,idx,sensortype,value,userssi=-1,usebattery=-1,tasknum=-1,changedvalue=-1): # called by plugin if self.enabled and self.initialized and self.enablesend: if int(idx)>0: if int(Settings.Tasks[tasknum].remotefeed) < 1: # do not republish received values dp2 = p2pbuffer.data_packet() dp2.sensordata["sunit"] = Settings.Settings["Unit"] dp2.sensordata["dunit"] = self.defaultunit dp2.sensordata["idx"] = idx if tasknum>-1: dp2.sensordata["pluginid"] = Settings.Tasks[tasknum].pluginid else: dp2.sensordata["pluginid"] = 33 dp2.sensordata["valuecount"] = Settings.Tasks[tasknum].valuecount for u in range(Settings.Tasks[tasknum].valuecount): dp2.sensordata["values"][u] = Settings.Tasks[tasknum].uservar[u] dp2.encode(5) # print(dp2.buffer) # debug if self.serdev is not None: try: self.serdev.write(dp2.buffer) except Exception as e: print(e)
def pkt_receiver(self, payload): # processing incoming packets # print(payload) # debug if self.enabled: dp = p2pbuffer.data_packet() pbuf = list(payload) if pbuf[0] == '/' or pbuf[0] == 47: # base64 encoded dp.buffer = base64.b64decode(payload.decode("utf-8")) else: # otherwise plain data arrived dp.buffer = payload dp.decode() # asking p2pbuffer library to decode it # print(dp.buffer) # debug if dp.pkgtype != 0: if dp.pkgtype == 1: # info packet received # print(dp.infopacket) if int(dp.infopacket["unitno"]) == int( Settings.Settings["Unit"]): # skip own messages return False un = getunitordfromnum(dp.infopacket["unitno"] ) # process incoming alive reports if un == -1: # CAPABILITIES byte: first bit 1 if able to send, second bit 1 if able to receive Settings.p2plist.append({ "protocol": "BLE", "unitno": dp.infopacket["unitno"], "name": dp.infopacket["name"], "build": dp.infopacket["build"], "type": dp.infopacket["type"], "mac": dp.infopacket["mac"], "lastseen": datetime.now(), "lastrssi": self.bleserv.getrssi(), "cap": dp.infopacket["cap"] }) misc.addLog( rpieGlobals.LOG_LEVEL_INFO, "New BLE unit discovered: " + str(dp.infopacket["unitno"]) + " " + str(dp.infopacket["name"])) Settings.p2plist.sort(reverse=False, key=self.nodesort) else: misc.addLog( rpieGlobals.LOG_LEVEL_DEBUG, "Unit alive: " + str(dp.infopacket["unitno"])) if Settings.p2plist[un]["type"] == 0: Settings.p2plist[un]["name"] = dp.infopacket[ "name"] Settings.p2plist[un]["build"] = dp.infopacket[ "build"] Settings.p2plist[un]["type"] = dp.infopacket[ "type"] Settings.p2plist[un]["mac"] = dp.infopacket["mac"] Settings.p2plist[un]["cap"] = dp.infopacket["cap"] Settings.p2plist[un]["lastseen"] = datetime.now() Settings.p2plist[un][ "lastrssi"] = self.bleserv.getrssi() elif dp.pkgtype == 5: # process incoming data if int(dp.sensordata["sunit"]) == int( Settings.Settings["Unit"]): return False un = getunitordfromnum(dp.sensordata["sunit"]) if un > -1: # refresh lastseen data Settings.p2plist[un]["lastseen"] = datetime.now() Settings.p2plist[un][ "lastrssi"] = self.bleserv.getrssi() else: Settings.p2plist.append({ "protocol": "BLE", "unitno": dp.sensordata["sunit"], "name": "", "build": 0, "type": 0, "mac": "", "lastseen": datetime.now(), "lastrssi": self.bleserv.getrssi(), "cap": 1 }) if (int(Settings.Settings["Unit"]) == int( dp.sensordata["dunit"] )) or (0 == int( dp.sensordata["dunit"] )): # process only if we are the destination or broadcast ltaskindex = -1 for x in range( 0, len(Settings.Tasks) ): # check if the sent IDX already exists? try: if (type(Settings.Tasks[x]) is not bool and Settings.Tasks[x]): if Settings.Tasks[x].controlleridx[ self.controllerindex] == int( dp.sensordata["idx"]): ltaskindex = x break except Exception as e: print(e) dvaluecount = int(dp.sensordata["valuecount"]) if rpieGlobals.VARS_PER_TASK < dvaluecount: # avoid possible buffer overflow dvaluecount = rpieGlobals.VARS_PER_TASK if ltaskindex < 0: # create new task if necessarry devtype = int(dp.sensordata["pluginid"]) m = False try: for y in range(len( rpieGlobals.deviceselector)): if int(rpieGlobals.deviceselector[y] [1]) == devtype: m = __import__( rpieGlobals.deviceselector[y][0]) break except: m = False TempEvent = None if m: try: TempEvent = m.Plugin(-1) except: TempEvent = None if True: ltaskindex = -1 for x in range( 0, len(Settings.Tasks) ): # check if there are free TaskIndex slot exists try: if (type(Settings.Tasks[x]) is bool): if Settings.Tasks[x] == False: ltaskindex = x break except: pass devtype = 33 # dummy device m = False try: for y in range( len(rpieGlobals.deviceselector)): if int(rpieGlobals.deviceselector[y] [1]) == devtype: m = __import__( rpieGlobals.deviceselector[y] [0]) break except: m = False if m: if ltaskindex < 0: ltaskindex = len(Settings.Tasks) try: Settings.Tasks[ltaskindex] = m.Plugin( ltaskindex) except: ltaskindex = len(Settings.Tasks) Settings.Tasks.append( m.Plugin(ltaskindex) ) # add a new device Settings.Tasks[ltaskindex].plugin_init( True) Settings.Tasks[ ltaskindex].remotefeed = True # Mark that this task accepts incoming data updates! Settings.Tasks[ltaskindex].enabled = True Settings.Tasks[ltaskindex].interval = 0 Settings.Tasks[ltaskindex].senddataenabled[ self.controllerindex] = True Settings.Tasks[ltaskindex].controlleridx[ self.controllerindex] = int( dp.sensordata["idx"]) if TempEvent is not None: Settings.Tasks[ ltaskindex].taskname = TempEvent.PLUGIN_NAME.replace( " ", "") for v in range(dvaluecount): Settings.Tasks[ ltaskindex].valuenames[ v] = TempEvent.valuenames[ v] Settings.Tasks[ ltaskindex].taskdevicepluginconfig[ 0] = TempEvent.vtype Settings.Tasks[ ltaskindex].vtype = TempEvent.vtype else: Settings.Tasks[ ltaskindex].taskname = Settings.Tasks[ ltaskindex].PLUGIN_NAME.replace( " ", "") Settings.Tasks[ ltaskindex].valuecount = dvaluecount Settings.savetasks() if ltaskindex < 0: return False misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG, "Sensordata update arrived from unit " + str(dp.sensordata["sunit"]) ) # save received values if Settings.Tasks[ltaskindex].remotefeed: for v in range(dvaluecount): Settings.Tasks[ltaskindex].set_value( v + 1, dp.sensordata["values"][v], False) Settings.Tasks[ltaskindex].plugin_senddata() elif (int(Settings.Settings["Unit"]) != int( dp.sensordata["dunit"]) ): # reroute if pkt is not for us if ( self.defaultunit != int(dp.sensordata["dunit"]) ) and ( self.enablesend or self.directsend ): # ... and not came from default target, and sending is enabled if self.directsend: un = getunitordfromnum( dp.sensordata["dunit"] ) # try direct send only if instructed to do so else: un = -1 self.bleclient.setdestination( self.defaultdestination) if un > -1: if ( int(Settings.p2plist[un]["cap"]) & 2 ) == 2: # try only if endpoint is able to receive self.bleclient.setdestination( Settings.p2plist[un]["mac"]) success = self.bleclient.send(dp.buffer) if success == False and un > -1: self.bleclient.setdestination( self.defaultdestination) success = self.bleclient.send(dp.buffer) elif dp.pkgtype == 7: # process incoming command if int(dp.cmdpacket["sunit"]) == int( Settings.Settings["Unit"]): return False un = getunitordfromnum(dp.cmdpacket["sunit"]) if un > -1: # refresh lastseen data Settings.p2plist[un]["lastseen"] = datetime.now() Settings.p2plist[un][ "lastrssi"] = self.bleserv.getrssi() else: Settings.p2plist.append({ "protocol": "BLE", "unitno": dp.cmdpacket["sunit"], "name": "", "build": 0, "type": 0, "mac": "", "lastseen": datetime.now(), "lastrssi": self.bleserv.getrssi(), "cap": 1 }) if (int(Settings.Settings["Unit"]) == int( dp.cmdpacket["dunit"] )) or (0 == int( dp.cmdpacket["dunit"] )): # process only if we are the destination or broadcast misc.addLog( rpieGlobals.LOG_LEVEL_INFO, "Command arrived from " + str(dp.cmdpacket["sunit"])) # print(dp.cmdpacket["cmdline"]) # DEBUG commands.doExecuteCommand(dp.cmdpacket["cmdline"], True)
def pkt_receiver(self,payload): if self.enabled: while len(payload)>0: dp = p2pbuffer.data_packet() dp.buffer = payload dp.decode() # print(dp.pktlen," T:",dp.pkgtype," FL:",len(payload)," PL:",dp.pktlen) # print("DATA ARRIVED ",payload,dp.buffer) if int(dp.pkgtype)!=0: if dp.pkgtype==1: if int(dp.infopacket["unitno"]) == int(Settings.Settings["Unit"]): # skip own messages return False un = getunitordfromnum(dp.infopacket["unitno"]) # process incoming alive reports if un==-1: # CAPABILITIES byte: first bit 1 if able to send, second bit 1 if able to receive Settings.p2plist.append({"protocol":"ESPNOW","unitno":dp.infopacket["unitno"],"name":dp.infopacket["name"],"build":dp.infopacket["build"],"type":dp.infopacket["type"],"mac":dp.infopacket["mac"],"lastseen":datetime.now(),"lastrssi":"","cap":dp.infopacket["cap"]}) misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"New ESPNOW unit discovered: "+str(dp.infopacket["unitno"])+" "+str(dp.infopacket["name"])) Settings.p2plist.sort(reverse=False,key=self.nodesort) else: misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"Unit alive: "+str(dp.infopacket["unitno"])) if Settings.p2plist[un]["type"]==0: Settings.p2plist[un]["name"] = dp.infopacket["name"] Settings.p2plist[un]["build"] = dp.infopacket["build"] Settings.p2plist[un]["type"] = dp.infopacket["type"] Settings.p2plist[un]["mac"] = dp.infopacket["mac"] Settings.p2plist[un]["cap"] = dp.infopacket["cap"] Settings.p2plist[un]["lastseen"] = datetime.now() elif dp.pkgtype==5: # process incoming data if int(dp.sensordata["sunit"])==int(Settings.Settings["Unit"]): return False un = getunitordfromnum(dp.sensordata["sunit"]) if un>-1: # refresh lastseen data Settings.p2plist[un]["lastseen"] = datetime.now() else: Settings.p2plist.append({"protocol":"ESPNOW","unitno":dp.sensordata["sunit"],"name":"","build":0,"type":0,"mac":"","lastseen":datetime.now(),"lastrssi":"","cap":1}) if (int(Settings.Settings["Unit"])==int(dp.sensordata["dunit"])) or (0==int(dp.sensordata["dunit"])): # process only if we are the destination or broadcast ltaskindex = -1 for x in range(0,len(Settings.Tasks)): # check if the sent IDX already exists? try: if (type(Settings.Tasks[x]) is not bool and Settings.Tasks[x]): if Settings.Tasks[x].controlleridx[self.controllerindex]==int(dp.sensordata["idx"]): ltaskindex = x break except Exception as e: print(e) dvaluecount = int(dp.sensordata["valuecount"]) if rpieGlobals.VARS_PER_TASK<dvaluecount: # avoid possible buffer overflow dvaluecount = rpieGlobals.VARS_PER_TASK if ltaskindex < 0: # create new task if necessarry devtype = int(dp.sensordata["pluginid"]) m = False try: for y in range(len(rpieGlobals.deviceselector)): if int(rpieGlobals.deviceselector[y][1]) == devtype: m = __import__(rpieGlobals.deviceselector[y][0]) break except: m = False TempEvent = None if m: try: TempEvent = m.Plugin(-1) except: TempEvent = None if True: ltaskindex = -1 for x in range(0,len(Settings.Tasks)): # check if there are free TaskIndex slot exists try: if (type(Settings.Tasks[x]) is bool): if Settings.Tasks[x]==False: ltaskindex = x break except: pass devtype = 33 # dummy device m = False try: for y in range(len(rpieGlobals.deviceselector)): if int(rpieGlobals.deviceselector[y][1]) == devtype: m = __import__(rpieGlobals.deviceselector[y][0]) break except: m = False if m: if ltaskindex<0: ltaskindex = len(Settings.Tasks) try: Settings.Tasks[ltaskindex] = m.Plugin(ltaskindex) except: ltaskindex = len(Settings.Tasks) Settings.Tasks.append(m.Plugin(ltaskindex)) # add a new device Settings.Tasks[ltaskindex].plugin_init(True) Settings.Tasks[ltaskindex].remotefeed = int(dp.sensordata["sunit"]) # True # Mark that this task accepts incoming data updates! Settings.Tasks[ltaskindex].enabled = True Settings.Tasks[ltaskindex].interval = 0 Settings.Tasks[ltaskindex].senddataenabled[self.controllerindex]=True Settings.Tasks[ltaskindex].controlleridx[self.controllerindex]=int(dp.sensordata["idx"]) if TempEvent is not None: Settings.Tasks[ltaskindex].taskname = TempEvent.PLUGIN_NAME.replace(" ","") for v in range(dvaluecount): Settings.Tasks[ltaskindex].valuenames[v] = TempEvent.valuenames[v] Settings.Tasks[ltaskindex].taskdevicepluginconfig[0] = TempEvent.vtype Settings.Tasks[ltaskindex].vtype = TempEvent.vtype else: Settings.Tasks[ltaskindex].taskname = Settings.Tasks[ltaskindex].PLUGIN_NAME.replace(" ","") Settings.Tasks[ltaskindex].valuecount = dvaluecount Settings.savetasks() if ltaskindex<0: return False misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"Sensordata update arrived from unit "+str(dp.sensordata["sunit"])) # save received values if Settings.Tasks[ltaskindex].remotefeed: for v in range(dvaluecount): Settings.Tasks[ltaskindex].set_value(v+1,dp.sensordata["values"][v],False) Settings.Tasks[ltaskindex].plugin_senddata() elif dp.pkgtype==7: # process incoming command if int(dp.cmdpacket["sunit"])==int(Settings.Settings["Unit"]): return False un = getunitordfromnum(dp.cmdpacket["sunit"]) if un>-1: # refresh lastseen data Settings.p2plist[un]["lastseen"] = datetime.now() else: Settings.p2plist.append({"protocol":"ESPNOW","unitno":dp.cmdpacket["sunit"],"name":"","build":0,"type":0,"mac":"","lastseen":datetime.now(),"lastrssi":"","cap":1}) if (int(Settings.Settings["Unit"])==int(dp.cmdpacket["dunit"])) or (0==int(dp.cmdpacket["dunit"])): # process only if we are the destination or broadcast misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"Command arrived from "+str(dp.cmdpacket["sunit"])) commands.doExecuteCommand(dp.cmdpacket["cmdline"],True) elif dp.pkgtype==8: # process incoming text/log if int(dp.cmdpacket["sunit"])==int(Settings.Settings["Unit"]): return False un = getunitordfromnum(dp.cmdpacket["sunit"]) if un>-1: # refresh lastseen data Settings.p2plist[un]["lastseen"] = datetime.now() else: Settings.p2plist.append({"protocol":"ESPNOW","unitno":dp.cmdpacket["sunit"],"name":"","build":0,"type":0,"mac":"","lastseen":datetime.now(),"lastrssi":"","cap":1}) if (int(Settings.Settings["Unit"])==int(dp.cmdpacket["dunit"])) or (0==int(dp.cmdpacket["dunit"])): # process only if we are the destination or broadcast misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"ESPNOW: "+str(dp.cmdpacket["cmdline"])) payload = payload[dp.pktlen:-1] else: payload = payload[1:-1]