def test(self): m = self.m assert m.leaf_state == self.s11 m.dispatch(Event('a')) assert m.leaf_state == self.s11 # This transition toggles state between s11 and s211 m.dispatch(Event('c')) assert m.leaf_state == self.s211 m.dispatch(Event('b')) assert m.leaf_state == self.s211 m.dispatch(Event('i')) assert m.leaf_state == self.s211 m.dispatch(Event('c')) assert m.leaf_state == self.s11 assert self.foo is True m.dispatch(Event('h')) assert self.foo is False assert m.leaf_state == self.s11 # Do nothing if foo is False m.dispatch(Event('h')) assert m.leaf_state == self.s11 # This transition toggles state between s11 and s211 m.dispatch(Event('c')) assert m.leaf_state == self.s211 assert self.foo is False m.dispatch(Event('h')) assert self.foo is True assert m.leaf_state == self.s211 m.dispatch(Event('h')) assert m.leaf_state == self.s211
def on_message(self, state, event): topic = event.cargo[MQTT_EVENT_TOPIC] payload = json.loads(event.cargo[MQTT_EVENT_PAYLOAD]) # Handle accepted pending jobs executions if topic_matches_sub(self.get_pending_job_executions_response, topic): in_progress_job_executions = filter_upparat_job_exectutions( payload.get(IN_PROGRESS_JOBS, [])) queued_job_executions = filter_upparat_job_exectutions( payload.get(QUEUED_JOBS, [])) # If there are jobs available go to prepare state if in_progress_job_executions or queued_job_executions: logger.debug("Job executions available.") self.publish( Event( JOBS_AVAILABLE, **{ JOB_EXECUTION_SUMMARIES: { JOB_EXECUTION_SUMMARIES_PROGRESS: in_progress_job_executions, JOB_EXECUTION_SUMMARIES_QUEUED: queued_job_executions, } })) else: logger.debug("No pending job executions available.") return self.publish(Event(NO_JOBS_PENDING))
def test(): global action_one_called, action_two_called assert sm.state == on sm.dispatch(Event('off')) assert sm.state == off assert action_one_called == True assert action_two_called == True sm.dispatch(Event('on')) assert sm.state == on
def login(state, event): path = "POST /login?username="******"&password=ok_password" protocol = "HTTP/1.1" status = "200" log = create_http_log(source_ip, machine_ip, path, protocol, status) process_request(log, request_interval) events = [Event("normal"), Event("forbidden"), Event("logout")] weights = [80, 10, 10] new_event = choices(events, weights, k=1)[0] sm.dispatch(new_event)
def forbidden(state, event): num_of_requets = randint(1, 5) for _ in range(num_of_requets): path = generate_forbidden_path() protocol = "HTTP/1.1" status = "403" log = create_http_log(source_ip, machine_ip, path, protocol, status) process_request(log, request_interval) events = [Event("normal"), Event("forbidden"), Event("logout")] weights = [50, 30, 20] state.parent.dispatch(choices(events, weights, k=1)[0])
def on_message(self, state, event): topic = event.cargo[MQTT_EVENT_TOPIC] if topic_matches_sub(self.job_pending_response, topic): payload = json.loads(event.cargo[MQTT_EVENT_PAYLOAD]) in_progress_job_executions = filter_upparat_job_exectutions( payload["jobs"].get(JOBS_IN_PROGRESS, [])) queued_job_executions = filter_upparat_job_exectutions( payload["jobs"].get(JOBS_QUEUED, [])) # If there are jobs available go to job selection state if in_progress_job_executions or queued_job_executions: logger.debug("Job executions available.") self.publish( Event( JOBS_AVAILABLE, **{ JOB_EXECUTION_SUMMARIES: { JOB_EXECUTION_SUMMARIES_PROGRESS: in_progress_job_executions, JOB_EXECUTION_SUMMARIES_QUEUED: queued_job_executions, } }))
def _on_unsubscribe_handler(self, _, __, mid): # See comment (B), same applies here. if mid in self._unsubscription_mid: topic = self._unsubscription_mid.pop(mid) self._queue.put( Event(MQTT_UNSUBSCRIBED, **{MQTT_EVENT_TOPIC: topic})) else: logger.error(f"No topic mapping found for unsubscription {mid}")
def _on_message_handler(self, _, __, message): self._queue.put( Event( MQTT_MESSAGE_RECEIVED, **{ MQTT_EVENT_TOPIC: message.topic, MQTT_EVENT_PAYLOAD: message.payload, }, ))
def _create_mqtt_message_event(topic, payload=None): if not payload: payload = {} return Event( MQTT_MESSAGE_RECEIVED, **{ MQTT_EVENT_TOPIC: topic, MQTT_EVENT_PAYLOAD: json.dumps(payload) }, )
def _on_subscribe_handler(self, _, __, mid, ___): # (B) see comment (A) in subscribe(): # we want to know the mid → topic mapping here # since we want to publish an event with the topic # that has been subscribed to. if mid in self._subscription_mid: self._queue.put( Event( MQTT_SUBSCRIBED, **{MQTT_EVENT_TOPIC: self._subscription_mid.pop(mid)}, )) else: logger.error(f"No topic mapping found for subscription {mid}")
def test_restarted_failure(restart_state): statemachine, _ = restart_state statemachine.dispatch(Event(RESTART_INTERRUPTED)) assert isinstance(statemachine.state, FetchJobsState) return statemachine, statemachine.state
def send_event(self, event): self.sm.dispatch(Event(event))
def _create_mqtt_subscription_event(topic): return Event(MQTT_SUBSCRIBED, **{MQTT_EVENT_TOPIC: topic})
def event_loop(self): self.instructions = 0 self.stepsize = 10000 self.executing = True # exit event loop via setting exit_while, to do cleanup afterwards exit_while = False while not exit_while: if self.is_executing(): for index, (active, func) in enumerate(self.checkpoints): if active: (continue_active, execute) = func(self) if not execute: print("break from breakpoint") return else: if not continue_active: self.checkpoints[index] = False, func # IMPORTANT: we first execute the current opcode (i.e. where pc points to)... self.cpu.do_next_step() self.post_op() self.instructions += 1 # breakpoint events if False: if self.cpu.PC in [0x4ec4, 0x4f65, 0x4f68]: self.states.dispatch(Event('breakpoint')) # do things at certain PCs if False: if self.cpu.PC == 0x4066: self.mem[0x1407] = 20 # empty pygame event queue for event in pygame.event.get(): if event.type == pygame.QUIT: return if event.type == pygame.KEYDOWN: key = ord( event.unicode.upper()) if event.unicode != '' else 0 if event.key == pygame.K_x and (pygame.key.get_mods() & pygame.KMOD_CTRL): self.states.dispatch(Event('ctrlx')) continue elif event.key == pygame.K_LEFT: # A2 0x08 self.states.dispatch(Event('left')) continue elif event.key == pygame.K_RIGHT: # A2 0x15 self.states.dispatch(Event('right')) continue elif event.key == pygame.K_PRINT: self.states.dispatch(Event('halt')) exit_while = True break elif event.key == pygame.K_d: self.states.dispatch(Event('d')) continue elif event.key == pygame.K_l: self.states.dispatch(Event('l')) continue if key != 0: self.states.dispatch(Event('key', key=key)) continue # after we've emptied the event queue we can update the screen self.update_display() # do some cleanup here print(self.states.leaf_state.name)
def restart_state(mocker, install_state): statemachine, _ = install_state statemachine.dispatch(Event(INSTALLATION_DONE)) assert isinstance(statemachine.state, RestartState) return statemachine, statemachine.state
def install_state(mocker, download_state): statemachine, _ = download_state statemachine.dispatch(Event(DOWNLOAD_COMPLETED)) assert isinstance(statemachine.state, InstallState) return statemachine, statemachine.state
def download_state(mocker, verify_job_state): statemachine, _ = verify_job_state statemachine.dispatch(Event(JOB_VERIFIED)) assert isinstance(statemachine.state, DownloadState) return statemachine, statemachine.state
def toast(self): self.sm.dispatch(Event('toast', oven=self))
def close_door(self): self.sm.dispatch(Event('close', oven=self))
def test_verify_installation_complete(verify_installation_state): statemachine, _ = verify_installation_state statemachine.dispatch(Event(JOB_INSTALLATION_COMPLETE)) assert isinstance(statemachine.state, FetchJobsState) return statemachine, statemachine.state
def test_install_done(install_state): statemachine, _ = install_state statemachine.dispatch(Event(INSTALLATION_DONE)) assert isinstance(statemachine.state, RestartState) return statemachine, statemachine.state
def test_install_interrupted(install_state): statemachine, _ = install_state statemachine.dispatch(Event(INSTALLATION_INTERRUPTED)) assert isinstance(statemachine.state, FetchJobsState) return statemachine, statemachine.state
def test_download_completed(download_state): statemachine, _ = download_state statemachine.dispatch(Event(DOWNLOAD_COMPLETED)) assert isinstance(statemachine.state, InstallState) return statemachine, statemachine.state
def test_download_interrupted(download_state): statemachine, _ = download_state statemachine.dispatch(Event(DOWNLOAD_INTERRUPTED)) assert isinstance(statemachine.state, FetchJobsState) return statemachine, statemachine.state
def bake(self): self.sm.dispatch(Event('bake', oven=self))
def monitor_state(fetch_jobs_state): statemachine, _ = fetch_jobs_state statemachine.dispatch(Event(NO_JOBS_PENDING)) assert isinstance(statemachine.state, MonitorState) return statemachine, statemachine.state
def open_door(self): self.sm.dispatch(Event('open', oven=self))
def select_job_state(fetch_jobs_state): statemachine, _ = fetch_jobs_state statemachine.dispatch(Event(JOBS_AVAILABLE)) assert isinstance(statemachine.state, SelectJobState) return statemachine, statemachine.state
def on_timeout(self): print('Timeout...') self.sm.dispatch(Event('timeout', oven=self)) self.timer = threading.Timer(Oven.TIMEOUT, self.on_timeout)
def verify_job_state(select_job_state): statemachine, _ = select_job_state statemachine.dispatch(Event(JOB_SELECTED)) assert isinstance(statemachine.state, VerifyJobState) return statemachine, statemachine.state