def mea_dataFromSensor(data):
   fn_name= __name__ + "/" + str(sys._getframe().f_code.co_name)

#   verbose(9, fn_name, "data=", data)
   try:
      id_actuator=data["device_id"]
   except:
      verbose(2, "ERROR (", fn_name, ") - device_id not found")
      return False

   try:
      serial_data=data["data"]
   except:
      return False
   try:
      s=serial_data.decode('latin-1')
   except:
      return False

   mem_actuator=mea.getMemory(id_actuator)
#   verbose(9, fn_name, "mem_actuator=", mem_actuator)
   if not 'msg' in mem_actuator or mem_actuator['msg']==False:
      return True

   now=int(round(time() * 1000))
   if 'chrono' in mem_actuator and mem_actuator['chrono'] <> False:
      if now -  mem_actuator['chrono'] < 5000:  
         if s.find(u"\r\n> ") >=0:
            mea.sendSerialData(data['fd'], bytearray(mem_actuator['msg']+"\r\n"+chr(26)))
            mem_actuator['msg']==False
            mem_actuator['chrono']=False
      else:
         mem_actuator['chrono']=False
         mem_actuator['msg']==False
Exemple #2
0
def mea_xplCmndMsg(data):
    fn_name = str(sys._getframe().f_code.co_name) + "/" + __name__

    verbose(9, fn_name, " data=", data)

    try:
        id_actuator = data["device_id"]
    except:
        verbose(2, "ERROR (", fn_name, ") - device_id not found")
        return False

    mem_actuator = mea.getMemory(id_actuator)
    verbose(9, fn_name, " mem_actuator=", mem_actuator)

    # premier passage, pas encore de msg
    if not 'chrono' in mem_actuator or not 'msg' in mem_actuator:
        mem_actuator['chrono'] = False
        mem_actuator['msg'] = False

    # le dernier message n'a pas encore été envoyé après 5 secondes
    if mem_actuator['chrono'] != False:
        now = int(round(time() * 1000))
        if now - mem_actuator['chrono'] > 5000:
            mem_actuator['chrono'] = False
            mem_actuator['msg'] = False

    if mem_actuator['msg'] != False and mem_actuator[
            'chrono'] != False:  # un sms est déjà en cours d'envoi, on ne peut traiter qu'un seul à la fois
        return False

    # récupération des données xpl
    try:
        x = data["xplmsg"]
        body = x["body"]
        schema = x["schema"]
    except:
        return False

    if schema == "sendmsg.basic":
        try:
            msg = str(body["body"])
            to = str(body["to"])
        except:
            verbose(2, "ERROR - (", fn_name, ") : bad data")
            return False

        mea.sendSerialData(data['fd'], bytearray("AT+CMGS=\"" + to + "\"\r\n"))
        mem_actuator['msg'] = msg
        mem_actuator['chrono'] = int(round(time() * 1000))

        return True
    else:
        return False
    return False
def mea_xplCmndMsg(data):
   fn_name=str(sys._getframe().f_code.co_name) + "/" + __name__

   verbose(9, fn_name," data=",data)

   try:
      id_actuator=data["device_id"]
   except:
      verbose(2, "ERROR (", fn_name, ") - device_id not found")
      return False

   mem_actuator=mea.getMemory(id_actuator)
   verbose(9, fn_name," mem_actuator=",mem_actuator)

   # premier passage, pas encore de msg
   if not 'chrono' in mem_actuator or not 'msg' in mem_actuator: 
      mem_actuator['chrono']=False
      mem_actuator['msg']=False


   # le dernier message n'a pas encore été envoyé après 5 secondes
   if mem_actuator['chrono']<>False:
      now=int(round(time() * 1000))
      if now - mem_actuator['chrono'] > 5000:
         mem_actuator['chrono']=False
         mem_actuator['msg']=False

   if mem_actuator['msg']!=False and mem_actuator['chrono']!=False: # un sms est déjà en cours d'envoi, on ne peut traiter qu'un seul à la fois
      return False

   # récupération des données xpl
   try:
      x=data["xplmsg"]
      body=x["body"]
      schema=x["schema"]
   except:
      return False

   if schema=="sendmsg.basic":
      try:
         msg=str(body["body"])
         to=str(body["to"])
      except:
         verbose(2, "ERROR - (", fn_name, ") : bad data")
         return False

      mea.sendSerialData(data['fd'], bytearray("AT+CMGS=\""+to+"\"\r\n"))
      mem_actuator['msg']= msg
      mem_actuator['chrono']=int(round(time() * 1000))

      return True
   else:
      return False
   return False
Exemple #4
0
def mea_dataFromSensor(data):
    fn_name = __name__ + "/" + str(sys._getframe().f_code.co_name)

    #   verbose(9, fn_name, "data=", data)
    try:
        id_actuator = data["device_id"]
    except:
        verbose(2, "ERROR (", fn_name, ") - device_id not found")
        return False

    try:
        serial_data = data["data"]
    except:
        return False
    try:
        s = serial_data.decode('latin-1')
    except:
        return False

    mem_actuator = mea.getMemory(id_actuator)
    #   verbose(9, fn_name, "mem_actuator=", mem_actuator)
    if not 'msg' in mem_actuator or mem_actuator['msg'] == False:
        return True

    now = int(round(time() * 1000))
    if 'chrono' in mem_actuator and mem_actuator['chrono'] != False:
        if now - mem_actuator['chrono'] < 5000:
            if s.find(u"\r\n> ") >= 0:
                mea.sendSerialData(
                    data['fd'],
                    bytearray(mem_actuator['msg'] + "\r\n" + chr(26)))
                mem_actuator['msg'] == False
                mem_actuator['chrono'] = False
        else:
            mem_actuator['chrono'] = False
            mem_actuator['msg'] == 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
Exemple #6
0
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