etho-python 0.22.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- etho/__init__.py +9 -0
- etho/app.py +569 -0
- etho/assets/etho_icon1_pipeline_dark.svg +23 -0
- etho/assets/etho_icon1_pipeline_light.svg +30 -0
- etho/cli.py +215 -0
- etho/client.py +384 -0
- etho/services/ANAZeroService.py +123 -0
- etho/services/BLTZeroService.py +108 -0
- etho/services/DAQZeroService.py +326 -0
- etho/services/DLPZeroService.py +240 -0
- etho/services/GCMZeroService.py +271 -0
- etho/services/GOVZeroService.py +231 -0
- etho/services/NICounterZeroService.py +92 -0
- etho/services/NITriggerZeroService.py +120 -0
- etho/services/TemplateZeroService.py +98 -0
- etho/services/ZeroService.py +272 -0
- etho/services/__init__.py +0 -0
- etho/services/callbacks/__init__.py +19 -0
- etho/services/callbacks/_base.py +92 -0
- etho/services/callbacks/_image.py +798 -0
- etho/services/callbacks/_trace.py +456 -0
- etho/services/camera/__init__.py +13 -0
- etho/services/camera/base.py +164 -0
- etho/services/camera/basler.py +201 -0
- etho/services/camera/dummy.py +139 -0
- etho/services/camera/hamamatsu.py +133 -0
- etho/services/camera/spinnaker.py +317 -0
- etho/services/camera/ximea.py +167 -0
- etho/services/daq/IOTask.py +311 -0
- etho/services/daq/ai.py +174 -0
- etho/services/dlp/dlp_make_warpfiles.py +131 -0
- etho/services/dlp/dlp_runners.py +238 -0
- etho/services/utils/__init__.py +0 -0
- etho/services/utils/concurrent_task.py +236 -0
- etho/services/utils/log_exceptions.py +39 -0
- etho/utils/__init__.py +0 -0
- etho/utils/calibration.py +88 -0
- etho/utils/config.py +41 -0
- etho/utils/runner.py +121 -0
- etho/utils/shuffled_cycle.py +45 -0
- etho/utils/sound.py +336 -0
- etho/utils/tui.py +105 -0
- etho/utils/zeroclient.py +95 -0
- etho_python-0.22.0.dist-info/METADATA +49 -0
- etho_python-0.22.0.dist-info/RECORD +47 -0
- etho_python-0.22.0.dist-info/WHEEL +4 -0
- etho_python-0.22.0.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
import asyncio
|
|
3
|
+
import logging
|
|
4
|
+
import threading
|
|
5
|
+
import time
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from datetime import datetime
|
|
8
|
+
from typing import Any, Optional
|
|
9
|
+
|
|
10
|
+
import defopt
|
|
11
|
+
|
|
12
|
+
from .ZeroService import BaseZeroService
|
|
13
|
+
from .utils.log_exceptions import for_all_methods, log_exceptions
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
from bleak import AdvertisementData, BleakScanner, BLEDevice
|
|
17
|
+
|
|
18
|
+
bleak_import_error = None
|
|
19
|
+
except ImportError as bleak_import_error:
|
|
20
|
+
AdvertisementData = Any
|
|
21
|
+
BLEDevice = Any
|
|
22
|
+
BleakScanner = None
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
DEFAULT_INTERVAL = 60.0
|
|
26
|
+
H5075_MANUFACTURER_ID = 0xEC88
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def normalize_address(address: str) -> str:
|
|
30
|
+
if address is None:
|
|
31
|
+
raise ValueError("address is required")
|
|
32
|
+
address = address.strip()
|
|
33
|
+
if not address:
|
|
34
|
+
raise ValueError("address is required")
|
|
35
|
+
return address.upper()
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass
|
|
39
|
+
class H5075Measurement:
|
|
40
|
+
timestamp: datetime
|
|
41
|
+
temperature_c: float
|
|
42
|
+
humidity: float
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def decode_h5075_measurement(payload: bytes, timestamp: Optional[datetime] = None) -> H5075Measurement:
|
|
46
|
+
if len(payload) < 4:
|
|
47
|
+
raise ValueError("manufacturer payload is too short")
|
|
48
|
+
|
|
49
|
+
raw = int.from_bytes(payload[1:4], byteorder="big", signed=False)
|
|
50
|
+
is_negative = bool(raw & 0x800000)
|
|
51
|
+
if is_negative:
|
|
52
|
+
raw ^= 0x800000
|
|
53
|
+
|
|
54
|
+
temperature_c = int(raw / 1000) / 10.0
|
|
55
|
+
if is_negative:
|
|
56
|
+
temperature_c = -temperature_c
|
|
57
|
+
humidity = (raw % 1000) / 10.0
|
|
58
|
+
|
|
59
|
+
if timestamp is None:
|
|
60
|
+
timestamp = datetime.now()
|
|
61
|
+
|
|
62
|
+
return H5075Measurement(timestamp=timestamp, temperature_c=temperature_c, humidity=humidity)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def format_measurement_log_line(measurement: H5075Measurement) -> str:
|
|
66
|
+
return f"{measurement.timestamp:%Y-%m-%d %H:%M:%S},{measurement.temperature_c:.1f},{measurement.humidity:.1f}"
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@for_all_methods(log_exceptions(logging.getLogger(__name__)))
|
|
70
|
+
class GOV(BaseZeroService):
|
|
71
|
+
LOGGING_PORT = 1455
|
|
72
|
+
SERVICE_PORT = 4255
|
|
73
|
+
SERVICE_NAME = "GOV"
|
|
74
|
+
|
|
75
|
+
def setup(self, address: str, interval: float = DEFAULT_INTERVAL, duration: float = 0):
|
|
76
|
+
if bleak_import_error is not None:
|
|
77
|
+
raise bleak_import_error
|
|
78
|
+
|
|
79
|
+
interval = DEFAULT_INTERVAL if interval is None else float(interval)
|
|
80
|
+
if interval <= 0:
|
|
81
|
+
raise ValueError("interval must be > 0")
|
|
82
|
+
|
|
83
|
+
self.address = normalize_address(address)
|
|
84
|
+
self.interval = interval
|
|
85
|
+
self.duration = float(duration)
|
|
86
|
+
self.measurement: Optional[H5075Measurement] = None
|
|
87
|
+
self.data = None
|
|
88
|
+
|
|
89
|
+
self._state_lock = threading.Lock()
|
|
90
|
+
self._latest_measurement: Optional[H5075Measurement] = None
|
|
91
|
+
self._latest_measurement_seen_at: Optional[float] = None
|
|
92
|
+
self._last_logged_measurement_seen_at: Optional[float] = None
|
|
93
|
+
self._last_emit_monotonic: Optional[float] = None
|
|
94
|
+
self._thread_stopper = threading.Event()
|
|
95
|
+
self._scanner_thread = threading.Thread(target=self._scanner_worker, args=(self._thread_stopper,), daemon=True)
|
|
96
|
+
|
|
97
|
+
if self.duration > 0:
|
|
98
|
+
self._thread_timer = threading.Timer(self.duration, self.finish, kwargs={"stop_service": True})
|
|
99
|
+
|
|
100
|
+
self.info = {
|
|
101
|
+
"job": {
|
|
102
|
+
"address": self.address,
|
|
103
|
+
"interval": f"{self.interval}s",
|
|
104
|
+
"duration": f"{self.duration}s",
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
def start(self):
|
|
109
|
+
self._time_started = time.time()
|
|
110
|
+
self._scanner_thread.start()
|
|
111
|
+
self._emit_info(f"scanning for {self.address}")
|
|
112
|
+
self._emit_info(f"logging at {self.interval:.1f}s resolution")
|
|
113
|
+
if hasattr(self, "_thread_timer"):
|
|
114
|
+
self._emit_info(f"duration {self.duration} seconds")
|
|
115
|
+
self._thread_timer.start()
|
|
116
|
+
self._emit_info("finish timer started")
|
|
117
|
+
|
|
118
|
+
def _scanner_worker(self, stop_event):
|
|
119
|
+
try:
|
|
120
|
+
asyncio.run(self._scanner_loop(stop_event))
|
|
121
|
+
except Exception:
|
|
122
|
+
self.log.exception("scanner loop failed")
|
|
123
|
+
|
|
124
|
+
async def _scanner_loop(self, stop_event):
|
|
125
|
+
async with BleakScanner(detection_callback=self._detection_callback):
|
|
126
|
+
while not stop_event.is_set():
|
|
127
|
+
self._maybe_log_latest_measurement()
|
|
128
|
+
await asyncio.sleep(0.2)
|
|
129
|
+
|
|
130
|
+
def _detection_callback(self, device: BLEDevice, advertisement_data: AdvertisementData):
|
|
131
|
+
if getattr(device, "address", None) is None:
|
|
132
|
+
return
|
|
133
|
+
if normalize_address(device.address) != self.address:
|
|
134
|
+
return
|
|
135
|
+
|
|
136
|
+
manufacturer_data = advertisement_data.manufacturer_data.get(H5075_MANUFACTURER_ID)
|
|
137
|
+
if manufacturer_data is None:
|
|
138
|
+
return
|
|
139
|
+
|
|
140
|
+
measurement = decode_h5075_measurement(manufacturer_data, timestamp=datetime.now())
|
|
141
|
+
seen_at = time.monotonic()
|
|
142
|
+
self._record_measurement(measurement, seen_at)
|
|
143
|
+
self._maybe_log_latest_measurement(now_monotonic=seen_at)
|
|
144
|
+
|
|
145
|
+
def _record_measurement(self, measurement: H5075Measurement, seen_at: Optional[float] = None):
|
|
146
|
+
if seen_at is None:
|
|
147
|
+
seen_at = time.monotonic()
|
|
148
|
+
|
|
149
|
+
with self._state_lock:
|
|
150
|
+
self.measurement = measurement
|
|
151
|
+
self._latest_measurement = measurement
|
|
152
|
+
self._latest_measurement_seen_at = seen_at
|
|
153
|
+
|
|
154
|
+
def _maybe_log_latest_measurement(self, now_monotonic: Optional[float] = None):
|
|
155
|
+
if now_monotonic is None:
|
|
156
|
+
now_monotonic = time.monotonic()
|
|
157
|
+
|
|
158
|
+
with self._state_lock:
|
|
159
|
+
if self._latest_measurement is None or self._latest_measurement_seen_at is None:
|
|
160
|
+
return
|
|
161
|
+
|
|
162
|
+
if self._latest_measurement_seen_at == self._last_logged_measurement_seen_at:
|
|
163
|
+
return
|
|
164
|
+
|
|
165
|
+
if self._last_emit_monotonic is not None and now_monotonic - self._last_emit_monotonic < self.interval:
|
|
166
|
+
return
|
|
167
|
+
|
|
168
|
+
line = format_measurement_log_line(self._latest_measurement)
|
|
169
|
+
self.data = line
|
|
170
|
+
self._last_logged_measurement_seen_at = self._latest_measurement_seen_at
|
|
171
|
+
self._last_emit_monotonic = now_monotonic
|
|
172
|
+
|
|
173
|
+
self._emit_info(line)
|
|
174
|
+
|
|
175
|
+
def _write_to_console(self, message: str):
|
|
176
|
+
try:
|
|
177
|
+
print(message, flush=True)
|
|
178
|
+
except OSError:
|
|
179
|
+
# Hidden/disowned Windows service processes can have an invalid stdout.
|
|
180
|
+
# Logging should continue even when there is no usable console.
|
|
181
|
+
pass
|
|
182
|
+
|
|
183
|
+
def _emit_info(self, message: str):
|
|
184
|
+
self.log.info(message)
|
|
185
|
+
self._write_to_console(message)
|
|
186
|
+
|
|
187
|
+
def _emit_warning(self, message: str):
|
|
188
|
+
self.log.warning(message)
|
|
189
|
+
self._write_to_console(message)
|
|
190
|
+
|
|
191
|
+
def finish(self, stop_service=False):
|
|
192
|
+
self._emit_warning("stopping")
|
|
193
|
+
if hasattr(self, "_thread_stopper"):
|
|
194
|
+
self._thread_stopper.set()
|
|
195
|
+
if hasattr(self, "_thread_timer"):
|
|
196
|
+
self._thread_timer.cancel()
|
|
197
|
+
if hasattr(self, "_scanner_thread") and self._scanner_thread.is_alive() and threading.current_thread() is not self._scanner_thread:
|
|
198
|
+
self._scanner_thread.join(timeout=5)
|
|
199
|
+
|
|
200
|
+
self._emit_warning(" stopped ")
|
|
201
|
+
self._flush_loggers()
|
|
202
|
+
if stop_service:
|
|
203
|
+
time.sleep(2)
|
|
204
|
+
self.service_stop()
|
|
205
|
+
|
|
206
|
+
def disp(self):
|
|
207
|
+
pass
|
|
208
|
+
|
|
209
|
+
def is_busy(self):
|
|
210
|
+
return hasattr(self, "_scanner_thread") and self._scanner_thread.is_alive()
|
|
211
|
+
|
|
212
|
+
def test(self):
|
|
213
|
+
return bleak_import_error is None
|
|
214
|
+
|
|
215
|
+
def cleanup(self):
|
|
216
|
+
self.finish()
|
|
217
|
+
return True
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def cli(serializer: str = "default", port: Optional[str] = None):
|
|
221
|
+
if port is None:
|
|
222
|
+
port = GOV.SERVICE_PORT
|
|
223
|
+
s = GOV(serializer=serializer)
|
|
224
|
+
s.bind(f"tcp://0.0.0.0:{port}")
|
|
225
|
+
print("running GOVZeroService")
|
|
226
|
+
s.run()
|
|
227
|
+
print("done")
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
if __name__ == "__main__":
|
|
231
|
+
defopt.run(cli)
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
from .ZeroService import BaseZeroService
|
|
2
|
+
import time
|
|
3
|
+
import sys
|
|
4
|
+
import numpy as np
|
|
5
|
+
from .utils.log_exceptions import for_all_methods, log_exceptions
|
|
6
|
+
import logging
|
|
7
|
+
import threading
|
|
8
|
+
|
|
9
|
+
try:
|
|
10
|
+
import PyDAQmx as daq
|
|
11
|
+
pydaqmx_import_error = None
|
|
12
|
+
except (ImportError, NotImplementedError) as pydaqmx_import_error:
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@for_all_methods(log_exceptions(logging.getLogger(__name__)))
|
|
17
|
+
class NIC(BaseZeroService):
|
|
18
|
+
"""[summary]"""
|
|
19
|
+
|
|
20
|
+
LOGGING_PORT = 1450 # set this to range 1420-1460
|
|
21
|
+
SERVICE_PORT = 4250 # last to digits match logging output_channels - but start with "42" instead of "14"
|
|
22
|
+
SERVICE_NAME = "NIC" # short, uppercase, 3-letter ID of the service (equals class name)
|
|
23
|
+
|
|
24
|
+
def setup(self, output_channel, duration, frequency, duty_cycle, params):
|
|
25
|
+
"""Setup the counter service (intiates the digital output channels).
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
duration (float): Unused - kept to keep the interface same across services [description]
|
|
29
|
+
output_channels (str): , e.g. "/dev1/port0/line0:1"
|
|
30
|
+
"""
|
|
31
|
+
if pydaqmx_import_error is not None:
|
|
32
|
+
raise pydaqmx_import_error
|
|
33
|
+
|
|
34
|
+
self._time_started = None
|
|
35
|
+
self.duration = float(duration)
|
|
36
|
+
|
|
37
|
+
self.task = daq.Task()
|
|
38
|
+
self.task.CreateCOPulseChanFreq(output_channel, "", daq.DAQmx_Val_Hz, daq.DAQmx_Val_Low, 0.0, frequency, duty_cycle)
|
|
39
|
+
self.task.CfgImplicitTiming(daq.DAQmx_Val_ContSamps, 1000)
|
|
40
|
+
|
|
41
|
+
if self.duration > 0: # if zero, will stop when nothing is to be outputted
|
|
42
|
+
self._thread_timer = threading.Timer(self.duration, self.finish, kwargs={'stop_service': True})
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def start(self):
|
|
46
|
+
self.task.StartTask()
|
|
47
|
+
self.log.warning('Started')
|
|
48
|
+
self._time_started = time.time()
|
|
49
|
+
if hasattr(self, '_thread_timer'):
|
|
50
|
+
self.log.warning(f'duration {self.duration} seconds')
|
|
51
|
+
self._thread_timer.start()
|
|
52
|
+
self.log.warning('finish timer started')
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def stop(self):
|
|
56
|
+
self.task.StopTask()
|
|
57
|
+
self.log.warning('Stopped')
|
|
58
|
+
|
|
59
|
+
def finish(self, stop_service=False):
|
|
60
|
+
self.log.warning("stopping")
|
|
61
|
+
self.stop()
|
|
62
|
+
time.sleep(0.2)
|
|
63
|
+
self.task.ClearTask()
|
|
64
|
+
self.cleanup()
|
|
65
|
+
# clean up code here
|
|
66
|
+
self.log.warning(" stopped ")
|
|
67
|
+
# mode log file and savefilename
|
|
68
|
+
if stop_service:
|
|
69
|
+
time.sleep(.2)
|
|
70
|
+
self.service_stop()
|
|
71
|
+
return True
|
|
72
|
+
|
|
73
|
+
def is_busy(self):
|
|
74
|
+
return self._time_started is not None
|
|
75
|
+
|
|
76
|
+
def test(self):
|
|
77
|
+
return True
|
|
78
|
+
|
|
79
|
+
def cleanup(self):
|
|
80
|
+
# self.finish()
|
|
81
|
+
# your code here
|
|
82
|
+
return True
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
if __name__ == "__main__":
|
|
86
|
+
if len(sys.argv) > 1:
|
|
87
|
+
ser = sys.argv[1]
|
|
88
|
+
else:
|
|
89
|
+
ser = "default"
|
|
90
|
+
s = NIC(serializer=ser)
|
|
91
|
+
s.bind("tcp://0.0.0.0:{0}".format(NIC.SERVICE_PORT)) # broadcast on all IPs
|
|
92
|
+
s.run()
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
from .ZeroService import BaseZeroService
|
|
2
|
+
import time
|
|
3
|
+
import sys
|
|
4
|
+
import numpy as np
|
|
5
|
+
from .utils.log_exceptions import for_all_methods, log_exceptions
|
|
6
|
+
import logging
|
|
7
|
+
|
|
8
|
+
try:
|
|
9
|
+
import PyDAQmx as daq
|
|
10
|
+
pydaqmx_import_error = None
|
|
11
|
+
except ImportError as pydaqmx_import_error:
|
|
12
|
+
pass
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@for_all_methods(log_exceptions(logging.getLogger(__name__)))
|
|
16
|
+
class NIT(BaseZeroService):
|
|
17
|
+
"""[summary]"""
|
|
18
|
+
|
|
19
|
+
LOGGING_PORT = 1450 # set this to range 1420-1460
|
|
20
|
+
SERVICE_PORT = 4250 # last to digits match logging output_channels - but start with "42" instead of "14"
|
|
21
|
+
SERVICE_NAME = "NIT" # short, uppercase, 3-letter ID of the service (equals class name)
|
|
22
|
+
|
|
23
|
+
def setup(self, duration, output_channels):
|
|
24
|
+
"""Setup the trigger service (intiates the digital output channels).
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
duration (float): Unused - kept to keep the interface same across services [description]
|
|
28
|
+
output_channels (str): , e.g. "/dev1/port0/line0:1"
|
|
29
|
+
"""
|
|
30
|
+
if pydaqmx_import_error is not None:
|
|
31
|
+
raise pydaqmx_import_error
|
|
32
|
+
|
|
33
|
+
self._time_started = None
|
|
34
|
+
self.duration = float(duration)
|
|
35
|
+
|
|
36
|
+
self.task = daq.Task()
|
|
37
|
+
self.task.CreateDOChan(output_channels, "", daq.DAQmx_Val_ChanForAllLines)
|
|
38
|
+
self.nb_channels = None # TODO: get nb_channels from task obhect
|
|
39
|
+
self.task.StartTask()
|
|
40
|
+
|
|
41
|
+
# APPLICATION SPECIFIC SETUP CODE HERE
|
|
42
|
+
|
|
43
|
+
# background jobs should be run and controlled via a thread
|
|
44
|
+
# threads can be stopped by setting an event: `_thread_stopper.set()`
|
|
45
|
+
|
|
46
|
+
def send_trigger(self, state, duration=0.0001):
|
|
47
|
+
"""Set the digital output channels to specified state.
|
|
48
|
+
|
|
49
|
+
Args:
|
|
50
|
+
state ([type]): [description]
|
|
51
|
+
duration (float, optional): How long the trigger state should last (in seconds).
|
|
52
|
+
Will reset to all 0 after the duration.
|
|
53
|
+
If None will return immediatelly and keep the trigger state permanent.
|
|
54
|
+
Defaults to 0.0001.
|
|
55
|
+
"""
|
|
56
|
+
# if len(state) != self.nb_channels:
|
|
57
|
+
# raise ValueError(f"State vector should have same length as the number of digital output channels. Is {len(state)}, should be {'x'}.")
|
|
58
|
+
self.log.info("sending {state} on {output_channels}")
|
|
59
|
+
state = np.array(state, dtype=np.uint8)
|
|
60
|
+
self.task.WriteDigitalLines(1, 1, 10.0, daq.DAQmx_Val_GroupByChannel, state, None, None)
|
|
61
|
+
if duration is not None:
|
|
62
|
+
time.sleep(duration) # alternatively could return immediately using a threaded timer
|
|
63
|
+
self.task.WriteDigitalLines(1, 1, 10.0, daq.DAQmx_Val_GroupByChannel, 0 * state, None, None)
|
|
64
|
+
self.log.info(" success.")
|
|
65
|
+
|
|
66
|
+
def start(self):
|
|
67
|
+
pass
|
|
68
|
+
|
|
69
|
+
def finish(self, stop_service=False):
|
|
70
|
+
self.log.warning("stopping")
|
|
71
|
+
self.task.StopTask()
|
|
72
|
+
|
|
73
|
+
# clean up code here
|
|
74
|
+
self.log.warning(" stopped ")
|
|
75
|
+
# mode log file and savefilename
|
|
76
|
+
if stop_service:
|
|
77
|
+
time.sleep(2)
|
|
78
|
+
self.service_stop()
|
|
79
|
+
|
|
80
|
+
def is_busy(self):
|
|
81
|
+
return True # should return True/False
|
|
82
|
+
|
|
83
|
+
def test(self):
|
|
84
|
+
return True
|
|
85
|
+
|
|
86
|
+
def cleanup(self):
|
|
87
|
+
self.finish()
|
|
88
|
+
# your code here
|
|
89
|
+
return True
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def oneshot_trigger(this, trigger_names, do_port="/Dev1/port0/line1:3"):
|
|
93
|
+
|
|
94
|
+
trigger_types = {"START": [1, 0, 0], "STOP": [0, 1, 0], "NEXT": [0, 0, 1], "NULL": [0, 0, 0]}
|
|
95
|
+
try:
|
|
96
|
+
nit = NIT.make(
|
|
97
|
+
this["serializer"],
|
|
98
|
+
this["host"],
|
|
99
|
+
this["python_exe"],
|
|
100
|
+
new_console=False,
|
|
101
|
+
)
|
|
102
|
+
nit.setup(-1, do_port)
|
|
103
|
+
for trigger_name in trigger_names:
|
|
104
|
+
logging.info(f"sending {trigger_name}")
|
|
105
|
+
nit.send_trigger(trigger_types[trigger_name])
|
|
106
|
+
except Exception as e:
|
|
107
|
+
logging.exception(e)
|
|
108
|
+
nit.finish()
|
|
109
|
+
nit.stop_server()
|
|
110
|
+
del nit
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
if __name__ == "__main__":
|
|
114
|
+
if len(sys.argv) > 1:
|
|
115
|
+
ser = sys.argv[1]
|
|
116
|
+
else:
|
|
117
|
+
ser = "default"
|
|
118
|
+
s = NIT(serializer=ser)
|
|
119
|
+
s.bind("tcp://0.0.0.0:{0}".format(NIT.SERVICE_PORT)) # broadcast on all IPs
|
|
120
|
+
s.run()
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# required imports
|
|
3
|
+
from .ZeroService import BaseZeroService # import super class
|
|
4
|
+
import time
|
|
5
|
+
import threading
|
|
6
|
+
from .utils.log_exceptions import for_all_methods, log_exceptions
|
|
7
|
+
import logging
|
|
8
|
+
import defopt
|
|
9
|
+
from typing import Optional
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# decorate all methods in the class so that exceptions are properly logged
|
|
13
|
+
@for_all_methods(log_exceptions(logging.getLogger(__name__)))
|
|
14
|
+
class TMP(BaseZeroService):
|
|
15
|
+
LOGGING_PORT = 1443 # set this to range 1420-1460
|
|
16
|
+
SERVICE_PORT = 4243 # last two digits match logging port - but start with "42" instead of "14"
|
|
17
|
+
SERVICE_NAME = "TMP" # short, uppercase, 3-letter ID of the service (must equal class name)
|
|
18
|
+
|
|
19
|
+
def setup(self, duration):
|
|
20
|
+
self._time_started = None
|
|
21
|
+
self.duration = float(duration)
|
|
22
|
+
|
|
23
|
+
# APPLICATION SPECIFIC SETUP CODE HERE
|
|
24
|
+
|
|
25
|
+
# background jobs should be run and controlled via a thread
|
|
26
|
+
# threads can be stopped by setting an event: `_thread_stopper.set()`
|
|
27
|
+
self._thread_stopper = threading.Event()
|
|
28
|
+
# and/or via a timer
|
|
29
|
+
if self.duration > 0:
|
|
30
|
+
self._thread_timer = threading.Timer(self.duration, self.finish, kwargs={"stop_service": True})
|
|
31
|
+
#
|
|
32
|
+
self._worker_thread = threading.Thread(target=self._worker, args=(self._thread_stopper,))
|
|
33
|
+
|
|
34
|
+
def start(self):
|
|
35
|
+
self._time_started = time.time()
|
|
36
|
+
|
|
37
|
+
# background jobs should be run and controlled via a thread
|
|
38
|
+
self._worker_thread.start()
|
|
39
|
+
self.log.info("started")
|
|
40
|
+
if hasattr(self, "_thread_timer"):
|
|
41
|
+
self.log.info("duration {0} seconds".format(self.duration))
|
|
42
|
+
self._thread_timer.start()
|
|
43
|
+
self.log.info("finish timer started")
|
|
44
|
+
|
|
45
|
+
def _worker(self, stop_event):
|
|
46
|
+
RUN = True
|
|
47
|
+
while RUN:
|
|
48
|
+
if stop_event.is_set():
|
|
49
|
+
RUN = False
|
|
50
|
+
# APPLICATION SPECIFIC RUN CODE HERE
|
|
51
|
+
|
|
52
|
+
def finish(self, stop_service=False):
|
|
53
|
+
self.log.warning("stopping")
|
|
54
|
+
# stop thread if necessary
|
|
55
|
+
if hasattr(self, "_thread_stopper"):
|
|
56
|
+
self._thread_stopper.set()
|
|
57
|
+
if hasattr(self, "_thread_timer"):
|
|
58
|
+
self._thread_timer.cancel()
|
|
59
|
+
# clean up code here
|
|
60
|
+
self.log.warning(" stopped ")
|
|
61
|
+
# mode log file and savefilename
|
|
62
|
+
if stop_service:
|
|
63
|
+
time.sleep(2)
|
|
64
|
+
self.service_stop()
|
|
65
|
+
|
|
66
|
+
def disp(self):
|
|
67
|
+
pass
|
|
68
|
+
|
|
69
|
+
def is_busy(self):
|
|
70
|
+
return True # should return True/False
|
|
71
|
+
|
|
72
|
+
def test(self):
|
|
73
|
+
return True
|
|
74
|
+
|
|
75
|
+
def cleanup(self):
|
|
76
|
+
self.finish()
|
|
77
|
+
# your code here
|
|
78
|
+
return True
|
|
79
|
+
|
|
80
|
+
def info(self):
|
|
81
|
+
if self.is_busy():
|
|
82
|
+
pass # your code here
|
|
83
|
+
else:
|
|
84
|
+
return None
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def cli(serializer: str = "default", port: Optional[str] = None):
|
|
88
|
+
if port is None:
|
|
89
|
+
port = TMP.SERVICE_PORT
|
|
90
|
+
s = TMP(serializer=serializer)
|
|
91
|
+
s.bind("tcp://0.0.0.0:{0}".format(port)) # broadcast on all IPs
|
|
92
|
+
print("running TMPZeroService")
|
|
93
|
+
s.run()
|
|
94
|
+
print("done")
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
if __name__ == "__main__":
|
|
98
|
+
defopt.run(cli)
|