Skip to content

Commit 13a1db9

Browse files
committed
move cumulative reset to RPC and wake main thread after reset
RPC is better suited than LightDB State for instructing the device to reset its cumulative ontime count. Wake the main thread after resetting cumulative ontime so that the sensor read will immediately update the cloud with new (reset) cumulative values. Before this change, we waited until the next scheduled sensor reading to update the cloud copy of cumulative values. Signed-off-by: Mike Szczys <mike@golioth.io>
1 parent a78ade3 commit 13a1db9

File tree

3 files changed

+24
-111
lines changed

3 files changed

+24
-111
lines changed

README.rst

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ The following RPCs can be initiated in the Remote Procedure Call menu of the
152152
``reboot``
153153
Reboot the system.
154154

155+
``reset_cumulative``
156+
Reset the cumulative "on time" values stored on the device. After executing, the device will
157+
update the cloud's ``state/cumulative`` values using the LightDB State service.
158+
155159
``set_log_level``
156160
Set the log level.
157161

@@ -188,15 +192,11 @@ endpoint.
188192
Stateful Data (LightDB State)
189193
-----------------------------
190194

191-
The concept of Digital Twin is demonstrated with the LightDB State via the ``desired`` and
192-
``actual`` endpoints.
195+
The concept of Digital Twin is demonstrated with the LightDB State via the ``state`` path.
193196

194197
.. code-block:: json
195198
196199
{
197-
"desired": {
198-
"reset_cumulative": false
199-
},
200200
"state": {
201201
"cumulative": {
202202
"ch0": 138141,
@@ -209,12 +209,8 @@ The concept of Digital Twin is demonstrated with the LightDB State via the ``des
209209
}
210210
}
211211
212-
* ``desired.reset_cumulative`` values may be changed from the cloud side. The device will recognize
213-
when this endpoint is set to ``true``, clearing the stored ``cumulative`` values and writing the
214-
``reset_cumulative`` value to ``false`` to indicate the operation was completed.
215-
216-
* ``actual`` values will be updated by the device. The cloud may read the ``actual`` endpoints to
217-
determine device status, but only the device should ever write to the ``actual`` endpoints.
212+
* ``state`` values will be updated by the device. The cloud may read the ``state`` path to
213+
determine device status, but only the device should ever write to that path.
218214

219215
Hardware Variations
220216
*******************

src/app_rpc.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ LOG_MODULE_REGISTER(app_rpc, LOG_LEVEL_DBG);
1414
#include <zephyr/sys/reboot.h>
1515

1616
#include <network_info.h>
17+
#include "main.h"
18+
#include "app_sensors.h"
1719
#include "app_rpc.h"
1820

1921
static void reboot_work_handler(struct k_work *work)
@@ -96,6 +98,17 @@ static enum golioth_rpc_status on_reboot(zcbor_state_t *request_params_array,
9698
return GOLIOTH_RPC_OK;
9799
}
98100

101+
static enum golioth_rpc_status on_reset_cumulative(zcbor_state_t *request_params_array,
102+
zcbor_state_t *response_detail_map,
103+
void *callback_arg)
104+
{
105+
LOG_INF("Request to reset cumulative values received. Processing now.");
106+
reset_cumulative_totals();
107+
wake_system_thread();
108+
109+
return GOLIOTH_RPC_OK;
110+
}
111+
99112
static void rpc_log_if_register_failure(int err)
100113
{
101114
if (err) {
@@ -115,6 +128,9 @@ void app_rpc_register(struct golioth_client *client)
115128
err = golioth_rpc_register(rpc, "reboot", on_reboot, NULL);
116129
rpc_log_if_register_failure(err);
117130

131+
err = golioth_rpc_register(rpc, "reset_cumulative", on_reset_cumulative, NULL);
132+
rpc_log_if_register_failure(err);
133+
118134
err = golioth_rpc_register(rpc, "set_log_level", on_set_log_level, NULL);
119135
rpc_log_if_register_failure(err);
120136
}

src/app_state.c

Lines changed: 1 addition & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2424
static struct golioth_client *client;
2525
static 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-
7742
static 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-
205116
int 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

Comments
 (0)