Пример #1
0
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
Пример #2
0
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
Пример #3
0
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
Пример #4
0
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
Пример #5
0
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
Пример #6
0
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
Пример #7
0
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
Пример #8
0
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
Пример #9
0
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):
   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