本文整理汇总了Python中domogik.common.database.DbHelper.get_xpl_stat方法的典型用法代码示例。如果您正苦于以下问题:Python DbHelper.get_xpl_stat方法的具体用法?Python DbHelper.get_xpl_stat怎么用?Python DbHelper.get_xpl_stat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类domogik.common.database.DbHelper
的用法示例。
在下文中一共展示了DbHelper.get_xpl_stat方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: XplManager
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import get_xpl_stat [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:
#.........这里部分代码省略.........
示例2: XplManager
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import get_xpl_stat [as 别名]
#.........这里部分代码省略.........
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:
if par.key in request['cmdparams']:
value = request['cmdparams'][par.key]
# chieck if we need a conversion
if par.conversion is not None and par.conversion != '':
if dev['client_id'] in self.client_conversion_map:
if par.conversion in self.client_conversion_map[dev['client_id']]:
exec(self.client_conversion_map[dev['client_id']][par.conversion])
value = locals()[par.conversion](value)
msg.add_data({par.key : value})
else:
failed = "Parameter ({0}) for device command msg is not provided in the mq message".format(par.key)
if not failed:
# send out the msg
self.log.debug(u"Sending xplmessage: {0}".format(msg))
示例3: __init__
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import get_xpl_stat [as 别名]
class StatsManager:
"""
Listen on the xPL network and keep stats of device and system state
"""
def __init__(self, handler_params, xpl):
"""
@param handler_params : The server params
@param xpl : A xPL Manager instance
"""
try:
self.myxpl = xpl
# logging initialization
log = logger.Logger('rest-stat')
self._log_stats = log.get_logger('rest-stat')
self._log_stats.info("Rest Stat Manager initialisation...")
# create the dbHelper
self._db = DbHelper()
### Rest data
self.handler_params = handler_params
self.handler_params.append(self._log_stats)
self.handler_params.append(self._db)
self._event_requests = self.handler_params[0]._event_requests
self.get_exception = self.handler_params[0].get_exception
self.stats = None
except :
self._log_stats.error("%s" % traceback.format_exc())
def load(self):
""" (re)load all xml files to (re)create _Stats objects
"""
self._log_stats.info("Rest Stat Manager loading.... ")
try:
# not the first load : clean
if self.stats != None:
for x in self.stats:
self.myxpl.del_listener(x.get_listener())
### Load stats
# key1, key2 = device_type_id, schema
self.stats = []
for sen in self._db.get_all_sensor():
self._log_stats.debug(sen)
statparam = self._db.get_xpl_stat_param_by_sensor(sen.id)
if statparam is None:
self._log_stats.error('Corresponding xpl-stat param can not be found for sensor %s' % (sen))
continue
stat = self._db.get_xpl_stat(statparam.xplstat_id)
if stat is None:
self._log_stats.error('Corresponding xpl-stat can not be found for xplstatparam %s' % (statparam))
continue
dev = self._db.get_device(stat.device_id)
if dev is None:
self._log_stats.error('Corresponding device can not be found for xpl-stat %s' % (stat))
continue
# xpl-trig
self.stats.append(self._Stat(self.myxpl, dev, stat, sen, \
"xpl-trig", self._log_stats, \
self._log_stats, self._db, \
self._event_requests))
# xpl-stat
self.stats.append(self._Stat(self.myxpl, dev, stat, sen, \
"xpl-stat", self._log_stats, \
self._log_stats, self._db, \
self._event_requests))
except:
self._log_stats.error("%s" % traceback.format_exc())
class _Stat:
""" This class define a statistic parser and logger instance
Each instance create a Listener and the associated callbacks
"""
def __init__(self, xpl, dev, stat, sensor, xpl_type, log_stats, log_stats_unknown, db, event_requests):
""" Initialize a stat instance
@param xpl : A xpl manager instance
@param dev : A Device reference
@param stat : A XplStat reference
@param sensor: A Sensor reference
@param xpl-type: what xpl-type to listen for
"""
### Rest data
self._event_requests = event_requests
self._db = db
self._log_stats = log_stats
#self._log_stats_unknown = log_stats_unknown
self._dev = dev
self._stat = stat
self._sen = sensor
### build the filter
params = {'schema': stat.schema, 'xpltype': xpl_type}
for p in stat.params:
if p.static:
params[p.key] = p.value
#.........这里部分代码省略.........