add embedded tests for building sensor

This commit is contained in:
Nicu Hodos 2022-10-27 21:22:17 +02:00
parent 7a10b9e770
commit f9ae76aadf
8 changed files with 153 additions and 40 deletions

View File

@ -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

View File

@ -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);

View File

@ -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;
}
};

View File

@ -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;

View File

@ -0,0 +1,32 @@
#include <ArduinoJson.h>
#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;
}

View File

@ -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

View File

@ -0,0 +1,76 @@
#include <Arduino.h>
#include <unity.h>
#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() {
}

View File

@ -1,6 +1,7 @@
#include <unity.h>
#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);