diff --git a/.drone.yml b/.drone.yml index 8fe9ac6..e8d7926 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,24 +1,38 @@ --- kind: pipeline type: exec -name: upload gateway firmare +name: gateway pipeline platform: os: linux arch: arm steps: -- name: upload +- name: native tests + commands: + - cd gateway + - pio test -e native + +- name: embedded tests + commands: + - cd gateway + - pio test -e embedded --without-uploading + +- name: upload firmware commands: - cd gateway - service ser2net stop - pio run -e pro-mini - echo -n 'reset' > /dev/ttyUSB0; sleep 1s; avrdude -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:.pio/build/pro-mini/firmware.hex:i -v - service ser2net start + when: + target: + - production trigger: branch: - gateway + - gw/* node: host: homebox diff --git a/gateway/include/Protocol_1.h b/gateway/include/Protocol_1.h index 70d7817..f05e7f8 100644 --- a/gateway/include/Protocol_1.h +++ b/gateway/include/Protocol_1.h @@ -1,6 +1,6 @@ #pragma once #include "Protocol.h" -#include "Decoder.h" +#include "RcDecoder.h" class Protocol_1 : public Protocol { @@ -19,7 +19,7 @@ public: void toJson(unsigned long value, JsonDocument& jsonDoc) override { JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch"); rcSwitch["protocol"] = protocol; - Decoder decoder; + RcDecoder decoder; decoder.decode(value); rcSwitch["state"] = decoder.state; rcSwitch["group"] = String(decoder.group, BIN); diff --git a/gateway/include/Protocol_2.h b/gateway/include/Protocol_2.h index 0e32a85..589154e 100644 --- a/gateway/include/Protocol_2.h +++ b/gateway/include/Protocol_2.h @@ -1,6 +1,6 @@ #pragma once #include "Protocol.h" -#include "Tiny.h" +#include "TinyComponent.h" class Protocol_2 : public Protocol { @@ -33,35 +33,4 @@ public: } } -private: - bool buildSensorJson(unsigned long value, JsonDocument& jsonDoc) { - JsonObject sensor = jsonDoc.createNestedObject("sensor"); - sensor["id"] = ID(value); - - float voltage = (float)GET_VCC(value) / 1000; - if (voltage != 0) { - JsonObject diagnostic = sensor.createNestedObject("diagnostic"); - diagnostic["voltage"] = voltage; - } - - switch (GET_TYPE(value)) { - case SensorType::GENERIC: - sensor["value"] = GET_VALUE(value); - break; - case SensorType::TEMPERATURE: - sensor["temperature"] = (float)GET_TEMP(value) / 10; - break; - case SensorType::HUMIDITY: - sensor["humidity"] = (float)GET_HUMIDITY(value) / 10; - break; - case SensorType::CONTACT: - sensor["state"] = GET_STATE(value) ? "on" : "off"; - break; - default: - return false; - } - - return true; - } - }; \ No newline at end of file diff --git a/gateway/include/decoder.h b/gateway/include/RcDecoder.h similarity index 97% rename from gateway/include/decoder.h rename to gateway/include/RcDecoder.h index 37e9eac..e43e564 100644 --- a/gateway/include/decoder.h +++ b/gateway/include/RcDecoder.h @@ -2,7 +2,7 @@ #define RC_DEVICE(value) (value >> 1) & 0x1F #define RC_GROUP(value) (value >> 6) & 0x1F -struct Decoder { +struct RcDecoder { bool state; char group; unsigned char device; diff --git a/gateway/include/TinyComponent.h b/gateway/include/TinyComponent.h new file mode 100644 index 0000000..beb5c12 --- /dev/null +++ b/gateway/include/TinyComponent.h @@ -0,0 +1,32 @@ +#include +#include "Tiny.h" + +bool buildSensorJson(unsigned long value, JsonDocument& jsonDoc) { + JsonObject sensor = jsonDoc.createNestedObject("sensor"); + sensor["id"] = ID(value); + + float voltage = (float)GET_VCC(value) / 1000; + if (voltage != 0) { + JsonObject diagnostic = sensor.createNestedObject("diagnostic"); + diagnostic["voltage"] = voltage; + } + + switch (GET_TYPE(value)) { + case SensorType::GENERIC: + sensor["value"] = GET_VALUE(value); + break; + case SensorType::TEMPERATURE: + sensor["temperature"] = (float)GET_TEMP(value) / 10; + break; + case SensorType::HUMIDITY: + sensor["humidity"] = (float)GET_HUMIDITY(value) / 10; + break; + case SensorType::CONTACT: + sensor["state"] = GET_STATE(value) ? "on" : "off"; + break; + default: + return false; + } + + return true; +} diff --git a/gateway/platformio.ini b/gateway/platformio.ini index 2afa12c..7c9ead9 100644 --- a/gateway/platformio.ini +++ b/gateway/platformio.ini @@ -24,3 +24,26 @@ upload_port = /dev/ttyUSB0 [env:native] platform = native +test_filter = test_native + +[env:embedded] +platform = atmelavr +framework = arduino +board = miniatmega328 +lib_extra_dirs = + ../libraries +lib_deps = + sui77/rc-switch@^2.6.3 + bblanchon/ArduinoJson@6.16.1 + +platform_packages = + platformio/tool-simavr +test_speed = 9600 +test_testing_command = + ${platformio.packages_dir}/tool-simavr/bin/simavr + -m + atmega328p + -f + 16000000L + ${platformio.build_dir}/${this.__env__}/firmware.elf +test_filter = test_embedded diff --git a/gateway/test/test_embedded/sensor_builder.cpp b/gateway/test/test_embedded/sensor_builder.cpp new file mode 100644 index 0000000..a5ea9a2 --- /dev/null +++ b/gateway/test/test_embedded/sensor_builder.cpp @@ -0,0 +1,76 @@ +#include +#include +#include "TinyComponent.h" + +void setUp(void) { + // set stuff up here +} + +void tearDown(void) { + // clean stuff up here +} + +void test_unknown_sensor_type(void) { + StaticJsonDocument<200> jsonDoc; + unsigned long value = ID(SensorId::TEMP_SENSOR) | TYPE(0); + TEST_ASSERT_EQUAL(false, buildSensorJson(value, jsonDoc)); +} + +void test_temp_sensor(void) { + StaticJsonDocument<200> jsonDoc; + unsigned long value = ID(SensorId::TEMP_SENSOR) | TEMP(210) | TYPE(SensorType::TEMPERATURE); + TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc)); + + JsonObject sensor = jsonDoc["sensor"]; + TEST_ASSERT_EQUAL(SensorId::TEMP_SENSOR, sensor["id"]); + TEST_ASSERT_EQUAL(21, sensor["temperature"]); +} + +void test_temp_sensor_with_voltage(void) { + StaticJsonDocument<200> jsonDoc; + unsigned long value = ID(SensorId::TEMP_SENSOR) | TEMP(320) | TYPE(SensorType::TEMPERATURE) | VCC(2847L); + TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc)); + + JsonObject sensor = jsonDoc["sensor"]; + TEST_ASSERT_EQUAL(SensorId::TEMP_SENSOR, sensor["id"]); + TEST_ASSERT_EQUAL(32, sensor["temperature"]); + + JsonObject diagnostic = sensor["diagnostic"]; + TEST_ASSERT_EQUAL(2.847, diagnostic["voltage"]); +} + +void test_oil_sensor(void) { + StaticJsonDocument<200> jsonDoc; + unsigned long value = ID(SensorId::OIL_SENSOR) | VALUE(150) | TYPE(SensorType::GENERIC); + TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc)); + + JsonObject sensor = jsonDoc["sensor"]; + TEST_ASSERT_EQUAL(SensorId::OIL_SENSOR, sensor["id"]); + TEST_ASSERT_EQUAL(150, sensor["value"]); +} + +void test_oil_sensor_with_voltage(void) { + StaticJsonDocument<200> jsonDoc; + unsigned long value = ID(SensorId::OIL_SENSOR) | TEMP(200) | TYPE(SensorType::GENERIC) | VCC(2847L); + TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc)); + + JsonObject sensor = jsonDoc["sensor"]; + TEST_ASSERT_EQUAL(SensorId::OIL_SENSOR, sensor["id"]); + TEST_ASSERT_EQUAL(200, sensor["value"]); + + JsonObject diagnostic = sensor["diagnostic"]; + TEST_ASSERT_EQUAL(2.847, diagnostic["voltage"]); +} + +void setup() { + UNITY_BEGIN(); + RUN_TEST(test_unknown_sensor_type); + RUN_TEST(test_temp_sensor); + RUN_TEST(test_temp_sensor_with_voltage); + RUN_TEST(test_oil_sensor); + RUN_TEST(test_oil_sensor_with_voltage); + UNITY_END(); +} + +void loop() { +} diff --git a/gateway/test/test_decoder.cpp b/gateway/test/test_native/decoder.cpp similarity index 91% rename from gateway/test/test_decoder.cpp rename to gateway/test/test_native/decoder.cpp index 32ba2d7..dea0636 100644 --- a/gateway/test/test_decoder.cpp +++ b/gateway/test/test_native/decoder.cpp @@ -1,6 +1,7 @@ #include -#include "Decoder.h" +#include "RcDecoder.h" +RcDecoder d; void setUp(void) { // set stuff up here @@ -11,7 +12,6 @@ void tearDown(void) { } void test_1_2_on(void) { - Decoder d; d.decode(5574993); TEST_ASSERT_EQUAL(true, d.state); TEST_ASSERT_EQUAL(1, d.group); @@ -19,7 +19,6 @@ void test_1_2_on(void) { } void test_1_1_off(void) { - Decoder d; d.decode(5571924); TEST_ASSERT_EQUAL(false, d.state); TEST_ASSERT_EQUAL(1, d.group);