本文整理汇总了Python中domogik.common.database.DbHelper.session_scope方法的典型用法代码示例。如果您正苦于以下问题:Python DbHelper.session_scope方法的具体用法?Python DbHelper.session_scope怎么用?Python DbHelper.session_scope使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类domogik.common.database.DbHelper
的用法示例。
在下文中一共展示了DbHelper.session_scope方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: upgrade
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import session_scope [as 别名]
def upgrade():
db = DbHelper()
with db.session_scope():
db.add_user_account_with_person('Anonymous', 'Anonymous', 'Rest', 'Anonymous')
del db
pass
示例2: _callback
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import session_scope [as 别名]
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})
示例3: user
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import session_scope [as 别名]
class ScenarioManager:
""" Manage scenarios : create them, evaluate them, etc ...
A scenario instance contains a condition, which is a boolean
combination of many tests,
and a list of actions
Each test can be :
- test on the test of any device
- test on the time
- action triggered by user (click on UI for ex)
The test on devices are managed directly by xpl Listeners
The test on time will be managed by a TimeManager
The actions will be managed by an ActionManager
{
"condition" :
{ "AND" : {
"OR" : {
"one-uuid" : {
"param_name_1" : {
"token1" : "value",
"token2" : "othervalue"
},
"param_name_2" : {
"token3" : "foo"
}
},
"another-uuid" : {
"param_name_1" : {
"token4" : "bar"
}
}
},
"yet-another-uuid" : {
"param_name_1" : {
"url" : "http://google.fr",
"interval" : "5"
}
}
}
},
"actions" : [
"uid-for-action" : {
"param1" : "value1",
"param2" : "value2"
},
"uid-for-action2" : {
"param3" : "value3"
}
]
}
"""
def __init__(self, log):
""" Create ScenarioManager instance
@param log : Logger instance
"""
# Keep list of conditions as name : instance
self._instances = {}
# an instance of the logger
self.log = log
# load all scenarios from the DB
self._db = DbHelper()
self.load_scenarios()
def load_scenarios(self):
""" Loads all scenarios from the db
for each scenario call the create_scenario method
"""
with self._db.session_scope():
for scenario in self._db.list_scenario():
self.create_scenario(scenario.name, scenario.json, int(scenario.id), scenario.disabled, scenario.description)
def shutdown(self):
""" Callback to shut down all parameters
"""
for cond in self._conditions.keys():
self.delete_scenario(cond, db_delete=False)
def get_parsed_condition(self, name):
""" Call cond.get_parsed_condition on the cond with name 'name'
@param name : name of the Condition
@return {'name':name, 'data': parsed_condition} or raise Exception
"""
if name not in self._conditions:
raise KeyError('no key %s in conditions table' % name)
else:
parsed = self._conditions[name].get_parsed_condition()
return {'name': name, 'data': parsed}
def update_scenario(self, cid, name, json_input, dis, desc):
if int(cid) != 0:
self.del_scenario(cid, False)
return self.create_scenario(name, json_input, cid, dis, desc, True)
def del_scenario(self, cid, doDB=True):
try:
if cid == '' or int(cid) not in self._instances.keys():
self.log.info(u"Scenario deletion : id '{0}' doesn't exist".format(cid))
return {'status': 'ERROR', 'msg': u"Scenario {0} doesn't exist".format(cid)}
else:
self._instances[int(cid)]['instance'].destroy()
#.........这里部分代码省略.........
示例4: XplManager
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import session_scope [as 别名]
#.........这里部分代码省略.........
for cli in data:
tmp[cli] = data[cli]['xpl_source']
self.client_xpl_map = tmp
def _load_conversions(self):
""" Request the client conversion info
This is an mq req to manager
"""
cli = MQSyncReq(self.zmq)
msg = MQMessage()
msg.set_action('client.conversion.get')
response = cli.request('manager', msg.get(), timeout=10)
if response:
self._parse_conversions(response.get_data())
else:
self.log.error(\
u"Updating conversion list failed, no response from manager")
def _parse_conversions(self, data):
""" Translate the mq data into a dict
"""
tmp = {}
for cli in data:
tmp[cli] = data[cli]
self.client_conversion_map = tmp
def _send_xpl_command(self, data):
""" Reply to config.get MQ req
@param data : MQ req message
Needed info in data:
- cmdid => command id to send
- cmdparams => key/value pair of all params needed for this command
"""
with self._db.session_scope():
self.log.info(u"Received new cmd request: {0}".format(data))
failed = False
request = data.get_data()
if 'cmdid' not in request:
failed = "cmdid not in message data"
if 'cmdparams' not in request:
failed = "cmdparams not in message data"
if not failed:
# get the command
cmd = self._db.get_command(request['cmdid'])
if cmd is not None:
if cmd.xpl_command is not None:
xplcmd = cmd.xpl_command
xplstat = self._db.get_xpl_stat(xplcmd.stat_id)
if xplstat is not None:
# get the device from the db
dev = self._db.get_device(int(cmd.device_id))
msg = XplMessage()
if not dev['client_id'] in self.client_xpl_map.keys():
self._load_client_to_xpl_target()
if not dev['client_id'] in self.client_xpl_map.keys():
failed = "Can not fincd xpl source for {0} client_id".format(dev['client_id'])
else:
msg.set_target(self.client_xpl_map[dev['client_id']])
msg.set_source(self.myxpl.get_source())
msg.set_type("xpl-cmnd")
msg.set_schema(xplcmd.schema)
# static paramsw
for par in xplcmd.params:
msg.add_data({par.key : par.value})
# dynamic params
for par in cmd.params:
示例5: _SensorThread
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import session_scope [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(\
#.........这里部分代码省略.........
示例6: XplManager
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import session_scope [as 别名]
class XplManager(XplPlugin, MQAsyncSub):
""" Statistics manager
"""
def __init__(self):
""" Initiate DbHelper, Logs and config
"""
XplPlugin.__init__(self, 'xplgw', log_prefix = "")
MQAsyncSub.__init__(self, self.zmq, 'xplgw', ['client.conversion', 'client.list'])
self.log.info(u"XPL manager initialisation...")
self._db = DbHelper()
self.pub = MQPub(zmq.Context(), 'xplgw')
self.stats = None
self.client_xpl_map = {}
self.client_conversion_map = {}
self._load_client_to_xpl_target()
self._load_conversions()
self.load()
self.ready()
def on_mdp_request(self, msg):
# XplPlugin handles MQ Req/rep also
try:
XplPlugin.on_mdp_request(self, msg)
if msg.get_action() == "reload":
self.load()
msg = MQMessage()
msg.set_action( 'reload.result' )
self.reply(msg.get())
elif msg.get_action() == "cmd.send":
self._send_xpl_command(msg)
except:
self.log.error(traceback.format_exc())
def on_message(self, msgid, content):
try:
if msgid == 'client.conversion':
self._parse_conversions(content)
elif msgid == 'client.list':
self._parse_xpl_target(content)
except:
self.log.error(traceback.format_exc())
def _load_client_to_xpl_target(self):
cli = MQSyncReq(self.zmq)
msg = MQMessage()
msg.set_action('client.list.get')
response = cli.request('manager', msg.get(), timeout=10)
if response:
self._parse_xpl_target(response.get_data())
else:
self.log.error(u"Updating client list was not successfull, no response from manager")
def _parse_xpl_target(self, data):
tmp = {}
for cli in data:
tmp[cli] = data[cli]['xpl_source']
self.client_xpl_map = tmp
def _load_conversions(self):
cli = MQSyncReq(self.zmq)
msg = MQMessage()
msg.set_action('client.conversion.get')
response = cli.request('manager', msg.get(), timeout=10)
if response:
self._parse_conversions(response.get_data())
else:
self.log.error(u"Updating client conversion list was not successfull, no response from manager")
def _parse_conversions(self, data):
tmp = {}
for cli in data:
tmp[cli] = data[cli]
self.client_conversion_map = tmp
def _send_xpl_command(self, data):
""" Reply to config.get MQ req
@param data : MQ req message
Needed info in data:
- cmdid => command id to send
- cmdparams => key/value pair of all params needed for this command
"""
with self._db.session_scope():
self.log.info(u"Received new cmd request: {0}".format(data))
failed = False
request = data.get_data()
if 'cmdid' not in request:
failed = "cmdid not in message data"
if 'cmdparams' not in request:
failed = "cmdparams not in message data"
if not failed:
# get the command
cmd = self._db.get_command(request['cmdid'])
if cmd is not None:
if cmd.xpl_command is not None:
xplcmd = cmd.xpl_command
xplstat = self._db.get_xpl_stat(xplcmd.stat_id)
if xplstat is not None:
#.........这里部分代码省略.........
示例7: DBConnector
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import session_scope [as 别名]
class DBConnector(Plugin, MQRep):
'''
Manage the connection between database and the plugins
Should be the *only* object along with the StatsManager to access to the database on the core side
'''
def __init__(self):
'''
Initialize database and xPL connection
'''
Plugin.__init__(self, 'dbmgr', log_prefix='core_')
# Already done in Plugin
#MQRep.__init__(self, zmq.Context(), 'dbmgr')
self.log.debug(u"Init database_manager instance")
# Check for database connexion
self._db = DbHelper()
with self._db.session_scope():
# TODO : move in a function and use it (also used in dbmgr)
nb_test = 0
db_ok = False
while not db_ok and nb_test < DATABASE_CONNECTION_NUM_TRY:
nb_test += 1
try:
self._db.list_user_accounts()
db_ok = True
except:
msg = "The database is not responding. Check your configuration of if the database is up. Test {0}/{1}. The error while trying to connect to the database is : {2}".format(nb_test, DATABASE_CONNECTION_NUM_TRY, traceback.format_exc())
self.log.error(msg)
msg = "Waiting for {0} seconds".format(DATABASE_CONNECTION_WAIT)
self.log.info(msg)
time.sleep(DATABASE_CONNECTION_WAIT)
if nb_test >= DATABASE_CONNECTION_NUM_TRY:
msg = "Exiting dbmgr!"
self.log.error(msg)
self.force_leave()
return
msg = "Connected to the database"
self.log.info(msg)
try:
self._engine = self._db.get_engine()
except:
self.log.error(u"Error while starting database engine : {0}".format(traceback.format_exc()))
self.force_leave()
return
self.ready()
# Already done in ready()
#IOLoop.instance().start()
def on_mdp_request(self, msg):
""" Handle Requests over MQ
@param msg : MQ req message
"""
try:
with self._db.session_scope():
# Plugin handles MQ Req/rep also
Plugin.on_mdp_request(self, msg)
# configuration
if msg.get_action() == "config.get":
self._mdp_reply_config_get(msg)
elif msg.get_action() == "config.set":
self._mdp_reply_config_set(msg)
elif msg.get_action() == "config.delete":
self._mdp_reply_config_delete(msg)
# devices list
elif msg.get_action() == "device.get":
self._mdp_reply_devices_result(msg)
# device get params
elif msg.get_action() == "device.params":
self._mdp_reply_devices_params_result(msg)
# device create
elif msg.get_action() == "device.create":
self._mdp_reply_devices_create_result(msg)
# device delete
elif msg.get_action() == "device.delete":
self._mdp_reply_devices_delete_result(msg)
# device update
elif msg.get_action() == "device.update":
self._mdp_reply_devices_update_result(msg)
# deviceparam update
elif msg.get_action() == "deviceparam.update":
self._mdp_reply_deviceparam_update_result(msg)
# sensor update
elif msg.get_action() == "sensor.update":
self._mdp_reply_sensor_update_result(msg)
# sensor history
elif msg.get_action() == "sensor_history.get":
self._mdp_reply_sensor_history(msg)
except:
msg = "Error while processing request. Message is : {0}. Error is : {1}".format(msg, traceback.format_exc())
self.log.error(msg)
def _mdp_reply_config_get(self, data):
""" Reply to config.get MQ req
@param data : MQ req message
"""
msg = MQMessage()
#.........这里部分代码省略.........
示例8: DBConnector
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import session_scope [as 别名]
class DBConnector(Plugin, MQRep):
'''
Manage the connection between database and the plugins
Should be the *only* object along with the StatsManager to access to the database on the core side
'''
def __init__(self):
'''
Initialize database and xPL connection
'''
Plugin.__init__(self, 'dbmgr')
# Already done in Plugin
#MQRep.__init__(self, zmq.Context(), 'dbmgr')
self.log.debug(u"Init database_manager instance")
# Check for database connexion
self._db = DbHelper()
with self._db.session_scope():
nb_test = 0
db_ok = False
while not db_ok and nb_test < DATABASE_CONNECTION_NUM_TRY:
nb_test += 1
try:
self._db.list_user_accounts()
db_ok = True
except:
msg = "The database is not responding. Check your configuration of if the database is up. Test {0}/{1}".format(nb_test, DATABASE_CONNECTION_NUM_TRY)
self.log.error(msg)
msg = "Waiting for {0} seconds".format(DATABASE_CONNECTION_WAIT)
self.log.info(msg)
time.sleep(DATABASE_CONNECTION_WAIT)
if nb_test >= DATABASE_CONNECTION_NUM_TRY:
msg = "Exiting dbmgr!"
self.log.error(msg)
self.force_leave()
return
msg = "Connected to the database"
self.log.info(msg)
try:
self._engine = self._db.get_engine()
except:
self.log.error(u"Error while starting database engine : {0}".format(traceback.format_exc()))
self.force_leave()
return
self.ready()
# Already done in ready()
#IOLoop.instance().start()
def on_mdp_request(self, msg):
""" Handle Requests over MQ
@param msg : MQ req message
"""
try:
with self._db.session_scope():
# Plugin handles MQ Req/rep also
Plugin.on_mdp_request(self, msg)
# configuration
if msg.get_action() == "config.get":
self._mdp_reply_config_get(msg)
elif msg.get_action() == "config.set":
self._mdp_reply_config_set(msg)
elif msg.get_action() == "config.delete":
self._mdp_reply_config_delete(msg)
# devices list
elif msg.get_action() == "device.get":
self._mdp_reply_devices_result(msg)
# device get params
elif msg.get_action() == "device.params":
self._mdp_reply_devices_params_result(msg)
# device create
elif msg.get_action() == "device.create":
self._mdp_reply_devices_create_result(msg)
# device delete
elif msg.get_action() == "device.delete":
self._mdp_reply_devices_delete_result(msg)
except:
msg = "Error while processing request. Message is : {0}. Error is : {1}".format(msg, traceback.format_exc())
self.log.error(msg)
def _mdp_reply_config_get(self, data):
""" Reply to config.get MQ req
@param data : MQ req message
"""
msg = MQMessage()
msg.set_action('config.result')
status = True
msg_data = data.get_data()
if 'type' not in msg_data:
status = False
reason = "Config request : missing 'type' field : {0}".format(data)
if msg_data['type'] != "plugin":
status = False
reason = "Config request not available for type={0}".format(msg_data['type'])
if 'name' not in msg_data:
status = False
#.........这里部分代码省略.........
示例9: DBConnector
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import session_scope [as 别名]
class DBConnector(XplPlugin, MQRep):
'''
Manage the connection between database and the plugins
Should be the *only* object along with the StatsManager to access to the database on the core side
'''
def __init__(self):
'''
Initialize database and xPL connection
'''
XplPlugin.__init__(self, 'dbmgr')
# Already done in XplPlugin
#MQRep.__init__(self, zmq.Context(), 'dbmgr')
self.log.debug(u"Init database_manager instance")
# Check for database connexion
self._db = DbHelper()
with self._db.session_scope():
nb_test = 0
db_ok = False
while not db_ok and nb_test < DATABASE_CONNECTION_NUM_TRY:
nb_test += 1
try:
self._db.list_user_accounts()
db_ok = True
except:
msg = "The database is not responding. Check your configuration of if the database is up. Test {0}/{1}".format(nb_test, DATABASE_CONNECTION_NUM_TRY)
self.log.error(msg)
msg = "Waiting for {0} seconds".format(DATABASE_CONNECTION_WAIT)
self.log.info(msg)
time.sleep(DATABASE_CONNECTION_WAIT)
if nb_test >= DATABASE_CONNECTION_NUM_TRY:
msg = "Exiting dbmgr!"
self.log.error(msg)
self.force_leave()
return
msg = "Connected to the database"
self.log.info(msg)
try:
self._engine = self._db.get_engine()
except:
self.log.error(u"Error while starting database engine : {0}".format(traceback.format_exc()))
self.force_leave()
return
self.ready()
# Already done in ready()
#IOLoop.instance().start()
def on_mdp_request(self, msg):
""" Handle Requests over MQ
@param msg : MQ req message
"""
with self._db.session_scope():
# XplPlugin handles MQ Req/rep also
XplPlugin.on_mdp_request(self, msg)
# configuration
if msg.get_action() == "config.get":
self._mdp_reply_config_get(msg)
elif msg.get_action() == "config.set":
self._mdp_reply_config_set(msg)
elif msg.get_action() == "config.delete":
self._mdp_reply_config_delete(msg)
# devices list
elif msg.get_action() == "device.get":
self._mdp_reply_devices_result(msg)
def _mdp_reply_config_get(self, data):
""" Reply to config.get MQ req
@param data : MQ req message
"""
msg = MQMessage()
msg.set_action('config.result')
status = True
msg_data = data.get_data()
if 'type' not in msg_data:
status = False
reason = "Config request : missing 'type' field : {0}".format(data)
if msg_data['type'] != "plugin":
status = False
reason = "Config request not available for type={0}".format(msg_data['type'])
if 'name' not in msg_data:
status = False
reason = "Config request : missing 'name' field : {0}".format(data)
if 'host' not in msg_data:
status = False
reason = "Config request : missing 'host' field : {0}".format(data)
if 'key' not in msg_data:
get_all_keys = True
key = "*"
else:
get_all_keys = False
key = msg_data['key']
#.........这里部分代码省略.........
示例10: _SensorStoreThread
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import session_scope [as 别名]
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)
#.........这里部分代码省略.........
示例11: _XplSensorThread
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import session_scope [as 别名]
class _XplSensorThread(threading.Thread):
""" XplSensorThread class
Thread that will handle the received xplStat(s) and xplTrigger(s)
It will try to find the matching sensor and then store it into the sensor Store Queue
This is done in a thread as it can be time consuming to do the DB lookups
"""
def __init__(self, log, queue, storeQueue):
threading.Thread.__init__(self)
self._db = DbHelper()
self._log = log
self._queue = queue
self._queue_store = storeQueue
# 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 _XplSensorThread")
self.all_xpl_stat = []
with self._db.session_scope():
for xplstat in self._db.get_all_xpl_stat():
#print(xplstat)
#print(xplstat.params)
# <XplStat: name='Open/Close sensor', json_id='open_close', device_id='95', id='185', schema='ac.basic'>
# <XplStatParam: xplstat_id='188', multiple='None', value='None', ignore_values='', sensor_id='411', static='False', key='current', type='None'>
a_xplstat = {'name' : xplstat.name,
'json_id' : xplstat.json_id,
'device_id' : xplstat.device_id,
'id' : xplstat.id,
'schema' : xplstat.schema,
'params' : []
}
for a_xplstat_param in xplstat.params:
a_xplstat['params'].append({
'xplstat_id' : a_xplstat_param.xplstat_id,
'multiple' : a_xplstat_param.multiple,
'value' : a_xplstat_param.value,
'ignore_values' : a_xplstat_param.ignore_values,
'sensor_id' : a_xplstat_param.sensor_id,
'static' : a_xplstat_param.static,
'key' : a_xplstat_param.key,
'type' : a_xplstat_param.type
})
self.all_xpl_stat.append(a_xplstat)
#print(self.all_xpl_stat)
self._log.info("Event : one device changed. Reloading data for _XplSensorThread -- finished")
def _find_storeparam(self, item):
#print("ITEM = {0}".format(item['msg']))
found = False
tostore = []
#for xplstat in self._db.get_all_xpl_stat():
for xplstat in self.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:
#.........这里部分代码省略.........
示例12: XplManager
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import session_scope [as 别名]
#.........这里部分代码省略.........
def on_message(self, msgid, content):
""" Method called on a subscribed message
"""
try:
XplPlugin.on_message(self, msgid, content)
if msgid == 'client.conversion':
self._parse_conversions(content)
elif msgid == 'client.list':
self._parse_xpl_target(content)
elif msgid == 'client.sensor':
self._handle_mq_sensor(content)
elif msgid == 'device.update':
self._handle_mq_device_update(content)
except Exception as exp:
self.log.error(traceback.format_exc())
def _handle_mq_device_update(self, content):
""" On a device change, a Mq message is received
So we update all the devices parameters used by xplgw
"""
self.log.debug(u"New message (from MQ) about some device changes > reload the devices parameters...")
self._x_thread.on_device_changed()
self._s_thread.on_device_changed()
self._reload_devices()
self._reload_commands()
def _reload_devices(self):
""" Reload the commands
"""
self.log.info("Event : one device changed. Reloading data for devices (light data)")
self.all_devices = {}
with self._db.session_scope():
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 devices (light data) -- finished")
def _reload_commands(self):
""" Reload the commands
"""
self.log.info("Event : one device changed. Reloading data for commands")
self.all_commands = {}
with self._db.session_scope():
for cmd in self._db.get_all_command():
#print(cmd)
#<Command: return_confirmation='True', name='Swith', reference='switch_lighting2', id='6', device_id='21'>
#print(cmd.params)
#[<CommandParam: data_type='DT_Trigger', conversion='', cmd_id='16', key='state'>]
self.all_commands[str(cmd.id)] = {
'return_confirmation' : cmd.return_confirmation,
'name' : cmd.name,
'reference' : cmd.reference,
'id' : cmd.id,
'device_id' : cmd.device_id,
'xpl_command' : None,
'params' : []
}
示例13: _SensorThread
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import session_scope [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())
示例14: user
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import session_scope [as 别名]
class ScenarioManager:
""" Manage scenarios : create them, evaluate them, etc ...
A scenario instance contains a condition, which is a boolean
combination of many tests,
and a list of actions
Each test can be :
- test on the test of any device
- test on the time
- action triggered by user (click on UI for ex)
The test on devices are managed directly by xpl Listeners
The test on time will be managed by a TimeManager
The actions will be managed by an ActionManager
{
"condition" :
{ "AND" : {
"OR" : {
"one-uuid" : {
"param_name_1" : {
"token1" : "value",
"token2" : "othervalue"
},
"param_name_2" : {
"token3" : "foo"
}
},
"another-uuid" : {
"param_name_1" : {
"token4" : "bar"
}
}
},
"yet-another-uuid" : {
"param_name_1" : {
"url" : "http://google.fr",
"interval" : "5"
}
}
}
},
"actions" : [
"uid-for-action" : {
"param1" : "value1",
"param2" : "value2"
},
"uid-for-action2" : {
"param3" : "value3"
}
]
}
"""
def __init__(self, log):
""" Create ScenarioManager instance
@param log : Logger instance
"""
# Keep uuid <-> instance mapping for tests and actions
self._tests_mapping = {}
self._actions_mapping = {}
# Keep list of conditions as name : instance
self._conditions = {}
# Keep list of actions uuid linked to a condition as name : [uuid1, uuid2, ... ]
self._conditions_actions = {}
# an instance of the logger
self.log = log
# As we lazy-load all the tests/actions in __instanciate
# we need to keep the module in a list so that we don't need to reload them
# every time.
self._test_cache = {}
self._action_cache = {}
# load all scenarios from the DB
self._db = DbHelper()
self.load_scenarios()
def load_scenarios(self):
""" Loads all scenarios from the db
for each scenario call the create_scenario method
"""
with self._db.session_scope():
for scenario in self._db.list_scenario():
# add all uuids to the mappings
for uid in scenario.uuids:
if uid.is_test:
self.__ask_instance(uid.key, self._tests_mapping, uid.uuid)
else:
self.__ask_instance(uid.key, self._actions_mapping, uid.uuid)
# now all uuids are created go and install the condition
self.create_scenario(scenario.name, scenario.json, store=False)
for (name, cond) in self._conditions.items():
cond.parse_condition()
def __ask_instance(self, obj, mapping, set_uuid=None):
""" Generate an uuid corresponding to the object passed as parameter
@param obj : a string as "objectmodule.objectClass" to instanciate
@param mapping : in what map to store the new uuid
@param uuid : if the uuid is provided, don't generate it
@return an uuid referencing a new instance of the object
"""
if set_uuid is None:
_uuid = self.get_uuid()
else:
#.........这里部分代码省略.........