// 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 checkParams from "../utilities/checkparams.js" const mockdb = false let readProperties if (mockdb) { readProperties = mock.readProperties } else { readProperties = mongo.readProperties } // Read properties for sensorid and properties for all other sensors on same location 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}); // 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 }