diff --git a/modules/robot/robot.go b/modules/robot/robot.go index 21d08bd..dd25df1 100644 --- a/modules/robot/robot.go +++ b/modules/robot/robot.go @@ -124,10 +124,11 @@ func ProcessJobTask(r *structs.Robot, jobId string, jobName string) { func UpdateRobotCurrentJob(r *structs.Robot, jobId string, jobName string) { r.JobMutex.Lock() defer r.JobMutex.Unlock() - - if r.CurrentJobId == jobId { - return - } + /* + if r.CurrentJobId == jobId { + log.Info().Msgf("Robot %s current job id already set to %s", r.Name, jobId) + return + } */ r.CurrentJobId = jobId r.CurrentJobName = jobName @@ -186,7 +187,7 @@ func RemoveJobFromJobsWaitingList(r *structs.Robot, jobId string) { } } -func CountDownJobsWaiting(r *structs.Robot, jobId string) { +func CountDownJobsWaiting(r *structs.Robot) { r.JobMutex.Lock() defer r.JobMutex.Unlock() diff --git a/modules/utils/globals.go b/modules/utils/globals.go index 78424a8..0061c18 100644 --- a/modules/utils/globals.go +++ b/modules/utils/globals.go @@ -18,6 +18,7 @@ const ( RobotStatusConnecting = 3 RobotStatusError = 4 RobotStatusOffline = 5 + RobotStatusWaiting = 6 ) const ( diff --git a/public/swagger/swagger.json b/public/swagger/swagger.json index 819ae03..5742b2f 100644 --- a/public/swagger/swagger.json +++ b/public/swagger/swagger.json @@ -325,7 +325,7 @@ { "description": "Robot id.", "name": "robotId", - "in": "path", + "in": "params", "required": true, "schema": { "type": "string" @@ -342,6 +342,40 @@ } } }, + "/robot/fuj/{robotId}": { + "patch": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "robot" + ], + "summary": "Free up job from robot.", + "operationId": "robotFreeUpJob", + "parameters": [ + { + "description": "Robot id.", + "name": "robotId", + "in": "params", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "Job freed up" + }, + "400": { + "description": "Invalid robot id" + } + } + } + }, "/robot/{robotId}": { "delete": { "consumes": [ @@ -359,7 +393,7 @@ { "description": "Robot id.", "name": "robotId", - "in": "path", + "in": "params", "required": true, "schema": { "type": "string" @@ -513,7 +547,8 @@ "type": "object", "properties": { "Status": { - "type": "string" + "type": "integer", + "format": "int64" } }, "x-go-package": "jannex/robot-control-manager/modules/structs" diff --git a/routers/api/v1/control/control.go b/routers/api/v1/control/control.go index cd1a98c..f49dd6f 100644 --- a/routers/api/v1/control/control.go +++ b/routers/api/v1/control/control.go @@ -61,7 +61,9 @@ func ControlRex(c *fiber.Ctx) error { return c.JSON(structs.StatusResponse{Status: utils.ResponseStatusOffline}) } - if r.Status != utils.RobotStatusIdle && r.Status != utils.RobotStatusRunning { + if r.Status != utils.RobotStatusIdle && + r.Status != utils.RobotStatusRunning && + r.Status != utils.RobotStatusWaiting { return c.JSON(structs.StatusResponse{Status: utils.ResponseStatusError}) } @@ -74,7 +76,7 @@ func ControlRex(c *fiber.Ctx) error { return c.JSON(structs.StatusResponse{Status: utils.ResponseStatusError}) } - robot.UpdateRobotStatus(r, utils.RobotStatusIdle) + robot.UpdateRobotStatus(r, utils.RobotStatusWaiting) return c.JSON(structs.StatusResponse{Status: utils.ResponseStatusOk}) } @@ -109,8 +111,10 @@ func FinishControlRex(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusUnprocessableEntity) } - robot.CountDownJobsWaiting(r, body.JobId) robot.UpdateRobotCurrentJob(r, "", "") + robot.CountDownJobsWaiting(r) + + logger.AddRobotLog(rslogger.LogTypeInfo, utils.RobotTypeRex, r.Id, "Finish control robot") return c.SendStatus(fiber.StatusOK) } diff --git a/routers/api/v1/permitjoin/permitjoin.go b/routers/api/v1/permitjoin/permitjoin.go index ff3d76a..0272be7 100644 --- a/routers/api/v1/permitjoin/permitjoin.go +++ b/routers/api/v1/permitjoin/permitjoin.go @@ -19,7 +19,7 @@ func SetPermitJoin(c *fiber.Ctx) error { // description: | // This is used to enable or disable permit join. // parameters: - // - in: path + // - in: params // name: enabled // description: Enable or disable permit join. 0 = disable, 1 = enable. // required: true diff --git a/routers/api/v1/robot/robot.go b/routers/api/v1/robot/robot.go index 19f295e..68ac048 100644 --- a/routers/api/v1/robot/robot.go +++ b/routers/api/v1/robot/robot.go @@ -54,7 +54,7 @@ func FirstRequest(c *fiber.Ctx) error { database.DB.First(&foundRobot, "id = ?", body.Id) if !cache.IsPermitJoinEnabled() && foundRobot.Id == "" { - logger.AddSystemLog("Unauthorized robot tried to connect with id %v and type %v", body.Id, utils.GetRobotTypeString(body.Type)) + logger.AddSystemLog(rslogger.LogTypeWarning, "Unauthorized robot tried to connect with id %v and type %v", body.Id, utils.GetRobotTypeString(body.Type)) return c.SendStatus(fiber.StatusForbidden) } @@ -85,7 +85,7 @@ func FirstRequest(c *fiber.Ctx) error { }, }) - logger.AddSystemLog("Unauthorized robot connected with id %v and type %v", body.Id, utils.GetRobotTypeString(body.Type)) + logger.AddSystemLog(rslogger.LogTypeInfo, "Unauthorized robot connected with id %v and type %v", body.Id, utils.GetRobotTypeString(body.Type)) } else { newRobot := structs.Robot{ Id: body.Id, @@ -179,7 +179,7 @@ func AuthorizeRobot(c *fiber.Ctx) error { } if !cache.IsUnauthorizedRobotInList(params.RobotId) { - logger.AddSystemLog("Unauthorized robot with id %v not found", params.RobotId) + logger.AddSystemLog(rslogger.LogTypeWarning, "Unauthorized robot with id %v not found", params.RobotId) return c.SendStatus(fiber.StatusUnprocessableEntity) } @@ -208,7 +208,7 @@ func AuthorizeRobot(c *fiber.Ctx) error { addRobotSSEMessage(&newRobot) - logger.AddSystemLog("Robot authorized with id %v and type %v", params.RobotId, utils.GetRobotTypeString(unauthorizedRobot.Type)) + logger.AddSystemLog(rslogger.LogTypeInfo, "Robot authorized with id %v and type %v", params.RobotId, utils.GetRobotTypeString(unauthorizedRobot.Type)) return c.SendStatus(fiber.StatusOK) } @@ -222,7 +222,7 @@ func DeleteRobot(c *fiber.Ctx) error { // produces: // - application/json // parameters: - // - in: path + // - in: params // name: robotId // description: Robot id. // required: true @@ -254,7 +254,7 @@ func DeleteRobot(c *fiber.Ctx) error { }, }) - logger.AddSystemLog("Robot deleted with id %v", params.RobotId) + logger.AddSystemLog(rslogger.LogTypeInfo, "Robot deleted with id %v", params.RobotId) return c.SendStatus(fiber.StatusOK) } @@ -268,7 +268,7 @@ func DenyUnauthorizedRobot(c *fiber.Ctx) error { // produces: // - application/json // parameters: - // - in: path + // - in: params // name: robotId // description: Robot id. // required: true @@ -299,7 +299,7 @@ func DenyUnauthorizedRobot(c *fiber.Ctx) error { }, }) - logger.AddSystemLog("Unauthorized robot denied with id %v", params.RobotId) + logger.AddSystemLog(rslogger.LogTypeInfo, "Unauthorized robot denied with id %v", params.RobotId) return c.SendStatus(fiber.StatusOK) } @@ -333,15 +333,10 @@ func UpdateRobot(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusBadRequest) } - if !cache.IsRobotInList(body.RobotId) { - logger.AddSystemLog("Robot with id %v not found", body.RobotId) - - return c.SendStatus(fiber.StatusUnprocessableEntity) - } - foundRobot := cache.GetRobotById(body.RobotId) if foundRobot == nil || cache.IsRobotNameInList(body.RobotName) { + logger.AddSystemLog(rslogger.LogTypeInfo, "Robot with id %v not found", body.RobotId) return c.SendStatus(fiber.StatusUnprocessableEntity) } @@ -356,7 +351,53 @@ func UpdateRobot(c *fiber.Ctx) error { Body: body, }) - logger.AddSystemLog("Robot with id %v name changed to %v", body.RobotId, body.RobotName) + logger.AddSystemLog(rslogger.LogTypeInfo, "Robot with id %v name changed to %v", body.RobotId, body.RobotName) + + return c.SendStatus(fiber.StatusOK) +} + +// This will be used to free up a job from a robot if it is stuck or job was not correctly finished +func FreeUpJob(c *fiber.Ctx) error { + // swagger:operation PATCH /robot/fuj/{robotId} robot robotFreeUpJob + // --- + // summary: Free up job from robot. + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - in: params + // name: robotId + // description: Robot id. + // required: true + // schema: + // type: string + // responses: + // "200": + // description: Job freed up + // "400": + // description: Invalid request body + // "422": + // description: Invalid robot id + + var params structs.RobotIdParams + + if err := rsutils.ParamsParserHelper(c, ¶ms); err != nil { + return c.SendStatus(fiber.StatusBadRequest) + } + + foundRobot := cache.GetRobotById(params.RobotId) + + if foundRobot == nil { + logger.AddSystemLog(rslogger.LogTypeWarning, "Robot %v not found. Cannot free ub job.", params.RobotId) + return c.SendStatus(fiber.StatusUnprocessableEntity) + } + + robot.UpdateRobotCurrentJob(foundRobot, "", "") + robot.UpdateRobotStatus(foundRobot, utils.RobotStatusIdle) + robot.CountDownJobsWaiting(foundRobot) + + logger.AddSystemLog(rslogger.LogTypeInfo, "Job freed up from robot %s", params.RobotId) return c.SendStatus(fiber.StatusOK) } diff --git a/routers/router/router.go b/routers/router/router.go index 87b836a..dc53bb7 100644 --- a/routers/router/router.go +++ b/routers/router/router.go @@ -19,6 +19,7 @@ func SetupRoutes(app *fiber.App) { r.Delete("/:robotId", robot.DeleteRobot) r.Delete("/deny/:robotId", robot.DenyUnauthorizedRobot) r.Patch("/", robot.UpdateRobot) + r.Patch("/fuj/:robotId", robot.FreeUpJob) rs := v1.Group("/robots") rs.Get("/", robots.GetRobots)