-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmodels.py
125 lines (98 loc) · 3.13 KB
/
models.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
import abc
class GracefulExit(SystemExit):
code = 1
class CredentialsCache(object):
def __init__(self, hub_name, device_id, device_key=None, certificate=None):
self._hub_name = hub_name
self._device_id = device_id
self._device_key = device_key
self._certificate = certificate
@property
def hub_name(self):
return self._hub_name
@property
def device_id(self):
return self._device_id
@property
def device_key(self):
return self._device_key
@property
def certificate(self):
return self._certificate
@property
def connection_string(self):
if self._hub_name is None or self._device_id is None:
return None
elif self._device_key is not None:
return "HostName={};DeviceId={};SharedAccessKey={}".format(
self._hub_name, self._device_id, self._device_key
)
elif self._certificate is not None:
return "HostName={};DeviceId={};x509=true".format(
self._hub_name, self._device_id
)
def todict(self):
return {
"device_id": self.device_id,
"hub_name": self.hub_name,
"connection_string": self.connection_string,
"device_key": self.device_key,
}
@classmethod
def from_dict(classref, data: dict):
return CredentialsCache(data["hub_name"], data["device_id"], data["device_key"])
class Storage(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def persist(self, credentials):
pass
@abc.abstractmethod
def retrieve(self):
pass
class Command(object):
def __init__(self, command_name, command_value, component_name=None):
self._command_name = command_name
self._command_value = command_value
if component_name is not None:
self._component_name = component_name
else:
self._component_name = None
self.reply = None
@property
def name(self):
return self._command_name
@property
def value(self):
return self._command_value
@property
def component_name(self):
return self._component_name
def __eq__(self, o: object) -> bool:
return (
self.name == o.name
and self.value == o.value
and self.component_name == o.component_name
)
class Property(object):
def __init__(self, property_name, property_value, component_name=None):
self._property_name = property_name
self._property_value = property_value
if component_name is not None:
self._component_name = component_name
else:
self._component_name = None
@property
def name(self):
return self._property_name
@property
def value(self):
return self._property_value
@property
def component_name(self):
return self._component_name
def __eq__(self, o: object) -> bool:
return (
self.name == o.name
and self.value == o.value
and self.component_name == o.component_name
)