Skip to content

Commit daa4ca4

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 across multiple files. - Updated frequency handling in actuatorSomfy, gatewayPilight, gatewayRF, and gatewayRF2 to use iRFConfig. - Modified webUI to interact with iRFConfig for RF settings management. - Removed deprecated RFConfig structure and related functions. - Enhanced JSON handling for RF configuration loading and saving. - Improved logging for RF configuration operations.
1 parent 382fa5e commit daa4ca4

File tree

12 files changed

+554
-207
lines changed

12 files changed

+554
-207
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ sdkconfig.*
1717
__pycache__
1818
*.pem
1919
managed_components
20+
21+
22+
.github/chatmodes
23+
.github/prompts
24+
.github/*instructions.md

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: 39 additions & 151 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,28 @@
3739
extern rtl_433_ESP rtl_433;
3840
# endif
3941

40-
RFConfig_s RFConfig;
41-
void RFConfig_init();
42-
void RFConfig_load();
43-
42+
int currentReceiver = ACTIVE_NONE;
43+
extern void enableActiveReceiver();
44+
extern void disableCurrentReceiver();
45+
46+
// Note: this is currently just a simple wrapper used to make everything work.
47+
// It prevents introducing external dependencies on newly added C++ structures,
48+
// and acts as a first approach to mask the concrete implementations (rf, rf2,
49+
// pilight, etc.). Later this can be extended or replaced by more complete driver
50+
// abstractions without changing the rest of the system.
51+
class ZCommonRFWrapper : public RFReceiver {
52+
public:
53+
ZCommonRFWrapper() : RFReceiver() {}
54+
void enable() override { enableActiveReceiver(); }
55+
void disable() override { disableCurrentReceiver(); }
56+
57+
int getReceiverID() const override { return currentReceiver; }
58+
};
59+
60+
ZCommonRFWrapper iRFReceiver;
61+
RFConfiguration iRFConfig(iRFReceiver);
62+
63+
//TODO review
4464
void initCC1101() {
4565
# ifdef ZradioCC1101 //receiving with CC1101
4666
// Loop on getCC1101() until it returns true and break after 10 attempts
@@ -54,7 +74,7 @@ void initCC1101() {
5474
if (ELECHOUSE_cc1101.getCC1101()) {
5575
THEENGS_LOG_NOTICE(F("C1101 spi Connection OK" CR));
5676
ELECHOUSE_cc1101.Init();
57-
ELECHOUSE_cc1101.SetRx(RFConfig.frequency);
77+
ELECHOUSE_cc1101.SetRx(iRFConfig.getFrequency());
5878
break;
5979
} else {
6080
THEENGS_LOG_ERROR(F("C1101 spi Connection Error" CR));
@@ -68,23 +88,10 @@ void initCC1101() {
6888
}
6989

7090
void setupCommonRF() {
71-
RFConfig_init();
72-
RFConfig_load();
73-
}
74-
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;
91+
iRFConfig.reInit();
92+
iRFConfig.loadFromStorage();
8493
}
8594

86-
int currentReceiver = ACTIVE_NONE;
87-
8895
# if !defined(ZgatewayRFM69) && !defined(ZactuatorSomfy)
8996
// Check if a receiver is available
9097
bool validReceiver(int receiver) {
@@ -138,13 +145,13 @@ void disableCurrentReceiver() {
138145
break;
139146
# endif
140147
default:
141-
THEENGS_LOG_ERROR(F("ERROR: unsupported receiver %d" CR), RFConfig.activeReceiver);
148+
THEENGS_LOG_ERROR(F("ERROR: unsupported receiver %d" CR), iRFConfig.getActiveReceiver());
142149
}
143150
}
144151

145152
void enableActiveReceiver() {
146-
THEENGS_LOG_TRACE(F("enableActiveReceiver: %d" CR), RFConfig.activeReceiver);
147-
switch (RFConfig.activeReceiver) {
153+
THEENGS_LOG_TRACE(F("enableActiveReceiver: %d" CR), iRFConfig.getActiveReceiver());
154+
switch (iRFConfig.getActiveReceiver()) {
148155
# ifdef ZgatewayPilight
149156
case ACTIVE_PILIGHT:
150157
initCC1101();
@@ -155,7 +162,7 @@ void enableActiveReceiver() {
155162
# ifdef ZgatewayRF
156163
case ACTIVE_RF:
157164
initCC1101();
158-
enableRFReceive(RFConfig.frequency, RF_RECEIVER_GPIO, RF_EMITTER_GPIO);
165+
enableRFReceive(iRFConfig.getFrequency(), RF_RECEIVER_GPIO, RF_EMITTER_GPIO);
159166
currentReceiver = ACTIVE_RF;
160167
break;
161168
# endif
@@ -177,18 +184,21 @@ void enableActiveReceiver() {
177184
THEENGS_LOG_ERROR(F("ERROR: no receiver selected" CR));
178185
break;
179186
default:
180-
THEENGS_LOG_ERROR(F("ERROR: unsupported receiver %d" CR), RFConfig.activeReceiver);
187+
THEENGS_LOG_ERROR(F("ERROR: unsupported receiver %d" CR), iRFConfig.getActiveReceiver());
181188
}
182189
}
183190

184191
String stateRFMeasures() {
185192
//Publish RTL_433 state
186193
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
187194
JsonObject RFdata = jsonBuffer.to<JsonObject>();
188-
RFdata["active"] = RFConfig.activeReceiver;
195+
196+
// load the configuration
197+
iRFConfig.toJson(RFdata);
198+
199+
// load the current state
189200
# if defined(ZradioCC1101) || defined(ZradioSX127x)
190-
RFdata["frequency"] = RFConfig.frequency;
191-
if (RFConfig.activeReceiver == ACTIVE_RTL) {
201+
if (iRFConfig.getActiveReceiver() == ACTIVE_RTL) {
192202
# ifdef ZgatewayRTL_433
193203
RFdata["rssithreshold"] = (int)getRTLrssiThreshold();
194204
RFdata["rssi"] = (int)getRTLCurrentRSSI();
@@ -211,134 +221,12 @@ String stateRFMeasures() {
211221
return output;
212222
}
213223

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-
322224
void XtoRFset(const char* topicOri, JsonObject& RFdata) {
323225
if (cmpToMainTopic(topicOri, subjectMQTTtoRFset)) {
324226
THEENGS_LOG_TRACE(F("MQTTtoRF json set" CR));
325227

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-
}
228+
iRFConfig.loadFromMessage(RFdata);
339229

340-
// Load config from json if available
341-
RFConfig_fromJson(RFdata);
342230
stateRFMeasures();
343231
}
344232
}

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)