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
def get_attributes(dev_id, attr_name): try: MyLog.logger.info('查询服务名称') dev_svr_name = DevCall.query_srv_name_by_dev_id(dev_id) msg = MyLog.color_green('设备(%s)的服务名为%s' % (dev_id, dev_svr_name)) MyLog.logger.info(msg) if dev_svr_name: msg = MyLog.color_green('RPC调用设备(%s)获取属性值(%s)' % (dev_id, attr_name)) MyLog.logger.info(msg) function_name = 'rpc.' + dev_svr_name + '.property_read' with ClusterRpcProxy(url) as rpc: result, value = eval(function_name)(dev_id, attr_name) if result == g_retValue.qjBoxOpcodeSucess.value: msg = MyLog.color_green( f'RPC调用设备({dev_id})获取属性值({attr_name}:{value})') MyLog.logger.info(msg) return value else: msg = MyLog.color_red( '获取设备(%s)属性值(%s)返回错误(%s)' % (dev_id, attr_name, g_retValue(result).name)) MyLog.logger.error(msg) return None except Exception as e: msg = MyLog.color_red("get_attributes has except: " + str(e)) MyLog.logger.error(msg) return None
def release(self): try: with self.rdcon.pipeline() as pipe: while True: try: # watch 锁, multi 后如果该 key 被其他客户端改变, 事务操作会抛出 WatchError 异常 pipe.watch(self._lock_key) iden = pipe.get(self._lock_key) if iden and iden.decode('utf-8') == self._lock_key: # 事务开始 pipe.multi() pipe.delete(self._lock_key) pipe.execute() pipe.unwatch() #print("release success") return None pipe.unwatch() break except WatchError as e: msg = MyLog.color_red("watch lock has except: " + str(e)) MyLog.logger.error(msg) #print("release falied") return None except Exception as e: msg = MyLog.color_red("redislock release has except: " + str(e)) MyLog.logger.error(msg) #print("redislock release has except: " + str(e)) pass
def query_srv_name_by_dev_id(dev_id): try: if dev_id in g_dev_id_to_srv_name_map_dict.keys(): dev_srv_name = g_dev_id_to_srv_name_map_dict[dev_id] msg = MyLog.color_green('从内存获取到设备(%s)的服务名为%s' % (dev_id, dev_srv_name)) MyLog.logger.info(msg) return dev_srv_name else: MyLog.logger.info(f'RPC查询设备({dev_id})服务名称') with ClusterRpcProxy(url) as rpc: dev_srv_name = rpc.mng_srv.get_srv_name_from_sn(dev_id) if dev_srv_name: msg = MyLog.color_green('RPC查询得到设备(%s)的服务名为%s' % (dev_id, dev_srv_name)) MyLog.logger.info(msg) g_dev_id_to_srv_name_map_dict[dev_id] = dev_srv_name else: msg = MyLog.color_green('RPC查询得到设备(%s)的服务名为空' % (dev_id)) MyLog.logger.info(msg) return dev_srv_name except Exception as e: msg = MyLog.color_red("query_srv_name_by_dev_id has except: " + str(e)) MyLog.logger.error(msg) return None
def manual_control(cls, services) -> int: try: keys = {'priority', 'script'} for service_dict in services: if not cls.__check_keys_exists(service_dict, keys): # 返回参数错误 msg = MyLog.color_red('必要参数不存在') MyLog.logger.error(msg) return g_retValue.qjBoxOpcodeInputParamErr.value if type(service_dict['script'] ) != str or service_dict['script'] == '': msg = MyLog.color_red('script param is invalid') MyLog.logger.error(msg) return g_retValue.qjBoxOpcodeInputParamErr.value if type(service_dict['priority']) != int or service_dict[ 'priority'] < 1 or service_dict['priority'] > 99: msg = MyLog.color_red('priority param is invalid') MyLog.logger.error(msg) return g_retValue.qjBoxOpcodeInputParamErr.value for service_dict in services: current_ts = time.time() py_file_name = '%d' % (current_ts * 1000000) py_path = "/tmp/" + py_file_name + '.py' pyc_path = "/tmp/" + py_file_name + '.pyc' conver_to_py(service_dict['script'], py_path) # 执行脚本 file = importlib.import_module(py_file_name) importlib.reload(file) # dev_command_list = [{'product_id': '', 'dev_id': "", "command_list":[{'service':'', 'param':'', "time":10}]}] dev_command_list, event_list, attr_list = file.script_fun() if dev_command_list: current_ts = time.time() for dev_command in dev_command_list: command_info_list = [] for command in dev_command['command_list']: command_info = CommandInfo( 'manual', command['service'], command['param'], current_ts, current_ts + command['time'], service_dict['priority'], 'manual') command_info_list.append(command_info) if command_info_list: DevCommandQueueMng.add_manual_command( dev_command['product_id'], dev_command['dev_id'], command_info_list) if os.path.exists(py_path): os.remove(py_path) if os.path.exists(pyc_path): os.remove(pyc_path) return g_retValue.qjBoxOpcodeSucess.value except Exception as e: msg = MyLog.color_red('manual_control in rule_mng has except: ' + str(e)) MyLog.logger.error(msg) return g_retValue.qjBoxOpcodeExcept.value
def report_rule_end_event(uuid): try: msg = MyLog.color_green('上报规则结束事件 uuid=%s' % (uuid)) MyLog.logger.info(msg) with ClusterRpcProxy(url) as rpc: resp = rpc.GW.raiseEvent.call_async('ruleEnd', uuid=uuid) except Exception as e: msg = MyLog.color_red("report_rule_end_event has except: " + str(e)) MyLog.logger.error(msg) return None
def report_rule_command_cover_event(dev_id, command, be_cover_rule_uuid, cover_rule_uuid): try: msg = MyLog.color_green('上报指令被高优先级抢占事件 dev_id=%s, command=%s, be_cover_rule_uuid=%s, cover_rule_uuid=%s'\ %(dev_id, command, be_cover_rule_uuid, cover_rule_uuid)) MyLog.logger.info(msg) with ClusterRpcProxy(url) as rpc: resp = rpc.GW.raiseEvent.call_async('ruleCommandCover',\ devId=dev_id, command=command, beCoverRuleUuid=be_cover_rule_uuid, coverRuleUuid=cover_rule_uuid) except Exception as e: msg = MyLog.color_red( "report_rule_command_cover_event has except: " + str(e)) MyLog.logger.error(msg) return None
def report_linkage_custom_event(eventId, srcList): try: msg = MyLog.color_green( f'上报用户自定义事件 eventid={eventId} srcList={srcList}') MyLog.logger.info(msg) with ClusterRpcProxy(url) as rpc: resp = rpc.GW.raiseEvent.call_async('customEvent', eventId=eventId, srcList=srcList) except Exception as e: msg = MyLog.color_red("report_linkage_custom_event has except: " + str(e)) MyLog.logger.error(msg) return None
def report_default_command_status_event(dev_id, command, result_code): try: msg = MyLog.color_green( '上报指令执行默认参数结果事件 dev_id=%s, command=%s, result_code=%d' % (dev_id, command, result_code)) MyLog.logger.info(msg) with ClusterRpcProxy(url) as rpc: resp = rpc.GW.raiseEvent.call_async('ruleDefaultCommandStatus', devId=dev_id, command=command, resultCode=result_code) except Exception as e: msg = MyLog.color_red( "report_default_command_status_event has except: " + str(e)) MyLog.logger.error(msg) return None
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
def add_rule(self, rules) -> int: if not service_ready(): return g_retValue.qjBoxOpcodeNoReady.value payload = json.dumps(rules) msg = MyLog.color_green('rpc call add_rule: %s' % (payload)) MyLog.logger.info(msg) return RuleMng.add_rule(rules)
def get_rule_by_uuid(self, uuids) -> (int, dict): if not service_ready(): return g_retValue.qjBoxOpcodeNoReady.value, {} payload = json.dumps(uuids) msg = MyLog.color_green('rpc call get_rule_by_uuid: %s' % (payload)) MyLog.logger.info(msg) return RuleMng.get_rule_by_uuid(uuids)
def delete_rule_by_uuid(self, uuids) -> int: if not service_ready(): return g_retValue.qjBoxOpcodeNoReady.value payload = json.dumps(uuids) msg = MyLog.color_green('rpc call delete_rule_by_uuid: %s' % (payload)) MyLog.logger.info(msg) return RuleMng.delete_rule_by_uuid(uuids)
def call_service(dev_command_list, service, duration, **param) -> None: try: key_list = service.split(".") product_id = key_list[0] dev_id = key_list[1] command = {} command['service'] = key_list[3] command['param'] = param if duration == 0: # 0代表永久执行,在这里赋值时间为1年 command['time'] = 31536000 else: command['time'] = duration command_list = get_command_list_by_dev_id(dev_command_list, dev_id) if command_list: command_list.append(command) else: command_list = [] command_list.append(command) dev_command = {} dev_command['product_id'] = product_id dev_command['dev_id'] = dev_id dev_command['command_list'] = command_list dev_command_list.append(dev_command) except Exception as e: msg = MyLog.color_red('call_service has except: ' + str(e)) MyLog.logger.error(msg)
def manual_control(self, services) -> int: if not service_ready(): return g_retValue.qjBoxOpcodeNoReady.value payload = json.dumps(services) msg = MyLog.color_green('rpc call manual_control: %s' % (payload)) MyLog.logger.info(msg) return RuleMng.manual_control(services)
def outside_linkage(self, services) -> int: if not service_ready(): return g_retValue.qjBoxOpcodeNoReady.value payload = json.dumps(services) msg = MyLog.color_green('rpc call outside_linkage: %s' % (payload)) MyLog.logger.info(msg) # 添加联动指令到指令列表 return RuleMng.outside_linkage(services)
def stop_linkage_rule_running(self, uuids) -> int: if not service_ready(): return g_retValue.qjBoxOpcodeNoReady.value payload = json.dumps(uuids) msg = MyLog.color_green('rpc call stop_linkage_rule_running: %s' % (payload)) MyLog.logger.info(msg) # 删除指令列表中指定uuid的指令 return RuleMng.stop_linkage_rule_running(uuids)
def main(): msg = MyLog.color_green('+++++++++规则引擎启动+++++++++') MyLog.logger.info(msg) RuleNameko.open() time.sleep(6) MyLog.logger.info("query has mng srv ready") while not DevCall.mng_srv_ready(): time.sleep(1) msg = MyLog.color_green('管理服务已就就绪') set_service_ready() MyLog.logger.info(msg) # 恢复设备之前联动过程中没有恢复的指令 DevAttributeMng.restore_devices_when_start_up() RuleMng.timer_rule_decision() while True: time.sleep(3600)
def attribute_notice(self, product_id, dev_id, attrs) -> None: if not service_ready(): return None msg = MyLog.color_green('rpc call attribute_notice, dev_id:%s' % dev_id) MyLog.logger.debug(msg) DevAttributeMng.update_dev_now_attributes(dev_id=dev_id, attr_dict=attrs) timer = Timer(0, RuleMng.run_linkage_rule_by_devid, args=(dev_id, attrs,)) timer.start()
def add_rule(self, uuid, enable, type, priority, date_list, time_list, src_dev_list, dst_dev_list, script_path, py_path): try: if (type != 'timer' and type != 'linkage') or (priority < 0 or priority > 99): # 参数错误 msg = MyLog.color_red('添加规则参数错误') MyLog.logger.error(msg) return False ret = self.__session.query(rule_main_tbl).filter(rule_main_tbl.uuid == uuid).all() if (ret): # 资源已存在 msg = MyLog.color_red('规则uuid已存在') MyLog.logger.error(msg) return False element = rule_main_tbl(uuid=uuid, enable=enable, type=type, priority=priority, script_path=script_path, py_path=py_path) self.__session.add(element) for d in date_list: start_d = datetime.datetime.strptime(d['startDate'], "%Y-%m-%d") end_d = datetime.datetime.strptime(d['endDate'], "%Y-%m-%d") element = rule_date_tbl(uuid=uuid, start_date=start_d, end_date=end_d) self.__session.add(element) for t in time_list: start_h, start_m, start_s = map(int, t['startTime'].split(':')) end_h, end_m, end_s = map(int, t['endTime'].split(':')) start_t = datetime.time(start_h, start_m, start_s) end_t = datetime.time(end_h, end_m, end_s) element = rule_time_tbl(uuid=uuid, start_time=start_t, end_time=end_t) self.__session.add(element) for s in src_dev_list: element = rule_src_device_tbl(uuid=uuid, src_device=s) self.__session.add(element) for ds in dst_dev_list: element = rule_dst_device_tbl(uuid=uuid, dst_device=ds) self.__session.add(element) self.__session.commit() return True except Exception as e: msg = MyLog.color_red("add_rule has except: " + str(e)) MyLog.logger.error(msg) return False
def mng_srv_ready(): try: with ClusterRpcProxy(url) as rpc: return rpc.mng_srv.ready_go() except Exception as e: msg = MyLog.color_red("mng_srv_ready has except: " + str(e)) MyLog.logger.error(msg) return False
def event_notice(self, product_id, dev_id, event, payload) -> None: if not service_ready(): return None msg = MyLog.color_green('rpc call event_notice, dev_id:%s, event:%s, payload:%s' % (dev_id, event, payload)) MyLog.logger.info(msg) if EventHandle.update_event(product_id, dev_id, event, payload): # 根据dev_id,触发与其相关的联动脚本执行 timer = Timer(0, RuleMng.run_linkage_rule_by_devid, args=(dev_id, None,)) timer.start()
def add_rule(cls, rules) -> int: try: 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 if SqliteInterface.rule_exist(rule_dict['uuid']): msg = MyLog.color_red("rule(%s) has exist" % (rule_dict['uuid'])) MyLog.logger.error(msg) return g_retValue.qjBoxOpcodeInputParamErr.value for rule_dict in rules: if not os.path.exists(RULE_JS_SCRIPT_FOLDER): os.makedirs(RULE_JS_SCRIPT_FOLDER) if not os.path.exists(RULE_PY_SCRIPT_FOLDER): os.makedirs(RULE_PY_SCRIPT_FOLDER) # 将平台下发的规则脚本转换成py脚本,并将两个脚本内容都保存到文件,以uuid为文件名。 js_path = RULE_JS_SCRIPT_FOLDER + "/" + rule_dict[ 'uuid'] + '.js' py_path = RULE_PY_SCRIPT_FOLDER + "/" + rule_dict[ 'uuid'] + '.py' MyLog.logger.info("js_path:%s" % js_path) MyLog.logger.info("py_path:%s" % py_path) write_file(rule_dict['script'], js_path) conver_to_py(rule_dict['script'], py_path) # 将规则插入数据库 SqliteInterface.add_rule( rule_dict['uuid'], rule_dict['enable'], rule_dict['type'], rule_dict['priority'], rule_dict['date'], rule_dict['time'], rule_dict['srcDevice'], rule_dict['dstDevice'], js_path, py_path) # 规则决策 RuleMng.start_new_rule_decision_timer(0) return g_retValue.qjBoxOpcodeSucess.value except Exception as e: msg = MyLog.color_red('add_rule has except: ' + str(e)) MyLog.logger.error(msg) return g_retValue.qjBoxOpcodeExcept.value
def report_rule_command_ignore_event(dev_id, command, uuid, higher_priority_uuid): try: msg = MyLog.color_green( '上报指令忽略执行事件 dev_id=%s, command=%s, uuid=%s, higher_priority_uuid=%s' % (dev_id, command, uuid, higher_priority_uuid)) MyLog.logger.info(msg) with ClusterRpcProxy(url) as rpc: resp = rpc.GW.raiseEvent.call_async( 'ruleCommandIgnore', devId=dev_id, command=command, uuid=uuid, higherPriorityUuid=higher_priority_uuid) except Exception as e: msg = MyLog.color_red( "report_rule_command_ignore_event has except: " + str(e)) MyLog.logger.error(msg) return None
def set_rule_disable(self, uuid_dict): try: for uuid in uuid_dict: self.__session.query(rule_main_tbl).filter(rule_main_tbl.uuid == uuid).update({'enable': False}) self.__session.commit() return True except Exception as e: msg = MyLog.color_red("set_rule_disable has except: " + str(e)) MyLog.logger.error(msg) return False
def get_type_by_uuid(self, uuid) -> str: try: main_sql = self.__session.query(rule_main_tbl).filter(rule_main_tbl.uuid == uuid).all() for info in main_sql: return info.type return None except Exception as e: msg = MyLog.color_red("get_type_by_uuid has except: " + str(e)) MyLog.logger.error(msg) return None
def raise_event(event_list, event_id, srcList): try: event_dict = {} event_dict['event_id'] = event_id src_dev_list_dict = json.loads(srcList) event_dict['src_dev_list'] = src_dev_list_dict event_list.append(event_dict) except Exception as e: msg = MyLog.color_red('raise_event has except: ' + str(e)) MyLog.logger.error(msg)
def add_linkage_command_body(product_id, dev_id, command_list): lk = DevCommandQueueMng.get_exe_locker(dev_id) try: ts = time.time() MyLog.logger.info(f'添加联动指令, 等待获取锁({ts}), 设备id:{dev_id}') lk.acquire() MyLog.logger.info(f'添加联动指令, 获得锁({ts}), 设备id:{dev_id}') need_add_to_command_queue_list = [] dev_command_queue = DevCommandQueueMng.get_dev_command_queue( dev_id) if not dev_command_queue: dict = {} dict["dev_id"] = dev_id dev_command_queue = DevCommandQueue(product_id, dev_id) dict["dev_command_queue"] = dev_command_queue g_dev_command_queue_list.append(dict) for command in command_list: need_exe = True current_running_command = dev_command_queue.get_current_running_command( command.command) if current_running_command: MyLog.logger.info( 'command.command = %s, command.priority = %d, current_running_command.priority = %d' % (command.command, command.priority, current_running_command.priority)) if current_running_command and command.priority <= current_running_command.priority: EventReport.report_rule_command_ignore_event( dev_id, command.command, command.uuid, current_running_command.uuid) need_exe = False if need_exe: need_retry, service_has_recv, ret = DevCommandQueueMng.command_exe( dev_id, command) if service_has_recv: dev_command_queue.clear_linkage_command( command.command) need_add_to_command_queue_list.append(command) if need_add_to_command_queue_list: MyLog.logger.info( 'dev_command_queue.add_linkage_command_list size: %d' % (len(need_add_to_command_queue_list))) dev_command_queue.add_linkage_command_list( need_add_to_command_queue_list) name_list = dev_command_queue.get_all_command_name_list() DevCommandQueueMng.__reset_dev_timer(dev_id) lk.release() MyLog.logger.info(f'添加联动指令, 释放锁({ts}), 设备id:{dev_id}') except Exception as e: msg = MyLog.color_red( 'add_linkage_command_body has exception: ' + str(e)) MyLog.logger.error(msg) lk.release()
def add_manual_command_body(product_id, dev_id, command_list): lk = DevCommandQueueMng.get_exe_locker(dev_id) try: ts = time.time() MyLog.logger.info(f'添加手动指令, 等待获取锁({ts}), 设备id:{dev_id}') lk.acquire() MyLog.logger.info(f'添加手动指令, 获得锁({ts}), 设备id:{dev_id}') need_add_to_command_queue_list = [] dev_command_queue = DevCommandQueueMng.get_dev_command_queue( dev_id) if not dev_command_queue: dict = {} dict["dev_id"] = dev_id dev_command_queue = DevCommandQueue(product_id, dev_id) dict["dev_command_queue"] = dev_command_queue g_dev_command_queue_list.append(dict) for command in command_list: command.uuid = 'manual' need_exe = True current_running_command = dev_command_queue.get_current_running_command( command.command) # 同一优先级的临时手动命令需要更新执行 if current_running_command and current_running_command.effective and \ (( current_running_command.type == 'manual' and command.priority < current_running_command.priority) or \ ( current_running_command.type != 'manual' and command.priority <= current_running_command.priority)): EventReport.report_rule_command_ignore_event( dev_id, command.command, command.uuid, current_running_command.uuid) need_exe = False if need_exe: need_retry, service_has_recv, ret = DevCommandQueueMng.command_exe( dev_id, command, force=True) if service_has_recv: # 如果执行临时手动指令,则联动指令已被顶替失效,删除 dev_command_queue.clear_linkage_command( command.command) need_add_to_command_queue_list.append(command) if need_add_to_command_queue_list: dev_command_queue.add_manual_command_list( need_add_to_command_queue_list) DevCommandQueueMng.__reset_dev_timer(dev_id) lk.release() MyLog.logger.info(f'添加手动指令, 释放锁({ts}), 设备id:{dev_id}') except Exception as e: msg = MyLog.color_red( 'add_manual_command_body has exception: ' + str(e)) MyLog.logger.error(msg) lk.release()
def enable_rule(cls, uuids): 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_enable(uuids) # 规则决策 RuleMng.start_new_rule_decision_timer(0) return g_retValue.qjBoxOpcodeSucess.value except Exception as e: msg = MyLog.color_red('enable_rule has except: ' + str(e)) MyLog.logger.error(msg) return g_retValue.qjBoxOpcodeExcept.value