-
Notifications
You must be signed in to change notification settings - Fork 0
/
oocsi.py
726 lines (613 loc) · 26.7 KB
/
oocsi.py
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
# Copyright (c) 2025 Mathias Funk
# This software is released under the MIT License.
# http://opensource.org/licenses/mit-license.php
import wifi
import socketpool
import time
import json
import random
__author__ = 'matsfunk'
class OOCSI:
"""
OOCSI class for managing communication with an OOCSI server.
"""
def __init__(self, handle=None, host='localhost', port=4444, callback=None):
"""
Initializes the OOCSI client and connects to the server.
Args:
handle (str): Unique identifier for the client. Defaults to None.
host (str): Server hostname. Defaults to 'localhost'.
port (int): Server port. Defaults to 4444.
callback (function): Callback function to handle received messages. Defaults to None.
"""
if handle is None or len(handle.strip()) == 0:
handle = "OOCSIClient_####"
while "#" in handle:
handle = handle.replace("#", str(random.randrange(10)), 1)
self.handle = handle
self.receivers = {self.handle: [callback]}
self.calls = {}
self.services = {}
self.reconnect = True
self.connected = False
# Connect the socket to the port where the server is listening
self.server_address = (host, port)
self.log('connecting to %s port %s' % self.server_address)
self.init()
# Block till we are connected
while not self.connected:
time.sleep(0.2)
def init(self):
"""
Initializes the socket connection to the OOCSI server.
"""
try:
# Create a socket pool
pool = socketpool.SocketPool(wifi.radio)
# Create a TCP/IP socket
self.sock = pool.socket(type=pool.SOCK_STREAM, proto=pool.IPPROTO_TCP)
# Connect to the server
self.sock.connect(self.server_address)
try:
# Send initial data to establish connection
message = self.handle + '(JSON)'
self.internalSend(message)
try:
# Receive the server response
buffer = bytearray(1024)
received_bytes = self.sock.recv_into(buffer)
data = buffer[:received_bytes].decode('utf-8')
except:
pass
if data.startswith('{'):
self.log('connection established')
# Re-subscribe to channels
for channelName in self.receivers:
self.internalSend('subscribe {0}'.format(channelName))
self.connected = True
self.sock.setblocking(False)
self.sock.settimeout(0)
elif data.startswith('error'):
self.log(data)
self.reconnect = False
finally:
pass
except:
pass
def log(self, message):
"""
Logs a message with the client's handle.
Args:
message (str): Message to log.
"""
print('[{0}]: {1}'.format(self.handle, message))
def internalSend(self, msg):
"""
Sends a message to the server.
Args:
msg (str): Message to send.
"""
try:
self.sock.sendall((msg + '\n').encode())
except:
self.connected = False
def check(self):
"""
Checks for incoming messages from the server and processes them.
"""
try:
buffer = bytearray(1024)
received_bytes = self.sock.recv_into(buffer)
data = buffer[:received_bytes].decode('utf-8')
lines = data.split("\n")
for line in lines:
if len(data) == 0:
self.sock.close()
self.connected = False
elif line.startswith('ping') or line.startswith('.'):
self.internalSend('.')
elif line.startswith('{'):
self.receive(json.loads(line))
except:
pass
def receive(self, event):
"""
Processes a received event message.
Args:
event (dict): Event data received from the server.
"""
sender = event['sender']
recipient = event['recipient']
# Clean up the event data
del event['recipient']
del event['sender']
del event['timestamp']
if 'data' in event:
del event['data']
if '_MESSAGE_HANDLE' in event and event['_MESSAGE_HANDLE'] in self.services:
service = self.services[event['_MESSAGE_HANDLE']]
del event['_MESSAGE_HANDLE']
service(event)
self.send(sender, event)
self.receiveChannelEvent(sender, recipient, event)
else:
if '_MESSAGE_ID' in event:
myCall = self.calls[event['_MESSAGE_ID']]
if myCall['expiration'] > time.time():
response = self.calls[event['_MESSAGE_ID']]
response['response'] = event
del response['expiration']
del response['_MESSAGE_ID']
del response['response']['_MESSAGE_ID']
else:
del self.calls[event['_MESSAGE_ID']]
else:
self.receiveChannelEvent(sender, recipient, event)
def receiveChannelEvent(self, sender, recipient, event):
"""
Handles an event received on a specific channel by invoking the registered callbacks.
Args:
sender (str): Sender of the message.
recipient (str): Recipient channel.
event (dict): Event data.
"""
if recipient in self.receivers and self.receivers[recipient] is not None:
for x in self.receivers[recipient]:
x(sender, recipient, event)
def send(self, channelName, data):
"""
Sends a message to a specific channel.
Args:
channelName (str): Name of the channel to send the message to.
data (dict): Data to send.
"""
self.internalSend('sendraw {0} {1}'.format(channelName, json.dumps(data)))
def call(self, channelName, callName, data, timeout=1):
"""
Sends a call message to a specific channel and waits for a response.
Args:
channelName (str): Name of the channel to send the call to.
callName (str): Name of the call.
data (dict): Data to send.
timeout (int): Timeout for the call in seconds. Defaults to 1.
Returns:
dict: Response data.
"""
data['_MESSAGE_HANDLE'] = callName
data['_MESSAGE_ID'] = self.uuid4()
self.calls[data['_MESSAGE_ID']] = {
'_MESSAGE_HANDLE': callName,
'_MESSAGE_ID': data['_MESSAGE_ID'],
'expiration': time.time() + timeout
}
self.send(channelName, data)
return self.calls[data['_MESSAGE_ID']]
def callAndWait(self, channelName, callName, data, timeout=1):
"""
Sends a call message to a specific channel and waits for a response within the specified timeout.
Args:
channelName (str): Name of the channel to send the call to.
callName (str): Name of the call.
data (dict): Data to send.
timeout (int): Timeout for the call in seconds. Defaults to 1.
Returns:
dict: Response data.
"""
call = self.call(channelName, callName, data, timeout)
expiration = time.time() + timeout
while time.time() < expiration:
time.sleep(0.1)
if 'response' in call:
break
return call
def register(self, channelName, callName, callback):
"""
Registers a callback for a specific call on a channel.
Args:
channelName (str): Name of the channel.
callName (str): Name of the call.
callback (function): Callback function to handle the call.
"""
self.services[callName] = callback
self.internalSend('subscribe {0}'.format(channelName))
self.log('registered responder on {0} for {1}'.format(channelName, callName))
def subscribe(self, channelName, f):
"""
Subscribes to a channel with a callback function.
Args:
channelName (str): Name of the channel to subscribe to.
f (function): Callback function to handle received messages.
"""
if channelName in self.receivers:
self.receivers[channelName].append(f)
else:
self.receivers[channelName] = [f]
self.internalSend('subscribe {0}'.format(channelName))
self.log('subscribed to {0}'.format(channelName))
def unsubscribe(self, channelName):
"""
Unsubscribes from a channel.
Args:
channelName (str): Name of the channel to unsubscribe from.
"""
del self.receivers[channelName]
self.internalSend('unsubscribe {0}'.format(channelName))
self.log('unsubscribed from {0}'.format(channelName))
def variable(self, channelName, key):
"""
Creates an OOCSIVariable for a specific channel and key.
Args:
channelName (str): Name of the channel.
key (str): Key for the variable.
Returns:
OOCSIVariable: The created OOCSIVariable instance.
"""
return OOCSIVariable(self, channelName, key)
def stop(self):
"""
Stops the OOCSI client and disconnects from the server.
"""
self.reconnect = False
self.internalSend('quit')
self.sock.close()
self.connected = False
def handleEvent(self, sender, receiver, message):
"""
Placeholder method for handling events. Can be overridden by subclasses.
Args:
sender (str): Sender of the message.
receiver (str): Receiver of the message.
message (dict): Message data.
"""
{}
@staticmethod
def uuid4():
"""
Generates a random UUID compliant with RFC 4122.
Returns:
str: Generated UUID.
"""
random_bytes = bytearray(random.getrandbits(8) for _ in range(16))
random_bytes[6] = (random_bytes[6] & 0x0F) | 0x40 # Set the version to 0100
random_bytes[8] = (random_bytes[8] & 0x3F) | 0x80 # Set the variant to 10
# Convert bytes to hex string without using ubinascii
hex_string = ''.join(f'{byte:02x}' for byte in random_bytes)
# Format the UUID
return '-'.join((hex_string[0:8], hex_string[8:12], hex_string[12:16], hex_string[16:20], hex_string[20:32]))
def returnHandle(self):
"""
Returns the handle of the OOCSI client.
Returns:
str: Handle of the client.
"""
return self.handle
def heyOOCSI(self, custom_name=None):
"""
Creates an OOCSIDevice with the client's handle or a custom name.
Args:
custom_name (str): Custom name for the OOCSIDevice. Defaults to None.
Returns:
OOCSIDevice: The created OOCSIDevice instance.
"""
if custom_name is None:
return OOCSIDevice(self, self.handle)
else:
return OOCSIDevice(self, custom_name)
class OOCSICall:
"""
OOCSICall class represents a call made to an OOCSI server, containing metadata about the call.
"""
def __init__(self, parent=None):
"""
Initializes an OOCSICall instance.
Args:
parent (OOCSI): Parent OOCSI instance used to generate the UUID. Defaults to None.
"""
self.uuid = parent.uuid4() # Generate a unique identifier for the call using the parent's UUID generation method.
self.expiration = time.time() # Set the expiration timestamp to the current time.
class OOCSIVariable:
"""
OOCSIVariable class represents a variable in an OOCSI channel, allowing for subscribing to updates and setting values.
"""
def __init__(self, oocsi, channelName, key):
"""
Initializes an OOCSIVariable instance and subscribes to the channel for updates.
Args:
oocsi (OOCSI): Reference to the OOCSI instance.
channelName (str): Name of the channel.
key (str): Key associated with the variable.
"""
self.key = key
self.channel = channelName
oocsi.subscribe(channelName, self.internalReceiveValue)
self.oocsi = oocsi
self.value = None
self.windowLength = 0
self.values = []
self.minvalue = None
self.maxvalue = None
self.sigma = None
def get(self):
"""
Retrieves the current value of the variable, applying smoothing if applicable.
Returns:
any: The current value or the smoothed value.
"""
self.oocsi.check()
if self.windowLength > 0 and len(self.values) > 0:
return sum(self.values) / float(len(self.values))
else:
return self.value
def set(self, value):
"""
Sets the value of the variable, applying constraints and smoothing if applicable, and sends the value to the channel.
Args:
value (any): The value to set.
"""
tempvalue = value
if self.minvalue is not None and tempvalue < self.minvalue:
tempvalue = self.minvalue
elif self.maxvalue is not None and tempvalue > self.maxvalue:
tempvalue = self.maxvalue
elif self.sigma is not None:
mean = self.get()
if mean is not None:
if abs(mean - tempvalue) > self.sigma:
if mean - tempvalue > 0:
tempvalue = mean - self.sigma / float(len(self.values))
else:
tempvalue = mean + self.sigma / float(len(self.values))
if self.windowLength > 0:
self.values.append(tempvalue)
self.values = self.values[-self.windowLength:]
else:
self.value = tempvalue
self.oocsi.send(self.channel, {self.key: value})
def internalReceiveValue(self, sender, recipient, data):
"""
Internal method to handle received values from the channel.
Args:
sender (str): Sender of the message.
recipient (str): Recipient of the message.
data (dict): Data received from the channel.
"""
if self.key in data:
tempvalue = data[self.key]
if self.minvalue is not None and tempvalue < self.minvalue:
tempvalue = self.minvalue
elif self.maxvalue is not None and tempvalue > self.maxvalue:
tempvalue = self.maxvalue
elif self.sigma is not None:
mean = self.get()
if mean is not None:
if abs(mean - tempvalue) > self.sigma:
if mean - tempvalue > 0:
tempvalue = mean - self.sigma / float(len(self.values))
else:
tempvalue = mean + self.sigma / float(len(self.values))
if self.windowLength > 0:
self.values.append(tempvalue)
self.values = self.values[-self.windowLength:]
else:
self.value = tempvalue
def min(self, minvalue):
"""
Sets the minimum value constraint for the variable.
Args:
minvalue (any): Minimum value.
Returns:
OOCSIVariable: The current instance for chaining.
"""
self.minvalue = minvalue
if self.value < self.minvalue:
self.value = self.minvalue
return self
def max(self, maxvalue):
"""
Sets the maximum value constraint for the variable.
Args:
maxvalue (any): Maximum value.
Returns:
OOCSIVariable: The current instance for chaining.
"""
self.maxvalue = maxvalue
if self.value > self.maxvalue:
self.value = self.maxvalue
return self
def smooth(self, windowLength, sigma=None):
"""
Configures smoothing for the variable by setting the window length and optional sigma value.
Args:
windowLength (int): Length of the window for smoothing.
sigma (float): Allowed deviation for smoothing. Defaults to None.
Returns:
OOCSIVariable: The current instance for chaining.
"""
self.windowLength = windowLength
self.sigma = sigma
return self
class OOCSIDevice:
"""
OOCSIDevice class represents a device in the OOCSI network, allowing for managing properties, locations, and components of the device.
"""
def __init__(self, OOCSI, device_name: str) -> None:
"""
Initializes an OOCSIDevice instance and logs its creation.
Args:
OOCSI (OOCSI): Reference to the OOCSI instance.
device_name (str): Name of the device.
"""
self._device_name = device_name
self._device = {self._device_name: {}}
self._device[self._device_name]["properties"] = {}
self._device[self._device_name]["properties"]["device_id"] = OOCSI.returnHandle()
self._device[self._device_name]["components"] = {}
self._device[self._device_name]["location"] = {}
self._components = self._device[self._device_name]["components"]
self._oocsi = OOCSI
self._oocsi.log(f'Created device {self._device_name}.')
def addProperty(self, properties: str, propertyValue):
"""
Adds a property to the device and logs the addition.
Args:
properties (str): Name of the property.
propertyValue (any): Value of the property.
Returns:
OOCSIDevice: The current instance for chaining.
"""
self._device[self._device_name]["properties"][properties] = propertyValue
self._oocsi.log(f'Added {properties} to the properties list of device {self._device_name}.')
return self
def addLocation(self, location_name: str, latitude: float = 0, longitude: float = 0):
"""
Adds a location to the device and logs the addition.
Args:
location_name (str): Name of the location.
latitude (float): Latitude of the location. Defaults to 0.
longitude (float): Longitude of the location. Defaults to 0.
Returns:
OOCSIDevice: The current instance for chaining.
"""
self._device[self._device_name]["location"][location_name] = [latitude, longitude]
self._oocsi.log(f'Added {location_name} to the locations list of device {self._device_name}.')
return self
def addSensor(self, sensor_name: str, sensor_channel: str, sensor_type: str, sensor_unit: str, sensor_default: float, mode: str = "auto", step: float = None, icon: str = None):
"""
Adds a sensor component to the device and logs the addition.
Args:
sensor_name (str): Name of the sensor.
sensor_channel (str): Channel associated with the sensor.
sensor_type (str): Type of the sensor.
sensor_unit (str): Unit of the sensor value.
sensor_default (float): Default value of the sensor.
mode (str): Mode of the sensor. Defaults to "auto".
step (float): Step value for the sensor. Defaults to None.
icon (str): Icon representing the sensor. Defaults to None.
Returns:
OOCSIDevice: The current instance for chaining.
"""
self._components[sensor_name] = {}
self._components[sensor_name]["channel_name"] = sensor_channel
self._components[sensor_name]["type"] = "sensor"
self._components[sensor_name]["sensor_type"] = sensor_type
self._components[sensor_name]["unit"] = sensor_unit
self._components[sensor_name]["value"] = sensor_default
self._components[sensor_name]["mode"] = mode
self._components[sensor_name]["step"] = step
self._components[sensor_name]["icon"] = icon
self._device[self._device_name]["components"][sensor_name] = self._components[sensor_name]
self._oocsi.log(f'Added {sensor_name} to the components list of device {self._device_name}.')
return self
def addNumber(self, number_name: str, number_channel: str, number_min_max, number_unit: str, number_default: float, icon: str = None):
"""
Adds a number component to the device and logs the addition.
Args:
number_name (str): Name of the number component.
number_channel (str): Channel associated with the number component.
number_min_max (tuple): Minimum and maximum values for the number component.
number_unit (str): Unit of the number value.
number_default (float): Default value of the number component.
icon (str): Icon representing the number component. Defaults to None.
Returns:
OOCSIDevice: The current instance for chaining.
"""
self._components[number_name] = {}
self._components[number_name]["channel_name"] = number_channel
self._components[number_name]["min_max"] = number_min_max
self._components[number_name]["type"] = "number"
self._components[number_name]["unit"] = number_unit
self._components[number_name]["value"] = number_default
self._components[number_name]["icon"] = icon
self._device[self._device_name]["components"][number_name] = self._components[number_name]
self._oocsi.log(f'Added {number_name} to the components list of device {self._device_name}.')
return self
def addBinarySensor(self, sensor_name: str, sensor_channel: str, sensor_type: str, sensor_default: bool = False, icon: str = None):
"""
Adds a binary sensor component to the device and logs the addition.
Args:
sensor_name (str): Name of the binary sensor.
sensor_channel (str): Channel associated with the binary sensor.
sensor_type (str): Type of the binary sensor.
sensor_default (bool): Default state of the binary sensor. Defaults to False.
icon (str): Icon representing the binary sensor. Defaults to None.
Returns:
OOCSIDevice: The current instance for chaining.
"""
self._components[sensor_name] = {}
self._components[sensor_name]["channel_name"] = sensor_channel
self._components[sensor_name]["type"] = "binary_sensor"
self._components[sensor_name]["sensor_type"] = sensor_type
self._components[sensor_name]["state"] = sensor_default
self._components[sensor_name]["icon"] = icon
self._device[self._device_name]["components"][sensor_name] = self._components[sensor_name]
self._oocsi.log(f'Added {sensor_name} to the components list of device {self._device_name}.')
return self
def addSwitch(self, switch_name: str, switch_channel: str, switch_default: bool = False, icon: str = None):
"""
Adds a switch component to the device and logs the addition.
Args:
switch_name (str): Name of the switch.
switch_channel (str): Channel associated with the switch.
switch_default (bool): Default state of the switch. Defaults to False.
icon (str): Icon representing the switch. Defaults to None.
Returns:
OOCSIDevice: The current instance for chaining.
"""
self._components[switch_name] = {}
self._components[switch_name]["channel_name"] = switch_channel
self._components[switch_name]["type"] = "switch"
self._components[switch_name]["state"] = switch_default
self._components[switch_name]["icon"] = icon
self._device[self._device_name]["components"][switch_name] = self._components[switch_name]
self._oocsi.log(f'Added {switch_name} to the components list of device {self._device_name}.')
return self
def addLight(self, light_name: str, light_channel: str, led_type: str, spectrum, light_default_state: bool = False, light_default_brightness: int = 0, mired_min_max = None, icon: str = None):
"""
Adds a light component to the device and logs the addition.
Args:
light_name (str): Name of the light.
light_channel (str): Channel associated with the light.
led_type (str): Type of LED.
spectrum (str): Spectrum of the light.
light_default_state (bool): Default state of the light. Defaults to False.
light_default_brightness (int): Default brightness of the light. Defaults to 0.
mired_min_max (tuple): Minimum and maximum mired values. Defaults to None.
icon (str): Icon representing the light. Defaults to None.
Returns:
OOCSIDevice: The current instance for chaining.
"""
SPECTRUM = ["WHITE", "CCT", "RGB"]
LEDTYPE = ["RGB", "RGBW", "RGBWW", "CCT", "DIMMABLE", "ONOFF"]
self._components[light_name] = {}
if led_type in LEDTYPE:
if spectrum in SPECTRUM:
self._components[light_name]["spectrum"] = spectrum
else:
self._oocsi.log(f'error, {light_name} spectrum does not exist.')
pass
else:
self._oocsi.log(f'error, {light_name} ledtype does not exist.')
pass
self._components[light_name]["channel_name"] = light_channel
self._components[light_name]["type"] = "light"
self._components[light_name]["ledType"] = led_type
self._components[light_name]["spectrum"] = spectrum
self._components[light_name]["min_max"] = mired_min_max
self._components[light_name]["state"] = light_default_state
self._components[light_name]["brightness"] = light_default_brightness
self._components[light_name]["icon"] = icon
self._device[self._device_name]["components"][light_name] = self._components[light_name]
self._oocsi.log(f'Added {light_name} to the components list of device {self._device_name}.')
return self
def submit(self):
"""
Submits the device information to the OOCSI network by sending a message and logs the submission.
"""
data = self._device
self._oocsi.internalSend('sendraw {0} {1}'.format("heyOOCSI!", json.dumps(data)))
self._oocsi.log(f'Sent heyOOCSI! message for device {self._device_name}.')
def sayHi(self):
"""
Alias for submit method to send the device information to the OOCSI network.
"""
self.submit()