Snorktracker
 All Data Structures Files Functions Variables Macros Pages
SmsCmd.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 */
26 class MySmsCmd
27 {
28 protected:
32 
33 protected:
34  void checkSms ();
35 
36  String getGoogleMapGpsUrl();
37 
38  void sendSms (const String &message);
39  void sendOk (const SmsData &sms);
40 
41  bool readValues (String &value, const String message);
42  bool readValues (long &value, const String message);
43  bool readValues (long &value, long &value2, const String message);
44 
45  void cmdOn (const SmsData &sms);
46  void cmdOff (const SmsData &sms);
47  void cmdStatus (const SmsData &sms);
48  void cmdPsm (const SmsData &sms);
49  void cmdGps (const SmsData &sms);
50  void cmdSms (const SmsData &sms);
51  void cmdMqtt (const SmsData &sms);
52  void cmdPhone (const SmsData &sms);
53  void cmdDefault (const SmsData &sms);
54 
55 public:
56  MySmsCmd(MyGsmGps &gsmGps, MyOptions &options, MyData &data);
57 
58  bool begin();
59  void handleClient();
60 };
61 
62 /* ******************************************** */
63 
65 MySmsCmd::MySmsCmd(MyGsmGps &gsmGps, MyOptions &options, MyData &data)
66  : myGsmGps(gsmGps)
67  , myOptions(options)
68  , myData(data)
69 {
70 }
71 
74 {
75  MyDbg(F("MySmsCmd::begin"));
76  return true;
77 }
78 
81 {
83  checkSms();
84  }
85 }
86 
89 {
91  return (String) F("https://maps.google.com/maps?q=") + myData.rtcData.lastGps.latitudeString() + F(",") + myData.rtcData.lastGps.longitudeString();
92  } else {
93  if (myOptions.isGpsEnabled) {
94  return F("No Gps position.\n");
95  } else {
96  return F("Gps not enabled.\n");
97  }
98  }
99 }
100 
103 {
104  if (!myData.isGsmActive) {
105  return;
106  }
107 
108  SmsData sms;
109 
110  MyDbg(F("checkSMS"));
111  while (myGsmGps.getSMS(sms)) {
112  String messageLower = sms.message;
113 
114  messageLower.toLowerCase();
115  myGsmGps.deleteSMS(sms.index);
116 
117  MyDbg((String) F("SMS: ") + sms.message + F(" [") + sms.phoneNumber + F("]"));
118  if (messageLower.indexOf(F("on")) == 0) {
119  cmdOn(sms);
120  } else if (messageLower.indexOf(F("off")) == 0) {
121  cmdOff(sms);
122  } else if (messageLower.indexOf(F("status")) == 0) {
123  cmdStatus(sms);
124  } else if (messageLower.indexOf(F("psm")) == 0) {
125  cmdPsm(sms);
126  } else if (messageLower.indexOf(F("gps")) == 0) {
127  cmdGps(sms);
128  } else if (messageLower.indexOf(F("sms")) == 0) {
129  cmdSms(sms);
130  } else if (messageLower.indexOf(F("mqtt")) == 0) {
131  cmdMqtt(sms);
132  } else if (messageLower.indexOf(F("phone")) == 0) {
133  cmdPhone(sms);
134  } else {
135  cmdDefault(sms);
136  }
137  }
138 }
139 
141 void MySmsCmd::sendSms(const String &message)
142 {
144 }
145 
147 void MySmsCmd::sendOk(const SmsData &sms)
148 {
149  sendSms(sms.message + F(" -> OK"));
150 }
151 
153 bool MySmsCmd::readValues(String &value, const String message)
154 {
155  int idx = message.indexOf(':');
156 
157  if (idx != -1) {
158  value = message.substring(idx + 1);
159  value.toLowerCase();
160  return true;
161  }
162  return false;
163 }
164 
166 bool MySmsCmd::readValues(long &value, const String message)
167 {
168  int idx = message.indexOf(':');
169 
170  if (idx != -1) {
171  value = atoi(message.substring(idx + 1).c_str());
172  return true;
173  }
174  return false;
175 }
176 
178 bool MySmsCmd::readValues(long &value1, long &value2, const String message)
179 {
180  int first = message.indexOf(':');
181 
182  if (first != -1) {
183  int second = message.indexOf(':', first + 1);
184 
185  if (second != -1) {
186  value1 = atoi(message.substring(first + 1, second).c_str());
187  value2 = atoi(message.substring(second + 1).c_str());
188  return true;
189  }
190  }
191  return false;
192 }
193 
195 void MySmsCmd::cmdOn(const SmsData &sms)
196 {
197  myOptions.powerOn = true;
198  myOptions.isGpsEnabled = true;
199  myOptions.save();
200  sendOk(sms);
201 }
202 
204 void MySmsCmd::cmdOff(const SmsData &sms)
205 {
206  myOptions.powerOn = false;
207  myOptions.save();
208  sendOk(sms);
209 }
210 
212 void MySmsCmd::cmdStatus(const SmsData &sms)
213 {
214  String status;
215 
216  status += (String) F("Status: ") + myData.status + '\n';
217  status += (String) F("Voltage: ") + String(myData.voltage, 2) + F(" V\n");
218  status += (String) F("Temperature: ") + String(myData.temperature) + F(" C\n");
219  status += (String) F("Humidity: ") + String(myData.humidity) + F(" %\n");
220  status += (String) F("Pressure: ") + String(myData.pressure) + F(" hPa\n");
222  if (myOptions.isGpsEnabled) {
223  status += F("No Gps positions.");
224  } else {
225  status += F("Gps not enabled.");
226  }
227  } else {
228  status += (String) F("Altitude: ") + myData.rtcData.lastGps.altitudeString() + F(" m\n");
229  status += (String) F("Speed: ") + myData.rtcData.lastGps.kmphString() + F(" kmph\n");
230  status += (String) F("Satellites: ") + myData.rtcData.lastGps.satellitesString() + '\n';
231  status += getGoogleMapGpsUrl();
232  }
233  sendSms(status);
234 }
235 
237 void MySmsCmd::cmdPsm(const SmsData &sms)
238 {
239  if (sms.message.indexOf(F(":")) == -1) {
241  myOptions.save();
242  sendOk(sms);
243  } else {
244  String off;
245 
246  if (readValues(off, sms.message) && off == F("off")) {
248  myOptions.save();
249  sendOk(sms);
250  } else {
251  MyDbg((String) F("psm:[") + off + F("]"));
252  cmdDefault(sms);
253  }
254  }
255 }
256 
258 void MySmsCmd::cmdGps(const SmsData &sms)
259 {
260  if (sms.message.indexOf(F(":")) == -1) {
262  } else {
264  sendOk(sms);
265  } else {
266  cmdDefault(sms);
267  }
268  }
269 }
270 
272 void MySmsCmd::cmdSms(const SmsData &sms)
273 {
275  sendOk(sms);
276  } else {
277  cmdDefault(sms);
278  }
279 }
280 
282 void MySmsCmd::cmdMqtt(const SmsData &sms)
283 {
285  sendOk(sms);
286  } else {
287  cmdDefault(sms);
288  }
289 }
290 
292 void MySmsCmd::cmdPhone(const SmsData &sms)
293 {
295  sendOk(sms);
296  } else {
297  cmdDefault(sms);
298  }
299 }
300 
303 {
304  String info;
305 
306  info += F("wrong command\n");
307  info += F("on\n");
308  info += F("off\n");
309  info += F("status\n");
310  info += F("psm[:off] - power saving mode\n");
311  info += F("gps[:15] - check every (sec)\n");
312  info += F("sms[:15] - check every (sec)\n");
313  info += F("mqtt[30:60] - (moving:standing (sec)\n");
314  info += F("phone:1234\n");
315  sendSms(info);
316 }
MyOptions & myOptions
Reference to the options.
Definition: SmsCmd.h:30
String phoneNumber
Pone number for sms answers.
Definition: Options.h:47
bool isGpsEnabled
Is the gps part of the sim808 active?
Definition: Options.h:43
long smsCheckIntervalSec
SMS check intervall.
Definition: Options.h:48
String status
Status information.
Definition: Data.h:63
class MyData::RtcData rtcData
Data to store in the RTC memory.
MyOptions myOptions
The global options.
Definition: tracker.ino:57
String phoneNumber
Sms sender number.
Definition: Sim808.h:37
long gpsCheckIntervalSec
Time interval to check the gps position.
Definition: Options.h:45
Definition: Data.h:27
void MyDbg(String info, bool fromWebServer=false, bool newline=true)
Definition: Utils.h:94
bool powerOn
Is the GSM power from the DC-DC modul switched on?
Definition: Options.h:41
void cmdSms(const SmsData &sms)
Definition: SmsCmd.h:272
double temperature
Current BME280 temperature.
Definition: Data.h:75
bool sendSMS(String phoneNumber, String message)
Definition: GsmGps.h:280
MyGps lastGps
Last known gps location without timeout.
Definition: Data.h:35
String altitudeString()
Definition: Gps.h:552
void handleClient()
Definition: SmsCmd.h:80
String message
Sms content.
Definition: Sim808.h:40
void cmdPhone(const SmsData &sms)
Definition: SmsCmd.h:292
String satellitesString()
Definition: Gps.h:564
MySmsCmd(MyGsmGps &gsmGps, MyOptions &options, MyData &data)
Definition: SmsCmd.h:65
bool deleteSMS(long index)
Definition: GsmGps.h:292
double pressure
Current BME280 pressure.
Definition: Data.h:77
MyGsmGps myGsmGps(myOptions, myData, PIN_RX, PIN_TX)
sim808 gsm/gps communication class.
MyData & myData
Reference to the data.
Definition: SmsCmd.h:31
void cmdOff(const SmsData &sms)
Definition: SmsCmd.h:204
String longitudeString()
Definition: Gps.h:540
String kmphString()
Definition: Gps.h:558
MyGsmGps & myGsmGps
Reference to the gsm/gps instance.
Definition: SmsCmd.h:29
long index
Sms index on sim card.
Definition: Sim808.h:35
String getGoogleMapGpsUrl()
Definition: SmsCmd.h:88
double voltage
Current supply voltage.
Definition: Data.h:74
void cmdGps(const SmsData &sms)
Definition: SmsCmd.h:258
void cmdPsm(const SmsData &sms)
Definition: SmsCmd.h:237
bool save()
Definition: Options.h:209
String latitudeString()
Definition: Gps.h:546
void sendSms(const String &message)
Definition: SmsCmd.h:141
bool readValues(String &value, const String message)
Definition: SmsCmd.h:153
void cmdMqtt(const SmsData &sms)
Definition: SmsCmd.h:282
void cmdStatus(const SmsData &sms)
Definition: SmsCmd.h:212
void cmdOn(const SmsData &sms)
Definition: SmsCmd.h:195
bool isGsmActive
Is the sim808 modul connected to a gsm network?
Definition: Data.h:68
bool fixStatus
Are the gps is valid received?
Definition: Gps.h:133
void sendOk(const SmsData &sms)
Definition: SmsCmd.h:147
bool begin()
Definition: SmsCmd.h:73
double humidity
Current BME280 humidity.
Definition: Data.h:76
MyData myData
The global collected data.
Definition: tracker.ino:58
bool secondsElapsedAndUpdate(long &lastCheckSec, const long &intervalSec)
Definition: Utils.h:63
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
long lastSmsCheckSec
Timestamp of the last sms check.
Definition: Data.h:46
Definition: Sim808.h:32
bool getSMS(SmsData &sms)
Definition: GsmGps.h:268
long mqttSendOnMoveEverySec
Send data interval to MQTT server on moving.
Definition: Options.h:61
void checkSms()
Definition: SmsCmd.h:102
void cmdDefault(const SmsData &sms)
Definition: SmsCmd.h:302