mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-27 13:16:45 +00:00
Use CharStream instead of StringStream
This commit is contained in:
parent
f79a7cc231
commit
94a34daab9
43
lib/CharStream/CharStream.h
Normal file
43
lib/CharStream/CharStream.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#ifndef _CHAR_STREAM_H_
|
||||||
|
#define _CHAR_STREAM_H_
|
||||||
|
|
||||||
|
#include <Stream.h>
|
||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
class CharStream : public Stream {
|
||||||
|
public:
|
||||||
|
CharStream(char * s) : string(s), position(0)
|
||||||
|
{}
|
||||||
|
|
||||||
|
// Stream methods
|
||||||
|
virtual int available()
|
||||||
|
{
|
||||||
|
return strlen(string) - position;
|
||||||
|
}
|
||||||
|
virtual int read()
|
||||||
|
{
|
||||||
|
return position < strlen(string) ? string[position++] : -1;
|
||||||
|
}
|
||||||
|
virtual int peek()
|
||||||
|
{
|
||||||
|
return position < strlen(string) ? string[position] : -1;
|
||||||
|
}
|
||||||
|
virtual void flush(){};
|
||||||
|
// Print methods
|
||||||
|
virtual size_t write(uint8_t c)
|
||||||
|
{
|
||||||
|
/* char buf[2];
|
||||||
|
buf[0] = c;
|
||||||
|
buf[1] = '\0';
|
||||||
|
strncat((char *)string, buf, 1);
|
||||||
|
return 1;*/
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
char * string;
|
||||||
|
unsigned int length;
|
||||||
|
unsigned int position;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // _CHAR_STREAM_H_
|
15
src/hasp.cpp
15
src/hasp.cpp
@ -744,7 +744,8 @@ void haspNewObject(const JsonObject & config, uint8_t & saved_page_id)
|
|||||||
/* ----- Basic Objects ------ */
|
/* ----- Basic Objects ------ */
|
||||||
case LV_HASP_BUTTON: {
|
case LV_HASP_BUTTON: {
|
||||||
obj = lv_btn_create(parent_obj, NULL);
|
obj = lv_btn_create(parent_obj, NULL);
|
||||||
lv_obj_t * label = lv_label_create(obj, NULL);
|
/* lv_obj_t * label ; */
|
||||||
|
lv_label_create(obj, NULL);
|
||||||
// haspSetOpacity(obj, LV_OPA_COVER);
|
// haspSetOpacity(obj, LV_OPA_COVER);
|
||||||
lv_obj_set_event_cb(obj, btn_event_handler);
|
lv_obj_set_event_cb(obj, btn_event_handler);
|
||||||
break;
|
break;
|
||||||
@ -913,18 +914,10 @@ void haspLoadPage(String pages)
|
|||||||
Log.notice(F("HASP: Loading file %s"), pages.c_str());
|
Log.notice(F("HASP: Loading file %s"), pages.c_str());
|
||||||
|
|
||||||
File file = SPIFFS.open(pages, "r");
|
File file = SPIFFS.open(pages, "r");
|
||||||
// ReadBufferingStream bufferingStream(file, 256);
|
dispatchJsonl(file);
|
||||||
DynamicJsonDocument config(256);
|
file.close();
|
||||||
|
|
||||||
uint8_t savedPage = current_page;
|
|
||||||
while(deserializeJson(config, file) == DeserializationError::Ok) {
|
|
||||||
// serializeJson(config, Serial);
|
|
||||||
// Serial.println();
|
|
||||||
haspNewObject(config.as<JsonObject>(), savedPage);
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.notice(F("HASP: File %s loaded"), pages.c_str());
|
Log.notice(F("HASP: File %s loaded"), pages.c_str());
|
||||||
file.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "ArduinoJson.h"
|
#include "ArduinoJson.h"
|
||||||
#include "ArduinoLog.h"
|
#include "ArduinoLog.h"
|
||||||
#include "StringStream.h"
|
#include "StringStream.h"
|
||||||
|
#include "CharStream.h"
|
||||||
|
|
||||||
#include "hasp_dispatch.h"
|
#include "hasp_dispatch.h"
|
||||||
#include "hasp_config.h"
|
#include "hasp_config.h"
|
||||||
@ -249,20 +250,24 @@ void dispatchJson(char * payload)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void dispatchJsonl(Stream & stream)
|
||||||
|
{
|
||||||
|
DynamicJsonDocument jsonl(3 * 128u);
|
||||||
|
uint8_t savedPage = haspGetPage();
|
||||||
|
|
||||||
|
Log.notice(F("DISPATCH: jsonl"));
|
||||||
|
|
||||||
|
while(deserializeJson(jsonl, stream) == DeserializationError::Ok) {
|
||||||
|
serializeJson(jsonl, Serial);
|
||||||
|
Serial.println();
|
||||||
|
haspNewObject(jsonl.as<JsonObject>(), savedPage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void dispatchJsonl(char * payload)
|
void dispatchJsonl(char * payload)
|
||||||
{
|
{
|
||||||
uint8_t savedPage = 0;
|
CharStream stream(payload);
|
||||||
DynamicJsonDocument config(3 * 128u);
|
dispatchJsonl(stream);
|
||||||
String output((char *)0);
|
|
||||||
StringStream stream((String &)output);
|
|
||||||
output.reserve(3 * 128u);
|
|
||||||
|
|
||||||
stream.print(payload);
|
|
||||||
while(deserializeJson(config, stream) == DeserializationError::Ok) {
|
|
||||||
serializeJson(config, Serial);
|
|
||||||
Serial.println();
|
|
||||||
haspNewObject(config.as<JsonObject>(), savedPage);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void dispatchIdle(const char * state)
|
void dispatchIdle(const char * state)
|
||||||
|
@ -10,6 +10,7 @@ void dispatchAttribute(String strTopic, const char * strPayload);
|
|||||||
void dispatchCommand(String cmnd);
|
void dispatchCommand(String cmnd);
|
||||||
void dispatchJson(char * strPayload);
|
void dispatchJson(char * strPayload);
|
||||||
void dispatchJsonl(char * strPayload);
|
void dispatchJsonl(char * strPayload);
|
||||||
|
void dispatchJsonl(Stream & stream);
|
||||||
|
|
||||||
void dispatchPage(String strPageid);
|
void dispatchPage(String strPageid);
|
||||||
void dispatchClearPage(String strPageid);
|
void dispatchClearPage(String strPageid);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user