As an example we are going to add a new generic device to measure the temperature named "temperatureStation". Go to jsondef.h approx in line 960 and create the structure that contains the information you want to use.
1 struct temperatureStationStruc
3 //! Generic info must be here for every device
5 //! the following is any data specific to this device
6 float temperature; // your temperature data will be stored here
add your temperatureStationStruc structure to the devicestruc union (apporox in line 1400)
9 temperatureStationStruc temperatureStation; // << --- add here
add your temperatureStationStruc structure to the devspecstruc structure (apporox in line 1500)
7 uint16_t temperatureStation_cnt; // << --- add here
10 vector<thststruc *>thst;
11 vector<tsenstruc *>tsen;
12 vector<temperatureStationStruc *>temperatureStation; // << --- add here
now go to jsonlib.cpp , add your temperatureStation to the end of device_type_string
1 vector <string> device_type_string
6 "temperatureStation" // <-- add here
in jsondef.h you also must add the device type to the end of device_type\ enum (approx in line 400)
8 //! your tempStation here
9 DEVICE_TYPE_TEMPSTATION = 27, // <- add here
13 DEVICE_TYPE_NONE=65535
now we are going to modify some functions in the code. The first one is json_detroy in jsonlib.cpp
1 void json_destroy(cosmosstruc *cdata)
3 for (uint16_t i=0; i<2; ++i)
5 cdata[i].devspec.ant.resize(0);
7 cdata[i].devspec.tsen.resize(0);
8 cdata[i].devspec.temperatureStation.resize(0); // <- add here
9 cdata[i].device.resize(0);
(side note: for a really complex type further definitions must be added to the namespace, but most common types are already supported, so this is an advanced feature)
go to json_devices_specific and inside the for loop that goes over each type add some of the following
1 const char *json_devices_specific(string &jstring, cosmosstruc *cdata)
5 for (uint16_t j=0; j<*cnt; ++j)
9 // Dump Temperature Station
10 if (!strcmp(device_type_string[i].c_str(),"tempStation"))
12 json_out_1d(jstring,(char *)"device_tempStation_temperature",j,cdata);
13 json_out_character(jstring, '\n');
go to json_clone
1 int32_t json_clone(cosmosstruc *cdata)
4 case DEVICE_TYPE_TEMPSTATION:
5 cdata[1].devspec.tempStation[cdata[1].device[i].all.gen.didx] =
6 &cdata[1].device[i].tempStation;
add name for the device count in json_addbaseentry
1 uint16_t json_addbaseentry(cosmosstruc *cdata)
5 json_addentry("device_tempStation_cnt",
8 offsetof(devspecstruc,tempStation_cnt),
9 COSMOS_SIZEOF(uint16_t),
10 (uint16_t)JSON_TYPE_UINT16,
to json_adddeviceentry add
1 uint16_t json_adddeviceentry(uint16_t i, cosmosstruc *cdata)
5 case DEVICE_TYPE_TEMPSTATION:
7 json_addentry("device_tempStation_utc",
10 (ptrdiff_t)offsetof(genstruc,utc)+i*sizeof(devicestruc),
11 COSMOS_SIZEOF(double),
12 (uint16_t)JSON_TYPE_DOUBLE,
16 json_addentry("device_tempStation_temperature",
19 (ptrdiff_t)offsetof(temperatureStationStruc,temperature) +
20 i*sizeof(devicestruc),
21 COSMOS_SIZEOF(double),
22 (uint16_t)JSON_TYPE_DOUBLE,
26 cdata[0].devspec.tempStation.push_back(
27 (temperatureStationStruct *)&cdata[0].device[i].tempStation);
28 cdata[0].devspec.tempStation_cnt =
29 (uint16_t)cdata[0].devspec.tempStation.size();