Skip to content

Commit 8a9d3b1

Browse files
authored
Merge pull request #5444 from tonhuisman/feature/Latitude-longitude-sysvars-and-cmds
[SysVars] Add %latitude% and %longitude% vars and Latitude and Longitude commands
2 parents 05e1ad2 + 1e9fc26 commit 8a9d3b1

File tree

16 files changed

+165
-4
lines changed

16 files changed

+165
-4
lines changed

docs/source/Plugin/P000_commands.repl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,18 @@
303303

304304
Example output: ``IP:192.168.10.86(DHCP)``"
305305
"
306+
Latitude","
307+
:red:`Internal`","
308+
Set the latitude for the unit, that's used when calculation the sunrise/sunset times.
309+
310+
``Latitude[,<degrees>]``
311+
312+
Range: -90 .. 90 (decimal, negative is southern hemisphere)
313+
314+
When no argument is provided, the current setting is shown.
315+
316+
See also ``Longitude``."
317+
"
306318
Let","
307319
:red:`Internal`","
308320
Set the value of variable n (1..INT_MAX), or use a variable name.
@@ -344,6 +356,18 @@
344356

345357
``LogPortStatus``"
346358
"
359+
Longitude","
360+
:red:`Internal`","
361+
Set the longitude for the unit, that's used when calculation the sunrise/sunset times.
362+
363+
``Longitude[,<degrees>]``
364+
365+
Range: -180 .. 180 (decimal, negative is west of the prime meridian)
366+
367+
When no argument is provided, the current setting is shown.
368+
369+
See also ``Latitude``."
370+
"
347371
LoopTimerSet
348372

349373
LoopTimerSet_ms","

docs/source/Reference/SystemVariable.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ More uses of these system variables can be seen in the rules section and formula
123123

124124
Does not have the ``+xm`` and ``-xh`` calculations that ``%sunrise%`` and ``%sunset%`` support.
125125
-
126+
* - ``%latitude%``
127+
- 50.12345
128+
- Configured Latitude (decimal degrees) as used for calculating the sunrise and sunset times.
129+
-
130+
* - ``%longitude%``
131+
- 50.12345
132+
- Configured Longitude (decimal degrees) as used for calculating the sunrise and sunset times.
133+
-
126134
* - ``%lcltime_am%``
127135
- 2020-03-16 1:23:54 AM
128136
- Current date/time (AM/PM) if NTP is enabled (YYYY-MM-DD hh:mm:ss xM).

src/Custom-sample.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,23 @@
252252
// #define FEATURE_RTTTL_EVENTS 1 // Enable RTTTL events for Async use, for blocking it doesn't make sense
253253
// #define FEATURE_BUSCMD_STRING 1 // Enable support for String data-format in Helpers/BusCmd_Handler, default disabled for LIMIT_BUILD_SIZE only
254254
// #define FEATURE_STRING_VARIABLES 1 // Enable String variable support (enabled on ESP32, NOT supported on ESP8266 for memory restrictions!)
255+
// #define FEATURE_COMMAND_OWSCAN 0 // Disable 1-wire scanner support, only feasible when 1-wire support is included in the build (P004, P080, P100), default disabled for MINIMAL_OTA builds
256+
// #define FEATURE_MQTT_CONNECT_BACKGROUND 1 // Enable connecting to an MQTT broker in an ESP32 RTOS background thread (not possible on ESP8266)
257+
// #define FEATURE_I2C_MULTIPLE 0 // Disable multiple I2C buses, only available for ESP32, default enabled on ESP32, can be disabled here
258+
// #define FEATURE_PLUGIN_LIST 1 // Enable the Tools / Plugin list page (default enabled for ESP32)
259+
// #define FEATURE_LAT_LONG_VAR_CMD 1 // Enable the %latitude% and %longitude% system variables, and Latitude and Longitude commands (default enabled for ESP32)
260+
261+
// #define FEATURE_TASKVALUE_ATTRIBUTES 1 // Enable extra Task Value attributes (default enabled for ESP32)
262+
// #define FEATURE_TASKVALUE_UNIT_OF_MEASURE 1 // Enable Unit of Measure per Task Value (default enabled for ESP32), also useful for MQTT Discovery
263+
// #define FEATURE_CUSTOM_TASKVAR_VTYPE 1 // Enable Custom Value Type per Task Value (default enabled for ESP32), also useful for MQTT Discovery
264+
265+
// #define FEATURE_MQTT_DISCOVER 1 // Enable MQTT Auto Discovery (currently only available for Home Assistant C005)
266+
// #define FEATURE_MQTT_DEVICECLASS 1 // Enable selectable Device Class for Auto Discovery
267+
// #define FEATURE_MQTT_STATE_CLASS 1 // Enable selectable State Class per Task Valie for Auto Discovery
268+
269+
// #define FEATURE_MQTT_TLS 1 // Enable TLS support for MQTT Controller connections (only available on ESP32)
270+
// #define FEATURE_EMAIL_TLS 1 // Enable TLS support for Email Notifications (only available on ESP32)
271+
// #define FEATURE_HTTP_TLS 1 // Enable TLS support for HTTP connections (only available on ESP32)
255272

256273
#if FEATURE_CUSTOM_PROVISIONING
257274
// For device models, see src/src/DataTypes/DeviceModel.h

src/src/Commands/Common.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,35 @@ String Command_GetORSetInt8_t(struct EventStruct *event,
235235
}
236236
return return_command_success();
237237
}
238+
239+
String Command_GetORSetFloatMinMax(struct EventStruct *event,
240+
const __FlashStringHelper *targetDescription,
241+
const char *Line,
242+
float *value,
243+
int arg,
244+
float _min,
245+
float _max)
246+
{
247+
bool hasArgument = false;
248+
{
249+
// Check if command is valid. Leave in separate scope to delete the TmpStr1
250+
String TmpStr1;
251+
252+
if (GetArgv(Line, TmpStr1, arg + 1)) {
253+
hasArgument = true;
254+
TmpStr1.toLowerCase();
255+
256+
float tmp_float{};
257+
258+
if (validFloatFromString(TmpStr1, tmp_float) &&
259+
definitelyGreaterThan(tmp_float, _min) &&
260+
definitelyLessThan(tmp_float, _max)) {
261+
*value = tmp_float;
262+
} else {
263+
return return_command_failed();
264+
}
265+
}
266+
}
267+
268+
return return_result(event, concat(targetDescription, toString(*value)));
269+
}

src/src/Commands/Common.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,12 @@ String Command_GetORSetInt8_t(struct EventStruct *event,
6666
int8_t *value,
6767
int arg);
6868

69+
String Command_GetORSetFloatMinMax(struct EventStruct *event,
70+
const __FlashStringHelper * targetDescription,
71+
const char *Line,
72+
float *value,
73+
int arg,
74+
float _min,
75+
float _max);
76+
6977
#endif // COMMAND_COMMON_H

src/src/Commands/InternalCommands.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#include "../Commands/InternalCommands_decoder.h"
1919
#include "../Commands/i2c.h"
2020

21+
#if FEATURE_LAT_LONG_VAR_CMD
22+
#include "../Commands/LatitudeLongitude.h"
23+
#endif // if FEATURE_LAT_LONG_VAR_CMD
24+
2125
#if FEATURE_MQTT
2226
# include "../Commands/MQTT.h"
2327
#endif // if FEATURE_MQTT
@@ -319,12 +323,18 @@ bool InternalCommands::executeInternalCommand()
319323
#ifndef BUILD_NO_DIAGNOSTIC_COMMANDS
320324
case ESPEasy_cmd_e::jsonportstatus: COMMAND_CASE_A(Command_JSONPortStatus, -1); // Diagnostic.h
321325
#endif // ifndef BUILD_NO_DIAGNOSTIC_COMMANDS
326+
#if FEATURE_LAT_LONG_VAR_CMD
327+
case ESPEasy_cmd_e::latitude: COMMAND_CASE_A(Command_Latitude, -1); // LatitudeLongitude.h
328+
#endif // if FEATURE_LAT_LONG_VAR_CMD
322329
case ESPEasy_cmd_e::let: COMMAND_CASE_A(Command_Rules_Let, 2); // Rules.h
323330
#if FEATURE_STRING_VARIABLES
324331
case ESPEasy_cmd_e::letstr: COMMAND_CASE_A(Command_Rules_LetStr, 2); // Rules.h
325332
#endif // if FEATURE_STRING_VARIABLES
326333
case ESPEasy_cmd_e::load: COMMAND_CASE_A(Command_Settings_Load, 0); // Settings.h
327334
case ESPEasy_cmd_e::logentry: COMMAND_CASE_A(Command_logentry, -1); // Diagnostic.h
335+
#if FEATURE_LAT_LONG_VAR_CMD
336+
case ESPEasy_cmd_e::longitude: COMMAND_CASE_A(Command_Longitude, -1); // LatitudeLongitude.h
337+
#endif // if FEATURE_LAT_LONG_VAR_CMD
328338
case ESPEasy_cmd_e::looptimerset: COMMAND_CASE_A(Command_Loop_Timer_Set, 3); // Timers.h
329339
case ESPEasy_cmd_e::looptimerset_ms: COMMAND_CASE_A(Command_Loop_Timer_Set_ms, 3); // Timers.h
330340
case ESPEasy_cmd_e::looptimersetandrun: COMMAND_CASE_A(Command_Loop_Timer_SetAndRun, 3); // Timers.h

src/src/Commands/InternalCommands_decoder.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,24 @@ const char Internal_commands_fghij[] PROGMEM =
9696
#endif // ifndef BUILD_NO_DIAGNOSTIC_COMMANDS
9797
;
9898

99+
#if FEATURE_LAT_LONG_VAR_CMD
100+
#define Int_cmd_l_offset ESPEasy_cmd_e::latitude
101+
#else // if FEATURE_LAT_LONG_VAR_CMD
99102
#define Int_cmd_l_offset ESPEasy_cmd_e::let
103+
#endif // if FEATURE_LAT_LONG_VAR_CMD
100104
const char Internal_commands_l[] PROGMEM =
105+
#if FEATURE_LAT_LONG_VAR_CMD
106+
"latitude|"
107+
#endif // if FEATURE_LAT_LONG_VAR_CMD
101108
"let|"
102109
#if FEATURE_STRING_VARIABLES
103110
"letstr|"
104111
#endif
105112
"load|"
106113
"logentry|"
114+
#if FEATURE_LAT_LONG_VAR_CMD
115+
"longitude|"
116+
#endif // if FEATURE_LAT_LONG_VAR_CMD
107117
"looptimerset|"
108118
"looptimerset_ms|"
109119
"looptimersetandrun|"

src/src/Commands/InternalCommands_decoder.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,18 @@ enum class ESPEasy_cmd_e : uint8_t {
7676
jsonportstatus,
7777
#endif // ifndef BUILD_NO_DIAGNOSTIC_COMMANDS
7878

79+
#if FEATURE_LAT_LONG_VAR_CMD
80+
latitude,
81+
#endif // if FEATURE_LAT_LONG_VAR_CMD
7982
let,
8083
#if FEATURE_STRING_VARIABLES
8184
letstr,
8285
#endif // if FEATURE_STRING_VARIABLES
8386
load,
8487
logentry,
88+
#if FEATURE_LAT_LONG_VAR_CMD
89+
longitude,
90+
#endif // if FEATURE_LAT_LONG_VAR_CMD
8591
looptimerset,
8692
looptimerset_ms,
8793
looptimersetandrun,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "../Commands/Common.h"
2+
#include "../Commands/LatitudeLongitude.h"
3+
#include "../Globals/Settings.h"
4+
5+
String Command_Latitude(struct EventStruct *event,
6+
const char *Line) {
7+
return Command_GetORSetFloatMinMax(event, F("Latitude:"), Line, &Settings.Latitude, 1, -90.0001f, 90.0001f);
8+
}
9+
10+
String Command_Longitude(struct EventStruct *event,
11+
const char *Line) {
12+
return Command_GetORSetFloatMinMax(event, F("Longitude:"), Line, &Settings.Longitude, 1, -180.0001f, 180.0001f);
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
#include "../../ESPEasy_common.h"
4+
5+
String Command_Latitude(struct EventStruct *event,
6+
const char *Line);
7+
String Command_Longitude(struct EventStruct *event,
8+
const char *Line);

0 commit comments

Comments
 (0)