Major update of ThingSet Data Object structure for v0.5
The structure has been changed to match the new ThingSet v0.5 spec which is more suitable to be used in mobile phone apps because the data is structured based on logical subsystems (Battery, Load) and not on data type (conf, input, etc.) anymore. This required some updates in the ThingSet library aswell. Especially the update mechanism for configuration data had to be changed. The data_objects_parser script is now able to extract the units itself, reducing the commenting effort. All IDs have been kept the same, so previously stored settings in charge controllers NVM should be compatible.
This commit is contained in:
@@ -17,4 +17,3 @@ build
|
||||
.pio
|
||||
bin
|
||||
html
|
||||
scripts
|
||||
|
||||
@@ -26,6 +26,7 @@ CONFIG_LOG=y
|
||||
CONFIG_LOG_MODE_MINIMAL=y
|
||||
|
||||
CONFIG_THINGSET=y
|
||||
CONFIG_THINGSET_NESTED_JSON=y
|
||||
|
||||
# ThingSet protocol interface via UART serial
|
||||
CONFIG_THINGSET_SERIAL=y
|
||||
|
||||
+724
-834
File diff suppressed because it is too large
Load Diff
+12
-14
@@ -16,21 +16,19 @@
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* Categories / first layer node IDs
|
||||
* Groups / first layer data object IDs
|
||||
*/
|
||||
#define ID_ROOT 0x00
|
||||
#define ID_INFO 0x01 // read-only device information (e.g. manufacturer, device ID)
|
||||
#define ID_MEAS 0x02 // output data (e.g. measurement values)
|
||||
#define ID_STATE 0x03 // recorded data (history-dependent)
|
||||
#define ID_REC 0x04 // recorded data (history-dependent)
|
||||
#define ID_INPUT 0x05 // input data (e.g. set-points)
|
||||
#define ID_CONF 0x06 // configurable settings
|
||||
#define ID_CAL 0x07 // calibration
|
||||
#define ID_REPORT 0x0A // reports
|
||||
#define ID_RPC 0x0E // remote procedure calls
|
||||
#define ID_DFU 0x0F // device firmware upgrade
|
||||
#define ID_PUB 0x100 // publication setup
|
||||
#define ID_CTRL 0x8000 // control functions
|
||||
#define ID_ROOT 0x00
|
||||
#define ID_DEVICE 0x01
|
||||
#define ID_BATTERY 0x02
|
||||
#define ID_CHARGER 0x03
|
||||
#define ID_SOLAR 0x04
|
||||
#define ID_LOAD 0x05
|
||||
#define ID_USB 0x06
|
||||
#define ID_NANOGRID 0x07
|
||||
#define ID_DFU 0x0F
|
||||
#define ID_PUB 0x100
|
||||
#define ID_CTRL 0x8000
|
||||
|
||||
/*
|
||||
* Subset definitions for statements and publish/subscribe
|
||||
|
||||
@@ -117,6 +117,10 @@ void can_isotp_thread()
|
||||
LOG_INF("Got %d bytes via ISO-TP. Processing ThingSet message.", req_len);
|
||||
resp_len = ts.process(rx_buffer, req_len, tx_buffer, sizeof(tx_buffer));
|
||||
LOG_DBG("TX buf: %x %x %x %x", tx_buffer[0], tx_buffer[1], tx_buffer[2], tx_buffer[3]);
|
||||
|
||||
if (ts.check_updated(SUBSET_NVM, true)) {
|
||||
data_objects_update_conf();
|
||||
}
|
||||
}
|
||||
else {
|
||||
tx_buffer[0] = TS_STATUS_INTERNAL_SERVER_ERR;
|
||||
|
||||
@@ -39,7 +39,7 @@ static struct k_sem rx_buf_mutex; // binary semaphore used as mutex in ISR conte
|
||||
|
||||
extern ThingSet ts;
|
||||
|
||||
const char serial_subset_path[] = "serial";
|
||||
const char serial_subset_path[] = "mSerial";
|
||||
static ThingSetDataObject *serial_subset;
|
||||
|
||||
void serial_pub_msg()
|
||||
@@ -65,6 +65,11 @@ void serial_process_command()
|
||||
uart_poll_out(uart_dev, tx_buf[i]);
|
||||
}
|
||||
uart_poll_out(uart_dev, '\n');
|
||||
|
||||
if (ts.check_updated(SUBSET_NVM, true)) {
|
||||
// ToDo: This could take a few milliseconds, better offload from ISR context
|
||||
data_objects_update_conf();
|
||||
}
|
||||
}
|
||||
|
||||
// release buffer and start waiting for new commands
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
# Parser to create JSON file containing data object metadata
|
||||
#
|
||||
# TODO
|
||||
# - Parse nesting in .pub/serial/Enable properly (needs to take parent IDs into account)
|
||||
# - Parse nesting in _pub/serial/Enable properly (needs to take parent IDs into account)
|
||||
# - Also add metadata to groups (using "self" key)
|
||||
|
||||
import json
|
||||
@@ -14,15 +14,22 @@ output_path = "cc-05.json" # version has to match DATA_OBJECTS_VERSION
|
||||
data_objects = {}
|
||||
id_macros = {}
|
||||
|
||||
# used to make units provided by ThingSet object names nicer
|
||||
unit_fix = {
|
||||
'degC': '°C',
|
||||
'degF': '°F',
|
||||
'pct': '%',
|
||||
}
|
||||
|
||||
# get IDs which are defined as macros
|
||||
with open("src/data_objects.h", 'r') as fd:
|
||||
with open("app/src/data_objects.h", 'r') as fd:
|
||||
for (num, line) in enumerate(fd, 1):
|
||||
if "#define ID_" in line:
|
||||
id_macros[line.split()[1]] = int(line.split()[2], base=16)
|
||||
continue
|
||||
|
||||
# get actual metadata from data_objects file
|
||||
with open("src/data_objects.cpp", 'r') as fd:
|
||||
with open("app/src/data_objects.cpp", 'r') as fd:
|
||||
json_str = ""
|
||||
group_name = ""
|
||||
save_lines = False
|
||||
@@ -51,31 +58,60 @@ with open("src/data_objects.cpp", 'r') as fd:
|
||||
if save_name:
|
||||
try:
|
||||
name = line.strip().split(",")[1].strip(' "')
|
||||
data_objects[group_name][name] = {}
|
||||
obj_id = line.split("(")[1].split(",")[0]
|
||||
id_parsed = line.split("(")[1].split(",")[0]
|
||||
|
||||
try:
|
||||
data_objects[group_name][name]["id"] = int(obj_id, base=16)
|
||||
data_objects[group_name][name]["idx"] = str(obj_id)
|
||||
obj_id = int(id_parsed, base=16)
|
||||
obj_idx = str(id_parsed)
|
||||
except ValueError:
|
||||
data_objects[group_name][name]["id"] = id_macros[obj_id]
|
||||
data_objects[group_name][name]["idx"] = hex(id_macros[obj_id])
|
||||
data_objects[group_name][name].update(json.loads(json_str))
|
||||
obj_id = id_macros[id_parsed]
|
||||
obj_idx = hex(id_macros[id_parsed])
|
||||
|
||||
if group_name == "":
|
||||
data_objects[name] = {}
|
||||
data_objects[name]["id"] = obj_id
|
||||
data_objects[name]["idx"] = obj_idx
|
||||
data_objects[name].update(json.loads(json_str))
|
||||
else:
|
||||
data_objects[group_name][name] = {}
|
||||
data_objects[group_name][name]["id"] = obj_id
|
||||
data_objects[group_name][name]["idx"] = obj_idx
|
||||
data_objects[group_name][name].update(json.loads(json_str))
|
||||
|
||||
except Exception as e:
|
||||
print("Error while parsing block between lines {} and {}".format(beginning_line, num))
|
||||
print(e)
|
||||
exit(-1)
|
||||
|
||||
save_name = False
|
||||
json_str = ""
|
||||
|
||||
def add_missing_fields(obj, name):
|
||||
if not 'unit' in obj:
|
||||
chunks = name.split('_')
|
||||
if len(chunks) > 2 and name[0] != '_':
|
||||
obj['unit'] = chunks[1] + '/' + chunks[2]
|
||||
elif len(chunks) > 1 and name[0] != '_':
|
||||
obj['unit'] = chunks[1]
|
||||
else:
|
||||
obj['unit'] = None
|
||||
if obj['unit'] in unit_fix:
|
||||
obj['unit'] = unit_fix[obj['unit']]
|
||||
if not 'min' in obj:
|
||||
obj['min'] = None
|
||||
if not 'max' in obj:
|
||||
obj['max'] = None
|
||||
return obj
|
||||
|
||||
# add missing fields
|
||||
for category in data_objects:
|
||||
for item in data_objects[category]:
|
||||
if not 'unit' in data_objects[category][item]:
|
||||
data_objects[category][item]['unit'] = None
|
||||
if not 'min' in data_objects[category][item]:
|
||||
data_objects[category][item]['min'] = None
|
||||
if not 'max' in data_objects[category][item]:
|
||||
data_objects[category][item]['max'] = None
|
||||
for obj_name in data_objects:
|
||||
if obj_name[0].islower():
|
||||
# data item at root level
|
||||
data_objects[obj_name] = add_missing_fields(data_objects[obj_name], obj_name)
|
||||
else:
|
||||
# group
|
||||
for item in data_objects[obj_name]:
|
||||
data_objects[obj_name][item] = add_missing_fields(data_objects[obj_name][item], item)
|
||||
|
||||
# save result
|
||||
with open(output_path, "w", encoding='utf8') as filename:
|
||||
|
||||
Reference in New Issue
Block a user