@@ -12,14 +12,14 @@ LOG_MODULE_REGISTER(app_state, LOG_LEVEL_DBG);
1212#include <zcbor_decode.h>
1313#include <zcbor_encode.h>
1414
15+ #include "main.h"
1516#include "app_sensors.h"
1617#include "app_state.h"
1718
1819#define LIVE_RUNTIME_FMT "{\"live_runtime\":{\"ch0\":%lld,\"ch1\":%lld}"
1920#define CUMULATIVE_RUNTIME_FMT ",\"cumulative\":{\"ch0\":%lld,\"ch1\":%lld}}"
2021#define DEVICE_STATE_FMT LIVE_RUNTIME_FMT "}"
2122#define DEVICE_STATE_FMT_CUMULATIVE LIVE_RUNTIME_FMT CUMULATIVE_RUNTIME_FMT
22- #define DESIRED_RESET_KEY "reset_cumulative"
2323
2424static struct golioth_client * client ;
2525static struct ontime ot ;
@@ -39,41 +39,6 @@ static void async_handler(struct golioth_client *client,
3939 LOG_DBG ("State successfully set" );
4040}
4141
42- static int app_state_reset_desired (void )
43- {
44- int err ;
45- uint8_t cbor_payload [32 ];
46- bool ok ;
47-
48- LOG_INF ("Resetting \"%s\" LightDB State endpoint to defaults." , APP_STATE_DESIRED_ENDP );
49-
50- ZCBOR_STATE_E (encoding_state , 16 , cbor_payload , sizeof (cbor_payload ), 0 );
51- ok = zcbor_map_start_encode (encoding_state , 2 ) &&
52- zcbor_tstr_put_lit (encoding_state , DESIRED_RESET_KEY ) &&
53- zcbor_bool_put (encoding_state , false) &&
54- zcbor_map_end_encode (encoding_state , 2 );
55-
56- if (!ok ) {
57- LOG_ERR ("Error encoding CBOR to reset desired endpoint" );
58- return - ENODATA ;
59- }
60-
61- size_t cbor_len = encoding_state -> payload - cbor_payload ;
62- LOG_HEXDUMP_DBG (cbor_payload , cbor_len , "cbor_payload" );
63-
64- err = golioth_lightdb_set_async (client ,
65- APP_STATE_DESIRED_ENDP ,
66- GOLIOTH_CONTENT_TYPE_CBOR ,
67- cbor_payload ,
68- cbor_len ,
69- async_handler ,
70- NULL );
71- if (err ) {
72- LOG_ERR ("Unable to write to LightDB State: %d" , err );
73- }
74- return err ;
75- }
76-
7742static int app_state_update_actual (void )
7843{
7944 int err ;
@@ -148,76 +113,12 @@ int app_state_report_ontime(adc_node_t *ch0, adc_node_t *ch1)
148113 return 0 ;
149114}
150115
151- static void app_state_desired_handler (struct golioth_client * client ,
152- const struct golioth_response * response ,
153- const char * path ,
154- const uint8_t * payload ,
155- size_t payload_size ,
156- void * arg )
157- {
158- if (response -> status != GOLIOTH_OK ) {
159- LOG_ERR ("Failed to receive '%s' endpoint: %d" ,
160- APP_STATE_DESIRED_ENDP ,
161- response -> status );
162- return ;
163- }
164-
165- LOG_HEXDUMP_DBG (payload , payload_size , APP_STATE_DESIRED_ENDP );
166-
167- if ((payload_size == 1 ) && (payload [0 ] == 0xf6 )) {
168- /* This is `null` in CBOR */
169- LOG_ERR ("Endpoint is null, resetting desired to defaults" );
170- app_state_reset_desired ();
171- return ;
172- }
173-
174- struct zcbor_string key ;
175- bool reset_cumulative ;
176- bool ok ;
177-
178- ZCBOR_STATE_D (decoding_state , 1 , payload , payload_size , 1 , NULL );
179- ok = zcbor_map_start_decode (decoding_state ) &&
180- zcbor_tstr_decode (decoding_state , & key ) &&
181- zcbor_bool_decode (decoding_state , & reset_cumulative ) &&
182- zcbor_map_end_decode (decoding_state );
183-
184- if (!ok ) {
185- LOG_ERR ("ZCBOR Decoding Error" );
186- LOG_HEXDUMP_ERR (payload , payload_size , "cbor_payload" );
187- app_state_reset_desired ();
188- return ;
189- }
190-
191- if (strncmp (key .value , DESIRED_RESET_KEY , strlen (DESIRED_RESET_KEY )) != 0 ) {
192- LOG_ERR ("Unexpected key received: %.*s" , key .len , key .value );
193- app_state_reset_desired ();
194- return ;
195- }
196-
197- LOG_DBG ("Decoded: %.*s == %s" , key .len , key .value , reset_cumulative ? "true" : "false" );
198- if (reset_cumulative ) {
199- LOG_INF ("Request to reset cumulative values received. Processing now." );
200- reset_cumulative_totals ();
201- app_state_reset_desired ();
202- }
203- }
204-
205116int app_state_observe (struct golioth_client * state_client )
206117{
207118 int err ;
208119
209120 client = state_client ;
210121
211- err = golioth_lightdb_observe_async (client ,
212- APP_STATE_DESIRED_ENDP ,
213- GOLIOTH_CONTENT_TYPE_CBOR ,
214- app_state_desired_handler ,
215- NULL );
216- if (err ) {
217- LOG_WRN ("failed to observe lightdb path: %d" , err );
218- return err ;
219- }
220-
221122 /* This will only run once. It updates the actual state of the device
222123 * with the Golioth servers. Future updates will be sent whenever
223124 * changes occur.
0 commit comments