terry 7 bulan lalu
induk
melakukan
9e0f5525ad
5 mengubah file dengan 52 tambahan dan 59 penghapusan
  1. 1 1
      lc/IDevice.go
  2. 16 9
      lc/camera_event.go
  3. 3 3
      lc/radar_event.go
  4. 12 3
      lc/screen.go
  5. 20 43
      lc/server.go

+ 1 - 1
lc/IDevice.go

@@ -1,7 +1,7 @@
 package lc
 
 type Notifier interface {
-	Notify()
+	Notify(id string)
 }
 
 // CorrectTimer 校时器接口

+ 16 - 9
lc/camera_event.go

@@ -32,21 +32,18 @@ func (s *CameraServer) Start() {
 
 func (s *CameraServer) RegisterCallback(branch byte, notifier Notifier) {
 	for _, camera := range s.Cameras {
-		//关联主路led屏和支路摄像头;关联支路led屏和主路摄像头
-		if branch == 0 && camera.Branch == 1 || branch == 1 && camera.Branch == 0 {
-			s.Notifiers[camera.IP] = notifier
-		}
+		s.Notifiers[camera.IP] = notifier
 	}
 }
 
-func (s *CameraServer) Callback(ip string) {
+func (s *CameraServer) Callback(ip, id string) {
 	notifier, ok := s.Notifiers[ip]
 	if !ok {
 		logrus.Errorf("回调函数注册表没有该ip:%s", ip)
 		return
 	}
-	notifier.Notify()
-	logrus.Debugf("camera [%s] Callback", ip)
+	notifier.Notify(id)
+	logrus.Debugf("camera [%s] [%s] Callback", ip, id)
 }
 
 func (s *CameraServer) Handler(w http.ResponseWriter, r *http.Request) {
@@ -69,7 +66,12 @@ func (s *CameraServer) Handler(w http.ResponseWriter, r *http.Request) {
 			return
 		}
 		//发送事件通知
-		s.Callback(event.IpAddress)
+		//s.Callback(event.IpAddress)
+		logrus.Errorf("Handler event =  %+v", event)
+		for _, v := range event.DetectionRegionList.DetectionRegionEntry {
+			logrus.Errorf("Handler id =  %s", v.RegionID)
+			s.Callback(event.IpAddress, v.RegionID)
+		}
 	} else if strings.Contains(contentType, "multipart/form-data") {
 		s.HandleMultipart(r)
 	}
@@ -101,5 +103,10 @@ func (s *CameraServer) HandleMultipart(r *http.Request) {
 		}
 	}
 	//发送事件通知
-	s.Callback(event.IpAddress)
+	//s.Callback(event.IpAddress, "")
+	logrus.Errorf("Handler event =  %+v", event)
+	for _, v := range event.DetectionRegionList.DetectionRegionEntry {
+		logrus.Errorf("Handler id =  %s", v.RegionID)
+		s.Callback(event.IpAddress, v.RegionID)
+	}
 }

+ 3 - 3
lc/radar_event.go

@@ -32,13 +32,13 @@ func (s *RadarServer) RegisterCallback(branch byte, notifier Notifier) {
 	}
 }
 
-func (s *RadarServer) Callback(port string) {
+func (s *RadarServer) Callback(port, id string) {
 	notifier, ok := s.Notifiers[port]
 	if !ok {
 		logrus.Errorf("回调函数注册表没有该ip:%s", port)
 		return
 	}
-	notifier.Notify()
+	notifier.Notify(id)
 }
 
 func (s *RadarServer) OpenSerial(portName string) {
@@ -74,7 +74,7 @@ func (s *RadarServer) OpenSerial(portName string) {
 			result = true
 		}
 		if result {
-			s.Callback(portName)
+			s.Callback(portName, "")
 		}
 	}
 }

+ 12 - 3
lc/screen.go

@@ -17,6 +17,7 @@ type Screener interface {
 
 type Screen struct {
 	Name      string
+	Branch    byte
 	Addr      string
 	conn      net.Conn
 	liveState bool
@@ -24,12 +25,13 @@ type Screen struct {
 	Params    *bx.Params    //屏参
 }
 
-func NewScreen(name string, ip, port string) *Screen {
+func NewScreen(name string, ip, port string, branch byte) *Screen {
 	s := &Screen{
 		Name:      name,
 		Addr:      fmt.Sprintf("%s:%s", ip, port),
 		StateInfo: &bx.StateInfo{},
 		Params:    &bx.Params{},
+		Branch:    branch,
 	}
 	s.Reconnect()
 	return s
@@ -116,6 +118,13 @@ const (
 
 // 发送动态区节目 0正常页面 1红色提醒页面
 func (s *Screen) SendRam(id int) {
+	logrus.Errorf("SendRam id =  %d", id)
+	msg := "支路来车"
+	audio := "支路来车,请减速"
+	if s.Branch == 1 {
+		msg = "主路来车"
+		audio = "主路来车,请减速"
+	}
 	file := FlashFile{}
 	if id == 0 {
 		file.SetMsg("减速慢行", Yellow)
@@ -124,8 +133,8 @@ func (s *Screen) SendRam(id int) {
 		file.SetArea(64, true, 16)
 		s.TextRam(file, false)
 	} else {
-		file.SetMsg("支路来车", Red)
-		file.SetSoundData("支路来车,请减速")
+		file.SetMsg(msg, Red)
+		file.SetSoundData(audio)
 		file.SetMode(DefaultRunMode, DefaultDisplayMode)
 		file.SetOrigin(0, true, 0)
 		file.SetArea(64, true, 16)

+ 20 - 43
lc/server.go

@@ -1,6 +1,7 @@
 package lc
 
 import (
+	"github.com/sirupsen/logrus"
 	"lc-smartX/util"
 	"lc-smartX/util/gopool"
 	"time"
@@ -13,8 +14,7 @@ type SmartXServer interface {
 type IntersectionServer struct {
 	RadarEventServer  *RadarServer
 	CameraEventServer *CameraServer
-	MainState         byte
-	SubState          byte
+	State             byte
 	MainDevices       []IDevice
 	SubDevices        []IDevice
 	ReTicker          *time.Ticker
@@ -25,7 +25,6 @@ type IntersectionServer struct {
 func StartIntersectionServer() {
 	is := &IntersectionServer{
 		Main:     time.NewTicker(5 * time.Second),  //主路状态回滚
-		Sub:      time.NewTicker(5 * time.Second),  //支路状态回滚
 		ReTicker: time.NewTicker(30 * time.Second), //重连
 	}
 	if util.Config.Server.SupportCamera {
@@ -44,38 +43,29 @@ func StartIntersectionServer() {
 type MainNotifier struct{ s *IntersectionServer }
 
 // Notify 主路来车,通知支路设备
-func (m MainNotifier) Notify() {
+func (m MainNotifier) Notify(id string) {
+	logrus.Errorf("Noitfy m.s.State = %d", m.s.State)
+	logrus.Errorf("Noitfy dev = %v", m.s.SubDevices)
+	logrus.Errorf("Noitfy dev = %v", m.s.MainDevices)
 	m.s.Main.Reset(5 * time.Second)
-	if m.s.MainState != 1 {
-		m.s.MainState = 1
-		for _, v := range m.s.SubDevices {
-			gopool.Go(v.Call)
-		}
-	}
-}
-
-type SubNotifier struct{ s *IntersectionServer }
-
-// Notify 支路来车,通知主路设备
-func (sub SubNotifier) Notify() {
-	sub.s.Sub.Reset(5 * time.Second)
-	if sub.s.SubState != 1 {
-		sub.s.SubState = 1
-		for _, v := range sub.s.MainDevices {
-			gopool.Go(v.Call)
+	if m.s.State != 1 {
+		m.s.State = 1
+		if id == "1" || id == "2" {
+			for _, v := range m.s.SubDevices {
+				gopool.Go(v.Call)
+			}
+		} else if id == "3" || id == "4" {
+			for _, v := range m.s.MainDevices {
+				gopool.Go(v.Call)
+			}
 		}
 	}
 }
 
 func (is *IntersectionServer) Serve() {
 	if util.Config.Server.SupportCamera {
-		is.CameraEventServer.RegisterCallback(1, &SubNotifier{is})
 		is.CameraEventServer.RegisterCallback(0, &MainNotifier{is})
 	}
-	if util.Config.Server.SupportRadar {
-		is.RadarEventServer.RegisterCallback(1, &SubNotifier{is})
-		is.RadarEventServer.RegisterCallback(0, &MainNotifier{is})
-	}
 
 	//先创建响应设备
 	for _, c := range util.Config.Screens {
@@ -86,7 +76,7 @@ func (is *IntersectionServer) Serve() {
 				Port:   c.Port,
 				Branch: c.Branch,
 			},
-			Screen: NewScreen(c.Name, c.Ip, c.Port),
+			Screen: NewScreen(c.Name, c.Ip, c.Port, c.Branch),
 		}
 		if c.Branch == 1 {
 			is.MainDevices = append(is.MainDevices, iDevice)
@@ -115,29 +105,16 @@ func (is *IntersectionServer) Serve() {
 		select {
 		case <-is.Main.C: //检查主路状态->支路输出设备回到初始状态
 			for _, v := range is.SubDevices {
-				if is.MainState == 1 {
+				if is.State == 1 {
 					gopool.Go(v.Rollback)
 				}
 			}
-			is.MainState = 0
-		case <-is.Sub.C: //检查支路状态->主路输出设备作出响应
 			for _, v := range is.MainDevices {
-				if is.SubState == 1 {
+				if is.State == 1 {
 					gopool.Go(v.Rollback)
 				}
 			}
-			is.SubState = 0
-		case <-is.ReTicker.C: //每19s检查并尝试重连
-			gopool.Go(func() {
-				for _, v := range is.MainDevices {
-					gopool.Go(v.Reconnect)
-				}
-			})
-			gopool.Go(func() {
-				for _, v := range is.SubDevices {
-					gopool.Go(v.Reconnect)
-				}
-			})
+			is.State = 0
 		}
 	}
 }