def broadcastXplDirectControlBasic(internal, channel, b1, b2, device): fn_name = sys._getframe().f_code.co_name xplMsg = 0 xplcurrent = 0 xplsource = "" if (internal != 0): xplsource = "mea-internal.00000000" # destination interne (internal) sans attendre de reponse (00000000) else: xplsource = "me" xplMsg = mea_utils.xplMsgNew(xplsource, "*", "xpl-cmnd", "control", "basic") mea_utils.xplMsgAddValue(xplMsg, "type", "output") mea_utils.xplMsgAddValue(xplMsg, "device", device) if ((b1 == 0 or b2 == 0) and channel == "A"): # AI xplcurrent = "high" elif ((b1 == 1 or b2 == 1) and channel == "A"): # AO xplcurrent = "low" elif ((b1 == 2 or b2 == 2) and channel == "B"): # BI xplcurrent = "high" elif ((b1 == 3 or b2 == 3) and channel == "B"): # BO xplcurrent = "low" if (xplcurrent != 0): mea_utils.xplMsgAddValue(xplMsg, "current", xplcurrent) mea.xplSendMsg(xplMsg)
def mea_xplCmndMsg(data): fn_name=sys._getframe().f_code.co_name try: id_sensor=data["device_id"] except: verbose(2, "ERROR (", fn_name, ") - device_id not found") return 0 mem=mea.getMemory(id_sensor) x=data["xplmsg"] body=x["body"] target="*" if "source" in x: target=x["source"] if x["schema"]=="sensor.request": try: xplMsg=mea_utils.xplMsgNew("me", target, "xpl-stat", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg,"device", data["device_name"].lower()) if body["request"]=="current": mea_utils.xplMsgAddValue(xplMsg,"current",mem["current"]) elif body["request"]=="last": mea_utils.xplMsgAddValue(xplMsg,"last",mem["last"]) else: verbose(2, "ERROR (", fn_name, ") - invalid request (", body["request"],")") return False mea_utils.xplMsgAddValue(xplMsg,"type","input") mea.xplSendMsg(xplMsg) return True except: verbose(2, "ERROR (", fn_name, ") - can't create xpl message") return False return False
def mea_dataFromSensor(data): fn_name = __name__ + "/" + str(sys._getframe().f_code.co_name) try: device_id = data["device_id"] except: verbose(2, "ERROR (", fn_name, ") - device_id not found") return False try: device_name = data["device_name"] interface_id = data["interface_id"] parameters = data["device_parameters"] parameters = parameters.strip().lower() params = mea_utils.parseKeyValueDatasToDictionary(parameters, ",", ":") typeoftype = data["typeoftype"] api_key = data["api_key"] except: verbose(2, "ERROR (", fn_name, ") - invalid data") return False if not "value" in params: verbose(2, "ERROR (", fn_name, ") - no value parameter") return False value = params["value"] mem_device = mea.getMemory(device_id) mem_interface = mea.getMemory("interface" + str(interface_id)) pdata = mea_utils.parseKeyValueDatasToDictionary( str(data['data']).strip().lower(), ";", "=") if value in pdata: v = pdata[value] f = 0.0 if type(v) == str: try: f = float(v) except: return False mem_device["current"] = f mea.addDataToSensorsValuesTable(device_id, f, 0, 0, "") xplMsg = mea_utils.xplMsgNew("me", "*", "xpl-trig", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg, "device", device_name) mea_utils.xplMsgAddValue(xplMsg, "current", str(f)) mea.xplSendMsg(xplMsg) # mea.interfaceAPI(api_key, "mea_writeData", "{{{OK}}}") else: return -1
def mea_dataFromSensor(data): fn_name = __name__ + "/" + str(sys._getframe().f_code.co_name) try: device_id = data["device_id"] except: verbose(2, "ERROR (", fn_name, ") - device_id not found") return False try: device_name = data["device_name"] interface_id = data["interface_id"] parameters = data["device_parameters"] parameters = parameters.strip().lower() params = mea_utils.parseKeyValueDatasToDictionary(parameters, ",", ":") typeoftype = data["typeoftype"] api_key = data["api_key"] except: verbose(2, "ERROR (", fn_name, ") - invalid data") return False if not "value" in params: verbose(2, "ERROR (", fn_name, ") - no value parameter") return False value = params["value"] mem_device = mea.getMemory(device_id) mem_interface = mea.getMemory("interface" + str(interface_id)) pdata = mea_utils.parseKeyValueDatasToDictionary( str(data['data']).strip().lower(), ";", "=") if value in pdata: v = pdata[value].lower() f = 0 if type(v) == str: try: f = float(v) except: if v in ["on", "off", "high", "low", "true", "false"]: f = v else: return False xplMsg = mea_utils.xplMsgNew("me", "*", "xpl-trig", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg, "device", device_name) mea_utils.xplMsgAddValue(xplMsg, "current", str(f)) mea.xplSendMsg(xplMsg) mea.interfaceAPI(api_key, "mea_writeData", "[[[" + device_name + "!OK]]]\n") return True return False
def mea_dataFromSensor(data): fn_name= __name__ + "/" + str(sys._getframe().f_code.co_name) try: device_id=data["device_id"] except: verbose(2, "ERROR (", fn_name, ") - device_id not found") return False try: device_name=data["device_name"] interface_id=data["interface_id"] parameters=data["device_parameters"] parameters=parameters.strip().lower() params=mea_utils.parseKeyValueDatasToDictionary(parameters, ",", ":") typeoftype=data["typeoftype"] api_key=data["api_key"] except: verbose(2, "ERROR (", fn_name, ") - invalid data") return False if not "value" in params: verbose(2, "ERROR (", fn_name, ") - no value parameter") return False value = params["value"] mem_device=mea.getMemory(device_id) mem_interface=mea.getMemory("interface"+str(interface_id)) pdata = mea_utils.parseKeyValueDatasToDictionary(str(data['data']).strip().lower(),";","=") if value in pdata: v = pdata[value] f = 0.0 if type(v) == str: try: f=float(v) except: return False mem_device["current"]=f mea.addDataToSensorsValuesTable(device_id, f, 0,0,"") xplMsg=mea_utils.xplMsgNew("me", "*", "xpl-trig", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg, "device", device_name) mea_utils.xplMsgAddValue(xplMsg, "current", str(f)) mea.xplSendMsg(xplMsg) # mea.interfaceAPI(api_key, "mea_writeData", "{{{OK}}}") else: return -1
def mea_dataFromSensor(data): fn_name=sys._getframe().f_code.co_name try: id_sensor=data["device_id"] verbose(1, "DEBUG (", fn_name, ") - id_sensor=", id_sensor) packet=data["data"] l_packet=data["l_data"] parameters=data["device_parameters"] except: verbose(2, "ERROR (", fn_name, ") - invalid data") return False mem=mea.getMemory(id_sensor) paramsDict=mea_utils.parseKeyValueDatasToDictionary(parameters, ",", ":") current="" try: device=paramsDict["xpldevice"]; except: device=-1; if packet[4]==1: # ENOCEAN_RADIO_ERP1 if packet[6]==0xD2: if (packet[7] & 0b00001111) == 4: # reception packet status try: if (paramsDict["channel"]=="A" and (packet[8] & 0b00011111) == 0) or (paramsDict["channel"]=="B" and (packet[8] & 0b00011111) == 1): if (packet[9] & 0b01111111) == 0: current='low' else: current='high' except: return False if current != "": try: mem["last"]=mem["current"] except: mem["last"]=False mem["current"]=current xplMsg=mea_utils.xplMsgNew("me", "*", "xpl-trig", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg,"device", data["device_name"].lower()) mea_utils.xplMsgAddValue(xplMsg,"current", mem["current"]) mea_utils.xplMsgAddValue(xplMsg,"type", "input") mea_utils.xplMsgAddValue(xplMsg,"last", mem["last"]) mea.xplSendMsg(xplMsg) return True return False
def mea_xplCmndMsg(data): fn_name = sys._getframe().f_code.co_name try: id_sensor = data["device_id"] except: verbose(2, "ERROR (", fn_name, ") - device_id not found") return 0 mem = mea.getMemory(id_sensor) x = data["xplmsg"] body = x["body"] target = "*" if "source" in x: target = x["source"] if x["schema"] == "sensor.request": try: xplMsg = mea_utils.xplMsgNew("me", target, "xpl-stat", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg, "device", data["device_name"].lower()) if body["request"] == "current": mea_utils.xplMsgAddValue(xplMsg, "current", mem["current"]) elif body["request"] == "last": mea_utils.xplMsgAddValue(xplMsg, "last", mem["last"]) else: verbose(2, "ERROR (", fn_name, ") - invalid request (", body["request"], ")") return False mea_utils.xplMsgAddValue(xplMsg, "type", "input") except: verbose(2, "ERROR (", fn_name, ") - can't create xpl message") return False mea.xplSendMsg(xplMsg) return True return False
def mea_xplCmndMsg(data): fn_name=str(sys._getframe().f_code.co_name) + "/" + __name__ # récupération des données xpl try: x=data["xplmsg"] body=x["body"] schema=x["schema"] except: return False try: id_sensor=data["device_id"] except: verbose(2, "ERROR - (", fn_name, ") : no device id") return False mem=mea.getMemory(id_sensor) if schema=="sensor.request": target="*" if "source" in x: target=x["source"] try: value=mem[body['request']] except: verbose(2, "ERROR - (", fn_name, ") : ", body['request'], " data not found") return False xplMsg=mea_utils.xplMsgNew('me', target, "xpl-stat", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg, "device", data["device_name"]) mea_utils.xplMsgAddValue(xplMsg, body['request'], value) mea.xplSendMsg(xplMsg) return True return False
def broadcastXplDirectControlBasic(internal, channel, b1, b2, device): fn_name=sys._getframe().f_code.co_name xplMsg=0 xplcurrent=0 xplsource="" if(internal!=0): xplsource="mea-internal.00000000" # destination interne (internal) sans attendre de reponse (00000000) else: xplsource="me" xplMsg=mea_utils.xplMsgNew(xplsource, "*", "xpl-cmnd", "control", "basic") mea_utils.xplMsgAddValue(xplMsg,"type","output") mea_utils.xplMsgAddValue(xplMsg, "device", device) if ((b1 == 0 or b2 == 0) and channel == "A"): # AI xplcurrent="high" elif((b1 == 1 or b2 == 1) and channel == "A"): # AO xplcurrent="low" elif((b1 == 2 or b2 == 2) and channel == "B"): # BI xplcurrent="high" elif((b1 == 3 or b2 == 3) and channel == "B"): # BO xplcurrent="low" if(xplcurrent != 0): mea_utils.xplMsgAddValue(xplMsg,"current",xplcurrent) mea.xplSendMsg(xplMsg)
def mea_dataFromSensor(data): fn_name=sys._getframe().f_code.co_name try: id_sensor=data["device_id"] verbose(1, "DEBUG (", fn_name, ") - id_sensor=", id_sensor) packet=data["data"] l_packet=data["l_data"] parameters=data["device_parameters"] except: verbose(2, "ERROR (", fn_name, ") - invalid data") return False mem=mea.getMemory(id_sensor) paramsDict=mea_utils.parseKeyValueDatasToDictionary(parameters, ",", ":") device1=0 device2=0 try: device=paramsDict["xpldevice"]; except: device=-1; if packet[4]==1: # ENOCEAN_RADIO_ERP1 if packet[6]==0xF6: t21 = (packet[12] & 0b00100000) >> 5 nu = (packet[12] & 0b00010000) >> 4 if t21 == 1: if nu == 1: button1Num = (packet[7] & 0b11100000) >> 5 button2Num = -1; action = (packet[7] & 0b00010000) >> 4 if packet[7] & 0b00000001: button2Num=(packet[7] & 0b00001110) >> 1 current="" try: if paramsDict["channel"]=="A" and action == 1: if (button1Num == 0 or button2Num == 0): current="high" elif (button1Num == 1 or button2Num == 1): current="low" if paramsDict["channel"]=="B" and action == 1: if (button1Num == 2 or button2Num == 2): current="high" elif (button1Num == 3 or button2Num == 3): current="low" except: return False if current != "": try: mem["last"]=mem["current"] except: mem["last"]=False mem["current"]=current mem["button1"]=button1Num mem["button2"]=button2Num internal=-1 if(device!=-1): try: internal=int(paramsDict["internal"]) except: internal=0 broadcastXplDirectControlBasic(internal, paramsDict["channel"], button1Num, button2Num, device); xplMsg=mea_utils.xplMsgNew("me", "*", "xpl-trig", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg,"device", data["device_name"].lower()) mea_utils.xplMsgAddValue(xplMsg,"current", mem["current"]) mea_utils.xplMsgAddValue(xplMsg,"type", "input") mea_utils.xplMsgAddValue(xplMsg,"last",mem["last"]) mea.xplSendMsg(xplMsg) return True # else: # bouton relache # if mem["button1"] != -1: # verbose(2, "Relachement de : ", mem["button1"]) # if mem["button2"] != -1: # verbose(2, "Relachement de : ", mem["button2"]) # mem["button1"]=-1 # mem["button2"]=-1 # verbose(2,"Nb boutons appuyes = ", (packet[7] & 0b11100000) >> 5) # verbose(2,"Energy bow = ", (packet[7] & 0b00010000) >> 4) # return True return False
def mea_xplCmndMsg(data): fn_name = __name__ + "/" + str(sys._getframe().f_code.co_name) try: id_actuator = data["device_id"] except: verbose(2, "ERROR (", fn_name, ") - device_id not found") return False try: device_name = data["device_name"] interface_id = data["interface_id"] parameters = data["device_parameters"] parameters = parameters.strip().lower() params = mea_utils.parseKeyValueDatasToDictionary(parameters, ",", ":") typeoftype = data["typeoftype"] except: verbose(2, "ERROR (", fn_name, ") - parameters error") return False mem_actuator = mea.getMemory(id_actuator) mem_interface = mea.getMemory("interface" + str(interface_id)) # validation du parametre pin = 0 if "pin" in params: pin = params["pin"] else: verbose(9, "DEBUG (", fn_name, ") - parameters error : no pin") return False if len(pin) != 2: verbose(2, "ERROR (", fn_name, ") - PIN parameter error") return False if pin[0] != "l" and pin[0] != "a": verbose(2, "ERROR (", fn_name, ") - PIN type error") return False else: pinType = pin[0] if pin[1] < "0" or pin[1] > "9": verbose(2, "ERROR (", fn_name, ") - PIN id error") return False else: pinNum = int(pin[1]) # validation du message xpl try: x = data["xplmsg"] schema = x["schema"] msgtype = x["msgtype"] body = x["body"] device = body["device"] except: verbose(2, "ERROR (", fn_name, ") - no or incomplet xpl message error") return False if msgtype != "xpl-cmnd": verbose(9, data) return False current = False request = False if "current" in body: current = body["current"] elif "request" in body: request = body["request"] else: verbose(2, "ERROR (", fn_name, ') - no "current" or "request" in message body') return False # préparation de la commande _cmnd = str(pinNum) + "," if schema == "control.basic" and typeoftype == 1: if current == False: verbose(2, "ERROR (", fn_name, ') - no "current" in message body') return False try: type = body["type"] except: verbose(2, "ERROR (", fn_name, ') - no "type" in message body') return False if pinNum == 0 or pinNum == 3: verbose(2, "ERROR (", fn_name, ") - PIN 0 and 3 are input only") return False if type == "output" and pinType == "l": if current == "pulse": if "data1" in body: data1 = body["data1"] if not data1.isdigit(): verbose(2, "ERROR (", fn_name, ") - data1 not numeric value") return False data1 = int(data1) else: data1 = 200 _cmnd = _cmnd + "P," + str(data1) elif current == "high" or (current.isdigit() and int(current) == 1): _cmnd = _cmnd + "L,H" elif current == "low" or (current.isdigit() and int(current) == 0): _cmnd = _cmnd + "L,L" elif type == "variable" and pinType == "a": if pinNum != 1 and pinNum != 2: verbose(2, "ERROR (", fn_name, ") - Analog output(PWM) only available on pin 1 and 2") return False else: value = 0 if "current" in mem_interface[pinNum]: value = mem_interface[pinNum]["current"] if current == "dec": value = value - 1 elif current == "inc": value = value + 1 elif current[0] == "-" or current[0] == "+": if current[1:].isdigit(): value = int(current) else: verbose(2, "ERROR (", fn_name, ") - analog value error (", current, ")") return False elif current.isdigit(): value = int(current) else: verbose(2, "ERROR (", fn_name, ") - analog value error (", current, ")") return False if value < 0: value = 0 if value > 255: value = 255 _cmnd = _cmnd + "A," + str(value) else: verbose(2, "ERROR (", fn_name, ") - xpl command error") return False elif schema == "sensor.basic": if request == False: verbose(2, "ERROR (", fn_name, ') - no "request" in message body') return False if request != "current": verbose(2, "ERROR (", fn_name, ") - xpl error request!=current") return False if typeoftype == 0: if pinType == "a" and pinNum < 4: verbose(2, "ERROR (", fn_name, ") - Analog input only available on pin 4 to pin 9") return False _cmnd = _cmnd + pinType.upper() + ",G" else: _cmnd = False # interrogation de l'état d'une sortie, on récupére de l'info aux niveaux de l'interface si dispo if not "current" in mem_interface[pinNum]: return False current_value = mem_interface[pinNum]["current"] if pinType == "l": if current_value == 0: current_value = "low" else: current_value = "high" xplMsg = mea_utils.xplMsgNew("me", "*", "xpl-trig", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg, "device", device_name) mea_utils.xplMsgAddValue(xplMsg, "current", current_value) mea.xplSendMsg(xplMsg) return True else: verbose(9, data) verbose(2, "ERROR (", fn_name, ") - xpl schema incompatible with sensor/actuator (", schema, ")") return False if _cmnd != False: cmnd = "~~~~CMD:##" + _cmnd + "##\r\nEND\r\n" # verbose(9, cmnd) mea.sendSerialData(data["fd"], bytearray(cmnd)) return True
def mea_dataFromSensor(data): xplMsg = {} try: id_type = data["device_type_id"] id_sensor = data["device_id"] cmd = data["cmd"] except: verbose(2, "ERROR - (mea_dataFromSensor) : data not found") return False verbose(9, "INFO (mea_dataFromSensor) : data from ", id_sensor) mem = mea.getMemory(id_sensor) if debug == 1: verbose(9, "INFO - (mea_dataFromSensor) : ", mem) # voir http://www.tutorialspoint.com/python/python_reg_expressions.htm pour match x = re.match("^\s*<(.*)>\s*$", cmd[12:-2]) if (x != None): t = x.group(1) list = t.split(';') else: verbose(5, "WARNING - (mea_dataFromSensor) : data error (", cmd[12:-2], ")") return False for i in list: l2 = i.split('=') # traitement Humidite if id_type == 2000 and l2[0] == 'H': humidite = float(l2[1]) last_h = 0 try: last_h = mem["current_h"] mem["last_h"] = mem["current_h"] except: pass mem["current_h"] = humidite verbose(9, "INFO (mea_dataFromSensor) : humidite =", humidite, "%") if last_h != humidite: current = humidite type = "humidity" unit = 5 last = last_h else: return True # traitement temperature elif id_type == 2001 and l2[0] == 'T': temperature = float(l2[1]) last_t = 0 try: last_t = mem["current_t"] mem["last_t"] = mem["current_t"] except: pass mem["current_t"] = temperature verbose(9, "INFO (mea_dataFromSensor) : temperature =", temperature, "C") if last_t != temperature: current = temperature unit = 3 type = "temp" last = last_t else: return True # traitement niveau piles elif id_type == 2002 and l2[0] == 'P': pile = float(l2[1]) last_p = 0 try: last_p = mem["current_p"] mem["last_p"] = mem["current_p"] except: pass mem["current_p"] = pile verbose(9, "INFO (mea_dataFromSensor) : Pile =", pile, "V") if last_p != pile: current = pile unit = 4 type = "volt" last = last_p else: return True else: continue mea.addDataToSensorsValuesTable(id_sensor, current, unit, 0, "") xplMsg = mea_utils.xplMsgNew("me", "*", "xpl-trig", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg, "device", data["device_name"]) mea_utils.xplMsgAddValue(xplMsg, "current", current) mea_utils.xplMsgAddValue(xplMsg, "type", type) mea_utils.xplMsgAddValue(xplMsg, "last", last) mea.xplSendMsg(xplMsg) return True
def mea_xplCmndMsg(data): try: x = data["xplmsg"] body = x["body"] except: return False try: if x["schema"] != "sensor.request": return False except: return False try: id_sensor = data["device_id"] id_type = data["device_type_id"] except: verbose(2, "ERROR - (mea_xplCmndMsg) : data not found") return False target = "*" if "source" in x: target = x["source"] mem = mea.getMemory(id_sensor) if body["request"] == "current": try: if id_type == 2000: type = "humidity" current = mem["current_h"] elif id_type == 2001: type = "temperature" current = mem["current_t"] elif id_type == 2002: type = "voltage" current = mem["current_p"] else: return False except: return False xplMsg = mea_utils.xplMsgNew("me", target, "xpl-stat", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg, "device", data["device_name"]) mea_utils.xplMsgAddValue(xplMsg, "current", current) mea_utils.xplMsgAddValue(xplMsg, "type", type) mea.xplSendMsg(xplMsg) return True elif body["request"] == "last": try: if id_type == 2000: type = "humidity" last = mem["last_h"] elif id_type == 2001: type = "temperature" last = mem["last_t"] elif id_type == 2002: type = "voltage" last = mem["last_p"] else: return False except: return False xplMsg = mea_utils.xplMsgNew("me", target, "xpl-stat", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg, "device", data["device_name"]) mea_utils.xplMsgAddValue(xplMsg, "last", last) mea_utils.xplMsgAddValue(xplMsg, "type", type) mea.xplSendMsg(xplMsg) return True else: return False
def mea_xplCmndMsg(data): fn_name=sys._getframe().f_code.co_name try: id_sensor=data["device_id"] parameters=data["device_parameters"] api_key=data["api_key"] except: verbose(2, "ERROR (", fn_name, ") - device_id not found") return 0 mem=mea.getMemory(id_sensor) paramsDict=mea_utils.parseKeyValueDatasToDictionary(parameters, ",", ":") x=data["xplmsg"] body=x["body"] target="*" if "source" in x: target=x["source"] channel = -1; current = -1; if x["schema"]=="control.basic": try: if body["type"] == "output": if body["current"]=="high": current = 100; elif body["current"]=="low": current = 0; if paramsDict["channel"]=="A": channel = 0; elif paramsDict["channel"]=="B": channel = 1; if channel <> -1 and current <> -1: buf=bytearray(); buf.append(0x01) buf.append(channel & 0b11111) buf.append(current & 0b1111111) mea.interfaceAPI(api_key, "sendEnoceanRadioErp1Packet", 0xD2, 0, data["ENOCEAN_ADDR"], buf) except Exception as e: verbose(2, "ERROR (", fn_name, ") - can't create enocean packet ", str(e)) return False elif x["schema"]=="sensor.request": try: xplMsg=mea_utils.xplMsgNew("me", target, "xpl-stat", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg,"device", data["device_name"].lower()) if body["request"]=="current": mea_utils.xplMsgAddValue(xplMsg,"current",mem["current"]) elif body["request"]=="last": mea_utils.xplMsgAddValue(xplMsg,"last",mem["last"]) else: verbose(2, "ERROR (", fn_name, ") - invalid request (", body["request"],")") return False mea_utils.xplMsgAddValue(xplMsg,"type","input") except: verbose(2, "ERROR (", fn_name, ") - can't create xpl message") return False mea.xplSendMsg(xplMsg) return True return False
def mea_dataFromSensor(data): fn_name = __name__ + "/" + str(sys._getframe().f_code.co_name) # verbose(9, "DEBUG (", fn_name, ") data = ", data) try: device_id = data["device_id"] except: verbose(2, "ERROR (", fn_name, ") - device_id not found") return False try: device_name = data["device_name"] interface_id = data["interface_id"] parameters = data["device_parameters"] parameters = parameters.strip().lower() params = mea_utils.parseKeyValueDatasToDictionary(parameters, ",", ":") typeoftype = data["typeoftype"] except: verbose(2, "ERROR (", fn_name, ") - invalid data") return False mem_device = mea.getMemory(device_id) mem_interface = mea.getMemory("interface" + str(interface_id)) # validation du parametre pin = 0 try: pin = params["pin"] except: verbose(2, "ERROR (", fn_name, ") - ", device_name, " parameters error : no PIN defined") return False if len(pin) != 2: verbose(2, "ERROR (", fn_name, ") - ", device_name, " PIN format error") return False if pin[0] != "l" and pin[0] != "a": verbose(2, "ERROR (", fn_name, ") - ", device_name, " PIN type error") return Flase else: pinType = pin[0] if pin[1] < "0" or pin[1] > "9": verbose(2, "ERROR (", fn_name, ") - ", device_name, " PIN number error") return False else: pinNum = int(pin[1]) # on va traiter les données prémachées par mea_serialDataPre if "sendFlag" in mem_interface[pinNum] and mem_interface[pinNum]["sendFlag"] == True: if "current" in mem_interface[pinNum]: current_value = mem_interface[pinNum]["current"] if pinType == "l": if mem_interface[pinNum]["current"] == 0: current_value = "low" else: current_value = "high" mea.addDataToSensorsValuesTable(device_id, mem_interface[pinNum]["current"], 0, 0, "") xplMsg = mea_utils.xplMsgNew("me", "*", "xpl-trig", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg, "device", device_name) mea_utils.xplMsgAddValue(xplMsg, "current", current_value) mea.xplSendMsg(xplMsg) return True return False
def mea_dataFromSensor(data): xplMsg = {} try: id_type=data["device_type_id"] id_sensor=data["device_id"] cmd=data["cmd"] except: verbose(2, "ERROR - (mea_dataFromSensor) : data not found") return False verbose(9, "INFO (mea_dataFromSensor) : data from ", id_sensor) mem=mea.getMemory(id_sensor) if debug == 1: verbose(9, "INFO - (mea_dataFromSensor) : ", mem) # voir http://www.tutorialspoint.com/python/python_reg_expressions.htm pour match x=re.match("^\s*<(.*)>\s*$",cmd[12:-2]) if(x!=None): t=x.group(1) list=t.split(';') else: verbose(5, "WARNING - (mea_dataFromSensor) : data error (", cmd[12:-2], ")") return False for i in list: l2=i.split('=') # traitement Humidite if id_type == 2000 and l2[0] == 'H': humidite = float(l2[1]) last_h=0 try: last_h=mem["current_h"] mem["last_h"]=mem["current_h"] except: pass mem["current_h"]=humidite verbose(9, "INFO (mea_dataFromSensor) : humidite =", humidite, "%") if last_h != humidite: current=humidite type="humidity" unit=5 last=last_h else: return True # traitement temperature elif id_type == 2001 and l2[0] == 'T': temperature = float(l2[1]) last_t=0 try: last_t=mem["current_t"] mem["last_t"]=mem["current_t"] except: pass mem["current_t"]=temperature verbose(9, "INFO (mea_dataFromSensor) : temperature =", temperature, "C") if last_t != temperature: current=temperature unit=3 type="temp" last=last_t else: return True # traitement niveau piles elif id_type == 2002 and l2[0] == 'P': pile = float(l2[1]) last_p=0 try: last_p=mem["current_p"] mem["last_p"]=mem["current_p"] except: pass mem["current_p"]=pile verbose(9, "INFO (mea_dataFromSensor) : Pile =", pile, "V") if last_p != pile: current=pile unit=4 type="volt" last=last_p else: return True else: continue mea.addDataToSensorsValuesTable(id_sensor,current,unit,0,"") xplMsg=mea_utils.xplMsgNew("me", "*", "xpl-trig", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg,"device", data["device_name"]) mea_utils.xplMsgAddValue(xplMsg,"current",current) mea_utils.xplMsgAddValue(xplMsg,"type",type) mea_utils.xplMsgAddValue(xplMsg,"last",last) mea.xplSendMsg(xplMsg) return True
def mea_xplCmndMsg(data): try: x=data["xplmsg"] body=x["body"] except: return False try: if x["schema"]!="sensor.request": return False except: return False try: id_sensor=data["device_id"] id_type=data["device_type_id"] except: verbose(2, "ERROR - (mea_xplCmndMsg) : data not found") return False target="*" if "source" in x: target=x["source"] mem=mea.getMemory(id_sensor) if body["request"]=="current": try: if id_type==2000: type="humidity" current=mem["current_h"] elif id_type==2001: type="temperature" current=mem["current_t"] elif id_type==2002: type="voltage" current=mem["current_p"] else: return False except: return False xplMsg=mea_utils.xplMsgNew("me", target, "xpl-stat", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg,"device", data["device_name"]) mea_utils.xplMsgAddValue(xplMsg,"current",current) mea_utils.xplMsgAddValue(xplMsg,"type",type) mea.xplSendMsg(xplMsg) return True elif body["request"]=="last": try: if id_type==2000: type="humidity" last=mem["last_h"] elif id_type==2001: type="temperature" last=mem["last_t"] elif id_type==2002: type="voltage" last=mem["last_p"] else: return False except: return False xplMsg=mea_utils.xplMsgNew("me", target, "xpl-stat", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg,"device", data["device_name"]) mea_utils.xplMsgAddValue(xplMsg,"last",last) mea_utils.xplMsgAddValue(xplMsg,"type",type) mea.xplSendMsg(xplMsg) return True else: return False
def mea_dataFromSensor(data): fn_name=sys._getframe().f_code.co_name xplMsg = {} try: id_sensor=data["device_id"] cmd_data=data["cmd_data"] l_cmd_data=data["l_cmd_data"] data_type=data["data_type"] except: verbose(2, "ERROR (", fn_name, ") - invalid data") return False mem=mea.getMemory(id_sensor) if data_type==0x92: mem["iodata"]=parseIOData(cmd_data, l_cmd_data) paramsDict=mea_utils.parseKeyValueDatasToDictionary(data["device_parameters"], ",", ":") if "pin" in paramsDict: pin=paramsDict["pin"] else: return False if "sensor_type" in paramsDict: type=paramsDict["sensor_type"] else: type="generic" current_key=pin+"_current" last_key=pin+"_last" if pin in mem["iodata"].lower(): val=mem["iodata"][pin] last_val=0 try: mem[last_key]=mem[current_key] except: mem[last_key]=0 strVal="" if pin[0]=="d": if val==1: mem[current_key]="high" else: mem[current_key]="low" logval=val else: if "compute" in paramsDict: x=val y=eval(paramsDict["compute"]) logval=round(y,2) else: logval=val mem[current_key]=logval if mem[current_key] != mem[last_key]: if "log" in paramsDict and paramsDict["log"].lower()=="yes": if "unit" in paramsDict: unit=int(paramsDict["unit"]) else: unit=0 mea.addDataToSensorsValuesTable(id_sensor,logval,unit,val,"") xplMsg=mea_utils.xplMsgNew("me", "*", "xpl-trig", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg,"device", data["device_name"].lower()) mea_utils.xplMsgAddValue(xplMsg,"current", mem[current_key]) mea_utils.xplMsgAddValue(xplMsg,"type", type) mea_utils.xplMsgAddValue(xplMsg,"last",mem[last_key]) mea.xplSendMsg(xplMsg) return True return False
def mea_xplCmndMsg(data): fn_name=sys._getframe().f_code.co_name print data try: id_sensor=data["device_id"] parameters=data["device_parameters"] # id_driver=data["driver_id"] api_key=data["api_key"] except: verbose(2, "ERROR (", fn_name, ") - device_id not found") return 0 mem=mea.getMemory(id_sensor) paramsDict=mea_utils.parseKeyValueDatasToDictionary(parameters, ",", ":") x=data["xplmsg"] body=x["body"] target="*" if "source" in x: target=x["source"] channel = -1; current = -1; if x["schema"]=="control.basic": try: if body["type"] == "output": if body["current"]=="high": current = 100; elif body["current"]=="low": current = 0; if paramsDict["channel"]=="A": channel = 0; elif paramsDict["channel"]=="B": channel = 1; if channel <> -1 and current <> -1: buf=bytearray(); buf.append(0x01) buf.append(channel & 0b11111) buf.append(current & 0b1111111) # mea.sendEnoceanRadioErp1Packet(data["ID_ENOCEAN"], 0xD2, 0, data["ENOCEAN_ADDR"], buf) # mea.interfaceAPI(id_driver, api_key, "sendEnoceanRadioErp1Packet", data["ID_ENOCEAN"], 0xD2, 0, data["ENOCEAN_ADDR"], buf) # mea.interfaceAPI(id_driver, api_key, "sendEnoceanRadioErp1Packet", 0xD2, 0, data["ENOCEAN_ADDR"], buf) mea.interfaceAPI(api_key, "sendEnoceanRadioErp1Packet", 0xD2, 0, data["ENOCEAN_ADDR"], buf) except Exception as e: verbose(2, "ERROR (", fn_name, ") - can't create enocean packet ", str(e)) return False elif x["schema"]=="sensor.request": try: xplMsg=mea_utils.xplMsgNew("me", target, "xpl-stat", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg,"device", data["device_name"].lower()) if body["request"]=="current": mea_utils.xplMsgAddValue(xplMsg,"current",mem["current"]) elif body["request"]=="last": mea_utils.xplMsgAddValue(xplMsg,"last",mem["last"]) else: verbose(2, "ERROR (", fn_name, ") - invalid request (", body["request"],")") return False mea_utils.xplMsgAddValue(xplMsg,"type","input") except: verbose(2, "ERROR (", fn_name, ") - can't create xpl message") return False mea.xplSendMsg(xplMsg) return True return False
def mea_xplCmndMsg(data): fn_name = sys._getframe().f_code.co_name try: id_sensor = data["device_id"] except: verbose(2, "ERROR (", fn_name, ") - device_id not found") return 0 mem = mea.getMemory(id_sensor) paramsDict = mea_utils.parseKeyValueDatasToDictionary( data["device_parameters"], ",", ":") if "pin" in paramsDict: pin = paramsDict["pin"].lower() else: return False if "sensor_type" in paramsDict: type = paramsDict["sensor_type"] else: type = "generic" x = data["xplmsg"] body = x["body"] target = "*" if "source" in x: target = x["source"] if x["schema"] == "sensor.request": current_key = pin + "_current" last_key = pin + "_last" if body["type"] == type: try: xplMsg = mea_utils.xplMsgNew("me", target, "xpl-stat", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg, "device", data["device_name"].lower()) if body["request"] == "current": mea_utils.xplMsgAddValue(xplMsg, "current", mem[current_key]) elif body["request"] == "last": mea_utils.xplMsgAddValue(xplMsg, "last", mem[last_key]) else: verbose(2, "ERROR (", fn_name, ") - invalid request (", body["request"], ")") return False mea_utils.xplMsgAddValue(xplMsg, "type", type) except: verbose(2, "ERROR (", fn_name, ") - can't create xpl message") return False else: verbose(2, "ERROR (", fn_name, ") - invalid type (", body["type"], ")") return False mea.xplSendMsg(xplMsg) return True elif x["schema"] == "control.basic": if body["type"] == "output": if body["current"].lower() == "low": mea.sendXbeeCmd(data["ID_XBEE"], data["ADDR_H"], data["ADDR_L"], pin, 4) mea.sendXbeeCmd(data["ID_XBEE"], data["ADDR_H"], data["ADDR_L"], "AC", "") return True elif body["current"].lower() == "high": mea.sendXbeeCmd(data["ID_XBEE"], data["ADDR_H"], data["ADDR_L"], pin, 5) mea.sendXbeeCmd(data["ID_XBEE"], data["ADDR_H"], data["ADDR_L"], "AC", "") return True else: return False else: verbose(2, "ERROR (", fn_name, ") - invalid request (", body["request"], ")") return False else: verbose(2, "ERROR (", fn_name, ") - invalid schema (", x["schema"], ")") return False
def mea_dataFromSensor(data): fn_name = sys._getframe().f_code.co_name try: id_sensor = data["device_id"] verbose(1, "DEBUG (", fn_name, ") - id_sensor=", id_sensor) packet = data["data"] l_packet = data["l_data"] parameters = data["device_parameters"] except: verbose(2, "ERROR (", fn_name, ") - invalid data") return False mem = mea.getMemory(id_sensor) paramsDict = mea_utils.parseKeyValueDatasToDictionary(parameters, ",", ":") device1 = 0 device2 = 0 try: device = paramsDict["xpldevice"] except: device = -1 if packet[4] == 1: # ENOCEAN_RADIO_ERP1 if packet[6] == 0xF6: t21 = (packet[12] & 0b00100000) >> 5 nu = (packet[12] & 0b00010000) >> 4 if t21 == 1: if nu == 1: button1Num = (packet[7] & 0b11100000) >> 5 button2Num = -1 action = (packet[7] & 0b00010000) >> 4 if packet[7] & 0b00000001: button2Num = (packet[7] & 0b00001110) >> 1 current = "" try: if paramsDict["channel"] == "A" and action == 1: if (button1Num == 0 or button2Num == 0): current = "high" elif (button1Num == 1 or button2Num == 1): current = "low" if paramsDict["channel"] == "B" and action == 1: if (button1Num == 2 or button2Num == 2): current = "high" elif (button1Num == 3 or button2Num == 3): current = "low" except: return False if current != "": try: mem["last"] = mem["current"] except: mem["last"] = False mem["current"] = current mem["button1"] = button1Num mem["button2"] = button2Num internal = -1 if (device != -1): try: internal = int(paramsDict["internal"]) except: internal = 0 broadcastXplDirectControlBasic( internal, paramsDict["channel"], button1Num, button2Num, device) xplMsg = mea_utils.xplMsgNew("me", "*", "xpl-trig", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg, "device", data["device_name"].lower()) mea_utils.xplMsgAddValue(xplMsg, "current", mem["current"]) mea_utils.xplMsgAddValue(xplMsg, "type", "input") mea_utils.xplMsgAddValue(xplMsg, "last", mem["last"]) mea.xplSendMsg(xplMsg) return True # else: # bouton relache # if mem["button1"] != -1: # verbose(2, "Relachement de : ", mem["button1"]) # if mem["button2"] != -1: # verbose(2, "Relachement de : ", mem["button2"]) # mem["button1"]=-1 # mem["button2"]=-1 # verbose(2,"Nb boutons appuyes = ", (packet[7] & 0b11100000) >> 5) # verbose(2,"Energy bow = ", (packet[7] & 0b00010000) >> 4) # return True return False
def mea_dataFromSensor(data): fn_name = __name__ + "/" + str(sys._getframe().f_code.co_name) # verbose(9, "DEBUG (", fn_name, ") data = ", data) try: device_id = data["device_id"] except: verbose(2, "ERROR (", fn_name, ") - device_id not found") return False try: device_name = data["device_name"] interface_id = data["interface_id"] parameters = data["device_parameters"] parameters = parameters.strip().lower() params = mea_utils.parseKeyValueDatasToDictionary(parameters, ",", ":") typeoftype = data["typeoftype"] except: verbose(2, "ERROR (", fn_name, ") - invalid data") return False mem_device = mea.getMemory(device_id) mem_interface = mea.getMemory("interface" + str(interface_id)) # validation du parametre pin = 0 try: pin = params["pin"] except: verbose(2, "ERROR (", fn_name, ") - ", device_name, " parameters error : no PIN defined") return False if len(pin) != 2: verbose(2, "ERROR (", fn_name, ") - ", device_name, " PIN format error") return False if pin[0] != 'l' and pin[0] != 'a': verbose(2, "ERROR (", fn_name, ") - ", device_name, " PIN type error") return Flase else: pinType = pin[0] if pin[1] < '0' or pin[1] > '9': verbose(2, "ERROR (", fn_name, ") - ", device_name, " PIN number error") return False else: pinNum = int(pin[1]) # on va traiter les données prémachées par mea_serialDataPre if "sendFlag" in mem_interface[pinNum] and mem_interface[pinNum][ "sendFlag"] == True: if "current" in mem_interface[pinNum]: current_value = mem_interface[pinNum]["current"] if pinType == 'l': if mem_interface[pinNum]["current"] == 0: current_value = "low" else: current_value = "high" mea.addDataToSensorsValuesTable(device_id, mem_interface[pinNum]["current"], 0, 0, "") xplMsg = mea_utils.xplMsgNew("me", "*", "xpl-trig", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg, "device", device_name) mea_utils.xplMsgAddValue(xplMsg, "current", current_value) mea.xplSendMsg(xplMsg) return True return False
def mea_xplCmndMsg(data): fn_name = __name__ + "/" + str(sys._getframe().f_code.co_name) try: id_actuator = data["device_id"] except: verbose(2, "ERROR (", fn_name, ") - device_id not found") return False try: device_name = data["device_name"] interface_id = data["interface_id"] parameters = data["device_parameters"] parameters = parameters.strip().lower() params = mea_utils.parseKeyValueDatasToDictionary(parameters, ",", ":") typeoftype = data["typeoftype"] except: verbose(2, "ERROR (", fn_name, ") - parameters error") return False mem_actuator = mea.getMemory(id_actuator) mem_interface = mea.getMemory("interface" + str(interface_id)) # validation du parametre pin = 0 if "pin" in params: pin = params["pin"] else: verbose(9, "DEBUG (", fn_name, ") - parameters error : no pin") return False if len(pin) != 2: verbose(2, "ERROR (", fn_name, ") - PIN parameter error") return False if pin[0] != 'l' and pin[0] != 'a': verbose(2, "ERROR (", fn_name, ") - PIN type error") return False else: pinType = pin[0] if pin[1] < '0' or pin[1] > '9': verbose(2, "ERROR (", fn_name, ") - PIN id error") return False else: pinNum = int(pin[1]) # validation du message xpl try: x = data["xplmsg"] schema = x["schema"] msgtype = x["msgtype"] body = x["body"] device = body["device"] except: verbose(2, "ERROR (", fn_name, ") - no or incomplet xpl message error") return False if msgtype != "xpl-cmnd": verbose(9, data) return False current = False request = False if "current" in body: current = body["current"] elif "request" in body: request = body["request"] else: verbose(2, "ERROR (", fn_name, ") - no \"current\" or \"request\" in message body") return False # préparation de la commande _cmnd = str(pinNum) + "," if schema == "control.basic" and typeoftype == 1: if current == False: verbose(2, "ERROR (", fn_name, ") - no \"current\" in message body") return False try: type = body["type"] except: verbose(2, "ERROR (", fn_name, ") - no \"type\" in message body") return False if (pinNum == 0 or pinNum == 3): verbose(2, "ERROR (", fn_name, ") - PIN 0 and 3 are input only") return False if type == "output" and pinType == 'l': if current == "pulse": if "data1" in body: data1 = body["data1"] if not data1.isdigit(): verbose(2, "ERROR (", fn_name, ") - data1 not numeric value") return False data1 = int(data1) else: data1 = 200 _cmnd = _cmnd + "P," + str(data1) elif current == "high" or (current.isdigit() and int(current) == 1): _cmnd = _cmnd + "L,H" elif current == "low" or (current.isdigit() and int(current) == 0): _cmnd = _cmnd + "L,L" elif type == "variable" and pinType == 'a': if pinNum != 1 and pinNum != 2: verbose( 2, "ERROR (", fn_name, ") - Analog output(PWM) only available on pin 1 and 2") return False else: value = 0 if 'current' in mem_interface[pinNum]: value = mem_interface[pinNum]['current'] if current == "dec": value = value - 1 elif current == "inc": value = value + 1 elif current[0] == '-' or current[0] == '+': if current[1:].isdigit(): value = int(current) else: verbose(2, "ERROR (", fn_name, ") - analog value error (", current, ")") return False elif current.isdigit(): value = int(current) else: verbose(2, "ERROR (", fn_name, ") - analog value error (", current, ")") return False if value < 0: value = 0 if value > 255: value = 255 _cmnd = _cmnd + "A," + str(value) else: verbose(2, "ERROR (", fn_name, ") - xpl command error") return False elif schema == "sensor.basic": if request == False: verbose(2, "ERROR (", fn_name, ") - no \"request\" in message body") return False if request != "current": verbose(2, "ERROR (", fn_name, ") - xpl error request!=current") return False if typeoftype == 0: if pinType == 'a' and pinNum < 4: verbose(2, "ERROR (", fn_name, ") - Analog input only available on pin 4 to pin 9") return False _cmnd = _cmnd + pinType.upper() + ",G" else: _cmnd = False # interrogation de l'état d'une sortie, on récupére de l'info aux niveaux de l'interface si dispo if not "current" in mem_interface[pinNum]: return False current_value = mem_interface[pinNum]["current"] if pinType == 'l': if current_value == 0: current_value = "low" else: current_value = "high" xplMsg = mea_utils.xplMsgNew("me", "*", "xpl-trig", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg, "device", device_name) mea_utils.xplMsgAddValue(xplMsg, "current", current_value) mea.xplSendMsg(xplMsg) return True else: verbose(9, data) verbose(2, "ERROR (", fn_name, ") - xpl schema incompatible with sensor/actuator (", schema, ")") return False if _cmnd != False: cmnd = "~~~~CMD:##" + _cmnd + "##\r\nEND\r\n" # verbose(9, cmnd) mea.sendSerialData(data['fd'], bytearray(cmnd)) return True
def mea_xplCmndMsg(data): fn_name=sys._getframe().f_code.co_name try: id_sensor=data["device_id"] except: verbose(2, "ERROR (", fn_name, ") - device_id not found") return 0 mem=mea.getMemory(id_sensor) paramsDict=mea_utils.parseKeyValueDatasToDictionary(data["device_parameters"], ",", ":") if "pin" in paramsDict: pin=paramsDict["pin"].lower() else: return False if "sensor_type" in paramsDict: type=paramsDict["sensor_type"] else: type="generic" x=data["xplmsg"] body=x["body"] target="*" if "source" in x: target=x["source"] if x["schema"]=="sensor.request": current_key=pin+"_current" last_key=pin+"_last" if body["type"]==type: try: xplMsg=mea_utils.xplMsgNew("me", target, "xpl-stat", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg,"device", data["device_name"].lower()) if body["request"]=="current": mea_utils.xplMsgAddValue(xplMsg,"current",mem[current_key]) elif body["request"]=="last": mea_utils.xplMsgAddValue(xplMsg,"last",mem[last_key]) else: verbose(2, "ERROR (", fn_name, ") - invalid request (", body["request"],")") return False mea_utils.xplMsgAddValue(xplMsg,"type",type) except: verbose(2, "ERROR (", fn_name, ") - can't create xpl message") return False else: verbose(2, "ERROR (", fn_name, ") - invalid type (", body["type"],")") return False mea.xplSendMsg(xplMsg) return True elif x["schema"]=="control.basic": if body["type"]=="output": if body["current"].lower()=="low": mea.sendXbeeCmd(data["ID_XBEE"], data["ADDR_H"], data["ADDR_L"], pin, 4); mea.sendXbeeCmd(data["ID_XBEE"], data["ADDR_H"], data["ADDR_L"], "AC", ""); return True elif body["current"].lower()=="high": mea.sendXbeeCmd(data["ID_XBEE"], data["ADDR_H"], data["ADDR_L"], pin, 5); mea.sendXbeeCmd(data["ID_XBEE"], data["ADDR_H"], data["ADDR_L"], "AC", ""); return True else: return False else: verbose(2, "ERROR (", fn_name, ") - invalid request (", body["request"],")") return False else: verbose(2, "ERROR (", fn_name, ") - invalid schema (", x["schema"],")") return False
def mea_dataFromSensor(data): fn_name=str(sys._getframe().f_code.co_name) + "/" + __name__ parameters=False try: id_sensor=data["device_id"] device=data["device_name"] serial_data=data["data"] l_serial_data=data["l_data"] toDbFlag=data["todbflag"] # parameters=data["device_parameters"] except: verbose(2, "ERROR (", fn_name, ") - invalid data") return False mem=mea.getMemory(id_sensor) # récupération des paramétres if parameters <> False: params=mea_utils.parseKeyValueDatasToDictionary(parameters, ",", ":") # faire ce qu'il faut ici avec les parametres # conversion des données si nécessaire # récupération des données dans une chaine de caractères unicode s=serial_data.decode('latin-1') # traitement des données # récupère (ou pas) un SMS dans les données sms=mea_getSMS(s) if sms[0] == False: if sms[1] == 1: verbose(2, "ERROR (", fn_name, ") - incomplete SMS data") else: verbose(9, "DEBUG (", fn_name, ") - not a SMS") return False # analyse des données alarm=_analyseData(sms[1]) if alarm[0] == False: verbose(9, "DEBUG (", fn_name, ") - not an EPG message : ", alarm[1]) return False verbose(9, alarm) # vérification validité try: last_date = datetime.strptime( mem['date']+"-"+ mem['time'], '%d/%m/%Y-%H:%M:%S') except: last_date = datetime.fromtimestamp(0); try: current_date = datetime.strptime( alarm[2]+"-"+ alarm[3], '%d/%m/%Y-%H:%M:%S') except: verbose(9, "ERROR (", fn_name, ") - EPG message date and time error : ", alarm[2]+"-"+alarm[3]+" ?") return False if current_date < last_date: verbose(9, "ERROR (", fn_name, ") - older EPG message than previous received") return False now = datetime.now() d = (now - current_date).total_seconds() / 60; verbose (9, d) if d > 15: verbose(9, "ERROR (", fn_name, ") - too old EPG message") return False # memorisation des données try: mem['last']=mem['current'] except: mem['last']=-1 mem['current']=alarm[0] if alarm[1]==False: who=""; else: who=str(alarm[1]) mem['who']=who mem['date']=str(alarm[2]) mem['time']=str(alarm[3]) mea.addDataToSensorsValuesTable(id_sensor,mem['current'],0,0,who) # emission XPL xplMsg=mea_utils.xplMsgNew('me', "*", "xpl-trig", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg, "device", data["device_name"]) mea_utils.xplMsgAddValue(xplMsg, 'current', mem['current']) mea.xplSendMsg(xplMsg) return True
def mea_dataFromSensor(data): fn_name = sys._getframe().f_code.co_name xplMsg = {} try: id_sensor = data["device_id"] cmd_data = data["cmd_data"] l_cmd_data = data["l_cmd_data"] data_type = data["data_type"] except: verbose(2, "ERROR (", fn_name, ") - invalid data") return False mem = mea.getMemory(id_sensor) if data_type == 0x92: mem["iodata"] = parseIOData(cmd_data, l_cmd_data) paramsDict = mea_utils.parseKeyValueDatasToDictionary( data["device_parameters"], ",", ":") if "pin" in paramsDict: pin = paramsDict["pin"] else: return False if "sensor_type" in paramsDict: type = paramsDict["sensor_type"] else: type = "generic" current_key = pin + "_current" last_key = pin + "_last" if pin in mem["iodata"].lower(): val = mem["iodata"][pin] last_val = 0 try: mem[last_key] = mem[current_key] except: mem[last_key] = 0 strVal = "" if pin[0] == "d": if val == 1: mem[current_key] = "high" else: mem[current_key] = "low" logval = val else: if "compute" in paramsDict: x = val y = eval(paramsDict["compute"]) logval = round(y, 2) else: logval = val mem[current_key] = logval if mem[current_key] != mem[last_key]: if "log" in paramsDict and paramsDict["log"].lower() == "yes": if "unit" in paramsDict: unit = int(paramsDict["unit"]) else: unit = 0 mea.addDataToSensorsValuesTable(id_sensor, logval, unit, val, "") xplMsg = mea_utils.xplMsgNew("me", "*", "xpl-trig", "sensor", "basic") mea_utils.xplMsgAddValue(xplMsg, "device", data["device_name"].lower()) mea_utils.xplMsgAddValue(xplMsg, "current", mem[current_key]) mea_utils.xplMsgAddValue(xplMsg, "type", type) mea_utils.xplMsgAddValue(xplMsg, "last", mem[last_key]) mea.xplSendMsg(xplMsg) return True return False