@@ -32,14 +32,21 @@ void ClassFlowLoRaWAN::SetInitialParameter(void)
32
32
// OTAA parameters
33
33
joinEUI = NULL ;
34
34
devEUI = NULL ;
35
- nwkKey[ 16 ] = NULL ;
36
- appKey[ 16 ] = NULL ;
35
+ nwkKey = NULL ;
36
+ appKey = NULL ;
37
37
// ABP parameters
38
38
devAddr = NULL ;
39
- fNwkSIntKey [16 ] = NULL ;
40
- sNwkSIntKey [16 ] = NULL ;
41
- nwkSEncKey[16 ] = NULL ;
42
- appSKey[16 ] = NULL ;
39
+ fNwkSIntKey = NULL ;
40
+ sNwkSIntKey = NULL ;
41
+ nwkSEncKey = NULL ;
42
+ appSKey = NULL ;
43
+
44
+ OldValue = " " ;
45
+ flowpostprocessing = NULL ;
46
+ previousElement = NULL ;
47
+ ListFlowControll = NULL ;
48
+ disabled = false ;
49
+ LoRaWANenable = false ;
43
50
}
44
51
45
52
ClassFlowLoRaWAN::ClassFlowLoRaWAN ()
@@ -77,18 +84,38 @@ ClassFlowLoRaWAN::ClassFlowLoRaWAN(std::vector<ClassFlow*>* lfc, ClassFlow *_pre
77
84
}
78
85
}
79
86
87
+
88
+ std::vector<uint8_t > hexStringToByteArray (const std::string& hexString)
89
+ {
90
+ std::vector<uint8_t > byteArray;
91
+
92
+ // Loop through the hex string, two characters at a time
93
+ for (size_t i = 0 ; i < hexString.length (); i += 2 ) {
94
+ // Extract two characters representing a byte
95
+ std::string byteString = hexString.substr (i, 2 );
96
+
97
+ // Convert the byte string to a uint8_t value
98
+ uint8_t byteValue = static_cast <uint8_t >(
99
+ stoi (byteString, nullptr , 16 ));
100
+
101
+ // Add the byte to the byte array
102
+ byteArray.push_back (byteValue);
103
+ }
104
+
105
+ return byteArray;
106
+ }
107
+
80
108
bool ClassFlowLoRaWAN::ReadParameter (FILE* pfile, string& aktparamgraph)
81
109
{
82
110
std::vector<string> splitted;
83
111
84
112
aktparamgraph = trim (aktparamgraph);
85
- printf (" akt param: %s\n " , aktparamgraph.c_str ());
86
113
87
114
if (aktparamgraph.size () == 0 )
88
115
if (!this ->GetNextParagraph (pfile, aktparamgraph))
89
116
return false ;
90
117
91
- if (toUpper (aktparamgraph).compare (" [LoRaWAN ]" ) != 0 )
118
+ if (toUpper (aktparamgraph).compare (" [LORAWAN ]" ) != 0 )
92
119
return false ;
93
120
94
121
while (this ->getNextLine (pfile, &aktparamgraph) && !this ->isNewParagraph (aktparamgraph))
@@ -197,11 +224,13 @@ bool ClassFlowLoRaWAN::ReadParameter(FILE* pfile, string& aktparamgraph)
197
224
if ((toUpper (_param) == " NWKKEY" ) && (splitted.size () > 1 ))
198
225
{
199
226
std::vector<uint8_t > nwkKeyVector = hexStringToByteArray (splitted[1 ]);
227
+ this ->nwkKey = new uint8_t [16 ];
200
228
copy (nwkKeyVector.begin (), nwkKeyVector.end (), this ->nwkKey );
201
229
}
202
230
if ((toUpper (_param) == " APPKEY" ) && (splitted.size () > 1 ))
203
231
{
204
232
std::vector<uint8_t > appKeyVector = hexStringToByteArray (splitted[1 ]);
233
+ this ->appKey = new uint8_t [16 ];
205
234
copy (appKeyVector.begin (), appKeyVector.end (), this ->appKey );
206
235
}
207
236
// ABP parameters
@@ -236,136 +265,76 @@ bool ClassFlowLoRaWAN::ReadParameter(FILE* pfile, string& aktparamgraph)
236
265
237
266
bool ClassFlowLoRaWAN::Start (float AutoInterval)
238
267
{
239
- // std::stringstream stream;
240
- // stream << std::fixed << std::setprecision(1) << "Digitizer interval is " << roundInterval <<
241
- // " minutes => setting MQTT LWT timeout to " << ((float)keepAlive/60) << " minutes.";
242
- // LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, stream.str());
268
+ LogFile.WriteToFile (ESP_LOG_DEBUG, TAG, " Starting LoRaWAN communication" );
243
269
244
- // mqttServer_setParameter(flowpostprocessing->GetNumbers(), keepAlive, roundInterval);
245
-
246
- // bool MQTTConfigCheck = MQTT_Configure(uri, clientname, user, password, maintopic, LWT_TOPIC, LWT_CONNECTED,
247
- // LWT_DISCONNECTED, caCertFilename, clientCertFilename, clientKeyFilename,
248
- // keepAlive, SetRetainFlag, (void *)&GotConnected);
249
-
250
- // if (!MQTTConfigCheck) {
251
- // return false;
252
- // }
253
-
254
- // return (MQTT_Init() == 1);
255
- uint16_t LoRaWANInitializeCheck = LoRaWAN_Init (region, subBand, uplinkInterval, initialDatarate, ADRActive,
270
+ roundInterval = AutoInterval; // Minutes
271
+ uint16_t LoRaWANInitializeCheck = LoRaWAN_Init (region, subBand, roundInterval, uplinkInterval, initialDatarate, ADRActive,
256
272
CSMAActive, CSMAMaxChanges, CSMABackoffMax, CSMADifsSlots,
257
273
dutyCycleLimitsActive, dutyCycleMsPerHour, dwellTimeLimitsActive, dwellTimeMsPerUplink,
258
274
deviceActivationMethod,
259
275
joinEUI, devEUI, nwkKey, appKey,
260
276
devAddr, fNwkSIntKey , sNwkSIntKey , nwkSEncKey, appSKey);
277
+
278
+ if (LoRaWANInitializeCheck != 0 ) {
279
+ return false ;
280
+ }
281
+
282
+ LoRaWANenable = true ;
283
+
261
284
return true ;
262
285
}
263
286
264
287
bool ClassFlowLoRaWAN::doFlow (string zwtime)
265
288
{
266
- bool success;
289
+ if (!LoRaWANenable)
290
+ return true ;
291
+
267
292
std::string result;
268
293
std::string resulterror = " " ;
294
+ uint8_t resulterrorCode = 0 ;
269
295
std::string resultraw = " " ;
270
296
std::string resultpre = " " ;
271
297
std::string resultrate = " " ; // Always Unit / Minute
272
298
std::string resultRatePerTimeUnit = " " ; // According to selection
273
- std::string resulttimestamp = " " ;
274
299
std::string resultchangabs = " " ;
275
- string zw = " " ;
300
+ std::string resulttimestamp = " " ;
301
+ time_t resulttimeutc = 0 ;
276
302
string namenumber = " " ;
277
- int qos = 1 ;
278
-
279
- /* Send the the Homeassistant Discovery and the Static Topics in case they where scheduled */
280
- // sendDiscovery_and_static_Topics();
281
-
282
- // success = publishSystemData(qos);
283
-
284
- // if (flowpostprocessing && getMQTTisConnected())
285
- // {
286
- // std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
287
-
288
- // LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Publishing MQTT topics...");
289
-
290
- // for (int i = 0; i < (*NUMBERS).size(); ++i)
291
- // {
292
- // result = (*NUMBERS)[i]->ReturnValue;
293
- // resultraw = (*NUMBERS)[i]->ReturnRawValue;
294
- // resultpre = (*NUMBERS)[i]->ReturnPreValue;
295
- // resulterror = (*NUMBERS)[i]->ErrorMessageText;
296
- // resultrate = (*NUMBERS)[i]->ReturnRateValue; // Unit per minutes
297
- // resultchangabs = (*NUMBERS)[i]->ReturnChangeAbsolute; // Units per round
298
- // resulttimestamp = (*NUMBERS)[i]->timeStamp;
299
-
300
- // namenumber = (*NUMBERS)[i]->name;
301
- // if (namenumber == "default")
302
- // namenumber = maintopic + "/";
303
- // else
304
- // namenumber = maintopic + "/" + namenumber + "/";
305
-
306
-
307
- // if (result.length() > 0)
308
- // success |= MQTTPublish(namenumber + "value", result, qos, SetRetainFlag);
309
-
310
- // if (resulterror.length() > 0)
311
- // success |= MQTTPublish(namenumber + "error", resulterror, qos, SetRetainFlag);
312
-
313
- // if (resultrate.length() > 0) {
314
- // success |= MQTTPublish(namenumber + "rate", resultrate, qos, SetRetainFlag);
315
-
316
- // std::string resultRatePerTimeUnit;
317
- // if (getTimeUnit() == "h") { // Need conversion to be per hour
318
- // resultRatePerTimeUnit = resultRatePerTimeUnit = to_string((*NUMBERS)[i]->FlowRateAct * 60); // per minutes => per hour
319
- // }
320
- // else { // Keep per minute
321
- // resultRatePerTimeUnit = resultrate;
322
- // }
323
- // success |= MQTTPublish(namenumber + "rate_per_time_unit", resultRatePerTimeUnit, qos, SetRetainFlag);
324
- // }
325
-
326
- // if (resultchangabs.length() > 0) {
327
- // success |= MQTTPublish(namenumber + "changeabsolut", resultchangabs, qos, SetRetainFlag); // Legacy API
328
- // success |= MQTTPublish(namenumber + "rate_per_digitalization_round", resultchangabs, qos, SetRetainFlag);
329
- // }
330
-
331
- // if (resultraw.length() > 0)
332
- // success |= MQTTPublish(namenumber + "raw", resultraw, qos, SetRetainFlag);
333
-
334
- // if (resulttimestamp.length() > 0)
335
- // success |= MQTTPublish(namenumber + "timestamp", resulttimestamp, qos, SetRetainFlag);
336
-
337
- // std::string json = flowpostprocessing->getJsonFromNumber(i, "\n");
338
- // success |= MQTTPublish(namenumber + "json", json, qos, SetRetainFlag);
339
- // }
340
- // }
341
-
342
- // OldValue = result;
343
303
344
- // if (!success) {
345
- // LogFile.WriteToFile(ESP_LOG_WARN, TAG, "One or more MQTT topics failed to be published!");
346
- // }
347
-
348
- return true ;
349
- }
304
+ if (flowpostprocessing && getLoRaWANisSessionActive ())
305
+ {
306
+ std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers ();
350
307
351
- std::vector<uint8_t > hexStringToByteArray (const std::string& hexString)
352
- {
353
- std::vector<uint8_t > byteArray;
308
+ for (int i = 0 ; i < (*NUMBERS).size (); ++i)
309
+ {
310
+ result = (*NUMBERS)[i]->ReturnValue ;
311
+ resultraw = (*NUMBERS)[i]->ReturnRawValue ;
312
+ resulterror = (*NUMBERS)[i]->ErrorMessageText ;
313
+ resultrate = (*NUMBERS)[i]->ReturnRateValue ; // Unit per minutes
314
+ resulttimeutc = (*NUMBERS)[i]->timeStampTimeUTC ;
354
315
355
- // Loop through the hex string, two characters at a time
356
- for (size_t i = 0 ; i < hexString.length (); i += 2 ) {
357
- // Extract two characters representing a byte
358
- std::string byteString = hexString.substr (i, 2 );
316
+ namenumber = (*NUMBERS)[i]->name ;
359
317
360
- // Convert the byte string to a uint8_t value
361
- uint8_t byteValue = static_cast <uint8_t >(
362
- stoi (byteString, nullptr , 16 ));
318
+ if (resulterror == " no error" ) {
319
+ resulterrorCode = 0 ;
320
+ }
321
+ else {
322
+ if (resulterror.rfind (" Neg. Rate" , 0 ) == 0 ){
323
+ resulterrorCode = 1 ;
324
+ }
325
+ if (resulterror.rfind (" Rate too high" , 0 ) == 0 ){
326
+ resulterrorCode = 2 ;
327
+ }
328
+ }
363
329
364
- // Add the byte to the byte array
365
- byteArray.push_back (byteValue);
330
+ if (result.length () > 0 ) {}
331
+ LoRaWANQueueMessage (i, resulttimeutc, std::stod (result), std::stod (resultrate), resulterrorCode);
332
+ }
366
333
}
367
-
368
- return byteArray;
334
+
335
+ OldValue = result;
336
+
337
+ return true ;
369
338
}
370
339
371
340
#endif // ENABLE_LORAWAN
0 commit comments