Skip to content

Commit

Permalink
Bump version to v1.3.3
Browse files Browse the repository at this point in the history
* Add api write object, array
* Add Modbus action
* Fix modbus tcp
* Add function support ERaString
* Some refactor...
  • Loading branch information
hung-eoh committed Jun 30, 2024
1 parent 40d0299 commit 30bf631
Show file tree
Hide file tree
Showing 39 changed files with 1,434 additions and 267 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ERa",
"version": "1.3.2",
"version": "1.3.3",
"description": "E-Ra by EoH. An IoT Market Enabler! It supports WiFi, Ethernet, Zigbee, Modbus, Serial. Works with boards like Arduino, ESP8266, ESP32, STM32, Raspberry Pi...",
"keywords": "ERa, E-Ra, esp8266, esp32, stm32, raspberry-pi, http, mqtt, zigbee, modbus, sensors, control, device, smartphone, mobile, app, web, cloud, communication, protocol, iot, wifi, ethernet, serial",
"authors":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ERa
version=1.3.2
version=1.3.3
author=EoH Ltd
license=MIT
maintainer=EoH Ltd <info@eoh.io>
Expand Down
6 changes: 3 additions & 3 deletions linux/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

ERA_MAJOR = 1
ERA_MINOR = 3
ERA_PATCH = 2
ERA_VERSION = "1.3.2"
ERA_FIRMWARE_VERSION = "1.3.2"
ERA_PATCH = 3
ERA_VERSION = "1.3.3"
ERA_FIRMWARE_VERSION = "1.3.3"

BUTTON_GPIO = 16

Expand Down
111 changes: 85 additions & 26 deletions src/ERa/ERaApi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,42 +77,60 @@ class ERaApi
{}

template <typename T>
void virtualWrite(int pin, T value, bool send = false) {
void virtualWrite(int pin, const T& value, bool send = false) {
#if defined(ERA_VIRTUAL_WRITE_LEGACY)
this->virtualWriteSingle(pin, value);
this->virtualWriteSingle(pin, value, false);
ERA_FORCE_UNUSED(send);
#else
Property::virtualWriteProperty(pin, value, send);
#endif
}

template <typename T, typename... Args>
void virtualWrite(int pin, T value, Args... tail) {
void virtualWrite(int pin, const T& value, const Args&... tail) {
this->virtualWriteMulti(pin, value, tail...);
}

void virtualWriteObject(const char* value) {
template <typename... Args>
void virtualObject(int pin, const Args&... tail) {
ERaDataJson data;
data.add_multi(tail...);
#if defined(ERA_VIRTUAL_WRITE_LEGACY)
this->virtualWriteSingle(pin, data, ERA_API_JSON);
#else
Property::virtualObjectProperty(pin, data, false);
#endif
}

template <typename... Args>
void virtualArray(int pin, const Args&... tail) {
ERaDataJson data;
data.array_multi(tail...);
#if defined(ERA_VIRTUAL_WRITE_LEGACY)
this->virtualWriteSingle(pin, data, ERA_API_JSON);
#else
Property::virtualArrayProperty(pin, data, false);
#endif
}

void virtualObject(const char* value) {
ERaDataJson data(value);
this->virtualWriteObject(data);
this->virtualObject(data);
}

void virtualWriteObject(cJSON* value) {
void virtualObject(cJSON* value) {
ERaDataJson data(value);
this->virtualWriteObject(data);
this->virtualObject(data);
}

void virtualWriteObject(ERaDataJson& value) {
ERaRsp_t rsp;
rsp.type = ERaTypeWriteT::ERA_WRITE_VIRTUAL_PIN_MULTI;
rsp.retained = ERA_MQTT_PUBLISH_RETAINED;
rsp.id = 0;
rsp.param = 0;
this->thisProto().sendCommand(rsp, &value);
void virtualObject(ERaDataJson& value) {
this->virtualWriteMultiReal(value, false);
}

void digitalWrite(int pin, bool value) {
ERaRsp_t rsp;
rsp.type = ERaTypeWriteT::ERA_WRITE_DIGITAL_PIN;
rsp.json = false;
rsp.retained = ERA_MQTT_PUBLISH_RETAINED;
rsp.id = pin;
rsp.param = value;
Expand All @@ -122,6 +140,7 @@ class ERaApi
void analogWrite(int pin, int value) {
ERaRsp_t rsp;
rsp.type = ERaTypeWriteT::ERA_WRITE_ANALOG_PIN;
rsp.json = false;
rsp.retained = ERA_MQTT_PUBLISH_RETAINED;
rsp.id = pin;
rsp.param = value;
Expand All @@ -131,6 +150,7 @@ class ERaApi
void pwmWrite(int pin, int value) {
ERaRsp_t rsp;
rsp.type = ERaTypeWriteT::ERA_WRITE_PWM_PIN;
rsp.json = false;
rsp.retained = ERA_MQTT_PUBLISH_RETAINED;
rsp.id = pin;
rsp.param = value;
Expand All @@ -140,24 +160,26 @@ class ERaApi
void pinWrite(int pin, int value) {
ERaRsp_t rsp;
rsp.type = ERaTypeWriteT::ERA_WRITE_PIN;
rsp.json = false;
rsp.retained = ERA_MQTT_PUBLISH_RETAINED;
rsp.id = pin;
rsp.param = value;
this->thisProto().sendCommand(rsp);
}

template <typename T>
void configIdWrite(ERaInt_t configId, T value) {
void configIdWrite(ERaInt_t configId, const T& value) {
ERaRsp_t rsp;
rsp.type = ERaTypeWriteT::ERA_WRITE_CONFIG_ID;
rsp.json = false;
rsp.retained = ERA_MQTT_PUBLISH_RETAINED;
rsp.id = configId;
rsp.param = value;
this->thisProto().sendCommand(rsp);
}

template <typename T, typename... Args>
void configIdWrite(ERaInt_t configId, T value, Args... tail) {
void configIdWrite(ERaInt_t configId, const T& value, const Args&... tail) {
this->configIdMultiWrite(configId, value, tail...);
}

Expand All @@ -178,6 +200,7 @@ class ERaApi
#else
ERaRsp_t rsp;
rsp.type = ERaTypeWriteT::ERA_WRITE_SPECIFIC_DATA;
rsp.json = false;
rsp.retained = retained;
rsp.id = id;
rsp.param = value;
Expand All @@ -194,6 +217,7 @@ class ERaApi
#else
ERaRsp_t rsp;
rsp.type = ERaTypeWriteT::ERA_WRITE_SPECIFIC_DATA;
rsp.json = false;
rsp.retained = retained;
rsp.id = id;
rsp.param = value;
Expand Down Expand Up @@ -426,6 +450,7 @@ class ERaApi
#else
ERaRsp_t rsp;
rsp.type = ERaTypeWriteT::ERA_WRITE_MODBUS_DATA;
rsp.json = false;
rsp.retained = ERA_MQTT_PUBLISH_RETAINED;
rsp.id = 0;
rsp.param = 0;
Expand All @@ -443,6 +468,7 @@ class ERaApi
#else
ERaRsp_t rsp;
rsp.type = ERaTypeWriteT::ERA_WRITE_ZIGBEE_DATA;
rsp.json = false;
rsp.retained = retained;
rsp.id = id;
rsp.param = value;
Expand Down Expand Up @@ -550,33 +576,40 @@ class ERaApi

private:
template <typename T>
void virtualWriteSingle(int pin, T value) {
void virtualWriteSingle(int pin, const T& value, bool json = false) {
ERaRsp_t rsp;
rsp.type = ERaTypeWriteT::ERA_WRITE_VIRTUAL_PIN;
rsp.json = json;
rsp.retained = ERA_MQTT_PUBLISH_RETAINED;
rsp.id = pin;
rsp.param = value;
this->thisProto().sendCommand(rsp);
}

template <typename... Args>
void virtualWriteMulti(Args... tail) {
ERaRsp_t rsp;
void virtualWriteMulti(const Args&... tail) {
ERaDataJson data;
data.add_multi(tail...);
this->virtualWriteMultiReal(data, false);
}

void virtualWriteMultiReal(ERaDataJson& value, bool json = false) {
ERaRsp_t rsp;
rsp.type = ERaTypeWriteT::ERA_WRITE_VIRTUAL_PIN_MULTI;
rsp.json = json;
rsp.retained = ERA_MQTT_PUBLISH_RETAINED;
rsp.id = 0;
rsp.param = 0;
this->thisProto().sendCommand(rsp, &data);
this->thisProto().sendCommand(rsp, &value);
}

template <typename... Args>
void configIdMultiWrite(Args... tail) {
void configIdMultiWrite(const Args&... tail) {
ERaRsp_t rsp;
ERaDataJson data;
data.add_multi(tail...);
rsp.type = ERaTypeWriteT::ERA_WRITE_CONFIG_ID_MULTI;
rsp.json = false;
rsp.retained = ERA_MQTT_PUBLISH_RETAINED;
rsp.id = 0;
rsp.param = data.getObject();
Expand Down Expand Up @@ -761,8 +794,10 @@ void ERaApi<Proto, Flash>::processVirtualPinRequest(const ERaDataBuff& arrayTopi
ERaParam param(data);
uint8_t pin = ERA_DECODE_PIN_NAME(str);
cJSON* item = cJSON_GetObjectItem(root, "value");
if (cJSON_IsNumber(item) ||
cJSON_IsBool(item)) {
if (cJSON_IsBool(item)) {
param.add(item->valueint);
}
else if (cJSON_IsNumber(item)) {
param.add(item->valuedouble);
}
else if (cJSON_IsString(item)) {
Expand All @@ -771,8 +806,17 @@ void ERaApi<Proto, Flash>::processVirtualPinRequest(const ERaDataBuff& arrayTopi
else if (item != nullptr) {
param.add(item);
}

if (cJSON_IsObject(item) ||
cJSON_IsArray(item)) {
data.detachObject();
data.setObject(item, false);
}

this->callERaWriteHandler(pin, param);

data.detachObject();
cJSON_Delete(root);
root = nullptr;
item = nullptr;
}
Expand Down Expand Up @@ -1155,9 +1199,10 @@ template <class Proto, class Flash>
inline
void ERaApi<Proto, Flash>::callERaProHandler(const char* deviceId, const cJSON* const root) {
char id[65] {0};
ERaParam param;
ERaDataJson data;
cJSON* current = nullptr;
for (current = root->child; current != nullptr && current->string != nullptr; current = current->next) {
ERaParam param;
ClearArray(id);
FormatString(id, "%s:%s", deviceId, current->string);
if (cJSON_IsBool(current)) {
Expand All @@ -1166,15 +1211,22 @@ void ERaApi<Proto, Flash>::callERaProHandler(const char* deviceId, const cJSON*
else if (cJSON_IsNumber(current)) {
param = current->valuedouble;
}
else if (cJSON_IsString(current)){
param = current->valuestring;
else if (cJSON_IsString(current)) {
param.add_static(current->valuestring);
}
else if (cJSON_IsObject(current) ||
cJSON_IsArray(current)) {
data.setObject(current, false);
param = current;
param = data;
}
else {
continue;
}
#if !defined(ERA_ABBR)
Property::handler(id, param);
#endif
data.detachObject();
}
}

Expand Down Expand Up @@ -1220,6 +1272,7 @@ void ERaApi<Proto, Flash>::callERaProHandler(const char* deviceId, const cJSON*
ERaEvent_t event;
event.type = ERaTypeWriteT::ERA_WRITE_MODBUS_DATA;
event.specific = false;
event.json = false;
event.retained = ERA_MQTT_PUBLISH_RETAINED;
event.id = nullptr;
event.data = value;
Expand All @@ -1236,6 +1289,7 @@ void ERaApi<Proto, Flash>::callERaProHandler(const char* deviceId, const cJSON*
ERaRsp_t rsp;
ERaDataBuff* data = (ERaDataBuff*)event.data;
rsp.type = ERaTypeWriteT::ERA_WRITE_MODBUS_DATA;
rsp.json = false;
rsp.retained = ERA_MQTT_PUBLISH_RETAINED;
rsp.id = 0;
rsp.param = 0;
Expand All @@ -1255,6 +1309,7 @@ void ERaApi<Proto, Flash>::callERaProHandler(const char* deviceId, const cJSON*
ERaEvent_t event;
event.type = ERaTypeWriteT::ERA_WRITE_ZIGBEE_DATA;
event.specific = specific;
event.json = false;
event.retained = retained;
if (!specific) {
event.id = (void*)id;
Expand All @@ -1277,6 +1332,7 @@ void ERaApi<Proto, Flash>::callERaProHandler(const char* deviceId, const cJSON*

ERaRsp_t rsp;
rsp.type = ERaTypeWriteT::ERA_WRITE_ZIGBEE_DATA;
rsp.json = event.json;
rsp.retained = event.retained;
rsp.id.add_static((char*)event.id);
if (!event.specific) {
Expand Down Expand Up @@ -1307,6 +1363,7 @@ void ERaApi<Proto, Flash>::callERaProHandler(const char* deviceId, const cJSON*
ERaEvent_t event;
event.type = ERaTypeWriteT::ERA_WRITE_SPECIFIC_DATA;
event.specific = specific;
event.json = false;
event.retained = retained;
if (!specific) {
event.id = (void*)id;
Expand All @@ -1330,6 +1387,7 @@ void ERaApi<Proto, Flash>::callERaProHandler(const char* deviceId, const cJSON*
ERaEvent_t event;
event.type = ERaTypeWriteT::ERA_WRITE_SPECIFIC_DATA;
event.specific = specific;
event.json = false;
event.retained = retained;
if (!specific) {
event.id = (void*)id;
Expand All @@ -1352,6 +1410,7 @@ void ERaApi<Proto, Flash>::callERaProHandler(const char* deviceId, const cJSON*

ERaRsp_t rsp;
rsp.type = ERaTypeWriteT::ERA_WRITE_SPECIFIC_DATA;
rsp.json = false;
rsp.retained = event.retained;
rsp.id.add_static((char*)event.id);
if (!event.specific) {
Expand Down
2 changes: 2 additions & 0 deletions src/ERa/ERaApiDef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ enum ERaTypeWriteT {

typedef struct __ERaRsp_t {
uint8_t type;
bool json;
bool retained;
ERaParam id;
ERaParam param;
Expand All @@ -96,6 +97,7 @@ typedef struct __ERaRsp_t {
typedef struct __ERaEvent_t {
uint8_t type;
bool specific;
bool json;
bool retained;
void* id;
void* data;
Expand Down
7 changes: 7 additions & 0 deletions src/ERa/ERaApiHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdint.h>
#include <ERa/ERaDefine.hpp>
#include <ERa/ERaConfig.hpp>
#include <ERa/ERaState.hpp>
#include <ERa/ERaDebug.hpp>
#include <ERa/ERaTimer.hpp>
#include <ERa/ERaCallbacksHelper.hpp>
Expand Down Expand Up @@ -129,6 +130,12 @@ class ERaApiHandler
this->ERaTm.run();
}

virtual bool afterNetwork() {
return (ERaState::is(StateT::STATE_RUNNING) ||
ERaState::is(StateT::STATE_CONNECTED) ||
ERaState::is(StateT::STATE_CONNECTING_CLOUD));
}

virtual bool connected() = 0;

protected:
Expand Down
Loading

0 comments on commit 30bf631

Please sign in to comment.