/* * configPWR.h * File-class for PWR-Switch * for configuration of 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 0.09 * 06.10.2022 */ // class for manage config-data into and from file "config.ini" #include // LittleFS is declared #define ReglerName "rname" #define ActionOn "von" #define ActionOff "voff" #define WaitOn "wait" #define ScanMeter "scan" #define MeterIP "turl" #define ActionRelais "ract" #define UpdateUrl "upser" /***** class for save data intern into ESP8266 *****/ class configPWR { private: String TARGET_METER; String UPDATE_SERVER; String NAME_PWR; int VALUE_ACTION_ON = 0; int VALUE_ACTION_OFF = 0; int WAIT_TIME_ACTION = 1; int SCAN_METER = 2; int ACTION_RELAIS = 0; // negative or zero value turned automatic off, positive value turned on bool MOUNTED = false; void loadConfig(String s); int calculateFreeSpaceFS(); String decodeOnValue(String s); unsigned char h2int(char c); public: int FREESPACE=0; bool AUTOMATIC = true; bool begin(); // start to get data from file void end(); int getValueON(); void setValueON(int value); int getValueOFF(); void setValueOFF(int value); int getWaitAction(); void setWaitAction(int value); int getScanTime(); void setScanTime(int value); String getPName(); bool setPName(String nameDTZ); String getTargetMeter(); bool setTargetMeter(String url); String getUpdateServer(); bool setUpdateServer(String url); bool readConfig(); bool saveConfig(); int getFreeSpace(); bool savePostValues(String saveValues); String urldecode(String str); String urlencode(String str); String getOnePostValue(String post,String value); String getOneFileValue(String file,String ident); bool getActionOn(); }; /**** mount the file-object ****/ bool configPWR::begin(){ if(LittleFS.begin()){ this->MOUNTED = this->readConfig(); this->calculateFreeSpaceFS(); } return this->MOUNTED; } /**** read values from file ****/ bool configPWR::readConfig(){ String s; bool r=false; File Fconfig = LittleFS.open("/config.ini","r"); Fconfig = LittleFS.open("/config.ini","r"); if (Fconfig){ s=Fconfig.readString(); this->loadConfig(s); Fconfig.close(); r = true; } return r; } /****** for compatibility ******/ void configPWR::end(){ // do nothing, it's for compatibility only } /******** handle value for switch on ********/ int configPWR::getValueON(){ return this->VALUE_ACTION_ON; } void configPWR::setValueON(int value){ this->VALUE_ACTION_ON = value; } /******** handle value for switch off ******/ int configPWR::getValueOFF(){ return this->VALUE_ACTION_OFF; } void configPWR::setValueOFF(int value){ this->VALUE_ACTION_OFF = value; } /******** handle value for wait time ******/ int configPWR::getWaitAction(){ return this->WAIT_TIME_ACTION; } void configPWR::setWaitAction(int value){ this->WAIT_TIME_ACTION = value; } /******** handle value for scan meter time ******/ int configPWR::getScanTime(){ return this->SCAN_METER; } void configPWR::setScanTime(int value){ this->SCAN_METER = value; } /******** handle PWR-name *******/ String configPWR::getPName(){ return this->NAME_PWR; } bool configPWR::setPName(String namep){ bool r=false; if(namep){ this->NAME_PWR = namep; r = true; } return r; } /******** automatic on / off **************/ bool configPWR::getActionOn(){ if(this->ACTION_RELAIS>0)return true; else return false; } /******** handle target meter ******/ String configPWR::getTargetMeter(){ return this->TARGET_METER; } bool configPWR::setTargetMeter(String url){ bool r=false; if(url){ this->TARGET_METER = url; r = true; } return r; } /****** handle update server ******/ String configPWR::getUpdateServer(){ return this->UPDATE_SERVER; } bool configPWR::setUpdateServer(String url){ bool r=false; if(url){ this->UPDATE_SERVER = url; r = true; } return r; } /**** save actuelle config-data ****/ bool configPWR::saveConfig(){ bool r = false; String s = "{\n"; s += "action_on: \""; s += this->VALUE_ACTION_ON; s += "\",\n"; s += "action_off: \""; s += this->VALUE_ACTION_OFF; s += "\",\n"; s += "waittime: "; s += this->WAIT_TIME_ACTION; s += ",\n"; s += "scantime: "; s += this->SCAN_METER; s += ",\n"; s += "action_r: "; s += this->ACTION_RELAIS; s += ",\n"; s += "url: \""; s += this->TARGET_METER; s += "\",\n"; s += "namepwr: \""; s += this->NAME_PWR; s += "\",\n"; s += "upserv: \""; s += this->UPDATE_SERVER; s += "\",\n"; s += "}"; // 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 configPWR::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 file-data and save into class ****/ void configPWR::loadConfig(String s){ String _oneValue=""; _oneValue=getOneFileValue(s,"action_on"); this->VALUE_ACTION_ON=_oneValue.toInt(); _oneValue=getOneFileValue(s,"action_off"); this->VALUE_ACTION_OFF=_oneValue.toInt(); _oneValue=getOneFileValue(s,"waittime"); this->WAIT_TIME_ACTION=_oneValue.toInt(); _oneValue=getOneFileValue(s,"scantime"); this->SCAN_METER=_oneValue.toInt(); _oneValue=getOneFileValue(s,"action_r"); this->ACTION_RELAIS=_oneValue.toInt(); this->TARGET_METER=getOneFileValue(s,"url"); this->UPDATE_SERVER=getOneFileValue(s,"upserv"); this->NAME_PWR=getOneFileValue(s,"namepwr"); } /**** extract one value from string ****/ String configPWR::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 configPWR::calculateFreeSpaceFS(){ if(MOUNTED){ FSInfo fsinfo; LittleFS.info(fsinfo); FREESPACE = fsinfo.totalBytes - fsinfo.usedBytes; } else FREESPACE = 0; return FREESPACE; } int configPWR::getFreeSpace(){ return this->FREESPACE; } //******************* decode post-values ******************************* String configPWR::getOnePostValue(String post,String value){ String returnValue=""; if(post.length()>0){ int mmenBegin = post.indexOf(value); if(mmenBegin >= 0){ int argEnd = post.indexOf("&",mmenBegin); if (argEnd == -1) argEnd = post.length(); returnValue = post.substring(post.indexOf("=",mmenBegin)+1, argEnd); } } return this->urldecode(returnValue); } /***** decode values from html-post-arguments *****/ bool configPWR::savePostValues(String saveValues){ bool r=false; String setValue; Serial.println(saveValues); this->NAME_PWR=getOnePostValue(saveValues,ReglerName); this->TARGET_METER=getOnePostValue(saveValues,MeterIP); this->UPDATE_SERVER=getOnePostValue(saveValues,UpdateUrl); setValue=getOnePostValue(saveValues,ActionOn); this->VALUE_ACTION_ON=setValue.toInt(); setValue=getOnePostValue(saveValues,ActionOff); this->VALUE_ACTION_OFF=setValue.toInt(); setValue=getOnePostValue(saveValues,WaitOn); this->WAIT_TIME_ACTION=setValue.toInt(); setValue=getOnePostValue(saveValues,ScanMeter); this->SCAN_METER=setValue.toInt(); setValue=getOnePostValue(saveValues,ActionRelais); this->ACTION_RELAIS=setValue.toInt(); this->saveConfig(); return true; } /***** url decoding part *****/ unsigned char configPWR::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 configPWR::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 configPWR::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; } //*** class end