1
|
|
|
from __future__ import absolute_import |
2
|
|
|
|
3
|
|
|
from ._connectorobject import ConnectorObject |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class Device(ConnectorObject): |
7
|
|
|
def create(self, public=False, **kwargs): |
8
|
|
|
"""Creates the device. Attempts to create private devices by default, |
9
|
|
|
but if public is set to true, creates public devices. |
10
|
|
|
|
11
|
|
|
You can also set other default properties by passing in the relevant information. |
12
|
|
|
For example, setting a device with the given nickname and description:: |
13
|
|
|
|
14
|
|
|
dev.create(nickname="mydevice", description="This is an example") |
15
|
|
|
|
16
|
|
|
Furthermore, ConnectorDB supports creation of a device's streams immediately, |
17
|
|
|
which can considerably speed up device setup:: |
18
|
|
|
|
19
|
|
|
dev.create(streams={ |
20
|
|
|
"stream1": {"schema": '{\"type\":\"number\"}'} |
21
|
|
|
}) |
22
|
|
|
|
23
|
|
|
Note that the schema must be encoded as a string when creating in this format. |
24
|
|
|
""" |
25
|
|
|
kwargs["public"] = public |
26
|
|
|
self.metadata = self.db.create(self.path,kwargs).json() |
27
|
|
|
|
28
|
|
View Code Duplication |
def streams(self): |
|
|
|
|
29
|
|
|
"""Returns the list of streams that belong to the device""" |
30
|
|
|
result = self.db.read(self.path, {"q": "ls"}) |
31
|
|
|
|
32
|
|
|
if result is None or result.json() is None: |
33
|
|
|
return [] |
34
|
|
|
streams = [] |
35
|
|
|
for s in result.json(): |
36
|
|
|
strm = self[s["name"]] |
37
|
|
|
strm.metadata = s |
38
|
|
|
streams.append(strm) |
39
|
|
|
return streams |
40
|
|
|
|
41
|
|
|
def __getitem__(self, stream_name): |
42
|
|
|
"""Gets the child stream by name""" |
43
|
|
|
return Stream(self.db, self.path + "/" + stream_name) |
44
|
|
|
|
45
|
|
|
def __repr__(self): |
46
|
|
|
"""Returns a string representation of the device""" |
47
|
|
|
return "[Device:%s]" % (self.path, ) |
48
|
|
|
|
49
|
|
|
# ----------------------------------------------------------------------- |
50
|
|
|
# Following are getters and setters of the device's properties |
51
|
|
|
|
52
|
|
|
@property |
53
|
|
|
def apikey(self): |
54
|
|
|
"""gets the device's api key. Returns None if apikey not accessible.""" |
55
|
|
|
if "apikey" in self.data: |
56
|
|
|
return self.data["apikey"] |
57
|
|
|
return None |
58
|
|
|
|
59
|
|
|
def reset_apikey(self): |
60
|
|
|
"""invalidates the device's current api key, and generates a new one""" |
61
|
|
|
self.set({"apikey": ""}) |
62
|
|
|
return self.metadata["apikey"] |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
@property |
66
|
|
|
def public(self): |
67
|
|
|
"""gets whether the device is public |
68
|
|
|
(this means different things based on connectordb permissions setup - connectordb.com |
69
|
|
|
has this be whether the device is publically visible. Devices are individually public/private.) |
70
|
|
|
""" |
71
|
|
|
if "public" in self.data: |
72
|
|
|
return self.data["public"] |
73
|
|
|
return None |
74
|
|
|
|
75
|
|
|
@public.setter |
76
|
|
|
def public(self,new_public): |
77
|
|
|
"""Attempts to set whether the device is public""" |
78
|
|
|
self.set({"public": new_public}) |
79
|
|
|
|
80
|
|
|
@property |
81
|
|
|
def role(self): |
82
|
|
|
"""Gets the role of the device. This is the permissions level that the device has. It might |
83
|
|
|
not be accessible depending on the permissions setup of ConnectorDB. Returns None if not accessible""" |
84
|
|
|
if "role" in self.data: |
85
|
|
|
return self.data["role"] |
86
|
|
|
return None |
87
|
|
|
|
88
|
|
|
@role.setter |
89
|
|
|
def role(self,new_role): |
90
|
|
|
""" Attempts to set the device's role""" |
91
|
|
|
self.set({"role": new_role}) |
92
|
|
|
|
93
|
|
|
@property |
94
|
|
|
def enabled(self): |
95
|
|
|
""" gets whether the device is enabled. This allows a device to notify ConnectorDB when |
96
|
|
|
it is active and when it is not running""" |
97
|
|
|
if "enabled" in self.data: |
98
|
|
|
return self.data["enabled"] |
99
|
|
|
return None |
100
|
|
|
@enabled.setter |
101
|
|
|
def enabled(self,new_enabled): |
102
|
|
|
"""Sets the enabled state of the device""" |
103
|
|
|
self.set({"enabled": new_enabled}) |
104
|
|
|
|
105
|
|
|
@property |
106
|
|
|
def user(self): |
107
|
|
|
"""user returns the user which owns the given device""" |
108
|
|
|
return User(self.db, self.path.split("/")[0]) |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
# The import has to go on the bottom because py3 imports are annoying about circular dependencies |
112
|
|
|
from ._user import User |
113
|
|
|
from ._stream import Stream |
114
|
|
|
|