當前位置: 首頁>>代碼示例>>Python>>正文


Python DbHelper.add_scenario_uuid方法代碼示例

本文整理匯總了Python中domogik.common.database.DbHelper.add_scenario_uuid方法的典型用法代碼示例。如果您正苦於以下問題:Python DbHelper.add_scenario_uuid方法的具體用法?Python DbHelper.add_scenario_uuid怎麽用?Python DbHelper.add_scenario_uuid使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在domogik.common.database.DbHelper的用法示例。


在下文中一共展示了DbHelper.add_scenario_uuid方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: user

# 需要導入模塊: from domogik.common.database import DbHelper [as 別名]
# 或者: from domogik.common.database.DbHelper import add_scenario_uuid [as 別名]

#.........這裏部分代碼省略.........
        except Exception as e:
            self.log.error(u"Creation of a scenario failed, invallid json: {0}".format(json_input))
            self.log.debug(e)
            return {'status': 'NOK', 'msg': 'invallid json'}

        if 'condition' not in payload.keys() \
                or 'actions' not in payload.keys():
            msg = u"the json for the scenario does not contain condition or actions for scenario {0}".format(name)
            self.log.error(msg)
            return {'status': 'NOK', 'msg': msg}

        # instantiate all objects
        self.__instanciate()
        # create the condition itself
        c = Condition(self.log, name, json.dumps(payload['condition']), mapping=self._tests_mapping, on_true=self.trigger_actions)
        self._conditions[name] = c
        self._conditions_actions[name] = []
        self.log.debug(u"Create condition {0} with payload {1}".format(name, payload['condition']))
        # build a list of actions
        for action in payload['actions'].keys():
            # action is now a tuple
            #   (uid, params)
            self._conditions_actions[name].append(action) 
            self._actions_mapping[action].do_init(payload['actions'][action]) 
 
        # store the scenario in the db
        if store:
            with self._db.session_scope():
                # store the scenario
                scen = self._db.add_scenario(name, json_input)
                # store the tests
                for uuid in c.get_mapping():
                    cls = str(self._tests_mapping[uuid].__class__).replace('domogik.common.scenario.tests.', '')
                    self._db.add_scenario_uuid(scen.id, uuid, cls, 1)
                # store the actions
                for uuid in self._conditions_actions[name]:
                    cls = str(self._actions_mapping[uuid].__class__).replace('domogik.common.scenario.actions.', '')
                    self._db.add_scenario_uuid(scen.id, uuid, cls, 0)
        # return
        return {'name': name}

    def eval_condition(self, name):
        """ Evaluate a condition calling eval_condition from Condition instance
        @param name : The name of the condition instance
        @return {'name':name, 'result': evaluation result} or raise Exception
        """
        if name not in self._conditions:
            raise KeyError('no key %s in conditions table' % name)
        else:
            res = self._conditions[name].eval_condition()
            return {'name': name, 'result': res}

    def trigger_actions(self, name):
        """ Trigger that will be called when a condition evaluates to True
        """
        if name not in self._conditions_actions \
                or name not in self._conditions:
            raise KeyError('no key %s in one of the _conditions tables table' % name)
        else:
            for action in self._conditions_actions[name]:
                self._actions_mapping[action].do_action( \
                        self._conditions[name], \
                        self._conditions[name].get_mapping() \
                        )

    def list_actions(self):
開發者ID:Basilic,項目名稱:domogik,代碼行數:70,代碼來源:manager.py


注:本文中的domogik.common.database.DbHelper.add_scenario_uuid方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。