Skip to content

Commit a276c3b

Browse files
authored
Update README.md
1 parent 272e528 commit a276c3b

1 file changed

Lines changed: 183 additions & 81 deletions

File tree

README.md

Lines changed: 183 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Asynchronous Python library to get data from the cloud, and switch Crownstones.
1111

1212
## Requirements
1313

14-
* Python 3.7 or higher
14+
* Python 3.7 (only version that it's tested on currently)
1515
* Aiohttp 3.6.2
1616

1717
## Standard installation
@@ -39,33 +39,70 @@ $ python setup.py install
3939

4040
## Getting started
4141

42-
### Example
42+
### Examples
43+
44+
#### Async example
4345

4446
```Python
45-
from cloudLib.lib.cloud import CrownstoneCloud
47+
from crownstone_cloud.lib.cloud import CrownstoneCloud
48+
import asyncio
49+
50+
51+
async def main():
52+
# init cloud
53+
cloud = CrownstoneCloud('email', 'password')
54+
await cloud.initialize()
55+
56+
# get a crownstone by name and switch it on
57+
crownstone = cloud.get_crownstone('awesomeCrownstone')
58+
await crownstone.turn_on()
4659

47-
cloud = CrownstoneCloud()
60+
# switch all crownstones in a sphere to on:
61+
sphere = cloud.spheres.find('awesomeSphere')
62+
for crownstone in sphere.crownstones:
63+
await crownstone.turn_on()
4864

65+
asyncio.run(main())
66+
```
67+
68+
#### Sync example
69+
70+
```Python
71+
from crownstone_cloud.lib.cloud import CrownstoneCloud
4972

50-
############# this function will be executed #############
51-
async def run():
52-
# login to the cloud using your account (replace with your own credentials)
53-
await cloud.login('email', 'password')
54-
# load all the user data from the cloud
55-
await cloud.sync()
73+
# init cloud
74+
cloud = CrownstoneCloud('email', 'password')
75+
cloud.initialize_sync()
5676

57-
# get a crownstone by name
58-
lamp = cloud.get_crownstone('Lamp')
59-
# turn the crownstone on
60-
await lamp.turn_on()
77+
# get a crownstone and turn it on
78+
crownstone = cloud.get_crownstone('awesomeCrownstone')
79+
crownstone.turn_on_sync()
80+
```
6181

62-
# close the session
63-
await cloud.cleanup()
64-
##########################################################
82+
### Initialisation
83+
Crownstone cloud is initialized with 2 arguments:
84+
* User email
85+
* User password
6586

66-
# start executing the function
67-
cloud.start(run())
87+
If you do not yet have a Crownstone account, go to (My Crownstone)[my.crownstone.rocks] to set one up.
88+
The email and password are used to re-login after an access token has expired.
89+
```Python
90+
cloud = CrownstoneCloud('email', 'password')
91+
```
92+
if already have an access token and you want to skip to first login for speed you can use:
93+
```Python
94+
cloud.set_access_token('myAccessToken')
95+
```
96+
to initialize all the cloud data into the lib use
97+
```Python
98+
await cloud.initialize()
99+
```
100+
for async usage, or use
101+
```Python
102+
cloud.initialize_sync()
68103
```
104+
for sync usage.<br>
105+
It is only required to call initialize once at the beginning of the program.
69106

70107
## Data structure
71108

@@ -83,35 +120,29 @@ The cloud can be displayed with the following structure:
83120
The user is the to whom the data belongs.<br>
84121
The user is the one that logs in using email and password.<br>
85122
By getting a user specific access token after login, the data for that specific user can be requested.
86-
```python
87-
await cloud.login('email', 'password')
88-
```
89123

90124
### Keys
91125

92126
The keys are user specific.<br>
93127
They are required to connect to the crownstone bluetooth mesh.<br>
94128
The most common used keys are the sphere keys. They are located within each individual sphere.<br>
95-
Getting the keys is optional and not included in the general sync.<br>
96-
The keys are returned as a dictionary.
97-
To get the keys for a sphere:
98-
```python
99-
sphere = cloud.spheres.find('MySphere')
100-
keys = await sphere.get_keys()
101-
```
102129

103130
### Spheres
104131

105-
Spheres can be seen as a house, apartement or office. They have rooms (locations), Crownstones and users in them.<br>
106-
Note that the spheres need to be synced **FIRST** before the other data can be synced.<br>
107-
To get spheres for the user without any data (only name and id) use:
108-
```python
109-
await cloud.spheres.sync()
110-
```
111-
To get the spheres and all their data at once use:
112-
```python
113-
await cloud.sync()
114-
```
132+
Spheres are the main data entry. They have rooms (locations), Crownstones and users in them.<br>
133+
Example spheres:
134+
* House
135+
* Office
136+
* Apartement
137+
138+
A Sphere has the following fields in the cloud lib:
139+
* crownstones: Crownstones
140+
* locations: Locations
141+
* users: Users
142+
* keys: Dict (optional, default = None)
143+
* name: String
144+
* cloud_id: String
145+
* unique_id: String
115146

116147
### Locations
117148

@@ -122,48 +153,29 @@ For example for a house:
122153
* Garage
123154
* Bathroom
124155

125-
To get the locations for your sphere use:
126-
```python
127-
sphere = cloud.spheres.find('MySphere')
128-
await sphere.locations.sync()
129-
```
130-
For the presence tracking, an advanced algorithm detects whenever a user is on a specific location.<br>
131-
The presence is saved for each location in a list. The list contains names of the people who are present.<br>
132-
To print the present people list:
133-
```python
134-
location = sphere.locations.find('MyLocation')
135-
print(location.present_people)
136-
```
156+
A Location has the following fields in the cloud lib:
157+
* present_people: List
158+
* name: String
159+
* cloud_id: String
160+
* unique_id: String
137161

138162
### Crownstones
139163

140164
Crownstones are smart plugs that can make every device that isn't smart, way smarter!<br>
141165
Crownstones are located within a sphere.<br>
142-
Example Crownstones:
166+
Example names of Crownstones:
143167
* Lamp
144168
* Charger
145169
* Television
146170

147-
to get the crownstones for your sphere:
148-
```python
149-
sphere = cloud.spheres.find('MySphere')
150-
await sphere.crownstones.sync()
151-
```
152-
Crownstones can also be turned on/off remotely.<br>
153-
Since this is a command sent to your phone, it is required to have Bluetooth on and to be present in your sphere.<br>
154-
To switch a crownstone on/off:
155-
```python
156-
crownstone = cloud.get_crownstone('MyCrownstone')
157-
await crownstone.turn_on()
158-
await crownstone.turn_off()
159-
```
160-
It is also possible to set the brightness(dimming) of a crownstone.<br>
161-
For this to work, you need to enable dimming in your crownstone app for that specific crownstone. Dimming only works with lights and it is **not** recommended to use it for any other device.<br>
162-
For the brightness use a percentage between 0% and 100%. To set the brightness:
163-
```python
164-
crownstone = cloud.get_crownstone('MyCrownstone')
165-
await crownstone.set_brightness(50)
166-
```
171+
A Crownstone has the following fields in the cloud lib:
172+
* state: Float (0..1)
173+
* dimming_enabled: Bool (default = False)
174+
* name: String
175+
* unique_id: String
176+
* cloud_id: String
177+
* type: String
178+
* sw_version: String
167179

168180
### Users
169181

@@ -173,16 +185,106 @@ A user can have 3 roles:
173185
* Member
174186
* Guest
175187

176-
Each role has different privileges.<br>
177-
Users can have similar names. To view the info of a user's first name in a sphere:
178-
```python
179-
sphere = cloud.spheres.find('MySphere')
180-
users = sphere.users.find_by_first_name('MyUser')
181-
for username in users:
182-
print(user.role)
183-
print(user.last_name)
184-
print(user.email)
188+
A User has the following fields in the cloud lib:
189+
* role: String
190+
* first_name: String
191+
* last_name: String
192+
* email: String
193+
* cloud_id: String
194+
* email_verified: Bool
195+
196+
## Function list
197+
198+
### Cloud
199+
200+
#### initialize()
201+
> Login if no access token available, and sync all data for the user from the cloud.
202+
203+
#### set_access_token(access_token: String)
204+
> Set an access token to skip the login part, if you already have one
205+
206+
#### get_crownstone(crownstone_name: String) -> Crownstone
207+
> Get a Crownstone object by name, if it exists.
208+
209+
#### get_crownstone_by_id(crownstone_id: String) -> Crownstone
210+
> Get a Crownstone object by it's id, it's it exists.
211+
212+
### Spheres
213+
214+
#### update()
215+
> Async function. Sync the Spheres with the cloud. Doing this will replace all current sphere data with that of the cloud.
216+
217+
#### find(sphere_name: String) -> Sphere
218+
> Returns a sphere object if one exists by that name.
219+
220+
#### find_by_id(sphere_id: String) -> Sphere
221+
> Return a sphere object if one exists by that id.
222+
223+
### Sphere
224+
225+
#### get_keys() -> dict
226+
> Async function. Returns a dict with the keys of this sphere. The keys can be used for BLE connectivity with the Crownstones.
227+
228+
### Crownstones
229+
230+
#### update()
231+
> Async function. Sync the Crownstones with the cloud for a sphere. Doing this will replace all the current crownstone data with that of the cloud.
232+
233+
#### find(crownstone_name: String) -> Crownstone
234+
> Return a Crownstone object if one exists by that name.
235+
236+
#### find_by_id(crownstone_id: String) -> Crownstone
237+
> Return a Crownstone object if one exists by that id.
238+
239+
### Crownstone
240+
241+
#### turn_on()
242+
> Async function. Send a command to turn a Crownstone on. To make this work make sure to be in the selected sphere and have Bluetooth enabled on your phone.
243+
244+
#### turn_off()
245+
> Async function. Send a command to turn a Crownstone off. To make this work make sure to be in the selected sphere and have Bluetooth enabled on your phone.
246+
247+
#### set_brightness(percentage: int)
248+
> Async function. Send a command to set a Crownstone to a given brightness level. To make this work make sure to be in the selected sphere and have Bluetooth enabled on your phone.
249+
250+
### Locations
251+
252+
#### update()
253+
> Async function. Sync the Locations en present people in that location with the cloud for a sphere. Doing this will replace all the current location data with that of the cloud.
254+
255+
#### find(location_name: String) -> Location
256+
> Return a location object if one exists by that name.
257+
258+
#### find_by_id(location_id: String) -> Location
259+
> Return a location object if one exists by that id.
260+
261+
### Users
262+
263+
#### update()
264+
> Async function. Sync the Users with the cloud for a sphere. Doing this will replace all the current user data with that of the cloud.
265+
266+
#### find_by_first_name(first_name: String) -> list
267+
> Returns a list of all users with that first name, as duplicate first names can exist.
268+
269+
#### find_by_last_name(last_name: String) -> list
270+
> Return a list of all users with that last name, as duplicate last names can exist.
271+
272+
#### find_by_id(user_id: String) -> Location
273+
> Return a location object if one exists by that id.
274+
275+
## Async vs sync
276+
The lib can be used synchonously and asynchronously.<br>
277+
Async functions need to be awaited:
278+
```Python
279+
await cloud.spheres.update()
280+
```
281+
All the async functions mentioned above can also be used synchronously.<br>
282+
Simply type the same function name, but with underscore sync at the end. for example:
283+
```Python
284+
cloud.initialize_sync()
285+
cloud.spheres.update_sync()
185286
```
287+
Make sure to see the examples above!
186288

187289
## Testing
188290
Tests coming soon!

0 commit comments

Comments
 (0)