1+ from crownstone_cloud import CrownstoneCloud
2+
3+
4+ class CloudDataUpdater :
5+ """
6+ Optional API.
7+ Contains callbacks that can be used with the Crownstone SSE lib, or own events.
8+ """
9+ def __init__ (self , cloud_instance : CrownstoneCloud ) -> None :
10+ """
11+ :param cloud_instance: your instance of the cloud lib.
12+
13+ Make sure your instance is initialized with data first!
14+ """
15+ self .cloud_instance = cloud_instance
16+
17+ def update_switch_state (self , switch_state_event ) -> None :
18+ """
19+ Updates the switch state of a crownstone.
20+
21+ :param switch_state_event: Instance of SwitchStateUpdateEvent of the Crownstone SSE lib.
22+
23+ SwitchStateUpdateEvent is a class containing: sphere_id, cloud_id, unique_id, switch_state.
24+ """
25+ sphere = self .cloud_instance .spheres .find_by_id (switch_state_event .sphere_id )
26+ crownstone = sphere .crownstones .find_by_id (switch_state_event .cloud_id )
27+ crownstone .state = switch_state_event .switch_state
28+
29+ def update_presence (self , presence_event ) -> None :
30+ """
31+ Updates the presence in a location.
32+
33+ :param presence_event: Instance of PresenceEvent of the Crownstone SSE lib.
34+
35+ PresenceEvent is a class containing: event_type, sphere_id, location_id, user_id.
36+ """
37+ sphere = self .cloud_instance .spheres .find_by_id (presence_event .sphere_id )
38+ location = sphere .locations .find_by_id (presence_event .location_id )
39+ user = sphere .users .find_by_id (presence_event .user_id )
40+
41+ if presence_event .type == 'enterLocation' :
42+ location .present_people .append (user .cloud_id )
43+
44+ if presence_event .type == 'exitLocation' :
45+ location .present_people .remove (user .cloud_id )
46+
47+ def update_data (self , data_change_event ) -> None :
48+ """
49+ Replace the current data with new data from the cloud after a change.
50+
51+ :param data_change_event: Instance of DataChangeEvent of the Crownstone SSE lib.
52+
53+ DataChangeEvent is a class containing: operation, sphere_id, changed_item_id, changed_item_name
54+ """
55+ if data_change_event .type == 'spheres' :
56+ self .cloud_instance .spheres .update_sync ()
57+
58+ sphere = self .cloud_instance .spheres .find_by_id (data_change_event .sphere_id )
59+
60+ if data_change_event .type == 'stones' :
61+ sphere .crownstones .update_sync ()
62+
63+ if data_change_event .type == 'users' :
64+ sphere .users .update_sync ()
65+
66+ if data_change_event .type == 'locations' :
67+ sphere .locations .update_sync ()
0 commit comments