Exemplo n.º 1
0
 def _callback(self, message):
     """ Callback for the xpl message
     @param message : the Xpl message received
     """
     self._log_stats.debug("Stat received for device {0}." \
             .format(self._dev['name']))
     current_date = calendar.timegm(time.gmtime())
     stored_value = None
     try:
         # find what parameter to store
         for param in self._stat.params:
             # self._log_stats.debug("Checking param {0}".format(param))
             if param.sensor_id is not None and param.static is False:
                 if param.key in message.data:
                     value = message.data[param.key]
                     # self._log_stats.debug( \
                     #        "Key found {0} with value {1}." \
                     #        .format(param.key, value))
                     store = True
                     if param.ignore_values:
                         if value in eval(param.ignore_values):
                             self._log_stats.debug( \
                                     "Value {0} is in the ignore list {0}, so not storing." \
                                     .format(value, param.ignore_values))
                             store = False
                     if store:
                         # check if we need a conversion
                         if self._sen.conversion is not None and self._sen.conversion != '':
                             value = call_package_conversion(\
                                         self._log_stats, \
                                         self._dev['client_id'], \
                                         self._sen.conversion, \
                                         value)
                         self._log_stats.info( \
                                 "Storing stat for device '{0}' ({1}) and sensor'{2}' ({3}): key '{4}' with value '{5}' after conversion." \
                                 .format(self._dev['name'], self._dev['id'], self._sen.name, self._sen.id, param.key, value))
                         # do the store
                         stored_value = value
                         my_db = DbHelper()
                         with my_db.session_scope():
                             my_db.add_sensor_history(\
                                     param.sensor_id, \
                                     value, \
                                     current_date)
                         del(my_db)
                     else:
                         self._log_stats.debug("Don't need to store this value")
                 #else:
                 #    self._log_stats.debug("Key not found in message data")
             #else:
             #    self._log_stats.debug("No sensor attached")
     except:
         self._log_stats.error(traceback.format_exc())
     # publish the result
     self._pub.send_event('device-stats', \
                   {"timestamp" : current_date, \
                   "device_id" : self._dev['id'], \
                   "sensor_id" : self._sen.id, \
                   "stored_value" : stored_value})
Exemplo n.º 2
0
 def _callback(self, message):
     """ Callback for the xpl message
     @param message : the Xpl message received 
     """
     my_db = DbHelper()
     self._log_stats.debug("Stat received for device %s." \
             % (self._dev.name))
     current_date = calendar.timegm(time.gmtime())
     device_data = []
     try:
         # find what parameter to store
         for p in self._stat.params:
             if p.sensor_id is not None:
                 if p.key in message.data:
                     value = message.data[p.key]
                     self._log_stats.debug("Key found %s with value %s." \
                         % (p.key, value))
                     store = True
                     if p.ignore_values:
                         if value in eval(p.ignore_values):
                             self._log_stats.debug("Value %s is in the ignore list %s, so not storing." \
                                  % (value, p.ignore_values))
                             store = False
                     if store:
                         # check if we need a conversion
                         if self._sen.conversion is not None and self._sen.conversion != '':
                             value = call_package_conversion(\
                                         self._log_stats, self._dev.device_type.plugin_id, \
                                         self._sen.conversion, value)
                             self._log_stats.debug("Key found %s with value %s after conversion." \
                                 % (p.key, value))
                         # do the store
                         device_data.append({"value" : value, "sensor": p.sensor_id})
                         my_db.add_sensor_history(p.sensor_id, value, current_date)
     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 the event queue
     self._event_requests.add_in_queues(self._dev.id, 
             {"timestamp" : current_date, "device_id" : self._dev.id, "data" : device_data})
     del(my_db)
Exemplo n.º 3
0
    class _SensorThread(threading.Thread):
        """ SensorThread class
        Class that will handle the sensor storage in a seperated thread
        This will get messages from the SensorQueue
        """
        def __init__(self, log, queue, conv, pub):
            threading.Thread.__init__(self)
            self._db = DbHelper()
            self._log = log
            self._queue = queue
            self._conv = conv
            self._pub = pub

        def _find_storeparam(self, item):
            #print("ITEM = {0}".format(item['msg']))
            found = False
            tostore = []
            for xplstat in self._db.get_all_xpl_stat():
                sensors = 0
                matching = 0
                statics = 0
                if xplstat.schema == item["msg"].schema:
                    #print("  XPLSTAT = {0}".format(xplstat))
                    # we found a possible xplstat
                    # try to match all params and try to find a sensorid and a vlaue to store
                    for param in xplstat.params:
                        #print("    PARAM = {0}".format(param))
                        ### Caution !
                        # in case you, who are reading this, have to debug something like that :
                        # 2015-08-16 22:04:26,190 domogik-xplgw INFO Storing stat for device 'Garage' (6) and sensor 'Humidity' (69): key 'current' with value '53' after conversion.
                        # 2015-08-16 22:04:26,306 domogik-xplgw INFO Storing stat for device 'Salon' (10) and sensor 'Humidity' (76): key 'current' with value '53' after conversion.
                        # 2015-08-16 22:04:26,420 domogik-xplgw INFO Storing stat for device 'Chambre d'Ewan' (11) and sensor 'Humidity' (80): key 'current' with value '53' after conversion.
                        # 2015-08-16 22:04:26,533 domogik-xplgw INFO Storing stat for device 'Chambre des parents' (12) and sensor 'Humidity' (84): key 'current' with value '53' after conversion.
                        # 2015-08-16 22:04:26,651 domogik-xplgw INFO Storing stat for device 'Chambre de Laly' (13) and sensor 'Humidity' (88): key 'current' with value '53' after conversion.
                        # 2015-08-16 22:04:26,770 domogik-xplgw INFO Storing stat for device 'Entrée' (17) and sensor 'Humidity' (133): key 'current' with value '53' after conversion.
                        #
                        # which means that for a single xPL message, the value is stored in several sensors (WTF!!! ?)
                        # It can be related to the fact that the device address key is no more corresponding between the plugin (info.json and xpl sent by python) and the way the device was create in the databse
                        # this should not happen, but in case... well, we may try to find a fix...

                        if param.key in item["msg"].data and param.static:
                            statics = statics + 1
                            if param.multiple is not None and len(param.multiple) == 1 and item["msg"].data[param.key] in param.value.split(param.multiple):
                                matching = matching + 1
                            elif item["msg"].data[param.key] == param.value:
                                matching = matching + 1
                    # now we have a matching xplstat, go and find all sensors
                    if matching == statics:
                        #print("  MATHING !!!!!")
                        for param in xplstat.params:
                            if param.key in item["msg"].data and not param.static:     
                                #print("    ===> TOSTORE !!!!!!!!! : {0}".format({'param': param, 'value': item["msg"].data[param.key]}))
                                tostore.append( {'param': param, 'value': item["msg"].data[param.key]} )
                    if len(tostore) > 0:
                        found = True
            if found:
                return (found, tostore)
            else:
                return False

        def run(self):
            while True:
                try:
                    item = self._queue.get()
                    self._log.debug(u"Getting item from the sensorQueue, current length = {0}".format(self._queue.qsize()))
                    # if clientid is none, we don't know this sender so ignore
                    # TODO check temp disabled until external members are working
                    #if item["clientId"] is not None:
                    if True:
                        with self._db.session_scope():
                            fdata = self._find_storeparam(item)
                            if fdata:
                                #// ICI !!!
                                self._log.debug(u"Found a matching sensor, so starting the storage procedure. Sensor : {0}".format(fdata))
                                for data in fdata[1]:
                                    value = data['value']
                                    storeparam = data['param']
                                    current_date = calendar.timegm(time.gmtime())
                                    store = True
                                    if storeparam.ignore_values:
                                        if value in eval(storeparam.ignore_values):
                                            self._log.debug(u"Value {0} is in the ignore list {0}, so not storing.".format(value, storeparam.ignore_values))
                                            store = False
                                    if store:
                                        # get the sensor and dev
                                        sen = self._db.get_sensor(storeparam.sensor_id)
                                        dev = self._db.get_device(sen.device_id)
                                        # check if we need a conversion
                                        if sen.conversion is not None and sen.conversion != '':
                                            if dev['client_id'] in self._conv and sen.conversion in self._conv[dev['client_id']]:
                                                self._log.debug( \
                                                    u"Calling conversion {0}".format(sen.conversion))
                                                exec(self._conv[dev['client_id']][sen.conversion])
                                                value = locals()[sen.conversion](value)
                                        self._log.info( \
                                                u"Storing stat for device '{0}' ({1}) and sensor '{2}' ({3}): key '{4}' with value '{5}' after conversion." \
                                                .format(dev['name'], dev['id'], sen.name, sen.id, storeparam.key, value))
                                        # do the store
                                        try:
                                            self._db.add_sensor_history(\
                                                    storeparam.sensor_id, \
                                                    value, \
                                                    current_date)
                                        except Exception as exp:
                                            self._log.error(u"Error when adding sensor history : {0}".format(traceback.format_exc()))
                                    else:
                                        self._log.debug(u"Don't need to store this value")
                                    # publish the result
                                    self._pub.send_event('device-stats', \
                                              {"timestamp" : current_date, \
                                              "device_id" : dev['id'], \
                                              "sensor_id" : sen.id, \
                                              "stored_value" : value})

                except Queue.Empty:
                    # nothing in the queue, sleep for 1 second
                    time.sleep(1)
                except Exception as exp:
                    self._log.error(traceback.format_exc())
Exemplo n.º 4
0
 def _callback(self, message):
     """ Callback for the xpl message
     @param message : the Xpl message received
     """
     self._log_stats.debug( "_callback started for: {0}".format(message) )
     db = DbHelper()
     current_date = calendar.timegm(time.gmtime())
     stored_value = None
     try:
         # find what parameter to store
         for param in self._stat.params:
             # self._log_stats.debug("Checking param {0}".format(param))
             if param.sensor_id is not None and param.static is False:
                 if param.key in message.data:
                     with db.session_scope():
                         value = message.data[param.key]
                         # self._log_stats.debug( \
                         #        "Key found {0} with value {1}." \
                         #        .format(param.key, value))
                         store = True
                         if param.ignore_values:
                             if value in eval(param.ignore_values):
                                 self._log_stats.debug( \
                                         "Value {0} is in the ignore list {0}, so not storing." \
                                         .format(value, param.ignore_values))
                                 store = False
                         if store:
                             # get the sensor and dev
                             sen = db.get_sensor(param.sensor_id)
                             dev = db.get_device(sen.device_id)
                             # check if we need a conversion
                             if sen.conversion is not None and sen.conversion != '':
                                 if dev['client_id'] in self._conv:
                                     if sen.conversion in self._conv[dev['client_id']]:
                                         self._log_stats.debug( \
                                             "Calling conversion {0}".format(sen.conversion))
                                         exec(self._conv[dev['client_id']][sen.conversion])
                                         value = locals()[sen.conversion](value)
                             self._log_stats.info( \
                                     "Storing stat for device '{0}' ({1}) and sensor'{2}' ({3}): key '{4}' with value '{5}' after conversion." \
                                     .format(dev['name'], dev['id'], sen.name, sen.id, param.key, value))
                             # do the store
                             stored_value = value
                             try:
                                 db.add_sensor_history(\
                                         param.sensor_id, \
                                         value, \
                                         current_date)
                             except:
                                 self._log_stats.error("Error when adding sensor history : {0}".format(traceback.format_exc()))
                         else:
                             self._log_stats.debug("Don't need to store this value")
                         # publish the result
                         self._pub.send_event('device-stats', \
                                   {"timestamp" : current_date, \
                                   "device_id" : dev['id'], \
                                   "sensor_id" : sen.id, \
                                   "stored_value" : stored_value})
                 #else:
                 #    self._log_stats.debug("Key not found in message data")
             #else:
             #    self._log_stats.debug("No sensor attached")
     except:
         self._log_stats.error(traceback.format_exc())
Exemplo n.º 5
0
    class _SensorStoreThread(threading.Thread):
        """ SensorStoreThread class
        Thread that will handle the sensorStore queue
        every item in this queue should be stored in the db
        - conversion will happend
        - formula applying
        - rounding
        - and eventually storing it in the db
        Its a thread to make sure it does not block anything else
        """
        def __init__(self, queue, log, get_conversion_map, pub):
            threading.Thread.__init__(self)
            self._log = log
            self._db = DbHelper()
            self._conv = get_conversion_map
            self._queue = queue
            self._pub = pub
            # on startup, load the device parameters
            self.on_device_changed()

        def on_device_changed(self):
            """ Function called when a device have been changed to reload the devices parameters
            """
            self._log.info("Event : one device changed. Reloading data for _SensorStoreThread")
            self.all_sensors = {}
            self.all_devices = {}
            with self._db.session_scope():
                for sen in self._db.get_all_sensor():
                    #print(sen)
                    #<Sensor: conversion='', value_min='None', history_round='0.0', reference='adco', data_type='DT_String', history_duplicate='False', last_received='1474968431', incremental='False', id='29', history_expire='0', timeout='180', history_store='True', history_max='0', formula='None', device_id='2', last_value='030928084432', value_max='3.09036843008e+11', name='Electric meter address'>
                    self.all_sensors[str(sen.id)] = { 'id' : sen.id,
                                                 'conversion' : sen.conversion,
                                                 'value_min' : sen.value_min,
                                                 'history_round' : sen.history_round,
                                                 'reference' : sen.reference,
                                                 'data_type' : sen.data_type,
                                                 'history_duplicate' : sen.history_duplicate,
                                                 'last_received' : sen.last_received,
                                                 'incremental' : sen.incremental,
                                                 'history_expire' : sen.history_expire,
                                                 'timeout' : sen.timeout,
                                                 'history_store' : sen.history_store,
                                                 'history_max' : sen.history_max,
                                                 'formula' : sen.formula,
                                                 'device_id' : sen.device_id,
                                                 'last_value' : sen.last_value,
                                                 'value_max' : sen.value_max,
                                                 'name' : sen.name}
                #print(self.all_sensors)


                    
                for dev in self._db.list_devices():
                    #print(dev)
                    #{'xpl_stats': {u'get_total_space': {'json_id': u'get_total_space', 'schema': u'sensor.basic', 'id': 3, 'parameters': {'dynamic': [{'ignore_values': u'', 'sensor_name': u'get_total_space', 'key': u'current'}], 'static': [{'type': u'string', 'value': u'/', 'key': u'device'}, {'type': None, 'value': u'total_space', 'key': u'type'}]}, 'name': u'Total space'}, u'get_free_space': {'json_id': u'get_free_space', 'schema': u'sensor.basic', 'id': 4, 'parameters': {'dynamic': [{'ignore_values': u'', 'sensor_name': u'get_free_space', 'key': u'current'}], 'static': [{'type': u'string', 'value': u'/', 'key': u'device'}, {'type': None, 'value': u'free_space', 'key': u'type'}]}, 'name': u'Free space'}, u'get_used_space': {'json_id': u'get_used_space', 'schema': u'sensor.basic', 'id': 5, 'parameters': {'dynamic': [{'ignore_values': u'', 'sensor_name': u'get_used_space', 'key': u'current'}], 'static': [{'type': u'string', 'value': u'/', 'key': u'device'}, {'type': None, 'value': u'used_space', 'key': u'type'}]}, 'name': u'Used space'}, u'get_percent_used': {'json_id': u'get_percent_used', 'schema': u'sensor.basic', 'id': 6, 'parameters': {'dynamic': [{'ignore_values': u'', 'sensor_name': u'get_percent_used', 'key': u'current'}], 'static': [{'type': u'string', 'value': u'/', 'key': u'device'}, {'type': None, 'value': u'percent_used', 'key': u'type'}]}, 'name': u'Percent used'}}, 'commands': {}, 'description': u'', 'reference': u'', 'sensors': {u'get_total_space': {'value_min': None, 'data_type': u'DT_Byte', 'incremental': False, 'id': 57, 'reference': u'get_total_space', 'conversion': u'', 'name': u'Total Space', 'last_received': 1459192737, 'timeout': 0, 'formula': None, 'last_value': u'14763409408', 'value_max': 14763409408.0}, u'get_free_space': {'value_min': None, 'data_type': u'DT_Byte', 'incremental': False, 'id': 59, 'reference': u'get_free_space', 'conversion': u'', 'name':u'Free Space', 'last_received': 1459192737, 'timeout': 0, 'formula': None, 'last_value': u'1319346176', 'value_max': 8220349952.0}, u'get_used_space': {'value_min': None, 'data_type': u'DT_Byte', 'incremental': False, 'id': 60, 'reference': u'get_used_space', 'conversion': u'', 'name': u'Used Space', 'last_received': 1459192737, 'timeout': 0, 'formula': None, 'last_value': u'13444063232', 'value_max': 14763409408.0}, u'get_percent_used': {'value_min': None, 'data_type':u'DT_Scaling', 'incremental': False, 'id': 58, 'reference': u'get_percent_used', 'conversion': u'', 'name': u'Percent used', 'last_received': 1459192737, 'timeout': 0, 'formula': None, 'last_value': u'91', 'value_max': 100.0}}, 'xpl_commands': {}, 'client_id': u'plugin-diskfree.ambre', 'device_type_id': u'diskfree.disk_usage', 'client_version': u'1.0', 'parameters': {u'interval': {'value': u'5', 'type': u'integer', 'id': 5, 'key': u'interval'}}, 'id': 3, 'name': u'Ambre /'}
                    self.all_devices[str(dev['id'])] = {
                                                    'client_id': dev['client_id'],
                                                    'id': dev['id'],
                                                    'name': dev['name'],
                                                  }
                #print(self.all_devices)
            self._log.info("Event : one device changed. Reloading data for _SensorStoreThread -- finished")

        def run(self):
            while True:
                try:
                    store = True
                    item = self._queue.get()
                    #self._log.debug(u"Getting item from the store queue, current length = {0}".format(self._queue.qsize()))
                    self._log.debug(u"Getting item from the store queue, current length = {0}, item = '{1}'".format(self._queue.qsize(), item))
                    # handle ignore
                    value = item['value']
                    senid = item['sensor_id']
                    current_date = item['time']
                    # get the sensor and dev
                    # TODO : DEBUG - LOG TO REMOVE
                    #self._log.debug(u"DEBUG - BEFORE THE with self._db.session_scope()")
                    with self._db.session_scope():
                        #self._log.debug(u"DEBUG - BEGINNING OF THE with self._db.session_scope()")
                        #sen = self._db.get_sensor(senid)
                        #dev = self._db.get_device(sen.device_id)
                        sen = self.all_sensors[str(senid)]
                        dev = self.all_devices[str(sen['device_id'])]
                        # check if we need a conversion
                        if sen['conversion'] is not None and sen['conversion'] != '':
                            if dev['client_id'] in self._conv() and sen['conversion'] in self._conv()[dev['client_id']]:
                                self._log.debug( \
                                    u"Calling conversion {0}".format(sen['conversion']))
                                exec(self._conv()[dev['client_id']][sen['conversion']])
                                value = locals()[sen['conversion']](value)
                        self._log.info( \
                                u"Storing stat for device '{0}' ({1}) and sensor '{2}' ({3}) with value '{4}' after conversion." \
                                .format(dev['name'], dev['id'], sen['name'], sen['id'], value))
                        try:
                            # do the store
                            value = self._db.add_sensor_history(\
                                    senid, \
                                    sen, \
                                    value, \
                                    current_date)
                            # publish the result
                            self._pub.send_event('device-stats', \
                                  {"timestamp" : current_date, \
                                  "device_id" : dev['id'], \
                                  "sensor_id" : senid, \
                                  "stored_value" : value})
                        except Exception as exp:
                            self._log.error(u"Error when adding sensor history : {0}".format(traceback.format_exc()))
                except queue.Empty:
                    # nothing in the queue, sleep for 1 second
                    time.sleep(1)
                except Exception as exp:
                    self._log.error(traceback.format_exc())
Exemplo n.º 6
0
    class _SensorThread(threading.Thread):
        """ SensorThread class
        Class that will handle the sensor storage in a seperated thread
        This will get messages from the SensorQueue
        """
        def __init__(self, log, queue, conv, pub):
            threading.Thread.__init__(self)
            self._db = DbHelper()
            self._log = log
            self._queue = queue
            self._conv = conv
            self._pub = pub

        def _find_storeparam(self, item):
            found = False
            tostore = []
            for xplstat in self._db.get_all_xpl_stat():
                sensors = 0
                matching = 0
                statics = 0
                if xplstat.schema == item["msg"].schema:
                    # we found a possible xplstat
                    # try to match all params and try to find a sensorid and a vlaue to store
                    for param in xplstat.params:
                        if param.key in item["msg"].data and param.static:
                            statics = statics + 1
                            if param.multiple is not None and len(param.multiple) == 1 and item["msg"].data[param.key] in param.value.split(param.multiple):
                                matching = matching + 1
                            elif item["msg"].data[param.key] == param.value:
                                matching = matching + 1
                    # now we have a matching xplstat, go and find all sensors
                    if matching == statics:
                        for param in xplstat.params:
                            if param.key in item["msg"].data and not param.static:
                                tostore.append( {'param': param, 'value': item["msg"].data[param.key]} )
                    if len(tostore) > 0:
                        found = True
            if found:
                return (found, tostore)
            else:
                return False

        def run(self):
            while True:
                try:
                    item = self._queue.get()
                    self._log.debug(u"Getting item from the sensorQueue, current length = {0}".format(self._queue.qsize()))
                    # if clientid is none, we don't know this sender so ignore
                    # TODO check temp disabled until external members are working
                    #if item["clientId"] is not None:
                    if True:
                        with self._db.session_scope():
                            fdata = self._find_storeparam(item)
                            if fdata:
                                self._log.debug(u"Found a matching sensor, so starting the storage procedure")
                                for data in fdata[1]:
                                    value = data['value']
                                    storeparam = data['param']
                                    current_date = calendar.timegm(time.gmtime())
                                    store = True
                                    if storeparam.ignore_values:
                                        if value in eval(storeparam.ignore_values):
                                            self._log.debug(u"Value {0} is in the ignore list {0}, so not storing.".format(value, storeparam.ignore_values))
                                            store = False
                                    if store:
                                        # get the sensor and dev
                                        sen = self._db.get_sensor(storeparam.sensor_id)
                                        dev = self._db.get_device(sen.device_id)
                                        # check if we need a conversion
                                        if sen.conversion is not None and sen.conversion != '':
                                            if dev['client_id'] in self._conv and sen.conversion in self._conv[dev['client_id']]:
                                                self._log.debug( \
                                                    u"Calling conversion {0}".format(sen.conversion))
                                                exec(self._conv[dev['client_id']][sen.conversion])
                                                value = locals()[sen.conversion](value)
                                        self._log.info( \
                                                u"Storing stat for device '{0}' ({1}) and sensor '{2}' ({3}): key '{4}' with value '{5}' after conversion." \
                                                .format(dev['name'], dev['id'], sen.name, sen.id, storeparam.key, value))
                                        # do the store
                                        try:
                                            self._db.add_sensor_history(\
                                                    storeparam.sensor_id, \
                                                    value, \
                                                    current_date)
                                        except Exception as exp:
                                            self._log.error(u"Error when adding sensor history : {0}".format(traceback.format_exc()))
                                    else:
                                        self._log.debug(u"Don't need to store this value")
                                    # publish the result
                                    self._pub.send_event('device-stats', \
                                              {"timestamp" : current_date, \
                                              "device_id" : dev['id'], \
                                              "sensor_id" : sen.id, \
                                              "stored_value" : value})

                except Queue.Empty:
                    # nothing in the queue, sleep for 1 second
                    time.sleep(1)
                except Exception as exp:
                    self._log.error(traceback.format_exc())
Exemplo n.º 7
0
    class _SensorStoreThread(threading.Thread):
        """ SensorStoreThread class
        Thread that will handle the sensorStore queue
        every item in this queue should be stored in the db
        - conversion will happend
        - formula applying
        - rounding
        - and eventually storing it in the db
        Its a thread to make sure it does not block anything else
        """
        def __init__(self, queue, log, get_conversion_map, pub):
            threading.Thread.__init__(self)
            self._log = log
            self._db = DbHelper()
            self._conv = get_conversion_map
            self._queue = queue
            self._pub = pub
            # on startup, load the device parameters
            self.on_device_changed()

        def on_device_changed(self):
            """ Function called when a device have been changed to reload the devices parameters
            """
            self._log.info(
                "Event : one device changed. Reloading data for _SensorStoreThread"
            )
            self.all_sensors = {}
            self.all_devices = {}
            with self._db.session_scope():
                for sen in self._db.get_all_sensor():
                    #print(sen)
                    #<Sensor: conversion='', value_min='None', history_round='0.0', reference='adco', data_type='DT_String', history_duplicate='False', last_received='1474968431', incremental='False', id='29', history_expire='0', timeout='180', history_store='True', history_max='0', formula='None', device_id='2', last_value='030928084432', value_max='3.09036843008e+11', name='Electric meter address'>
                    self.all_sensors[str(sen.id)] = {
                        'id': sen.id,
                        'conversion': sen.conversion,
                        'value_min': sen.value_min,
                        'history_round': sen.history_round,
                        'reference': sen.reference,
                        'data_type': sen.data_type,
                        'history_duplicate': sen.history_duplicate,
                        'last_received': sen.last_received,
                        'incremental': sen.incremental,
                        'history_expire': sen.history_expire,
                        'timeout': sen.timeout,
                        'history_store': sen.history_store,
                        'history_max': sen.history_max,
                        'formula': sen.formula,
                        'device_id': sen.device_id,
                        'last_value': sen.last_value,
                        'value_max': sen.value_max,
                        'name': sen.name
                    }
                #print(self.all_sensors)

                for dev in self._db.list_devices():
                    #print(dev)
                    #{'xpl_stats': {u'get_total_space': {'json_id': u'get_total_space', 'schema': u'sensor.basic', 'id': 3, 'parameters': {'dynamic': [{'ignore_values': u'', 'sensor_name': u'get_total_space', 'key': u'current'}], 'static': [{'type': u'string', 'value': u'/', 'key': u'device'}, {'type': None, 'value': u'total_space', 'key': u'type'}]}, 'name': u'Total space'}, u'get_free_space': {'json_id': u'get_free_space', 'schema': u'sensor.basic', 'id': 4, 'parameters': {'dynamic': [{'ignore_values': u'', 'sensor_name': u'get_free_space', 'key': u'current'}], 'static': [{'type': u'string', 'value': u'/', 'key': u'device'}, {'type': None, 'value': u'free_space', 'key': u'type'}]}, 'name': u'Free space'}, u'get_used_space': {'json_id': u'get_used_space', 'schema': u'sensor.basic', 'id': 5, 'parameters': {'dynamic': [{'ignore_values': u'', 'sensor_name': u'get_used_space', 'key': u'current'}], 'static': [{'type': u'string', 'value': u'/', 'key': u'device'}, {'type': None, 'value': u'used_space', 'key': u'type'}]}, 'name': u'Used space'}, u'get_percent_used': {'json_id': u'get_percent_used', 'schema': u'sensor.basic', 'id': 6, 'parameters': {'dynamic': [{'ignore_values': u'', 'sensor_name': u'get_percent_used', 'key': u'current'}], 'static': [{'type': u'string', 'value': u'/', 'key': u'device'}, {'type': None, 'value': u'percent_used', 'key': u'type'}]}, 'name': u'Percent used'}}, 'commands': {}, 'description': u'', 'reference': u'', 'sensors': {u'get_total_space': {'value_min': None, 'data_type': u'DT_Byte', 'incremental': False, 'id': 57, 'reference': u'get_total_space', 'conversion': u'', 'name': u'Total Space', 'last_received': 1459192737, 'timeout': 0, 'formula': None, 'last_value': u'14763409408', 'value_max': 14763409408.0}, u'get_free_space': {'value_min': None, 'data_type': u'DT_Byte', 'incremental': False, 'id': 59, 'reference': u'get_free_space', 'conversion': u'', 'name':u'Free Space', 'last_received': 1459192737, 'timeout': 0, 'formula': None, 'last_value': u'1319346176', 'value_max': 8220349952.0}, u'get_used_space': {'value_min': None, 'data_type': u'DT_Byte', 'incremental': False, 'id': 60, 'reference': u'get_used_space', 'conversion': u'', 'name': u'Used Space', 'last_received': 1459192737, 'timeout': 0, 'formula': None, 'last_value': u'13444063232', 'value_max': 14763409408.0}, u'get_percent_used': {'value_min': None, 'data_type':u'DT_Scaling', 'incremental': False, 'id': 58, 'reference': u'get_percent_used', 'conversion': u'', 'name': u'Percent used', 'last_received': 1459192737, 'timeout': 0, 'formula': None, 'last_value': u'91', 'value_max': 100.0}}, 'xpl_commands': {}, 'client_id': u'plugin-diskfree.ambre', 'device_type_id': u'diskfree.disk_usage', 'client_version': u'1.0', 'parameters': {u'interval': {'value': u'5', 'type': u'integer', 'id': 5, 'key': u'interval'}}, 'id': 3, 'name': u'Ambre /'}
                    self.all_devices[str(dev['id'])] = {
                        'client_id': dev['client_id'],
                        'id': dev['id'],
                        'name': dev['name'],
                    }
                #print(self.all_devices)
            self._log.info(
                "Event : one device changed. Reloading data for _SensorStoreThread -- finished"
            )

        def run(self):
            while True:
                try:
                    store = True
                    item = self._queue.get()
                    #self._log.debug(u"Getting item from the store queue, current length = {0}".format(self._queue.qsize()))
                    self._log.debug(
                        u"Getting item from the store queue, current length = {0}, item = '{1}'"
                        .format(self._queue.qsize(), item))
                    # handle ignore
                    value = item['value']
                    senid = item['sensor_id']
                    current_date = item['time']
                    # get the sensor and dev
                    # TODO : DEBUG - LOG TO REMOVE
                    #self._log.debug(u"DEBUG - BEFORE THE with self._db.session_scope()")
                    with self._db.session_scope():
                        #self._log.debug(u"DEBUG - BEGINNING OF THE with self._db.session_scope()")
                        #sen = self._db.get_sensor(senid)
                        #dev = self._db.get_device(sen.device_id)
                        sen = self.all_sensors[str(senid)]
                        dev = self.all_devices[str(sen['device_id'])]
                        # check if we need a conversion
                        if sen['conversion'] is not None and sen[
                                'conversion'] != '':
                            if dev['client_id'] in self._conv(
                            ) and sen['conversion'] in self._conv()[
                                    dev['client_id']]:
                                self._log.debug( \
                                    u"Calling conversion {0}".format(sen['conversion']))
                                exec(self._conv()[dev['client_id']][
                                    sen['conversion']])
                                value = locals()[sen['conversion']](value)
                        self._log.info( \
                                u"Storing stat for device '{0}' ({1}) and sensor '{2}' ({3}) with value '{4}' after conversion." \
                                .format(dev['name'], dev['id'], sen['name'], sen['id'], value))
                        try:
                            # do the store
                            value = self._db.add_sensor_history(\
                                    senid, \
                                    sen, \
                                    value, \
                                    current_date)
                            # publish the result
                            self._pub.send_event('device-stats', \
                                  {"timestamp" : current_date, \
                                  "device_id" : dev['id'], \
                                  "sensor_id" : senid, \
                                  "stored_value" : value})
                        except Exception as exp:
                            self._log.error(
                                u"Error when adding sensor history : {0}".
                                format(traceback.format_exc()))
                except queue.Empty:
                    # nothing in the queue, sleep for 1 second
                    time.sleep(1)
                except Exception as exp:
                    self._log.error(traceback.format_exc())