-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestScript.py
More file actions
198 lines (156 loc) · 7.44 KB
/
TestScript.py
File metadata and controls
198 lines (156 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import asyncio
from datetime import timedelta
import structlog
from pyhocon import ConfigFactory
from csw.CommandResponse import Started, Completed
from csw.CurrentState import CurrentState
from csw.Event import Event, SystemEvent
from csw.EventName import EventName
from csw.ExposureId import ExposureId
from csw.ObsId import ObsId
from csw.ParameterSetType import Observe, Setup
from csw.Prefix import Prefix
from csw.Subsystem import Subsystem
from csw.TMTTime import UTCTime
from esw.ObsMode import ObsMode
from sequencer.Keys import longKey
from sequencer.Script import Script
from sequencer.examples.testData.InitialCommandHandler import InitialCommandHandler
def script(ctx: Script):
log = structlog.get_logger()
lgsfSequencer = ctx.Sequencer(Subsystem.LGSF, ObsMode("darknight"))
testAssembly = ctx.Assembly(Prefix(Subsystem.ESW, "test"), timedelta(seconds=10))
# ESW-134: Reuse code by ability to import logic from one script into another
InitialCommandHandler(ctx, log)
@ctx.onSetup("command-2")
async def handleCommand2(setup: Setup):
log.info(f"XXX TestScript: Received a command-2 setup: {setup}")
# ESW-421 demonstrate creating exposureId and obsId. Getting components from exposureId and ObsId
@ctx.onObserve("exposure-start")
async def handleExposureStart(_: Observe):
obsId = ObsId.make("2021A-011-153")
# do something with ObsId components
log.info(obsId.programId)
log.info(obsId.programId.semesterId)
log.info(obsId.programId.semesterId.semester)
# create exposureId
exposureIdStr = f"{obsId}-TCS-DET-SCI0-0001"
exposureId = ExposureId.make(exposureIdStr)
# do something with exposureId components
log.info(exposureId.subsystem)
log.info(exposureId.det)
await ctx.publishEvent(ctx.exposureStart(exposureId))
@ctx.onSetup("command-3")
async def handleCommand3(setup: Setup):
log.info(f"XXX Received a command-3 setup: {setup}")
@ctx.onSetup("command-4")
async def handleCommand4(_: Setup):
# try sending concrete sequence
setupCommand = ctx.Setup("ESW.test", "command-3")
sequence = ctx.sequenceOf(setupCommand)
# ESW-88, ESW-145, ESW-195
tcsSequencer = ctx.Sequencer(Subsystem.TCS, ObsMode("darknight"))
await tcsSequencer.submitAndWait(sequence, timedelta(seconds=10))
@ctx.onSetup("check-config")
async def handleCheckConfig(setup: Setup):
if await ctx.existsConfig("/tmt/test/wfos.conf"):
await ctx.publishEvent(ctx.SystemEvent("WFOS.test", "check-config.success"))
@ctx.onSetup("get-config-data")
async def handleGetConfigData(setup: Setup):
configValue = "component = wfos"
configData = await ctx.getConfig("/tmt/test/wfos.conf")
if configData:
if str(configData) == str(ConfigFactory.parse_string(configValue)):
await ctx.publishEvent(SystemEvent(Prefix.from_str("WFOS.test"), EventName("get-config.success")))
@ctx.onSetup("get-event")
async def handleGetEvent(s: Setup):
# ESW-88
event = await ctx.getEvent("ESW.test.get.event")
successEvent = ctx.SystemEvent("ESW.test", "get.success")
if not event.isInvalid():
await ctx.publishEvent(successEvent)
@ctx.onSetup("on-event")
async def handleOnEvent(_: Setup):
async def handleEvent(event: Event):
successEvent = ctx.SystemEvent("ESW.test", "onevent.success")
if not event.isInvalid():
await ctx.publishEvent(successEvent)
await ctx.onEvent(handleEvent, "ESW.test.get.event")
@ctx.onSetup("command-for-assembly")
async def handleCommandForAssembly(command: Setup):
submitResponse = await testAssembly.submit(ctx.Setup(str(command.source), "long-running"))
if isinstance(await testAssembly.query(submitResponse.runId), Started):
await ctx.publishEvent(ctx.SystemEvent("tcs.filter.wheel", "query-started-command-from-script"))
if isinstance(await testAssembly.queryFinal(submitResponse.runId, timedelta(seconds=1)), Completed):
await ctx.publishEvent(ctx.SystemEvent("tcs.filter.wheel", "query-completed-command-from-script"))
async def handleCurrentState(currentState: CurrentState):
await ctx.publishEvent(ctx.SystemEvent(
"tcs.filter.wheel",
f"publish-{currentState.stateName}"))
await testAssembly.subscribeCurrentState(["stateName1", "stateName2"], handleCurrentState)
await testAssembly.oneway(command)
@ctx.onSetup("test-sequencer-hierarchy")
async def handleTestSequencerHierarchy(_: Setup):
await asyncio.sleep(5)
@ctx.onSetup("check-exception-1")
async def handleCheckException1(_: Setup):
raise Exception("boom")
@ctx.onSetup("check-exception-2")
async def handleCheckException2(_: Setup):
pass
# XXX TODO implement alarm service DSL
# onSetup("set-alarm-severity") {
# val alarmKey = AlarmKey(Prefix("NFIRAOS.trombone"), "tromboneAxisHighLimitAlarm")
# setSeverity(alarmKey, Major)
# delay(500)
# }
@ctx.onSetup("command-lgsf")
async def handleCommandLgsf(_: Setup):
# NOT update command response to avoid a sequencer to finish immediately
# so that others Add, Append command gets time
setupCommand = ctx.Setup("LGSF.test", "command-lgsf")
sequence = ctx.sequenceOf(setupCommand)
await lgsfSequencer.submitAndWait(sequence, timedelta(seconds=10))
@ctx.onSetup("schedule-once-from-now")
async def handleScheduleOnceFromNow(_: Setup):
currentTime = ctx.utcTimeNow()
async def func():
param = longKey("offset").set(int(abs(currentTime.offsetFromNow().total_seconds() * 1000)))
await ctx.publishEvent(ctx.SystemEvent("ESW.schedule.once", "offset", param))
ctx.scheduleOnceFromNow(timedelta(seconds=1), func)
@ctx.onSetup("schedule-periodically-from-now")
async def handleSchedulePeriodicallyFromNow(_: Setup):
currentTime = ctx.utcTimeNow()
counter = 0
async def publishEvents():
nonlocal counter
param = longKey("offset").set(round(abs(currentTime.offsetFromNow().total_seconds() * 1000)))
await ctx.publishEvent(ctx.SystemEvent("ESW.schedule.periodically", "offset", param))
counter = counter + 1
a = ctx.schedulePeriodicallyFromNow(timedelta(seconds=1), timedelta(seconds=1), publishEvents)
async def countEvents():
ctx.stopWhen(counter > 1)
await ctx.loop(countEvents, milliseconds=50)
a.cancel()
@ctx.onDiagnosticMode()
async def handleDiagnosticMode(startTime: UTCTime, hint: str):
await testAssembly.diagnosticMode(startTime, hint)
@ctx.onOperationsMode()
async def handleOperationsMode():
await testAssembly.operationsMode()
@ctx.onGoOffline()
async def handleGoOffline():
await testAssembly.goOffline()
@ctx.onGoOnline()
async def handleGoOnline():
await testAssembly.goOnline()
# do some actions to abort sequence
# send abortSequence command to downstream sequencer
@ctx.onAbortSequence()
async def handleAbortSequence():
await lgsfSequencer.abortSequence()
# do some actions to stop
# send stop command to downstream sequencer
@ctx.onStop()
async def handleStop():
await lgsfSequencer.stop()