From 8dfb8feb32bb5e4e460e23dcde42612a26fa2bcb Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期日, 27 八月 2023 01:11:28 +0800
Subject: [PATCH] fix

---
 nsq/msg_handler.go |  101 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 92 insertions(+), 9 deletions(-)

diff --git a/nsq/msg_handler.go b/nsq/msg_handler.go
index fef9a83..be6dea7 100644
--- a/nsq/msg_handler.go
+++ b/nsq/msg_handler.go
@@ -2,13 +2,30 @@
 
 import (
 	"apsClient/conf"
+	"apsClient/constvar"
 	"apsClient/model"
+	"apsClient/model/common"
 	"apsClient/pkg/logx"
 	"apsClient/pkg/structx"
+	"apsClient/service/plc_address"
+	"apsClient/utils/file"
 	"encoding/json"
 	"fmt"
+	"github.com/spf13/cast"
 	"gorm.io/gorm"
+	"strings"
 )
+
+type ReceivedMessage struct {
+	Topic   string
+	Message []byte
+}
+
+var ReceivedMessageChan chan *ReceivedMessage
+
+func init() {
+	ReceivedMessageChan = make(chan *ReceivedMessage, 1000)
+}
 
 type MsgHandler interface {
 	HandleMessage(data []byte) (err error)
@@ -18,10 +35,8 @@
 }
 
 func (slf *ScheduleTask) HandleMessage(data []byte) (err error) {
-	fmt.Println(string(data))
-
-	var tasks = make([]*DeliverScheduleTask, 0)
-
+	logx.Infof("get an message :%s", data)
+	var tasks = make([]*common.DeliverScheduleTask, 0)
 	err = json.Unmarshal(data, &tasks)
 	if err != nil {
 		logx.Errorf("ScheduleTask HandleMessage Unmarshal json err: %v", err.Error())
@@ -31,11 +46,11 @@
 		for _, procedure := range task.Procedures {
 			if procedure.DeviceID == conf.Conf.System.DeviceId { //鍙瓨鍌ㄦ湰璁惧鐨勫伐搴�
 				err = model.WithTransaction(func(db *gorm.DB) error {
-					_, err := model.NewOrderSearch(db).SetOrderId(task.Order.OrderID).First()
+					_, err := model.NewOrderSearch(db).SetWorkOrderId(task.WorkOrder.WorkOrderID).First()
 					if err == gorm.ErrRecordNotFound {
 						var orderRecord model.Order
-						err = structx.AssignTo(task.Order, &orderRecord)
-						orderRecord.Status = model.OrderStatusUnFinished
+						err = structx.AssignTo(task.WorkOrder, &orderRecord)
+						orderRecord.Status = model.OrderStatusWaitProcess
 						if err != nil {
 							logx.Errorf(" structx.Assign task.Order to orderRecord err: %v", err.Error())
 							return err
@@ -45,8 +60,11 @@
 						}
 					}
 					procedureRecord := model.Procedures{
-						OrderID: task.Order.OrderID,
-						Status:  model.ProcedureStatusUnFinished,
+						StartTime:   procedure.StartTime,
+						EndTime:     procedure.EndTime,
+						WorkOrderID: task.WorkOrder.WorkOrderID,
+						OrderID:     task.WorkOrder.OrderID,
+						Status:      model.ProcedureStatusWaitProcess,
 					}
 					procedureData, err := json.Marshal(procedure)
 					procedureRecord.ProcedureData = string(procedureData)
@@ -64,3 +82,68 @@
 	}
 	return nil
 }
+
+type PlcAddress struct {
+	Topic string
+}
+
+func (slf *PlcAddress) HandleMessage(data []byte) (err error) {
+	logx.Infof("get an PlcAddress message :%s", data)
+	var resp = new(common.ResponsePlcAddress)
+	err = json.Unmarshal(data, &resp)
+	if err != nil {
+		logx.Errorf("ScheduleTask HandleMessage Unmarshal json err: %v", err.Error())
+		return err
+	}
+	//鍐欏叆鍒版枃浠�
+	err = file.WriteFile(fmt.Sprintf("%s%s", constvar.PlcAddressDataPath, constvar.PlcAddressDataKeyFileName), resp.KeyData)
+	if err != nil {
+		return err
+	}
+	err = file.WriteFile(fmt.Sprintf("%s%s", constvar.PlcAddressDataPath, constvar.PlcAddressDataValueFileName), resp.AddressData)
+	if err != nil {
+		return err
+	}
+	//鍐欏叆鍒板唴瀛�
+	keyString := string(resp.KeyData)
+	addressString := string(resp.AddressData)
+
+	keys := strings.Split(keyString, "\n")
+	addresses := strings.Split(addressString, "\n")
+	if len(keys) != len(addresses) {
+		logx.Error("plc address message error: key length not equal address length")
+		return nil
+	}
+	for i := 0; i < len(keys); i++ {
+		key := strings.ReplaceAll(keys[i], "\r", "")
+		address := cast.ToInt(strings.ReplaceAll(addresses[i], "\r", ""))
+		plc_address.Set(key, address)
+		logx.Infof("plc address set ok: key:%v, address:%v", key, address)
+	}
+	//閫氱煡鍥炲鏀跺埌
+	ReceivedMessageChan <- &ReceivedMessage{
+		Topic:   slf.Topic,
+		Message: data,
+	}
+	return nil
+}
+
+type ProcessParams struct {
+	Topic string
+}
+
+func (slf *ProcessParams) HandleMessage(data []byte) (err error) {
+	logx.Infof("get an process params message :%s", data)
+	var resp = new(common.ResponseProcessParams)
+	err = json.Unmarshal(data, &resp)
+	if err != nil {
+		logx.Errorf("ScheduleTask HandleMessage Unmarshal json err: %v", err.Error())
+		return err
+	}
+	//閫氱煡鍥炲鏀跺埌
+	ReceivedMessageChan <- &ReceivedMessage{
+		Topic:   slf.Topic,
+		Message: data,
+	}
+	return nil
+}

--
Gitblit v1.8.0