146 lines
3.3 KiB
Go
146 lines
3.3 KiB
Go
package control
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"jannex/robot-control-manager/modules/cache"
|
|
"jannex/robot-control-manager/modules/structs"
|
|
|
|
"git.ex.umbach.dev/Alex/roese-utils/rsutils"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func ControlRex(c *fiber.Ctx) error {
|
|
// swagger:operation POST /control/0 control controlRex
|
|
// ---
|
|
// summary: Control Rex.
|
|
// description: |
|
|
// This is used to control Rex.
|
|
// consumes:
|
|
// - application/json
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - in: body
|
|
// name: body
|
|
// description: Control Rex body.
|
|
// required: true
|
|
// schema:
|
|
// "$ref": "#/definitions/ControlBody"
|
|
// responses:
|
|
// "200":
|
|
// description: Control Rex
|
|
// "400":
|
|
// description: Invalid request body
|
|
// "422":
|
|
// description: Robot not found
|
|
|
|
log.Info().Msgf("body %v", string(c.Body()))
|
|
|
|
var body structs.ControlBody
|
|
|
|
if err := rsutils.BodyParserHelper(c, &body); err != nil {
|
|
return c.SendStatus(fiber.StatusBadRequest)
|
|
}
|
|
|
|
log.Info().Msgf("ControlRex: %v", body)
|
|
|
|
if body.Task.Y == nil {
|
|
fmt.Println("y not sent")
|
|
} else {
|
|
fmt.Printf("y-value: %d\n", *body.Task.Y)
|
|
}
|
|
|
|
robot := cache.GetRobotByName(body.RobotName)
|
|
|
|
if robot.Id == "" {
|
|
return c.SendStatus(fiber.StatusUnprocessableEntity)
|
|
}
|
|
|
|
log.Info().Msgf("robot %v", robot)
|
|
|
|
// wait until the robot finished the job
|
|
|
|
for robot.CurrentJobId != "" {
|
|
robot.CurrentJobId = body.JobId
|
|
log.Debug().Msgf("robot is free %v", body.JobId)
|
|
|
|
controlRexRequest(robot.Address, body)
|
|
break
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
}
|
|
|
|
func controlRexRequest(address string, body any) {
|
|
a := fiber.AcquireAgent()
|
|
|
|
// TODO: port only for testing - remove it
|
|
url := "http://" + address + ":5000" + "/api/v1/control"
|
|
|
|
req := a.Request()
|
|
req.Header.SetMethod(fiber.MethodPost)
|
|
req.SetRequestURI(url)
|
|
req.Header.SetContentType("application/json")
|
|
|
|
log.Info().Msgf("url %s", url)
|
|
|
|
reqestBodyBytes, err := json.Marshal(body)
|
|
|
|
if err != nil {
|
|
log.Error().Msgf("Failed to marshal request body, err: %s", err)
|
|
return
|
|
}
|
|
|
|
req.SetBody(reqestBodyBytes)
|
|
|
|
if err := a.Parse(); err != nil {
|
|
log.Error().Msgf("Failed to parse request, err: %s", err)
|
|
return
|
|
}
|
|
|
|
code, body, _ := a.Bytes()
|
|
|
|
log.Info().Msgf("code %v body %v", code, body)
|
|
}
|
|
|
|
func FinishControlRex(c *fiber.Ctx) error {
|
|
// swagger:operation POST /control/0/finish/{robotName} control finishControlRex
|
|
// ---
|
|
// summary: Finish control Rex.
|
|
// description: |
|
|
// This is used to finish control Rex.
|
|
// consumes:
|
|
// - application/json
|
|
// produces:
|
|
// - application/json
|
|
// responses:
|
|
// "200":
|
|
// description: Finish control Rex
|
|
// "400":
|
|
// description: Invalid robot name
|
|
// "422":
|
|
// description: Robot not found
|
|
|
|
var params structs.RobotNameParams
|
|
|
|
if err := rsutils.ParamsParserHelper(c, ¶ms); err != nil {
|
|
return c.SendStatus(fiber.StatusBadRequest)
|
|
}
|
|
|
|
log.Debug().Msgf("before finish control robot %v", cache.GetRobotByName(params.RobotName).CurrentJobId)
|
|
|
|
robot := cache.GetRobotByName(params.RobotName)
|
|
|
|
if robot.Id == "" {
|
|
return c.SendStatus(fiber.StatusUnprocessableEntity)
|
|
}
|
|
|
|
robot.CurrentJobId = ""
|
|
|
|
log.Debug().Msgf("finish control robot %v", cache.GetRobotByName(params.RobotName).CurrentJobId)
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
}
|