| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package devices
- import (
- "encoding/json"
- "fmt"
- "server/dao"
- "server/global"
- "server/model/devices"
- "server/utils/protocol"
- )
- type SoundPeriodService struct {
- }
- func (sps *SoundPeriodService) CreateSoundPeriod(req devices.SoundPeriodReq) error {
- result := devices.PeriodtimeJSON{
- Setperiodtime0: devices.SetPeropdtime{
- Time: req.SoundPeriod.Time,
- Period0: req.SoundPeriod.Period0,
- Period1: req.SoundPeriod.Period1,
- Period2: req.SoundPeriod.Period2,
- Period3: req.SoundPeriod.Period3,
- Period4: req.SoundPeriod.Period4,
- Period5: req.SoundPeriod.Period5,
- Period6: req.SoundPeriod.Period6,
- Period7: req.SoundPeriod.Period7,
- },
- }
- // 序列化为 JSON 字符串
- jsonBytes, err := json.MarshalIndent(result, "", " ")
- if err != nil {
- return fmt.Errorf("转换失败: %v", err)
- }
- err = MqttService.Publish(MqttService.GetTopic(req.DeviceSn, protocol.TopicChanStatus), jsonBytes)
- if err != nil {
- return fmt.Errorf("error updating: %v", err)
- }
- err = global.GVA_DB.Create(&req.SoundPeriod).Error
- if err != nil {
- return err
- }
- fmt.Println("SoundPeriod CreateSoundPeriod === ", req.SoundPeriod)
- err = global.GVA_DB.Model(dao.Screens{}).Where("sn = ?", req.DeviceSn).Update("sound_period_id", req.SoundPeriod.ID).Error
- if err != nil {
- return err
- }
- return err
- }
- func (sps *SoundPeriodService) UpdateSoundPeriod(req devices.SoundPeriodReq) error {
- result := devices.PeriodtimeJSON{
- Setperiodtime0: devices.SetPeropdtime{
- Time: req.SoundPeriod.Time,
- Period0: req.SoundPeriod.Period0,
- Period1: req.SoundPeriod.Period1,
- Period2: req.SoundPeriod.Period2,
- Period3: req.SoundPeriod.Period3,
- Period4: req.SoundPeriod.Period4,
- Period5: req.SoundPeriod.Period5,
- Period6: req.SoundPeriod.Period6,
- Period7: req.SoundPeriod.Period7,
- },
- }
- // 序列化为 JSON 字符串
- jsonBytes, err := json.MarshalIndent(result, "", " ")
- if err != nil {
- return fmt.Errorf("转换失败: %v", err)
- }
- err = MqttService.Publish(MqttService.GetTopic(req.DeviceSn, protocol.TopicChanStatus), jsonBytes)
- if err != nil {
- return fmt.Errorf("error updating: %v", err)
- }
- err = global.GVA_DB.Where("id = ?", req.SoundPeriod.ID).Updates(&req.SoundPeriod).Error
- if err != nil {
- return err
- }
- return err
- }
|