Snorktracker
All Data Structures Files Functions Variables Macros Pages
Options.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018 SFini
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
23 #define OPTION_FILE_NAME "/options.txt"
24 
25 
30 class MyOptions
31 {
32 public:
33  String gprsAP;
34  String gprsUser;
35  String gprsPassword;
37  String wifiAP;
38  String wifiPassword;
41  bool powerOn;
42  bool isSmsEnabled;
43  bool isGpsEnabled;
47  String phoneNumber;
55  String mqttName;
56  String mqttId;
57  String mqttServer;
58  long mqttPort;
59  String mqttUser;
60  String mqttPassword;
63 
64 public:
65  MyOptions();
66 
67  bool load();
68  bool save();
69 };
70 
71 /* ******************************************** */
72 
73 MyOptions::MyOptions()
74  : isDebugActive(false)
75  , gprsAP(GPRS_AP)
76  , gprsUser(GPRS_USER)
77  , gprsPassword(GPRS_PASSWORD)
78  , wifiAP(WIFI_SID)
79  , connectWifiAP(false)
80  , wifiPassword(WIFI_PW)
81  , bme280CheckIntervalSec(60) // 1 Min
82  , powerOn(false)
83  , isSmsEnabled(false)
84  , isGpsEnabled(true)
85  , gpsTimeoutSec(180) // 3 Min
86  , gpsCheckIntervalSec(300) // 5 Min
87  , minMovingDistance(3000) // 3 km
88  , phoneNumber(PHONE_NUMBER)
89  , smsCheckIntervalSec(600) // 1 Min
90  , isDeepSleepEnabled(false)
91  , powerSaveModeVoltage(16.0)
92  , powerCheckIntervalSec(300) // 5 Min
93  , activeTimeSec(60) // 1 Min
94  , deepSleepTimeSec(900) // 15 Min
95  , isMqttEnabled(false)
96  , mqttName(MQTT_NAME)
97  , mqttId(MQTT_ID)
98  , mqttServer(MQTT_SERVER)
99  , mqttPort(MQTT_PORT)
100  , mqttUser(MQTT_USER)
101  , mqttPassword(MQTT_PASSWORD)
102  , mqttSendOnMoveEverySec(900) // 15 Min
103  , mqttSendOnNonMoveEverySec(10800) // 180 Min
104 {
105 }
106 
109 {
110  bool ret = false;
111  File file = SPIFFS.open(OPTION_FILE_NAME, "r");
112 
113  if (!file) {
114  MyDbg(F("Failed to read options file"));
115  } else {
116  ret = true;
117  while (file.available() && ret) {
118  String line = file.readStringUntil('\n');
119  int idx = line.indexOf('=');
120 
121  if (idx == -1) {
122  MyDbg((String) F("Wrong option entry: ") + line);
123  ret = false;
124  } else {
125  String key = line.substring(0, idx);
126  String value = line.substring(idx + 1);
127  long lValue = atol(value.c_str());
128  double fValue = atof(value.c_str());
129 
130  value.replace("\r", "");
131  value.replace("\n", "");
132  MyDbg((String) F("Load option '") + key + F("=") + value + F("'"));
133 
134  if (key == F("isDebugActive")) {
135  isDebugActive = lValue;
136  } else if (key == F("gprsAP")) {
137  gprsAP = value;
138  } else if (key == F("gprsUser")) {
139  gprsUser = value;
140  } else if (key == F("gprsPassword")) {
141  gprsPassword = value;
142  } else if (key == F("wifiAP")) {
143  wifiAP = value;
144  } else if (key == F("connectWifiAP")) {
145  connectWifiAP = lValue;
146  } else if (key == F("wifiPassword")) {
147  wifiPassword = value;
148  } else if (key == F("powerOn")) {
149  powerOn = lValue;
150  } else if (key == F("bme280CheckIntervalSec")) {
151  bme280CheckIntervalSec = lValue;
152  } else if (key == F("isSmsEnabled")) {
153  isSmsEnabled = lValue;
154  } else if (key == F("isGpsEnabled")) {
155  isGpsEnabled = lValue;
156  } else if (key == F("gpsTimeoutSec")) {
157  gpsTimeoutSec = lValue;
158  } else if (key == F("gpsCheckIntervalSec")) {
159  gpsCheckIntervalSec = lValue;
160  } else if (key == F("minMovingDistance")) {
161  minMovingDistance = lValue;
162  } else if (key == F("phoneNumber")) {
163  phoneNumber = value;
164  } else if (key == F("smsCheckIntervalSec")) {
165  smsCheckIntervalSec = lValue;
166  } else if (key == F("isDeepSleepEnabled")) {
167  isDeepSleepEnabled = lValue;
168  } else if (key == F("powerSaveModeVoltage")) {
169  powerSaveModeVoltage = fValue;
170  } else if (key == F("powerCheckIntervalSec")) {
171  powerCheckIntervalSec = lValue;
172  } else if (key == F("activeTimeSec")) {
173  activeTimeSec = lValue;
174  } else if (key == F("deepSleepTimeSec")) {
175  deepSleepTimeSec = lValue;
176  } else if (key == F("isMqttEnabled")) {
177  isMqttEnabled = lValue;
178  } else if (key == F("mqttName")) {
179  mqttName = value;
180  } else if (key == F("mqttId")) {
181  mqttId = value;
182  } else if (key == F("mqttServer")) {
183  mqttServer = value;
184  } else if (key == F("mqttPort")) {
185  mqttPort = lValue;
186  } else if (key == F("mqttUser")) {
187  mqttUser = value;
188  } else if (key == F("mqttPassword")) {
189  mqttPassword = value;
190  } else if (key == F("mqttSendOnMoveEverySec")) {
191  mqttSendOnMoveEverySec = lValue;
192  } else if (key == F("mqttSendOnNonMoveEverySec")) {
193  mqttSendOnNonMoveEverySec = lValue;
194  } else {
195  MyDbg((String) F("Wrong option entry: ") + line);
196  ret = false;
197  }
198  }
199  }
200  file.close();
201  }
202  if (ret) {
203  MyDbg(F("Settings loaded"));
204  }
205  return ret;
206 }
207 
210 {
211  File file = SPIFFS.open(OPTION_FILE_NAME, "w+");
212 
213  if (!file) {
214  MyDbg("Failed to write options file");
215  } else {
216  file.println((String) F("isDebugActive=") + String(isDebugActive));
217  file.println((String) F("gprsAP=") + gprsAP);
218  file.println((String) F("gprsUser=") + gprsUser);
219  file.println((String) F("gprsPassword=") + gprsPassword);
220  file.println((String) F("connectWifiAP=") + String(connectWifiAP));
221  file.println((String) F("wifiAP=") + wifiAP);
222  file.println((String) F("wifiPassword=") + wifiPassword);
223  file.println((String) F("powerOn=") + String(powerOn));
224  file.println((String) F("bme280CheckIntervalSec=") + String(bme280CheckIntervalSec));
225  file.println((String) F("isSmsEnabled=") + String(isSmsEnabled));
226  file.println((String) F("isGpsEnabled=") + String(isGpsEnabled));
227  file.println((String) F("gpsTimeoutSec=") + String(gpsTimeoutSec));
228  file.println((String) F("gpsCheckIntervalSec=") + String(gpsCheckIntervalSec));
229  file.println((String) F("minMovingDistance=") + String(minMovingDistance));
230  file.println((String) F("phoneNumber=") + phoneNumber);
231  file.println((String) F("smsCheckIntervalSec=") + String(smsCheckIntervalSec));
232  file.println((String) F("isDeepSleepEnabled=") + String(isDeepSleepEnabled));
233  file.println((String) F("powerSaveModeVoltage=") + String(powerSaveModeVoltage, 2));
234  file.println((String) F("powerCheckIntervalSec=") + String(powerCheckIntervalSec));
235  file.println((String) F("activeTimeSec=") + String(activeTimeSec));
236  file.println((String) F("deepSleepTimeSec=") + String(deepSleepTimeSec));
237  file.println((String) F("isMqttEnabled=") + String(isMqttEnabled));
238  file.println((String) F("mqttName=") + mqttName);
239  file.println((String) F("mqttId=") + mqttId);
240  file.println((String) F("mqttServer=") + mqttServer);
241  file.println((String) F("mqttPort=") + String(mqttPort));
242  file.println((String) F("mqttUser=") + mqttUser);
243  file.println((String) F("mqttPassword=") + mqttPassword);
244  file.println((String) F("mqttSendOnMoveEverySec=") + String(mqttSendOnMoveEverySec));
245  file.println((String) F("mqttSendOnNonMoveEverySec=") + String(mqttSendOnNonMoveEverySec));
246  file.close();
247  MyDbg(F("Settings saved"));
248  return true;
249  }
250  return false;
251 }
long deepSleepTimeSec
Time to stay in deep sleep (without check interrupts)
Definition: Options.h:53
String phoneNumber
Pone number for sms answers.
Definition: Options.h:47
bool load()
Definition: Options.h:108
#define MQTT_USER
MQTT connection user.
Definition: Config.h:39
bool isGpsEnabled
Is the gps part of the sim808 active?
Definition: Options.h:43
double powerSaveModeVoltage
Minimum voltage to stay always alive.
Definition: Options.h:50
long smsCheckIntervalSec
SMS check intervall.
Definition: Options.h:48
#define PHONE_NUMBER
my phone number for sms communication
Definition: Config.h:33
bool connectWifiAP
Should we connect to wifi.
Definition: Options.h:36
String mqttUser
MQTT user.
Definition: Options.h:59
#define MQTT_ID
MQTT ID.
Definition: Config.h:36
String wifiAP
WiFi AP name.
Definition: Options.h:37
long gpsCheckIntervalSec
Time interval to check the gps position.
Definition: Options.h:45
void MyDbg(String info, bool fromWebServer=false, bool newline=true)
Definition: Utils.h:94
#define MQTT_PORT
MQTT Port (Default is 1883)
Definition: Config.h:38
bool powerOn
Is the GSM power from the DC-DC modul switched on?
Definition: Options.h:41
String mqttId
MQTT ID.
Definition: Options.h:56
long gpsTimeoutSec
Timeout for waiting for gps position.
Definition: Options.h:44
#define WIFI_PW
WiFi password.
Definition: Config.h:29
String mqttPassword
MQTT password.
Definition: Options.h:60
long powerCheckIntervalSec
Time interval to check the power supply.
Definition: Options.h:51
String wifiPassword
WiFi AP password.
Definition: Options.h:38
String gprsUser
GRPS access point User.
Definition: Options.h:34
String mqttServer
MQTT server url.
Definition: Options.h:57
bool isDebugActive
Is detailed debugging enabled?
Definition: Options.h:39
long mqttPort
MQTT server port.
Definition: Options.h:58
#define GPRS_PASSWORD
gprs password
Definition: Config.h:32
long activeTimeSec
Maximum alive time after deepsleep.
Definition: Options.h:52
long bme280CheckIntervalSec
Time interval to read the temp, hum and pressure.
Definition: Options.h:40
String gprsPassword
GRPS access point Password.
Definition: Options.h:35
#define GPRS_AP
gprs access point name
Definition: Config.h:30
#define WIFI_SID
WiFi SID.
Definition: Config.h:28
bool isSmsEnabled
Is the sms check functionality active?
Definition: Options.h:42
bool save()
Definition: Options.h:209
#define MQTT_SERVER
MQTT Server URL.
Definition: Config.h:37
String gprsAP
GRPS access point of the sim card supplier.
Definition: Options.h:33
String mqttName
MQTT server name.
Definition: Options.h:55
#define MQTT_NAME
MQTT name.
Definition: Config.h:35
bool isMqttEnabled
Should the system connect to a MQTT server?
Definition: Options.h:54
#define OPTION_FILE_NAME
Option file name.
Definition: Options.h:23
#define MQTT_PASSWORD
MQTT connection password.
Definition: Config.h:40
long mqttSendOnNonMoveEverySec
Send data interval to MQTT server on non moving.
Definition: Options.h:62
bool isDeepSleepEnabled
Should the system go into deepsleep if needed.
Definition: Options.h:49
#define GPRS_USER
gprs user
Definition: Config.h:31
long mqttSendOnMoveEverySec
Send data interval to MQTT server on moving.
Definition: Options.h:61
long minMovingDistance
Minimum distance to accept as moving or not.
Definition: Options.h:46