soundPeriod.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package devices
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "server/dao"
  6. "server/global"
  7. "server/model/devices"
  8. "server/utils/protocol"
  9. )
  10. type SoundPeriodService struct {
  11. }
  12. func (sps *SoundPeriodService) CreateSoundPeriod(req devices.SoundPeriodReq) error {
  13. result := devices.PeriodtimeJSON{
  14. Setperiodtime0: devices.SetPeropdtime{
  15. Time: req.SoundPeriod.Time,
  16. Period0: req.SoundPeriod.Period0,
  17. Period1: req.SoundPeriod.Period1,
  18. Period2: req.SoundPeriod.Period2,
  19. Period3: req.SoundPeriod.Period3,
  20. Period4: req.SoundPeriod.Period4,
  21. Period5: req.SoundPeriod.Period5,
  22. Period6: req.SoundPeriod.Period6,
  23. Period7: req.SoundPeriod.Period7,
  24. },
  25. }
  26. // 序列化为 JSON 字符串
  27. jsonBytes, err := json.MarshalIndent(result, "", " ")
  28. if err != nil {
  29. return fmt.Errorf("转换失败: %v", err)
  30. }
  31. err = MqttService.Publish(MqttService.GetTopic(req.DeviceSn, protocol.TopicSetControl), jsonBytes)
  32. if err != nil {
  33. return fmt.Errorf("error updating: %v", err)
  34. }
  35. err = global.GVA_DB.Create(&req.SoundPeriod).Error
  36. if err != nil {
  37. return err
  38. }
  39. fmt.Println("SoundPeriod CreateSoundPeriod === ", req.SoundPeriod)
  40. err = global.GVA_DB.Model(dao.Screens{}).Where("sn = ?", req.DeviceSn).Update("sound_period_id", req.SoundPeriod.ID).Error
  41. if err != nil {
  42. return err
  43. }
  44. return err
  45. }
  46. func (sps *SoundPeriodService) UpdateSoundPeriod(req devices.SoundPeriodReq) error {
  47. result := devices.PeriodtimeJSON{
  48. Setperiodtime0: devices.SetPeropdtime{
  49. Time: req.SoundPeriod.Time,
  50. Period0: req.SoundPeriod.Period0,
  51. Period1: req.SoundPeriod.Period1,
  52. Period2: req.SoundPeriod.Period2,
  53. Period3: req.SoundPeriod.Period3,
  54. Period4: req.SoundPeriod.Period4,
  55. Period5: req.SoundPeriod.Period5,
  56. Period6: req.SoundPeriod.Period6,
  57. Period7: req.SoundPeriod.Period7,
  58. },
  59. }
  60. // 序列化为 JSON 字符串
  61. jsonBytes, err := json.MarshalIndent(result, "", " ")
  62. if err != nil {
  63. return fmt.Errorf("转换失败: %v", err)
  64. }
  65. err = MqttService.Publish(MqttService.GetTopic(req.DeviceSn, protocol.TopicSetControl), jsonBytes)
  66. if err != nil {
  67. return fmt.Errorf("error updating: %v", err)
  68. }
  69. err = global.GVA_DB.Where("id = ?", req.SoundPeriod.ID).Updates(&req.SoundPeriod).Error
  70. if err != nil {
  71. return err
  72. }
  73. return err
  74. }