-
Notifications
You must be signed in to change notification settings - Fork 22
/
service.h
655 lines (526 loc) · 18.3 KB
/
service.h
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//***************************************************************************
// p4d / Linux - Heizungs Manager
// File service.h
// This code is distributed under the terms and conditions of the
// GNU GENERAL PUBLIC LICENSE. See the file LICENSE for details.
// Date 04.11.10 - 27.02.15 Jörg Wendel
//***************************************************************************
#pragma once
#include <stdio.h>
#include "lib/common.h"
#include "lib/json.h"
//***************************************************************************
// Class Froeling Service
//***************************************************************************
class FroelingService
{
public:
static const char* nameChars;
struct StateInfo
{
int code;
std::vector<const char*> titles;
};
// statics
static StateInfo stateInfos[];
static const char* toTitle(int code);
static int toState(const char* title);
static int isError(int code);
// -------------------------
// COM1 (service interface)
enum Error
{
wrnNonUpdate = -1000,
wrnOutOfRange, // -999
wrnLast, // -998
wrnEmpty, // -997
wrnTimeout, // -996
wrnSkip, // -995
errRequestFailed, // -994
errWrongAddress, // -993
errTransmissionFailed // -992
};
enum InterfaceDef1
{
commId = 0x02FD, // communication identifier
sizeId = 2,
posSize = 2,
sizeSize = 2,
sizeCommand = 1,
sizeDataMax = 254,
sizeCrc = 1, // CRC (over all bytes incl. complete header)
sizeAddress = 2,
sizeByte = 1,
sizeMaxRequest = sizeId + sizeSize + sizeCommand + sizeDataMax + sizeCrc,
sizeMaxReply = sizeMaxRequest,
maxAddresses = sizeDataMax / sizeAddress,
maxBytes = sizeDataMax / sizeByte,
//
addrUnknown = 0xFFFF
};
#pragma pack(1)
struct Header
{
word id; // 2 bytes frame start kennung stets 0x02 0xFD
word size; // 2 bytes size of data packet -> payload incl. CRC without header
byte command; // 1 byte command
};
#pragma pack()
enum Command
{
cmdUnknown = 0,
cmdCheck = 0x22, // Verbindung zur Steuerung überprüfen
cmdGetValue = 0x30, // Aktuelle Werte der Heizung
cmdGetValueListFirst = 0x31, // Liste der verfügbaren Aktuellen Werte
cmdGetValueListNext = 0x32, // "
cmdGetUnknownFirst = 0x35, // What ever :o
cmdGetUnknownNext = 0x36, //
cmdGetMenuListFirst = 0x37, // Menuestruktur auslesen
cmdGetMenuListNext = 0x38, // "
cmdSetParameter = 0x39, // Einstellung ändern
cmdGetBaseSetup = 0x40, // Konfiguration des Kessels
cmdGetVersion = 0x41, // Software Version, aktuelle Zeit und Datum
cmdGetTimesFirst = 0x42, // Zeiten abfragen
cmdGetTimesNext = 0x43, // Zeiten abfragen
cmdGetDigOut = 0x44, // Digitale Ausgänge abfragen
cmdGetAnlOut = 0x45, // Analoge Ausgänge abfragen
cmdGetDigIn = 0x46, // Digitale Eingänge abfragen
cmdGetErrorFirst = 0x47, // Fehlerpuffer abfragen
cmdGetErrorNext = 0x48, // "
cmdSetTimes = 0x50, // Zeiten ändern
cmdGetState = 0x51, // Kesselzustand abfragen
cmdSetDateTime = 0x54, // Datum und Uhrzeit einstellen
cmdGetParameter = 0x55, // Einstellung abfragen
cmdSetDigOut = 0x58, // Digitale Ausgänge manipulieren
cmdSetAnlOut = 0x59, // Analoge Ausgänge manipulieren
cmdSetDigIn = 0x5A, // Digitale Eingänge manipulieren
cmdGetForce = 0x5E, // Überprüfen ob forcen aktiv
cmdSetForce = 0x7E // Force ein/aus
};
enum UserDefined
{
udState = 1,
udMode,
udTime
};
// menu struct types (parameter types)
enum MenuStructType
{
// getValue:
mstMesswert = 0x03, // 3 value
mstMesswert1 = 0x46, // 70 value
// setParameter/getParameter:
mstPar = 0x07, // 7 number parameter
mstParDig = 0x08, // 8 boolean parameter
mstParSet = 0x32, // 50
mstParSet1 = 0x39, // 57
mstParSet2 = 0x40, // 64
mstParWeekday = 0x0b, // 11 -> TODO: check what means value 8
mstParZeit = 0x0a, // 10 hh:mm -> TODO: check in service menu if it works
mstWorkMode = 0x2f, // 47 -> TODO can set? check
// getDigitalIn/getDigitalOut/getAnalogOut:
mstDigOut = 0x11, // 17
mstAnlOut = 0x12, // 18
mstDigIn = 0x13, // 19
// ... ??
mstEmpty = 0x22, // 34
mstReset = 0x23, // 35
mstZeiten = 0x26, // 38
mstAnzeigen = 0x3a, // 58
mstFirmware = 0x16, // 22
mstBusValues = 0x31, // 49
mstFuelKind = 0x3c, // 60
mstUnknown0 = 0x09, // 9
mstCurrentDate = 0x0c, // 12
mstCurrentTime = 0x0d, // 13
mstUnknown3 = 0x0f, // 15
mstMenuLevel = 0x10, // 16
mstErrors = 0x14, // 20
mstSetDefaults = 0x15, // 21
mstRost = 0x18, // 24
mstUnknown8 = 0x19, // 25
mstLanguage = 0x1d, // 29
mstErrorMem = 0x1e, // 30
mstErrReset = 0x1f, // 31
mstUnknown12 = 0x2c, // 44
mstXxxExist = 0x38, // 56
mstSaugzug = 0x3b, // 59
mstIgnitonStart= 0x3d, // 61
mstUnknown17 = 0x3e, // 62
mstDisplay = 0x41, // 65
mstKesselChoice= 0x43, // 67
mstKesselType = 0x44, // 68
mstWarningMsg = 0x45, // 69
// menu groups
mstMenuMain = 0x01, // 1
mstMenuSysChoice = 0x0e, // 14
mstMenu1 = 0x21, // 33
mstMenuDaySel = 0x27, // 39
mstMenuRoot = 0x28, // 40
mstMenuChoice = 0x2a, // 42
mstMenu2 = 0x2d, // 45
mstMenu3 = 0x2e, // 46
mstMenuDisplay = 0x30, // 48
mstGroup9 = 0x35, // 53
mstGroup10 = 0x37, // 55
mstMenuControlUnit = 0x3f // 63
};
static bool isGroup(int type)
{ return
type == mstMenuMain ||
type == mstMenuSysChoice ||
type == mstMenu1 ||
type == mstMenuDaySel ||
type == mstMenuRoot ||
type == mstMenuChoice ||
type == mstMenu2 ||
type == mstMenu3 ||
type == mstMenuDisplay ||
type == mstGroup9 ||
type == mstGroup10 ||
type == mstMenuControlUnit; }
// error states
static const char* errState2Text(int state)
{
switch (state)
{
case 1: return "gekommen";
case 4: return "gegangen";
case 2: return "quittiert";
default: return "unknown";
}
}
struct ConfigParameter
{
friend class P4Request;
public:
ConfigParameter(word addr = addrUnknown) { unit = 0; address = addr; }
~ConfigParameter() { free(unit); }
int getFactor() { return factor; }
void setUnit(const char* u) { free(unit); unit = strdup(u); }
void show()
{
tell(eloAlways,
" Address: 0x%4.4x\n" \
" Unit: %s\n" \
" Digits: %d\n" \
escBlue " Value: %.*f (%d)\n" escReset \
" Min: %.*f (%d)\n" \
" Max: %.*f (%d)\n" \
" Default: %.*f (%d)\n" \
" Factor: %d\n",
address, unit, digits, digits, rValue, value, digits, rMin, min, digits, rMax, max, digits, rDefault, def, getFactor());
tell(eloInfo,
" UB1 %d \n" \
" UB2 %d \n" \
" UB3 %d \n" \
" UW1 %d \n", \
ub1, ub2, ub3, uw1);
}
cRetBuf toNice(byte type)
{
char nice[100+TB] {};
if (type == mstParDig)
sprintf(nice, "%s", value ? "ja" : "nein");
else if (type == mstParZeit)
sprintf(nice, "%02d:%02d", value/60, value%60);
else
sprintf(nice, "%.*f", digits, rValue);
return cRetBuf(nice);
}
int toJson(json_t* jData)
{
json_object_set_new(jData, "address", json_integer(address));
json_object_set_new(jData, "unit", json_string(unit));
json_object_set_new(jData, "digits", json_integer(digits));
json_object_set_new(jData, "min", json_real(rMin));
json_object_set_new(jData, "max", json_real(rMax));
json_object_set_new(jData, "default", json_real(rDefault));
if (!digits)
json_object_set_new(jData, "value", json_integer(rValue));
else
json_object_set_new(jData, "value", json_real(rValue));
return done;
}
int setValueDirect(const char* strValue, int dig, int factor)
{
if (factor <= 0)
factor = 1;
if (dig == 0)
{
if (!isNum(strValue))
return fail;
rValue = atoi(strValue);
value = rValue * factor;
}
else
{
if (!isFloat(strValue))
return fail;
rValue = strtod(strValue, nullptr);
value = rValue * factor;
}
return success;
}
int setValue(byte type, const char* strValue)
{
if (type == mstParDig)
{
if (strcmp(strValue, "ja") != 0 && strcmp(strValue, "nein") != 0)
return fail;
value = strcmp(strValue, "ja") == 0 ? 1 : 0;
rValue = value;
}
else if (type == mstParZeit)
{
char* h {strdup(strValue)};
char* m {};
if ((m = strchr(h, ':')))
{
*m++ = 0;
if (!isNum(h) || !isNum(m))
{
free(h);
return fail;
}
value = atoi(h)*60 + atoi(m);
rValue = value;
}
else
{
free(h);
return fail;
}
free(h);
}
else if (type == mstPar || type == mstParSet || type == mstParSet1 || type == mstParSet2)
{
tell(eloAlways, "Set value '%s' with %d digits and factor %d", strValue, digits, factor);
if (digits == 0)
{
if (!isNum(strValue))
return fail;
rValue = atoi(strValue);
value = rValue * factor;
}
else
{
if (!isFloat(strValue))
return fail;
rValue = strtod(strValue, nullptr);
value = rValue * factor;
tell(eloAlways, "Set value to %.*f (%d)", digits, rValue, value);
}
}
else
{
return fail;
}
return success;
}
word address {0};
char* unit {};
byte digits {0};
double rValue {0}; // werte mit factor applied
double rMin {0};
double rMax {0};
double rDefault {0};
byte ub1 {0};
byte ub2 {0};
byte ub3 {0};
sword uw1 {0};
private:
byte factor {1};
sword value {0};
sword min {0};
sword max {0};
sword def {0};
};
struct Status
{
Status() { clear(); }
~Status() { clear(); }
void clear()
{
free(modeinfo); modeinfo = strdup("");
free(stateinfo); stateinfo = strdup("");
*version = '\0';
time = 0;
mode = 0;
state = 0;
}
time_t time {0};
byte mode {0}; // Betriebsart
byte state; // Zustand
char* modeinfo {}; // Betriebsart Text
char* stateinfo {}; // Zustand Text
char version[12+TB] {'\0'};
};
struct Value
{
Value(word addr = addrUnknown) { address = addr; }
~Value() { }
word address;
sword value;
};
struct IoValue // digital and analog in/out
{
IoValue(word addr = addrUnknown) { address = addr; }
~IoValue() { }
word address;
byte mode;
byte state;
};
struct ValueSpec
{
ValueSpec() {}
~ValueSpec() { free(unit); free(description); free(name); }
word address;
char* unit {};
word factor;
char* name {};
char* description {};
word type; // this seems to be something like the type
};
struct MenuItem
{
MenuItem() { description = 0; unit = 0; clear(); }
~MenuItem() { free(unit); free(description); }
void clear()
{
type = 0;
unknown1 = 0;
parent = 0;
child = 0;
address = 0;
unknown2 = 0;
free(description); description = 0;
free(unit); unit = 0;
}
byte type;
byte unknown1;
word parent;
word child;
word address;
word unknown2;
char* description;
char* unit;
};
struct TimeRanges
{
TimeRanges(byte addr = 0xff) { address = addr; }
~TimeRanges() { }
int isSet(int n)
{
if (n >= 4 || (timesFrom[n] == 0xff && timesTo[n] == 0xff))
return no;
return yes;
}
const char* getTimeRangeFrom(int n)
{
static char nice[10+TB];
if (!isSet(n))
return "nn:nn";
sprintf(nice, "%02d:%02d", timesFrom[n]/10, timesFrom[n]%10*10);
return nice;
}
const char* getTimeRangeTo(int n)
{
static char nice[10+TB];
if (!isSet(n))
return "nn:nn";
sprintf(nice, "%02d:%02d", timesTo[n]/10, timesTo[n]%10*10);
return nice;
}
const char* getTimeRange(int n)
{
static char nice[20+TB];
sprintf(nice, "%s - %s", getTimeRangeFrom(n), getTimeRangeTo(n));
return nice;
}
int setTimeRange(int n, const char* from, const char* to)
{
if (!strchr(from, ':') || !strchr(to, ':'))
return fail;
if (strcasecmp(from, "nn:nn") != 0 && strcasecmp(to, "nn:nn") != 0)
{
uint hF, mF, hT, mT;
hF = atoi(from);
mF = atoi(strchr(from, ':') + 1);
hT = atoi(to);
mT = atoi(strchr(to, ':') + 1);
if (hF > 23 || hT > 23 || mF > 59 || mT > 59)
return fail;
timesFrom[n] = hF*10 + mF/10;
timesTo[n] = hT*10 + mT/10;
}
else
{
timesFrom[n] = 0xff;
timesTo[n] = 0xff;
}
return success;
}
byte address;
byte timesFrom[4];
byte timesTo[4];
};
struct ErrorInfo
{
ErrorInfo() { text = 0; }
~ErrorInfo() { free(text); }
byte info; // info byte
word number; // error number
byte state; // state
time_t time; // time
char* text; // text
};
// -------------------------
// -------------------------
// COM2
// -------------------------
enum InterfaceDef2
{
seqStart = 0x24, // $
seqEnd = 0x1A, // ^Z <SUB>
delimiter = ';',
sizeName = 29,
sizeUnit = 4,
sizeText = 19,
sizeMaxPacket = 2048
};
enum ParamPart
{
partUnknown = na,
partName,
partValue,
partIndex,
partDiv,
partUnit,
partCount
};
enum ParameterIndex
{
parState = 1,
parCopperTemp = 2,
parExhaustTemp = 3,
parPufferOben = 20,
parPufferUnten = 21,
parPufferPump = 22,
parHk1Temp = 24,
parHk2Temp = 25,
parHk1Pump = 26,
parHk2Pump = 27,
parOutsideTemp = 28,
parError = 99
};
struct Parameter
{
int index; // 17
char name[sizeName+TB]; // "Kesseltemp."
double value; // 80,5
char text[sizeText+TB]; // "Kein Fehler"
char unit[sizeUnit+TB]; // °C"
};
};
typedef FroelingService Fs;