Skip to content

Commit d5136aa

Browse files
committed
initial commit
1 parent c70b66f commit d5136aa

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

src/SensorSettingsService.h

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#ifndef SensorService_h
2+
#define SensorService_h
3+
4+
// #include <LightMqttSettingsService.h>
5+
#include <LITTLEFS.h>
6+
7+
#include <HttpEndpoint.h>
8+
#include <MqttPubSub.h>
9+
#include <WebSocketTxRx.h>
10+
#include <FSPersistence.h>
11+
#include <sensor/CSensor.h>
12+
#include "sensor/BMP280.cpp"
13+
#include "sensor/BMPsensor.cpp"
14+
#include "sensor/DHT11Sensor.cpp"
15+
#include "sensor/FreeMemSensor.cpp"
16+
#include "sensor/AnalogInSensor.cpp"
17+
#include "sensor/TestSensor.h"
18+
19+
#define SENSOR_SETTINGS_FILE "/config/sensorSettings.json"
20+
#define SENSOR_SETTINGS_ENDPOINT_PATH "/rest/sensorsState"
21+
#define SENSOR_SETTINGS_SOCKET_PATH "/ws/sensorsState"
22+
23+
class SensorConfig {
24+
public:
25+
26+
CSensor* sensorList[5];
27+
28+
// Add all sensors here
29+
static constexpr const char* driverList[] = {
30+
BMP180Sensor::description,
31+
DHT11Sensor::description,
32+
TestSensor::description,
33+
BMP280Sensor::description,
34+
FreeMemSensor::description,
35+
AnalogInSensor::description
36+
};
37+
38+
static void read(SensorConfig& settings, JsonObject& root);
39+
40+
static CSensor* getSensor(JsonObject& sensorConf) {
41+
Serial.println("Adding sensor with conf");
42+
serializeJsonPretty(sensorConf,Serial);
43+
44+
if (strcmp(sensorConf["driver"]["name"], "Random") == 0) {
45+
return new TestSensor(sensorConf);
46+
}
47+
if (strcmp(sensorConf["driver"]["name"], "BMP") == 0) {
48+
return new BMP180Sensor(sensorConf);
49+
}
50+
if (strcmp(sensorConf["driver"]["name"], "BMP280") == 0) {
51+
return new BMP280Sensor(sensorConf);
52+
}
53+
if (strcmp(sensorConf["driver"]["name"], "DHT11") == 0) {
54+
return new DHT11Sensor(sensorConf);
55+
}
56+
if (strcmp(sensorConf["driver"]["name"], "FreeMem") == 0) {
57+
return new FreeMemSensor(sensorConf);
58+
}
59+
if (strcmp(sensorConf["driver"]["name"], "AnalogIn") == 0) {
60+
return new AnalogInSensor(sensorConf);
61+
}
62+
63+
return new TestSensor(sensorConf);
64+
// Add here your custom sensor
65+
}
66+
67+
// Received updated settings from file/UI and update JsonObject
68+
static StateUpdateResult update(JsonObject& root, SensorConfig& settings) {
69+
for (size_t i = 0; i < (sizeof(sensorList) / sizeof(CSensor*)); i++) {
70+
if (settings.sensorList[i] != NULL) {
71+
settings.sensorList[i]->end();
72+
Serial.println("Deleting sensor");
73+
delete (settings.sensorList[i]);
74+
settings.sensorList[i] = NULL;
75+
}
76+
}
77+
int i = 0;
78+
Serial.println("Adding sensors from json conf:");
79+
JsonArray jsensors = root.getMember("sensors");
80+
serializeJsonPretty(jsensors,Serial);
81+
82+
if (jsensors.size() > 0) {
83+
for (JsonObject jsensor : jsensors) {
84+
settings.sensorList[i] = getSensor(jsensor);
85+
i++;
86+
}
87+
}
88+
89+
return StateUpdateResult::CHANGED;
90+
91+
}
92+
};
93+
94+
class SensorSettingsService : public StatefulService<SensorConfig> {
95+
public:
96+
SensorSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
97+
void begin();
98+
SensorConfig config;
99+
100+
private:
101+
HttpEndpoint<SensorConfig> _httpEndpoint;
102+
WebSocketTxRx<SensorConfig> _webSocket;
103+
FSPersistence<SensorConfig> _fsPersistence;
104+
105+
void registerConfig();
106+
void onConfigUpdated();
107+
};
108+
109+
#endif

0 commit comments

Comments
 (0)