forked from wmbusmeters/wmbusmeters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meter_multical302.cc
226 lines (189 loc) · 7.38 KB
/
meter_multical302.cc
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
/*
Copyright (C) 2018 Fredrik Öhrström
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include"meters.h"
#include"meters_common_implementation.h"
#include"wmbus.h"
#include"wmbus_utils.h"
#include"util.h"
#include<memory.h>
#include<stdio.h>
#include<string>
#include<time.h>
#include<vector>
struct MeterMultical302 : public virtual HeatMeter, public virtual MeterCommonImplementation {
MeterMultical302(WMBus *bus, const char *name, const char *id, const char *key);
double totalEnergyConsumption();
double currentPowerConsumption();
double totalVolume();
void printMeterHumanReadable(FILE *output);
void printMeterFields(FILE *output, char separator);
void printMeterJSON(FILE *output);
private:
void handleTelegram(Telegram *t);
void processContent(Telegram *t);
double total_energy_ {};
double current_power_ {};
double total_volume_ {};
};
MeterMultical302::MeterMultical302(WMBus *bus, const char *name, const char *id, const char *key) :
MeterCommonImplementation(bus, name, id, key, MULTICAL302_METER, MANUFACTURER_KAM, 0x04)
{
MeterCommonImplementation::bus()->onTelegram(calll(this,handleTelegram,Telegram*));
}
double MeterMultical302::totalEnergyConsumption()
{
return total_energy_;
}
double MeterMultical302::currentPowerConsumption()
{
return current_power_;
}
double MeterMultical302::totalVolume()
{
return total_volume_;
}
void MeterMultical302::handleTelegram(Telegram *t) {
if (!isTelegramForMe(t)) {
// This telegram is not intended for this meter.
return;
}
verbose("(multical302) %s %02x%02x%02x%02x ",
name().c_str(),
t->a_field_address[0], t->a_field_address[1], t->a_field_address[2],
t->a_field_address[3]);
if (t->a_field_device_type != 0x04) {
warning("(multical302) expected telegram for heat media, but got \"%s\"!\n",
mediaType(t->m_field, t->a_field_device_type).c_str());
}
/*
if (t->m_field != manufacturer() ||
t->a_field_version != 0x1b) {
warning("(multical302) expected telegram from KAM meter with version 0x1b, but got \"%s\" version 0x2x !\n",
manufacturerFlag(t->m_field).c_str(), t->a_field_version);
}*/
if (useAes()) {
vector<uchar> aeskey = key();
decryptKamstrupC1(t, aeskey);
} else {
t->content = t->payload;
}
logTelegram("(multical302) log", t->parsed, t->content);
int content_start = t->parsed.size();
processContent(t);
if (isDebugEnabled()) {
t->explainParse("(multical302)", content_start);
}
triggerUpdate(t);
}
void MeterMultical302::processContent(Telegram *t) {
vector<uchar>::iterator bytes = t->content.begin();
int crc0 = t->content[0];
int crc1 = t->content[1];
t->addExplanation(bytes, 2, "%02x%02x payload crc", crc0, crc1);
int frame_type = t->content[2];
t->addExplanation(bytes, 1, "%02x frame type (%s)", frame_type, frameTypeKamstrupC1(frame_type).c_str());
if (frame_type == 0x79) {
if (t->content.size() != 17) {
fprintf(stderr, "(multical302) warning: Unexpected length of frame %zu. Expected 17 bytes! ", t->content.size());
padWithZeroesTo(&t->content, 17, &t->content);
warning("\n");
}
// This code should be rewritten to use parseDV see the Multical21 code.
// But I cannot do this without more examples of 302 telegrams.
t->addExplanation(bytes, 4, "%02x%02x%02x%02x unknown", t->content[3], t->content[4], t->content[5], t->content[6]);
int rec1val0 = t->content[7];
int rec1val1 = t->content[8];
int rec1val2 = t->content[9];
t->addExplanation(bytes, 4, "%02x%02x%02x unknown", t->content[10], t->content[11], t->content[12]);
int total_energy_raw = rec1val2*256*256 + rec1val1*256 + rec1val0;
total_energy_ = total_energy_raw;
t->addExplanation(bytes, 3, "%02x%02x%02x total power (%d)",
rec1val0, rec1val1, rec1val2, total_energy_raw);
int rec2val0 = t->content[13];
int rec2val1 = t->content[14];
int rec2val2 = t->content[15];
int total_volume_raw = rec2val2*256*256 + rec2val1*256 + rec2val0;
total_volume_ = total_volume_raw;
t->addExplanation(bytes, 3, "%02x%02x%02x total volume (%d)",
rec2val0, rec2val1, rec2val2, total_volume_raw);
}
else if (frame_type == 0x78)
{
if (t->content.size() != 26) {
fprintf(stderr, "(multical302) warning: Unexpected length of frame %zu. Expected 26 bytes! ", t->content.size());
padWithZeroesTo(&t->content, 26, &t->content);
warning("\n");
}
// This code should be rewritten to use parseDV see the Multical21 code.
// But I cannot do this without more examples of 302 telegrams.
vector<uchar> unknowns;
unknowns.insert(unknowns.end(), t->content.begin()+3, t->content.begin()+24);
string hex = bin2hex(unknowns);
t->addExplanation(bytes, 23-2, "%s unknown", hex.c_str());
int rec1val0 = t->content[24];
int rec1val1 = t->content[25];
int current_power_raw = (rec1val1*256 + rec1val0)*100;
current_power_ = current_power_raw;
t->addExplanation(bytes, 2, "%02x%02x current power (%d)",
rec1val0, rec1val1, current_power_raw);
}
else {
warning("(multical302) warning: unknown frame %02x (did you use the correct encryption key?)\n", frame_type);
}
}
HeatMeter *createMultical302(WMBus *bus, const char *name, const char *id, const char *key) {
return new MeterMultical302(bus,name,id,key);
}
void MeterMultical302::printMeterHumanReadable(FILE *output)
{
fprintf(output, "%s\t%s\t% 3.3f kwh\t% 3.3f m3\t% 3.3f kwh\t%s\n",
name().c_str(),
id().c_str(),
totalEnergyConsumption(),
totalVolume(),
currentPowerConsumption(),
datetimeOfUpdateHumanReadable().c_str());
}
void MeterMultical302::printMeterFields(FILE *output, char separator)
{
fprintf(output, "%s%c%s%c%f%c%f%c%f%c%s\n",
name().c_str(), separator,
id().c_str(), separator,
totalEnergyConsumption(), separator,
totalVolume(), separator,
currentPowerConsumption(), separator,
datetimeOfUpdateRobot().c_str());
}
#define Q(x,y) "\""#x"\":"#y","
#define QS(x,y) "\""#x"\":\""#y"\","
#define QSE(x,y) "\""#x"\":\""#y"\""
void MeterMultical302::printMeterJSON(FILE *output)
{
fprintf(output, "{"
QS(media,heat)
QS(meter,multical302)
QS(name,%s)
QS(id,%s)
Q(total_kwh,%f)
Q(total_volume_m3,%f)
QS(current_kw,%f)
QSE(timestamp,%s)
"}\n",
name().c_str(),
id().c_str(),
totalEnergyConsumption(),
totalVolume(),
currentPowerConsumption(),
datetimeOfUpdateRobot().c_str());
}