| | |
| | | package v1 |
| | | |
| | | import ( |
| | | "apsClient/conf" |
| | | "apsClient/constvar" |
| | | "apsClient/model" |
| | | "apsClient/model/common" |
| | | "apsClient/model/response" |
| | | _ "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" |
| | | "apsClient/service/plc_address" |
| | | "errors" |
| | | "fmt" |
| | | "github.com/gin-gonic/gin" |
| | | "gorm.io/gorm" |
| | | "time" |
| | | ) |
| | | |
| | | type TaskApi struct{} |
| | | |
| | | // TaskCountdown |
| | | // @Tags Task |
| | | // @Summary 新任务倒计时 |
| | | // @Produce application/json |
| | | // @Success 200 {object} contextx.Response{data=response.TaskCountdown} "成功" |
| | | // @Router /v1/task/countdown [get] |
| | | func (slf *TaskApi) TaskCountdown(c *gin.Context) { |
| | | ctx, ok := contextx.NewContext(c, nil) |
| | | if !ok { |
| | | return |
| | | } |
| | | var resp response.TaskCountdown |
| | | workOrder, err := service.NewTaskService().GetCurrentTask() |
| | | if err == nil { |
| | | seconds := workOrder.StartTime - time.Now().Unix() |
| | | resp.CountDownHour = seconds / 3600 |
| | | resp.CountDownMinute = seconds % 3600 / 60 |
| | | resp.ShowCountDown = true |
| | | } |
| | | ctx.OkWithDetailed(resp) |
| | | } |
| | | |
| | | // TaskGet |
| | | // @Tags Task |
| | |
| | | // @Summary 任务开始 |
| | | // @Produce application/json |
| | | // @Param id path int true "工序id" |
| | | // @Success 200 {object} contextx.Response{data=[]response.ProcessParams} "成功" |
| | | // @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) |
| | |
| | | ctx.Fail(code) |
| | | return |
| | | } |
| | | |
| | | if procedure.Status != model.ProcedureStatusUnFinished { |
| | | ctx.FailWithMsg(ecode.ParamsErr, "该工序已结束") |
| | | if procedure.Status != model.ProcedureStatusWaitProcess { |
| | | ctx.FailWithMsg(ecode.ParamsErr, "该工序状态不是未开始") |
| | | return |
| | | } |
| | | |
| | |
| | | return |
| | | } |
| | | |
| | | params := service.GetProcessModelParams{ |
| | | 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, |
| | | } |
| | | |
| | | resp, err := service.ProcessModel{}.GetProcessModel(params) |
| | | |
| | | Device: procedure.ProceduresInfo.DeviceName}, &result, time.Second*3) |
| | | if err != nil { |
| | | logx.Errorf("TaskStart Notice GetProcessModel error: %v", err.Error()) |
| | | logx.Errorf("TaskStart GetProcessModel error:%v", err.Error()) |
| | | ctx.FailWithMsg(ecode.UnknownErr, "未获取到工艺参数") |
| | | return |
| | | } |
| | | processParamsArr := make([]*response.ProcessParams, 0, len(resp.ParamsMap)) |
| | | for k, v := range resp.ParamsMap { |
| | | processParamsArr = append(processParamsArr, &response.ProcessParams{ |
| | | 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, |
| | | }) |
| | | } |
| | | logx.Infof("TaskStart Notice GetProcessModel: %+v", resp) |
| | | ctx.OkWithDetailed(processParamsArr) |
| | | data := response.ProcessParamsResponse{ |
| | | Number: result.Number, |
| | | Params: processParamsArr, |
| | | } |
| | | logx.Infof("TaskStart Notice GetProcessModel: %+v", result) |
| | | ctx.OkWithDetailed(data) |
| | | } |
| | | |
| | | // TaskFinish |
| | |
| | | ctx.Fail(code) |
| | | return |
| | | } |
| | | err := service.NewTaskService().UpdateProcedureStatus(id, model.ProcedureStatusFinished) |
| | | err := service.NewTaskService().UpdateProcedureStatus(nil, id, model.ProcedureStatusFinished) |
| | | if err != nil { |
| | | logx.Errorf("UpdateProcedureStatus err: %v", err.Error()) |
| | | ctx.Fail(ecode.UnknownErr) |
| | |
| | | return |
| | | } |
| | | |
| | | params := service.GetProcessModelParams{ |
| | | WorkOrder: "", |
| | | 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.DeviceID, |
| | | } |
| | | |
| | | resp, err := service.ProcessModel{}.GetProcessModel(params) |
| | | Device: procedure.ProceduresInfo.DeviceName}, &result, time.Second*3) |
| | | if err != nil { |
| | | logx.Errorf("SendProcessModel GetProcessModel err: %v", err.Error()) |
| | | ctx.Fail(ecode.UnknownErr) |
| | | logx.Errorf("SendProcessParams GetProcessModel error:%v", err.Error()) |
| | | 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) |
| | | if result.ParamsMap == nil { |
| | | logx.Errorf("SendProcessParams GetProcessModel response miss process params:%v", result) |
| | | ctx.FailWithMsg(ecode.UnknownErr, "未获取到工艺参数") |
| | | return |
| | | } |
| | | fmt.Println("----------------下发工艺参数完毕-----------------") |
| | | |
| | | err = model.WithTransaction(func(db *gorm.DB) error { |
| | | err = service.NewTaskService().UpdateProcedureStatus(db, id, model.ProcedureStatusProcessing) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | err = service.NewTaskService().UpdateOrderStatus(db, order.ID, model.OrderStatusProcessing) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | return nil |
| | | }) |
| | | if err != nil { |
| | | logx.Errorf("SendProcessParams update order and procedure status error:%v", err.Error()) |
| | | ctx.FailWithMsg(ecode.DBErr, "更改工单状态失败") |
| | | return |
| | | } |
| | | |
| | | safe.Go(func() { |
| | | err = SendParams(result.ParamsMap, 0) |
| | | if err != nil { |
| | | logx.Errorf("SendProcessParams: %v", err.Error()) |
| | | return |
| | | } |
| | | plcConfig, code := service.NewDevicePlcService().GetDevicePlc() |
| | | if code != ecode.OK { |
| | | logx.Errorf("get plcConfig err: %v", err.Error()) |
| | | return |
| | | } |
| | | _ = service.PlcWrite(plcConfig, constvar.PlcStartAddressTypeTotalNumber, order.Amount.IntPart()) |
| | | }) |
| | | ctx.Ok() |
| | | } |
| | | |
| | | func SendParams(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.GetModbusConnection(fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port)) |
| | | if err != nil { |
| | | return errors.New(fmt.Sprintf("连接plc失败: %v", err.Error())) |
| | | } |
| | | |
| | | 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 { |
| | | logx.Errorf("miss param address, k:%v, v:%v", k, v) |
| | | 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 SendParams(paramsMap, tryTimes) |
| | | } |
| | | logx.Info("----------------下发工艺参数完毕-----------------") |
| | | return nil |
| | | } |