Example #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
Example #2
0
 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
Example #3
0
 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
Example #4
0
    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
Example #5
0
 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
Example #6
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
Example #7
0
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)
Example #8
0
    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
Example #9
0
 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
Example #10
0
    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
Example #11
0
 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
Example #12
0
 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
Example #13
0
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)
Example #14
0
        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()
Example #15
0
 def get_all_uuids(self):
     try:
         uuid_list = []
         sql = self.__session.query(rule_main_tbl).all()
         for info in sql:
             uuid_list.append(info.uuid)
         return True, uuid_list
     except Exception as e:
         msg = MyLog.color_red("get_all_uuids has except: " + str(e))
         MyLog.logger.error(msg)
         return False, None
Example #16
0
 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
Example #17
0
 def rule_exist(self, uuid):
     try:
         main_sql = self.__session.query(rule_main_tbl).filter(rule_main_tbl.uuid == uuid).all()
         if main_sql:
             return True
         else:
             return False
     except Exception as e:
         msg = MyLog.color_red("rule_exist has except: " + str(e))
         MyLog.logger.error(msg)
         return False
Example #18
0
def get_event_time_from_now(payload):
    try:
        MyLog.logger.info("get_event_time_from_now payload: %s" % (payload))
        key_list = payload.split(".")
        if key_list[2] != 'events':
            msg = MyLog.color_red("payload error")
            MyLog.logger.error(msg)
            return 2592000
        ts = EventHandle.get_event_timestamp(key_list[0], key_list[1],
                                             key_list[3])
        if ts is None:
            return 2592000
        current_ts = time.time()
        t = current_ts - ts
        MyLog.logger.info('get_event_time_from_now: %f' % (t))
        return t
    except Exception as e:
        msg = MyLog.color_red('get_event_time_from_now has except: ' + str(e))
        MyLog.logger.error(msg)
        return 2592000
Example #19
0
 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
Example #20
0
        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()
Example #21
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
Example #22
0
 def get_array_name_and_index(cls, string):
     try:
         if string[len(string) - 1] == ']':
             start_index = string.find('[')
             return string[0:start_index], int(string[start_index +
                                                      1:len(string) - 1])
         else:
             return None, None
     except Exception as e:
         msg = MyLog.color_red("get_array_name_and_index has except: " +
                               str(e))
         MyLog.logger.error(msg)
         return None, None
Example #23
0
 def clear_all_rule(self):
     try:
         self.__session.query(rule_main_tbl).delete()
         self.__session.query(rule_date_tbl).delete()
         self.__session.query(rule_time_tbl).delete()
         self.__session.query(rule_src_device_tbl).delete()
         self.__session.query(rule_dst_device_tbl).delete()
         self.__session.commit()
         return True
     except Exception as e:
         msg = MyLog.color_red("clear_all_rule has except: " + str(e))
         MyLog.logger.error(msg)
         return False
Example #24
0
    def stop_manual_control(cls, services) -> int:
        try:
            keys = {'productId', 'devId', 'service'}
            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

                pid = service_dict['productId']
                did = service_dict['devId']
                srv = service_dict['service']

                if type(pid) != str or type(did) != str or type(srv) != str:
                    # 数据类型错误
                    msg = MyLog.color_red('参数数据类型错误')
                    MyLog.logger.error(msg)
                    return g_retValue.qjBoxOpcodeInputParamErr.value

                if pid == "" or did == "" or srv == "":
                    # 数据值为空
                    msg = MyLog.color_red('参数字符串数据为空字符串')
                    MyLog.logger.error(msg)
                    return g_retValue.qjBoxOpcodeInputParamErr.value

            dev_id_list = []
            for service_dict in services:
                DevCommandQueueMng.clear_manual_command(
                    service_dict['productId'], service_dict['devId'],
                    service_dict['service'])
                dev_id_list.append(service_dict['devId'])
            DevCommandQueueMng.dev_list_exe(dev_id_list)
            return g_retValue.qjBoxOpcodeSucess.value
        except Exception as e:
            msg = MyLog.color_red(
                'stop_manual_control in rule_mng has except: ' + str(e))
            MyLog.logger.error(msg)
            return g_retValue.qjBoxOpcodeExcept.value
Example #25
0
 def update_event(cls, product_id, dev_id, event, payload):
     try:
         # 将事件添加/更新到redis, key = 'product_id.dev_id.event'
         key = product_id + '.' + dev_id + '.' + event
         mapping = {}
         mapping['ts'] = time.time()
         mapping['payload'] = payload
         r = RedisHandle()
         return r.hmset_value(key, mapping)
     except Exception as e:
         msg = MyLog.color_red("update_event has except: " + str(e))
         MyLog.logger.error(msg)
         return False
Example #26
0
    def get_rule(self, uuid):
        try:
            date_list = []
            time_list = []
            src_dev_list = []
            dst_dev_list = []
            enable = True
            type = ''
            priority = -1
            script_path = ''
            main_sql = self.__session.query(rule_main_tbl).filter(rule_main_tbl.uuid == uuid).all()
            if main_sql:
                if main_sql[0].enable == 0:
                    enable = False

                type = main_sql[0].type
                priority = main_sql[0].priority
                script_path = main_sql[0].script_path
            else:
                return False, None

            date_sql = self.__session.query(rule_date_tbl).filter(rule_date_tbl.uuid == uuid).all()
            for date in date_sql:
                date_dict = {}
                date_dict['start_date'] = str(date.start_date)
                date_dict['end_date'] = str(date.end_date)
                date_list.append(date_dict)

            time_sql = self.__session.query(rule_time_tbl).filter(rule_time_tbl.uuid == uuid).all()
            for time in time_sql:
                time_dict = {}
                time_dict['start_time'] = time.start_time
                time_dict['end_time'] = time.end_time
                time_list.append(time_dict)

            src_dev_sql = self.__session.query(rule_src_device_tbl).filter(rule_src_device_tbl.uuid == uuid).all()
            for dev in src_dev_sql:
                src_dev_list.append(dev.src_device)

            dst_dev_sql = self.__session.query(rule_dst_device_tbl).filter(rule_dst_device_tbl.uuid == uuid).all()
            for dev in dst_dev_sql:
                dst_dev_list.append(dev.dst_device)

            return True, {'enable': enable, 'type': type, 'priority': priority, 'date_list': date_list, \
                          'time_list': time_list, 'src_dev_list': src_dev_list, 'dst_dev_list': dst_dev_list,
                          'script_path': script_path}
        except Exception as e:
            msg = MyLog.color_red("get_rule has except: " + str(e))
            MyLog.logger.error(msg)
            return False, None
Example #27
0
 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
Example #28
0
 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
Example #29
0
 def delete_rule(self, uuid_dict):
     try:
         for uuid in uuid_dict:
             self.__session.query(rule_main_tbl).filter(rule_main_tbl.uuid == uuid).delete()
             self.__session.query(rule_date_tbl).filter(rule_date_tbl.uuid == uuid).delete()
             self.__session.query(rule_time_tbl).filter(rule_time_tbl.uuid == uuid).delete()
             self.__session.query(rule_src_device_tbl).filter(rule_src_device_tbl.uuid == uuid).delete()
             self.__session.query(rule_dst_device_tbl).filter(rule_dst_device_tbl.uuid == uuid).delete()
         self.__session.commit()
         return True
     except Exception as e:
         msg = MyLog.color_red("delete_rule has except: " + str(e))
         MyLog.logger.error(msg)
         return False
Example #30
0
 def call_service(dev_id, service_name, type, params=None, default=False):
     try:
         dev_svr_name = DevCall.query_srv_name_by_dev_id(dev_id)
         if dev_svr_name:
             command_save = True
             if type == 'linkage':
                 command_save = False
             if not default:
                 msg = MyLog.color_green(
                     'RPC调用设备(%s)服务(%s)类型(%s),参数:%s' %
                     (dev_id, service_name, type, params))
                 MyLog.logger.info(msg)
                 function_name = 'rpc.' + dev_svr_name + '.ioctl'
                 with ClusterRpcProxy(url) as rpc:
                     ret, data = eval(function_name)(dev_id, service_name,
                                                     command_save, params)
                     return False, True, ret, data
             else:
                 msg = MyLog.color_green('RPC调用设备(%s)服务(%s)类型(%s),默认参数' %
                                         (dev_id, service_name, type))
                 MyLog.logger.info(msg)
                 function_name = 'rpc.' + dev_svr_name + '.set_default'
                 with ClusterRpcProxy(url) as rpc:
                     ret, data = eval(function_name)(dev_id, service_name,
                                                     command_save)
                     return False, True, ret, data
         else:
             return False, False, g_retValue.qjBoxOpcodeSrvNoRunning.value, {}
     except UnknownService as e:
         msg = MyLog.color_red(
             "DevCall call_service has UnknownService except: " + str(e))
         MyLog.logger.error(msg)
         return True, False, g_retValue.qjBoxOpcodeSrvNoRunning.value, {}
     except Exception as e:
         msg = MyLog.color_red("DevCall call_service has except: " + str(e))
         MyLog.logger.error(msg)
         return True, False, g_retValue.qjBoxOpcodeExcept.value, {}