job handling

main
alex 2023-10-09 23:16:38 +02:00
parent aa85119dad
commit 38caf71144
7 changed files with 128 additions and 9 deletions

View File

@ -48,17 +48,17 @@ func IsRobotInList(robotId string) bool {
return false
}
func GetRobotByName(robotId string) structs.Robot {
func GetRobotByName(robotName string) *structs.Robot {
rMu.RLock()
defer rMu.RUnlock()
for _, r := range robots {
if r.Id == robotId {
return *r
if r.Name == robotName {
return r
}
}
return structs.Robot{}
return nil
}
func RemoveRobotById(robotId string) {

View File

@ -10,6 +10,7 @@ type Robot struct {
Address string `gorm:"-"`
CurrentJobId string
CurrentTask string
LastTaskAt time.Time `gorm:"-"`
ConnectedAt time.Time `gorm:"-"`
CreatedAt time.Time
}
@ -48,3 +49,7 @@ type GetRobotsResponse struct {
type RobotIdParams struct {
RobotId string
}
type RobotNameParams struct {
RobotName string
}

View File

@ -1,9 +1,10 @@
package control
import (
"encoding/json"
"fmt"
"jannex/robot-control-manager/modules/cache"
"jannex/robot-control-manager/modules/structs"
"time"
"git.ex.umbach.dev/Alex/roese-utils/rsutils"
"github.com/gofiber/fiber/v2"
@ -30,6 +31,10 @@ func ControlRex(c *fiber.Ctx) error {
// responses:
// "200":
// description: Control Rex
// "400":
// description: Invalid request body
// "422":
// description: Robot not found
log.Info().Msgf("body %v", string(c.Body()))
@ -47,7 +52,94 @@ func ControlRex(c *fiber.Ctx) error {
fmt.Printf("y-value: %d\n", *body.Task.Y)
}
time.Sleep(10 * time.Second)
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, &params); 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)
}

View File

@ -27,6 +27,8 @@ func SetPermitJoin(c *fiber.Ctx) error {
// responses:
// "200":
// description: Permit join set
// "400":
// description: Invalid request body
var params structs.PermitJoinParam

View File

@ -36,6 +36,10 @@ func FirstRequest(c *fiber.Ctx) error {
// description: Robot identified
// schema:
// "$ref": "#/definitions/StatusResponse"
// "400":
// description: Invalid request body
// "403":
// description: Permit join is enabled
var body structs.FirstRequestBody
@ -118,6 +122,8 @@ func AuthorizeRobot(c *fiber.Ctx) error {
// responses:
// "200":
// description: Robot authorized
// "400":
// description: Invalid robot id
// "422":
// description: Robot not found

View File

@ -23,6 +23,7 @@ func SetupRoutes(app *fiber.App) {
c := v1.Group("/control")
c.Post("/0", control.ControlRex)
c.Post("/0/finish/:robotName", control.FinishControlRex)
pj := v1.Group("/permitjoin")
pj.Post("/:enabled", permitjoin.SetPermitJoin)

View File

@ -2,6 +2,7 @@
from flask import Flask, request, jsonify
import requests
import time
robot_control_server_url = 'http://localhost:50055/v1'
@ -9,6 +10,7 @@ class RexRobot:
def __init__(self):
self.id = "B24"
self.version = "0.0.1"
self.currentJobId = ""
# connecting with robot server
print("connecting with robot server")
@ -20,13 +22,24 @@ class RexRobot:
print("permit join disabled")
exit(1) # esp should here restart
RexRobot()
def setCurrentJobId(self, jobId):
self.currentJobId = jobId
rex = RexRobot()
app = Flask(__name__)
@app.route('/api/control', methods=['POST'])
@app.route('/api/v1/control', methods=['POST'])
def hello():
print("Hallo, Welt!")
body = request.get_json()
print("controlling robot", body)
time.sleep(15)
print("robot controlled")
return jsonify({'status': 'ok'})
if __name__ == '__main__':