-
Notifications
You must be signed in to change notification settings - Fork 0
/
messaging.py
51 lines (33 loc) · 1.34 KB
/
messaging.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
import json
class SensorMeasurement:
def __init__(self, init_value):
self.value = init_value
def set_temperature(self, new_value):
self.value = new_value
def get_temperature(self):
return self.value
def to_json(self):
sensor_measurement_encoded = json.dumps(self.__dict__)
return sensor_measurement_encoded
@staticmethod
def json_decoder(json_sensor_measurement_dict):
return SensorMeasurement(json_sensor_measurement_dict['value'])
@staticmethod
def from_json(json_sensor_measurement_str: str):
json_sensor_measurement_dict = json.loads(json_sensor_measurement_str)
actuator_state = SensorMeasurement.json_decoder(json_sensor_measurement_dict)
return actuator_state
class ActuatorState:
def __init__(self, init_state):
self.state = init_state
def to_json(self):
actuator_state_encoded = json.dumps(self.__dict__)
return actuator_state_encoded
@staticmethod
def json_decoder(json_actuator_state_dict):
return ActuatorState(json_actuator_state_dict['state'])
@staticmethod
def from_json(json_actuator_state_str: str):
json_actuator_state_dict = json.loads(json_actuator_state_str)
actuator_state = ActuatorState.json_decoder(json_actuator_state_dict)
return actuator_state