Difference between revisions of "How to report data with timestamp"

From ESS-WIKI
Jump to: navigation, search
Line 177: Line 177:
 
buffer = IoT_PrintCapability(myCapability);
 
buffer = IoT_PrintCapability(myCapability);
 
printf("IoT Format:\r\n%s\r\n", buffer);
 
printf("IoT Format:\r\n%s\r\n", buffer);
 +
free(buffer);
 +
}
 +
{//print report data for older MessageGenerator Library
 +
int size = 9;
 +
char* filter[] ={"n", "bn", "v","sv","bv","id","StatusCode","sessionID", "$date"};
 +
 +
buffer = MSG_PrintWithFiltered(myCapability,filter,size);
 +
printf("Report Data:\r\n%s\r\n", buffer);
 
free(buffer);
 
free(buffer);
 
}
 
}

Revision as of 06:23, 3 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"
	}
}
Report Data Sample:
{
	"HWM": {
		"e": [
			{
				"n": "cpu1_volt",
				"v": 10
			},
			{
				"n": "cpu2_volt",
				"v": 10
			},
			{
				"n": "cpu1_temp",
				"v": 10
			},
			{
				"n": "timestamp",
				"v": 1508998354
			}
		],
		"bn": "HWM"
	}
}
In this case, the report data carried timestamp as sensor data. But server process the timestamp as a single sensor data and cannot related to others snesor data. So user cannot query cpu1_volt with timestamp.

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}
}
Report Data Sample with Timestamp
{
	"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);
		}
		{//print report data for older MessageGenerator Library
			int size = 9;
			char* filter[] ={"n", "bn", "v","sv","bv","id","StatusCode","sessionID", "$date"};
	
			buffer = MSG_PrintWithFiltered(myCapability,filter,size);
			printf("Report Data:\r\n%s\r\n", buffer);
			free(buffer);
		}
output json:
{
	"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
	}
}