def __init__(self, driver: Driver, serverAPI: ServerAPI, py_audio: PyAudio): self.logger = logging.getLogger("WalkieTalkie") self.players = {} # userID -> audio player object self.serverAPI = serverAPI self.driver = driver self.py_audio = py_audio self.queue = [] self.max_current_priority = -1 MQTT_BROKER = 'mqtt.item.ntnu.no' MQTT_PORT = 1883 # create MQTT client self.logger.info( f'Connecting to MQTT broker {MQTT_BROKER} at port {MQTT_PORT}') self.mqtt_client = mqtt.Client() self.mqtt_client.on_connect = self.on_connect self.mqtt_client.on_message = self.on_message self.mqtt_client.connect(MQTT_BROKER, MQTT_PORT) self.mqtt_client.loop_start() self.update_subscriptions() # Create state machine self.state_machine = Machine(name="queue_manager", transitions=self._get_transitions(), states=self._get_states(), obj=self) driver.add_machine(self.state_machine)
def test_blink(): blink = Blink() t0 = {"source": "initial", "target": "blink_on", "effect": "on"} t1 = { "trigger": "t", "source": "blink_on", "target": "blink_off", "effect": "off" } t2 = { "trigger": "t", "source": "blink_off", "target": "blink_on", "effect": "on" } stm_blink = Machine(transitions=[t0, t1, t2], obj=blink, name="blink") blink.stm = stm_blink print(stm_blink.print_graph()) scheduler = Driver() scheduler.add_stm(stm_blink) scheduler.start() print("scheduler started") scheduler.wait_until_finished()
def __init__(self, mqtt_client, device): self.recording = False self.chunk = 1024 self.sample_format = pyaudio.paInt16 self.channels = 2 # you might change this based on your mic self.fs = 44100 self.filename = "output.wav" self.p = pyaudio.PyAudio() self.mqtt_client = mqtt_client self.device = device t0 = {'source': 'initial', 'target': 'ready'} t1 = {'trigger': 'start', 'source': 'ready', 'target': 'recording'} t2 = { 'trigger': 'done', 'source': 'recording', 'target': 'processing', 'effect': 'stop' } t3 = {'trigger': 'done', 'source': 'processing', 'target': 'ready'} s_recording = { 'name': 'recording', 'do': 'record()', "stop": "stop()", "start_timer": "start_timer('stop', 5000)" } s_processing = {'name': 'processing', 'do': 'process()'} self.stm = Machine(name='recorder', transitions=[t0, t1, t2, t3], states=[s_recording, s_processing], obj=self)
def __init__(self, driver: Driver, py_audio: PyAudio): self.logger = logging.getLogger("WalkieTalkie") self.py_audio = py_audio self.state_machine = Machine( name="audio_player_" + str(id(self)), # Unique identifier for each audio player object transitions=self._get_transitions(), states=self._get_states(), obj=self) driver.add_machine(self.state_machine)
def test(self): cfc = CompoundFunctionContainer(13) t0 = {"source": "initial", "target": "s1"} t1 = { "trigger": "a", "source": "s1", "function": cfc.compound, "targets": "s2 s3", } t2 = {"trigger": "b", "source": "s2", "target": "final"} t3 = {"trigger": "b", "source": "s3", "target": "final"} stm = Machine(name="stm", transitions=[t0, t1, t2, t3], obj=None) _ = stmpy.get_graphviz_dot(stm) driver = Driver() driver.add_machine(stm) driver.start(keep_active=False) driver.send("a", "stm", args=["hi"], kwargs={"b1": "bbb"}) driver.send("b", "stm") driver.wait_until_finished()
def test(self): terminate = Terminate() t0 = {"source": "initial", "target": "s_1"} t1 = { "trigger": "t", "source": "s_1", "target": "s_2", "effect": "action" } stm_terminate = Machine(name="stm_terminate", transitions=[t0, t1], obj=terminate) terminate.stm = stm_terminate scheduler = Driver() scheduler.add_machine(stm_terminate) scheduler.start(max_transitions=2, keep_active=False) scheduler.send("t", "stm_terminate") scheduler.wait_until_finished() _ = stmpy.get_graphviz_dot(stm_terminate) _ = scheduler.print_status() self.assertTrue(True)
def test(self): tick1 = EntryExitSelfLogic() t0 = {"source": "initial", "target": "s1"} t1 = { "trigger": "b", "source": "s1", "target": "s1", "effect": "add('b')" } t2 = { "trigger": "done", "source": "s1", "target": "final", "effect": "add('c')", } s1 = {"name": "s1", "do": "do_action('a')"} stm = Machine(name="stm", transitions=[t0, t1, t2], states=[s1], obj=tick1) tick1.stm = stm _ = stmpy.get_graphviz_dot(stm) driver = Driver() driver.add_machine(stm) driver.start() driver.wait_until_finished()
def get_state_machine(name: str, observers: list): recognizer = Recognizer(recognition_keyword='lisa', stm_observers=observers) t_i0 = {'source': 'initial', 'target': 'listening'} t_01 = {'trigger': 'done', 'source': 'listening', 'target': 'recognizing'} t_10_a = { 'trigger': 'recognition_error', 'source': 'recognizing', 'target': 'listening' } t_10_b = { 'trigger': 'recognized', 'source': 'recognizing', 'function': recognizer.was_adressed } s_listening = {'name': 'listening', 'do': 'listen'} s_recognizing = {'name': 'recognizing', 'entry': 'recognize'} stm = Machine(name=name, transitions=[t_i0, t_01, t_10_a, t_10_b], states=[s_listening, s_recognizing], obj=recognizer) recognizer.stm = stm return stm
def __init__(self, driver): self.stm_driver = driver self.recording = False self.chunk = 1024 # Record in chunks of 1024 samples self.sample_format = pyaudio.paInt16 # 16 bits per sample self.channels = 1 self.fs = 44100 # Record at 44100 samples per second self.filename = "output.wav" self._logger = logging.getLogger(__name__) t0 = {'source': 'initial', 'target': 'ready'} t1 = {'trigger': 'start', 'source': 'ready', 'target': 'recording'} t2 = {'trigger': 'done', 'source': 'recording', 'target': 'processing'} t3 = {'trigger': 'done', 'source': 'processing', 'target': 'ready'} s_recording = { 'name': 'recording', 'do': 'record()', "stop": "stop()", 'start': 'defer' } s_processing = { 'name': 'processing', 'do': 'process()', 'start': 'defer' } self.stm = Machine(name="recorder", transitions=[t0, t1, t2, t3], states=[s_recording, s_processing], obj=self) self.stm_driver.add_machine(self.stm)
def __init__(self, transitions, states, debug): self.payload = {} self._logger = logging.getLogger(__name__) self._logger.info('logging under name {}.'.format(__name__)) self._logger.info('Starting Component') self.debug = debug self.app = None self.lock = Lock() self.recorder = Recorder(self) self.text_to_speech = Speaker() self.overwrote_last = False self.uuid = uuid4().hex if self.debug: self.uuid = "122ec9e8edda48f8a6dd290747acfa8c" self.channel = "{server}{uuid}".format(server=MQTT_TOPIC_BASE,uuid=self.uuid) self.name = "jonas" stm_walkie_talkie_name = "{}_walkie_talkie".format(self.name) walkie_talkie_machine = Machine(transitions=transitions, states=states, obj=self, name=stm_walkie_talkie_name) self.stm = walkie_talkie_machine recognizer_stm = get_state_machine('stm_recognizer', [stm_walkie_talkie_name]) self.stm_driver = Driver() self.stm_driver.add_machine(walkie_talkie_machine) self.stm_driver.add_machine(recognizer_stm) self.stm_driver.start() self._logger.debug('Component initialization finished')
def test(self): t0 = { "source": "initial", "target": "s1", "effect": "start_timer('t1', 1000)", } t1 = { "trigger": "a", "source": "s1", "target": "s1", "effect": "stop_timer('t1')", } t2 = {"trigger": "b", "source": "s1", "target": "final"} stm = Machine(name="stm", transitions=[t0, t1, t2], obj=None) _ = stmpy.get_graphviz_dot(stm) driver = Driver() driver.add_machine(stm) driver.start(keep_active=False) driver.send("a", "stm") driver.send("b", "stm") driver.wait_until_finished()
def test_tick_2(): logger = logging.getLogger("stmpy.Driver") logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s") ch.setFormatter(formatter) logger.addHandler(ch) scheduler = Driver() tick = Tick_2() t0 = {"source": "initial", "target": "s_tick", "effect": "on_init"} t1 = {"trigger": "tick", "source": "s_tick", "function": tick.t_1} t2 = {"trigger": "tock", "source": "s_tock", "function": tick.t_2} stm_tick = Machine(name="stm_tick", transitions=[t0, t1, t2], obj=tick) # the object may need the stm to add events and control timers tick.stm = stm_tick scheduler.add_stm(stm_tick) scheduler.start(max_transitions=10) scheduler.wait_until_finished()
def test(self): print(__name__) debug_level = logging.DEBUG logger = logging.getLogger(__name__) logger.setLevel(debug_level) ch = logging.StreamHandler() ch.setLevel(debug_level) formatter = logging.Formatter( "%(asctime)s - %(name)-12s - %(levelname)-8s - %(message)s") ch.setFormatter(formatter) logger.addHandler(ch) # terminate = Terminate() t0 = {"source": "initial", "target": "s1"} t1 = {"trigger": "b", "source": "s1", "target": "s2"} t2 = {"trigger": "a", "source": "s2", "target": "final"} s1 = {"name": "s1", "a": "defer", "a2": "defer", "a3": "defer"} s2 = {"name": "s2", "a2": "defer", "a3": "defer"} stm_terminate = Machine(name="stm", transitions=[t0, t1, t2], states=[s1, s2], obj=None) # terminate.stm = stm_terminate _ = stmpy.get_graphviz_dot(stm_terminate) print(_) def unwrap(queue): s = [] if queue is None: return s for event in queue: if event is not None: s.append(event["id"]) return s driver = Driver() driver.add_machine(stm_terminate) driver.start(max_transitions=30, keep_active=False) driver.send("a", "stm") driver.send("a2", "stm") driver.send("a3", "stm") print("Events {}".format(unwrap(driver._event_queue.queue))) print(stm_terminate.state) print("Defers {}".format(unwrap(stm_terminate._defer_queue))) print(driver._max_transitions) driver.send("b", "stm") print("Events {}".format(unwrap(driver._event_queue.queue))) print(stm_terminate.state) print("Defers {}".format(unwrap(stm_terminate._defer_queue))) print(driver._max_transitions) driver.wait_until_finished() print(stm_terminate.state) self.assertTrue(True)
def create_machine(self, name): t0 = {'source': 'initial', 'target': 'ready'} t5 = {'trigger': 'emg_msg', 'source': 'ready', 'target': 'ready', 'effect': 'switch_emg_mode'} t7 = {'trigger': 'change_channel_signal', 'source': 'ready', 'target': 'ready', 'effect': 'change_channel'} stm = Machine(name=name, transitions=[t0, t5, t7], obj=self) self.stm = stm return self.stm
def test_ping_pong(): scheduler = Driver() ping = Ping() t0 = {"source": "initial", "target": "s_1", "effect": "on_init"} t1 = { "trigger": "t", "source": "s_1", "target": "s_2", "effect": "on_timeout" } t2 = { "trigger": "pong", "source": "s_2", "target": "s_1", "effect": "on_pong" } stm_ping = Machine(transitions=[t0, t1, t2], obj=ping, name="stm_ping") ping.stm = stm_ping pong = Pong() t0 = {"source": "initial", "target": "s_1"} t1 = { "trigger": "t", "source": "s_2", "target": "s_1", "effect": "on_timeout" } t2 = { "trigger": "ping", "source": "s_1", "target": "s_2", "effect": "on_ping" } stm_pong = Machine(transitions=[t0, t1, t2], obj=pong, name="stm_pong") pong.stm = stm_pong scheduler.add_stm(stm_ping) scheduler.add_stm(stm_pong) scheduler.step(4) scheduler.print_state() scheduler.step() scheduler.print_state()
def __init__(self, mqttAPI: MqttAPI, driver: Driver, py_audio: PyAudio, serverAPI: ServerAPI): self.logger = logging.getLogger("WalkieTalkie") self.serverAPI = serverAPI # Define private variables self._recording = False # Save references self.mqttAPI = mqttAPI self.py_audio = py_audio # Create state machine self.state_machine = Machine(name="audio_recorder", transitions=self._get_transitions(), states=self._get_states(), obj=self) driver.add_machine(self.state_machine)
def __init__(self, plant_name, myclient): self._logger = logging.getLogger(__name__) self._logger.warning("Created airHumidity machine") self.plant_name = plant_name self.sh = SenseHat() watering_machine = Machine(transitions=[self.t0, self.t1], obj=self, states=[self.idle], name='AirHumid') self.stm = watering_machine self.mqtt = myclient
def __init__(self, plant_name, humidityCh): self._logger = logging.getLogger(__name__) self._logger.warning("Created watering machine") self.plant_name = plant_name watering_machine = Machine( transitions=[self.t0, self.t1, self.t2], obj=self, states=[self.not_watering, self.watering_plant], name='Watering') self.stm = watering_machine self.humidityCh = humidityCh
def __init__(self): self.recorder = Recorder() t0_r = {'source': 'initial', 'target': 'ready'} t1_r = { 'trigger': 'start_recording', 'source': 'ready', 'target': 'recording' } t2_r = {'trigger': 'done', 'source': 'recording', 'target': 'ready'} s_ready = {'name': 'ready'} s_recording = { 'name': 'recording', 'do': 'record()', "stop": "stop_recording()" } self.stm_recording = Machine(name='stm_recording', transitions=[t0_r, t1_r, t2_r], states=[s_ready, s_recording], obj=self.recorder) self.recorder.stm = self.stm_recording self.speaker = Speaker() t0_s = {'source': 'initial', 'target': 'ready'} t1_s = {'trigger': 'speak', 'source': 'ready', 'target': 'speaking'} t2_s = {'trigger': 'done', 'source': 'speaking', 'target': 'ready'} s1_s = {'name': 'speaking', 'do': 'speak(*)', 'speak': 'defer'} self.stm_speaker = Machine(name='stm_speaker', transitions=[t0_s, t1_s, t2_s], states=[s1_s], obj=self.speaker) self.speaker.stm = self.stm_speaker self.driver = Driver() self.driver.add_machine(self.stm_recording) self.driver.add_machine(self.stm_speaker) self.driver.start() print('Audio Module ready')
def __init__(self, driver): self.stm_driver = driver t0 = {'source': 'initial', 'target': 'ready'} t1 = {'trigger': 'start', 'source': 'ready', 'target': 'playing'} t2 = {'trigger': 'done', 'source': 'playing', 'target': 'ready'} s_playing = {'name': 'playing', 'do': 'play(*)', 'start': 'defer'} self.stm = Machine(name='player', transitions=[t0, t1, t2], states=[s_playing], obj=self) self.stm_driver.add_machine(self.stm)
def __init__(self, driver): self.recording = False self.chunk = 1024 # Record in chunks of 1024 samples self.sample_format = pyaudio.paInt16 # 16 bits per sample self.channels = 2 self.fs = 44100 # Record at 44100 samples per second self.filename = "output.wav" self.p = pyaudio.PyAudio() self.stm_recorder = Machine( name='stm_recorder', transitions=[self.t0, self.t1, self.t2, self.t3], states=[self.s_recording, self.s_processing], obj=self) self.driver = driver self.driver.add_machine(self.stm_recorder)
def __init__(self, driver): self.stm_driver = driver t0 = {'source': 'initial', 'target': 'ready'} t1 = {'trigger': 'saveFile', 'source': 'ready', 'target': 'writing'} t2 = {'trigger': 'done', 'source': 'writing', 'target': 'ready'} t3 = {'trigger': 'deleteFile', 'source': 'ready', 'target': 'deleting'} t4 = {'trigger': 'done', 'source': 'deleting', 'target': 'ready'} s_writing = {'name': 'writing', 'do': 'save(*)', 'saveFile': 'defer', 'deleteFile': 'defer', } s_deleting = {'name': 'deleting', 'do': 'delete(*)', 'saveFile': 'defer', 'deleteFile': 'defer'} self.stm = Machine(name="fileManager", transitions=[t0, t1, t2, t3, t4], states=[s_writing, s_deleting], obj=self) self.stm_driver.add_machine(self.stm)
def test_timer(): if "s" is "s": print("same!") t0 = {"source": "initial", "target": "s_1"} m = Machine(transitions=[t0], obj=None, name="t") driver = Driver() driver.add_stm(m) m.start_timer("t", 1000) print(driver.print_status()) m.stop_timer("t") print(driver.print_status())
def create_machine(m_name, component): recorder = Recorder() t0 = {'source': 'initial', 'target': 'ready'} t1 = {'trigger': 'start', 'source': 'ready', 'target': 'recording'} t2 = {'trigger': 'done', 'source': 'recording', 'target': 'processing'} t3 = {'trigger': 'done', 'source': 'processing', 'target': 'ready'} s_recording = {'name': 'recording', 'do': 'record()', "stop": "stop()"} s_processing = {'name': 'processing', 'do': 'process()'} recorder_stm = Machine(name=m_name, transitions=[t0, t1, t2, t3], states=[s_recording, s_processing], obj=recorder) recorder.stm = recorder_stm return recorder
def test(self): busy = Busy() t0 = {"source": "initial", "target": "s_busy", "effect": "on_busy"} t1 = { "trigger": "busy", "source": "s_busy", "target": "s_busy", "effect": "on_busy", } stm_busy = Machine(name="busy", transitions=[t0, t1], obj=busy) busy.stm = stm_busy scheduler = Driver() scheduler.add_machine(stm_busy) scheduler.start(max_transitions=5) scheduler.wait_until_finished() self.assertTrue(True)
def test_busy(): busy = Busy() t0 = {"source": "initial", "target": "s_busy", "effect": "on_busy"} t1 = { "trigger": "busy", "source": "s_busy", "target": "s_busy", "effect": "on_busy", } stm_busy = Machine(transitions=[t0, t1], obj=busy, name="stm_busy") busy.stm = stm_busy scheduler = Driver() scheduler.add_stm(stm_busy) scheduler.start(max_transitions=100) print("scheduler started") scheduler.wait_until_finished()
def test(self): t0 = {"source": "initial", "target": "s1"} t1 = { "trigger": "a", "source": "s1", "function": self.compound, "targets": "final", } stm = Machine(name="stm", transitions=[t0, t1], obj=None) _ = stmpy.get_graphviz_dot(stm) driver = Driver() driver.add_machine(stm) driver.start(keep_active=False) driver.send("a", "stm", args=["hi"], kwargs={"b1": "bbb"}) driver.wait_until_finished()
def test(self): two = TwoMethods() t0 = {"source": "initial", "target": "s_1"} t1 = { "trigger": "t", "source": "s_1", "target": "s_2", "effect": "m1;m2" } stm_two = Machine(name="stm_two", transitions=[t0, t1], obj=two) two.stm = stm_two scheduler = Driver() scheduler.add_machine(stm_two) scheduler.start(max_transitions=2) print("scheduler started") scheduler.send("t", "stm_two") scheduler.wait_until_finished() self.assertTrue(True)
def activate_lights(): global scheduler lights = Lights() t0 = {'source': 'initial', 'target': 's_none', 'effect': 'on_init'} t1 = { 'trigger': 'goto_green', 'source': 's_none', 'target': 's_green', 'effect': 'on_green' } t2 = { 'trigger': 'goto_none', 'source': 's_green', 'target': 's_none', 'effect': 'on_none' } t3 = { 'trigger': 'goto_red', 'source': 's_none', 'target': 's_red', 'effect': 'on_red' } t4 = { 'trigger': 'goto_none', 'source': 's_red', 'target': 's_none', 'effect': 'on_none' } stm_lights = Machine(name='stm_lights', transitions=[t0, t1, t2, t3, t4], obj=lights) lights.stm = stm_lights scheduler.add_machine(stm_lights) scheduler.start()
def test(self): tick1 = EntryExitSelfLogic() t0 = {"source": "initial", "target": "s1"} t1 = { "trigger": "b", "source": "s1", "target": "s1", "effect": "add('b')" } t2 = { "trigger": "c", "source": "s1", "target": "final", "effect": "add('c')" } s1 = { "name": "s1", "a": "add('a')", "entry": "add('entry')", "exit": "add('exit')", } stm = Machine(name="stm", transitions=[t0, t1, t2], states=[s1], obj=tick1) tick1.stm = stm _ = stmpy.get_graphviz_dot(stm) driver = Driver() driver.add_machine(stm) driver.start(max_transitions=30, keep_active=False) driver.send("a", "stm") driver.send("b", "stm") driver.send("c", "stm") driver.wait_until_finished()