49 lines
1.9 KiB
JavaScript
49 lines
1.9 KiB
JavaScript
// Fetch the properties for the given sensor
|
|
|
|
import * as mongo from "../databases/mongo.js"
|
|
import {returnOnError} from "../utilities/reporterror.js"
|
|
import checkParams from "../utilities/checkparams.js"
|
|
|
|
let readProperties = mongo.readProperties
|
|
let readChipData = mongo.readChipData
|
|
|
|
// Read properties for sensorid and properties for all other sensors on same location
|
|
export const getOneProperty = async (params) => {
|
|
let properties = {err: null, props: {}, chip: {}}
|
|
let {opts, err} = checkParams(params, {mandatory:[{name:'sensorid', type: 'int'}], optional:[]})
|
|
if (err) {
|
|
return returnOnError(properties, err, getOneProperty.name)
|
|
}
|
|
// read 'chip'-data (special for noise sensors)
|
|
const chipdata = await readChipData(opts.sensorid)
|
|
if (chipdata.err == undefined) {
|
|
properties.chip = chipdata
|
|
}
|
|
let sensorEntries = [];
|
|
try {
|
|
let pp = await readProperties({sid: opts.sensorid}); // read for given sensorID
|
|
if ((pp.properties == null) || (pp.error)) {
|
|
return returnOnError(properties, 'NOPROPSREAD', getOneProperty.name, opts.sensorid)
|
|
}
|
|
// now find sensors with same location
|
|
let query = {"location.0.id": pp.properties.location[0].id}
|
|
let others = await readProperties(query)
|
|
if (others.err) {
|
|
return returnOnError(properties, 'NOPROPSREAD', getOneProperty.name, others.errortext)
|
|
}
|
|
if (others.properties.length > 0) {
|
|
for (const x of others.properties) {
|
|
if(x._id === pp.properties._id) {
|
|
continue
|
|
}
|
|
sensorEntries.push({name: x.name[0].name, sid: x._id})
|
|
}
|
|
}
|
|
properties.props = pp.properties
|
|
properties.props.othersensors = sensorEntries;
|
|
} catch (e) {
|
|
return returnOnError(properties, e, getOneProperty.name)
|
|
}
|
|
return properties
|
|
}
|