From 4bac2c3babb24d9588f50bad1d4109317d3435a1 Mon Sep 17 00:00:00 2001 From: Ted Pearson <1477390+tedpearson@users.noreply.github.com> Date: Sat, 10 Aug 2024 22:07:11 -0400 Subject: [PATCH] add configurable timezone - For some reason, the SmartHub API decided to return "unix timestamps", but in the utility's timezone instead of in UTC, which would be the normal choice for an API. - My utility returned timestamps in the EST timezone, but other users have reported their local timezones are used as well. --- README.md | 3 +++ config.example.yaml | 1 + internal/app/main.go | 3 ++- internal/app/parser.go | 6 +++--- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5344f60..534e47e 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,9 @@ Download [config.example.yaml](config.example.yaml) and fill in your own values. - Navigate to Usage Explorer (example: https://novec.smarthub.coop/ui/#/usageExplorer) - Find a call to `services/secured/utility-usage/poll` in the Network tab - Open the call, and copy the `serviceLocationNumber` field from the Payload tab. +- `timezone` needs to be set to the timezone used by your utility. For some reason, + the SmartHub API decided to return unix timestamps, but in the utility's timezone + instead of in UTC, which would be the normal choice for an API. - `influxdb.insecure` allows connecting to a server with certificate issues. - The other fields should be fairly self-explanatory. diff --git a/config.example.yaml b/config.example.yaml index afd7132..c091d61 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -5,6 +5,7 @@ smarthub: password: smarthub_password account: 1234567001 service_location: 123456 + timezone: America/New_York influxdb: host: https://localhost:8428 auth_token: johndoe:influx_password diff --git a/internal/app/main.go b/internal/app/main.go index 584d996..2ceda8c 100644 --- a/internal/app/main.go +++ b/internal/app/main.go @@ -29,6 +29,7 @@ type SmartHubConfig struct { Password string `yaml:"password"` Account string `yaml:"account"` ServiceLocation string `yaml:"service_location"` + Timezone string `yaml:"timezone"` } // Config is the config format for electric-usage-downloader @@ -105,7 +106,7 @@ func Main() error { if err != nil { return nil, err } - records, err := ParseReader(r) + records, err := ParseReader(r, config.SmartHub.Timezone) if err != nil { return nil, err } diff --git a/internal/app/parser.go b/internal/app/parser.go index fa857f4..2e32dd8 100644 --- a/internal/app/parser.go +++ b/internal/app/parser.go @@ -68,7 +68,7 @@ func (t *RetryableError) Error() string { // ParseReader parses the json response received in FetchData from the SmartHub poll api. // It can return a normal error, a RetryableError, or parsed ElectricUsage. -func ParseReader(readCloser io.ReadCloser) ([]ElectricUsage, error) { +func ParseReader(readCloser io.ReadCloser, timezone string) ([]ElectricUsage, error) { defer func() { if err := readCloser.Close(); err != nil { fmt.Println("Error: failed to close response body") @@ -107,10 +107,10 @@ func ParseReader(readCloser io.ReadCloser) ([]ElectricUsage, error) { } } // this is dumb, but the SmartHub api returns "unix timestamps" - // that are based on EST (which is incorrect), at least as of 2/29/2024. + // that are based on the utility timezone (which is incorrect), at least as of 2/29/2024. // Example: For Midnight, Jan 1, 1970, EST, this api would return "0" // However, the correct value (UTC) would be "18000". - zone, err := time.LoadLocation("America/New_York") + zone, err := time.LoadLocation(timezone) if err != nil { return nil, err }