mirror of
https://github.com/anklimov/lighthub
synced 2025-12-06 11:49:51 +03:00
interim (compiled) commit
This commit is contained in:
@@ -61,9 +61,9 @@ class systemConfig {
|
|||||||
int openStream(char mode = '\0')
|
int openStream(char mode = '\0')
|
||||||
{
|
{
|
||||||
#if defined(FS_STORAGE)
|
#if defined(FS_STORAGE)
|
||||||
stream.open("/config.bin",mode);
|
stream->open("/config.bin",mode);
|
||||||
#else
|
#else
|
||||||
stream.open(EEPROM_offsetJSON,mode);
|
stream->open(EEPROM_offsetJSON,mode);
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#define _FLASHSTREAM_H_
|
#define _FLASHSTREAM_H_
|
||||||
|
|
||||||
#include <main.h>
|
#include <main.h>
|
||||||
|
#include <WiFiOTA.h>
|
||||||
|
|
||||||
#if defined(FS_STORAGE)
|
#if defined(FS_STORAGE)
|
||||||
#include <FS.h>
|
#include <FS.h>
|
||||||
@@ -40,21 +41,9 @@ extern NRFFlashStorage EEPROM;
|
|||||||
|
|
||||||
#include <Stream.h>
|
#include <Stream.h>
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include "seekablestream.h"
|
||||||
|
|
||||||
class seekableStream : public Stream
|
#define EOF 255
|
||||||
{
|
|
||||||
protected:
|
|
||||||
unsigned int streamSize;
|
|
||||||
bool textMode;
|
|
||||||
|
|
||||||
public:
|
|
||||||
seekableStream(unsigned int size):Stream(),streamSize(size) {};
|
|
||||||
unsigned int getSize() {return streamSize;}
|
|
||||||
virtual unsigned int seek(unsigned int _pos = 0) = 0;
|
|
||||||
virtual int open(unsigned int _startPos=0, unsigned int _size=4096, char mode='\0') = 0;
|
|
||||||
virtual int open(String _filename, char mode='\0') = 0;
|
|
||||||
virtual void close() = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
#if defined(FS_STORAGE)
|
#if defined(FS_STORAGE)
|
||||||
class flashStream : public seekableStream
|
class flashStream : public seekableStream
|
||||||
@@ -64,16 +53,43 @@ String filename;
|
|||||||
char openedMode;
|
char openedMode;
|
||||||
File fs;
|
File fs;
|
||||||
public:
|
public:
|
||||||
flashStream(String _filename):seekableStream(65535)
|
flashStream():seekableStream(65535)
|
||||||
{
|
{
|
||||||
filename=_filename;
|
|
||||||
openedMode='\0';
|
openedMode='\0';
|
||||||
open('r');
|
//fs = 0;
|
||||||
}
|
filename = "";
|
||||||
virtual int open(String _filename, char mode='\0')
|
// open('r');
|
||||||
|
};
|
||||||
|
|
||||||
|
virtual int open(String _filename, char mode) override
|
||||||
{
|
{
|
||||||
textMode = _filename suffix == txt or json
|
char modestr[2];
|
||||||
open file
|
modestr[0]=mode; modestr[1]=0;
|
||||||
|
filename=_filename;
|
||||||
|
|
||||||
|
if (fs = SPIFFS.open(_filename,modestr))
|
||||||
|
{
|
||||||
|
openedMode=mode;
|
||||||
|
//fs.seek(savedPos);
|
||||||
|
|
||||||
|
if (_filename.endsWith(".json")) {contentType=HTTP_TEXT_JSON;textMode=true;}
|
||||||
|
else if (_filename.endsWith(".bin")) {contentType=HTTP_OCTET_STREAM;textMode=false;}
|
||||||
|
else if (_filename.endsWith(".txt")) {contentType=HTTP_TEXT_PLAIN;textMode=true;}
|
||||||
|
|
||||||
|
|
||||||
|
debugSerial<<("Opened/")<<modestr<<(":")<<filename<<endl;
|
||||||
|
return fs;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
openedMode='\0';
|
||||||
|
debugSerial<<("Open error/")<<modestr<<(":")<<filename<<endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
virtual int open(char mode='\0')
|
virtual int open(char mode='\0')
|
||||||
{
|
{
|
||||||
@@ -101,14 +117,21 @@ flashStream(String _filename):seekableStream(65535)
|
|||||||
}
|
}
|
||||||
return fs;
|
return fs;
|
||||||
}
|
}
|
||||||
virtual int available() { if (!fs) return 0; return fs.available(); };
|
virtual int available() override
|
||||||
|
{
|
||||||
|
if (!fs) return 0;
|
||||||
|
if (textMode && peek()==EOF) return 0;
|
||||||
|
return fs.available();
|
||||||
|
};
|
||||||
|
|
||||||
virtual int read() {open('r');return fs.read();};
|
virtual int read() {open('r');return fs.read();};
|
||||||
virtual int peek() {open('r'); return fs.peek();};
|
virtual int peek() {open('r'); return fs.peek();};
|
||||||
virtual unsigned int seek(unsigned int _pos = 0){return open();fs.seek(_pos,SeekSet);};
|
virtual unsigned int seek (unsigned int _pos = 0) override {return open();fs.seek(_pos,SeekSet);};
|
||||||
virtual void close() {fs.flush(); open('r'); };
|
virtual void close() override {fs.close(); };
|
||||||
|
virtual void flush() override {fs.flush(); };
|
||||||
virtual size_t write(uint8_t ch) {open('w'); return fs.write(ch);};
|
virtual size_t write(uint8_t ch) {open('w'); return fs.write(ch);};
|
||||||
using Print::write;
|
using Print::write;
|
||||||
void putEOF(){write (255);};
|
|
||||||
virtual ~flashStream () {if (fs) fs.close();} ;
|
virtual ~flashStream () {if (fs) fs.close();} ;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -123,10 +146,9 @@ unsigned int startPos;
|
|||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
flashStream(unsigned int _startPos=0, unsigned int _size=4096 ):seekableStream(_size)
|
flashStream(unsigned int _size=4096 ):seekableStream(_size)
|
||||||
{
|
{
|
||||||
pos = 0; startPos = _startPos;
|
// pos = 0; startPos = _startPos;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void setSize(unsigned int _size) {streamSize=_size;};
|
void setSize(unsigned int _size) {streamSize=_size;};
|
||||||
@@ -139,6 +161,7 @@ flashStream(unsigned int _startPos=0, unsigned int _size=4096 ):seekableStream(_
|
|||||||
streamSize = _size;
|
streamSize = _size;
|
||||||
startPos = EEPROM_offsetJSON;
|
startPos = EEPROM_offsetJSON;
|
||||||
textMode = true;
|
textMode = true;
|
||||||
|
contentType = HTTP_TEXT_JSON;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -148,11 +171,13 @@ flashStream(unsigned int _startPos=0, unsigned int _size=4096 ):seekableStream(_
|
|||||||
startPos = SYSCONF_OFFSET;
|
startPos = SYSCONF_OFFSET;
|
||||||
streamSize = SYSCONF_SIZE;
|
streamSize = SYSCONF_SIZE;
|
||||||
textMode =false;
|
textMode =false;
|
||||||
|
contentType = HTTP_OCTET_STREAM;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else return 0;
|
else return 0;
|
||||||
};
|
};
|
||||||
|
/*
|
||||||
virtual int open(unsigned int _startPos=0, unsigned int _size=4096 char mode='\0')
|
virtual int open(unsigned int _startPos=0, unsigned int _size=4096 char mode='\0')
|
||||||
{
|
{
|
||||||
pos = 0;
|
pos = 0;
|
||||||
@@ -160,7 +185,7 @@ flashStream(unsigned int _startPos=0, unsigned int _size=4096 ):seekableStream(_
|
|||||||
streamSize = _size;
|
streamSize = _size;
|
||||||
return 1;
|
return 1;
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
virtual unsigned int seek(unsigned int _pos = 0)
|
virtual unsigned int seek(unsigned int _pos = 0)
|
||||||
{ pos=min(_pos, streamSize);
|
{ pos=min(_pos, streamSize);
|
||||||
debugSerial<<F("Seek:")<<pos<<endl;
|
debugSerial<<F("Seek:")<<pos<<endl;
|
||||||
@@ -168,6 +193,7 @@ flashStream(unsigned int _startPos=0, unsigned int _size=4096 ):seekableStream(_
|
|||||||
};
|
};
|
||||||
virtual int available() {
|
virtual int available() {
|
||||||
//debugSerial<<pos<<"%"<<streamSize<<";";
|
//debugSerial<<pos<<"%"<<streamSize<<";";
|
||||||
|
if (textMode && peek()==EOF) return 0;
|
||||||
return (pos<streamSize);
|
return (pos<streamSize);
|
||||||
};
|
};
|
||||||
virtual int read()
|
virtual int read()
|
||||||
@@ -203,14 +229,6 @@ flashStream(unsigned int _startPos=0, unsigned int _size=4096 ):seekableStream(_
|
|||||||
EEPROM.write(startPos+pos++,(char)ch);
|
EEPROM.write(startPos+pos++,(char)ch);
|
||||||
return 1;
|
return 1;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// #if defined(__SAM3X8E__)
|
|
||||||
// return EEPROM.write(pos++,(char)ch);
|
|
||||||
// #else
|
|
||||||
// EEPROM.update(pos++,(char)ch);
|
|
||||||
// return 1;
|
|
||||||
// #endif
|
|
||||||
// return 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#if defined(__SAM3X8E__)
|
#if defined(__SAM3X8E__)
|
||||||
@@ -224,11 +242,19 @@ flashStream(unsigned int _startPos=0, unsigned int _size=4096 ):seekableStream(_
|
|||||||
#else
|
#else
|
||||||
using Print::write;//(const uint8_t *buffer, size_t size);
|
using Print::write;//(const uint8_t *buffer, size_t size);
|
||||||
#endif
|
#endif
|
||||||
void putEOF(){write (255);
|
|
||||||
#if defined(ESP8266) || defined(ESP32)
|
#if defined(ESP8266) || defined(ESP32)
|
||||||
|
virtual void putEOF() override
|
||||||
|
{
|
||||||
|
write (255);
|
||||||
EEPROM.commit();
|
EEPROM.commit();
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
virtual void close() override
|
||||||
|
{
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ flashStream JSONStream(EEPROM_offsetJSON,MAX_JSON_CONF_SIZE);
|
|||||||
#endif
|
#endif
|
||||||
*/
|
*/
|
||||||
|
|
||||||
flashStream sysConfStream();
|
flashStream sysConfStream;
|
||||||
|
|
||||||
systemConfig sysConf(&sysConfStream);
|
systemConfig sysConf(&sysConfStream);
|
||||||
|
|
||||||
@@ -221,23 +221,28 @@ debugSerial<<F("Deleting conf. RAM was:")<<freeRam();
|
|||||||
bool isNotRetainingStatus() {
|
bool isNotRetainingStatus() {
|
||||||
return (lanStatus != RETAINING_COLLECTING);
|
return (lanStatus != RETAINING_COLLECTING);
|
||||||
}
|
}
|
||||||
|
// Custom HTTP request handler
|
||||||
|
// Return values:
|
||||||
|
// 1 - work completed. Dont need to respond, just close socket
|
||||||
|
// 0 - no match continue with default lib behavior
|
||||||
|
// 401 - not authorized
|
||||||
|
// 400 - bad request
|
||||||
|
// 200 | CONTENT_TYPE - ok + content_type
|
||||||
|
// ... same sor other http codes
|
||||||
|
// if response != "" - put response in http answer
|
||||||
|
|
||||||
int httpHandler(Client& client, String request, long contentLength, bool authorized)
|
uint16_t httpHandler(Client& client, String request, long contentLength, bool authorized, String& response )
|
||||||
{
|
{
|
||||||
String response = "";
|
//String response = "";
|
||||||
|
|
||||||
debugSerial<<request<<endl;
|
debugSerial<<request<<endl;
|
||||||
|
|
||||||
|
|
||||||
if (request == (F("GET /")))
|
if (request == (F("GET /")))
|
||||||
{
|
{
|
||||||
client.println(F("HTTP/1.1 301 Moved permanently"));
|
response = "Location: http://lazyhome.ru/pwa?mac=";
|
||||||
client.print(F("Location: http://lazyhome.ru/pwa?mac="));
|
// todo for (int i=0; i<6; i++) {response+=(sysConf.mac[i]>>4,HEX);response+=(sysConf.mac[i]&0xf,HEX);}
|
||||||
for (int i=0; i<6; i++) {client.print(sysConf.mac[i]>>4,HEX);client.print(sysConf.mac[i]&0xf,HEX);}
|
response+=(F("&ip="));
|
||||||
client.print(F("&ip="));
|
//todo response+=(Ethernet.localIP());
|
||||||
client.println(Ethernet.localIP());
|
|
||||||
delay(100);
|
delay(100);
|
||||||
return 1;
|
return 301;
|
||||||
}
|
}
|
||||||
if (!authorized) return 401;
|
if (!authorized) return 401;
|
||||||
|
|
||||||
@@ -252,6 +257,7 @@ int httpHandler(Client& client, String request, long contentLength, bool authori
|
|||||||
ic.loadItem(&item,SEND_COMMAND|SEND_PARAMETERS);
|
ic.loadItem(&item,SEND_COMMAND|SEND_PARAMETERS);
|
||||||
char buf[32];
|
char buf[32];
|
||||||
response=ic.toString(buf, sizeof(buf));
|
response=ic.toString(buf, sizeof(buf));
|
||||||
|
return 200 | HTTP_TEXT_PLAIN;
|
||||||
}
|
}
|
||||||
else if (request.startsWith(F("GET /item/")))
|
else if (request.startsWith(F("GET /item/")))
|
||||||
{
|
{
|
||||||
@@ -265,6 +271,7 @@ int httpHandler(Client& client, String request, long contentLength, bool authori
|
|||||||
{if (item.isActive()) item.setCmd(CMD_ON); else item.setCmd(CMD_OFF);}
|
{if (item.isActive()) item.setCmd(CMD_ON); else item.setCmd(CMD_OFF);}
|
||||||
char buf[32];
|
char buf[32];
|
||||||
response=ic.toString(buf, sizeof(buf));
|
response=ic.toString(buf, sizeof(buf));
|
||||||
|
return 200 | HTTP_TEXT_PLAIN;
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (request.startsWith(F("POST /command/")))
|
else if (request.startsWith(F("POST /command/")))
|
||||||
@@ -279,25 +286,9 @@ int httpHandler(Client& client, String request, long contentLength, bool authori
|
|||||||
if (request=="reboot ") client.stop();
|
if (request=="reboot ") client.stop();
|
||||||
const char* res=request.c_str();
|
const char* res=request.c_str();
|
||||||
cmd_parse((char*) res);
|
cmd_parse((char*) res);
|
||||||
|
return 200 | HTTP_TEXT_PLAIN;
|
||||||
}
|
}
|
||||||
else return -1; //Unknown
|
else return 0; //Unknown
|
||||||
|
|
||||||
client.println(F("HTTP/1.1 200 OK"));
|
|
||||||
client.println(F("Connection: close"));
|
|
||||||
|
|
||||||
#ifdef CORS
|
|
||||||
client.print(F("Access-Control-Allow-Origin: "));
|
|
||||||
client.println(F(CORS));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (response!="") {
|
|
||||||
client.println(F("Content-Type: text/plain"));
|
|
||||||
client.println();
|
|
||||||
client.println(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(100);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int inTopic (char * topic, topicType tt)
|
int inTopic (char * topic, topicType tt)
|
||||||
@@ -1003,7 +994,7 @@ void Changed(int i, DeviceAddress addr, float currentTemp) {
|
|||||||
//char addrbuf[17];
|
//char addrbuf[17];
|
||||||
//char valstr[16] = "NIL";
|
//char valstr[16] = "NIL";
|
||||||
//char *owEmitString = NULL;
|
//char *owEmitString = NULL;
|
||||||
char *owItem = NULL;
|
//char *owItem = NULL;
|
||||||
|
|
||||||
SetBytes(addr, 8, addrstr);
|
SetBytes(addr, 8, addrstr);
|
||||||
addrstr[17] = 0;
|
addrstr[17] = 0;
|
||||||
@@ -1240,8 +1231,8 @@ int loadConfigFromEEPROM()
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
//JSONStream.seek();
|
//JSONStream.seek();
|
||||||
if (JSONStream.peek() == '{') {
|
if (sysConfStream.peek() == '{') {
|
||||||
aJsonStream as = aJsonStream(&JSONStream);
|
aJsonStream as = aJsonStream(&sysConfStream);
|
||||||
cleanConf();
|
cleanConf();
|
||||||
root = aJson.parse(&as);
|
root = aJson.parse(&as);
|
||||||
sysConfStream.close();
|
sysConfStream.close();
|
||||||
@@ -1255,7 +1246,7 @@ int loadConfigFromEEPROM()
|
|||||||
ethClient.stop(); //Refresh MQTT connect to get retained info
|
ethClient.stop(); //Refresh MQTT connect to get retained info
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
JSONStream.writeEOF(); //truncate garbage
|
sysConfStream.putEOF(); //truncate garbage
|
||||||
infoSerial<<F("No stored config")<<endl;
|
infoSerial<<F("No stored config")<<endl;
|
||||||
sysConfStream.close();
|
sysConfStream.close();
|
||||||
return 0;
|
return 0;
|
||||||
@@ -1297,11 +1288,11 @@ if (arg_cnt>1)
|
|||||||
#else
|
#else
|
||||||
//JSONStream.open('w');
|
//JSONStream.open('w');
|
||||||
//JSONStream.seek();
|
//JSONStream.seek();
|
||||||
aJsonStream jsonEEPROMStream = aJsonStream(&JSONStream);
|
aJsonStream jsonEEPROMStream = aJsonStream(&sysConfStream);
|
||||||
infoSerial<<F("Saving config to EEPROM..");
|
infoSerial<<F("Saving config to EEPROM..");
|
||||||
aJson.print(root, &jsonEEPROMStream);
|
aJson.print(root, &jsonEEPROMStream);
|
||||||
JSONStream.putEOF();
|
sysConfStream.putEOF();
|
||||||
JSONStream.close();
|
sysConfStream.close();
|
||||||
infoSerial<<F("Saved to EEPROM")<<endl;
|
infoSerial<<F("Saved to EEPROM")<<endl;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
25
lighthub/seekablestream.h
Normal file
25
lighthub/seekablestream.h
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef _SEEKABLESTREAM_H_
|
||||||
|
#define _SEEKABLESTREAM_H_
|
||||||
|
|
||||||
|
#include <Stream.h>
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
class seekableStream : public Stream
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
unsigned int streamSize;
|
||||||
|
bool textMode;
|
||||||
|
uint16_t contentType;
|
||||||
|
|
||||||
|
public:
|
||||||
|
seekableStream(unsigned int size):Stream(),streamSize(size) {};
|
||||||
|
unsigned int getSize() {return streamSize;}
|
||||||
|
virtual unsigned int seek(unsigned int _pos = 0) = 0;
|
||||||
|
//virtual int open(unsigned int _startPos=0, unsigned int _size=4096, char mode='\0') = 0;
|
||||||
|
virtual int open(String _filename, char mode) = 0;
|
||||||
|
virtual void close() = 0;
|
||||||
|
virtual uint16_t getContentType() {return contentType;};
|
||||||
|
virtual void putEOF() {if (textMode) write (EOF);};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user