mqtt.go
5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package mqtt
import (
"fmt"
pahomqtt "github.com/eclipse/paho.mqtt.golang"
logimp "github.com/linmadan/egglib-go/log"
"github.com/linmadan/egglib-go/utils/json"
"github.com/tidwall/gjson"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/deviceCollection/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/deviceCollection/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/mqtt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
"time"
)
func Start(log logimp.Logger) {
mqtt.StartSubscribe(constant.MQTT_TOPIC, OnReceiveData, log)
}
func OnReceiveData(client pahomqtt.Client, message pahomqtt.Message) {
payload := make(map[string]interface{})
var collectionTime time.Time
err := json.Unmarshal(message.Payload(), &payload)
if err != nil {
log.Logger.Error("车间数据json解析失败:" + err.Error())
return
}
if workShop, ok := payload["WorkShop"]; ok {
if t, ok := payload["UpTime"]; ok {
collectionTime, _ = time.ParseInLocation("2006-01-02 - 15:04:05", t.(string), time.Local)
}
if collectionTime.IsZero() {
log.Logger.Error(fmt.Sprintf("采集时间有误:%v ", collectionTime), map[string]interface{}{"data": message})
return
}
var mBytes []byte
for key, item := range payload {
if key == "WorkShop" || key == "UpTime" {
continue
}
mBytes, err = json.Marshal(item)
if err != nil {
continue
}
deviceCollection := &domain.DeviceCollection{
WorkShopName: workShop.(string),
DeviceSn: key,
CollectionTime: collectionTime,
StartupStatus: gjson.Get(string(message.Payload()), key+".StartupState").Int(),
ComStatus: gjson.Get(string(message.Payload()), key+".ComStatus").Int(),
}
deviceType4 := utils.SubStr(key, 0, 4)
deviceType := utils.SubStr(key, 0, 3)
deviceCollection.DeviceType = deviceType
if deviceType4 == domain.DeviceTypeMianBaoXieJi {
deviceCollection.DeviceType = domain.DeviceTypeMianBaoXieJi
}
if deviceType == domain.DeviceTypeYouZhaJi {
deviceType = deviceType4
}
switch deviceType {
//包馅机
case domain.DeviceTypeBaoXianJi:
deviceBaoXianJi := &domain.DeviceBaoXianJi{}
err = json.Unmarshal(mBytes, deviceBaoXianJi)
if err != nil {
continue
}
deviceCollection.Values = utils.ToMap(deviceBaoXianJi)
break
//油炸机
case domain.DeviceTypeYouZhaJi1:
deviceYouZhaJi := &domain.DeviceYouZhaJi{}
err = json.Unmarshal(mBytes, deviceYouZhaJi)
if err != nil {
continue
}
deviceCollection.Values = utils.ToMap(deviceYouZhaJi)
break
//油炸机
case domain.DeviceTypeYouZhaJi2:
deviceYouZhaJi2 := &domain.DeviceYouZhaJi2{}
err = json.Unmarshal(mBytes, deviceYouZhaJi2)
if err != nil {
continue
}
deviceCollection.Values = utils.ToMap(deviceYouZhaJi2)
break
//串串机
case domain.DeviceTypeChuanChuanJi:
deviceChuanChuanJi := &domain.DeviceChuanChuanJi{}
err = json.Unmarshal(mBytes, deviceChuanChuanJi)
if err != nil {
log.Logger.Error(err.Error())
continue
}
deviceCollection.Values = utils.ToMap(deviceChuanChuanJi)
break
//速冻线
case domain.DeviceTypeSuDongXian:
deviceSuDongXian := &domain.DeviceSuDongXian{}
err = json.Unmarshal(mBytes, deviceSuDongXian)
if err != nil {
continue
}
deviceCollection.Values = utils.ToMap(deviceSuDongXian)
break
//封口机
case domain.DeviceTypeFengKouJi:
deviceFengKouJi := &domain.DeviceFengKouJi{}
err = json.Unmarshal(mBytes, deviceFengKouJi)
if err != nil {
continue
}
deviceCollection.Values = utils.ToMap(deviceFengKouJi)
break
//封箱机
case domain.DeviceTypeFengXiangJi:
deviceFengXiangJi := &domain.DeviceFengXiangJi{}
err = json.Unmarshal(mBytes, deviceFengXiangJi)
if err != nil {
continue
}
deviceCollection.Values = utils.ToMap(deviceFengXiangJi)
break
//打浆机 //面包屑机
case domain.DeviceTypeDaJiangJi:
break
default:
}
//if deviceType != domain.DeviceTypeChuanChuanJi {
// continue
//}
// 发送数据
deviceCollectionService := service.NewDeviceCollectionService(nil)
_, err = deviceCollectionService.DeviceCollection(&command.CreateDeviceCollectionCommand{
WorkShopName: deviceCollection.WorkShopName,
StartupStatus: deviceCollection.StartupStatus,
CollectionTime: deviceCollection.CollectionTime,
DeviceSn: deviceCollection.DeviceSn,
DeviceType: deviceCollection.DeviceType,
ComStatus: deviceCollection.ComStatus,
Values: deviceCollection.Values,
PreCheck: true,
})
if err != nil {
continue
}
}
//TODO:日志先注释
if err == nil {
log.Logger.Info(fmt.Sprintf("MQTT:%v", collectionTime), map[string]interface{}{
"Topic": message.Topic(),
"MessageId": message.MessageID(),
"Message": payload,
})
}
}
}