async def receive(self, request): ws = web.WebSocketResponse(heartbeat=1.0) await ws.prepare(request) connection_id = str(uuid.uuid1()) ws_connection = WebSocketConnection(ws, connection_id) self._websockets.add(ws_connection) await self._send_connection_id_to_ws(ws, connection_id) # master should propagate the available settings each time a page reloaded # or new websocket connection is required self.handle_new_connection(connection_id) await self.send_configuration(self._app['master_app'].configuration, ws_connection) self._log.log_message(LogLevel.Debug(), 'websocket connection opened.') try: async for msg in ws: if msg.type == WSMsgType.TEXT: self.handle_client_message(msg.data) elif msg.type == WSMsgType.ERROR: self._log.log_message(LogLevel.Error(), f'ws connection closed with exception: {ws.exception()}') finally: pass self._discard_ws_connection_if_needed()
def on_cmd_message(self, message): try: data = json.loads(message.decode('utf-8')) assert data['type'] == 'cmd' cmd = data['command'] sites = data['sites'] self.log.log_message(LogLevel.Debug(), f'received command: {cmd}') if self.site_id not in sites: self.log.log_message( LogLevel.Warning(), f'ignoring TestApp cmd for other sites {sites} (current site_id is {self.site_id})' ) return to_exec_command = self.commands.get(cmd) if to_exec_command is None: self.log.log_message(LogLevel.Warning(), 'received command not found') return to_exec_command(data) except Exception as e: self._statemachine.error(str(e))
def _execute_cmd_next(self, job_data: Optional[dict]): self.logger.log_message(LogLevel.Debug(), 'COMMAND: next') self.send_status("TESTING") result = self._sequencer_instance.run(self._execution_policy, job_data) self.send_status("IDLE") self.send_testresult(result)
def _send_set_log_level(self): level = { LogLevel.Debug(): 'Debug', LogLevel.Info(): 'Info', LogLevel.Warning(): 'Warning', LogLevel.Error(): 'Error', }[self.loglevel] self.log.log_message(LogLevel.Info(), f'set loglevel to {level}') self.connectionHandler.send_set_log_level(self.loglevel)
def on_loadcommand_issued(self, param_data: dict): jobname = param_data['lot_number'] # TODO: see no difference !! self.loaded_jobname = str(jobname) self.loaded_lot_number = str(jobname) jobformat = self.configuration.get('jobformat') parser = parser_factory.CreateParser(jobformat) source = parser_factory.CreateDataSource(jobname, self.configuration, parser, self.log) if self.configuration.get('skip_jobdata_verification', False): data = { "DEBUG_OPTION": "no content because skip_jobdata_verification enabled" } else: param_data = source.retrieve_data() if param_data is None: self.error_message = "Failed to execute load command" self.load_error() return if not source.verify_data(param_data): self.error_message = "Malformed jobfile" self.load_error() return data = source.get_test_information(param_data) self.log.log_message(LogLevel.Debug(), f'testprogram information: {data}') self.arm_timeout( LOAD_TIMEOUT, lambda: self.timeout("not all sites loaded the testprogram")) self.pendingTransitionsControl = SequenceContainer( [CONTROL_STATE_LOADING, CONTROL_STATE_BUSY], self.configuredSites, lambda: None, lambda site, state: self.on_error( f"Bad statetransition of control {site} during load to {state}" )) self.pendingTransitionsTest = SequenceContainer( [TEST_STATE_IDLE], self.configuredSites, lambda: self.all_siteloads_complete(), lambda site, state: self.on_error( f"Bad statetransition of testapp {site} during load to {state}" )) self.error_message = '' self.connectionHandler.send_load_test_to_all_sites( self.get_test_parameters(data)) self._store_user_settings(UserSettings.get_defaults())
def _execute_cmd_init(self): self.logger.log_message(LogLevel.Debug(), 'COMMAND: init') # ToDo: How should we perform the selftest? self.logger.log_message(LogLevel.Info(), 'running self test...') selftest_result = self._sequencer_instance.init() self.logger.log_message(LogLevel.Info(), f'self test completed: {selftest_result}') self.send_status("IDLE") # TODO: how to report positive init command results? we could also write the testresult # self.publish_result(selftest_result, "<insert init result data here>") # note that currently once "init" failed the status will not be restored to ALIVE if not selftest_result: self._mqtt.publish_status(TheTestAppStatusAlive.INITFAIL, {'state': self._statemachine.state, 'payload': {'state': self._statemachine.state, 'message': 'selftest is failed'}})
def do_connect(self, target_ip, target_port): num_retries = 0 MAX_NUM_RETRIES = 5 self.connected = False while True: try: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.settimeout(3) self.sock.connect((target_ip, target_port)) # Connection succeeded, return. self.connected = True self._log.log_message(LogLevel.Debug(), "Connected to DSC6k") return except Exception as ex: self._log.log_message( LogLevel.Error(), f"Failed to connect to DCS6k@{self.target_ip}:{self.target_port}. Retrying." ) self._log.log_message(LogLevel.Error(), f"exception: {ex}") time.sleep(2) num_retries = num_retries + 1 if num_retries > MAX_NUM_RETRIES: raise ex
def handle_client_message(self, message_data: str): json_data = json.loads(message_data) self._log.log_message(LogLevel.Debug(), f'Server received message {json_data}') if json_data.get('type') == 'cmd': self._app["master_app"].dispatch_command(json_data)
def on_disconnect(self, client, userdata, distc_res): self._log.log_message(LogLevel.Debug(), 'Disconnected')
def on_connect(self, client, userdata, flags, conect_res): topic = f"ate/{self.device_id}/{self.actuator_type}/io-control/request" self.mqtt_client.subscribe(topic) self.post_status() self._log.log_message(LogLevel.Debug(), 'Connection established')
def _execute_cmd_terminate(self): self.logger.log_message(LogLevel.Debug(), 'COMMAND: terminate') result = self._sequencer_instance.aggregate_tests_summary() self.send_summary(result)
def _on_disconnect(self, client, userdata, rc) -> None: self._log.log_message(LogLevel.Debug(), "disconnected")