/* * configDTZ.h * File-class for DTZ-Data-Logger * for configuration of user-data * * author Creator P.Rebesky * Copyright (©): 2020-2022 by Peter Rebesky * This code can use in private cases only. Every business or companies using of this codes or codes parts is required an approval of us (me) * Every private use can exchange some parts of code or modify some code-lines. This code can change for private use only. * This software is basicly owned by Peter Rebesky and any comercial using is forbidden without approval of us (me). * Version 5.00 * 04.11.2022 */ #ifndef CONFIG_DTZ_H_ #define CONFIG_DTZ_H_ // class for manage config-data into and from file "config.ini" #include // LittleFS is declared #ifdef SML_DIN #include "decodeMeterSML.h" #endif #ifdef ISKRA_MT174 #include "decodeMeterMT174.h" #endif #ifdef VDE_DIN #include "decodeMeterDIN.h" #endif #ifdef ZE311_DR #include "decodeMeterZE311.h" #endif #ifdef LG_E350 #include "decodeMeterE350.h" #endif #define PostMmeName "XZmmen" #define PostUser "XZuser" #define PostPW "XZpw" #define PostID "XZid" #define PostHash "XZhash" #define PostUrl "XZurl" #define PostChecked "XZLEO" #define restday "restDay" #define wfactor "wFactor" #define HTTP "http://" #define HTTPS "https://" //extern uint16_t crc_one_byte(uint16_t crcR, uint16_t oneByte, uint16_t poly); /***** class for save data intern into ESP8266 *****/ class configDTZ { private: String USER; String PW; String TARGET_URL; String NAMEDTZ; uint32_t USERID = 0; int RESTDAY = 0; int WFACTOR = 1; bool MOUNTED = false; void loadConfig(String s); int calculateFreeSpaceFS(); String decodeOnValue(String s); unsigned char h2int(char c); String getTargetWithout(); bool checkHTTPadjustTime(); public: int saveCounter = 0; int FREESPACE=0; int SHOWPROTOCOL = 0; int RestDay=0; int SendSSL=0; // 0 = send over http, send over https bool begin(); // start to get data from file String getUser(); String getPassw(); String getPasswEncrypt(); String getNameDTZ(); String getTargetURL(); String getTargetURLclean(); String getTargetServer(); uint32_t getUserID(); bool readConfigDTZ(); bool saveConfigDTZ(); int getFreeSpace(); bool savePostValues(String saveValues); String getOnePostValue(String post,String ident); String getOneFileValue(String file,String ident); String urldecode(String str); String urlencode(String str); void saveValues(String str); String readSavedValues(int pointer); bool delFile(int pointer); int getRestDay(); int getFactor(); void end(); }; /**** mount the file-object ****/ bool configDTZ::begin(){ if(LittleFS.begin()){ this->MOUNTED = this->readConfigDTZ(); this->delFile(0); // clear old saved data on file system this->calculateFreeSpaceFS(); } return this->MOUNTED; } /**** read values from file or create new default-values ****/ bool configDTZ::readConfigDTZ(){ bool r=false; File Fconfig = LittleFS.open("/config.ini","r"); if (Fconfig){ String s=Fconfig.readString(); this->loadConfig(s); Fconfig.close(); r = true; } return r; } /****** for compatibility ******/ void configDTZ::end(){ // do nothing, it's for compatibility only } /******** handle user **********/ String configDTZ::getUser(){ return this->USER; } /******** handle userID ********/ uint32_t configDTZ::getUserID(){ return this->USERID; } /******** handle password ******/ String configDTZ::getPassw(){ return this->PW; } /******** handle DTZ-name *******/ String configDTZ::getNameDTZ(){ return this->NAMEDTZ; } /******** handle target URL clean******/ String configDTZ::getTargetURLclean(){ return getTargetWithout(); } /******** handle target URL ******/ String configDTZ::getTargetURL(){ return this->TARGET_URL; } /****** handle update server ******/ String configDTZ::getTargetServer(){ String url = getTargetWithout(); int urlEnd = url.indexOf("/",0); return url.substring(0,urlEnd); } /******** handle day off *******/ int configDTZ::getRestDay(){ return this->RESTDAY; } /******** handle wandler factor *******/ int configDTZ::getFactor(){ return this->WFACTOR; } //*** get return without http r https ******/ String configDTZ::getTargetWithout(){ String url; int httpString = this->TARGET_URL.indexOf(HTTP,0); int httpsString = this->TARGET_URL.indexOf(HTTPS,0); if(httpString != -1){ url=TARGET_URL.substring(httpString+7); SendSSL=0; }else if(httpsString != -1){ url=TARGET_URL.substring(httpsString+8); SendSSL=1; }else{ url=this->TARGET_URL; SendSSL=0; } return url; } /*** end ***/ bool configDTZ::checkHTTPadjustTime(){ if(this->TARGET_URL.indexOf(HTTPS,0) >= 0) SendSSL=1; else SendSSL =0; if(SendSSL==1) _secAdjust=_ADJUST_HTTPS; else _secAdjust=_ADJUST_HTTP; return true; } /**** save actuelle config-data ****/ bool configDTZ::saveConfigDTZ(){ bool r = false; String s = "{\n"; s += "user: \""; s += this->getUser(); s += "\",\n"; s += "pw: \""; s += this->getPassw(); s += "\",\n"; s += "userid: "; s += this->getUserID(); s += ",\n"; s += "url: \""; s += this->getTargetURL(); s += "\",\n"; s += "nameDTZ: \""; s += this->getNameDTZ(); s += "\",\n"; s += "showled: "; s += this->SHOWPROTOCOL; s += ",\n"; s += "restday: "; s += this->getRestDay(); s += ",\n"; s += "wfactor: "; s += this->getFactor(); s += "\n"; s += "}"; this->checkHTTPadjustTime(); // Serial.println(s); if(this->MOUNTED){ File Fconfig = LittleFS.open("/config.ini","w"); if (Fconfig){ int w=Fconfig.print(s); Fconfig.close(); if (w > 1) r = true; // write file was successful } } return r; } //******************* load one value from saved config ***************** String configDTZ::getOneFileValue(String file,String ident){ String ret; int argBegin = file.indexOf(ident); if(argBegin>0){ int argEnd = file.indexOf(",",argBegin+2); ret = this->decodeOnValue(file.substring(argBegin, argEnd)); } return ret; } //******************* decode post-values ******************************* String configDTZ::getOnePostValue(String post,String ident){ String returnValue=""; if(post.length()>0){ int identBegin = post.indexOf(ident); if(identBegin >= 0){ int argEnd = post.indexOf("&",identBegin); if (argEnd == -1) argEnd = post.length(); returnValue = post.substring(post.indexOf("=",identBegin)+1,argEnd); } } return this->urldecode(returnValue); } /**** decode file-data and save into class ****/ void configDTZ::loadConfig(String s){ this->USER = getOneFileValue(s,"user:"); this->PW = getOneFileValue(s,"pw:"); this->TARGET_URL = getOneFileValue(s,"url:"); this->NAMEDTZ = getOneFileValue(s,"nameDTZ:"); this->USERID = (getOneFileValue(s,"userid:")).toInt(); this->SHOWPROTOCOL = (getOneFileValue(s,"showled:")).toInt(); this->RESTDAY = (getOneFileValue(s,"restday:")).toInt(); this->WFACTOR = (getOneFileValue(s,"wfactor:")).toInt(); this->checkHTTPadjustTime(); } /**** extract one value from string ****/ String configDTZ::decodeOnValue(String s){ int valueBegin = s.indexOf("\""); int valueLength = s.length(); if(valueLength >0){ if(valueBegin > 0){ s = s.substring(valueBegin+1,valueLength-1); } else { valueBegin = s.indexOf(":"); s = s.substring(valueBegin+1,valueLength); } } else s = ""; return s; } /****** get free space from file-system ******/ int configDTZ::calculateFreeSpaceFS(){ if(MOUNTED){ FSInfo fsinfo; LittleFS.info(fsinfo); FREESPACE = fsinfo.totalBytes - fsinfo.usedBytes; } else FREESPACE = 0; return FREESPACE; } int configDTZ::getFreeSpace(){ return this->FREESPACE; } /***** decode values from html-post-arguments *****/ bool configDTZ::savePostValues(String saveValues){ bool r=false; if(saveValues.length()>0){ this->NAMEDTZ = getOnePostValue(saveValues,PostMmeName); this->USER = getOnePostValue(saveValues,PostUser); this->PW = getOnePostValue(saveValues,PostPW); this->USERID = (getOnePostValue(saveValues,PostID)).toInt(); this->TARGET_URL = getOnePostValue(saveValues,PostUrl); this->RESTDAY = getOnePostValue(saveValues,restday).toInt(); this->WFACTOR = getOnePostValue(saveValues,wfactor).toInt(); if(saveValues.indexOf(PostChecked) >=0) SHOWPROTOCOL=1; else SHOWPROTOCOL=0; } // Serial.println(saveValues); return this->saveConfigDTZ(); } /***** url decoding part *****/ unsigned char configDTZ::h2int(char c) { if (c >= '0' && c <='9') return((unsigned char)c - '0'); if (c >= 'a' && c <='f') return((unsigned char)c - 'a' + 10); if (c >= 'A' && c <='F') return((unsigned char)c - 'A' + 10); return(0); } /***** url decoding function ******/ String configDTZ::urldecode(String str) { String encodedString=""; char c; char code0; char code1; for (int i =0; i < str.length(); i++){ c=str.charAt(i); if (c == '+') encodedString+=' '; else if (c == '%') { i++; code0=str.charAt(i); i++; code1=str.charAt(i); c = (h2int(code0) << 4) | h2int(code1); encodedString+=c; } else encodedString+=c; yield(); } return encodedString; } /***** url encoding function ******/ String configDTZ::urlencode(String str) { String encodedString=""; char c; char code0; char code1; char code2; for (int i =0; i < str.length(); i++){ c=str.charAt(i); if (c == ' ') encodedString+= '+'; else if (isalnum(c)) encodedString+=c; else{ code1=(c & 0xf)+'0'; if ((c & 0xf) >9) code1=(c & 0xf) - 10 + 'A'; c=(c>>4)&0xf; code0=c+'0'; if (c > 9) code0=c - 10 + 'A'; code2='\0'; encodedString+='%'; encodedString+=code0; encodedString+=code1; //encodedString+=code2; } yield(); } return encodedString; } void configDTZ::saveValues(String str){ if(this->MOUNTED && this->calculateFreeSpaceFS() > 4096){ // don't save more than avialable freespace this->saveCounter ++; String fileName = "/"; fileName += this->saveCounter; fileName += ".val"; File Fvalues = LittleFS.open(fileName,"w"); if (Fvalues){ int w=Fvalues.print(str); Fvalues.close(); } } else delFile(0); return; } String configDTZ::readSavedValues(int pointer){ String s= "*"; if(this->MOUNTED && this->saveCounter > 0 && pointer > 0){ String fileName = "/"; fileName += pointer; fileName += ".val"; File Fvalues = LittleFS.open(fileName,"r"); if (Fvalues){ s=Fvalues.readString(); Fvalues.close(); } } return s; } bool configDTZ::delFile(int pointer){ // pointer = 0 -> del all files String fileName = "/"; if(pointer == 0){ for (int i=0;i<15;i++){ fileName = "/"; fileName += i; fileName += ".val"; LittleFS.remove(fileName); this->saveCounter=0; if(i > 15) break; } } else { fileName += pointer; fileName += ".val"; LittleFS.remove(fileName); } this->calculateFreeSpaceFS(); return false; } #endif //*** CONFIG_DTZ_H_