get OTA enabling working

This commit is contained in:
2018-08-22 11:09:42 +02:00
parent 77f1b278f4
commit 426d495fd9
10 changed files with 49 additions and 35 deletions

View File

@@ -8,24 +8,26 @@
#define JSON_FROM "from"
#define JSON_TO "target"
#define JSON_MSG "msg"
#define JSON_TYPE "type"
struct MeshMessage {
String domain;
String to;
String from;
String msg;
enum MeshMessageType { NONE, SYSTEM, OTA } type;
enum MeshMessageType { NONE, SYSTEM, APP, OTA } type;
int valid = 0;
// ------------------------------------------------------------------------------------------
void init() {
//from = reinterpret_cast<char*>(ESP.getChipId());
}
//void init() {
// from = reinterpret_cast<char*>(ESP.getChipId());
//}
int verifyJsonObject(JsonObject& json){
return json.success()
return json.success();
//&& json.containsKey(JSON_DOMAIN)
//&& json.containsKey(JSON_TO)
//&& json.containsKey(JSON_FROM)
&& json.containsKey(JSON_MSG);
//&& json.containsKey(JSON_TYPE)
// && json.containsKey(JSON_MSG); // msg is only tx'ed from mqtt
};
String toJsonString(){
//StaticJsonBuffer<200> jsonBuffer;
@@ -35,6 +37,7 @@ struct MeshMessage {
root[JSON_TO] = to;
root[JSON_FROM] = from;
root[JSON_MSG] = msg;
root[JSON_TYPE] = type;
String jsonString;
root.printTo(jsonString);
return jsonString;
@@ -52,25 +55,26 @@ struct MeshMessage {
return 0;
}
// Map a json object to this struct.
void fromJsonObject(JsonObject& json){
int fromJsonObject(JsonObject& json){
if(!verifyJsonObject(json)){
Serial.println("ERROR: cannot parse MeshMessage JSON object");
valid = 0;
//return;
return valid;
}
domain = getAttrFromJson(json, JSON_DOMAIN);
to = getAttrFromJson(json, JSON_TO);
from = getAttrFromJson(json, JSON_FROM);
msg = getAttrFromJson(json, JSON_MSG);
type = (MeshMessageType) getIntAttrFromJson(json, "type");
type = (MeshMessageType) getIntAttrFromJson(json, JSON_TYPE);
valid = 1;
return valid;
};
// Parse a json string and map parsed object
void fromJsonString(String& str){
int fromJsonString(String& str){
//StaticJsonBuffer<200> jsonBuffer;
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
JsonObject& json = jsonBuffer.parseObject(str);
fromJsonObject(json);
return fromJsonObject(json);
};
};

View File

@@ -34,8 +34,8 @@ class MeshSprocket : public Sprocket {
}
void dispatchMessageToPlugins(MeshMessage msg){
for(Plugin* p : plugins){
if(msg.type != MeshMessage::NONE){
if(msg.type != MeshMessage::NONE){ // baaaa
for(Plugin* p : plugins){
Serial.println("dispatch to plugins");
p->onMessage(msg);
}
@@ -55,10 +55,8 @@ class MeshSprocket : public Sprocket {
};
void dispatch( uint32_t from, String &msg ) {
// TODO handle OTA before passing to onMessage
MeshMessage mMsg;
mMsg.fromJsonString(msg);
if(mMsg.valid){
if(mMsg.fromJsonString(msg)){
dispatchMessageToPlugins(mMsg);
}
onMessage(from, msg);