Ver Fonte

调整代码

terry há 8 meses atrás
pai
commit
30469ba583
8 ficheiros alterados com 60 adições e 42 exclusões
  1. 9 8
      config.yaml
  2. 10 3
      lc/IDevice.go
  3. 2 3
      lc/camera_event.go
  4. 17 16
      lc/model/speaker.go
  5. 2 3
      lc/radio_event.go
  6. 8 4
      lc/server.go
  7. 11 4
      lc/speaker.go
  8. 1 1
      util/logrus.go

+ 9 - 8
config.yaml

@@ -17,14 +17,15 @@ radios:
   - port: "/dev/ttymxc6"
     name: "同向"
     branch: 1
-screens:
-  - name: "主路1输出设备"
-    ip: "192.168.1.200"
-    port: "5005"
-    branch: 0
+#screens:
+#  - name: "主路1输出设备"
+#    ip: "192.168.1.200"
+#    port: "5005"
+#    branch: 0
 speakers:
-  - name: "主路1输出设备"
+  - name: "主路1 IP音柱"
     ip: "192.168.1.160"
     speed: 50
-    volume: 30
-    branch: 0
+    volume: 50
+    branch: 0
+    audio: "支路来车"

+ 10 - 3
lc/IDevice.go

@@ -41,6 +41,7 @@ type OutputDeviceInfo struct {
 	Ip     string
 	Port   string
 	Branch byte
+	Audio  string
 }
 
 type IntersectionDevice struct {
@@ -50,11 +51,17 @@ type IntersectionDevice struct {
 }
 
 func (id *IntersectionDevice) Call() {
-	id.Screen.Display(1)
-	id.Speaker.Speak("支路来车")
+	if id.Screen != nil {
+		id.Screen.Display(1)
+	}
+	if id.Speaker != nil {
+		id.Speaker.Speak("支路来车")
+	}
 }
 func (id *IntersectionDevice) Rollback() {
-	id.Screen.Display(0)
+	if id.Screen != nil {
+		id.Screen.Display(0)
+	}
 }
 
 func (id *IntersectionDevice) Reconnect() {

+ 2 - 3
lc/camera_event.go

@@ -15,9 +15,8 @@ import (
 	"time"
 )
 
-func StartCameraEventServer() *CameraServer {
+func NewCameraEventServer() *CameraServer {
 	server := &CameraServer{Cameras: util.Config.Cameras, Notifiers: make(map[string]Notifier, 4)}
-	server.start()
 	return server
 }
 
@@ -26,7 +25,7 @@ type CameraServer struct {
 	Notifiers map[string]Notifier
 }
 
-func (s *CameraServer) start() {
+func (s *CameraServer) Start() {
 	http.HandleFunc(util.Config.HikServer.Path, s.Handler)
 	logrus.Fatal("事件监听服务启动失败:", util.Config.HikServer.Addr, ", error:", http.ListenAndServe(util.Config.HikServer.Addr, nil))
 }

+ 17 - 16
lc/model/speaker.go

@@ -4,26 +4,27 @@ type SpeakerInfo struct {
 	Name   string `yaml:"name"`
 	Ip     string `yaml:"ip"`
 	Branch byte   `yaml:"branch"`
-	Speed  byte   `yaml:"branch"`
-	Volume byte   `yaml:"branch"`
+	Speed  byte   `yaml:"speed"`
+	Volume byte   `yaml:"volume"`
+	Audio  string `yaml:"audio"`
 }
 
 type PlayReq struct {
-	Url    string //要播放的网络音频 http/https/rtsp等⾳频地址
-	Text   string //要播放的文本内容
-	Vcn    string //发音人 xiaofeng xiaoyan
-	Speed  byte   //发音速度 0-100 默认50
-	Volume byte   //音量 0-100 默认50
-	Rdn    string //数字发音 0 数值优先, 1 完全数值, 2 完全字符串, 3 字符串优先 默认2
-	Rcn    string //数字1 的中文发音 0:表示发⾳为yao 1:表示发音为yi 默认0
-	Reg    byte   //英文发音 0:⾃动识别英语单词; 1:逐个字母发音
-	Sync   bool   //true: 同步模式,语音播放完毕后再响应; false:即时响应(不等待播放完成)
-	Queue  bool   //true: 队列模式,如果当前有语⾳在播放,则加到队列排队播放
-	Loop   LoopInfo
+	Url    string   `json:"url"`    //要播放的网络音频 http/https/rtsp等⾳频地址
+	Text   string   `json:"text"`   //要播放的文本内容
+	Vcn    string   `json:"vcn"`    //发音人 xiaofeng xiaoyan
+	Speed  byte     `json:"speed"`  //发音速度 0-100 默认50
+	Volume byte     `json:"volume"` //音量 0-100 默认50
+	Rdn    string   `json:"rdn"`    //数字发音 0 数值优先, 1 完全数值, 2 完全字符串, 3 字符串优先 默认2
+	Rcn    string   `json:"rcn"`    //数字1 的中文发音 0:表示发⾳为yao 1:表示发音为yi 默认0
+	Reg    byte     `json:"reg"`    //英文发音 0:⾃动识别英语单词; 1:逐个字母发音
+	Sync   bool     `json:"sync"`   //true: 同步模式,语音播放完毕后再响应; false:即时响应(不等待播放完成)
+	Queue  bool     `json:"queue"`  //true: 队列模式,如果当前有语⾳在播放,则加到队列排队播放
+	Loop   LoopInfo `json:"loop"`
 }
 
 type LoopInfo struct {
-	Duration int // 循环(重复)播放时⻓(秒)选填
-	Times    int // 循环(重复)播放次数(次)选填
-	Gap      int // 循环(重复)播放中的间歇时间(秒)
+	Duration int `json:"duration"` // 循环(重复)播放时⻓(秒)选填
+	Times    int `json:"times"`    // 循环(重复)播放次数(次)选填
+	Gap      int `json:"gap"`      // 循环(重复)播放中的间歇时间(秒)
 }

+ 2 - 3
lc/radio_event.go

@@ -7,9 +7,8 @@ import (
 	"lc-smartX/util"
 )
 
-func StartRadioEventServer() *RadioServer {
+func NewRadioEventServer() *RadioServer {
 	s := &RadioServer{Radios: util.Config.Radios, Notifiers: make(map[string]Notifier, 4)}
-	s.start()
 	return s
 }
 
@@ -18,7 +17,7 @@ type RadioServer struct {
 	Notifiers map[string]Notifier //485通道名
 }
 
-func (s *RadioServer) start() {
+func (s *RadioServer) Start() {
 	for _, radio := range s.Radios {
 		go s.OpenSerial(radio.Port)
 	}

+ 8 - 4
lc/server.go

@@ -1,6 +1,7 @@
 package lc
 
 import (
+	"github.com/sirupsen/logrus"
 	"lc-smartX/util"
 	"lc-smartX/util/gopool"
 	"time"
@@ -29,11 +30,12 @@ func StartIntersectionServer() {
 		ReTicker: time.NewTicker(30 * time.Second), //重连
 	}
 	if util.Config.Server.SupportCamera {
-		is.CameraEventServer = StartCameraEventServer()
-
+		is.CameraEventServer = NewCameraEventServer()
+		gopool.Go(is.CameraEventServer.Start)
 	}
 	if util.Config.Server.SupportRadio {
-		is.RadioEventServer = StartRadioEventServer()
+		is.RadioEventServer = NewRadioEventServer()
+		gopool.Go(is.RadioEventServer.Start)
 	}
 	//等事件服务先启动
 	time.Sleep(1 * time.Second)
@@ -48,6 +50,7 @@ func (m MainNotifier) Notify() {
 	if m.s.MainState != 1 {
 		m.s.MainState = 1
 		for _, v := range m.s.SubDevices {
+			logrus.Debugf("SubDevices v = %+v", v)
 			gopool.Go(v.Call)
 		}
 	}
@@ -99,8 +102,9 @@ func (is *IntersectionServer) Serve() {
 				Name:   c.Name,
 				Ip:     c.Ip,
 				Branch: c.Branch,
+				Audio:  c.Audio,
 			},
-			Speaker: NewIpCast(c.Name, c.Ip, c.Speed, c.Volume),
+			Speaker: NewIpCast(c.Name, c.Ip, c.Audio, c.Speed, c.Volume),
 		}
 		if c.Branch == 1 {
 			is.MainDevices = append(is.MainDevices, iDevice)

+ 11 - 4
lc/speaker.go

@@ -5,6 +5,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"github.com/sirupsen/logrus"
+	"io"
 	"lc-smartX/lc/model"
 	"net/http"
 )
@@ -19,15 +20,17 @@ type IpCast struct {
 	Ip        string
 	Speed     byte
 	Volume    byte
+	Audio     string
 	liveState bool
 }
 
-func NewIpCast(name, ip string, speed, volume byte) *IpCast {
+func NewIpCast(name, ip, audio string, speed, volume byte) *IpCast {
 	s := &IpCast{
 		Name:      name,
 		Ip:        ip,
 		Speed:     speed,
 		Volume:    volume,
+		Audio:     audio,
 		liveState: false,
 	}
 	s.Reconnect()
@@ -47,20 +50,24 @@ func (ip *IpCast) Speak(txt string) {
 		Queue:  false,
 	}
 	data.Loop.Times = 1
-
+	logrus.Debugf("IpCast Speak txt : %s", txt)
 	body, err := json.Marshal(data)
 	if err != nil {
 		logrus.Errorf("IpCast Marshal err : %s", err.Error())
 		return
 	}
 
-	c := http.DefaultClient
 	req, _ := http.NewRequest("POST", fmt.Sprintf("http://%s/v1/speech", ip.Ip), bytes.NewReader(body))
-	_, err = c.Do(req)
+	req.Header.Set("Content-Type", "application/json")
+	logrus.Debugf("IpCast Speak req : %+v", req)
+	rsp, err := http.DefaultClient.Do(req)
 	if err != nil {
 		logrus.Errorf("IpCast Speak Do err : %s", err.Error())
 		return
 	}
+	rspData, err := io.ReadAll(rsp.Body)
+	logrus.Debugf("IpCast Speak rsp : %+v", string(rspData))
+
 }
 
 func (ip *IpCast) Reconnect() {

+ 1 - 1
util/logrus.go

@@ -17,7 +17,7 @@ var _ = func() error {
 	fileName := path.Join("./log", "info")
 	writer, _ := rotatelogs.New(
 		fileName+".%Y%m%d.log",
-		rotatelogs.WithMaxAge(15*24*time.Hour),    // 文件最大保存时间
+		rotatelogs.WithMaxAge(5*24*time.Hour),     // 文件最大保存时间
 		rotatelogs.WithRotationTime(24*time.Hour), // 日志切割时间间隔
 	)
 	logrus.SetFormatter(&logrus.JSONFormatter{})