Skip to content

Commit

Permalink
Fix off by one hour error (#2)
Browse files Browse the repository at this point in the history
* adding posix time lib

* change time instatiation

* add dst boolean

* convert offset into possix offset

* ups go version to 1.19
  • Loading branch information
isaric authored Sep 3, 2022
1 parent 09a15b2 commit 45d3fd7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.17
go-version: 1.19
- name: Build motion-poll
run: go build -v motion-poll.go
- name: Build set-time
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ of this script don't support subscription.
In order to use the polling script you must provide the mandatory arguments or the script will fail immediately. These include the
base url of the camera, auth details and a slack webhook to which the notification will be posted.

It is also possible to provide the cooldown time and give the slack channel and bot token. The script will then
It is also possible to provide the cooldown time and give the Slack channel and bot token. The script will then
grab a snapshot from the camera and upload it to slack. The bot must have file upload privileges. There are more
details in this guide [here](https://api.slack.com/methods/files.upload).

Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module path-variable.com/onvif

go 1.17
go 1.19

require (
github.com/isaric/go-posix-time v1.0.0
github.com/use-go/onvif v0.0.1
gopkg.in/xmlpath.v2 v2.0.0-20150820204837-860cbeca3ebc
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/isaric/go-posix-time v1.0.0 h1:xSXSuxn9PiP6wjSFu8/9DTfPZQH19yZsZHrsEezoT9s=
github.com/isaric/go-posix-time v1.0.0/go.mod h1:obM8z4QYGl9iONV+vFO3N/u2iUC/oVv8Zf6r4UBxn7s=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
Expand Down
48 changes: 30 additions & 18 deletions set-time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"github.com/isaric/go-posix-time/pkg/p_time"
"github.com/use-go/onvif"
"github.com/use-go/onvif/device"
"github.com/use-go/onvif/xsd"
Expand All @@ -11,12 +12,14 @@ import (
"time"
)

/**
/*
*
Script for setting the date and time on an ONVIF camera- specifically designed for Hisseu cameras
CLI args:
0 - url and port, separated by semicolon
1 - username
2 - password
0 - url and port, separated by semicolon
1 - username
2 - password
*/
func main() {
// get and validate number of cli args
Expand All @@ -40,8 +43,9 @@ func main() {

// repeat call after interval passes
for true {
ct := time.Now().Local()
_, err := cam.CallMethod(getOnvifDateTime(ct))
ct := time.Now()
req := getOnvifDateTime(ct)
_, err := cam.CallMethod(req)
if err != nil {
fmt.Printf("Could not set time. %s Exiting!\n", err)
return
Expand All @@ -52,16 +56,24 @@ func main() {
}

func getOnvifDateTime(ct time.Time) device.SetSystemDateAndTime {
return device.SetSystemDateAndTime{DateTimeType: "Manual", UTCDateTime: onvif2.DateTime(struct {
Time onvif2.Time
Date onvif2.Date
}{Time: onvif2.Time(struct {
Hour xsd.Int
Minute xsd.Int
Second xsd.Int
}{Hour: xsd.Int(ct.Hour()), Minute: xsd.Int(ct.Minute()), Second: xsd.Int(ct.Second())}), Date: onvif2.Date(struct {
Year xsd.Int
Month xsd.Int
Day xsd.Int
}{Year: xsd.Int(ct.Year()), Month: xsd.Int(ct.Month()), Day: xsd.Int(ct.Day())})})}
_, ost := ct.Zone()
diff := time.Duration(-(ost/3600 - 1)) * time.Hour
ct = ct.Add(diff)

return device.SetSystemDateAndTime{
DaylightSavings: xsd.Boolean(ct.IsDST()),
TimeZone: onvif2.TimeZone{TZ: xsd.Token(p_time.FormatTimeZone(ct))},
DateTimeType: "Manual",
UTCDateTime: onvif2.DateTime(struct {
Time onvif2.Time
Date onvif2.Date
}{Time: onvif2.Time(struct {
Hour xsd.Int
Minute xsd.Int
Second xsd.Int
}{Hour: xsd.Int(ct.Hour()), Minute: xsd.Int(ct.Minute()), Second: xsd.Int(ct.Second())}), Date: onvif2.Date(struct {
Year xsd.Int
Month xsd.Int
Day xsd.Int
}{Year: xsd.Int(ct.Year()), Month: xsd.Int(ct.Month()), Day: xsd.Int(ct.Day())})})}
}

0 comments on commit 45d3fd7

Please sign in to comment.