Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ptz control #19

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions api/c/commanders.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package api

import (
"strconv"

"github.com/gin-gonic/gin"
"github.com/panjjo/gosip/m"
sipapi "github.com/panjjo/gosip/sip"
)

// | 名称 | 形式 | 类型 | 必选 | 描述 |
// | ------- | -------- | ------- | --- | -------------------------------------------------------------------------------------------- |
// | id | path | string | 是 | 设备id |
// | command | formData | string | 是 | 控制指令 允许值: left, right, up, down, upleft, upright, downleft, downright, zoomin, zoomout, stop |
// | channel | formData | string | 否 | 通道id,默认会选择第一个通道 |
// | speed | formData | Integer | 否 | 速度(0~255) 默认值: 129 |

func PTZCtrl(c *gin.Context) {
id := c.Param("id")
if id == "" {
m.JsonResponse(c, m.StatusParamsERR, "缺少设备ID")
return
}
device := sipapi.GetActiveDevice(id)
if device == nil {
m.JsonResponse(c, m.StatusDBERR, "该设备不在线")
return
}
command := c.Query("command")
if command == "" {
m.JsonResponse(c, m.StatusParamsERR, "缺少控制指令")
return
}
channelId := c.Query("channel")
if channelId != "" {
device.DeviceID = channelId
}
speedStr := c.Query("speed")
speed := 50
if speedStr != "" {
speed1, err := strconv.Atoi(speedStr)
if err != nil {
m.JsonResponse(c, m.StatusParamsERR, "速度参数转换数据类型有误")
return
}
if speed1 < 0 && speed1 > 255 {
m.JsonResponse(c, m.StatusParamsERR, "速度参数不在合理范围内0~255")
return
}
speed = speed1
}

cmdCode := 0
switch command {
case "left":
cmdCode = 2
case "right":
cmdCode = 1
case "up":
cmdCode = 8
case "down":
cmdCode = 4
case "upleft":
cmdCode = 10
case "upright":
cmdCode = 9
case "downleft":
cmdCode = 6
case "downright":
cmdCode = 5
case "zoomin":
cmdCode = 16
case "zoomout":
cmdCode = 32
case "stop":
cmdCode = 0
speed = 0
default:
m.JsonResponse(c, m.StatusParamsERR, "未知控制指令")
return
}
cmdStr := sipapi.CmdString(cmdCode, speed, speed, speed)

err := sipapi.DevicePTZ(*device, cmdStr)
if err != nil {
m.JsonResponse(c, m.StatusParamsERR, err)
return
}
m.JsonResponse(c, m.StatusSucc, "")
}
4 changes: 4 additions & 0 deletions api/c/zlm.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func ZLMWebHook(c *gin.Context) {
"enableRtxp": m.MConfig.Stream.RTMP,
"msg": "success",
})
case "on_server_keepalive":
c.JSON(http.StatusOK, map[string]any{
"code": 0,
"msg": "success"})
case "on_stream_none_reader":
// 无人阅读通知 关闭流
zlmStreamNoneReader(c)
Expand Down
4 changes: 4 additions & 0 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func Init(r *gin.Engine) {
{
r.GET("/channels/:id/records", api.RecordsList)
}
// PTZ 控制
{
r.POST("/devices/:id/ptz", api.PTZCtrl)
}
// zlm webhook
{
r.POST("/zlm/webhook/:method", api.ZLMWebHook)
Expand Down
2 changes: 1 addition & 1 deletion config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod: debug
database:
dialect: mysql # mysql postgresql sqllite
dialect: mysql # mysql postgresql sqlite3
url: root:123456@tcp(localhost:3307)/gosip?charset=utf8&parseTime=True&loc=Local # 数据库地址
udp: 0.0.0.0:5060 # sip服务器udp端口
api: 0.0.0.0:8090 # sip服务 restfulapi 端口
Expand Down
2 changes: 2 additions & 0 deletions db/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
"time"

_ "github.com/jinzhu/gorm/dialects/sqlite"

_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/panjjo/gorm"
// _ "github.com/jinzhu/gorm/dialects/postgres"
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ require (
github.com/gin-gonic/gin v1.8.1
github.com/gofrs/uuid v4.3.0+incompatible
github.com/jinzhu/gorm v1.9.16
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/panjjo/gorm v1.0.1
github.com/panjjo/gosdp v0.0.0-20201029020038-56e3a0ec56ef
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5
github.com/robfig/cron v1.2.0
github.com/sirupsen/logrus v1.9.0
github.com/spf13/viper v1.13.0
Expand Down Expand Up @@ -36,12 +38,15 @@ require (
github.com/goccy/go-json v0.9.7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lestrrat-go/strftime v1.0.6 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-sqlite3 v1.14.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M=
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand All @@ -227,6 +229,12 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc h1:RKf14vYWi2ttpEmkA4aQ3j4u9dStX2t4M8UM6qqNsG8=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is=
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkLibYKgg+SwmyFU9dF2hn6MdTj4=
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA=
github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205AhTIGQQ=
github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw=
github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4=
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
Expand Down Expand Up @@ -285,6 +293,8 @@ github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 h1:mZHayPoR0lNmnHyvtYjDeq0zlVHn9K/ZXoy17ylucdo=
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5/go.mod h1:GEXHk5HgEKCvEIIrSpFI3ozzG5xOKA2DVlEX/gGnewM=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down
63 changes: 63 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"bytes"
"fmt"
"path/filepath"
"time"

rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
)

type LoggerFormatter struct{}

func (m *LoggerFormatter) Format(entry *logrus.Entry) ([]byte, error) {
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}

timestamp := entry.Time.Format("2006-01-02 15:04:05")
var newLog string

//HasCaller()为true才会有调用信息
if entry.HasCaller() {
fName := filepath.Base(entry.Caller.File)
newLog = fmt.Sprintf("[%s] [%s] [%s:%d %s] %s\n",
timestamp, entry.Level, fName, entry.Caller.Line, entry.Caller.Function, entry.Message)
} else {
newLog = fmt.Sprintf("[%s] [%s] %s\n", timestamp, entry.Level, entry.Message)
}

b.WriteString(newLog)
return b.Bytes(), nil
}

func initLog() {

filepaths := "./log/gosip.log"
writer, _ := rotatelogs.New(
filepaths+".%Y%m%d%H%M",
rotatelogs.WithLinkName(filepaths),
rotatelogs.WithMaxAge(time.Duration(604800)*time.Second),
rotatelogs.WithRotationTime(time.Duration(86400)*time.Second),
)
writeMap := lfshook.WriterMap{
logrus.InfoLevel: writer,
logrus.FatalLevel: writer,
logrus.DebugLevel: writer,
logrus.WarnLevel: writer,
logrus.ErrorLevel: writer,
logrus.PanicLevel: writer,
logrus.TraceLevel: writer,
}

logrus.SetReportCaller(true)
lfHook := lfshook.NewHook(writeMap, &LoggerFormatter{})
logrus.AddHook(lfHook)

}
9 changes: 5 additions & 4 deletions m/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ func DefaultInfo() *SysInfo {

var MConfig *Config

func LoadConfig() {
viper.SetConfigType("yml")
viper.SetConfigName("config")
viper.AddConfigPath("./")
func LoadConfig(fpath string) {
// viper.SetConfigType("yml")
// viper.SetConfigName("gosip")
// viper.AddConfigPath(filepath.Dir(fpath))
viper.SetConfigFile(fpath)
viper.SetDefault("logger", "debug")
viper.SetDefault("udp", "0.0.0.0:5060")
viper.SetDefault("api", "0.0.0.0:8090")
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"net/http"
_ "net/http/pprof"

Expand Down Expand Up @@ -36,6 +37,9 @@ import (
// @securityDefinitions.basic BasicAuth

func main() {
var config_fpath = flag.String("c", "./conf.yml", "set a config file")
flag.Parse()
m.LoadConfig(*config_fpath)
//pprof
go func() {
http.ListenAndServe("0.0.0.0:6060", nil)
Expand All @@ -54,7 +58,7 @@ func main() {
}

func init() {
m.LoadConfig()
initLog()
_cron()
}

Expand Down
35 changes: 35 additions & 0 deletions sip/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/xml"
"fmt"
"net"
"strconv"
"time"

"github.com/panjjo/gosip/db"
Expand Down Expand Up @@ -222,6 +223,40 @@ func sipCatalog(to Devices) {
}
}

func GetActiveDevice(deviceID string) *Devices {
device, ok := _activeDevices.Get(deviceID)
if !ok {
return nil
}
return &device
}

func DevicePTZ(to Devices, cmd string) error {
to.addr.URI.SetHost(to.Host)
iport, _ := strconv.Atoi(to.Port)
to.addr.URI.FPort = sip.NewPort(iport)
logrus.Infoln("to addr URI:", to.addr.URI.String())
logrus.Infoln("from addr URI:", _serverDevices.addr.URI.String())

hb := sip.NewHeaderBuilder().SetTo(to.addr).SetFrom(_serverDevices.addr).AddVia(&sip.ViaHop{
Params: sip.NewParams().Add("branch", sip.String{Str: sip.GenerateBranch()}),
}).SetContentType(&sip.ContentTypeXML).SetMethod(sip.MESSAGE)

req := sip.NewRequest("", sip.MESSAGE, to.addr.URI, sip.DefaultSipVersion, hb.Build(), sip.GetDevicePTZXML(to.DeviceID, cmd))
req.SetDestination(to.source)
tx, err := srv.Request(req)
if err != nil {
logrus.Warnln("sipDevicePTZ error,", err)
return err
}
_, err = sipResponse(tx)
if err != nil {
logrus.Warnln("sipDevicePTZ response error,", err)
return err
}
return nil
}

// MessageDeviceInfoResponse 主设备明细返回结构
type MessageDeviceInfoResponse struct {
CmdType string `xml:"CmdType"`
Expand Down
2 changes: 1 addition & 1 deletion sip/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,6 @@ func handlerRegister(req *sip.Request, tx *sip.Transaction) {
}
}
resp := sip.NewResponseFromRequest("", req, http.StatusUnauthorized, http.StatusText(http.StatusUnauthorized), nil)
resp.AppendHeader(&sip.GenericHeader{HeaderName: "WWW-Authenticate", Contents: fmt.Sprintf("Digest nonce=\"%s\", algorithm=MD5, realm=\"%s\",qop=\"auth\"", utils.RandString(32), _sysinfo.Region)})
resp.AppendHeader(&sip.GenericHeader{HeaderName: "WWW-Authenticate", Contents: fmt.Sprintf("Digest nonce=\"%s\",algorithm=MD5,realm=\"%s\",qop=\"auth\"", utils.RandString(32), _sysinfo.Region)})
tx.Respond(resp)
}
2 changes: 1 addition & 1 deletion sip/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func SipRecordList(to *Channels, start, end int64) (*Records, error) {
defer close(resp)
device, ok := _activeDevices.Get(to.DeviceID)
if !ok {
return nil, errors.New("设备不在线")
return nil, errors.New(fmt.Sprintf("设备[%s]不在线", to.DeviceID))
}
channelURI, _ := sip.ParseURI(to.URIStr)
to.addr = &sip.Address{URI: channelURI}
Expand Down
17 changes: 17 additions & 0 deletions sip/s/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ var (
<SN>%d</SN>
<DeviceID>%s</DeviceID>
</Query>
`

DevicePTZXML = `<?xml version="1.0" encoding="GB2312"?>
<Control>
<CmdType>DeviceControl</CmdType>
<SN>%d</SN>
<DeviceID>%s</DeviceID>
<PTZCmd>%s</PTZCmd>
<Info>
<ControlPriority>5</ControlPriority>
</Info>
</Control>
`
)

Expand All @@ -114,6 +126,11 @@ func GetDeviceInfoXML(id string) []byte {
return []byte(fmt.Sprintf(DeviceInfoXML, utils.RandInt(100000, 999999), id))
}

// GetDevicePTZXML 获取设备ptz指令
func GetDevicePTZXML(id string, cmd string) []byte {
return []byte(fmt.Sprintf(DevicePTZXML, utils.RandInt(100000, 999999), id, cmd))
}

// GetCatalogXML 获取NVR下设备列表指令
func GetCatalogXML(id string) []byte {
return []byte(fmt.Sprintf(CatalogXML, utils.RandInt(100000, 999999), id))
Expand Down
Loading