| | |
| | | package v1 |
| | | |
| | | import ( |
| | | "apsClient/conf" |
| | | "apsClient/constvar" |
| | | "apsClient/model" |
| | | "apsClient/model/request" |
| | | "apsClient/model/response" |
| | | _ "apsClient/model/response" |
| | | "apsClient/pkg/contextx" |
| | | "apsClient/pkg/ecode" |
| | | "apsClient/pkg/logx" |
| | | "apsClient/pkg/plc" |
| | | "apsClient/service" |
| | | "encoding/binary" |
| | | "fmt" |
| | | "github.com/gin-gonic/gin" |
| | | "github.com/spf13/cast" |
| | | "time" |
| | | ) |
| | | |
| | | type PlcApi struct{} |
| | | |
| | | // GetProductProgress |
| | | // @Tags 获取动态数据 |
| | | // @Tags 生产数量 |
| | | // @Summary 获取生产进度 |
| | | // @Produce application/json |
| | | // @Param object body request.GetProductProgress true "查询参数" |
| | | // @Success 200 {object} contextx.Response{data=response.ProductProgress} "成功" |
| | | // @Router /v1/plc/productProgress [get] |
| | | // @Router /v1/plc/productProgress [post] |
| | | func (slf *PlcApi) GetProductProgress(c *gin.Context) { |
| | | var params request.GetProductProgress |
| | | ctx, ok := contextx.NewContext(c, ¶ms) |
| | | if !ok { |
| | | return |
| | | } |
| | | isConnect := service.PlcIsConnect() |
| | | var ( |
| | | finishNumber interface{} |
| | | totalNumber interface{} |
| | | ) |
| | | |
| | | resp := new(response.ProductProgress) |
| | | |
| | | if isConnect { |
| | | finishNumber, _ = service.PlcCacheGet(conf.Conf.CurrentDeviceID, params.Channel, constvar.PlcCacheKeyFinishNumber) |
| | | totalNumber, _ = service.PlcCacheGet(conf.Conf.CurrentDeviceID, params.Channel, constvar.PlcCacheKeyTotalNumber) |
| | | } else if params.ProcedureId != 0 { |
| | | reportWork, err := service.GetLastReportWork(params.ProcedureId) |
| | | if err == nil { |
| | | finishNumber = reportWork.ReportAmount |
| | | workOrder, err := model.NewOrderSearch(nil).SetWorkOrderId(reportWork.WorkOrderID).First() |
| | | if err == nil { |
| | | totalNumber = workOrder.Amount.IntPart() |
| | | } |
| | | } else { |
| | | procedure, workOrder, _ := service.NewTaskService().GetProcedureAndWorkOrder(params.ProcedureId) |
| | | if workOrder != nil { |
| | | totalNumber = workOrder.Amount.IntPart() |
| | | } |
| | | if procedure != nil { |
| | | resp.RealStartTime = procedure.RealStartTime |
| | | resp.RealEndTime = procedure.RealEndTime |
| | | } |
| | | } |
| | | } |
| | | |
| | | resp.FinishNumber = cast.ToInt(finishNumber) |
| | | resp.TotalNumber = cast.ToInt(totalNumber) |
| | | |
| | | plcStatus := 1 //断开连接 |
| | | if isConnect { |
| | | lastUpdateTime := service.FinishUpdateTimeGet(conf.Conf.CurrentDeviceID, params.Channel) |
| | | if time.Now().Unix()-cast.ToInt64(lastUpdateTime) < conf.Conf.PLC.StandbyTime { //生产 |
| | | plcStatus = 2 |
| | | } else { |
| | | plcStatus = 3 //待机 |
| | | } |
| | | } |
| | | resp.PlcStatus = plcStatus |
| | | |
| | | if params.ProcedureId == 0 { |
| | | resp.RealStartTime = service.TaskStartTimeGet(conf.Conf.CurrentDeviceID, params.Channel) |
| | | resp.RealEndTime = service.TaskEndTimeGet(conf.Conf.CurrentDeviceID, params.Channel) |
| | | } |
| | | |
| | | ctx.OkWithDetailed(resp) |
| | | } |
| | | |
| | | // SetProductNumber |
| | | // @Tags 生产数量 |
| | | // @Summary 设置生产总量 |
| | | // @Produce application/json |
| | | // @Success 200 {object} contextx.Response{} "成功" |
| | | // @Router /v1/plc/setProductNumber [post] |
| | | func (slf *PlcApi) SetProductNumber(c *gin.Context) { |
| | | ctx, ok := contextx.NewContext(c, nil) |
| | | if !ok { |
| | | return |
| | | } |
| | | |
| | | plcConfig, code := service.NewDevicePlcService().GetDevicePlc() |
| | | if code != ecode.OK { |
| | | ctx.FailWithMsg(ecode.UnknownErr, "请先配置PLC") |
| | | return |
| | | } |
| | | |
| | | var startAddress int |
| | | var valueType string |
| | | var dataLength int |
| | | var ipAddr string |
| | | |
| | | for _, pc := range plcConfig.Details { |
| | | if pc.FieldName == constvar.PlcStartAddressTypeFinishNumber { |
| | | startAddress = pc.StartAddress |
| | | valueType = pc.Type |
| | | dataLength = pc.Length |
| | | } |
| | | ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port) |
| | | } |
| | | |
| | | resp := new(response.ProductProgress) |
| | | if startAddress == 0 || valueType == "" { |
| | | ctx.OkWithDetailed(resp) |
| | | logx.Warnf("请先配置PLC") |
| | | return |
| | | } |
| | | conn, err := plc.NewModbusConnection(ipAddr) |
| | | if err != nil { |
| | | ctx.OkWithDetailed(resp) |
| | | logx.Errorf("GetProductProgress 连接plc失败: %v", err.Error()) |
| | | return |
| | | } |
| | | defer conn.Close() |
| | | |
| | | rawData, err := plc.ReadHoldingRegister(conn, startAddress, dataLength) |
| | | if err != nil { |
| | | ctx.OkWithDetailed(resp) |
| | | logx.Errorf("GetProductProgress 获取plc数据失败: %v", err.Error()) |
| | | return |
| | | } |
| | | resp.FinishNumber = int(binary.BigEndian.Uint16(rawData)) |
| | | if err != nil { |
| | | ctx.OkWithDetailed(resp) |
| | | logx.Errorf("GetProductProgress 获取生产进度数据解析失败: %v, data: %v, valueType:%v", err.Error(), rawData, valueType) |
| | | return |
| | | } |
| | | ctx.OkWithDetailed(resp) |
| | | ctx.Ok() |
| | | } |
| | | |
| | | //// SetSerfEvent |
| | | //// @Tags 生产数量 |
| | | //// @Summary 设置生产总量 |
| | | //// @Produce application/json |
| | | //// @Success 200 {object} contextx.Response{data=response.ProductProgress} "成功" |
| | | //// @Router /v1/plc/setSerfEvent [post] |
| | | //func (slf *PlcApi) SetSerfEvent(c *gin.Context) { |
| | | // ctx, ok := contextx.NewContext(c, nil) |
| | | // if !ok { |
| | | // return |
| | | // } |
| | | // status := c.Query("status") |
| | | // stat := cast.ToInt(status) |
| | | // switch stat { |
| | | // case serf.EventCreateCluster, serf.EventSlave2Master, serf.EventLeaveCluster: |
| | | // if err := nsq.Init(); err != nil { //开启nsq |
| | | // logx.Errorf("nsq Init err:%v", err) |
| | | // return |
| | | // } |
| | | // crontask.Once(true) |
| | | // if err := crontask.RestartTask(true); err != nil { //以master方式重启task |
| | | // logx.Errorf("crontab task Init err:%v", err) |
| | | // return |
| | | // } |
| | | // case serf.EventJoinCluster, serf.EventMaster2Slave: |
| | | // nsq.Stop() //关闭nsq |
| | | // crontask.Once(false) |
| | | // if err := crontask.RestartTask(false); err != nil { //以非master方式重启task |
| | | // logx.Errorf("crontab task Init err:%v", err) |
| | | // return |
| | | // } |
| | | // } |
| | | // |
| | | // logx.Infof("serf cluster event: %v", stat) |
| | | // |
| | | // ctx.Ok() |
| | | //} |
| | | |
| | | //// GetProductProgressRealTime |
| | | //// @Tags 生产数量 |
| | | //// @Summary 实时获取生产进度 |
| | | //// @Produce application/json |
| | | //// @Success 200 {object} contextx.Response{data=response.ProductProgress} "成功" |
| | | //// @Router /v1/plc/productProgressRealTime [post] |
| | | //func (slf *PlcApi) GetProductProgressRealTime(c *gin.Context) { |
| | | // ctx, ok := contextx.NewContext(c, nil) |
| | | // if !ok { |
| | | // return |
| | | // } |
| | | // var finishNumber, totalNumber int64 |
| | | // plcConfig, code := service.NewDevicePlcService().GetDevicePlc() |
| | | // if code != ecode.OK { |
| | | // return |
| | | // } |
| | | // for _, addressItem := range plcConfig.Details { |
| | | // if addressItem.FieldName == constvar.PlcStartAddressTypeFinishNumber { |
| | | // value, err := service.PlcReadDirect(plcConfig, addressItem.StartAddress, addressItem.Length, addressItem.Type) |
| | | // if err != nil { |
| | | // logx.Infof("plc read finish number err: %v", err) |
| | | // continue |
| | | // } |
| | | // finishNumber = cast.ToInt64(value) |
| | | // logx.Infof("plc read finish number: %v", finishNumber) |
| | | // break |
| | | // } |
| | | // } |
| | | // |
| | | // for _, addressItem := range plcConfig.Details { |
| | | // if addressItem.FieldName == constvar.PlcStartAddressTypeTotalNumber { |
| | | // value, err := service.PlcReadDirect(plcConfig, addressItem.StartAddress, addressItem.Length, addressItem.Type) |
| | | // if err != nil { |
| | | // logx.Infof("plc read total number err: %v", err) |
| | | // continue |
| | | // } |
| | | // totalNumber = cast.ToInt64(value) |
| | | // logx.Infof("plc read total number: %v", totalNumber) |
| | | // break |
| | | // } |
| | | // } |
| | | // resp := new(response.ProductProgress) |
| | | // resp.FinishNumber = cast.ToInt(finishNumber) |
| | | // resp.TotalNumber = cast.ToInt(totalNumber) |
| | | // |
| | | // plcStatus := 1 //断开连接 |
| | | // isConnect := apacheplc4x.IsConnect() |
| | | // if isConnect { |
| | | // if resp.FinishNumber > 0 { //生产 |
| | | // plcStatus = 2 |
| | | // } else { //待机 |
| | | // plcStatus = 3 |
| | | // } |
| | | // } |
| | | // resp.PlcStatus = plcStatus |
| | | // |
| | | // ctx.OkWithDetailed(resp) |
| | | //} |