Files
P42_UI/app/models/KeyframesModel.js

78 lines
2.5 KiB
JavaScript

class KeyframesModel extends WindozModel {
constructor() {
super()
this.ressource = '/keyframes'
}
async list(kfId, prevKFId='') {
let endpoint = {...app.config.api[this.ressource].list}
const filters = {}
//Caution, undefined is != NULL here, because prev_kfId ull means root, while undefined means no filtering
if((typeof(kfId) != 'undefined') && (kfId!=='')) filters['kfId'] = kfId
if((typeof(prevKFId) != 'undefined') && (prevKFId!=='')) filters['prevKFId'] = prevKFId
return (
this.request(endpoint.uri, endpoint.method, filters)
)
}
async getKeyframe(kfId){
let endpoint = {...app.config.api[this.ressource].get}
endpoint.uri = endpoint.uri.replace('{kfId}', kfId)
return (
this.request(endpoint.uri, endpoint.method, kfId)
)
}
async create(kfData, prevKFId) {
let endpoint = {...app.config.api[this.ressource].create}
return (
this.request(endpoint.uri, endpoint.method, kfData)
)
}
async rename(kfData, prevKFId) {
let endpoint = {...app.config.api[this.ressource].create}
return (
this.request(endpoint.uri, endpoint.method, kfData)
)
}
#normalizeAxisVector(vec) {
if(!vec || typeof(vec) !== 'object') return(null)
const axes = ['x', 'y', 'z']
const out = {}
for(const axis of axes) {
const n = Number(vec[axis])
if(!Number.isFinite(n)) return(null)
out[axis] = n
}
return(out)
}
async save(kfId, data) {
const kfData = Object.keys(data).map(aid => {
const { position, speed, ...storeValues} = data[aid].values
const gpsPosition = this.#normalizeAxisVector(position)
const gpsSpeed = this.#normalizeAxisVector(speed)
if(!gpsPosition || !gpsSpeed) {
throw(new Error(`Agent ${aid}: position and speed must be numeric vectors`))
}
return({
aid: aid,
type: data[aid].type,
storeValues: storeValues,
gpsValues: { position: gpsPosition, speed: gpsSpeed },
})
})
let endpoint = {...app.config.api[this.ressource].saveAgents}
endpoint.uri = endpoint.uri.replace('{kfId}', kfId)
return (
this.request(endpoint.uri, endpoint.method, kfData)
)
}
}
app.registerClass('KeyframesModel', KeyframesModel);