| | |
| | | |
| | | 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().GetComingTask() |
| | | 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 获取任务 |
| | |
| | | } |
| | | } |
| | | }, |
| | | "/v1/task/countdown": { |
| | | "get": { |
| | | "produces": [ |
| | | "application/json" |
| | | ], |
| | | "tags": [ |
| | | "Task" |
| | | ], |
| | | "summary": "新任务倒计时", |
| | | "responses": { |
| | | "200": { |
| | | "description": "成功", |
| | | "schema": { |
| | | "allOf": [ |
| | | { |
| | | "$ref": "#/definitions/contextx.Response" |
| | | }, |
| | | { |
| | | "type": "object", |
| | | "properties": { |
| | | "data": { |
| | | "$ref": "#/definitions/response.TaskCountdown" |
| | | } |
| | | } |
| | | } |
| | | ] |
| | | } |
| | | } |
| | | } |
| | | } |
| | | }, |
| | | "/v1/task/finish/{id}": { |
| | | "put": { |
| | | "produces": [ |
| | |
| | | } |
| | | } |
| | | }, |
| | | "response.TaskCountdown": { |
| | | "type": "object", |
| | | "properties": { |
| | | "countDownHour": { |
| | | "description": "倒计时 时", |
| | | "type": "integer" |
| | | }, |
| | | "countDownMinute": { |
| | | "description": "倒计时 分", |
| | | "type": "integer" |
| | | }, |
| | | "showCountDown": { |
| | | "description": "是否展示倒计时", |
| | | "type": "boolean" |
| | | } |
| | | } |
| | | }, |
| | | "response.TaskData": { |
| | | "type": "object", |
| | | "properties": { |
| | |
| | | } |
| | | } |
| | | }, |
| | | "/v1/task/countdown": { |
| | | "get": { |
| | | "produces": [ |
| | | "application/json" |
| | | ], |
| | | "tags": [ |
| | | "Task" |
| | | ], |
| | | "summary": "新任务倒计时", |
| | | "responses": { |
| | | "200": { |
| | | "description": "成功", |
| | | "schema": { |
| | | "allOf": [ |
| | | { |
| | | "$ref": "#/definitions/contextx.Response" |
| | | }, |
| | | { |
| | | "type": "object", |
| | | "properties": { |
| | | "data": { |
| | | "$ref": "#/definitions/response.TaskCountdown" |
| | | } |
| | | } |
| | | } |
| | | ] |
| | | } |
| | | } |
| | | } |
| | | } |
| | | }, |
| | | "/v1/task/finish/{id}": { |
| | | "put": { |
| | | "produces": [ |
| | |
| | | } |
| | | } |
| | | }, |
| | | "response.TaskCountdown": { |
| | | "type": "object", |
| | | "properties": { |
| | | "countDownHour": { |
| | | "description": "倒计时 时", |
| | | "type": "integer" |
| | | }, |
| | | "countDownMinute": { |
| | | "description": "倒计时 分", |
| | | "type": "integer" |
| | | }, |
| | | "showCountDown": { |
| | | "description": "是否展示倒计时", |
| | | "type": "boolean" |
| | | } |
| | | } |
| | | }, |
| | | "response.TaskData": { |
| | | "type": "object", |
| | | "properties": { |
| | |
| | | totalNumber: |
| | | type: integer |
| | | type: object |
| | | response.TaskCountdown: |
| | | properties: |
| | | countDownHour: |
| | | description: 倒计时 时 |
| | | type: integer |
| | | countDownMinute: |
| | | description: 倒计时 分 |
| | | type: integer |
| | | showCountDown: |
| | | description: 是否展示倒计时 |
| | | type: boolean |
| | | type: object |
| | | response.TaskData: |
| | | properties: |
| | | deviceName: |
| | |
| | | summary: 更新plc品牌 |
| | | tags: |
| | | - plc品牌 |
| | | /v1/task/countdown: |
| | | get: |
| | | produces: |
| | | - application/json |
| | | responses: |
| | | "200": |
| | | description: 成功 |
| | | schema: |
| | | allOf: |
| | | - $ref: '#/definitions/contextx.Response' |
| | | - properties: |
| | | data: |
| | | $ref: '#/definitions/response.TaskCountdown' |
| | | type: object |
| | | summary: 新任务倒计时 |
| | | tags: |
| | | - Task |
| | | /v1/task/finish/{id}: |
| | | put: |
| | | parameters: |
| | |
| | | FinishNumber int `json:"finishNumber"` |
| | | TotalNumber int `json:"totalNumber"` |
| | | } |
| | | |
| | | type TaskCountdown struct { |
| | | CountDownHour int64 //倒计时 时 |
| | | CountDownMinute int64 //倒计时 分 |
| | | ShowCountDown bool //是否展示倒计时 |
| | | } |
| | |
| | | Orm *gorm.DB |
| | | Preload bool |
| | | StartTimeMax int64 |
| | | StartTimeMin int64 |
| | | } |
| | | ) |
| | | |
| | |
| | | slf.StartTimeMax = ts |
| | | return slf |
| | | } |
| | | func (slf *OrderSearch) SetStartTimeMin(ts int64) *OrderSearch { |
| | | slf.StartTimeMin = ts |
| | | return slf |
| | | } |
| | | |
| | | func (slf *OrderSearch) SetStatus(status OrderStatus) *OrderSearch { |
| | | slf.Status = status |
| | |
| | | db = db.Where("start_time <= ?", slf.StartTimeMax) |
| | | } |
| | | |
| | | if slf.StartTimeMin != 0 { |
| | | db = db.Where("start_time >= ?", slf.StartTimeMin) |
| | | } |
| | | |
| | | if slf.Preload { |
| | | db = db.Preload("InputMaterials").Preload("OutputMaterials") |
| | | } |
| | |
| | | taskApi := new(v1.TaskApi) |
| | | taskGroup := v1Group.Group("task") |
| | | { |
| | | taskGroup.GET("countdown", taskApi.TaskCountdown) // 新任务倒计时 |
| | | taskGroup.GET("get", taskApi.TaskGet) // 获取工序 |
| | | taskGroup.GET("start/:id", taskApi.TaskStart) // 开启工序并获取参数 |
| | | taskGroup.PUT("finish/:id", taskApi.TaskFinish) // 完成工序 |
| | |
| | | valueType = pc.Type |
| | | dataLength = pc.Length |
| | | } |
| | | ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port) |
| | | } |
| | | ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port) |
| | | |
| | | conn, err := plc.NewModbusConnection(ipAddr) |
| | | if err != nil { |
| | |
| | | if pc.FieldName == fieldType { |
| | | startAddress = pc.StartAddress |
| | | } |
| | | ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port) |
| | | } |
| | | ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port) |
| | | |
| | | conn, err := plc.NewModbusConnection(ipAddr) |
| | | if err != nil { |
| | |
| | | if err != nil { |
| | | return nil, ecode.DBErr |
| | | } |
| | | return slf.GetTask() //当前订单已完全,继续查询未完成订单 |
| | | return slf.GetTask() //当前订单已完成,继续查询未完成订单 |
| | | } |
| | | if err != nil { |
| | | return nil, ecode.DBErr |
| | |
| | | func (slf TaskService) GetOrderByWorkOrderId(workOrderId string) (order *model.Order, err error) { |
| | | return model.NewOrderSearch(nil).SetWorkOrderId(workOrderId).First() |
| | | } |
| | | |
| | | // GetComingTask 获取时间未到的最早任务 |
| | | func (slf TaskService) GetComingTask() (workOrder *model.Order, err error) { |
| | | nowTs := time.Now().Unix() |
| | | orderSearch := model.NewOrderSearch(nil) |
| | | return orderSearch.SetOrder("created_at asc"). |
| | | SetStartTimeMin(nowTs). |
| | | SetStatus(model.OrderStatusUnFinished).First() |
| | | } |