How to report data with timestamp
From ESS-WIKI
Revision as of 09:47, 2 November 2017 by Scott68.chang (talk | contribs)
Situation
In Handler, user can attach their timestamp into report data and capability with tag 'opTS'.
In original capability and report data, user only describe and report the sensor data without carry any timestamp or carried as a sensor data as below.
Capability Sample:
{
"HWM": {
"e": [
{
"n": "cpu1_volt",
"type": "volt",
"u": "V",
"max": 20,
"min": 10
},
{
"n": "cpu2_volt",
"type": "volt",
"u": "V",
"max": 20,
"min": 10
},
{
"n": "cpu1_temp",
"type": "temp",
"u": "Cel",
"max": 100,
"min": 10
},
{
"n": "timestamp",
"type": "tick",
"u": "millisec"
}
],
"bn": "HWM"
}
}
{
"HWM": {
"e": [
{
"n": "cpu1_volt",
"v": 10
},
{
"n": "cpu2_volt",
"v": 10
},
{
"n": "cpu1_temp",
"v": 10
},
{
"n": "timestamp",
"v": 1508998354
}
],
"bn": "HWM"
}
}
Solution
For RMM 3.3, user can carried the timestamp as a special tag 'opTS' in the capability or report, the server will store the timestamp related with each sensor data. The 'opTS' format is "opTS":{"$date":1508998354000}. The '$date' must be the milli seconds since Jan 01 1970 (UTC).
Capability Sample with Timestamp
{
"HWM": {
"e": [
{
"n": "cpu1_volt",
"type": "volt",
"u": "V",
"max": 20,
"min": 10
},
{
"n": "cpu2_volt",
"type": "volt",
"u": "V",
"max": 20,
"min": 10
},
{
"n": "cpu1_temp",
"type": "temp",
"u": "Cel",
"max": 100,
"min": 10
},
{
"n": "timestamp",
"type": "tick",
"u": "millisec"
}
],
"bn": "HWM"
},
"opTS":{"$date":1508998354000}
}
{
"HWM": {
"e": [
{
"n": "cpu1_volt",
"v": 10
},
{
"n": "cpu2_volt",
"v": 10
},
{
"n": "cpu1_temp",
"v": 10
},
{
"n": "timestamp",
"v": 1508998354
}
],
"bn": "HWM"
},
"opTS":{"$date":1508998354000}
}
Sample Code
MSG_CLASSIFY_T *myCapability = IoT_CreateRoot("Test");
MSG_CLASSIFY_T *myVoltGroup = IoT_AddGroup(myCapability, "Voltage");
MSG_CLASSIFY_T *myTempGroup = IoT_AddGroup(myCapability, "Temperature");
MSG_ATTRIBUTE_T *attrVolt1 = NULL, *attrVolt2 = NULL, *attrTemp1 = NULL, *attrTemp2 = NULL;
MSG_ATTRIBUTE_T* attr = IoT_AddGroupAttribute(myVoltGroup, "bu");
if(attr)
IoT_SetStringValue(attr, "V", IoT_READONLY);
attr = IoT_AddGroupAttribute(myTempGroup, "bu");
if(attr)
IoT_SetStringValue(attr, "Celsius", IoT_READONLY);
attrVolt1 = IoT_AddSensorNode(myVoltGroup, "5V");
if(attrVolt1)
IoT_SetFloatValueWithMaxMin(attrVolt1, 4.9, IoT_READONLY, 5.5, 4.5, "V");
attrVolt2 = IoT_AddSensorNode(myVoltGroup, "12V");
if(attrVolt2)
IoT_SetFloatValueWithMaxMin(attrVolt2, 11, IoT_READONLY, 10, 14, "V");
attrTemp1 = IoT_AddSensorNode(myTempGroup, "CPU");
if(attrTemp1)
IoT_SetFloatValue(attrTemp1, 65, IoT_READONLY, "Celsius");
attrTemp2 = IoT_AddSensorNode(myTempGroup, "SYS");
if(attrTemp2)
IoT_SetFloatValue(attrTemp2, 50, IoT_READONLY, "Celsius");
//attr = IoT_AddOPTSNode(myCapability); //supported at MessageGenerator V1.0.4
attr = MSG_FindIoTSensor(myCapability, "opTS");
if(!attr)
attr = MSG_AddJSONAttribute(myCapability, "opTS");
if(attr)
IoT_SetDateValue(attr, "1509611813000", IoT_READONLY);
if(MSG_IsAttributeExist(myCapability, "Test/Voltage/5V", true))
{
buffer = IoT_PrintCapability(myCapability);
printf("IoT Format:\r\n%s\r\n", buffer);
free(buffer);
}