Skip to content

Commit 5634f46

Browse files
committed
Merge branch 'dev' of https://github.com/emsesp/EMS-ESP32 into dev_no_master_thermostat
2 parents f076829 + a2f8435 commit 5634f46

40 files changed

+642
-625
lines changed

.github/workflows/sonar_check.yml

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Sonar Check
2+
on:
3+
push:
4+
branches:
5+
- dev
6+
pull_request:
7+
types: [opened, synchronize, reopened]
8+
jobs:
9+
build:
10+
name: Build
11+
runs-on: ubuntu-latest
12+
env:
13+
# https://binaries.sonarsource.com/?prefix=Distribution/sonar-scanner-cli/
14+
# SONAR_SCANNER_VERSION: 4.6.1.2450
15+
SONAR_SCANNER_VERSION: 4.7.0.2747
16+
SONAR_SERVER_URL: "https://sonarcloud.io"
17+
BUILD_WRAPPER_OUT_DIR: build_wrapper_output_directory
18+
steps:
19+
- uses: actions/checkout@v2
20+
with:
21+
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
22+
- name: Set up JDK 11
23+
uses: actions/setup-java@v1
24+
with:
25+
java-version: 11
26+
- name: Cache SonarCloud packages
27+
uses: actions/cache@v1
28+
with:
29+
path: ~/.sonar/cache
30+
key: ${{ runner.os }}-sonar
31+
restore-keys: ${{ runner.os }}-sonar
32+
- name: Download and set up sonar-scanner
33+
env:
34+
SONAR_SCANNER_DOWNLOAD_URL: https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-${{ env.SONAR_SCANNER_VERSION }}-linux.zip
35+
run: |
36+
mkdir -p $HOME/.sonar
37+
curl -sSLo $HOME/.sonar/sonar-scanner.zip ${{ env.SONAR_SCANNER_DOWNLOAD_URL }}
38+
unzip -o $HOME/.sonar/sonar-scanner.zip -d $HOME/.sonar/
39+
echo "$HOME/.sonar/sonar-scanner-${{ env.SONAR_SCANNER_VERSION }}-linux/bin" >> $GITHUB_PATH
40+
- name: Download and set up build-wrapper
41+
env:
42+
BUILD_WRAPPER_DOWNLOAD_URL: ${{ env.SONAR_SERVER_URL }}/static/cpp/build-wrapper-linux-x86.zip
43+
run: |
44+
curl -sSLo $HOME/.sonar/build-wrapper-linux-x86.zip ${{ env.BUILD_WRAPPER_DOWNLOAD_URL }}
45+
unzip -o $HOME/.sonar/build-wrapper-linux-x86.zip -d $HOME/.sonar/
46+
echo "$HOME/.sonar/build-wrapper-linux-x86" >> $GITHUB_PATH
47+
- name: Run build-wrapper
48+
run: |
49+
make clean
50+
build-wrapper-linux-x86-64 --out-dir ${{ env.BUILD_WRAPPER_OUT_DIR }} make clean all
51+
- name: Run sonar-scanner
52+
env:
53+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
54+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
55+
run: |
56+
sonar-scanner

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ node_modules
2828
test.sh
2929
scripts/__pycache__
3030
.temp
31+
32+
# sonar
33+
.scannerwork/
34+
sonar/
35+
build_wrapper_output_directory/

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ COMPILE.cpp = $(CXX) $(CXX_STANDARD) $(CXXFLAGS) $(DEPFLAGS) -c $< -o $@
113113
#----------------------------------------------------------------------
114114
# Targets
115115
#----------------------------------------------------------------------
116+
.PHONY: all
116117
all: $(OUTPUT)
117118

118119
$(OUTPUT): $(OBJS)
@@ -138,6 +139,7 @@ cppcheck: $(SOURCES)
138139
run: $(OUTPUT)
139140
@$<
140141

142+
.PHONY: clean
141143
clean:
142144
@$(RM) -r $(BUILD) $(OUTPUT)
143145

interface/package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

interface/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"@types/lodash": "^4.14.179",
1313
"@types/node": "^17.0.21",
1414
"@types/react": "^17.0.39",
15-
"@types/react-dom": "^17.0.11",
15+
"@types/react-dom": "^17.0.13",
1616
"@types/react-router-dom": "^5.3.3",
1717
"async-validator": "^4.0.7",
1818
"axios": "^0.26.0",

scripts/run_sonar.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
3+
# make sure you add the soanr token here
4+
# export SONAR_TOKEN=""
5+
6+
make clean
7+
build-wrapper-linux-x86-64 --out-dir build_wrapper_output_directory make all
8+
sonar-scanner

sonar-project.properties

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
sonar.organization=emsesp
2+
sonar.projectKey=emsesp_EMS-ESP32
3+
sonar.projectName=EMS-ESP32
4+
sonar.projectVersion=3.4
5+
sonar.sources=./src
6+
sonar.cfamily.build-wrapper-output=build_wrapper_output_directory
7+
sonar.sourceEncoding=UTF-8
8+
sonar.host.url=https://sonarcloud.io
9+
sonar.cfamily.threads=8
10+
sonar.cfamily.cache.enabled=false
11+
; sonar.cfamily.cache.path=./sonar/cache

src/analogsensor.cpp

+41-45
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,11 @@ void AnalogSensor::reload() {
6464
// load the list of analog sensors from the customization service
6565
// and store them locally and then activate them
6666
EMSESP::webCustomizationService.read([&](WebCustomization & settings) {
67-
auto sensors = settings.analogCustomizations;
68-
auto it = sensors_.begin();
67+
auto it = sensors_.begin();
6968
for (auto & sensor_ : sensors_) {
7069
// update existing sensors
7170
bool found = false;
72-
for (auto & sensor : sensors) { //search customlist
71+
for (const auto & sensor : settings.analogCustomizations) { //search customlist
7372
if (sensor_.id() == sensor.id) {
7473
// for output sensors set value to new start-value
7574
if ((sensor.type == AnalogType::COUNTER || sensor.type >= AnalogType::DIGITAL_OUT)
@@ -90,10 +89,11 @@ void AnalogSensor::reload() {
9089
}
9190
it++;
9291
}
92+
9393
// add new sensors from list
94-
for (auto & sensor : sensors) {
94+
for (const auto & sensor : settings.analogCustomizations) {
9595
bool found = false;
96-
for (auto & sensor_ : sensors_) {
96+
for (const auto & sensor_ : sensors_) {
9797
if (sensor_.id() == sensor.id) {
9898
found = true;
9999
}
@@ -295,13 +295,13 @@ bool AnalogSensor::update(uint8_t id, const std::string & name, float offset, fl
295295
if (!found_sensor) {
296296
EMSESP::webCustomizationService.update(
297297
[&](WebCustomization & settings) {
298-
AnalogCustomization newSensor = AnalogCustomization();
299-
newSensor.id = id;
300-
newSensor.name = name;
301-
newSensor.offset = offset;
302-
newSensor.factor = factor;
303-
newSensor.uom = uom;
304-
newSensor.type = type;
298+
auto newSensor = AnalogCustomization();
299+
newSensor.id = id;
300+
newSensor.name = name;
301+
newSensor.offset = offset;
302+
newSensor.factor = factor;
303+
newSensor.uom = uom;
304+
newSensor.type = type;
305305
settings.analogCustomizations.push_back(newSensor);
306306
LOG_DEBUG(F("Adding new customization for analog sensor ID %d"), id);
307307
return StateUpdateResult::CHANGED; // persist the change
@@ -325,7 +325,7 @@ bool AnalogSensor::updated_values() {
325325
}
326326

327327
// publish a single sensor to MQTT
328-
void AnalogSensor::publish_sensor(const Sensor & sensor) {
328+
void AnalogSensor::publish_sensor(const Sensor & sensor) const {
329329
if (Mqtt::publish_single()) {
330330
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
331331
if (Mqtt::publish_single2cmd()) {
@@ -339,7 +339,7 @@ void AnalogSensor::publish_sensor(const Sensor & sensor) {
339339
}
340340

341341
// send empty config topic to remove the entry from HA
342-
void AnalogSensor::remove_ha_topic(const uint8_t id) {
342+
void AnalogSensor::remove_ha_topic(const uint8_t id) const {
343343
if (!Mqtt::ha_enabled()) {
344344
return;
345345
}
@@ -363,7 +363,6 @@ void AnalogSensor::publish_values(const bool force) {
363363
for (const auto & sensor : sensors_) {
364364
publish_sensor(sensor);
365365
}
366-
// return;
367366
}
368367

369368
DynamicJsonDocument doc(120 * num_sensors);
@@ -383,61 +382,59 @@ void AnalogSensor::publish_values(const bool force) {
383382
case AnalogType::PWM_0:
384383
case AnalogType::PWM_1:
385384
case AnalogType::PWM_2:
386-
dataSensor["value"] = (float)sensor.value(); // float
385+
dataSensor["value"] = sensor.value(); // float
387386
break;
388-
case AnalogType::DIGITAL_IN:
389-
case AnalogType::DIGITAL_OUT:
390387
default:
391388
dataSensor["value"] = (uint8_t)sensor.value(); // convert to char for 1 or 0
392389
break;
393390
}
394391

395392
// create HA config
396-
if (Mqtt::ha_enabled()) {
397-
if (!sensor.ha_registered || force) {
398-
LOG_DEBUG(F("Recreating HA config for analog sensor ID %d"), sensor.id());
393+
if (Mqtt::ha_enabled() && (!sensor.ha_registered || force)) {
394+
LOG_DEBUG(F("Recreating HA config for analog sensor ID %d"), sensor.id());
399395

400-
StaticJsonDocument<EMSESP_JSON_SIZE_MEDIUM> config;
396+
StaticJsonDocument<EMSESP_JSON_SIZE_MEDIUM> config;
401397

402-
char stat_t[50];
403-
snprintf(stat_t, sizeof(stat_t), "%s/analogsensor_data", Mqtt::base().c_str());
404-
config["stat_t"] = stat_t;
398+
char stat_t[50];
399+
snprintf(stat_t, sizeof(stat_t), "%s/analogsensor_data", Mqtt::base().c_str());
400+
config["stat_t"] = stat_t;
405401

406-
char str[50];
407-
snprintf(str, sizeof(str), "{{value_json['%d'].value}}", sensor.id());
408-
config["val_tpl"] = str;
402+
char str[50];
403+
snprintf(str, sizeof(str), "{{value_json['%d'].value}}", sensor.id());
404+
config["val_tpl"] = str;
409405

410-
snprintf(str, sizeof(str), "Analog Sensor %s", sensor.name().c_str());
411-
config["name"] = str;
406+
snprintf(str, sizeof(str), "Analog Sensor %s", sensor.name().c_str());
407+
config["name"] = str;
412408

413-
snprintf(str, sizeof(str), "analogsensor_%d", sensor.id());
414-
config["uniq_id"] = str;
409+
snprintf(str, sizeof(str), "analogsensor_%d", sensor.id());
410+
config["uniq_id"] = str;
415411

416-
JsonObject dev = config.createNestedObject("dev");
417-
JsonArray ids = dev.createNestedArray("ids");
418-
ids.add("ems-esp");
412+
JsonObject dev = config.createNestedObject("dev");
413+
JsonArray ids = dev.createNestedArray("ids");
414+
ids.add("ems-esp");
419415

420-
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
421-
snprintf(topic, sizeof(topic), "sensor/%s/analogsensor_%d/config", Mqtt::base().c_str(), sensor.id());
416+
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
417+
snprintf(topic, sizeof(topic), "sensor/%s/analogsensor_%d/config", Mqtt::base().c_str(), sensor.id());
422418

423-
Mqtt::publish_ha(topic, config.as<JsonObject>());
419+
Mqtt::publish_ha(topic, config.as<JsonObject>());
424420

425-
sensor.ha_registered = true;
426-
}
421+
sensor.ha_registered = true;
427422
}
428423

424+
429425
} else {
430426
// not nested
431427
doc[sensor.name()] = sensor.value();
432428
}
433429
}
434430
}
431+
435432
Mqtt::publish(F("analogsensor_data"), doc.as<JsonObject>());
436433
}
437434

438435
// called from emsesp.cpp, similar to the emsdevice->get_value_info
439436
// searches by name
440-
bool AnalogSensor::get_value_info(JsonObject & output, const char * cmd, const int8_t id) {
437+
bool AnalogSensor::get_value_info(JsonObject & output, const char * cmd, const int8_t id) const {
441438
for (const auto & sensor : sensors_) {
442439
if (strcmp(cmd, sensor.name().c_str()) == 0) {
443440
output["id"] = sensor.id();
@@ -455,8 +452,8 @@ bool AnalogSensor::get_value_info(JsonObject & output, const char * cmd, const i
455452

456453
// creates JSON doc from values
457454
// returns false if there are no sensors
458-
bool AnalogSensor::command_info(const char * value, const int8_t id, JsonObject & output) {
459-
if (sensors_.size() == 0) {
455+
bool AnalogSensor::command_info(const char * value, const int8_t id, JsonObject & output) const {
456+
if (sensors_.empty()) {
460457
return false;
461458
}
462459

@@ -532,7 +529,7 @@ bool AnalogSensor::command_setvalue(const char * value, const int8_t id) {
532529
return true;
533530
} else if (sensor.type() == AnalogType::DIGITAL_OUT) {
534531
uint8_t v = val;
535-
if ((sensor.id() == 25 || sensor.id() == 26)) {
532+
if (sensor.id() == 25 || sensor.id() == 26) {
536533
sensor.set_offset(v);
537534
sensor.set_value(v);
538535
pinMode(sensor.id(), OUTPUT);
@@ -573,7 +570,6 @@ bool AnalogSensor::command_commands(const char * value, const int8_t id, JsonObj
573570
// hard coded tests
574571
#ifdef EMSESP_DEBUG
575572
void AnalogSensor::test() {
576-
// Sensor(const uint8_t id, const std::string & name, const float offset, const float factor, const uint8_t uom, const int8_t type);
577573
sensors_.emplace_back(36, "test12", 0, 0.1, 17, AnalogType::ADC);
578574
sensors_.back().set_value(12.4);
579575

src/analogsensor.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -127,38 +127,38 @@ class AnalogSensor {
127127

128128
void start();
129129
void loop();
130-
void publish_sensor(const Sensor & sensor);
130+
void publish_sensor(const Sensor & sensor) const;
131131
void publish_values(const bool force);
132132
void reload();
133133
bool updated_values();
134134

135135
// return back reference to the sensor list, used by other classes
136-
const std::vector<Sensor> sensors() const {
136+
std::vector<Sensor> sensors() const {
137137
return sensors_;
138138
}
139139

140-
uint32_t reads() {
140+
uint32_t reads() const {
141141
return sensorreads_;
142142
}
143143

144-
uint32_t fails() {
144+
uint32_t fails() const {
145145
return sensorfails_;
146146
}
147147

148-
bool analog_enabled() {
148+
bool analog_enabled() const {
149149
return (analog_enabled_);
150150
}
151151

152-
bool have_sensors() {
153-
return (sensors_.size() > 0);
152+
bool have_sensors() const {
153+
return (!sensors_.empty());
154154
}
155155

156-
size_t no_sensors() {
156+
size_t no_sensors() const {
157157
return sensors_.size();
158158
}
159159

160160
bool update(uint8_t id, const std::string & name, float offset, float factor, uint8_t uom, int8_t type);
161-
bool get_value_info(JsonObject & output, const char * cmd, const int8_t id);
161+
bool get_value_info(JsonObject & output, const char * cmd, const int8_t id) const;
162162

163163
#ifdef EMSESP_DEBUG
164164
void test();
@@ -170,10 +170,10 @@ class AnalogSensor {
170170

171171
static uuid::log::Logger logger_;
172172

173-
void remove_ha_topic(const uint8_t id);
173+
void remove_ha_topic(const uint8_t id) const;
174174
bool command_setvalue(const char * value, const int8_t id);
175175
void measure();
176-
bool command_info(const char * value, const int8_t id, JsonObject & output);
176+
bool command_info(const char * value, const int8_t id, JsonObject & output) const;
177177
bool command_commands(const char * value, const int8_t id, JsonObject & output);
178178

179179
std::vector<Sensor> sensors_; // our list of sensors

0 commit comments

Comments
 (0)