refacto & cleanup of toplevel functions, use @p42/p42Modules, test microframework

This commit is contained in:
STEINNI
2026-06-14 17:39:52 +00:00
parent c399f9ddb4
commit 7435d96135
11 changed files with 427 additions and 108 deletions
+3 -3
View File
@@ -1,5 +1,5 @@
import { AccesRights } from '../accesRights.js'
import { createMysqlPool } from '../mysqlClient.js'
import { MySQLClient } from '@p42/p42modules'
import { SimRepository } from './simRepository.js'
import { ArenaGroom } from './arenaGroom.js'
import { MaestroState } from './orchestrationState.js'
@@ -61,8 +61,8 @@ export class maestroServer {
console.error('[Maestro] Missing mysql config')
return(false)
}
this.db = await createMysqlPool(mysqlCfg)
this.simRepo = new SimRepository(this.db, this.debug)
this.db = await MySQLClient.createPool(mysqlCfg)
this.simRepo = new SimRepository(this.db, MySQLClient.resolveDatabases(mysqlCfg), this.debug)
if(this.debug) console.log('[Maestro] MySQL pool ready')
return(true)
}
+42 -36
View File
@@ -1,4 +1,4 @@
import { mysqlExecute } from '../mysqlClient.js'
import { MySQLClient } from '@p42/p42modules'
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
@@ -6,46 +6,52 @@ export function isValidUuid(val) {
return(typeof(val) === 'string' && UUID_RE.test(val))
}
function parseGpsValues(raw) {
let v = raw
if(v == null) return(null)
if(typeof(v) === 'string') {
try { v = JSON.parse(v) }
catch { return(null) }
}
if(typeof(v) !== 'object') return(null)
const position = v.position
const speed = v.speed ?? v.vector
if(!position || !speed) return(null)
const axes = ['x', 'y', 'z']
for(const axis of axes) {
if(typeof(position[axis]) !== 'number' || typeof(speed[axis]) !== 'number') return(null)
}
return({
position: { x: position.x, y: position.y, z: position.z },
vector: { x: speed.x, y: speed.y, z: speed.z },
})
}
export class SimRepository {
constructor(dbPool, debug = false) {
constructor(dbPool, databases, debug = false) {
this.db = dbPool
this.guiDb = databases.guiDatabase
this.simDb = databases.simDatabase
this.debug = debug
}
#qualify(db, table) {
return(`\`${db}\`.${table}`)
}
#parseGpsValues(raw) {
let v = raw
if(v == null) return(null)
if(typeof(v) === 'string') {
try { v = JSON.parse(v) }
catch { return(null) }
}
if(typeof(v) !== 'object') return(null)
const position = v.position
const speed = v.speed ?? v.vector
if(!position || !speed) return(null)
const axes = ['x', 'y', 'z']
for(const axis of axes) {
if(typeof(position[axis]) !== 'number' || typeof(speed[axis]) !== 'number') return(null)
}
return({
position: { x: position.x, y: position.y, z: position.z },
vector: { x: speed.x, y: speed.y, z: speed.z },
})
}
async validateSimulationAccess(userUuid, simulationUuid, keyframeId) {
if(!isValidUuid(userUuid)) return({ ok: false, err: 'Invalid user UUID' })
if(!isValidUuid(simulationUuid)) return({ ok: false, err: 'Invalid simulation UUID' })
if(!isValidUuid(keyframeId)) return({ ok: false, err: 'Invalid keyframe ID' })
const rows = await mysqlExecute(this.db, `
const rows = await MySQLClient.poolExecute(this.db, `
SELECT s.sim_id,
BIN_TO_UUID(s.sim_uuid) AS sim_uuid,
BIN_TO_UUID(s.sim_root_kf_uuid) AS sim_root_kf_uuid
FROM p42SIM.simulations s
INNER JOIN p42GUI.simowners o ON o.own_sim_uuid = s.sim_uuid
INNER JOIN p42GUI.users u ON o.own_usr_id = u.usr_id
FROM ${this.#qualify(this.simDb, 'simulations')} s
INNER JOIN ${this.#qualify(this.guiDb, 'simowners')} o ON o.own_sim_uuid = s.sim_uuid
INNER JOIN ${this.#qualify(this.guiDb, 'users')} u ON o.own_usr_id = u.usr_id
WHERE u.usr_uuid = ?
AND s.sim_uuid = UUID_TO_BIN(?)
`, [userUuid, simulationUuid])
@@ -57,9 +63,9 @@ export class SimRepository {
return({ ok: false, err: 'Keyframe does not match simulation root keyframe' })
}
const kfRows = await mysqlExecute(this.db, `
const kfRows = await MySQLClient.poolExecute(this.db, `
SELECT ekf_uuid
FROM p42SIM.edited_keyframes
FROM ${this.#qualify(this.simDb, 'edited_keyframes')}
WHERE ekf_uuid = UUID_TO_BIN(?)
`, [keyframeId])
if(!kfRows.length) return({ ok: false, err: 'Keyframe not found' })
@@ -71,13 +77,13 @@ export class SimRepository {
if(!isValidUuid(userUuid)) return({ ok: false, err: 'Invalid user UUID' })
if(!isValidUuid(simulationUuid)) return({ ok: false, err: 'Invalid simulation UUID' })
const rows = await mysqlExecute(this.db, `
const rows = await MySQLClient.poolExecute(this.db, `
SELECT s.sim_id,
BIN_TO_UUID(s.sim_uuid) AS sim_uuid,
BIN_TO_UUID(s.sim_root_kf_uuid) AS sim_root_kf_uuid
FROM p42SIM.simulations s
INNER JOIN p42GUI.simowners o ON o.own_sim_uuid = s.sim_uuid
INNER JOIN p42GUI.users u ON o.own_usr_id = u.usr_id
FROM ${this.#qualify(this.simDb, 'simulations')} s
INNER JOIN ${this.#qualify(this.guiDb, 'simowners')} o ON o.own_sim_uuid = s.sim_uuid
INNER JOIN ${this.#qualify(this.guiDb, 'users')} u ON o.own_usr_id = u.usr_id
WHERE u.usr_uuid = ?
AND s.sim_uuid = UUID_TO_BIN(?)
`, [userUuid, simulationUuid])
@@ -87,9 +93,9 @@ export class SimRepository {
}
async loadKeyframeAgents(keyframeId) {
const rows = await mysqlExecute(this.db, `
const rows = await MySQLClient.poolExecute(this.db, `
SELECT BIN_TO_UUID(ekfs_agent_id) AS agent_id, ekfs_gps_values
FROM p42SIM.edited_kf_store
FROM ${this.#qualify(this.simDb, 'edited_kf_store')}
WHERE ekfs_ekf_uuid = UUID_TO_BIN(?)
`, [keyframeId])
@@ -97,7 +103,7 @@ export class SimRepository {
const errors = []
for(const row of rows) {
const parsed = parseGpsValues(row.ekfs_gps_values)
const parsed = this.#parseGpsValues(row.ekfs_gps_values)
if(!parsed) {
errors.push(`Invalid GPS values for agent ${row.agent_id}`)
continue