mirror of
https://github.com/Monibuca/plugin-onvif.git
synced 2025-10-06 00:07:01 +08:00
v4
This commit is contained in:
136
main.go
136
main.go
@@ -1,66 +1,70 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
. "github.com/Monibuca/engine/v3"
|
||||
)
|
||||
|
||||
var config struct {
|
||||
DiscoverInterval int `toml:"DiscoverInterval"`
|
||||
Interfaces []struct {
|
||||
InterfaceName string `toml:"InterfaceName"`
|
||||
Username string `toml:"Username"`
|
||||
Password string `toml:"Password"`
|
||||
} `toml:"interfaces"`
|
||||
Devices []struct {
|
||||
IP string `toml:"Ip"`
|
||||
Username string `toml:"Username"`
|
||||
Password string `toml:"Password"`
|
||||
} `toml:"devices"`
|
||||
}
|
||||
|
||||
var authCfg = &AuthConfig{
|
||||
Interfaces: make(map[string]deviceAuth),
|
||||
Devices: make(map[string]deviceAuth),
|
||||
}
|
||||
|
||||
var deviceList = &DeviceList{Data: make(map[string]map[string]*DeviceStatus)}
|
||||
|
||||
func init() {
|
||||
pconfig := PluginConfig{
|
||||
Name: "ONVIF",
|
||||
Config: &config,
|
||||
}
|
||||
pconfig.Install(runPlugin)
|
||||
}
|
||||
|
||||
func runPlugin() {
|
||||
preprocessAuth(authCfg)
|
||||
if config.DiscoverInterval == 0 {
|
||||
config.DiscoverInterval = 30
|
||||
}
|
||||
t := time.NewTicker(time.Duration(config.DiscoverInterval) * time.Second)
|
||||
go func() {
|
||||
for range t.C {
|
||||
deviceList.discoveryDevice()
|
||||
deviceList.pullStream()
|
||||
}
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
func preprocessAuth(c *AuthConfig) {
|
||||
for _, i := range config.Interfaces {
|
||||
c.Interfaces[i.InterfaceName] = deviceAuth{
|
||||
Username: i.Username,
|
||||
Password: i.Password,
|
||||
}
|
||||
}
|
||||
for _, d := range config.Devices {
|
||||
c.Devices[d.IP] = deviceAuth{
|
||||
Username: d.Username,
|
||||
Password: d.Password,
|
||||
}
|
||||
}
|
||||
}
|
||||
package onvif
|
||||
|
||||
import (
|
||||
. "m7s.live/engine/v4"
|
||||
"time"
|
||||
)
|
||||
|
||||
type OnvifConfig struct {
|
||||
DiscoverInterval int
|
||||
Interfaces []struct {
|
||||
InterfaceName string
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
Devices []struct {
|
||||
IP string
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
}
|
||||
|
||||
func (o *OnvifConfig) init() {
|
||||
preprocessAuth(authCfg)
|
||||
if o.DiscoverInterval == 0 {
|
||||
o.DiscoverInterval = 30
|
||||
}
|
||||
go func() {
|
||||
deviceList.discoveryDevice()
|
||||
deviceList.pullStream()
|
||||
}()
|
||||
t := time.NewTicker(time.Duration(o.DiscoverInterval) * time.Second)
|
||||
go func() {
|
||||
for range t.C {
|
||||
deviceList.discoveryDevice()
|
||||
deviceList.pullStream()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (o *OnvifConfig) OnEvent(event any) {
|
||||
switch event.(type) {
|
||||
case FirstConfig:
|
||||
o.init()
|
||||
}
|
||||
}
|
||||
|
||||
var authCfg = &AuthConfig{
|
||||
Interfaces: make(map[string]deviceAuth),
|
||||
Devices: make(map[string]deviceAuth),
|
||||
}
|
||||
|
||||
var deviceList = &DeviceList{Data: make(map[string]map[string]*DeviceStatus)}
|
||||
|
||||
var conf = &OnvifConfig{}
|
||||
var plugin = InstallPlugin(conf)
|
||||
|
||||
func preprocessAuth(c *AuthConfig) {
|
||||
for _, i := range conf.Interfaces {
|
||||
c.Interfaces[i.InterfaceName] = deviceAuth{
|
||||
Username: i.Username,
|
||||
Password: i.Password,
|
||||
}
|
||||
}
|
||||
for _, d := range conf.Devices {
|
||||
c.Devices[d.IP] = deviceAuth{
|
||||
Username: d.Username,
|
||||
Password: d.Password,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user