awtrix_upgrade fix + MQTT onstate added

This commit is contained in:
Elfish
2023-04-10 11:43:53 +02:00
parent b7cebe4809
commit a62136d4b3
6 changed files with 54 additions and 22 deletions

View File

@@ -43,7 +43,7 @@ The JSON object has the following properties:
| `sound` | string | The filename of your RTTTL ringtone file (without extension). | |
| `pushIcon` | number | 0 = Icon doesn't move. 1 = Icon moves with text and will not appear again. 2 = Icon moves with text but appears again when the text starts to scroll again. | 0 |
| `bar` | array of integers | draws a bargraph. Without icon maximum 16 values, with icon 11 values | |
| `textCase` | integer | Changes teh Uppercase setting. 0=global setting, 1=forces uppercase; 2=shows what is sent. | 0 |
| `textCase` | integer | Changes the Uppercase setting. 0=global setting, 1=forces uppercase; 2=shows what is sent. | 0 |
All keys are optional, so you can send just the properties you want to use.
@@ -222,7 +222,7 @@ The JSON object has the following properties:
Each value is optional, so you can set a timer for just minutes, or any combination of hours, minutes, and seconds. If you only want to start a timer in some minutes, just send the minutes.
## Example
#### Example
Here's an example JSON object to start a timer for 1 hour, 30 minutes, and 10 seconds, with the sound "friends":
@@ -235,3 +235,11 @@ Here's an example JSON object to start a timer for 1 hour, 30 minutes, and 10 se
}
```
## Turn display on or off
| Topic | URL | Payload/Body | HTTP method |
| --- | --- | --- | --- |
| `[PREFIX]/onstate` | `http://[IP]/api/onstate` | `{"state":value}` | POST |
Valid values are:
- `0` => off
- `1` => on

View File

@@ -37,6 +37,8 @@ CRGB leds[MATRIX_WIDTH * MATRIX_HEIGHT];
FastLED_NeoMatrix *matrix = new FastLED_NeoMatrix(leds, 8, 8, 4, 1, NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE);
MatrixDisplayUi *ui = new MatrixDisplayUi(matrix);
uint8_t lastBrightness;
DisplayManager_ &DisplayManager_::getInstance()
{
static DisplayManager_ instance;
@@ -800,10 +802,12 @@ std::pair<String, AppCallback> getNativeAppByName(const String &appName)
{
return std::make_pair("hum", HumApp);
}
#ifdef ULANZI
else if (appName == "bat")
{
return std::make_pair("bat", BatApp);
}
#endif
return std::make_pair("", nullptr);
}
@@ -941,3 +945,28 @@ String DisplayManager_::getAppsAsJson()
serializeJson(appsObject, json);
return json;
}
void DisplayManager_::onStateParse(const char *json)
{
DynamicJsonDocument doc(512);
DeserializationError error = deserializeJson(doc, json);
if (error)
return;
bool state = doc["state"].as<bool>();
onState(state);
}
void DisplayManager_::onState(bool state)
{
if (state)
{
MATRIX_OFF = false;
setBrightness(lastBrightness);
}
else
{
MATRIX_OFF = true;
lastBrightness = BRIGHTNESS;
setBrightness(0);
}
}

View File

@@ -66,6 +66,8 @@ public:
void setAppTime(uint16_t duration);
String getAppsAsJson();
String getStat();
void onState(bool state);
void onStateParse(const char *json);
};
extern DisplayManager_ &DisplayManager;

View File

@@ -10,7 +10,6 @@
#include "UpdateManager.h"
WiFiClient espClient;
uint8_t lastBrightness;
HADevice device;
HAMqtt mqtt(espClient, device, 22);
@@ -108,18 +107,7 @@ void onRGBColorCommand(HALight::RGBColor color, HALight *sender)
void onStateCommand(bool state, HALight *sender)
{
if (state)
{
MATRIX_OFF = false;
DisplayManager.setBrightness(lastBrightness);
}
else
{
MATRIX_OFF = true;
lastBrightness = BRIGHTNESS;
DisplayManager.setBrightness(0);
}
DisplayManager.onState(state);
sender->setState(state);
}
@@ -129,7 +117,6 @@ void onBrightnessCommand(uint8_t brightness, HALight *sender)
if (AUTO_BRIGHTNESS)
return;
BRIGHTNESS = brightness;
lastBrightness = brightness;
saveSettings();
DisplayManager.setBrightness(brightness);
}
@@ -208,7 +195,13 @@ void onMqttMessage(const char *topic, const uint8_t *payload, uint16_t length)
delete[] payloadCopy;
return;
}
if (strTopic.equals(MQTT_PREFIX + "/onstate"))
{
DisplayManager.onStateParse(payloadCopy);
Serial.println(payloadCopy);
delete[] payloadCopy;
return;
}
else if (strTopic.startsWith(MQTT_PREFIX + "/custom"))
{
String topic_str = topic;
@@ -239,7 +232,8 @@ void onMqttConnected()
"/nextapp",
"/doupdate",
"/nextapp",
"/apps"};
"/apps",
"/onstate"};
for (const char *topic : topics)
{
String fullTopic = prefix + topic;
@@ -273,7 +267,6 @@ char batID[40];
void MQTTManager_::setup()
{
if (HA_DISCOVERY)
{
Serial.println(F("Starting Homeassistant discorvery"));
@@ -368,8 +361,8 @@ void MQTTManager_::setup()
humidity->setName(HAhumName);
humidity->setDeviceClass(HAhumClass);
humidity->setUnitOfMeasurement(HAhumUnit);
#ifdef ULANZI
#ifdef ULANZI
sprintf(batID, HAbatID, macStr);
battery = new HASensor(batID);
battery->setIcon(HAbatIcon);

View File

@@ -96,7 +96,6 @@ void left_button_pressed()
#ifndef ULANZI
PeripheryManager.playFromFile(DFMINI_MP3_CLICK);
#endif
DisplayManager.leftButton();
MenuManager.leftButton();
}
@@ -106,7 +105,6 @@ void right_button_pressed()
#ifndef ULANZI
PeripheryManager.playFromFile(DFMINI_MP3_CLICK);
#endif
DisplayManager.rightButton();
MenuManager.rightButton();
}

View File

@@ -102,6 +102,8 @@ void ServerManager_::setup()
mws.addHandler("/api/doupdate", HTTP_POST, []()
{ if (UPDATE_AVAILABLE)
UpdateManager.updateFirmware(); mws.webserver->send(200,"OK"); });
mws.addHandler("/api/onstate", HTTP_POST, []()
{ DisplayManager.onStateParse(mws.webserver->arg("plain").c_str()); mws.webserver->send(200,"OK"); });
Serial.println("Webserver loaded");
}
mws.addHandler("/version", HTTP_GET, versionHandler);