|
|
@@ -7,36 +7,49 @@ import (
|
|
|
"server/global"
|
|
|
"server/model/devices"
|
|
|
"server/utils/protocol"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type ProgramService struct {
|
|
|
}
|
|
|
|
|
|
func (ps *ProgramService) CreateProgram(ref devices.ProgramReq) error {
|
|
|
- result := devices.DiscontentJSON{
|
|
|
- Setdiscontent0: devices.SetDisContent{
|
|
|
- Num: ref.Program.Num,
|
|
|
- Effect: ref.Program.Effect,
|
|
|
- Speed: ref.Program.Speed,
|
|
|
- Stay: ref.Program.Stay,
|
|
|
- Total: ref.Program.Total,
|
|
|
- Color: ref.Program.Color,
|
|
|
- Content: ref.Program.Content,
|
|
|
- },
|
|
|
+ contentList := []devices.SetDisContent{
|
|
|
+ {ref.Program.Num1, ref.Program.Effect1, ref.Program.Speed1, ref.Program.Stay1, ref.Program.Total1, ref.Program.Color1, ref.Program.Content1},
|
|
|
+ {ref.Program.Num2, ref.Program.Effect2, ref.Program.Speed2, ref.Program.Stay2, ref.Program.Total2, ref.Program.Color2, ref.Program.Content2},
|
|
|
+ {ref.Program.Num3, ref.Program.Effect3, ref.Program.Speed3, ref.Program.Stay3, ref.Program.Total3, ref.Program.Color3, ref.Program.Content3},
|
|
|
+ {ref.Program.Num4, ref.Program.Effect4, ref.Program.Speed4, ref.Program.Stay4, ref.Program.Total4, ref.Program.Color4, ref.Program.Content4},
|
|
|
+ {ref.Program.Num5, ref.Program.Effect5, ref.Program.Speed5, ref.Program.Stay5, ref.Program.Total5, ref.Program.Color5, ref.Program.Content5},
|
|
|
}
|
|
|
|
|
|
- // 序列化为 JSON 字符串
|
|
|
- jsonBytes, err := json.MarshalIndent(result, "", " ")
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("转换失败: %v", err)
|
|
|
- }
|
|
|
+ for _, p := range contentList {
|
|
|
+ // 组装成每个对象
|
|
|
+ result := map[string]map[string]string{
|
|
|
+ "setdiscontent0": {
|
|
|
+ "num": p.Num,
|
|
|
+ "effect": p.Effect,
|
|
|
+ "speed": p.Speed,
|
|
|
+ "stay": p.Stay,
|
|
|
+ "total": p.Total,
|
|
|
+ "color": p.Color,
|
|
|
+ "content": p.Content,
|
|
|
+ },
|
|
|
+ }
|
|
|
|
|
|
- err = MqttService.Publish(MqttService.GetTopic(ref.DeviceSn, protocol.TopicSetControl), jsonBytes)
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("error updating: %v", err)
|
|
|
+ // 序列化为 JSON 字符串
|
|
|
+ jsonBytes, err := json.MarshalIndent(result, "", " ")
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("转换失败: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = MqttService.Publish(MqttService.GetTopic(ref.DeviceSn, protocol.TopicSetControl), jsonBytes)
|
|
|
+ time.Sleep(300 * time.Millisecond)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("error updating: %v", err)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- err = global.GVA_DB.Create(&ref.Program).Error
|
|
|
+ err := global.GVA_DB.Create(&ref.Program).Error
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
@@ -49,27 +62,39 @@ func (ps *ProgramService) CreateProgram(ref devices.ProgramReq) error {
|
|
|
}
|
|
|
|
|
|
func (ps *ProgramService) UpdateProgram(ref devices.ProgramReq) error {
|
|
|
- result := devices.DiscontentJSON{
|
|
|
- Setdiscontent0: devices.SetDisContent{
|
|
|
- Num: ref.Program.Num,
|
|
|
- Effect: ref.Program.Effect,
|
|
|
- Speed: ref.Program.Speed,
|
|
|
- Stay: ref.Program.Stay,
|
|
|
- Total: ref.Program.Total,
|
|
|
- Color: ref.Program.Color,
|
|
|
- Content: ref.Program.Content,
|
|
|
- },
|
|
|
+ contentList := []devices.SetDisContent{
|
|
|
+ {ref.Program.Num1, ref.Program.Effect1, ref.Program.Speed1, ref.Program.Stay1, ref.Program.Total1, ref.Program.Color1, ref.Program.Content1},
|
|
|
+ {ref.Program.Num2, ref.Program.Effect2, ref.Program.Speed2, ref.Program.Stay2, ref.Program.Total2, ref.Program.Color2, ref.Program.Content2},
|
|
|
+ {ref.Program.Num3, ref.Program.Effect3, ref.Program.Speed3, ref.Program.Stay3, ref.Program.Total3, ref.Program.Color3, ref.Program.Content3},
|
|
|
+ {ref.Program.Num4, ref.Program.Effect4, ref.Program.Speed4, ref.Program.Stay4, ref.Program.Total4, ref.Program.Color4, ref.Program.Content4},
|
|
|
+ {ref.Program.Num5, ref.Program.Effect5, ref.Program.Speed5, ref.Program.Stay5, ref.Program.Total5, ref.Program.Color5, ref.Program.Content5},
|
|
|
}
|
|
|
|
|
|
- // 序列化为 JSON 字符串
|
|
|
- jsonBytes, err := json.MarshalIndent(result, "", " ")
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("转换失败: %v", err)
|
|
|
- }
|
|
|
+ for _, p := range contentList {
|
|
|
+ // 组装成每个对象
|
|
|
+ result := map[string]map[string]string{
|
|
|
+ "setdiscontent0": {
|
|
|
+ "num": p.Num,
|
|
|
+ "effect": p.Effect,
|
|
|
+ "speed": p.Speed,
|
|
|
+ "stay": p.Stay,
|
|
|
+ "total": p.Total,
|
|
|
+ "color": p.Color,
|
|
|
+ "content": p.Content,
|
|
|
+ },
|
|
|
+ }
|
|
|
|
|
|
- err = MqttService.Publish(MqttService.GetTopic(ref.DeviceSn, protocol.TopicSetControl), jsonBytes)
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("error updating: %v", err)
|
|
|
+ // 序列化为 JSON 字符串
|
|
|
+ jsonBytes, err := json.MarshalIndent(result, "", " ")
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("转换失败: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = MqttService.Publish(MqttService.GetTopic(ref.DeviceSn, protocol.TopicSetControl), jsonBytes)
|
|
|
+ time.Sleep(300 * time.Millisecond)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("error updating: %v", err)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return ref.Program.UpdateProgram()
|