Skip to content

Commit

Permalink
add configurable timezone
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
tedpearson committed Aug 11, 2024
1 parent 819e464 commit 4bac2c3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
1 change: 1 addition & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions internal/app/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 4bac2c3

Please sign in to comment.