@openhps/core
Version:
Open Hybrid Positioning System - Core component
93 lines • 2.59 kB
JavaScript
import { Absolute3DPosition } from '../data';
import { DataService } from './DataService';
/**
* The object service manages the data of objects that are currently being
* processed in the model and objects that need to be tracked.
*/
export class DataObjectService extends DataService {
constructor(dataServiceDriver) {
super(dataServiceDriver);
}
/**
* Insert a new data object
* @param {DataObject} object Data object to insert
* @returns {DataObject} Inserted object
*/
insertObject(object) {
return this.insert(object.uid, object);
}
insert(id, object) {
return new Promise((resolve, reject) => {
this.driver.insert(id, object).then(obj => {
resolve(obj);
}).catch(reject);
});
}
/**
* Find a data object by its display name
* @param {string} displayName Name to search for
* @returns {DataObject[]} Array of data objects that match the display name
*/
findByDisplayName(displayName) {
return this.findAll({
displayName
});
}
/**
* Find a data object by its current absolute position
* @param {AbsolutePosition} position Current absolute position
* @returns {DataObject[]} Array of data objects that match the position
*/
findByPosition(position) {
const vector = position.toVector3();
let filter;
if (position instanceof Absolute3DPosition) {
filter = {
'position.x': vector.x,
'position.y': vector.y,
'position.z': vector.z
};
} else {
filter = {
'position.x': vector.x,
'position.y': vector.y
};
}
return this.findAll(filter);
}
/**
* Find all data objects with a parent UID
* @param {string} parentUID string Parent UID
* @returns {DataObject[]} Array of data objects that match the parent UID
*/
findByParentUID(parentUID) {
return this.findAll({
parentUID
});
}
/**
* Find data objects created before a certain timestamp
* @param {number} timestamp Timestamp
* @returns {DataObject[]} Array of data objects before the specified timestamp
*/
findBefore(timestamp) {
return this._findTimestamp({
$lte: timestamp
});
}
/**
* Find data objects created after a certain timestamp
* @param {number} timestamp Timestamp
* @returns {DataObject[]} Array of data objects after the specified timestamp
*/
findAfter(timestamp) {
return this._findTimestamp({
$gte: timestamp
});
}
_findTimestamp(timestampFilter) {
return this.findAll({
createdTimestamp: timestampFilter
});
}
}