Add jsonl command

This commit is contained in:
fvanroie 2020-02-29 15:07:27 +01:00
parent b82f8d6df9
commit b54a85fd94
5 changed files with 67 additions and 2 deletions

38
include/StringStream.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef _STRING_STREAM_H_
#define _STRING_STREAM_H_
#include <Stream.h>
class StringStream : public Stream {
public:
StringStream(String & s) : string(s), position(0)
{}
// Stream methods
virtual int available()
{
return string.length() - position;
}
virtual int read()
{
return position < string.length() ? string[position++] : -1;
}
virtual int peek()
{
return position < string.length() ? string[position] : -1;
}
virtual void flush(){};
// Print methods
virtual size_t write(uint8_t c)
{
string += (char)c;
return 1;
};
private:
String & string;
unsigned int length;
unsigned int position;
};
#endif // _STRING_STREAM_H_

View File

@ -82,6 +82,7 @@ void haspBackground(uint16_t pageid, uint16_t imageid);
void haspProcessAttribute(uint8_t pageid, uint8_t objid, String strAttr, String strPayload);
void haspSendCmd(String nextionCmd);
void haspParseJson(String & strPayload);
void haspNewObject(const JsonObject & settings);
void haspReconnect(void);
void haspDisconnect(void);

View File

@ -1,3 +1,6 @@
#include "StringStream.h"
#include "ArduinoJson.h"
#include "hasp_dispatch.h"
#include "hasp_config.h"
#include "hasp_debug.h"
@ -124,6 +127,24 @@ void dispatchJson(char * payload)
}
}
void dispatchJsonl(char * strPayload)
{
Serial.println("JSONL\n");
DynamicJsonDocument config(254);
String output((char *)0);
output.reserve(1500);
StringStream stream((String &)output);
stream.print(strPayload);
while(deserializeJson(config, stream) == DeserializationError::Ok) {
serializeJson(config, Serial);
Serial.println();
haspNewObject(config.as<JsonObject>());
}
}
void IRAM_ATTR dispatchIdle(const __FlashStringHelper * state)
{
mqttSendState(String(F("idle")).c_str(), String(state).c_str());

View File

@ -9,6 +9,7 @@ void dispatchLoop(void);
void dispatchAttribute(String & strTopic, const char * strPayload);
void dispatchCommand(String cmnd);
void dispatchJson(char * strPayload);
void dispatchJsonl(char * strPayload);
void dispatchPage(String strPageid);
void dispatchDim(String strDimLevel);

View File

@ -241,8 +241,12 @@ void mqttCallback(char * topic, byte * payload, unsigned int length)
if(strTopic == F("json")) { // '[...]/device/command/json' -m '["dim=5", "page 1"]' =
// nextionSendCmd("dim=50"), nextionSendCmd("page 1")
dispatchJson((char *)payload); // Send to nextionParseJson()
} else { // '[...]/device/command/p[1].b[4].txt' -m '"Lights On"' ==
// nextionSetAttr("p[1].b[4].txt", "\"Lights On\"")
} else if(strTopic == F("jsonl")) {
dispatchJsonl((char *)payload);
} else if(strTopic == F("setupap")) {
haspDisplayAP("HASP-ABC123", "haspadmin");
} else { // '[...]/device/command/p[1].b[4].txt' -m '"Lights On"' ==
// nextionSetAttr("p[1].b[4].txt", "\"Lights On\"")
dispatchAttribute(strTopic, (char *)payload);
}
return;