diff --git a/.gitignore b/.gitignore index 6f54def..cf3ee3f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ .vscode/launch.json .vscode/ipch -include/config.h \ No newline at end of file +include/config.hpp \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 89f6be4..203fd96 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,13 +1,14 @@ { - "files.associations": { - "array": "cpp", - "deque": "cpp", - "list": "cpp", - "string": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "string_view": "cpp", - "initializer_list": "cpp", - "ranges": "cpp" - } -} \ No newline at end of file + "files.associations": { + "array": "cpp", + "deque": "cpp", + "list": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "string_view": "cpp", + "initializer_list": "cpp", + "ranges": "cpp" + }, + "clang-tidy.checks": ["readability-*", "modernize-*", "bugprone-*"] +} diff --git a/include/config.h.tmpl b/include/config.hpp.tmpl similarity index 89% rename from include/config.h.tmpl rename to include/config.hpp.tmpl index 1c56ef3..f6f02fc 100644 --- a/include/config.h.tmpl +++ b/include/config.hpp.tmpl @@ -1,3 +1,7 @@ +/* +* Copyright 2023 Maxim Slipenko +*/ +#pragma once // Настройки WiFi #define WIFI_SSID "SSID_HERE" #define WIFI_PASS "PASS_HERE" diff --git a/include/utils.h b/include/utils.hpp similarity index 100% rename from include/utils.h rename to include/utils.hpp diff --git a/lib/HomeAssistantDevices/HALightBrightness.hpp b/lib/HomeAssistantDevices/HALightBrightness.hpp index a0a5f65..21ee0f0 100644 --- a/lib/HomeAssistantDevices/HALightBrightness.hpp +++ b/lib/HomeAssistantDevices/HALightBrightness.hpp @@ -16,9 +16,9 @@ private: HALightBrightnessController *light; protected: - JSON_DOCUMENT_TYPE createConfigJSON(); - JSON_DOCUMENT_TYPE createStateJSON(); - void innerHanlder(JSON_DOCUMENT_TYPE &doc); + JSON_DOCUMENT_TYPE createConfigJSON() override; + JSON_DOCUMENT_TYPE createStateJSON() override; + void innerHanlder(JSON_DOCUMENT_TYPE &doc) override; public: HALightBrightness(PubSubClient &client, const char *name, const char *unique_id, HALightBrightnessController &baseLight); diff --git a/platformio.ini b/platformio.ini index 7b85205..1ffd6f6 100644 --- a/platformio.ini +++ b/platformio.ini @@ -19,4 +19,8 @@ lib_deps = bblanchon/ArduinoJson@^6.19.4 crankyoldgit/IRremoteESP8266@^2.8.2 monitor_speed = 115200 -monitor_filters = default, esp8266_exception_decoder \ No newline at end of file +monitor_filters = default, esp8266_exception_decoder +check_tool = cppcheck, clangtidy +check_flags = + cppcheck: -v --suppress=*:*/.pio/* + clangtidy: --checks=-*,clang-diagnostic-*,-clang-diagnostic-unused-value,clang-analyzer-*,-*,bugprone-*,performance-*,readability-*,-readability-magic-numbers,-readability-braces-around-statements,-readability-inconsistent-declaration-parameter-name,-readability-named-parameter --fix \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 798b4c1..6eb9d8d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,6 @@ +/* + * Copyright 2023 Maxim Slipenko + */ #include #include #include @@ -8,8 +11,8 @@ #include #include -#include -#include +#include +#include static const char ISR_Root_x1[] PROGMEM = R"EOF( -----BEGIN CERTIFICATE----- @@ -68,7 +71,8 @@ float temp; float hum; long lastMsg = 0; -void setup() { +void setup() +{ Serial.begin(115200); delay(1000); @@ -84,8 +88,10 @@ void setup() { mqtt_publish_state(); } -void loop() { - if (!client.connected()) { +void loop() +{ + if (!client.connected()) + { reconnect(); } client.loop(); @@ -104,7 +110,8 @@ void loop() { delay(100); } -void setup_wifi() { +void setup_wifi() +{ delay(10); Serial.println(); Serial.print("Connecting to "); @@ -112,7 +119,8 @@ void setup_wifi() { WiFi.begin(WIFI_SSID, WIFI_PASS); - while (WiFi.status() != WL_CONNECTED) { + while (WiFi.status() != WL_CONNECTED) + { delay(500); Serial.print("."); } @@ -126,12 +134,14 @@ void setup_wifi() { WiFi.persistent(true); } -void setup_time() { +void setup_time() +{ configTime(0, 0, "pool.ntp.org"); Serial.print(F("Waiting for NTP time sync: ")); time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) { + while (nowSecs < 8 * 3600 * 2) + { delay(500); Serial.print(F(".")); yield(); @@ -145,13 +155,15 @@ void setup_time() { Serial.print(asctime(&timeinfo)); } -void setup_mqtt() { +void setup_mqtt() +{ espClient.setTrustAnchors(&certISRG); client.setServer(MQTT_SERVER, MQTT_PORT); client.setCallback(mqtt_callback); - if (!client.connected()) { + if (!client.connected()) + { reconnect(); } @@ -159,13 +171,18 @@ void setup_mqtt() { halight.subscribeToCommandTopic(); } -void reconnect() { +void reconnect() +{ // Loop until we're reconnected - while (!client.connected()) { + while (!client.connected()) + { Serial.print("Attempting MQTT connection..."); - if (client.connect("ESP8266Client", MQTT_LOGIN, MQTT_PASS)) { + if (client.connect("ESP8266Client", MQTT_LOGIN, MQTT_PASS)) + { Serial.println("connected"); - } else { + } + else + { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); @@ -175,27 +192,34 @@ void reconnect() { } } -void mqtt_callback(char *topic, byte *payload, unsigned int length) { +const int unused = 0; + +void mqtt_callback(char *topic, byte *payload, unsigned int length) +{ Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); Serial.println(); - if (strcmp(topic, "homeassistant/status") == 0) { + if (strcmp(topic, "homeassistant/status") == 0) + { payload[length] = '\0'; - if (strcmp((char *)payload, "online") == 0) { + if (strcmp(reinterpret_cast(payload), "online") == 0) + { Serial.println("Home Assistant is online"); mqtt_publish_config(); } } - + halight.handle(topic, payload, length); } -void mqtt_publish_config() { +void mqtt_publish_config() +{ halight.sendConfig(); } -void mqtt_publish_state() { +void mqtt_publish_state() +{ halight.sendState(); } \ No newline at end of file diff --git a/src/utils.cpp b/src/utils.cpp index 7dbe18a..0df0aef 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1 +1 @@ -double round2(double value) { return (int)(value * 100 + 0.5) / 100.0; } \ No newline at end of file +// double round2(double value) { return (int)(value * 100 + 0.5) / 100.0; } \ No newline at end of file