XML data formatted with properties doesn't deserialize using the .NET DataContract serializer, so it would be advantageous to have correctly formatted JSON historic data.
When requesting historic data via REST api in JSON format, e.g:
Response data seems to be missing channel property information (name/ID):
{"prtg-version":"14.2.9.1690","treesize":24,"histdata":[{"datetime":"8/14/2014 12:00:00 AM - 1:00:00 AM","datetime_raw":41865.2083333333,"value":-31.03,"value_raw":-31.0275,"value":-62.05,"value_raw":-62.0550,"value":0.00,"value_raw":0.0000,"coverage":"100 %","coverage_raw":10000},{"datetime":"8/14/2014 1:00:00 AM - 2:00:00 AM","datetime_raw":41865.2500000000,"value":-31.03,"value_raw":-31.0300,"value":-62.06,"value_raw":-62.0600,"value":0.00,"value_raw":0.0000,"coverage":"100 %","coverage_raw":10000}, ... ]}
XML response content for similar request is :
<?xml version="1.0" encoding="UTF-8"?> <histdata> <prtg-version>14.2.9.1690</prtg-version> <item> <datetime>8/14/2014 12:00:00 AM - 1:00:00 AM</datetime> <datetime_raw>41865.2083333333</datetime_raw> <value channel="Avg" channelid="1">-31.03 </value> <value_raw channel="Avg" channelid="1">-31.0275</value_raw> <value channel="Min" channelid="2">-62.05 </value> <value_raw channel="Min" channelid="2">-62.0550</value_raw> <value channel="Max" channelid="3">0.00 </value> <value_raw channel="Max" channelid="3">0.0000</value_raw> <coverage>100 %</coverage> <coverage_raw>0000010000</coverage_raw> </item> <item> <datetime>8/14/2014 1:00:00 AM - 2:00:00 AM</datetime> <datetime_raw>41865.2500000000</datetime_raw> <value channel="Avg" channelid="1">-31.03 </value> <value_raw channel="Avg" channelid="1">-31.0300</value_raw> <value channel="Min" channelid="2">-62.06 </value> <value_raw channel="Min" channelid="2">-62.0600</value_raw> <value channel="Max" channelid="3">0.00 </value> <value_raw channel="Max" channelid="3">0.0000</value_raw> <coverage>100 %</coverage> <coverage_raw>0000010000</coverage_raw> </item> ...
I'm currently using a hack to deserialize the JSON, if anyone is interested it looks like this (ChannelType is a custom enum - could use an integer channel ID instead, depends on channel numbering 1 .. n matching order of raw data in the JSON response)
[DataContract] public class SensorHistoricData { [DataMember(Name = "histdata")] public HistoricDataPoint[] DataPoints { get; set; } } [DataContract] public class HistoricDataPoint { ... (other properties - coverage, datetime) [IgnoreDataMember] private ChannelType _channelType = ChannelType.None; /// <summary> /// HACK to accumulate channel data from badly formatted JSON /// </summary> [DataMember(Name = "value_raw")] public decimal? AddRawData { get { return null; } private set { _channelType += 1; if (value.HasValue) { ChannelData = new List<HistoricChannelData>(ChannelData ?? new HistoricChannelData [] {}) { new HistoricChannelData {ChannelType = _channelType, Data = value.Value} }.ToArray(); } } } [IgnoreDataMember] public HistoricChannelData[] ChannelData { get; set; } }
Add comment