forked from Botts-Innovative-Research/OSHConnect-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoshconnectapi.py
More file actions
306 lines (260 loc) · 10.4 KB
/
oshconnectapi.py
File metadata and controls
306 lines (260 loc) · 10.4 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# ==============================================================================
# Copyright (c) 2024. Botts Innovative Research, Inc.
# Date: 2024/5/15
# Author: Ian Patterson
# Contact email: ian@botts-inc.com
# ==============================================================================
import logging
import json
from uuid import UUID
from eventbus import EventHandler
from .csapi4py.default_api_helpers import APIHelper
from .datastore import DataStore
from .resource_datamodels import DatastreamResource
from .streamableresource import Node, System, SessionManager, Datastream, ControlStream
from .styling import Styling
from .timemanagement import TemporalModes, TimeManagement, TimePeriod
class OSHConnect:
_name: str = None
datastore: DataStore = None
styling: Styling = None
timestream: TimeManagement = None
_nodes: list[Node] = []
_systems: list[System] = []
_cs_api_builder: APIHelper = None
# _datasource_handler: DataStreamHandler = None
_datastreams: list[Datastream] = []
_controlstreams: list[ControlStream] = []
_datagroups: list = []
_tasks: list = []
_playback_mode: TemporalModes = TemporalModes.REAL_TIME
_session_manager: SessionManager = None
_event_bus: EventHandler = None
def __init__(self, name: str, **kwargs):
"""
:param name: name of the OSHConnect instance, in the event that
:param kwargs:
"""
self._name = name
logging.info(f"OSHConnect instance {name} created")
self._session_manager = SessionManager()
self._event_bus = EventHandler()
def get_name(self):
"""
Get the name of the OSHConnect instance.
:return:
"""
return self._name
def add_node(self, node: Node):
"""
Add a node to the OSHConnect instance.
:param node: Node object
:return:
"""
node.register_with_session_manager(self._session_manager)
self._nodes.append(node)
def remove_node(self, node_id: str):
"""
Remove a node from the OSHConnect instance.
:param node_id:
:return:
"""
# TODO: should disconnect datastreams and delete them and all systems at the same time.
# list of nodes in our node list that do not have the id of the node we want to remove
self._nodes = [node for node in self._nodes if
node.get_id() != node_id]
def save_config(self):
logging.info(f"Saving configuration for {self._name}")
data = {}
for node in self._nodes:
node_dict = node.serialize()
data.update({node.get_id(): node_dict})
# write to JSON file
file_path = f"{self._name}_config.json"
with open(file_path, 'w', encoding='utf-8') as f:
json.dump({"app_config": data}, f, ensure_ascii=False, indent=2)
@classmethod
def load_config(cls, file_name: str) -> 'OSHConnect':
"""Load configuration data from a JSON file and return the stored config dict.
Note: Despite the return type hint, this returns the configuration dictionary.
"""
with open(file_name, 'r', encoding='utf-8') as f:
obj = json.load(f)
return obj.get('app_config', obj)
def share_config(self, config: dict):
pass
def update_config(self, config: dict):
pass
def delete_config(self, config: dict):
pass
def configure_nodes(self, nodes: list):
pass
def filter_nodes(self, nodes: list):
pass
def task_system(self, task: dict):
pass
def select_temporal_mode(self, mode: str):
"""
Select the temporal mode for the system. Real-time, archive, batch, as well as synchronization settings.
:param mode:
:return:
"""
pass
def visualize_streams(self, streams: list):
pass
# Second Level Use Cases
def get_visualization_recommendations(self, streams: list):
pass
def discover_systems(self, nodes: list[str] = None):
"""
Discover systems from the nodes that have been added to the OSHConnect instance. They are associated with the
nodes that they are discovered from so access to them flows through there.
:param nodes:
:return:
"""
search_nodes = self._nodes
if nodes is not None:
search_nodes = [node for node in search_nodes if
node.get_id() in nodes]
for node in search_nodes:
res_systems = node.discover_systems()
self._systems.extend(res_systems)
def discover_datastreams(self):
for system in self._systems:
datastreams = system.discover_datastreams()
self._datastreams.extend(datastreams)
def discover_controlstreams(self, streams: list):
for system in self._systems:
controlstreams = system.discover_controlstreams()
self._controlstreams.extend(controlstreams)
def authenticate_user(self, user: dict):
pass
def synchronize_streams(self, systems: list):
pass
def set_playback_mode(self, mode: TemporalModes):
self._datasource_handler.set_playback_mode(mode)
def set_timeperiod(self, start_time: str, end_time: str):
"""
Sets the time range (TimePeriod) for the OSHConnect instance. This is used to bookend the playback of the
datastreams.
:param start_time: ISO8601 formatted string or one of (now or latest)
:param end_time: ISO8601 formatted string or one of (now or latest)
:return:
"""
tp = TimePeriod(start=start_time, end=end_time)
self.timestream = TimeManagement(time_range=tp)
# def get_message_list(self) -> list[MessageWrapper]:
# """
# Get the list of messages that have been received by the OSHConnect instance.
# :return: list of MessageWrapper objects
# """
# return self._datasource_handler.get_messages()
def _insert_system(self, system: System, target_node: Node):
"""
Create a system on the target node.
:param system: System object
:param target_node: Node object, must be within the OSHConnect instance
:return: the created system
"""
if target_node in self._nodes:
self.add_system_to_node(system, target_node, insert_resource=True)
return system
def add_datastream(self, datastream: DatastreamResource, system: str | System) -> str:
"""
Adds a datastream into the OSHConnect instance.
:param datastream: DataSource object
:param system: System object or system id
:return:
"""
sys_obj: System
if isinstance(system, str):
sys_obj = self.find_system(system)
if sys_obj is None:
raise ValueError(f"System with id {system} not found")
else:
sys_obj = system
sys_obj.add_insert_datastream(datastream)
self._datastreams.append(datastream)
def find_system(self, system_id: str) -> System | None:
"""
Find a system in the OSHConnect instance.
:param system_id:
:return: the found system or None if not found
"""
for system in self._systems:
if system.uid == system_id:
return system
return None
# System Management
def add_system_to_node(self, system: System, target_node: Node, insert_resource: bool = False):
"""
Add a system to the target node.
:param system: System object
:param target_node: Node object, must be within the OSHConnect instance
:param insert_resource: Whether to insert the system into the target node's server, default is False
:return:
"""
if target_node in self._nodes:
target_node.add_new_system(system)
if insert_resource:
system.insert_self()
self._systems.append(system)
return
def create_and_insert_system(self, system_opts: dict, target_node: Node):
"""
Create a system on the target node.
:param system_opts: System object parameters
:param target_node: Node object, must be within the OSHConnect instance
:return: the created system
"""
if target_node in self._nodes:
new_system = System(**system_opts)
self.add_system_to_node(new_system, target_node, insert_resource=True)
return new_system
def remove_system(self, system_id: str):
pass
# DataStream Helpers
def get_datastreams(self) -> list[Datastream]:
return self._datastreams
def get_datastream_ids(self) -> list[UUID]:
return [ds.get_internal_id() for ds in self._datastreams]
def connect_session_streams(self, session_id: str):
"""
Connects all datastreams that are associated with the given session ID.
:param session_id:
:return:
"""
self._session_manager.start_session_streams(session_id)
def get_resource_group(self, resource_ids: list[UUID]) -> tuple[list[System], list[Datastream]]:
"""
Get a group of resources by their IDs. Can be any mix of systems, datastreams, and controlstreams.
:param resource_ids: list of resource IDs (internal UUID)
"""
systems = [system for system in self._systems if system.get_internal_id() in resource_ids]
datastreams = [ds for ds in self._datastreams if ds.get_internal_id() in resource_ids]
return systems, datastreams
def initialize_resource_groups(self, resource_ids: list = None):
"""
Initializes the datastreams that are specified.
"""
systems, datastreams = self.get_resource_group(resource_ids)
if systems:
for system in systems:
system.initialize()
if datastreams:
for ds in datastreams:
ds.initialize()
def start_datastreams(self, dsid_list: list = None):
"""
Starts the datastreams that are specified.
"""
datastreams = self.get_resource_group(dsid_list)[1]
for ds in datastreams:
ds.start()
def start_systems(self, sysid_list: list = None):
"""
Starts the systems that are specified.
"""
systems = self.get_resource_group(sysid_list)[0]
for system in systems:
system.start()