Ejemplo n.º 1
0
        def _callback(self, message):
            """ Callback for the xpl message
            @param message : the Xpl message received 
            """

            ### we put data in database
            my_db = DbHelper()
            self._log_stats.debug("message catcher : %s" % message)
            try:
                if self._res["device"] != None:
                    try:
                        device = message.data[self._res["device"]]
                    except KeyError:
                        # key error means that we are trying to get some key from a xpl where there is no such key
                        # there may be an issue in the xml file!
                        self._log_stats.error("Key error : %s in the message : %s" % (self._res["device"], message))
                        return
                    my_device = my_db.get_device_by_technology_and_address(self._technology, \
                        message.data[self._res["device"]])
                    if my_device != None:
                        d_id = my_device.id
                    else:
                        raise AttributeError
                    #d_id = my_db.get_device_by_technology_and_address(self._technology, \
                    #    message.data[self._res["device"]]).id
                    device = message.data[self._res["device"]]
                elif self._res["static_device"] != None:
                    d_id = my_db.get_device_by_technology_and_address(self._technology, \
                        self._res["static_device"]).id
                    device = self._res["static_device"]
                elif self._res["device_type"] != None:
                    # device id equals 0 for a notification
                    if self._res["device_type"] == "notification":
                        d_id = 0
                        device = "notification"
                else:  # oups... something wrong in xml file ?
                    self._log_stats.error("Device has no name... is there a problem in xml file ?")
                    raise AttributeError
                #print("Stat for techno '%s' / adress '%s' / id '%s'" % (self._technology, message.data[self._res["device"]], d_id))
                #print("Stat for techno '%s' / adress '%s' / id '%s'" % (self._technology, device, d_id))
            except AttributeError:
                if self._res["device"] != None:
                    self._log_stats_unknown.debug("Received a stat for an unreferenced device : %s - %s" \
                        % (self._technology, message.data[self._res["device"]]))
                else:
                    self._log_stats_unknown.debug("Received a stat for an unreferenced device : %s - %s" \
                        % (self._technology, self._res["static_device"]))
                print("=> unknown device")
                del(my_db)
                return
            #self._log_stats.debug("Stat received for %s - %s." \
            #        % (self._technology, message.data[self._res["device"]]))
            self._log_stats.debug("Stat received for %s - %s." \
                    % (self._technology, device))
            current_date = calendar.timegm(time.gmtime())
            device_data = []

            ### mapping processing
            for my_map in self._res["mapping"]:
                # first : get value and default key
                key = my_map["name"]
                history_size = my_map["history_size"]
                try:
                    value = message.data[my_map["name"]].lower()
                    if my_map["filter_key"] == None:
                        key = my_map["name"]
                        device_data.append({"key" : key, "value" : value})
                        # we don't insert notifications
                        if d_id != 0:
                            my_db.add_device_stat(current_date, key, value, \
                                                  d_id, \
                                                  history_size)
                    else:
                        if my_map["filter_value"] != None and \
                           my_map["filter_value"].lower() == message.data[my_map["filter_key"]].lower():
                            key = my_map["new_name"]
                            device_data.append({"key" : key, "value" : value})
                            # we don't insert notifications
                            if d_id != 0:
                                my_db.add_device_stat(current_date, key, \
                                                  value, \
                                                  d_id, history_size)
                        else:
                            if my_map["filter_value"] == None:
                                self._log_stats.warning ("Stats : no filter_value defined in map : %s" % str(my_map))
                except KeyError:
                    # no value in message for key
                    # example : a x10 command = ON has no level value
                    print("No param value in message for key")
                except:
                    error = "Error when processing stat : %s" % traceback.format_exc()
                    print("==== Error in Stats ====")
                    print(error)
                    print("========================")
                    self._log_stats.error(error)
    
            # Put data in events queues
            self._event_requests.add_in_queues(d_id, 
                    {"timestamp" : current_date, "device_id" : d_id, "data" : device_data})
            del(my_db)