def on_run(self, request): flow_id = request.payload['flow_id'] prj_id = request.payload['prj_id'] try: flow = FlowFile.objects.get(id=flow_id) except FlowFile.DoesNotExist: return api.IncorrectResponse(request, reason="没有找到id={}的流程".format(flow_id)).do_response() flow_file_abs_path = flow.get_flow_file_as_abs_path() loader = flowex.FlowLoader() status, src_obj, reason = loader.load_source_from_abs_path(flow.name, flow_file_abs_path) if status != api.OK: return api.IncorrectResponse(request, reason=reason).do_response() status, flow_obj, reason = src_obj.compile() if status != api.OK: return api.IncorrectResponse(request, reason=reason).do_response() status, _, reason = flow_obj.link() if status != api.OK: return api.IncorrectResponse(request, reason=reason).do_response() self.srv.startup_worker(prj_id, flow_id, loader, src_obj, flow_obj) return api.CorrectResponse(request).do_response()
def process_control_command(self): """处理设备的控制命令""" status, length, reason = api.device_get_control_command_length(self.dev_type) if status != api.OK: print(reason) return if not length: return load = time.time() # 执行时间限制在500毫秒 while length > 0 and time.time() - load < 0.5: status, request, reason = api.device_get_control_command(self.dev_type) if status != api.OK: break if request is None: break if request.is_expire() is True: plog.warn("接收到过期的命令:", command=str(request)) break request.dump() response = api.IncorrectResponse(request, reason='还未实现') response.do_response() response.dump()
def run_until_exit(self, *args, **kwargs): plog.info("{}模拟驱动已启动,pid: {}, 工作目录: {}".format(Information.device_name, os.getpid(), os.getcwd())) #self.channel = self.open_channel() self.pubsub.psubscribe(profile.get_device_control_command_path(Information.dev_type)) while True: api.device_set_profile_statement(Information.dev_type,self.get_driver_statement()) # 200毫秒用于等待命令消息 pack = self.pubsub.get_message(ignore_subscribe_messages=True, timeout=0.2) if pack: data = json.loads(pack['data']) request = api.Request.load_request_from_json(data) response = oyo.process_control(self.channel, self.address, request, **request.payload) else: request = None response = None self.period_controller.probe_period_delay() input_registers = oyo.device_read_all_register(self.channel, self.address) if input_registers is None: plog.error("返回数据错误, 5秒后重连") time.sleep(5) self.channel.close() self.channel = self.open_channel() continue if pack and request: if not response: api.IncorrectResponse(request, '没有正确应答').do_response() else: response.do_response() api.device_set_data_registers(Information.dev_type, input_registers) time.sleep(0.3)
def i_not_supported(*args): return api.IncorrectResponse(request, "不支持的功能")
def on_not_supported_command(request): return api.IncorrectResponse(request, reason="不支持的命令").do_response()
def on_run(request): return api.IncorrectResponse(request, reason="流程正在执行中").do_response()
def on_stop(request): return api.IncorrectResponse(request, reason="流程还未执行").do_response()