1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package lc
- import (
- "github.com/jacobsa/go-serial/serial"
- "github.com/sirupsen/logrus"
- "lc-smartX/lc/model"
- "lc-smartX/util"
- )
- func NewRadarEventServer() *RadarServer {
- s := &RadarServer{Radars: util.Config.Radars, Notifiers: make(map[string]Notifier, 4)}
- return s
- }
- type RadarServer struct {
- Radars []model.RadarInfo
- Notifiers map[string]Notifier //485通道名
- }
- func (s *RadarServer) Start() {
- for _, radar := range s.Radars {
- go s.OpenSerial(radar.Port)
- }
- }
- func (s *RadarServer) RegisterCallback(branch byte, notifier Notifier) {
- for _, radar := range s.Radars {
- //关联主路led屏和支路雷达;关联支路led屏和主路雷达
- if branch == 0 && radar.Branch == 1 || branch == 1 && radar.Branch == 0 {
- s.Notifiers[radar.Port] = notifier
- }
- }
- }
- func (s *RadarServer) Callback(port, id string) {
- notifier, ok := s.Notifiers[port]
- if !ok {
- logrus.Errorf("回调函数注册表没有该ip:%s", port)
- return
- }
- notifier.Notify(id)
- }
- func (s *RadarServer) OpenSerial(portName string) {
- // 配置串口参数
- options := serial.OpenOptions{
- PortName: portName, // /dev/ttymxc4 6 3
- BaudRate: 9600,
- DataBits: 8,
- StopBits: 1,
- MinimumReadSize: 4,
- }
- // 打开串口
- port, err := serial.Open(options)
- if err != nil {
- panic(err.Error())
- }
- // 关闭串口
- defer port.Close()
- for {
- // 读取数据
- buf := make([]byte, 128)
- n, err := port.Read(buf)
- if err != nil {
- break
- }
- if n < 8 {
- continue
- }
- result := false
- if buf[0] == 'x' && (buf[1] > '0' || buf[2] > '0' || buf[3] > '0') {
- result = true
- }
- if result {
- s.Callback(portName, "")
- }
- }
- }
|