From 21e0e02fd465fef04f1834b078c621a0e3594308 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期六, 02 九月 2023 16:51:48 +0800
Subject: [PATCH] 任务开始和结束都发送消息通知apsserver

---
 constvar/const.go      |   13 +++---
 model/common/common.go |    9 ++++
 nsq/caller.go          |   11 +++++
 api/v1/task.go         |   35 +++++++++++++++++
 4 files changed, 61 insertions(+), 7 deletions(-)

diff --git a/api/v1/task.go b/api/v1/task.go
index 0a364f5..1729bc7 100644
--- a/api/v1/task.go
+++ b/api/v1/task.go
@@ -1,9 +1,12 @@
 package v1
 
 import (
+	"apsClient/conf"
 	"apsClient/constvar"
 	"apsClient/model"
+	"apsClient/model/common"
 	"apsClient/model/response"
+	"apsClient/nsq"
 	"apsClient/pkg/contextx"
 	"apsClient/pkg/convertx"
 	"apsClient/pkg/ecode"
@@ -130,7 +133,7 @@
 		return
 	}
 	id := convertx.Atoi(idx)
-	_, code := service.NewTaskService().GetProcedureById(id)
+	procedure, code := service.NewTaskService().GetProcedureById(id)
 	if code != ecode.OK {
 		ctx.Fail(code)
 		return
@@ -141,6 +144,21 @@
 		ctx.Fail(ecode.UnknownErr)
 		return
 	}
+
+	msg := &common.MsgTaskStatusUpdate{
+		WorkOrderId:  procedure.WorkOrderID,
+		ProcedureID:  procedure.ProceduresInfo.ProcedureID,
+		DeviceId:     procedure.ProceduresInfo.DeviceID,
+		IsProcessing: false,
+		IsFinish:     true,
+	}
+
+	caller := nsq.NewCaller(fmt.Sprintf(constvar.NsqTopicTaskProcedureStatusUpdate, conf.Conf.NsqConf.NodeId), "")
+	err = caller.Send(msg)
+	if err != nil {
+		logx.Errorf("send task status update msg error:%v", err.Error())
+	}
+
 	ctx.Ok()
 }
 
@@ -222,6 +240,21 @@
 		ctx.FailWithMsg(ecode.NeedConfirmedErr, "绯熺硶锛屽伐鑹轰笅鍙戝け璐ャ��")
 		return
 	}
+
+	msg := &common.MsgTaskStatusUpdate{
+		WorkOrderId:  procedure.WorkOrderID,
+		ProcedureID:  procedure.ProceduresInfo.ProcedureID,
+		DeviceId:     procedure.ProceduresInfo.DeviceID,
+		IsProcessing: true,
+		IsFinish:     false,
+	}
+
+	caller := nsq.NewCaller(fmt.Sprintf(constvar.NsqTopicTaskProcedureStatusUpdate, conf.Conf.NsqConf.NodeId), "")
+	err = caller.Send(msg)
+	if err != nil {
+		logx.Errorf("send task status update msg error:%v", err.Error())
+	}
+
 	ctx.Ok()
 }
 
diff --git a/constvar/const.go b/constvar/const.go
index 3856612..9f21401 100644
--- a/constvar/const.go
+++ b/constvar/const.go
@@ -1,12 +1,13 @@
 package constvar
 
 const (
-	NsqTopicScheduleTask          = "aps.%v.scheduleTask" //鎺掔▼浠诲姟涓嬪彂
-	NsqTopicGetPlcAddress         = "aps.%v.getPlcAddress"
-	NsqTopicSendPlcAddress        = "aps.%v.sendPlcAddress"
-	NsqTopicProcessParamsRequest  = "aps.%v.processParams.request"
-	NsqTopicProcessParamsResponse = "aps.%v.processParams.response"
-	NsqTopicApsProcessParams      = "aps.%v.aps.processParams" //鏈変簡鏂扮殑宸ヨ壓妯″瀷
+	NsqTopicScheduleTask              = "aps.%v.scheduleTask" //鎺掔▼浠诲姟涓嬪彂
+	NsqTopicGetPlcAddress             = "aps.%v.getPlcAddress"
+	NsqTopicSendPlcAddress            = "aps.%v.sendPlcAddress"
+	NsqTopicProcessParamsRequest      = "aps.%v.processParams.request"
+	NsqTopicProcessParamsResponse     = "aps.%v.processParams.response"
+	NsqTopicApsProcessParams          = "aps.%v.aps.processParams"     //鏈変簡鏂扮殑宸ヨ壓妯″瀷
+	NsqTopicTaskProcedureStatusUpdate = "aps.%v.task.procedure.status" //宸ュ簭鐘舵�佹洿鏂�
 )
 
 type PlcStartAddressType int
diff --git a/model/common/common.go b/model/common/common.go
index 9331347..a8fc8af 100644
--- a/model/common/common.go
+++ b/model/common/common.go
@@ -88,3 +88,12 @@
 		DeviceId  string                 `json:"deviceId" gorm:"-"` //鐢ㄤ簬杩囨护鑾峰彇nsq娑堟伅
 	}
 )
+
+// MsgTaskStatusUpdate 浠诲姟鐘舵�佹敼鍙�
+type MsgTaskStatusUpdate struct {
+	WorkOrderId  string `json:"workOrderId"` //宸ュ崟缂栧彿
+	ProcedureID  string `json:"procedureId"` // 宸ュ簭
+	DeviceId     string `json:"deviceId"`    //鐢ㄤ簬杩囨护鑾峰彇nsq娑堟伅
+	IsProcessing bool   //鏄惁澶勭悊涓�
+	IsFinish     bool   //鏄惁瀹屾垚
+}
diff --git a/nsq/caller.go b/nsq/caller.go
index 59c6848..ed0dbdd 100644
--- a/nsq/caller.go
+++ b/nsq/caller.go
@@ -49,3 +49,14 @@
 		}
 	}
 }
+
+func (caller *DefaultCaller) Send(input interface{}) error {
+	msg, err := json.Marshal(input)
+	if err != nil {
+		return err
+	}
+	producer := GetProducer()
+	err = producer.Publish(caller.RequestTopic, msg)
+	return err
+
+}

--
Gitblit v1.8.0