Files
sensorapi/actions/getproperties.js
T
2023-03-08 16:17:02 +01:00

56 lines
1.9 KiB
JavaScript

// Fetch the properties for the given sensor
import * as mongo from "../databases/mongo.js"
import * as mock from "../mocks/mongo_mock.js"
import {returnOnError} from "../utilities/reporterror.js"
import * as ERR from "../utilities/errortexts.js"
import checkParams from "../utilities/checkparams.js"
const mockdb = false
let readProperties
if (mockdb) {
readProperties = mock.readProperties
} else {
readProperties = mongo.readProperties
}
export const getOneProperty = async (params) => {
let properties = {props: {}, err: null}
let {opts, err} = checkParams(params, {mandatory:[{name:'sensorid', type: 'int'}], optional:[]})
if (err) {
return returnOnError(properties, err, getOneProperty.name)
}
let sensorEntries = [];
try {
let pp = await readProperties({sid: opts.sensorid});
if ((pp.values == null) || (pp.error)) {
return returnOnError(properties, ERR.NOPROPSREAD.replace('xx', opts.sensorid), getOneProperty.name)
}
// now find sensors with same location
let query
if (pp.values.location_id !== undefined) {
query = {location_id: pp.values.location_id}
} else {
query = {id: pp.values.id}
}
let others = await readProperties(query)
if (others.error) {
return returnOnError(properties, ERR.NOPROPSREAD.replace('xx',others.errortext), getOneProperty.name)
}
if (others.values.length > 0) {
for (const x of others.values) {
if(x.name[0].name === undefined) {
sensorEntries.push({name: x.name, sid: x._id})
} else {
sensorEntries.push({name: x.name[0].name, sid: x._id})
}
}
}
properties.props = pp.values
properties.props.othersensors = sensorEntries;
} catch (e) {
return returnOnError(properties, e, getOneProperty.name)
}
return properties
}