Skip to content

Commit

Permalink
v1.2.13
Browse files Browse the repository at this point in the history
- Implemented Location API; added support for TimeZone IDs
- Added support for `@SolarTimes` variables in scheduler cron expressions
- Added `System.SetTime` API to set system clock
  • Loading branch information
genemars committed May 9, 2024
1 parent 0ef3ba5 commit 59308dd
Show file tree
Hide file tree
Showing 16 changed files with 437 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void SensorValuesActivity::onDraw()
// Device name
canvas->setFont(&fonts::Roboto_Thin_24);
canvas->setTextColor(TFT_WHITE);
canvas->drawCenterString(Config::getDeviceName(), (float)canvas->width() / 2.0f, 56);
canvas->drawCenterString(Config::system.friendlyName, (float)canvas->width() / 2.0f, 56);

// Temperature
canvas->setFont(&fonts::Font6);
Expand Down
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ lib_deps =
vortigont/[email protected]
hideakitai/[email protected]
adafruit/[email protected]
[email protected].4
[email protected].6
lovyan03/[email protected]
dvarrel/[email protected]
jpb10/[email protected]


[env:default]
Expand Down
3 changes: 2 additions & 1 deletion src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@

#include <utility>

int Config::TimeZone = 0; // tz diff in seconds
ZoneConfig Config::zone;
SystemConfig Config::system;

bool Config::isStatusLedOn = false;
void Config::statusLedOn() {
Expand Down
138 changes: 81 additions & 57 deletions src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,82 +51,79 @@ const static char CONFIG_KEY_wifi_ssid[] PROGMEM = {"wifi:ssid"};
const static char CONFIG_KEY_wifi_password[] PROGMEM = {"wifi:password"};
const static char CONFIG_KEY_device_name[] PROGMEM = {"device:name"};
const static char CONFIG_KEY_system_mode[] PROGMEM = {"system:mode"};
const static char CONFIG_KEY_system_zone[] PROGMEM = {"system:zone"};
const static char CONFIG_KEY_screen_rotation[] PROGMEM = {"screen:rotation"};
const static char CONFIG_KEY_system_zone_id[] PROGMEM = {"system:zn_id"};
const static char CONFIG_KEY_system_zone_description[] PROGMEM = {"system:zn_dsc"};
const static char CONFIG_KEY_system_zone_name[] PROGMEM = {"system:zn_nam"};
const static char CONFIG_KEY_system_zone_offset[] PROGMEM = {"system:zn_ofs"};
const static char CONFIG_KEY_system_zone_lat[] PROGMEM = {"system:zn_lat"};
const static char CONFIG_KEY_system_zone_lng[] PROGMEM = {"system:zn_lng"};
const static char CONFIG_KEY_screen_rotation[] PROGMEM = {"screen:rotate"};

class ZoneConfig {
public:
String id;
String description;
String name;
int offset; // minutes
float latitude;
float longitude;
ZoneConfig() {
String id = "UTC";
String description = "Coordinated Universal Time";
name = "";
offset = 0; // UTC/GMT - Greenwich Mean Time
latitude = 51.477928; // Greenwich lat
longitude = -0.001545; // Greenwich lng
}
};

class SystemConfig {
public:
String friendlyName;
String systemMode;
String ssid, pass;
SystemConfig() {
friendlyName = CONFIG_BUILTIN_MODULE_NAME;
systemMode = "";
ssid = "";
pass = "";
}
};

class Config {
public:
const static short ServiceButtonPin = CONFIG_ServiceButtonPin;
const static short StatusLedPin = CONFIG_StatusLedPin;
const static uint16_t ConfigureButtonPushInterval = 2500;
static int TimeZone; // tz diff in milliseconds
static ZoneConfig zone;
static SystemConfig system;
#ifdef ESP32
static ESP32Time* getRTC() {
static ESP32Time rtc(TimeZone);
static ESP32Time rtc(0);
return &rtc;
}
static void setTimeZone(int tz) {
TimeZone = tz;
}
#endif
static void updateTimezone() {
int tzo = zone.offset / 60;
int tzm = zone.offset % 60;
char tz[9] = "";
sprintf(tz, "UTC%s%02d:%02d", tzo >= 0 ? "-" : "+", abs(tzo), abs(tzm));
setenv("TZ", tz, 1);
tzset();
}

static bool isDeviceConfigured() {
#ifdef CONFIGURE_WITH_WPS
return !WiFi.SSID().isEmpty() && !WiFi.psk().isEmpty();
#else
#ifndef DISABLE_PREFERENCES
String systemMode;
String ssid, pass;
Preferences preferences;
if (preferences.begin(CONFIG_SYSTEM_NAME, true)) {
systemMode = preferences.getString(CONFIG_KEY_system_mode);
ssid = preferences.getString(CONFIG_KEY_wifi_ssid);
pass = preferences.getString(CONFIG_KEY_wifi_password);
} else {
// initialize system preferences with default values
preferences.begin(CONFIG_SYSTEM_NAME, false);
preferences.putString(CONFIG_KEY_system_mode, "");
preferences.putString(CONFIG_KEY_wifi_ssid, "");
preferences.putString(CONFIG_KEY_wifi_password, "");
}
preferences.end();
return !systemMode.equals("config") && !ssid.isEmpty() && !pass.isEmpty();
return !system.systemMode.equals("config") && !system.ssid.isEmpty() && !system.pass.isEmpty();
#endif
#endif
return false;
}
static bool isWiFiConfigured() {
#ifdef CONFIGURE_WITH_WPS
return !WiFi.SSID().isEmpty() && !WiFi.psk().isEmpty();
#else
#ifndef DISABLE_PREFERENCES
String ssid, pass;
Preferences preferences;
if (preferences.begin(CONFIG_SYSTEM_NAME, true)) {
ssid = preferences.getString(CONFIG_KEY_wifi_ssid);
pass = preferences.getString(CONFIG_KEY_wifi_password);
}
preferences.end();
return !ssid.isEmpty() && !pass.isEmpty();
#endif
#endif
return false;
}

static String getDeviceName() {
#ifndef DISABLE_PREFERENCES
if (isDeviceConfigured()) {
// Read friendly name from prefs
Preferences preferences;
preferences.begin(CONFIG_SYSTEM_NAME, true);
String friendlyName = preferences.getString(CONFIG_KEY_device_name, CONFIG_BUILTIN_MODULE_NAME);
preferences.end();
return friendlyName;
} else {
return CONFIG_BUILTIN_MODULE_NAME;
}
#else
return CONFIG_BUILTIN_MODULE_NAME;
return !system.ssid.isEmpty() && !system.pass.isEmpty();
#endif
}

Expand Down Expand Up @@ -163,15 +160,42 @@ class Config {

static void init() {
// Setup status led
if (Config::StatusLedPin >= 0) pinMode(Config::StatusLedPin, OUTPUT);
if (StatusLedPin >= 0) pinMode(StatusLedPin, OUTPUT);
#ifndef DISABLE_PREFERENCES
// Time zone
Preferences preferences;

if (preferences.begin(CONFIG_SYSTEM_NAME, true)) {
Config::TimeZone = preferences.getInt(CONFIG_KEY_system_zone, 0);
// System and WiFi settings
system.friendlyName = preferences.getString(CONFIG_KEY_device_name);
system.systemMode = preferences.getString(CONFIG_KEY_system_mode);
system.ssid = preferences.getString(CONFIG_KEY_wifi_ssid);
system.pass = preferences.getString(CONFIG_KEY_wifi_password);
// Time Zone
zone.id = preferences.getString(CONFIG_KEY_system_zone_id);
zone.description = preferences.getString(CONFIG_KEY_system_zone_description);
zone.name = preferences.getString(CONFIG_KEY_system_zone_name);
zone.offset = preferences.getInt(CONFIG_KEY_system_zone_offset);
zone.latitude = preferences.getFloat(CONFIG_KEY_system_zone_lat);
zone.longitude = preferences.getFloat(CONFIG_KEY_system_zone_lng);
} else {
// initialize system preferences with default values
preferences.begin(CONFIG_SYSTEM_NAME, false);
preferences.putString(CONFIG_KEY_device_name, system.friendlyName);
preferences.putString(CONFIG_KEY_system_mode, system.systemMode);
preferences.putString(CONFIG_KEY_wifi_ssid, system.ssid);
preferences.putString(CONFIG_KEY_wifi_password, system.pass);
// Time Zone
preferences.putString(CONFIG_KEY_system_zone_id, zone.id);
preferences.putString(CONFIG_KEY_system_zone_description, zone.description);
preferences.putString(CONFIG_KEY_system_zone_name, zone.name);
preferences.putInt(CONFIG_KEY_system_zone_offset, zone.offset);
preferences.putFloat(CONFIG_KEY_system_zone_lat, zone.latitude);
preferences.putFloat(CONFIG_KEY_system_zone_lng, zone.longitude);
}

preferences.end();
#endif
updateTimezone();
}
};

Expand Down
3 changes: 2 additions & 1 deletion src/HomeGenie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ namespace Service {
HomeGenie::HomeGenie() {
eventRouter.withNetManager(netManager);

// Setup status led
// Load system settings and setup status led
Config::init();

// Setup button
pinMode(Config::ServiceButtonPin, INPUT_PULLUP);
// attachInterrupt(digitalPinToInterrupt(Config::ServiceButtonPin), buttonChange, CHANGE);
Expand Down
15 changes: 14 additions & 1 deletion src/Utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,17 @@ uint32_t Utility::getFreeMem() {
uint32_t freeMem = esp_get_free_heap_size();
#endif
return freeMem;
}
}

time_t Utility::relativeUtcHoursToLocalTime(double relativeHours, time_t dt) {
relativeHours = relativeHours + ((double)Config::zone.offset / 60.0);
int m = int(round(relativeHours * 60));
int hr = (m / 60) % 24;
int mn = m % 60;
struct tm* tm_struct = localtime(&dt);
tm_struct->tm_hour = hr;
tm_struct->tm_min = mn;
tm_struct->tm_sec = 0;
// TODO: should also set DST??
return mktime(tm_struct);
}
1 change: 1 addition & 0 deletions src/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Utility {
static uint8_t reverseByte(uint8_t n);
static ColorRGB hsv2rgb(float H, float S, float V);
static uint32_t getFreeMem();
static time_t relativeUtcHoursToLocalTime(double relativeHours, time_t time);
};

#endif //HOMEGENIE_MINI_UTILITY_H
Loading

0 comments on commit 59308dd

Please sign in to comment.