SpaceAPI: inline sensors

This commit is contained in:
He4eT 2025-05-23 00:19:13 +02:00
commit c833fa9c08

View file

@ -192,7 +192,8 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
.map(pickFields(fields))
);
const temperatureSensors = await getSensors(
const sensors = {
temperature: await getSensors(
'api::temperature-sensor.temperature-sensor',
[
'value',
@ -201,10 +202,9 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
'name',
'description',
'lastchange',
],
);
const carbondioxideSensors = await getSensors(
]
),
carbondioxide: await getSensors(
'api::carbondioxide-sensor.carbondioxide-sensor',
[
'value',
@ -213,10 +213,9 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
'name',
'description',
'lastchange',
],
);
const doorLockedSensors = await getSensors(
]
),
door_locked: await getSensors(
'api::door-locked-sensor.door-locked-sensor',
[
'value',
@ -224,10 +223,9 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
'name',
'description',
'lastchange',
],
);
const barometerSensors = await getSensors(
]
),
barometer: await getSensors(
'api::barometer-sensor.barometer-sensor',
[
'value',
@ -236,26 +234,24 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
'name',
'description',
'lastchange',
],
);
const radiationSensors = await (async () => {
]
),
radiation: await (async () => {
const types = [
'alpha',
'beta',
'gamma',
'beta_gamma',
]
];
const sensors = await getSensorsRaw(
'api::radiation-sensor.radiation-sensor',
)
'api::radiation-sensor.radiation-sensor'
);
const draft = Object.fromEntries(types.map((type) => [
type,
sensors
.filter((sensor: { type: string }) =>
sensor.type === type)
.filter((sensor: { type: string; }) => sensor.type === type)
.map(pickFields([
'value',
'unit',
@ -266,12 +262,11 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
'description',
'lastchange',
]))
]))
]));
return pickFields(types)(draft)
})()
const humiditySensors = (await getSensors(
return pickFields(types)(draft);
})(),
humidity: (await getSensors(
'api::humidity-sensor.humidity-sensor',
[
'value',
@ -280,16 +275,15 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
'name',
'description',
'lastchange',
],
)).map((sensor: { unit: string }) => {
]
)).map((sensor: { unit: string; }) => {
const { unit, ...rest } = sensor;
return {
...rest,
unit: unit === 'percents' ? '%' : unit,
};
});
const beverageSupplySensors = await getSensors(
}),
beverage_supply: await getSensors(
'api::beverage-supply.beverage-supply',
[
'value',
@ -298,10 +292,9 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
'name',
'description',
'lastchange',
],
);
const powerConsumptionSensors = await getSensors(
]
),
power_consumption: await getSensors(
'api::power-consumption-sensor.power-consumption-sensor',
[
'value',
@ -310,10 +303,9 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
'name',
'description',
'lastchange',
],
);
const powerGenerationSensors = await getSensors(
]
),
power_generation: await getSensors(
'api::power-generation-sensor.power-generation-sensor',
[
'value',
@ -322,10 +314,9 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
'name',
'description',
'lastchange',
],
);
const windSensors = (await getSensors(
]
),
wind: (await getSensors(
'api::wind-sensor.wind-sensor',
[
'properties',
@ -339,8 +330,8 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
'properties.gust',
'properties.direction',
'properties.elevation',
],
)).map((sensor: { properties: { bits_per_second: number, packets_per_second: number } }) => {
]
)).map((sensor: { properties: { bits_per_second: number; packets_per_second: number; }; }) => {
const { properties, ...rest } = sensor;
const propertiesEntries = Object.entries(pickFields([
@ -361,9 +352,8 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
: { properties: Object.fromEntries(propertiesEntries) }
),
};
});
const networkConnectionsSensors = (await getSensors(
}),
network_connections: (await getSensors(
'api::network-connections-sensor.network-connections-sensor',
[
'location',
@ -374,8 +364,8 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
],
[
'machines',
],
)).map((sensor: { machines: Array<{ name?: string, mac?: string }> }) => {
]
)).map((sensor: { machines: Array<{ name?: string; mac?: string; }>; }) => {
const { machines, ...rest } = sensor;
return {
...rest,
@ -383,9 +373,8 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
? {}
: { machines: machines.map(pickFields(['name', 'mac'])) }),
};
});
const accountBalanceSensors = await getSensors(
}),
account_balance: await getSensors(
'api::account-balance-sensor.account-balance-sensor',
[
'value',
@ -394,10 +383,9 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
'name',
'description',
'lastchange',
],
);
const totalMemberCountSensors = await getSensors(
]
),
total_member_count: await getSensors(
'api::total-member-count-sensor.total-member-count-sensor',
[
'value',
@ -405,10 +393,9 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
'name',
'description',
'lastchange',
],
);
const peopleNowPresentSensors = (await getSensors(
]
),
people_now_present: (await getSensors(
'api::people-now-present-sensor.people-now-present-sensor',
[
'value',
@ -420,8 +407,8 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
],
[
'names',
],
)).map((sensor: { names: Array<{ name: string }> }) => {
]
)).map((sensor: { names: Array<{ name: string; }>; }) => {
const { names, ...rest } = sensor;
return {
...rest,
@ -429,9 +416,8 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
? {}
: { names: names.map((x) => x.name) }),
};
});
const networkTrafficSensors = (await getSensors(
}),
network_traffic: (await getSensors(
'api::network-traffic-sensor.network-traffic-sensor',
[
'properties',
@ -445,8 +431,8 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
[
'properties.bits_per_second',
'properties.packets_per_second',
],
)).map((sensor: { properties: { bits_per_second: number, packets_per_second: number } }) => {
]
)).map((sensor: { properties: { bits_per_second: number; packets_per_second: number; }; }) => {
const { properties, ...rest } = sensor;
const propertiesEntries = Object.entries(pickFields([
@ -465,24 +451,7 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
: { 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))) {