Skip to content

Commit fd986c9

Browse files
committed
Refactor RF Configuration Management
- Introduced RFConfiguration class to encapsulate RF settings and operations. - Replaced direct usage of RFConfig structure with iRFConfig instance throughout the codebase. - Updated methods for loading, saving, and managing RF configurations to use the new RFConfiguration class. - Removed obsolete RFConfig structure and related functions. - Enhanced frequency validation and management in the new RFConfiguration class. - Updated web UI handling to reflect changes in RF configuration management. - Improved logging and error handling during RF configuration updates.
1 parent e495b61 commit fd986c9

File tree

11 files changed

+721
-206
lines changed

11 files changed

+721
-206
lines changed

main/actuatorSomfy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void setupSomfy() {
5858
void XtoSomfy(const char* topicOri, JsonObject& jsonData) {
5959
if (cmpToMainTopic(topicOri, subjectMQTTtoSomfy)) {
6060
THEENGS_LOG_TRACE(F("MQTTtoSomfy json data analysis" CR));
61-
float txFrequency = jsonData["frequency"] | RFConfig.frequency;
61+
float txFrequency = jsonData["frequency"] | iRFConfig.getFrequency();
6262
# ifdef ZradioCC1101 // set Receive off and Transmitt on
6363
disableCurrentReceiver();
6464
ELECHOUSE_cc1101.SetTx(txFrequency);

main/commonRF.cpp

Lines changed: 33 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
# ifdef ZradioCC1101
3030
# include <ELECHOUSE_CC1101_SRC_DRV.h>
3131
# endif
32+
# include <rf/RFConfiguration.h>
33+
3234
# include "TheengsCommon.h"
3335
# include "config_RF.h"
3436

@@ -37,10 +39,23 @@
3739
extern rtl_433_ESP rtl_433;
3840
# endif
3941

40-
RFConfig_s RFConfig;
41-
void RFConfig_init();
42-
void RFConfig_load();
42+
int currentReceiver = ACTIVE_NONE;
43+
extern void enableActiveReceiver();
44+
extern void disableCurrentReceiver();
45+
46+
class ZCommonRFWrapper : public RFReceiver {
47+
public:
48+
ZCommonRFWrapper() : RFReceiver() {}
49+
void enable() override { enableActiveReceiver(); }
50+
void disable() override { disableCurrentReceiver(); }
51+
52+
int getReceiverID() const override { return currentReceiver; }
53+
};
54+
55+
ZCommonRFWrapper iRFReceiver;
56+
RFConfiguration iRFConfig(iRFReceiver);
4357

58+
//TODO review
4459
void initCC1101() {
4560
# ifdef ZradioCC1101 //receiving with CC1101
4661
// Loop on getCC1101() until it returns true and break after 10 attempts
@@ -54,7 +69,7 @@ void initCC1101() {
5469
if (ELECHOUSE_cc1101.getCC1101()) {
5570
THEENGS_LOG_NOTICE(F("C1101 spi Connection OK" CR));
5671
ELECHOUSE_cc1101.Init();
57-
ELECHOUSE_cc1101.SetRx(RFConfig.frequency);
72+
ELECHOUSE_cc1101.SetRx(iRFConfig.getFrequency());
5873
break;
5974
} else {
6075
THEENGS_LOG_ERROR(F("C1101 spi Connection Error" CR));
@@ -68,23 +83,10 @@ void initCC1101() {
6883
}
6984

7085
void setupCommonRF() {
71-
RFConfig_init();
72-
RFConfig_load();
86+
iRFConfig.reInit();
87+
iRFConfig.loadFromStorage();
7388
}
7489

75-
bool validFrequency(float mhz) {
76-
// CC1101 valid frequencies 300-348 MHZ, 387-464MHZ and 779-928MHZ.
77-
if (mhz >= 300 && mhz <= 348)
78-
return true;
79-
if (mhz >= 387 && mhz <= 464)
80-
return true;
81-
if (mhz >= 779 && mhz <= 928)
82-
return true;
83-
return false;
84-
}
85-
86-
int currentReceiver = ACTIVE_NONE;
87-
8890
# if !defined(ZgatewayRFM69) && !defined(ZactuatorSomfy)
8991
// Check if a receiver is available
9092
bool validReceiver(int receiver) {
@@ -138,13 +140,13 @@ void disableCurrentReceiver() {
138140
break;
139141
# endif
140142
default:
141-
THEENGS_LOG_ERROR(F("ERROR: unsupported receiver %d" CR), RFConfig.activeReceiver);
143+
THEENGS_LOG_ERROR(F("ERROR: unsupported receiver %d" CR), iRFConfig.getActiveReceiver());
142144
}
143145
}
144146

145147
void enableActiveReceiver() {
146-
THEENGS_LOG_TRACE(F("enableActiveReceiver: %d" CR), RFConfig.activeReceiver);
147-
switch (RFConfig.activeReceiver) {
148+
THEENGS_LOG_TRACE(F("enableActiveReceiver: %d" CR), iRFConfig.getActiveReceiver());
149+
switch (iRFConfig.getActiveReceiver()) {
148150
# ifdef ZgatewayPilight
149151
case ACTIVE_PILIGHT:
150152
initCC1101();
@@ -155,7 +157,7 @@ void enableActiveReceiver() {
155157
# ifdef ZgatewayRF
156158
case ACTIVE_RF:
157159
initCC1101();
158-
enableRFReceive(RFConfig.frequency, RF_RECEIVER_GPIO, RF_EMITTER_GPIO);
160+
enableRFReceive(iRFConfig.getFrequency(), RF_RECEIVER_GPIO, RF_EMITTER_GPIO);
159161
currentReceiver = ACTIVE_RF;
160162
break;
161163
# endif
@@ -177,18 +179,21 @@ void enableActiveReceiver() {
177179
THEENGS_LOG_ERROR(F("ERROR: no receiver selected" CR));
178180
break;
179181
default:
180-
THEENGS_LOG_ERROR(F("ERROR: unsupported receiver %d" CR), RFConfig.activeReceiver);
182+
THEENGS_LOG_ERROR(F("ERROR: unsupported receiver %d" CR), iRFConfig.getActiveReceiver());
181183
}
182184
}
183185

184186
String stateRFMeasures() {
185187
//Publish RTL_433 state
186188
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
187189
JsonObject RFdata = jsonBuffer.to<JsonObject>();
188-
RFdata["active"] = RFConfig.activeReceiver;
190+
191+
// load the configuration
192+
iRFConfig.toJson(RFdata);
193+
194+
// load the current state
189195
# if defined(ZradioCC1101) || defined(ZradioSX127x)
190-
RFdata["frequency"] = RFConfig.frequency;
191-
if (RFConfig.activeReceiver == ACTIVE_RTL) {
196+
if (iRFConfig.getActiveReceiver() == ACTIVE_RTL) {
192197
# ifdef ZgatewayRTL_433
193198
RFdata["rssithreshold"] = (int)getRTLrssiThreshold();
194199
RFdata["rssi"] = (int)getRTLCurrentRSSI();
@@ -211,134 +216,12 @@ String stateRFMeasures() {
211216
return output;
212217
}
213218

214-
void RFConfig_fromJson(JsonObject& RFdata) {
215-
bool success = false;
216-
if (RFdata.containsKey("frequency") && validFrequency(RFdata["frequency"])) {
217-
Config_update(RFdata, "frequency", RFConfig.frequency);
218-
THEENGS_LOG_NOTICE(F("RF Receive mhz: %F" CR), RFConfig.frequency);
219-
success = true;
220-
}
221-
if (RFdata.containsKey("active")) {
222-
THEENGS_LOG_NOTICE(F("RF receiver active: %d" CR), RFConfig.activeReceiver);
223-
Config_update(RFdata, "active", RFConfig.activeReceiver);
224-
success = true;
225-
}
226-
# ifdef ZgatewayRTL_433
227-
if (RFdata.containsKey("rssithreshold")) {
228-
THEENGS_LOG_NOTICE(F("RTL_433 RSSI Threshold : %d " CR), RFConfig.rssiThreshold);
229-
Config_update(RFdata, "rssithreshold", RFConfig.rssiThreshold);
230-
rtl_433.setRSSIThreshold(RFConfig.rssiThreshold);
231-
success = true;
232-
}
233-
# if defined(RF_SX1276) || defined(RF_SX1278)
234-
if (RFdata.containsKey("ookthreshold")) {
235-
Config_update(RFdata, "ookthreshold", RFConfig.newOokThreshold);
236-
THEENGS_LOG_NOTICE(F("RTL_433 ookThreshold %d" CR), RFConfig.newOokThreshold);
237-
rtl_433.setOOKThreshold(RFConfig.newOokThreshold);
238-
success = true;
239-
}
240-
# endif
241-
if (RFdata.containsKey("status")) {
242-
THEENGS_LOG_NOTICE(F("RF get status:" CR));
243-
rtl_433.getStatus();
244-
success = true;
245-
}
246-
if (!success) {
247-
THEENGS_LOG_ERROR(F("MQTTtoRF Fail json" CR));
248-
}
249-
# endif
250-
disableCurrentReceiver();
251-
enableActiveReceiver();
252-
# ifdef ESP32
253-
if (RFdata.containsKey("erase") && RFdata["erase"].as<bool>()) {
254-
// Erase config from NVS (non-volatile storage)
255-
preferences.begin(Gateway_Short_Name, false);
256-
if (preferences.isKey("RFConfig")) {
257-
int result = preferences.remove("RFConfig");
258-
THEENGS_LOG_NOTICE(F("RF config erase result: %d" CR), result);
259-
preferences.end();
260-
return; // Erase prevails on save, so skipping save
261-
} else {
262-
THEENGS_LOG_NOTICE(F("RF config not found" CR));
263-
preferences.end();
264-
}
265-
}
266-
if (RFdata.containsKey("save") && RFdata["save"].as<bool>()) {
267-
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
268-
JsonObject jo = jsonBuffer.to<JsonObject>();
269-
jo["frequency"] = RFConfig.frequency;
270-
jo["active"] = RFConfig.activeReceiver;
271-
// Don't save those for now, need to be tested
272-
# ifdef ZgatewayRTL_433
273-
//jo["rssithreshold"] = RFConfig.rssiThreshold;
274-
//jo["ookthreshold"] = RFConfig.newOokThreshold;
275-
# endif
276-
// Save config into NVS (non-volatile storage)
277-
String conf = "";
278-
serializeJson(jsonBuffer, conf);
279-
preferences.begin(Gateway_Short_Name, false);
280-
int result = preferences.putString("RFConfig", conf);
281-
preferences.end();
282-
THEENGS_LOG_NOTICE(F("RF Config_save: %s, result: %d" CR), conf.c_str(), result);
283-
}
284-
# endif
285-
}
286-
287-
void RFConfig_init() {
288-
RFConfig.frequency = RF_FREQUENCY;
289-
RFConfig.activeReceiver = ACTIVE_RECEIVER;
290-
RFConfig.rssiThreshold = 0;
291-
RFConfig.newOokThreshold = 0;
292-
}
293-
294-
void RFConfig_load() {
295-
# ifdef ESP32
296-
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
297-
preferences.begin(Gateway_Short_Name, true);
298-
if (preferences.isKey("RFConfig")) {
299-
auto error = deserializeJson(jsonBuffer, preferences.getString("RFConfig", "{}"));
300-
preferences.end();
301-
if (error) {
302-
THEENGS_LOG_ERROR(F("RF Config deserialization failed: %s, buffer capacity: %u" CR), error.c_str(), jsonBuffer.capacity());
303-
return;
304-
}
305-
if (jsonBuffer.isNull()) {
306-
THEENGS_LOG_WARNING(F("RF Config is null" CR));
307-
return;
308-
}
309-
JsonObject jo = jsonBuffer.as<JsonObject>();
310-
RFConfig_fromJson(jo);
311-
THEENGS_LOG_NOTICE(F("RF Config loaded" CR));
312-
} else {
313-
preferences.end();
314-
THEENGS_LOG_NOTICE(F("RF Config not found using default" CR));
315-
enableActiveReceiver();
316-
}
317-
# else
318-
enableActiveReceiver();
319-
# endif
320-
}
321-
322219
void XtoRFset(const char* topicOri, JsonObject& RFdata) {
323220
if (cmpToMainTopic(topicOri, subjectMQTTtoRFset)) {
324221
THEENGS_LOG_TRACE(F("MQTTtoRF json set" CR));
325222

326-
/*
327-
* Configuration modifications priorities:
328-
* First `init=true` and `load=true` commands are executed (if both are present, INIT prevails on LOAD)
329-
* Then parameters included in json are taken in account
330-
* Finally `erase=true` and `save=true` commands are executed (if both are present, ERASE prevails on SAVE)
331-
*/
332-
if (RFdata.containsKey("init") && RFdata["init"].as<bool>()) {
333-
// Restore the default (initial) configuration
334-
RFConfig_init();
335-
} else if (RFdata.containsKey("load") && RFdata["load"].as<bool>()) {
336-
// Load the saved configuration, if not initialised
337-
RFConfig_load();
338-
}
223+
iRFConfig.loadFromMessage(RFdata);
339224

340-
// Load config from json if available
341-
RFConfig_fromJson(RFdata);
342225
stateRFMeasures();
343226
}
344227
}

main/config_RF.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
*/
2626
#ifndef config_RF_h
2727
#define config_RF_h
28+
#pragma once
2829

29-
#include "TheengsCommon.h"
30+
#include <rf/RFConfiguration.h>
3031

3132
#ifdef ZgatewayRF
32-
extern void setupRF();
3333
extern void RFtoX();
3434
extern void XtoRF(const char* topicOri, const char* datacallback);
3535
extern void XtoRF(const char* topicOri, JsonObject& RFdata);
@@ -137,6 +137,9 @@ const char parameters[51][4][24] = {
137137
# define DISCOVERY_TRACE_LOG(...)
138138
# endif
139139
#endif
140+
141+
extern RFConfiguration iRFConfig;
142+
140143
/*-------------------RF topics & parameters----------------------*/
141144
//433Mhz MQTT Subjects and keys
142145
#define subjectMQTTtoRF "/commands/MQTTto433"
@@ -198,13 +201,6 @@ const char parameters[51][4][24] = {
198201
* 4 = ZgatewayRF2
199202
*/
200203

201-
struct RFConfig_s {
202-
float frequency;
203-
int rssiThreshold;
204-
int newOokThreshold;
205-
int activeReceiver;
206-
};
207-
208204
#define ACTIVE_NONE -1
209205
#define ACTIVE_RECERROR 0
210206
#define ACTIVE_PILIGHT 1
@@ -224,8 +220,6 @@ struct RFConfig_s {
224220
# define ACTIVE_RECEIVER ACTIVE_NONE
225221
#endif
226222

227-
extern RFConfig_s RFConfig;
228-
229223
/*-------------------CC1101 DefaultTXPower----------------------*/
230224
//Adjust the default TX-Power for sending radio if ZradioCC1101 is used.
231225
//The following settings are possible depending on the frequency band. (-30 -20 -15 -10 -6 0 5 7 10 11 12) Default is max!

main/gatewayPilight.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ void XtoPilight(const char* topicOri, JsonObject& Pilightdata) {
218218
const char* protocol = Pilightdata["protocol"];
219219
THEENGS_LOG_NOTICE(F("MQTTtoPilight protocol: %s" CR), protocol);
220220
const char* raw = Pilightdata["raw"];
221-
float txFrequency = Pilightdata["frequency"] | RFConfig.frequency;
221+
float txFrequency = Pilightdata["frequency"] | iRFConfig.getFrequency();
222222
bool success = false;
223223
disableCurrentReceiver();
224224
initCC1101();
@@ -307,7 +307,7 @@ extern void disablePilightReceive() {
307307
};
308308

309309
extern void enablePilightReceive() {
310-
THEENGS_LOG_NOTICE(F("Switching to Pilight Receiver: %F" CR), RFConfig.frequency);
310+
THEENGS_LOG_NOTICE(F("Switching to Pilight Receiver: %F" CR), iRFConfig.getFrequency());
311311
THEENGS_LOG_NOTICE(F("RF_EMITTER_GPIO: %d " CR), RF_EMITTER_GPIO);
312312
THEENGS_LOG_NOTICE(F("RF_RECEIVER_GPIO: %d " CR), RF_RECEIVER_GPIO);
313313
THEENGS_LOG_TRACE(F("gatewayPilight command topic: %s%s%s" CR), mqtt_topic, gateway_name, subjectMQTTtoPilight);

0 commit comments

Comments
 (0)