本文整理匯總了Python中domogik.common.database.DbHelper.get_sensor方法的典型用法代碼示例。如果您正苦於以下問題:Python DbHelper.get_sensor方法的具體用法?Python DbHelper.get_sensor怎麽用?Python DbHelper.get_sensor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類domogik.common.database.DbHelper
的用法示例。
在下文中一共展示了DbHelper.get_sensor方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _callback
# 需要導入模塊: from domogik.common.database import DbHelper [as 別名]
# 或者: from domogik.common.database.DbHelper import get_sensor [as 別名]
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())
示例2: _SensorThread
# 需要導入模塊: from domogik.common.database import DbHelper [as 別名]
# 或者: from domogik.common.database.DbHelper import get_sensor [as 別名]
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(\
#.........這裏部分代碼省略.........
示例3: _SensorThread
# 需要導入模塊: from domogik.common.database import DbHelper [as 別名]
# 或者: from domogik.common.database.DbHelper import get_sensor [as 別名]
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())