Exemplo n.º 1
0
    def disable_rule(cls, uuids) -> int:
        try:
            if not uuids:
                msg = MyLog.color_red("uuids is empty")
                MyLog.logger.error(msg)
                return g_retValue.qjBoxOpcodeInputParamErr.value
            for uuid in uuids:
                if not SqliteInterface.rule_exist(uuid):
                    msg = MyLog.color_red("rule(%s) has not exist" % (uuid))
                    MyLog.logger.error(msg)
                    return g_retValue.qjBoxOpcodeInputParamErr.value
            # 数据库删除规则
            SqliteInterface.set_rule_disable(uuids)

            # 从正在运行的队列中删除
            remove_running_rule_endtime(uuids)
            DevCommandQueueMng.clear_command_by_rule_uuid(uuids)
            DevCommandQueueMng.all_dev_exe()

            # 规则决策
            RuleMng.start_new_rule_decision_timer(0)
            return g_retValue.qjBoxOpcodeSucess.value
        except Exception as e:
            msg = MyLog.color_red('disable_rule has except: ' + str(e))
            MyLog.logger.error(msg)
            return g_retValue.qjBoxOpcodeExcept.value
Exemplo n.º 2
0
 def delete_rule_by_uuid(cls, uuids) -> int:
     try:
         # 数据库删除规则
         SqliteInterface.delete_rule(uuids)
         # 删除规则脚本
         for uuid in uuids:
             js_path = RULE_JS_SCRIPT_FOLDER + "/" + uuid + '.js'
             py_path = RULE_PY_SCRIPT_FOLDER + "/" + uuid + '.py'
             pyc_path = RULE_PY_SCRIPT_FOLDER + "/" + uuid + '.pyc'
             if os.path.exists(js_path):
                 os.remove(js_path)
             if os.path.exists(py_path):
                 os.remove(py_path)
             if os.path.exists(pyc_path):
                 os.remove(pyc_path)
         # 从正在运行的队列中删除
         remove_running_rule_endtime(uuids)
         DevCommandQueueMng.clear_command_by_rule_uuid(uuids)
         DevCommandQueueMng.all_dev_exe()
         # 规则决策
         return g_retValue.qjBoxOpcodeSucess.value
     except Exception as e:
         msg = MyLog.color_red('delete_rule_by_uuid has except: ' + str(e))
         MyLog.logger.error(msg)
         return g_retValue.qjBoxOpcodeExcept.value
Exemplo n.º 3
0
def running_rule_endtime_handle():
    MyLog.logger.info("运行中的规则结束处理")
    current_ts = time.time()
    smallest_ts = 0
    uuid_list = []
    to_del_rule = []
    g_lk.acquire()
    for rule in g_running_rule_endtime_list:
        MyLog.logger.info(f'rule:{rule}')
        if rule['end_ts'] < current_ts:
            MyLog.logger.info("规则(%s)结束" % (rule['uuid']))
            # g_running_rule_endtime_list.remove(rule) # 遍历列表过程中改变列表size!!!
            to_del_rule.append(rule)
            uuid_list.append(rule['uuid'])
            # 上报规则结束事件
            EventReport.report_rule_end_event(rule['uuid'])
        else:
            if smallest_ts == 0:
                smallest_ts = rule['end_ts']
            elif rule['end_ts'] < smallest_ts:
                smallest_ts = rule['end_ts']

    # to fix the bug referred above
    for del_rule in to_del_rule:
        g_running_rule_endtime_list.remove(del_rule)
    g_lk.release()

    if uuid_list:
        # 删除结束规则的指令
        DevCommandQueueMng.clear_command_by_rule_uuid(uuid_list)
        # 所有设备队列重新执行指令决策
        DevCommandQueueMng.all_dev_exe()

    global g_running_rule_endtime_handle_timer
    if g_running_rule_endtime_handle_timer:
        g_running_rule_endtime_handle_timer.cancel()

    ts = smallest_ts - current_ts
    if ts > 0:
        g_running_rule_endtime_handle_timer = Timer(
            ts, running_rule_endtime_handle)
        g_running_rule_endtime_handle_timer.start()
Exemplo n.º 4
0
    def stop_linkage_rule_running(cls, uuids) -> int:
        try:
            for uuid in uuids:
                rule_type = SqliteInterface.get_type_by_uuid(uuid)
                if not rule_type or rule_type != 'linkage':
                    msg = MyLog.color_red(
                        "stop_linkage_rule_running the type of input uuid is invalid"
                    )
                    MyLog.logger.error(msg)
                    return g_retValue.qjBoxOpcodeInputParamErr.value

            DevCommandQueueMng.clear_command_by_rule_uuid(uuids)
            remove_running_rule_endtime(uuids)
            DevCommandQueueMng.all_dev_exe()

            return g_retValue.qjBoxOpcodeSucess.value
        except Exception as e:
            msg = MyLog.color_red('stop_linkage_rule_running has except: ' +
                                  str(e))
            MyLog.logger.error(msg)
            return g_retValue.qjBoxOpcodeExcept.value
Exemplo n.º 5
0
    def update_rule(cls, rules) -> int:
        try:
            uuid_list = []
            for rule_dict in rules:
                ret = cls.__check_rule_param(rule_dict)
                if ret != g_retValue.qjBoxOpcodeSucess.value:
                    msg = MyLog.color_red('check_rule_param failed')
                    MyLog.logger.error(msg)
                    return ret

                uuid_list.append(rule_dict['uuid'])
            # 数据库删除规则
            SqliteInterface.delete_rule(uuid_list)
            # 删除规则脚本
            for uuid in uuid_list:
                js_path = RULE_JS_SCRIPT_FOLDER + "/" + uuid + '.js'
                py_path = RULE_PY_SCRIPT_FOLDER + "/" + uuid + '.py'
                pyc_path = RULE_PY_SCRIPT_FOLDER + "/" + uuid + '.pyc'
                if os.path.exists(js_path):
                    os.remove(js_path)
                if os.path.exists(py_path):
                    os.remove(py_path)
                if os.path.exists(pyc_path):
                    os.remove(pyc_path)
            # 从正在运行的队列中删除
            remove_running_rule_endtime(uuid_list)
            # 删除指令队列中的相关指令
            DevCommandQueueMng.clear_command_by_rule_uuid(uuid_list)
            DevCommandQueueMng.all_dev_exe()
            # 重新添加规则
            ret = RuleMng.add_rule(rules)
            return ret
        except Exception as e:
            msg = MyLog.color_red('update_rule has except: ' + str(e))
            MyLog.logger.error(msg)
            return g_retValue.qjBoxOpcodeExcept.value