From 06cff5f59c72d36b745515d98b9e645e92bb91a3 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期三, 23 八月 2023 20:14:23 +0800 Subject: [PATCH] 修改获取工艺参数等待超时,修改生产进度返回值参数 --- api/v1/task.go | 311 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 288 insertions(+), 23 deletions(-) diff --git a/api/v1/task.go b/api/v1/task.go index 196ee69..48484b3 100644 --- a/api/v1/task.go +++ b/api/v1/task.go @@ -1,47 +1,312 @@ package v1 import ( - "apsClient/model/request" - _ "apsClient/model/response" + "apsClient/conf" + "apsClient/constvar" + "apsClient/model" + "apsClient/model/common" + "apsClient/model/response" + "apsClient/nsq" "apsClient/pkg/contextx" + "apsClient/pkg/convertx" "apsClient/pkg/ecode" + "apsClient/pkg/logx" + "apsClient/pkg/plc" + "apsClient/pkg/safe" "apsClient/service" - "encoding/json" + "apsClient/service/plc_address" + "errors" + "fmt" "github.com/gin-gonic/gin" + "time" ) type TaskApi struct{} -// TaskList -// @Tags Base -// @Summary 浠诲姟寮�鍚�氱煡 +// TaskGet +// @Tags Task +// @Summary 鑾峰彇浠诲姟 // @Produce application/json -// @Param object query request.TaskList true "鏌ヨ鍙傛暟" -// @Success 200 {object} contextx.Response{data=[]model.ScheduleTask} "鎴愬姛" -// @Router /v1/task/list [get] -func (slf *TaskApi) TaskList(c *gin.Context) { - var params request.TaskList - ctx, ok := contextx.NewContext(c, ¶ms) +// @Success 200 {object} contextx.Response{data=response.TaskData} "鎴愬姛" +// @Router /v1/task/get [get] +func (slf *TaskApi) TaskGet(c *gin.Context) { + ctx, ok := contextx.NewContext(c, nil) if !ok { return } - taskList, total, code := service.NewTaskService().GetTaskList(params.Page, params.PageSize) + taskData, code := service.NewTaskService().GetTask() if code != ecode.OK { ctx.Fail(code) return } + ctx.OkWithDetailed(taskData) +} - for _, task := range taskList { - if task.Data != "" { - err := json.Unmarshal([]byte(task.Data), &task.TaskInfo) - if err != nil { - ctx.Fail(ecode.UnknownErr) - return - } - } - +// TaskStart +// @Tags Task +// @Summary 浠诲姟寮�濮� +// @Produce application/json +// @Param id path int true "宸ュ簭id" +// @Success 200 {object} contextx.Response{data=response.ProcessParamsResponse} "鎴愬姛" +// @Router /v1/task/start/{id} [get] +func (slf *TaskApi) TaskStart(c *gin.Context) { + ctx, ok := contextx.NewContext(c, nil) + if !ok { + return + } + idx := c.Param("id") + if idx == "" { + ctx.Fail(ecode.ParamsErr) + return + } + id := convertx.Atoi(idx) + procedure, code := service.NewTaskService().GetProcedureById(id) + if code != ecode.OK { + ctx.Fail(code) + return + } + if procedure.Status != model.ProcedureStatusUnFinished { + ctx.FailWithMsg(ecode.ParamsErr, "璇ュ伐搴忓凡缁撴潫") + return } - ctx.ResultList(taskList, total) + order, err := service.NewTaskService().GetOrderByWorkOrderId(procedure.WorkOrderID) + if err != nil { + ctx.Fail(ecode.UnknownErr) + return + } + //params := service.GetProcessModelParams{ + // WorkOrder: procedure.WorkOrderID, + // OrderId: procedure.OrderID, + // Product: order.ProductName, + // Procedure: procedure.ProceduresInfo.ProcedureName, + // Device: procedure.ProceduresInfo.DeviceName, + //} + //resp, err := service.ProcessModel{}.GetProcessModel(params) + //if err != nil { + // logx.Errorf("TaskStart GetProcessModel error: %v", err.Error()) + // ctx.FailWithMsg(ecode.UnknownErr, "鏈幏鍙栧埌宸ヨ壓鍙傛暟") + // return + //} + + caller := nsq.NewCaller(fmt.Sprintf(constvar.NsqTopicProcessParamsRequest, conf.Conf.NsqConf.NodeId), fmt.Sprintf(constvar.NsqTopicProcessParamsResponse, conf.Conf.NsqConf.NodeId)) + var result common.ResponseProcessParams + + err = caller.Call(common.RequestProcessParams{ + WorkOrder: procedure.WorkOrderID, + OrderId: procedure.OrderID, + Product: order.ProductName, + Procedure: procedure.ProceduresInfo.ProcedureName, + Device: procedure.ProceduresInfo.DeviceName}, &result, time.Second*3) + if err != nil { + logx.Errorf("TaskStart GetProcessModel error:%v", err.Error()) + ctx.FailWithMsg(ecode.UnknownErr, "鏈幏鍙栧埌宸ヨ壓鍙傛暟") + return + } + if result.ParamsMap == nil { + logx.Errorf("TaskStart GetProcessModel response miss process params:%v", result) + ctx.FailWithMsg(ecode.UnknownErr, "鏈幏鍙栧埌宸ヨ壓鍙傛暟") + return + } + + processParamsArr := make([]response.ProcessParams, 0, len(result.ParamsMap)) + for k, v := range result.ParamsMap { + processParamsArr = append(processParamsArr, response.ProcessParams{ + Key: k, + Value: v, + }) + } + data := response.ProcessParamsResponse{ + Number: result.Number, + Params: processParamsArr, + } + logx.Infof("TaskStart Notice GetProcessModel: %+v", result) + ctx.OkWithDetailed(data) +} + +// TaskFinish +// @Tags Task +// @Summary 浠诲姟缁撴潫 +// @Produce application/json +// @Param id path int true "宸ュ簭id" +// @Success 200 {object} contextx.Response{service.GetProcessModel} "鎴愬姛" +// @Router /v1/task/finish/{id} [put] +func (slf *TaskApi) TaskFinish(c *gin.Context) { + ctx, ok := contextx.NewContext(c, nil) + if !ok { + return + } + idx := c.Param("id") + if idx == "" { + ctx.Fail(ecode.ParamsErr) + return + } + id := convertx.Atoi(idx) + _, code := service.NewTaskService().GetProcedureById(id) + if code != ecode.OK { + ctx.Fail(code) + return + } + err := service.NewTaskService().UpdateProcedureStatus(id, model.ProcedureStatusFinished) + if err != nil { + logx.Errorf("UpdateProcedureStatus err: %v", err.Error()) + ctx.Fail(ecode.UnknownErr) + return + } + ctx.Ok() +} + +// SendProcessParams +// @Tags Task +// @Summary 涓嬪彂宸ヨ壓鍙傛暟 +// @Produce application/json +// @Param id path int true "宸ュ簭id" +// @Success 200 {object} contextx.Response{service.GetProcessModel} "鎴愬姛" +// @Router /v1/task/sendProcessParams/{id} [post] +func (slf *TaskApi) SendProcessParams(c *gin.Context) { + ctx, ok := contextx.NewContext(c, nil) + if !ok { + return + } + idx := c.Param("id") + if idx == "" { + ctx.Fail(ecode.ParamsErr) + return + } + id := convertx.Atoi(idx) + procedure, code := service.NewTaskService().GetProcedureById(id) + if code != ecode.OK { + ctx.Fail(code) + return + } + order, err := service.NewTaskService().GetOrderByWorkOrderId(procedure.WorkOrderID) + if err != nil { + ctx.Fail(ecode.UnknownErr) + return + } + + caller := nsq.NewCaller(fmt.Sprintf(constvar.NsqTopicProcessParamsRequest, conf.Conf.NsqConf.NodeId), fmt.Sprintf(constvar.NsqTopicProcessParamsResponse, conf.Conf.NsqConf.NodeId)) + var result common.ResponseProcessParams + + err = caller.Call(common.RequestProcessParams{ + WorkOrder: procedure.WorkOrderID, + OrderId: procedure.OrderID, + Product: order.ProductName, + Procedure: procedure.ProceduresInfo.ProcedureName, + Device: procedure.ProceduresInfo.DeviceName}, &result, time.Second*3) + if err != nil { + logx.Errorf("SendProcessParams GetProcessModel error:%v", err.Error()) + ctx.FailWithMsg(ecode.UnknownErr, "鏈幏鍙栧埌宸ヨ壓鍙傛暟") + return + } + if result.ParamsMap == nil { + logx.Errorf("SendProcessParams GetProcessModel response miss process params:%v", result) + ctx.FailWithMsg(ecode.UnknownErr, "鏈幏鍙栧埌宸ヨ壓鍙傛暟") + return + } + + //fmt.Println("----------------寮�濮嬩笅鍙戝伐鑹哄弬鏁�-----------------") + //for k, v := range resp.ParamsMap { + // fmt.Println(fmt.Sprintf("%v : %v", k, v)) + // time.Sleep(time.Millisecond * 300) + //} + //fmt.Println("----------------涓嬪彂宸ヨ壓鍙傛暟瀹屾瘯-----------------") + + //err = SendParams(resp.ParamsMap) + //if err != nil { + // logx.Errorf("SendProcessParams: %v", err.Error()) + // ctx.Fail(ecode.UnknownErr) + // return + //} + + safe.Go(func() { + err = SendParams2(result.ParamsMap, 0) + if err != nil { + logx.Errorf("SendProcessParams: %v", err.Error()) + return + } + }) + ctx.Ok() +} + +func SendParams(paramsMap map[string]interface{}) error { + if len(paramsMap) == 0 { + return errors.New("empty params") + } + plcAddressMap := make(map[string]*int, len(conf.Conf.PLCAddresses)) + for _, item := range conf.Conf.PLCAddresses { + plcAddressMap[item.FieldName] = &item.Address + } + + plcConfig, code := service.NewDevicePlcService().GetDevicePlc() + if code != ecode.OK { + return errors.New("璇峰厛閰嶇疆PLC") + } + conn, err := plc.NewModbusConnection(fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port)) + if err != nil { + return errors.New(fmt.Sprintf("杩炴帴plc澶辫触: %v", err.Error())) + } + defer conn.Close() + + logx.Info("----------------寮�濮嬩笅鍙戝伐鑹哄弬鏁�-----------------") + for k, v := range paramsMap { + if plcAddressMap[k] != nil { + result, err := plc.WriteHoldingRegister(conn, *plcAddressMap[k], v) + if err != nil { + logx.Errorf("WriteHoldingRegister err:%v, address: %v, key: %v value: %v", err.Error(), *plcAddressMap[k], k, v) + } else { + logx.Infof("WriteHoldingRegister ok: key: %v, value: %v, result: %v", k, v, result) + } + } + } + logx.Info("----------------涓嬪彂宸ヨ壓鍙傛暟瀹屾瘯-----------------") + return nil +} + +func SendParams2(paramsMap map[string]interface{}, tryTimes int) error { + if len(paramsMap) == 0 { + return errors.New("empty params") + } + if tryTimes > 2 { + return errors.New("beyond max try time") + } + plcConfig, code := service.NewDevicePlcService().GetDevicePlc() + if code != ecode.OK { + return errors.New("璇峰厛閰嶇疆PLC") + } + conn, err := plc.NewModbusConnection(fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port)) + if err != nil { + return errors.New(fmt.Sprintf("杩炴帴plc澶辫触: %v", err.Error())) + } + defer conn.Close() + + logx.Info("----------------寮�濮嬩笅鍙戝伐鑹哄弬鏁�-----------------") + var missNumbers int + for k, v := range paramsMap { + if address, ok := plc_address.Get(k); ok { + result, err := plc.WriteHoldingRegister(conn, address, v) + if err != nil { + logx.Errorf("WriteHoldingRegister err:%v, address: %v, key: %v value: %v", err.Error(), address, k, v) + } else { + delete(paramsMap, k) + logx.Infof("WriteHoldingRegister ok: key: %v, value: %v, result: %v", k, v, result) + } + } else { + missNumbers++ + } + } + if missNumbers >= 1 { + caller := nsq.NewCaller(fmt.Sprintf(constvar.NsqTopicGetPlcAddress, conf.Conf.NsqConf.NodeId), fmt.Sprintf(constvar.NsqTopicSendPlcAddress, conf.Conf.NsqConf.NodeId)) + var addressResult common.ResponsePlcAddress + err := caller.Call(common.RequestPlcAddress{DeviceId: conf.Conf.System.DeviceId}, &addressResult, time.Second*2) + if err != nil { + logx.Infof("SendParams2 err: %v", err.Error()) + return err + } + tryTimes++ + return SendParams2(paramsMap, tryTimes) + } + logx.Info("----------------涓嬪彂宸ヨ壓鍙傛暟瀹屾瘯-----------------") + return nil } -- Gitblit v1.8.0