...
|
...
|
@@ -2,7 +2,6 @@ package mqtt |
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
pahomqtt "github.com/eclipse/paho.mqtt.golang"
|
|
|
"github.com/linmadan/egglib-go/utils/tool_funs"
|
|
|
"github.com/tidwall/gjson"
|
...
|
...
|
@@ -11,157 +10,137 @@ import ( |
|
|
"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/redis"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
func Start() {
|
|
|
mqtt.StartSubscribe(constant.MQTT_TOPIC, func(client pahomqtt.Client, message pahomqtt.Message) {
|
|
|
payload := make(map[string]interface{})
|
|
|
err := json.Unmarshal(message.Payload(), &payload)
|
|
|
if err != nil {
|
|
|
log.Logger.Error("车间数据json解析失败:" + err.Error())
|
|
|
return
|
|
|
}
|
|
|
if workShop, ok := payload["WorkShop"]; ok {
|
|
|
for key, item := range payload {
|
|
|
if key == "WorkShop" {
|
|
|
continue
|
|
|
}
|
|
|
mBytes, err := json.Marshal(item)
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
deviceCollection := &domain.DeviceCollection{
|
|
|
WorkShopName: workShop.(string),
|
|
|
DeviceSn: key,
|
|
|
CollectionTime: time.Now(),
|
|
|
StartupStatus: gjson.Get(string(message.Payload()), key+".StartupState").Int(),
|
|
|
ComStatus: gjson.Get(string(message.Payload()), key+".ComStatus").Int(),
|
|
|
}
|
|
|
if utils.SubStr(key, 0, 4) == domain.DeviceTypeMianBaoXieJi {
|
|
|
deviceCollection.DeviceType = domain.DeviceTypeMianBaoXieJi
|
|
|
} else {
|
|
|
deviceType := utils.SubStr(key, 0, 3)
|
|
|
deviceCollection.DeviceType = deviceType
|
|
|
switch deviceType {
|
|
|
//包馅机
|
|
|
case domain.DeviceTypeBaoXianJi:
|
|
|
deviceBaoXianJi := &domain.DeviceBaoXianJi{}
|
|
|
err = json.Unmarshal(mBytes, deviceBaoXianJi)
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
deviceCollection.Values = tool_funs.SimpleStructToMap(deviceBaoXianJi)
|
|
|
break
|
|
|
//油炸机
|
|
|
case domain.DeviceTypeYouZhaJi1:
|
|
|
deviceYouZhaJi := &domain.DeviceYouZhaJi{}
|
|
|
err = json.Unmarshal(mBytes, deviceYouZhaJi)
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
deviceCollection.Values = tool_funs.SimpleStructToMap(deviceYouZhaJi)
|
|
|
break
|
|
|
//油炸机
|
|
|
case domain.DeviceTypeYouZhaJi2:
|
|
|
deviceYouZhaJi2 := &domain.DeviceYouZhaJi2{}
|
|
|
err = json.Unmarshal(mBytes, deviceYouZhaJi2)
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
deviceCollection.Values = tool_funs.SimpleStructToMap(deviceYouZhaJi2)
|
|
|
break
|
|
|
//串串机
|
|
|
case domain.DeviceTypeChuanChuanJi:
|
|
|
deviceChuanChuanJi := &domain.DeviceChuanChuanJi{}
|
|
|
err = json.Unmarshal(mBytes, deviceChuanChuanJi)
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
deviceCollection.Values = tool_funs.SimpleStructToMap(deviceChuanChuanJi)
|
|
|
break
|
|
|
//速冻线
|
|
|
case domain.DeviceTypeSuDongXian:
|
|
|
deviceSuDongXian := &domain.DeviceSuDongXian{}
|
|
|
err = json.Unmarshal(mBytes, deviceSuDongXian)
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
deviceCollection.Values = tool_funs.SimpleStructToMap(deviceSuDongXian)
|
|
|
break
|
|
|
//封口机
|
|
|
case domain.DeviceTypeFengKouJi:
|
|
|
deviceFengKouJi := &domain.DeviceFengKouJi{}
|
|
|
err = json.Unmarshal(mBytes, deviceFengKouJi)
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
deviceCollection.Values = tool_funs.SimpleStructToMap(deviceFengKouJi)
|
|
|
break
|
|
|
//封箱机
|
|
|
case domain.DeviceTypeFengXiangJi:
|
|
|
deviceFengXiangJi := &domain.DeviceFengXiangJi{}
|
|
|
err = json.Unmarshal(mBytes, deviceFengXiangJi)
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
deviceCollection.Values = tool_funs.SimpleStructToMap(deviceFengXiangJi)
|
|
|
break
|
|
|
//打浆机 //面包屑机
|
|
|
case domain.DeviceTypeDaJiangJi:
|
|
|
default:
|
|
|
mqtt.StartSubscribe(constant.MQTT_TOPIC, OnReceiveData)
|
|
|
}
|
|
|
|
|
|
func OnReceiveData(client pahomqtt.Client, message pahomqtt.Message) {
|
|
|
payload := make(map[string]interface{})
|
|
|
err := json.Unmarshal(message.Payload(), &payload)
|
|
|
if err != nil {
|
|
|
log.Logger.Error("车间数据json解析失败:" + err.Error())
|
|
|
return
|
|
|
}
|
|
|
if workShop, ok := payload["WorkShop"]; ok {
|
|
|
for key, item := range payload {
|
|
|
if key == "WorkShop" {
|
|
|
continue
|
|
|
}
|
|
|
mBytes, err := json.Marshal(item)
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
deviceCollection := &domain.DeviceCollection{
|
|
|
WorkShopName: workShop.(string),
|
|
|
DeviceSn: key,
|
|
|
CollectionTime: time.Now(),
|
|
|
StartupStatus: gjson.Get(string(message.Payload()), key+".StartupState").Int(),
|
|
|
ComStatus: gjson.Get(string(message.Payload()), key+".ComStatus").Int(),
|
|
|
}
|
|
|
if utils.SubStr(key, 0, 4) == domain.DeviceTypeMianBaoXieJi {
|
|
|
deviceCollection.DeviceType = domain.DeviceTypeMianBaoXieJi
|
|
|
} else {
|
|
|
deviceType := utils.SubStr(key, 0, 3)
|
|
|
deviceCollection.DeviceType = deviceType
|
|
|
switch deviceType {
|
|
|
//包馅机
|
|
|
case domain.DeviceTypeBaoXianJi:
|
|
|
deviceBaoXianJi := &domain.DeviceBaoXianJi{}
|
|
|
err = json.Unmarshal(mBytes, deviceBaoXianJi)
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
deviceCollectionService := service.NewDeviceCollectionService(nil)
|
|
|
resp, err := deviceCollectionService.CreateDeviceCollection(&command.CreateDeviceCollectionCommand{
|
|
|
WorkShopName: deviceCollection.WorkShopName,
|
|
|
StartupStatus: deviceCollection.StartupStatus,
|
|
|
CollectionTime: deviceCollection.CollectionTime,
|
|
|
DeviceSn: deviceCollection.DeviceSn,
|
|
|
DeviceType: deviceCollection.DeviceType,
|
|
|
ComStatus: deviceCollection.ComStatus,
|
|
|
Values: deviceCollection.Values,
|
|
|
})
|
|
|
deviceCollection.Values = tool_funs.SimpleStructToMap(deviceBaoXianJi)
|
|
|
break
|
|
|
//油炸机
|
|
|
case domain.DeviceTypeYouZhaJi1:
|
|
|
deviceYouZhaJi := &domain.DeviceYouZhaJi{}
|
|
|
err = json.Unmarshal(mBytes, deviceYouZhaJi)
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
result := resp.(map[string]interface{})
|
|
|
if deviceCollectionResult, ok := result["deviceCollection"]; ok {
|
|
|
fmt.Println(deviceCollectionResult)
|
|
|
deviceCollection.DeviceCollectionId = deviceCollectionResult.(*domain.DeviceCollection).DeviceCollectionId
|
|
|
workShopBytes, err := json.Marshal(deviceCollection)
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
err = redis.GetRedis().LPush(constant.REDIS_WORKSHOP_KEY, string(workShopBytes)).Err()
|
|
|
if err != nil {
|
|
|
log.Logger.Error("车间设备数据加入redis失败:" + err.Error())
|
|
|
}
|
|
|
|
|
|
//workShopBytes, err := json.Marshal(deviceCollection)
|
|
|
//if err != nil {
|
|
|
// continue
|
|
|
//}
|
|
|
//err = redis.GetRedis().LPush(constant.REDIS_WORKSHOP_KEY, string(workShopBytes)).Err()
|
|
|
//if err != nil {
|
|
|
// log.Logger.Error("车间设备数据加入redis失败:" + err.Error())
|
|
|
//}
|
|
|
//err = domainService.SendWorkshopDeviceData(deviceCollection)
|
|
|
//if err != nil {
|
|
|
// log.Logger.Error("车间设备数据加入redis失败:" + err.Error())
|
|
|
//
|
|
|
//}
|
|
|
deviceCollection.Values = tool_funs.SimpleStructToMap(deviceYouZhaJi)
|
|
|
break
|
|
|
//油炸机
|
|
|
case domain.DeviceTypeYouZhaJi2:
|
|
|
deviceYouZhaJi2 := &domain.DeviceYouZhaJi2{}
|
|
|
err = json.Unmarshal(mBytes, deviceYouZhaJi2)
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
deviceCollection.Values = tool_funs.SimpleStructToMap(deviceYouZhaJi2)
|
|
|
break
|
|
|
//串串机
|
|
|
case domain.DeviceTypeChuanChuanJi:
|
|
|
deviceChuanChuanJi := &domain.DeviceChuanChuanJi{}
|
|
|
err = json.Unmarshal(mBytes, deviceChuanChuanJi)
|
|
|
if err != nil {
|
|
|
log.Logger.Error(err.Error())
|
|
|
continue
|
|
|
}
|
|
|
deviceCollection.Values = tool_funs.SimpleStructToMap(deviceChuanChuanJi)
|
|
|
break
|
|
|
//速冻线
|
|
|
case domain.DeviceTypeSuDongXian:
|
|
|
deviceSuDongXian := &domain.DeviceSuDongXian{}
|
|
|
err = json.Unmarshal(mBytes, deviceSuDongXian)
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
deviceCollection.Values = tool_funs.SimpleStructToMap(deviceSuDongXian)
|
|
|
break
|
|
|
//封口机
|
|
|
case domain.DeviceTypeFengKouJi:
|
|
|
deviceFengKouJi := &domain.DeviceFengKouJi{}
|
|
|
err = json.Unmarshal(mBytes, deviceFengKouJi)
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
deviceCollection.Values = tool_funs.SimpleStructToMap(deviceFengKouJi)
|
|
|
break
|
|
|
//封箱机
|
|
|
case domain.DeviceTypeFengXiangJi:
|
|
|
deviceFengXiangJi := &domain.DeviceFengXiangJi{}
|
|
|
err = json.Unmarshal(mBytes, deviceFengXiangJi)
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
deviceCollection.Values = tool_funs.SimpleStructToMap(deviceFengXiangJi)
|
|
|
break
|
|
|
//打浆机 //面包屑机
|
|
|
case domain.DeviceTypeDaJiangJi:
|
|
|
case domain.DeviceTypeChuanChuanJi:
|
|
|
default:
|
|
|
}
|
|
|
if deviceType != domain.DeviceTypeChuanChuanJi {
|
|
|
continue
|
|
|
}
|
|
|
log.Logger.Info("MQTT", map[string]interface{}{
|
|
|
"Topic": message.Topic(),
|
|
|
"MessageId": message.MessageID(),
|
|
|
"Message": payload,
|
|
|
// 发送数据
|
|
|
deviceCollectionService := service.NewDeviceCollectionService(nil)
|
|
|
_, err := deviceCollectionService.CreateDeviceCollection(&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
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
log.Logger.Info("MQTT", map[string]interface{}{
|
|
|
"Topic": message.Topic(),
|
|
|
"MessageId": message.MessageID(),
|
|
|
"Message": payload,
|
|
|
})
|
|
|
}
|
|
|
} |
...
|
...
|
|