Skip to content

Commit

Permalink
Install fix and discover command (#14)
Browse files Browse the repository at this point in the history
* fixes install process, adds prefix to commands and adds new discovery command

* adds new command to Makefile
  • Loading branch information
isaric authored Dec 3, 2023
1 parent 2ad1e36 commit a0d0085
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 60 deletions.
28 changes: 19 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ jobs:
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: "https://dl.google.com/go/go1.19.1.linux-amd64.tar.gz"
project_path: "./cmd/motion-poll"
binary_name: "motion-poll"
project_path: "./cmd/onvif-motion-poll"
binary_name: "onvif-motion-poll"
extra_files: LICENSE.txt README.md
- name: release set-time
uses: wangyoucao577/go-release-action@v1
Expand All @@ -39,18 +39,18 @@ jobs:
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: "https://dl.google.com/go/go1.19.1.linux-amd64.tar.gz"
project_path: "./cmd/set-time"
binary_name: "set-time"
project_path: "./cmd/onvif-set-time"
binary_name: "onvif-set-time"
extra_files: LICENSE.txt README.md
- name: release set-preset
- name: release onvif-set-preset
uses: wangyoucao577/go-release-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: "https://dl.google.com/go/go1.19.1.linux-amd64.tar.gz"
project_path: "./cmd/set-preset"
binary_name: "set-preset"
project_path: "./cmd/onvif-set-preset"
binary_name: "onvif-set-preset"
extra_files: LICENSE.txt README.md
- name: release goto-preset
uses: wangyoucao577/go-release-action@v1
Expand All @@ -59,6 +59,16 @@ jobs:
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: "https://dl.google.com/go/go1.19.1.linux-amd64.tar.gz"
project_path: "./cmd/goto-preset"
binary_name: "goto-preset"
project_path: "./cmd/onvif-goto-preset"
binary_name: "onvif-goto-preset"
extra_files: LICENSE.txt README.md
- name: release discover-all
uses: wangyoucao577/go-release-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: "https://dl.google.com/go/go1.19.1.linux-amd64.tar.gz"
project_path: "./cmd/discover-all"
binary_name: "onvif-discover-all"
extra_files: LICENSE.txt README.md
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ install:
## run go install
go install ./...
## create system-wide symlink to executables
bash ./install_scripts/create_symlinks.sh motion-poll set-preset goto-preset set-time
bash ./install_scripts/create_symlinks.sh onvif-motion-poll onvif-set-preset onvif-goto-preset onvif-set-time onvif-discover-all
## create config folder
bash ./install_scripts/create_service_templates.sh

61 changes: 61 additions & 0 deletions cmd/onvif-discover-all/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"fmt"
"github.com/jessevdk/go-flags"
"github.com/path-variable/onvif-cam-poll/pkg/model"
"github.com/path-variable/onvif-cam-poll/pkg/utils"
"github.com/use-go/onvif"
"os"
"strings"
"time"
)

const commandName = "onvif-discover-all"

func main() {
var opts discoverAllOptions
_, err := flags.ParseArgs(&opts, os.Args)

if err != nil {
fmt.Printf(utils.ArgParseError, err)
return
}

for {
fmt.Printf(utils.CommandSend, commandName)
res, err := onvif.GetAvailableDevicesAtSpecificEthernetInterface(opts.Interface)
if err != nil {
fmt.Printf(utils.CommandError, commandName, err)
return
}

fmt.Printf("Discovered %d devices on interface %s\n", len(res), opts.Interface)

for i := 0; i < len(res); i++ {
dev := res[i]
fmt.Printf("Device at %s\n", getAddressFromServices(dev))
}

fmt.Printf(utils.SleepTemplate, opts.CooldownTimer)
time.Sleep(time.Duration(opts.CooldownTimer) * time.Second)
}

}

func getAddressFromServices(device onvif.Device) string {
val, found := device.GetServices()["device"]
if found {
return getAddressFromUrl(val)
}
return ""
}

func getAddressFromUrl(url string) string {
return strings.Split(url, ":")[1][2:]
}

type discoverAllOptions struct {
model.CooldownParameters
model.InterfaceParameters
}
6 changes: 3 additions & 3 deletions cmd/goto-preset/main.go → cmd/onvif-goto-preset/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
token "github.com/use-go/onvif/xsd/onvif"
)

const commandName = "goto-preset"
const commandName = "onvif-goto-preset"

/**
Sends the camera to the target preset
Expand All @@ -38,10 +38,10 @@ func main() {
PresetToken: token.ReferenceToken(opts.PositionPreset),
ProfileToken: token.ReferenceToken(opts.Profile),
}
fmt.Printf(utils.CommandSend, commandName)
fmt.Printf(utils.CommandSend, commandName)
_, err := sdk_ptz.Call_GotoPreset(context.TODO(), cam, gtreq)
if err != nil {
fmt.Printf(utils.CommandError,commandName, err)
fmt.Printf(utils.CommandError, commandName, err)
return
}
fmt.Printf(utils.SleepTemplate, opts.CooldownTimer)
Expand Down
10 changes: 8 additions & 2 deletions cmd/motion-poll/main.go → cmd/onvif-motion-poll/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Script for polling an ONVIF camera and getting motion events - specifically desi
*/

const ssErrorTemplate = "Error while getting snapshot %s\n"
const commandName = "onvif-motion-poll"

func main() {
var opts options
Expand All @@ -38,7 +39,7 @@ func main() {
// make initial pull point subscription
cam, _ := onvif.NewDevice(onvif.DeviceParams{Xaddr: opts.Address, Username: opts.Username, Password: opts.Password})
res := &event.CreatePullPointSubscription{SubscriptionPolicy: event.SubscriptionPolicy{ChangedOnly: true},
InitialTerminationTime: event.AbsoluteOrRelativeTimeType {
InitialTerminationTime: event.AbsoluteOrRelativeTimeType{
Duration: "PT300S",
}}
_, err = cam.CallMethod(res)
Expand All @@ -56,7 +57,12 @@ func main() {

// continue polling for motion events. if motion is detected, send Slack notification
for {
r2, _ := cam.CallMethod(event.PullMessages{})
fmt.Printf(utils.CommandSend, commandName)
r2, err := cam.CallMethod(event.PullMessages{})
if err != nil {
fmt.Printf(utils.CommandError, commandName, err)
return
}
bodyBytes, _ := io.ReadAll(r2.Body)
bodyS := string(bodyBytes)
if strings.Contains(bodyS, "<tt:SimpleItem Name=\"IsMotion\" Value=\"true\" />") {
Expand Down
4 changes: 2 additions & 2 deletions cmd/set-preset/main.go → cmd/onvif-set-preset/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
token "github.com/use-go/onvif/xsd/onvif"
)

const commandName = "set-preset"
const commandName = "onvif-set-preset"

/**
Records the current camera position on the passed preset token
Expand All @@ -42,7 +42,7 @@ func main() {
fmt.Printf(utils.CommandSend, commandName)
_, err = sdk_ptz.Call_SetPreset(context.TODO(), cam, gtreq)
if err != nil {
fmt.Printf(utils.CommandError,commandName, err)
fmt.Printf(utils.CommandError, commandName, err)
return
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/set-time/main.go → cmd/onvif-set-time/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
onvif2 "github.com/use-go/onvif/xsd/onvif"
)

const commandName = "set-time"
const commandName = "onvif-set-time"

/*
*
Script for setting the date and time on an ONVIF camera- specifically designed for Hisseu cameras
Expand Down Expand Up @@ -47,7 +48,7 @@ func main() {
fmt.Printf(utils.CommandSend, commandName)
_, err := sdk.Call_SetSystemDateAndTime(context.TODO(), cam, req)
if err != nil {
fmt.Printf(utils.CommandError,commandName, err)
fmt.Printf(utils.CommandError, commandName, err)
return
}
fmt.Printf(utils.SleepTemplate, opts.CooldownTimer)
Expand Down
4 changes: 2 additions & 2 deletions install_scripts/create_symlinks.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function create_symlink() {
sudo rm "/usr/bin/$1"
sudo ln -s "$GOPATH/$1" "/usr/bin/$1"
sudo rm "/usr/bin/$1" || true
sudo ln -s "$GOPATH/bin/$1" "/usr/bin/$1"
}

for var in "$@"
Expand Down
16 changes: 0 additions & 16 deletions motion@.service

This file was deleted.

18 changes: 11 additions & 7 deletions pkg/model/types.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package model

type BasicParameters struct {
Username string `short:"u" long:"user" description:"The username for authenticating to the ONVIF device" required:"true"`
Password string `short:"p" long:"password" description:"The password for authenticating to the ONVIF device" required:"true"`
Address string `short:"a" long:"address" description:"The address of the ONVIF device and its port separated by semicolon" required:"true"`
Profile string `short:"r" long:"profile" description:"The onvif profile to be used" required:"false" default:"000"`
Username string `short:"u" long:"user" description:"The username for authenticating to the ONVIF device" required:"true"`
Password string `short:"p" long:"password" description:"The password for authenticating to the ONVIF device" required:"true"`
Address string `short:"a" long:"address" description:"The address of the ONVIF device and its port separated by semicolon" required:"true"`
Profile string `short:"r" long:"profile" description:"The onvif profile to be used" required:"false" default:"000"`
}

type SlackParameters struct {
Expand All @@ -18,9 +18,13 @@ type PresetParameters struct {
}

type CooldownParameters struct {
CooldownTimer int `short:"t" long:"cooldown" description:"The integer value of the number of seconds after an event has occurred before polling resumes" required:"false" default:"60"`
CooldownTimer int `short:"t" long:"cooldown" description:"The integer value of the number of seconds after an event has occurred before polling resumes" required:"false" default:"60"`
}

type CameraNameParameters struct {
CameraName string `short:"n" long:"name" description:"The name or location of the ONVIF device that will appear in all notifications" required:"true"`
}
CameraName string `short:"n" long:"name" description:"The name or location of the ONVIF device that will appear in all notifications" required:"true"`
}

type InterfaceParameters struct {
Interface string `short:"i" long:"interface" description:"Prints a list of all discovered ONVIF devices on the specified interface" required:"true"`
}
16 changes: 0 additions & 16 deletions time@.service

This file was deleted.

0 comments on commit a0d0085

Please sign in to comment.