device.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package admin
  2. import (
  3. "fmt"
  4. "server/dao"
  5. "server/model/common/request"
  6. "server/model/common/response"
  7. "server/utils/protocol"
  8. "strconv"
  9. )
  10. type DeviceService struct{}
  11. func (ds *DeviceService) QueryAllDevices() ([]dao.Device, error) {
  12. return dao.QueryAllDevices()
  13. }
  14. func (ds *DeviceService) QueryDeviceList(info request.DeviceSearch) ([]dao.Device, int64, error) {
  15. limit := info.PageInfo.PageSize
  16. offset := info.PageInfo.PageSize * (info.PageInfo.Page - 1)
  17. return dao.QueryDeviceList(info.Sn, info.Name, info.Genre, limit, offset)
  18. }
  19. func (ds *DeviceService) GenerateDeviceFile(id int) (data response.DeviceFileData, err error) {
  20. devices, err := dao.QueryDeviceByTunnelId(id)
  21. if err != nil {
  22. return data, err
  23. }
  24. for _, d := range devices {
  25. if d.DeviceGenre.Type == "环境设备" {
  26. envDev := response.EnvDev{
  27. Sn: d.Sn,
  28. Name: d.Name,
  29. Address: d.Address,
  30. TaskTime: d.TaskTime,
  31. WaitTime: d.WaitTime,
  32. SerialId: d.SerialId,
  33. }
  34. data.EnvDev = append(data.EnvDev, envDev)
  35. } else if d.DeviceGenre.Type == "开关设备" {
  36. lampDev := response.LampDev{
  37. Sn: d.Sn,
  38. Name: d.Name,
  39. Address: d.Address,
  40. RadarId: d.RadarId,
  41. SerialId: d.SerialId,
  42. }
  43. data.LampDev = append(data.LampDev, lampDev)
  44. }
  45. }
  46. return data, nil
  47. }
  48. func (ds *DeviceService) DeviceSwitch(info request.SwitchTunnel) error {
  49. device, err := dao.QueryDeviceByTunnelSnAndRadarId(info.TunnelSn, info.RadarId)
  50. if err != nil {
  51. return err
  52. }
  53. err = dao.UpdateRelayState(int(device.ID), info.RelayId, info.State)
  54. if err != nil {
  55. return err
  56. }
  57. var i int
  58. if info.State {
  59. i = 1
  60. } else {
  61. i = 0
  62. }
  63. data := strconv.Itoa(info.RadarId) + strconv.Itoa(info.RelayId) + strconv.Itoa(i)
  64. err = MqttService.Publish(MqttService.GetTopic(info.TunnelSn, protocol.TopicSwitchControl), []byte(data))
  65. if err != nil {
  66. return fmt.Errorf("error updating: %v", err)
  67. }
  68. return nil
  69. }
  70. func (ds *DeviceService) CreateDevice(device dao.Device) error {
  71. tunnel, err := dao.QueryTunnelById(device.TunnelId)
  72. if err != nil {
  73. return err
  74. }
  75. genre, err := dao.QueryDeviceGenreByID(device.Genre)
  76. if err != nil {
  77. return err
  78. }
  79. if genre.Name == tunnel.SwitchType && genre.Type == "开关设备" {
  80. return fmt.Errorf("开关设备需安装隧道需求添加")
  81. }
  82. return device.CreateDevice()
  83. }
  84. func (ds *DeviceService) UpdateDevice(device dao.Device) error {
  85. return device.UpdateDevice()
  86. }
  87. func (ds *DeviceService) DeleteDevice(id int) error {
  88. return dao.DeleteDevice(id)
  89. }