Skip to content

Commit

Permalink
feat: add methods to retrieve all meters, sensors, scales, notificati…
Browse files Browse the repository at this point in the history
…ons, indicator properties (#6929)
  • Loading branch information
AlCalzone committed Jun 17, 2024
1 parent a36ffe4 commit 123b9dc
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 45 deletions.
6 changes: 6 additions & 0 deletions packages/core/src/registries/Indicators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,9 @@ export function getIndicatorProperty<ID extends number>(
...property,
} satisfies IndicatorProperty as any;
}

/** Returns all defined indicator properties */
export function getAllIndicatorProperties(): readonly IndicatorProperty[] {
return Object.entries(indicatorProperties)
.map(([id, value]) => ({ id: parseInt(id, 10), ...value }));
}
17 changes: 17 additions & 0 deletions packages/core/src/registries/Meters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ export function getMeter<MeterType extends number>(
} satisfies Meter as any;
}

/** Returns all meter definitions including their scales */
export function getAllMeters(): readonly Meter[] {
return Object.entries(meters)
.map(([key, value]) => ({ key: parseInt(key, 10), ...value }));
}

export function getMeterName(meterType: number): string {
return getMeter(meterType)?.name
?? `UNKNOWN (${num2hex(meterType)})`;
Expand Down Expand Up @@ -175,3 +181,14 @@ export function getUnknownMeterScale(key: number): MeterScale {
label: `Unknown (${num2hex(key)})`,
};
}

/** Returns all scales of a given meter */
export function getAllMeterScales(
meterType: number,
): readonly MeterScale[] | undefined {
const meter = getMeter(meterType);
if (!meter) return;

return Object.entries(meter.scales)
.map(([key, value]) => ({ key: parseInt(key, 10), ...value }));
}
7 changes: 7 additions & 0 deletions packages/core/src/registries/Notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,13 @@ export function getNotificationName(type: number): string {
return notification?.name ?? `Unknown (${num2hex(type)})`;
}

/** Returns all defined notifications */
export function getAllNotifications(): readonly Notification[] {
return Object.keys(notifications).map((type) =>
getNotification(parseInt(type, 10))!
);
}

/** Returns a notification's event or state with the given value */
export function getNotificationValue(
notification: Notification,
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/registries/Scales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export interface Scale extends ScaleDefinition {

export type ScaleGroup = Record<number, ScaleDefinition>;

export interface NamedScaleGroup {
name: string;
scales: Record<number, ScaleDefinition>;
}

const namedScales = Object.freeze(
{
temperature: {
Expand Down Expand Up @@ -118,6 +123,12 @@ export function getNamedScaleGroup<
return scaleGroup;
}

/** Returns all defined named scale groups */
export function getAllNamedScaleGroups(): readonly NamedScaleGroup[] {
return Object.entries(namedScales)
.map(([name, scales]) => ({ name, scales }));
}

/** Returns a scale definition for a scale with known name and key */
export function getNamedScale<
Name extends keyof typeof namedScales,
Expand Down
107 changes: 62 additions & 45 deletions packages/core/src/registries/Sensors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface Sensor extends SensorDefinition {
readonly key: number;
}

function defineNamedScales<T extends Parameters<typeof getNamedScaleGroup>[0]>(
function useNamedScales<T extends Parameters<typeof getNamedScaleGroup>[0]>(
name: T,
): { scaleGroupName: T; scales: ReturnType<typeof getNamedScaleGroup<T>> } {
return { scaleGroupName: name, scales: getNamedScaleGroup(name) };
Expand All @@ -26,7 +26,7 @@ const sensors = Object.freeze(
{
[0x01]: {
label: "Air temperature",
...defineNamedScales("temperature"),
...useNamedScales("temperature"),
},
[0x02]: {
label: "General purpose",
Expand Down Expand Up @@ -68,7 +68,7 @@ const sensors = Object.freeze(
},
[0x05]: {
label: "Humidity",
...defineNamedScales("humidity"),
...useNamedScales("humidity"),
},
[0x06]: {
label: "Velocity",
Expand All @@ -85,15 +85,15 @@ const sensors = Object.freeze(
},
[0x07]: {
label: "Direction",
...defineNamedScales("direction"),
...useNamedScales("direction"),
},
[0x08]: {
label: "Atmospheric pressure",
...defineNamedScales("airPressure"),
...useNamedScales("airPressure"),
},
[0x09]: {
label: "Barometric pressure",
...defineNamedScales("airPressure"),
...useNamedScales("airPressure"),
},
[0x0a]: {
label: "Solar radiation",
Expand All @@ -106,7 +106,7 @@ const sensors = Object.freeze(
},
[0x0b]: {
label: "Dew point",
...defineNamedScales("temperature"),
...useNamedScales("temperature"),
},
[0x0c]: {
label: "Rain rate",
Expand Down Expand Up @@ -263,11 +263,11 @@ const sensors = Object.freeze(
},
[0x17]: {
label: "Water temperature",
...defineNamedScales("temperature"),
...useNamedScales("temperature"),
},
[0x18]: {
label: "Soil temperature",
...defineNamedScales("temperature"),
...useNamedScales("temperature"),
},
[0x19]: {
label: "Seismic Intensity",
Expand Down Expand Up @@ -389,7 +389,7 @@ const sensors = Object.freeze(
},
[0x22]: {
label: "Target temperature",
...defineNamedScales("temperature"),
...useNamedScales("temperature"),
},
[0x23]: {
label: "Particulate Matter 2.5",
Expand Down Expand Up @@ -463,11 +463,11 @@ const sensors = Object.freeze(
},
[0x29]: {
label: "Soil humidity",
...defineNamedScales("percentage"),
...useNamedScales("percentage"),
},
[0x2a]: {
label: "Soil reactivity",
...defineNamedScales("acidity"),
...useNamedScales("acidity"),
},
[0x2b]: {
label: "Soil salinity",
Expand Down Expand Up @@ -502,19 +502,19 @@ const sensors = Object.freeze(
},
[0x2e]: {
label: "Muscle mass",
...defineNamedScales("mass"),
...useNamedScales("mass"),
},
[0x2f]: {
label: "Fat mass",
...defineNamedScales("mass"),
...useNamedScales("mass"),
},
[0x30]: {
label: "Bone mass",
...defineNamedScales("mass"),
...useNamedScales("mass"),
},
[0x31]: {
label: "Total body water (TBW)",
...defineNamedScales("mass"),
...useNamedScales("mass"),
},
[0x32]: {
label: "Basis metabolic rate (BMR)",
Expand All @@ -535,19 +535,19 @@ const sensors = Object.freeze(
},
[0x34]: {
label: "Acceleration X-axis",
...defineNamedScales("acceleration"),
...useNamedScales("acceleration"),
},
[0x35]: {
label: "Acceleration Y-axis",
...defineNamedScales("acceleration"),
...useNamedScales("acceleration"),
},
[0x36]: {
label: "Acceleration Z-axis",
...defineNamedScales("acceleration"),
...useNamedScales("acceleration"),
},
[0x37]: {
label: "Smoke density",
...defineNamedScales("percentage"),
...useNamedScales("percentage"),
},
[0x38]: {
label: "Water flow",
Expand Down Expand Up @@ -604,23 +604,23 @@ const sensors = Object.freeze(
},
[0x3d]: {
label: "Relative Modulation level",
...defineNamedScales("percentage"),
...useNamedScales("percentage"),
},
[0x3e]: {
label: "Boiler water temperature",
...defineNamedScales("temperature"),
...useNamedScales("temperature"),
},
[0x3f]: {
label: "Domestic Hot Water (DHW) temperature",
...defineNamedScales("temperature"),
...useNamedScales("temperature"),
},
[0x40]: {
label: "Outside temperature",
...defineNamedScales("temperature"),
...useNamedScales("temperature"),
},
[0x41]: {
label: "Exhaust temperature",
...defineNamedScales("temperature"),
...useNamedScales("temperature"),
},
[0x42]: {
label: "Water Chlorine level",
Expand All @@ -633,7 +633,7 @@ const sensors = Object.freeze(
},
[0x43]: {
label: "Water acidity",
...defineNamedScales("acidity"),
...useNamedScales("acidity"),
},
[0x44]: {
label: "Water Oxidation reduction potential",
Expand All @@ -646,11 +646,11 @@ const sensors = Object.freeze(
},
[0x45]: {
label: "Heart Rate LF/HF ratio",
...defineNamedScales("unitless"),
...useNamedScales("unitless"),
},
[0x46]: {
label: "Motion Direction",
...defineNamedScales("direction"),
...useNamedScales("direction"),
},
[0x47]: {
label: "Applied force on the sensor",
Expand All @@ -663,71 +663,71 @@ const sensors = Object.freeze(
},
[0x48]: {
label: "Return Air temperature",
...defineNamedScales("temperature"),
...useNamedScales("temperature"),
},
[0x49]: {
label: "Supply Air temperature",
...defineNamedScales("temperature"),
...useNamedScales("temperature"),
},
[0x4a]: {
label: "Condenser Coil temperature",
...defineNamedScales("temperature"),
...useNamedScales("temperature"),
},
[0x4b]: {
label: "Evaporator Coil temperature",
...defineNamedScales("temperature"),
...useNamedScales("temperature"),
},
[0x4c]: {
label: "Liquid Line temperature",
...defineNamedScales("temperature"),
...useNamedScales("temperature"),
},
[0x4d]: {
label: "Discharge Line temperature",
...defineNamedScales("temperature"),
...useNamedScales("temperature"),
},
[0x4e]: {
label: "Suction Pressure",
...defineNamedScales("pressure"),
...useNamedScales("pressure"),
},
[0x4f]: {
label: "Discharge Pressure",
...defineNamedScales("pressure"),
...useNamedScales("pressure"),
},
[0x50]: {
label: "Defrost temperature",
...defineNamedScales("temperature"),
...useNamedScales("temperature"),
},
[0x51]: {
label: "Ozone",
...defineNamedScales("density"),
...useNamedScales("density"),
},
[0x52]: {
label: "Sulfur dioxide",
...defineNamedScales("density"),
...useNamedScales("density"),
},
[0x53]: {
label: "Nitrogen dioxide",
...defineNamedScales("density"),
...useNamedScales("density"),
},
[0x54]: {
label: "Ammonia",
...defineNamedScales("density"),
...useNamedScales("density"),
},
[0x55]: {
label: "Lead",
...defineNamedScales("density"),
...useNamedScales("density"),
},
[0x56]: {
label: "Particulate Matter 1",
...defineNamedScales("density"),
...useNamedScales("density"),
},
[0x57]: {
label: "Person counter (entering)",
...defineNamedScales("unitless"),
...useNamedScales("unitless"),
},
[0x58]: {
label: "Person counter (exiting)",
...defineNamedScales("unitless"),
...useNamedScales("unitless"),
},
} as const satisfies Record<number, SensorDefinition>,
);
Expand All @@ -753,6 +753,12 @@ export function getSensorName(sensorType: number): string {
?? `UNKNOWN (${num2hex(sensorType)})`;
}

/** Returns all sensor definitions including their scales */
export function getAllSensors(): readonly Sensor[] {
return Object.entries(sensors)
.map(([key, value]) => ({ key: parseInt(key, 10), ...value }));
}

/** Returns a scale definition for a scale with known name and key */
export function getSensorScale<
SensorType extends number,
Expand Down Expand Up @@ -780,3 +786,14 @@ export function getSensorScale<
...scaleDef,
} satisfies Scale as any;
}

/** Returns all scales of a given sensor */
export function getAllSensorScales(
sensorType: number,
): readonly Scale[] | undefined {
const sensor = getSensor(sensorType);
if (!sensor) return;

return Object.entries(sensor.scales)
.map(([key, scale]) => ({ key: parseInt(key, 10), ...scale }));
}

0 comments on commit 123b9dc

Please sign in to comment.