Support "tlv8" and "data" characteristic formats (#8)

This commit is contained in:
Lars Strojny 2022-11-09 14:10:50 +01:00 committed by GitHub
parent bbc19b935d
commit 72761ceee6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 4 deletions

View file

@ -1,6 +1,6 @@
import z from 'zod' import z from 'zod'
const NumberAlikeTypesTypesBoundary = z.union([ const NumericTypesBoundary = z.union([
z.literal('bool'), z.literal('bool'),
z.literal('float'), z.literal('float'),
z.literal('int'), z.literal('int'),
@ -9,17 +9,19 @@ const NumberAlikeTypesTypesBoundary = z.union([
z.literal('uint32'), z.literal('uint32'),
z.literal('uint64'), z.literal('uint64'),
]) ])
export type NumberAlikeTypes = z.infer<typeof NumberAlikeTypesTypesBoundary> export type NumericTypes = z.infer<typeof NumericTypesBoundary>
export const CharacteristicBoundary = z.intersection( export const CharacteristicBoundary = z.intersection(
z.object({ type: z.string(), description: z.string() }), z.object({ type: z.string(), description: z.string() }),
z.union([ z.union([
z.object({ z.object({
format: NumberAlikeTypesTypesBoundary, format: NumericTypesBoundary,
value: z.optional(z.number()), value: z.optional(z.number()),
unit: z.optional(z.string()), unit: z.optional(z.string()),
}), }),
z.object({ format: z.literal('string'), value: z.string() }), z.object({ format: z.literal('string'), value: z.string() }),
z.object({ format: z.literal('data'), value: z.optional(z.string()) }),
z.object({ format: z.literal('tlv8'), value: z.string(z.string()) }),
]), ]),
) )
export type Characteristic = z.infer<typeof CharacteristicBoundary> export type Characteristic = z.infer<typeof CharacteristicBoundary>

View file

@ -32,6 +32,8 @@ export function aggregate(devices: Device[], timestamp: Date): Metric[] {
const format = characteristic.format const format = characteristic.format
switch (format) { switch (format) {
case 'string': case 'string':
case 'tlv8':
case 'data':
break break
case 'bool': case 'bool':

View file

@ -1,5 +1,5 @@
import { describe, expect, test } from '@jest/globals' import { describe, expect, test } from '@jest/globals'
import { DeviceBoundary } from '../src/boundaries/hap' import { DeviceBoundary } from '../src/boundaries'
import { Metric, aggregate } from '../src/metrics' import { Metric, aggregate } from '../src/metrics'
import dysonData from './fixtures/dyson.json' import dysonData from './fixtures/dyson.json'
import emptyData from './fixtures/empty.json' import emptyData from './fixtures/empty.json'