87 lines
1.4 KiB
Go
87 lines
1.4 KiB
Go
package cache
|
|
|
|
import (
|
|
"jannex/robot-control-manager/modules/structs"
|
|
"jannex/robot-control-manager/modules/utils"
|
|
"sync"
|
|
|
|
"git.ex.umbach.dev/Alex/roese-utils/rspagination"
|
|
)
|
|
|
|
var robots = make(map[string]*structs.Robot)
|
|
var rMu sync.RWMutex
|
|
|
|
func AddRobot(robot *structs.Robot) {
|
|
rMu.Lock()
|
|
defer rMu.Unlock()
|
|
|
|
robots[robot.Id] = robot
|
|
}
|
|
|
|
func AddRobots(newRobots []structs.Robot) {
|
|
rMu.Lock()
|
|
defer rMu.Unlock()
|
|
|
|
for _, r := range newRobots {
|
|
robots[r.Id] = &r
|
|
}
|
|
}
|
|
|
|
func GetRobots() map[string]*structs.Robot {
|
|
rMu.RLock()
|
|
defer rMu.RUnlock()
|
|
|
|
return robots
|
|
}
|
|
|
|
func GetAllRobots(query rspagination.PageQuery) structs.RobotsResponse {
|
|
rMu.RLock()
|
|
defer rMu.RUnlock()
|
|
|
|
var r []structs.Robot
|
|
|
|
for _, v := range robots {
|
|
r = append(r, *v)
|
|
}
|
|
|
|
start, end := rspagination.GetPage(len(r), query.Page, utils.RobotsPageLimit)
|
|
|
|
return structs.RobotsResponse{
|
|
Robots: r[start:end],
|
|
TotalPages: rspagination.CalculateTotalPages(len(r), utils.RobotsPageLimit),
|
|
}
|
|
}
|
|
|
|
func IsRobotInList(robotId string) bool {
|
|
rMu.RLock()
|
|
defer rMu.RUnlock()
|
|
|
|
for _, r := range robots {
|
|
if r.Id == robotId {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func GetRobotByName(robotName string) *structs.Robot {
|
|
rMu.RLock()
|
|
defer rMu.RUnlock()
|
|
|
|
for _, r := range robots {
|
|
if r.Name == robotName {
|
|
return r
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func RemoveRobotById(robotId string) {
|
|
rMu.Lock()
|
|
defer rMu.Unlock()
|
|
|
|
delete(robots, robotId)
|
|
}
|