mirror of
https://github.com/He4eT/simple-spaceapi.git
synced 2026-05-05 01:47:24 +00:00
SpaceAPI: inline sensors
This commit is contained in:
parent
a8e56ce3e1
commit
c833fa9c08
1 changed files with 253 additions and 284 deletions
|
|
@ -192,297 +192,266 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
|
||||||
.map(pickFields(fields))
|
.map(pickFields(fields))
|
||||||
);
|
);
|
||||||
|
|
||||||
const temperatureSensors = await getSensors(
|
const sensors = {
|
||||||
'api::temperature-sensor.temperature-sensor',
|
temperature: await getSensors(
|
||||||
[
|
'api::temperature-sensor.temperature-sensor',
|
||||||
'value',
|
[
|
||||||
'unit',
|
|
||||||
'location',
|
|
||||||
'name',
|
|
||||||
'description',
|
|
||||||
'lastchange',
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
const carbondioxideSensors = await getSensors(
|
|
||||||
'api::carbondioxide-sensor.carbondioxide-sensor',
|
|
||||||
[
|
|
||||||
'value',
|
|
||||||
'unit',
|
|
||||||
'location',
|
|
||||||
'name',
|
|
||||||
'description',
|
|
||||||
'lastchange',
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
const doorLockedSensors = await getSensors(
|
|
||||||
'api::door-locked-sensor.door-locked-sensor',
|
|
||||||
[
|
|
||||||
'value',
|
|
||||||
'location',
|
|
||||||
'name',
|
|
||||||
'description',
|
|
||||||
'lastchange',
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
const barometerSensors = await getSensors(
|
|
||||||
'api::barometer-sensor.barometer-sensor',
|
|
||||||
[
|
|
||||||
'value',
|
|
||||||
'unit',
|
|
||||||
'location',
|
|
||||||
'name',
|
|
||||||
'description',
|
|
||||||
'lastchange',
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
const radiationSensors = await (async () => {
|
|
||||||
const types = [
|
|
||||||
'alpha',
|
|
||||||
'beta',
|
|
||||||
'gamma',
|
|
||||||
'beta_gamma',
|
|
||||||
]
|
|
||||||
|
|
||||||
const sensors = await getSensorsRaw(
|
|
||||||
'api::radiation-sensor.radiation-sensor',
|
|
||||||
)
|
|
||||||
|
|
||||||
const draft = Object.fromEntries(types.map((type) => [
|
|
||||||
type,
|
|
||||||
sensors
|
|
||||||
.filter((sensor: { type: string }) =>
|
|
||||||
sensor.type === type)
|
|
||||||
.map(pickFields([
|
|
||||||
'value',
|
'value',
|
||||||
'unit',
|
'unit',
|
||||||
'dead_time',
|
|
||||||
'conversion_factor',
|
|
||||||
'location',
|
'location',
|
||||||
'name',
|
'name',
|
||||||
'description',
|
'description',
|
||||||
'lastchange',
|
'lastchange',
|
||||||
]))
|
]
|
||||||
]))
|
),
|
||||||
|
carbondioxide: await getSensors(
|
||||||
|
'api::carbondioxide-sensor.carbondioxide-sensor',
|
||||||
|
[
|
||||||
|
'value',
|
||||||
|
'unit',
|
||||||
|
'location',
|
||||||
|
'name',
|
||||||
|
'description',
|
||||||
|
'lastchange',
|
||||||
|
]
|
||||||
|
),
|
||||||
|
door_locked: await getSensors(
|
||||||
|
'api::door-locked-sensor.door-locked-sensor',
|
||||||
|
[
|
||||||
|
'value',
|
||||||
|
'location',
|
||||||
|
'name',
|
||||||
|
'description',
|
||||||
|
'lastchange',
|
||||||
|
]
|
||||||
|
),
|
||||||
|
barometer: await getSensors(
|
||||||
|
'api::barometer-sensor.barometer-sensor',
|
||||||
|
[
|
||||||
|
'value',
|
||||||
|
'unit',
|
||||||
|
'location',
|
||||||
|
'name',
|
||||||
|
'description',
|
||||||
|
'lastchange',
|
||||||
|
]
|
||||||
|
),
|
||||||
|
radiation: await (async () => {
|
||||||
|
const types = [
|
||||||
|
'alpha',
|
||||||
|
'beta',
|
||||||
|
'gamma',
|
||||||
|
'beta_gamma',
|
||||||
|
];
|
||||||
|
|
||||||
return pickFields(types)(draft)
|
const sensors = await getSensorsRaw(
|
||||||
})()
|
'api::radiation-sensor.radiation-sensor'
|
||||||
|
);
|
||||||
|
|
||||||
const humiditySensors = (await getSensors(
|
const draft = Object.fromEntries(types.map((type) => [
|
||||||
'api::humidity-sensor.humidity-sensor',
|
type,
|
||||||
[
|
sensors
|
||||||
'value',
|
.filter((sensor: { type: string; }) => sensor.type === type)
|
||||||
'unit',
|
.map(pickFields([
|
||||||
'location',
|
'value',
|
||||||
'name',
|
'unit',
|
||||||
'description',
|
'dead_time',
|
||||||
'lastchange',
|
'conversion_factor',
|
||||||
],
|
'location',
|
||||||
)).map((sensor: { unit: string }) => {
|
'name',
|
||||||
const { unit, ...rest } = sensor;
|
'description',
|
||||||
return {
|
'lastchange',
|
||||||
...rest,
|
]))
|
||||||
unit: unit === 'percents' ? '%' : unit,
|
]));
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const beverageSupplySensors = await getSensors(
|
return pickFields(types)(draft);
|
||||||
'api::beverage-supply.beverage-supply',
|
})(),
|
||||||
[
|
humidity: (await getSensors(
|
||||||
'value',
|
'api::humidity-sensor.humidity-sensor',
|
||||||
'unit',
|
[
|
||||||
'location',
|
'value',
|
||||||
'name',
|
'unit',
|
||||||
'description',
|
'location',
|
||||||
'lastchange',
|
'name',
|
||||||
],
|
'description',
|
||||||
);
|
'lastchange',
|
||||||
|
]
|
||||||
|
)).map((sensor: { unit: string; }) => {
|
||||||
|
const { unit, ...rest } = sensor;
|
||||||
|
return {
|
||||||
|
...rest,
|
||||||
|
unit: unit === 'percents' ? '%' : unit,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
beverage_supply: await getSensors(
|
||||||
|
'api::beverage-supply.beverage-supply',
|
||||||
|
[
|
||||||
|
'value',
|
||||||
|
'unit',
|
||||||
|
'location',
|
||||||
|
'name',
|
||||||
|
'description',
|
||||||
|
'lastchange',
|
||||||
|
]
|
||||||
|
),
|
||||||
|
power_consumption: await getSensors(
|
||||||
|
'api::power-consumption-sensor.power-consumption-sensor',
|
||||||
|
[
|
||||||
|
'value',
|
||||||
|
'unit',
|
||||||
|
'location',
|
||||||
|
'name',
|
||||||
|
'description',
|
||||||
|
'lastchange',
|
||||||
|
]
|
||||||
|
),
|
||||||
|
power_generation: await getSensors(
|
||||||
|
'api::power-generation-sensor.power-generation-sensor',
|
||||||
|
[
|
||||||
|
'value',
|
||||||
|
'unit',
|
||||||
|
'location',
|
||||||
|
'name',
|
||||||
|
'description',
|
||||||
|
'lastchange',
|
||||||
|
]
|
||||||
|
),
|
||||||
|
wind: (await getSensors(
|
||||||
|
'api::wind-sensor.wind-sensor',
|
||||||
|
[
|
||||||
|
'properties',
|
||||||
|
'location',
|
||||||
|
'name',
|
||||||
|
'description',
|
||||||
|
'lastchange',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'properties.speed',
|
||||||
|
'properties.gust',
|
||||||
|
'properties.direction',
|
||||||
|
'properties.elevation',
|
||||||
|
]
|
||||||
|
)).map((sensor: { properties: { bits_per_second: number; packets_per_second: number; }; }) => {
|
||||||
|
const { properties, ...rest } = sensor;
|
||||||
|
|
||||||
const powerConsumptionSensors = await getSensors(
|
const propertiesEntries = Object.entries(pickFields([
|
||||||
'api::power-consumption-sensor.power-consumption-sensor',
|
'speed',
|
||||||
[
|
'gust',
|
||||||
'value',
|
'direction',
|
||||||
'unit',
|
'elevation',
|
||||||
'location',
|
])(properties))
|
||||||
'name',
|
.map(([k, { value, unit }]) => [k, {
|
||||||
'description',
|
value,
|
||||||
'lastchange',
|
unit: unit === 'degree' ? '°' : unit,
|
||||||
],
|
}]);
|
||||||
);
|
|
||||||
|
|
||||||
const powerGenerationSensors = await getSensors(
|
return {
|
||||||
'api::power-generation-sensor.power-generation-sensor',
|
...rest,
|
||||||
[
|
...(isEmpty(propertiesEntries)
|
||||||
'value',
|
? {}
|
||||||
'unit',
|
: { properties: Object.fromEntries(propertiesEntries) }
|
||||||
'location',
|
),
|
||||||
'name',
|
};
|
||||||
'description',
|
}),
|
||||||
'lastchange',
|
network_connections: (await getSensors(
|
||||||
],
|
'api::network-connections-sensor.network-connections-sensor',
|
||||||
);
|
[
|
||||||
|
'location',
|
||||||
|
'name',
|
||||||
|
'description',
|
||||||
|
'lastchange',
|
||||||
|
'machines',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'machines',
|
||||||
|
]
|
||||||
|
)).map((sensor: { machines: Array<{ name?: string; mac?: string; }>; }) => {
|
||||||
|
const { machines, ...rest } = sensor;
|
||||||
|
return {
|
||||||
|
...rest,
|
||||||
|
...(isEmpty(machines)
|
||||||
|
? {}
|
||||||
|
: { machines: machines.map(pickFields(['name', 'mac'])) }),
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
account_balance: await getSensors(
|
||||||
|
'api::account-balance-sensor.account-balance-sensor',
|
||||||
|
[
|
||||||
|
'value',
|
||||||
|
'unit',
|
||||||
|
'location',
|
||||||
|
'name',
|
||||||
|
'description',
|
||||||
|
'lastchange',
|
||||||
|
]
|
||||||
|
),
|
||||||
|
total_member_count: await getSensors(
|
||||||
|
'api::total-member-count-sensor.total-member-count-sensor',
|
||||||
|
[
|
||||||
|
'value',
|
||||||
|
'location',
|
||||||
|
'name',
|
||||||
|
'description',
|
||||||
|
'lastchange',
|
||||||
|
]
|
||||||
|
),
|
||||||
|
people_now_present: (await getSensors(
|
||||||
|
'api::people-now-present-sensor.people-now-present-sensor',
|
||||||
|
[
|
||||||
|
'value',
|
||||||
|
'location',
|
||||||
|
'name',
|
||||||
|
'names',
|
||||||
|
'description',
|
||||||
|
'lastchange',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'names',
|
||||||
|
]
|
||||||
|
)).map((sensor: { names: Array<{ name: string; }>; }) => {
|
||||||
|
const { names, ...rest } = sensor;
|
||||||
|
return {
|
||||||
|
...rest,
|
||||||
|
...(isEmpty(names)
|
||||||
|
? {}
|
||||||
|
: { names: names.map((x) => x.name) }),
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
network_traffic: (await getSensors(
|
||||||
|
'api::network-traffic-sensor.network-traffic-sensor',
|
||||||
|
[
|
||||||
|
'properties',
|
||||||
|
'value',
|
||||||
|
'location',
|
||||||
|
'name',
|
||||||
|
'names',
|
||||||
|
'description',
|
||||||
|
'lastchange',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'properties.bits_per_second',
|
||||||
|
'properties.packets_per_second',
|
||||||
|
]
|
||||||
|
)).map((sensor: { properties: { bits_per_second: number; packets_per_second: number; }; }) => {
|
||||||
|
const { properties, ...rest } = sensor;
|
||||||
|
|
||||||
const windSensors = (await getSensors(
|
const propertiesEntries = Object.entries(pickFields([
|
||||||
'api::wind-sensor.wind-sensor',
|
'bits_per_second',
|
||||||
[
|
'packets_per_second',
|
||||||
'properties',
|
])(properties))
|
||||||
'location',
|
.map(([k, v]) => [k, {
|
||||||
'name',
|
'bits_per_second': pickFields(['value', 'maximum']),
|
||||||
'description',
|
'packets_per_second': pickFields(['value']),
|
||||||
'lastchange',
|
}[k](v)]);
|
||||||
],
|
|
||||||
[
|
|
||||||
'properties.speed',
|
|
||||||
'properties.gust',
|
|
||||||
'properties.direction',
|
|
||||||
'properties.elevation',
|
|
||||||
],
|
|
||||||
)).map((sensor: { properties: { bits_per_second: number, packets_per_second: number } }) => {
|
|
||||||
const { properties, ...rest } = sensor;
|
|
||||||
|
|
||||||
const propertiesEntries = Object.entries(pickFields([
|
return {
|
||||||
'speed',
|
...rest,
|
||||||
'gust',
|
...(isEmpty(propertiesEntries)
|
||||||
'direction',
|
? {}
|
||||||
'elevation',
|
: { properties: Object.fromEntries(propertiesEntries) }
|
||||||
])(properties))
|
),
|
||||||
.map(([k, { value, unit }]) => [k, {
|
};
|
||||||
value,
|
}),
|
||||||
unit: unit === 'degree' ? '°' : unit,
|
|
||||||
}]);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...rest,
|
|
||||||
...(isEmpty(propertiesEntries)
|
|
||||||
? {}
|
|
||||||
: { properties: Object.fromEntries(propertiesEntries) }
|
|
||||||
),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const networkConnectionsSensors = (await getSensors(
|
|
||||||
'api::network-connections-sensor.network-connections-sensor',
|
|
||||||
[
|
|
||||||
'location',
|
|
||||||
'name',
|
|
||||||
'description',
|
|
||||||
'lastchange',
|
|
||||||
'machines',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'machines',
|
|
||||||
],
|
|
||||||
)).map((sensor: { machines: Array<{ name?: string, mac?: string }> }) => {
|
|
||||||
const { machines, ...rest } = sensor;
|
|
||||||
return {
|
|
||||||
...rest,
|
|
||||||
...(isEmpty(machines)
|
|
||||||
? {}
|
|
||||||
: { machines: machines.map(pickFields(['name', 'mac'])) }),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const accountBalanceSensors = await getSensors(
|
|
||||||
'api::account-balance-sensor.account-balance-sensor',
|
|
||||||
[
|
|
||||||
'value',
|
|
||||||
'unit',
|
|
||||||
'location',
|
|
||||||
'name',
|
|
||||||
'description',
|
|
||||||
'lastchange',
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
const totalMemberCountSensors = await getSensors(
|
|
||||||
'api::total-member-count-sensor.total-member-count-sensor',
|
|
||||||
[
|
|
||||||
'value',
|
|
||||||
'location',
|
|
||||||
'name',
|
|
||||||
'description',
|
|
||||||
'lastchange',
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
const peopleNowPresentSensors = (await getSensors(
|
|
||||||
'api::people-now-present-sensor.people-now-present-sensor',
|
|
||||||
[
|
|
||||||
'value',
|
|
||||||
'location',
|
|
||||||
'name',
|
|
||||||
'names',
|
|
||||||
'description',
|
|
||||||
'lastchange',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'names',
|
|
||||||
],
|
|
||||||
)).map((sensor: { names: Array<{ name: string }> }) => {
|
|
||||||
const { names, ...rest } = sensor;
|
|
||||||
return {
|
|
||||||
...rest,
|
|
||||||
...(isEmpty(names)
|
|
||||||
? {}
|
|
||||||
: { names: names.map((x) => x.name) }),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const networkTrafficSensors = (await getSensors(
|
|
||||||
'api::network-traffic-sensor.network-traffic-sensor',
|
|
||||||
[
|
|
||||||
'properties',
|
|
||||||
'value',
|
|
||||||
'location',
|
|
||||||
'name',
|
|
||||||
'names',
|
|
||||||
'description',
|
|
||||||
'lastchange',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'properties.bits_per_second',
|
|
||||||
'properties.packets_per_second',
|
|
||||||
],
|
|
||||||
)).map((sensor: { properties: { bits_per_second: number, packets_per_second: number } }) => {
|
|
||||||
const { properties, ...rest } = sensor;
|
|
||||||
|
|
||||||
const propertiesEntries = Object.entries(pickFields([
|
|
||||||
'bits_per_second',
|
|
||||||
'packets_per_second',
|
|
||||||
])(properties))
|
|
||||||
.map(([k, v]) => [k, {
|
|
||||||
'bits_per_second': pickFields(['value', 'maximum']),
|
|
||||||
'packets_per_second': pickFields(['value']),
|
|
||||||
}[k](v)]);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...rest,
|
|
||||||
...(isEmpty(propertiesEntries)
|
|
||||||
? {}
|
|
||||||
: { properties: Object.fromEntries(propertiesEntries) }
|
|
||||||
),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const sensors = {
|
|
||||||
temperature: temperatureSensors,
|
|
||||||
carbondioxide: carbondioxideSensors,
|
|
||||||
door_locked: doorLockedSensors,
|
|
||||||
barometer: barometerSensors,
|
|
||||||
radiation: radiationSensors,
|
|
||||||
humidity: humiditySensors,
|
|
||||||
beverage_supply: beverageSupplySensors,
|
|
||||||
power_consumption: powerConsumptionSensors,
|
|
||||||
power_generation: powerGenerationSensors,
|
|
||||||
wind: windSensors,
|
|
||||||
network_connections: networkConnectionsSensors,
|
|
||||||
account_balance: accountBalanceSensors,
|
|
||||||
total_member_count: totalMemberCountSensors,
|
|
||||||
people_now_present: peopleNowPresentSensors,
|
|
||||||
network_traffic: networkTrafficSensors,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (Object.entries(sensors).some(([_, sensor]) => !isEmpty(sensor))) {
|
if (Object.entries(sensors).some(([_, sensor]) => !isEmpty(sensor))) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue