Skip to content

Commit 00225de

Browse files
committed
added DelayWorkflow; removed sending null data; added optional data payload to SendTriggerWorkflow
1 parent 1801f1f commit 00225de

3 files changed

Lines changed: 49 additions & 17 deletions

File tree

logic/message.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,15 @@ def toJSON(self):
104104
>>> m.toJSON()
105105
'{"method": "trigger", "state": "on", "data": null}'
106106
"""
107-
result = json.dumps({
108-
"method": self.method.name.lower(),
109-
"state": self.state.name.lower(),
110-
"data": self.data
111-
})
107+
if self.data:
108+
result = json.dumps({
109+
"method": self.method.name.lower(),
110+
"state": self.state.name.lower(),
111+
"data": self.data
112+
})
113+
else:
114+
result = json.dumps({
115+
"method": self.method.name.lower(),
116+
"state": self.state.name.lower()
117+
})
112118
return result

logic/workflow_definition.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ def create(self, settings):
4040
LightControlWorkflow(Location.LOBBYROOM, State.ON, 255, (0, 0, 0)),
4141
LightControlWorkflow(Location.MAINROOM, State.ON, 255, (0, 0, 0)),
4242
LightControlWorkflow(Location.SERVERROOM, State.ON, 255, (0, 0, 0)),
43-
TTSAudioWorkflow("TTS1", "Warning, power failure! Warning, power failure!"),
43+
DelayWorkflow("FailDelay1", 1),
44+
TTSAudioWorkflow("FailTTS1", "Warning, power failure! Warning, power failure!"),
45+
DelayWorkflow("FailDelay2", 5),
4446
LightControlWorkflow(Location.LOBBYROOM, State.ON, 255, (255, 0, 0)),
4547
#SendTriggerWorkflow("Play Video", "env/video", State.ON),
4648
]),
@@ -52,7 +54,7 @@ def create(self, settings):
5254
CombinedWorkflow("1st Door", [
5355
SendTriggerWorkflow("Open Control Room Door", "4/door/entrance", State.ON),
5456
LightControlWorkflow(Location.MAINROOM, State.ON, 255, (255, 0, 0)),
55-
TTSAudioWorkflow("TTS2", "Warning, emergency backup battery empty, please recharge!"),
57+
TTSAudioWorkflow("Door1TTS1", "Warning, emergency backup battery empty, please recharge!"),
5658
]),
5759
]),
5860

@@ -65,7 +67,7 @@ def create(self, settings):
6567
LightControlWorkflow(Location.LOBBYROOM, State.ON, 255, (255, 255, 255)),
6668
LightControlWorkflow(Location.MAINROOM, State.ON, 255, (255, 255, 255)),
6769
LightControlWorkflow(Location.SERVERROOM, State.ON, 255, (255, 0, 0)),
68-
TTSAudioWorkflow("TTS3", "Emergency power restored! Generators offline! Restart server!"),
70+
TTSAudioWorkflow("RestoredTTS1", "Emergency power restored! Generators offline! Restart server!"),
6971
]),
7072
SequenceWorkflow("Puzzle 3 - Radio", [
7173
Workflow("Antenna Aligned", "3/gamecontrol/antenna"),

logic/workflow_extras.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import json
2-
from workflow import *
1+
import json, time
2+
from workflow import CombinedWorkflow, SingleCommandWorkflow
33
from message import Message, Method, State
44
from util import Location
55

@@ -60,10 +60,10 @@ def __init__(self, workflows, settings=None):
6060

6161
class SendTriggerWorkflow(SingleCommandWorkflow):
6262
"""
63-
This workflow sends trigger:on and trigger:off to a given topic.
63+
This workflow sends trigger:on and trigger:off to a given topic. Can optionally send data as well.
6464
"""
6565

66-
def __init__(self, name, topic, target_state):
66+
def __init__(self, name, topic, target_state, data=None):
6767
"""
6868
Initializes a new instance of this class.
6969
@@ -80,6 +80,7 @@ def __init__(self, name, topic, target_state):
8080
"""
8181
self.target_state = target_state
8282
self.topic = topic
83+
self.data = data
8384
super().__init__(name)
8485

8586
def _execute_single_command(self, client):
@@ -91,11 +92,11 @@ def _execute_single_command(self, client):
9192
client : Client
9293
MQTT client
9394
"""
94-
self._publishTrigger(client, self.target_state)
95+
self._publishTrigger(client, self.target_state, self.data)
9596

96-
def _publishTrigger(self, client, state):
97+
def _publishTrigger(self, client, state, data=None):
9798
if self.topic is not None:
98-
message = Message(Method.TRIGGER, state)
99+
message = Message(Method.TRIGGER, state, data)
99100
client.publish(self.topic, message.toJSON(), 2)
100101
print(f"[{self.name}] Triggered '{state.name}'...")
101102

@@ -250,8 +251,6 @@ def _publishTrigger(self, client, state, data):
250251
)
251252

252253

253-
254-
255254
class LightControlWorkflow(CombinedWorkflow):
256255
"""
257256
This workflow controls the LED stripes at the specified locationin one single workfow.
@@ -294,3 +293,28 @@ def __init__(self, target_location, target_state, brightness=255, color=(255, 25
294293

295294
name = f"Turn {target_state.name} {target_location.name} lights ({brightness})/255"
296295
super().__init__(name, workflows, None)
296+
297+
298+
class DelayWorkflow(SingleCommandWorkflow):
299+
"""
300+
This workflow implements a blocking delay.
301+
"""
302+
303+
def __init__(self, name, delay_sec):
304+
"""
305+
Initializes a new instance of this class.
306+
307+
Parameters
308+
----------
309+
name : str
310+
Display name of the workflow.
311+
312+
delay_sec : int
313+
The number of seconds delay.
314+
"""
315+
self.delay_sec = delay_sec
316+
super().__init__(name)
317+
318+
def _execute_single_command(self, client):
319+
if self.delay_sec > 0:
320+
time.sleep(self.delay_sec)

0 commit comments

Comments
 (0)