Difference between revisions of "How to report data with timestamp"
From ESS-WIKI
Line 142: | Line 142: | ||
MSG_CLASSIFY_T *myTempGroup = IoT_AddGroup(myCapability, "Temperature"); | MSG_CLASSIFY_T *myTempGroup = IoT_AddGroup(myCapability, "Temperature"); | ||
MSG_ATTRIBUTE_T *attrVolt1 = NULL, *attrVolt2 = NULL, *attrTemp1 = NULL, *attrTemp2 = NULL; | MSG_ATTRIBUTE_T *attrVolt1 = NULL, *attrVolt2 = NULL, *attrTemp1 = NULL, *attrTemp2 = NULL; | ||
− | + | MSG_CLASSIFY_T *pOPTSNode= NULL; | |
MSG_ATTRIBUTE_T* attr = IoT_AddGroupAttribute(myVoltGroup, "bu"); | MSG_ATTRIBUTE_T* attr = IoT_AddGroupAttribute(myVoltGroup, "bu"); | ||
if(attr) | if(attr) | ||
Line 163: | Line 163: | ||
//attr = IoT_AddOPTSNode(myCapability); //supported at MessageGenerator V1.0.4 | //attr = IoT_AddOPTSNode(myCapability); //supported at MessageGenerator V1.0.4 | ||
− | + | pOPTSNode = MSG_FindClassify(myCapability, "opTS"); | |
− | + | if(!pOPTSNode) | |
− | attr = MSG_AddJSONAttribute( | + | pOPTSNode= MSG_AddJSONClassify(myCapability, "opTS", NULL, false); |
+ | |||
+ | attr = MSG_FindJSONAttribute(pOPTSNode, "$date"); | ||
+ | if(!attr) | ||
+ | attr = MSG_AddJSONAttribute(pOPTSNode, "$date"); | ||
if(attr) | if(attr) | ||
− | + | IoT_SetFloatValue(attr, 1509611813000, IoT_READONLY, NULL); | |
if(MSG_IsAttributeExist(myCapability, "Test/Voltage/5V", true)) | if(MSG_IsAttributeExist(myCapability, "Test/Voltage/5V", true)) | ||
Line 175: | Line 179: | ||
free(buffer); | free(buffer); | ||
} | } | ||
− | </syntaxhighlight> | + | </syntaxhighlight>output json:<syntaxhighlight lang="JAVA">{ |
− | output json: | ||
− | <syntaxhighlight lang="JAVA">{ | ||
− | |||
− | |||
− | |||
"Test": { | "Test": { | ||
"Voltage": { | "Voltage": { | ||
Line 222: | Line 221: | ||
] | ] | ||
} | } | ||
+ | }, | ||
+ | "opTS": { | ||
+ | "$date": 1509611798528 | ||
} | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 10:18, 2 November 2017
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_CLASSIFY_T *pOPTSNode= 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
pOPTSNode = MSG_FindClassify(myCapability, "opTS");
if(!pOPTSNode)
pOPTSNode= MSG_AddJSONClassify(myCapability, "opTS", NULL, false);
attr = MSG_FindJSONAttribute(pOPTSNode, "$date");
if(!attr)
attr = MSG_AddJSONAttribute(pOPTSNode, "$date");
if(attr)
IoT_SetFloatValue(attr, 1509611813000, IoT_READONLY, NULL);
if(MSG_IsAttributeExist(myCapability, "Test/Voltage/5V", true))
{
buffer = IoT_PrintCapability(myCapability);
printf("IoT Format:\r\n%s\r\n", buffer);
free(buffer);
}
{
"Test": {
"Voltage": {
"bn": "Voltage",
"bu": "V",
"e": [
{
"n": "5V",
"v": 4.9,
"max": 5.5,
"min": 4.5,
"asm": "r",
"u": "V"
},
{
"n": "12V",
"v": 11,
"max": 10,
"min": 14,
"asm": "r",
"u": "V"
}
]
},
"Temperature": {
"bn": "Temperature",
"bu": "Celsius",
"e": [
{
"n": "CPU",
"v": 65,
"asm": "r",
"u": "Celsius"
},
{
"n": "SYS",
"v": 50,
"asm": "r",
"u": "Celsius"
}
]
}
},
"opTS": {
"$date": 1509611798528
}
}