simpleiot 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- simpleiot-0.1.0/.gitignore +6 -0
- simpleiot-0.1.0/CHANGELOG.md +17 -0
- simpleiot-0.1.0/LICENSE +202 -0
- simpleiot-0.1.0/PKG-INFO +160 -0
- simpleiot-0.1.0/README.md +140 -0
- simpleiot-0.1.0/examples/mirror_temperature/README.md +21 -0
- simpleiot-0.1.0/examples/mirror_temperature/mirror_temperature.py +38 -0
- simpleiot-0.1.0/examples/rover/README.md +117 -0
- simpleiot-0.1.0/examples/rover/dashboard.png +0 -0
- simpleiot-0.1.0/examples/rover/rover.py +202 -0
- simpleiot-0.1.0/examples/rover/rover.yaml +368 -0
- simpleiot-0.1.0/examples/rover/sim.py +156 -0
- simpleiot-0.1.0/examples/send_points/README.md +19 -0
- simpleiot-0.1.0/examples/send_points/send_points.py +43 -0
- simpleiot-0.1.0/examples/tree/README.md +18 -0
- simpleiot-0.1.0/examples/tree/tree.py +30 -0
- simpleiot-0.1.0/examples/watch/README.md +18 -0
- simpleiot-0.1.0/examples/watch/watch.py +27 -0
- simpleiot-0.1.0/pyproject.toml +48 -0
- simpleiot-0.1.0/src/simpleiot/__init__.py +47 -0
- simpleiot-0.1.0/src/simpleiot/client.py +449 -0
- simpleiot-0.1.0/src/simpleiot/codec.py +372 -0
- simpleiot-0.1.0/src/simpleiot/ids.py +31 -0
- simpleiot-0.1.0/src/simpleiot/py.typed +0 -0
- simpleiot-0.1.0/tests/test_codec.py +178 -0
- simpleiot-0.1.0/tests/test_integration.py +175 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to the `simpleiot` Python package are documented here. The
|
|
4
|
+
format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|
5
|
+
|
|
6
|
+
## [Unreleased]
|
|
7
|
+
|
|
8
|
+
## [0.1.0]
|
|
9
|
+
|
|
10
|
+
- **First release.** Connect with the shared token, read and write nodes and
|
|
11
|
+
points, create, move, and delete nodes, and subscribe to point changes.
|
|
12
|
+
- **Set up nodes from a YAML file with `provision()`.** It uploads the file for
|
|
13
|
+
the server to apply, as `siot import` does, and waits until it has, raising
|
|
14
|
+
the server's error if it fails.
|
|
15
|
+
- **Autonomous rover example.** A simulated rover reports its position, speed,
|
|
16
|
+
and battery to a dashboard with a live map and takes commands from it. It sets
|
|
17
|
+
up its own nodes with `provision()`. See `examples/rover`.
|
simpleiot-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
simpleiot-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: simpleiot
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python client for Simple IoT, over NATS
|
|
5
|
+
Project-URL: Homepage, https://simpleiot.org
|
|
6
|
+
Project-URL: Documentation, https://docs.simpleiot.org/python/
|
|
7
|
+
Project-URL: Repository, https://github.com/simpleiot/simpleiot
|
|
8
|
+
Project-URL: Changelog, https://github.com/simpleiot/simpleiot/blob/master/python/CHANGELOG.md
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: iot,nats,simpleiot
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Framework :: AsyncIO
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Topic :: System :: Networking
|
|
17
|
+
Requires-Python: >=3.10
|
|
18
|
+
Requires-Dist: nats-py>=2.6
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# simpleiot
|
|
22
|
+
|
|
23
|
+
A Python client for [Simple IoT](https://simpleiot.org). Read and write nodes
|
|
24
|
+
and points, and watch for changes, over the NATS API.
|
|
25
|
+
|
|
26
|
+
The client connects with the instance's shared token (`SIOT_AUTH_TOKEN`), which
|
|
27
|
+
reaches the whole tree. It suits scripts and services running alongside an
|
|
28
|
+
instance you operate.
|
|
29
|
+
|
|
30
|
+

|
|
31
|
+
|
|
32
|
+
A typical setup runs the Python process next to SIOT on an edge device. What it
|
|
33
|
+
writes reaches the cloud through sync, and a person sees it in the web UI on
|
|
34
|
+
site or remotely.
|
|
35
|
+
|
|
36
|
+
## Install
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
pip install simpleiot
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Requires Python 3.10 or newer. The client is built on
|
|
43
|
+
[nats-py](https://github.com/nats-io/nats.py) and uses `asyncio`.
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
import asyncio
|
|
49
|
+
import simpleiot
|
|
50
|
+
from simpleiot import Point
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
async def main():
|
|
54
|
+
async with await simpleiot.connect() as c:
|
|
55
|
+
root = await c.get_root()
|
|
56
|
+
print("root:", root.description)
|
|
57
|
+
|
|
58
|
+
id = await c.create_node(root.id, "variable", description="tank level")
|
|
59
|
+
await c.send_point(id, "value", 42.5)
|
|
60
|
+
|
|
61
|
+
async with await c.subscribe_node(id) as sub:
|
|
62
|
+
async for m in sub:
|
|
63
|
+
for p in m.points:
|
|
64
|
+
print(p.type, p.value)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
asyncio.run(main())
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`connect()` reads `SIOT_NATS_SERVER` (default `nats://localhost:4222`) and
|
|
71
|
+
`SIOT_AUTH_TOKEN` from the environment, or takes them as arguments:
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
c = await simpleiot.connect("nats://10.0.0.5:4222", token="...")
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Nodes and points
|
|
78
|
+
|
|
79
|
+
Data in Simple IoT is a tree of **nodes**, each holding **points**. A point has
|
|
80
|
+
a `type`, a `key` (`"0"` unless a node holds several of a type), a `value`, and
|
|
81
|
+
a `time`. The edge from a node to its parent carries its own points, such as
|
|
82
|
+
`tombstone` for a deleted node. See the
|
|
83
|
+
[data reference](https://docs.simpleiot.org/docs/ref/data.html).
|
|
84
|
+
|
|
85
|
+
`Point.value` sets the encoding when sending:
|
|
86
|
+
|
|
87
|
+
| Value | Sent as |
|
|
88
|
+
| ---------------------- | --------------------- |
|
|
89
|
+
| `float`, `int`, `bool` | a float point |
|
|
90
|
+
| `str` | a string point |
|
|
91
|
+
| `dict`, `list` | a JSON point |
|
|
92
|
+
| `None` | a point with no value |
|
|
93
|
+
|
|
94
|
+
Pass `data_type=DataType.INT` to send an integer as an int point. A received
|
|
95
|
+
point's `value` is a `float`, `int`, or `str` by its data type; `number` and
|
|
96
|
+
`text` read it as a number or string either way.
|
|
97
|
+
|
|
98
|
+
## API
|
|
99
|
+
|
|
100
|
+
All methods are coroutines on `Client`.
|
|
101
|
+
|
|
102
|
+
- `get_root()`, `get_node(id, parent="all")`, `get_children(parent, type=...)`
|
|
103
|
+
- `get_nodes(parent, id="all", type=..., include_deleted=..., depth=...)`, the
|
|
104
|
+
`nodes.<parent>.<id>` request
|
|
105
|
+
- `search(below, query, limit=..., type=...)`
|
|
106
|
+
- `send_points(id, points)`, `send_point(id, type, value, key="0")`
|
|
107
|
+
- `send_edge_points(id, parent, points)`
|
|
108
|
+
- `create_node(parent, type, points, id=..., description=..., primary=...)`
|
|
109
|
+
- `delete_node(id, parent)`, `move_node(id, old_parent, new_parent)`
|
|
110
|
+
- `provision(name, yaml)` uploads a node file for the server to apply, as
|
|
111
|
+
`siot import` would, and waits until it has
|
|
112
|
+
- `subscribe_node(id)`, `subscribe_tree(id)`, `subscribe(subject)`: async
|
|
113
|
+
iterators of `PointsMessage`
|
|
114
|
+
- `close()`
|
|
115
|
+
|
|
116
|
+
A server error raises `SiotError`; a missing node raises `NotFoundError`.
|
|
117
|
+
|
|
118
|
+
## Examples
|
|
119
|
+
|
|
120
|
+
Each example is a short script in its own directory with a README:
|
|
121
|
+
|
|
122
|
+
- [tree](https://github.com/simpleiot/simpleiot/tree/master/python/examples/tree)
|
|
123
|
+
prints the node tree
|
|
124
|
+
- [send_points](https://github.com/simpleiot/simpleiot/tree/master/python/examples/send_points)
|
|
125
|
+
creates a node and writes a value to it once a second
|
|
126
|
+
- [watch](https://github.com/simpleiot/simpleiot/tree/master/python/examples/watch)
|
|
127
|
+
prints every point change below a node
|
|
128
|
+
- [mirror_temperature](https://github.com/simpleiot/simpleiot/tree/master/python/examples/mirror_temperature)
|
|
129
|
+
reacts to one point by writing another
|
|
130
|
+
- [rover](https://github.com/simpleiot/simpleiot/tree/master/python/examples/rover)
|
|
131
|
+
drives a simulated autonomous rover from a dashboard, with a live map, status,
|
|
132
|
+
and commands
|
|
133
|
+
|
|
134
|
+
Run them against a local instance:
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
SIOT_AUTH_TOKEN=... python examples/tree/tree.py
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Development
|
|
141
|
+
|
|
142
|
+
The client lives in the
|
|
143
|
+
[Simple IoT repository](https://github.com/simpleiot/simpleiot) under `python/`,
|
|
144
|
+
so it changes together with the server.
|
|
145
|
+
|
|
146
|
+
```
|
|
147
|
+
cd python
|
|
148
|
+
uv sync
|
|
149
|
+
uv run pytest
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
The tests need [uv](https://docs.astral.sh/uv/). The codec tests check the
|
|
153
|
+
encoder against the fixtures `data/point_fixture_test.go` writes, the same ones
|
|
154
|
+
the JavaScript client is checked against. The integration tests start a scratch
|
|
155
|
+
`siot` when `SIOT_BIN` names one; `siot_test_python` in `envsetup.sh` builds it
|
|
156
|
+
and runs them.
|
|
157
|
+
|
|
158
|
+
To publish a release, set the version in `python/src/simpleiot/__init__.py`, add
|
|
159
|
+
a `python/CHANGELOG.md` entry, and push a `py-v<version>` tag. The
|
|
160
|
+
`Python release` workflow builds the package and publishes it to PyPI.
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# simpleiot
|
|
2
|
+
|
|
3
|
+
A Python client for [Simple IoT](https://simpleiot.org). Read and write nodes
|
|
4
|
+
and points, and watch for changes, over the NATS API.
|
|
5
|
+
|
|
6
|
+
The client connects with the instance's shared token (`SIOT_AUTH_TOKEN`), which
|
|
7
|
+
reaches the whole tree. It suits scripts and services running alongside an
|
|
8
|
+
instance you operate.
|
|
9
|
+
|
|
10
|
+

|
|
11
|
+
|
|
12
|
+
A typical setup runs the Python process next to SIOT on an edge device. What it
|
|
13
|
+
writes reaches the cloud through sync, and a person sees it in the web UI on
|
|
14
|
+
site or remotely.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
pip install simpleiot
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Requires Python 3.10 or newer. The client is built on
|
|
23
|
+
[nats-py](https://github.com/nats-io/nats.py) and uses `asyncio`.
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
import asyncio
|
|
29
|
+
import simpleiot
|
|
30
|
+
from simpleiot import Point
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
async def main():
|
|
34
|
+
async with await simpleiot.connect() as c:
|
|
35
|
+
root = await c.get_root()
|
|
36
|
+
print("root:", root.description)
|
|
37
|
+
|
|
38
|
+
id = await c.create_node(root.id, "variable", description="tank level")
|
|
39
|
+
await c.send_point(id, "value", 42.5)
|
|
40
|
+
|
|
41
|
+
async with await c.subscribe_node(id) as sub:
|
|
42
|
+
async for m in sub:
|
|
43
|
+
for p in m.points:
|
|
44
|
+
print(p.type, p.value)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
asyncio.run(main())
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`connect()` reads `SIOT_NATS_SERVER` (default `nats://localhost:4222`) and
|
|
51
|
+
`SIOT_AUTH_TOKEN` from the environment, or takes them as arguments:
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
c = await simpleiot.connect("nats://10.0.0.5:4222", token="...")
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Nodes and points
|
|
58
|
+
|
|
59
|
+
Data in Simple IoT is a tree of **nodes**, each holding **points**. A point has
|
|
60
|
+
a `type`, a `key` (`"0"` unless a node holds several of a type), a `value`, and
|
|
61
|
+
a `time`. The edge from a node to its parent carries its own points, such as
|
|
62
|
+
`tombstone` for a deleted node. See the
|
|
63
|
+
[data reference](https://docs.simpleiot.org/docs/ref/data.html).
|
|
64
|
+
|
|
65
|
+
`Point.value` sets the encoding when sending:
|
|
66
|
+
|
|
67
|
+
| Value | Sent as |
|
|
68
|
+
| ---------------------- | --------------------- |
|
|
69
|
+
| `float`, `int`, `bool` | a float point |
|
|
70
|
+
| `str` | a string point |
|
|
71
|
+
| `dict`, `list` | a JSON point |
|
|
72
|
+
| `None` | a point with no value |
|
|
73
|
+
|
|
74
|
+
Pass `data_type=DataType.INT` to send an integer as an int point. A received
|
|
75
|
+
point's `value` is a `float`, `int`, or `str` by its data type; `number` and
|
|
76
|
+
`text` read it as a number or string either way.
|
|
77
|
+
|
|
78
|
+
## API
|
|
79
|
+
|
|
80
|
+
All methods are coroutines on `Client`.
|
|
81
|
+
|
|
82
|
+
- `get_root()`, `get_node(id, parent="all")`, `get_children(parent, type=...)`
|
|
83
|
+
- `get_nodes(parent, id="all", type=..., include_deleted=..., depth=...)`, the
|
|
84
|
+
`nodes.<parent>.<id>` request
|
|
85
|
+
- `search(below, query, limit=..., type=...)`
|
|
86
|
+
- `send_points(id, points)`, `send_point(id, type, value, key="0")`
|
|
87
|
+
- `send_edge_points(id, parent, points)`
|
|
88
|
+
- `create_node(parent, type, points, id=..., description=..., primary=...)`
|
|
89
|
+
- `delete_node(id, parent)`, `move_node(id, old_parent, new_parent)`
|
|
90
|
+
- `provision(name, yaml)` uploads a node file for the server to apply, as
|
|
91
|
+
`siot import` would, and waits until it has
|
|
92
|
+
- `subscribe_node(id)`, `subscribe_tree(id)`, `subscribe(subject)`: async
|
|
93
|
+
iterators of `PointsMessage`
|
|
94
|
+
- `close()`
|
|
95
|
+
|
|
96
|
+
A server error raises `SiotError`; a missing node raises `NotFoundError`.
|
|
97
|
+
|
|
98
|
+
## Examples
|
|
99
|
+
|
|
100
|
+
Each example is a short script in its own directory with a README:
|
|
101
|
+
|
|
102
|
+
- [tree](https://github.com/simpleiot/simpleiot/tree/master/python/examples/tree)
|
|
103
|
+
prints the node tree
|
|
104
|
+
- [send_points](https://github.com/simpleiot/simpleiot/tree/master/python/examples/send_points)
|
|
105
|
+
creates a node and writes a value to it once a second
|
|
106
|
+
- [watch](https://github.com/simpleiot/simpleiot/tree/master/python/examples/watch)
|
|
107
|
+
prints every point change below a node
|
|
108
|
+
- [mirror_temperature](https://github.com/simpleiot/simpleiot/tree/master/python/examples/mirror_temperature)
|
|
109
|
+
reacts to one point by writing another
|
|
110
|
+
- [rover](https://github.com/simpleiot/simpleiot/tree/master/python/examples/rover)
|
|
111
|
+
drives a simulated autonomous rover from a dashboard, with a live map, status,
|
|
112
|
+
and commands
|
|
113
|
+
|
|
114
|
+
Run them against a local instance:
|
|
115
|
+
|
|
116
|
+
```
|
|
117
|
+
SIOT_AUTH_TOKEN=... python examples/tree/tree.py
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Development
|
|
121
|
+
|
|
122
|
+
The client lives in the
|
|
123
|
+
[Simple IoT repository](https://github.com/simpleiot/simpleiot) under `python/`,
|
|
124
|
+
so it changes together with the server.
|
|
125
|
+
|
|
126
|
+
```
|
|
127
|
+
cd python
|
|
128
|
+
uv sync
|
|
129
|
+
uv run pytest
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
The tests need [uv](https://docs.astral.sh/uv/). The codec tests check the
|
|
133
|
+
encoder against the fixtures `data/point_fixture_test.go` writes, the same ones
|
|
134
|
+
the JavaScript client is checked against. The integration tests start a scratch
|
|
135
|
+
`siot` when `SIOT_BIN` names one; `siot_test_python` in `envsetup.sh` builds it
|
|
136
|
+
and runs them.
|
|
137
|
+
|
|
138
|
+
To publish a release, set the version in `python/src/simpleiot/__init__.py`, add
|
|
139
|
+
a `python/CHANGELOG.md` entry, and push a `py-v<version>` tag. The
|
|
140
|
+
`Python release` workflow builds the package and publishes it to PyPI.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# React to a point
|
|
2
|
+
|
|
3
|
+
Watches the `value` point of one node, converts it from Celsius to Fahrenheit,
|
|
4
|
+
and writes the result to a new node beside it. This is the shape of a small
|
|
5
|
+
control script: subscribe to one point, compute, and write another. Stop it with
|
|
6
|
+
Ctrl-C.
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
SIOT_AUTH_TOKEN=... python examples/mirror_temperature/mirror_temperature.py <node-id>
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
writing Fahrenheit to 2wer551eezqp8a683j3bbg7kn8
|
|
14
|
+
20.45 °C -> 68.81 °F
|
|
15
|
+
20.94 °C -> 69.69 °F
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Pass the ID of the node [send_points](../send_points/) writes to, or of any node
|
|
19
|
+
with a `value` point. The script reads the source with `get_node()`, creates the
|
|
20
|
+
target under the same parent with `create_node()`, and writes to it from a
|
|
21
|
+
`subscribe_node()` loop.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""Watches a node's value and writes it to another node in Fahrenheit.
|
|
2
|
+
|
|
3
|
+
This is the shape of a small control script: subscribe to one point, compute,
|
|
4
|
+
and write another.
|
|
5
|
+
|
|
6
|
+
SIOT_AUTH_TOKEN=... python examples/mirror_temperature/mirror_temperature.py <node-id>
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import asyncio
|
|
10
|
+
import sys
|
|
11
|
+
|
|
12
|
+
import simpleiot
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
async def main(source: str):
|
|
16
|
+
async with await simpleiot.connect() as c:
|
|
17
|
+
src = await c.get_node(source)
|
|
18
|
+
target = await c.create_node(
|
|
19
|
+
src.parent, "variable", description=f"{src.description or source} (°F)"
|
|
20
|
+
)
|
|
21
|
+
print("writing Fahrenheit to", target)
|
|
22
|
+
|
|
23
|
+
async with await c.subscribe_node(source) as sub:
|
|
24
|
+
async for m in sub:
|
|
25
|
+
for p in m.points:
|
|
26
|
+
if p.type == "value":
|
|
27
|
+
f = p.number * 9 / 5 + 32
|
|
28
|
+
await c.send_point(target, "value", round(f, 2))
|
|
29
|
+
print(f"{p.number} °C -> {f:.2f} °F")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
if len(sys.argv) != 2:
|
|
33
|
+
sys.exit(__doc__)
|
|
34
|
+
|
|
35
|
+
try:
|
|
36
|
+
asyncio.run(main(sys.argv[1]))
|
|
37
|
+
except KeyboardInterrupt:
|
|
38
|
+
pass
|