-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclsLocalNetHistoValues.c
412 lines (363 loc) · 11.9 KB
/
clsLocalNetHistoValues.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the ig-logger project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Original author: Christian Kaiser <[email protected]>
*/
#include <gpfprot.hpp>
#include "clsWatchdog.h"
bool clsLocalNet::QueryHistoricalData(const clsDate& Date, clsDeviceDataList* pDataList)
{
#if GPFPROTECT
clsGPFProtection GPF(GPFPROTFLAG_TRACE|GPFPROTFLAG_MSGBOX);
if (GPF.Error())
{
exit(255);
}
#endif
bool bRes(false);
clsResult<bool> Result(String::Format("clsLocalNet::clsLocalNetQueryHistoricalData",Date.Year(),Date.Month(),Date.Day()),bRes);
clsIGMessage MsgQuery( IGID_PC,
IGID_DATALOGGER,
clsIGMessage::IGCMD_GET_HISTORICALDATA,
3,
Date.Year()-2000,
Date.Month(),
Date.Day());
clsIGMessage MsgData( IGID_DATALOGGER,
IGID_PC,
clsIGMessage::IGCMD_DATA_HISTORICALDATA);
clsIGMessage MsgResponse;
bool bFinished(false);
unsigned nSizeToRead(0);
BYTE* pData(NULL);
unsigned nData(0);
unsigned nExpectedBlockID(0);
Log(">%-8s: %s","query", (LPCTSTR)MsgQuery.AsString());
bRes = Query(MsgQuery,MsgData,MsgResponse);
while (bRes && !bFinished)
{
if (bUserAbortRequested())
{
delete[] pData;
return(false);
}
// printf("Response %s\n",(LPCTSTR)MsgResponse.AsString());
const BYTE* pPayload = MsgResponse.Payload();
unsigned nID = GetValueFromPayloadBE(1,pPayload);
unsigned nUnk16 = 0;
unsigned nBlockID = 0;
unsigned nBlockCount = 0;
switch (nID)
{
case 0x80: // size follows
if (pData != NULL)
{
// must be the first block
delete[] pData;
printf("ERR: multiple 'begin' blocks are unexpected\n");
return(false);
}
nSizeToRead = GetValueFromPayloadBE(4,pPayload);
if (nSizeToRead > 0x100000)
{
// I assume no data is that large
delete[] pData;
printf("ERR: requested buffer size too large (%d bytes)\n",nSizeToRead);
return(false);
}
pData = new BYTE[nSizeToRead];
memset(pData,0,nSizeToRead);
nUnk16 = GetValueFromPayloadBE(2,pPayload);
// printf("START info: %d bytes to read\n",nSizeToRead);
MsgData.SetPayload(1,nID);
bRes = Query(MsgData,MsgResponse);
break;
case 0x81: // block follows
nBlockID = GetValueFromPayloadBE(2,pPayload);
nBlockCount = GetValueFromPayloadBE(1,pPayload);
// printf("BLOCK info: %d, consisting of %d blocks\n",nBlockID,nBlockCount);
if (nBlockID != nExpectedBlockID)
{
delete[] pData;
printf("ERR: block numbers not consecutive - try again later\n");
return(false);
}
if (nBlockCount > 512)
{
delete[] pData;
printf("ERR: block count too large (%d bytes)\n",nBlockCount);
return(false);
}
++nExpectedBlockID;
MsgData.SetPayload(3,0x81,LOBYTE(nBlockID),HIBYTE(nBlockID));
{
WCValSList<clsIGMessage> MsgList;
bRes = QueryMultiple(MsgData,MsgList,nBlockCount+1);
if (bRes)
{
MsgResponse = MsgList.get(nBlockCount);
// which has an ID of 82, which means 'end of block'
}
while (!MsgList.isEmpty() &&
nData < nSizeToRead)
{
clsIGMessage Msg(MsgList.get());
INT nSize = (INT)Msg.PayloadSize() - 1;
if (nSize <= 0)
{
delete[] pData;
printf("ERR: data too small (%d) - internal inconsistency",nSize);
return(false);
}
if (nData + nSize > nSizeToRead)
{
delete[] pData;
printf("ERR: data too large - internal inconsistency (%d-%d)",nData + nSize,nSizeToRead);
return(false);
}
memcpy(pData+nData,Msg.Payload()+1,min(nSize,nSizeToRead-nData));
nData += nSize;
}
printf(" %3d %% complete\n",MulDiv(nData,100,nSizeToRead));
}
break;
case 0x82: // end of block
{
// printf("BLOCK END info\n");
clsIGMessage MsgResponseOK(MsgResponse);
MsgResponseOK.AppendPayload(1,0xff); // OK
bRes = Query(MsgResponseOK,MsgResponse);
}
__Watchdog.Reset();
break;
case 0x83: // end of transmission
{
UINT16 nChecksum = (UINT16)GetValueFromPayloadBE(2,pPayload);
clsIGMessage MsgAck( IGID_PC,
IGID_DATALOGGER,
clsIGMessage::IGCMD_ACK_HISTORICALDATA);
bRes = Query(MsgAck,MsgResponse);
bFinished = true;
break;
}
default:
printf("ERR: unknown block type 0x%02x\n",nID);
delete[] pData;
bFinished = true;
return(false);
}
}
// clsFileRW("histo.dat").Write(pData,nData);
if (nData != nSizeToRead)
{
delete[] pData;
printf("ERR: read too few bytes\n");
return(false);
}
if (bFinished)
{
const BYTE* pCur = pData;
unsigned nUnk = GetValueFromPayloadLE(2,pCur); // 0800
unsigned nBlocksize = GetValueFromPayloadLE(2,pCur); // 0108
// printf("nBlocksize=0x%x\n",nBlocksize);
for (int nBlock = 1; nBlock * nBlocksize < nData; ++nBlock)
{
__Watchdog.Reset();
const BYTE* pDataBase = pData + nBlock * nBlocksize;
pCur = pDataBase;
unsigned nInfo = GetValueFromPayloadLE(2,pCur);
// printf(" %#p: info %04x\n",pCur - pData,nInfo);
switch (nInfo)
{
// default:
// printf("unknown info type %x\n",nInfo);
case 0x004f:
// only 0x0f/0x05
continue;
default:
case 0x0100:
case 0x0105:
case 0x0101:
case 0x0000:
{
unsigned nYear = GetValueFromPayloadLE(1,pCur) + 2000;
unsigned nMonth = GetValueFromPayloadLE(1,pCur);
unsigned nDay = GetValueFromPayloadLE(1,pCur);
while (true)
{
const BYTE* pCur2 = pCur;
BYTE nLength = (BYTE)GetValueFromPayloadLE(1,pCur2);
if (nLength == 0xff)
break;
pCur += nLength;
enLocalNetID nSourceID = (enLocalNetID)GetValueFromPayloadLE(1,pCur2);
enLocalNetType nSourceType = (enLocalNetType)GetValueFromPayloadLE(1,pCur2);
clsLocalNetID Device(nSourceID,nSourceType);
unsigned nHour = GetValueFromPayloadLE(1,pCur2);
unsigned nMinute = GetValueFromPayloadLE(1,pCur2);
clsSYSTEMTIME Time(nYear, nMonth, nDay, nHour, nMinute);
if (Time.bValid())
{
const BYTE* pData = pCur2;
#if 0
String sData;
while (pCur2 < pCur)
{
sData.AddStrSeparated(" ",String::Format("%02x",GetValueFromPayloadLE(1,pCur2)));
}
if (IsInverter(Device) ||
IsSensorcard(Device))
{
printf(" %s %04d-%02d-%02d %02d:%02d: %s\n",
(LPCTSTR)sLocalNetID(Device),
nYear, nMonth, nDay,
nHour, nMinute, (LPCTSTR)sData);
}
else
{
printf(" unk(%02x/%02x): %04d-%02d-%02d %02d:%02d: %s\n",
nSourceID, nSourceType,
nYear, nMonth, nDay,
nHour, nMinute, (LPCTSTR)sData);
}
#endif
pCur2 = pData;
unsigned nType = GetValueFromPayloadLE(1,pCur2);
switch (nType)
{
default:
case 0x25:
case 0x34:
if (IsInverter(Device))
{
unsigned nUnk1 = GetValueFromPayloadLE(1,pCur2);
switch (nUnk1)
{
case 0x01: // OK
break;
case 0x0b: // no output
break;
}
unsigned nDiv = GetValueFromPayloadLE(2,pCur2);
unsigned nUnk2 = GetValueFromPayloadLE(1,pCur2);
unsigned nPower = GetValueFromPayloadLE(3,pCur2);
UINT16 nACVoltageTimes10 = (UINT16)GetValueFromPayloadLE(2,pCur2);
UINT16 nDCVoltageTimes10 = (UINT16)GetValueFromPayloadLE(2,pCur2);
UINT16 nDCCurrent = (UINT16)GetValueFromPayloadLE(2,pCur2);
BYTE nImpedance = (BYTE)GetValueFromPayloadLE(1,pCur2);
BYTE nUnk3 = (BYTE)GetValueFromPayloadLE(1,pCur2);
double dPower = (double)nPower / nDiv;
double dACVoltage = (nACVoltageTimes10 != 0xffff) ? nACVoltageTimes10 / 10.0 : 0.0;
double dACCurrent = (dACVoltage > 0 ? dPower / dACVoltage : 0.0);
double dDCCurrent = (nDCCurrent != 0xffff) ? nDCCurrent / 100.0 : 0.0;
double dDCVoltage = (nDCVoltageTimes10 != 0xffff) ? nDCVoltageTimes10 / 10.0 : 0.0;
double dImpedance = (nImpedance != 0xff) ? nImpedance / 100.0 : 0.0;
// printf(" P=%.1fW --- AC=%.1fV, %.2fA --- DC=%.1fV, %.2fA--- R0=%.2f\n",dPower,dACVoltage,dACCurrent,dDCVoltage,dDCCurrent,dImpedance);
if (pDataList)
{
clsDataItemList Data;
Data.insert("State", clsDataItem(dPower > 0 ? clsVariant(2) : clsVariant::vNULL(),""));
Data.insert("Power", clsDataItem(dPower > 0 ? clsVariant(dPower) : clsVariant::vNULL(),"W"));
Data.insert("ACVoltage",clsDataItem(dACVoltage > 0 ? clsVariant(dACVoltage) : clsVariant::vNULL(),"V"));
Data.insert("DCVoltage",clsDataItem(dDCVoltage > 0 ? clsVariant(dDCVoltage) : clsVariant::vNULL(),"V"));
Data.insert("ACCurrent",clsDataItem(dACCurrent > 0 ? clsVariant(dACCurrent) : clsVariant::vNULL(),"A"));
Data.insert("DCCurrent",clsDataItem(dDCCurrent > 0 ? clsVariant(dDCCurrent) : clsVariant::vNULL(),"A"));
Data.insert("Impedance",clsDataItem(dImpedance > 0 ? clsVariant(dImpedance) : clsVariant::vNULL(),"Ohm"));
pDataList->Add(Device,Time,Data);
}
}
if (IsSensorcard(Device))
{
unsigned nUnk1 = GetValueFromPayloadLE(1,pCur2);
switch (nUnk1)
{
case 0x01: // OK
break;
case 0x0b: // no output
break;
}
unsigned nUnknown = GetValueFromPayloadLE(2,pCur2);
unsigned nMask = GetValueFromPayloadLE(1,pCur2);
static LPCTSTR __pszDescr[] =
{
"Temp1",
"Temp2",
"Irradiation",
"Digi1",
"Digi2",
"Current",
};
static LPCTSTR __pszUnit[] =
{
"deg",
"deg",
"W/m^2",
"km/h",
"?",
"?",
};
clsDataItemList Data;
for (int nValue = 0; nValue < 6; ++nValue)
{
// printf("MASK=%x\n",nMask);
if (nMask & (1 << nValue))
{
clsIGValue v(1 << nValue,4,__pszDescr[nValue],pCur2);
// printf(" %s=%s\n",(LPCTSTR)v.pszName(),(LPCTSTR)v.sValueEx());
if (pDataList)
{
Data.insert(v.pszName(),
clsDataItem(clsVariant(v.dValueEx()),__pszUnit[nValue]));
}
}
}
if (pDataList)
{
pDataList->Add(Device,Time,Data);
}
}
break;
case 0x45: // error or such?
{
unsigned nUnk1 = GetValueFromPayloadLE(1,pCur2);
unsigned nUnk2 = GetValueFromPayloadLE(1,pCur2);
UINT16 nDCVoltageTimes10 = (UINT16)GetValueFromPayloadLE(2,pCur2);
double dDCVoltage = (nDCVoltageTimes10 != 0xffff) ? nDCVoltageTimes10 / 10.0 : 0.0;
switch (nUnk1)
{
case 0x82: // OK
switch (nUnk2)
{
case 0x13: // DCVoltage
case 0x23: // DCVoltage
if (pDataList)
{
clsDataItemList Data;
Data.insert("State", clsDataItem(clsVariant::vNULL(),""));
Data.insert("DCVoltage",clsDataItem(dDCVoltage > 0 ? clsVariant(dDCVoltage) : clsVariant::vNULL(),"V"));
pDataList->Add(Device,Time,Data);
}
break;
}
break;
}
}
break;
}
}
}
}
break;
}
}
}
delete[] pData;
pDataList = pDataList;
return(bRes = true);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */