IDevice.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package lc
  2. import (
  3. "github.com/sirupsen/logrus"
  4. "lc-smartX/util"
  5. "net"
  6. "time"
  7. )
  8. type CorrectTimer interface {
  9. Correct()
  10. }
  11. func DoCorrectTime(a any) {
  12. if c, ok := a.(CorrectTimer); ok {
  13. c.Correct()
  14. }
  15. }
  16. // IDevice 抽象设备,包含屏和喇叭
  17. type IDevice interface {
  18. // Call 通知输出设备,输出信息。屏显示来车警告信息,扬声器语音提示
  19. Call()
  20. // Back 屏显示原来的无车提醒信息
  21. Back()
  22. ReConnect()
  23. // Correct time 校时
  24. CorrectTimer
  25. }
  26. type IntersectionDevice struct {
  27. Info util.OutputDevice
  28. S Screen
  29. L IpCast
  30. }
  31. func (id *IntersectionDevice) Call() {
  32. id.L.Speak()
  33. id.S.Display()
  34. }
  35. func (id *IntersectionDevice) Back() {
  36. id.S.Back()
  37. }
  38. func (id *IntersectionDevice) ReConnect() {
  39. //重连屏
  40. if !id.S.IsLive {
  41. conn, err := net.DialTimeout("tcp", id.Info.ScreenIp+":5000", 5*time.Second)
  42. if err != nil {
  43. logrus.WithFields(map[string]interface{}{"设备名": id.Info.Name, "IP": id.Info.ScreenIp, "error": err}).Error("屏重连接失败!")
  44. return
  45. }
  46. logrus.WithFields(map[string]interface{}{"设备名": id.Info.Name, "IP": id.Info.ScreenIp}).Info("屏重连接成功!")
  47. id.S.Conn = conn
  48. id.S.IsLive = true
  49. }
  50. //todo 重连loudspeaker
  51. }
  52. func (id *IntersectionDevice) Correct() {
  53. DoCorrectTime(id.S)
  54. DoCorrectTime(id.L)
  55. }