-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctrl9001.py
38 lines (31 loc) · 936 Bytes
/
ctrl9001.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
from socket import create_connection
from json import dumps, JSONEncoder
class StateEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, State):
return {
"power" : obj.power,
"cycle_time" : obj.cycle_time,
"operating_ratios" : obj.operating_ratios
}
return JSONEncoder.default(self, obj)
class State:
def __init__(self, power=False, cycle_time=100,
operating_ratios=None):
self.power = power
self.cycle_time = cycle_time
if operating_ratios is None:
self.operating_ratios = [1.0, 1.0]
else:
self.operating_ratios = operating_ratios
class Pigeon:
def __init__(self, address=("pigeon9001.local", 1631)):
self.sock = create_connection(address)
def push(self, state):
if type(state) is State:
self.sock.send(
dumps(state, cls=StateEncoder).encode() + b"\n")
else:
raise TypeError(
"expected type '%s', got '%s'" % (
State.__name__, type(state).__name__))