Skip to content

Commit

Permalink
feat: Add noise level to NoiseSensorDeviceDetails (#632)
Browse files Browse the repository at this point in the history
* feat: Add noise level to Noise Sensor card

* ci: Format code

* Bump `seamapi`

* Add `noiseaware_metadata` to seed

* Add return type

* Add return type (2)

* ci: Format code

* Satisfy linter boolean expression

---------

Co-authored-by: Seam Bot <[email protected]>
  • Loading branch information
xplato and seambot authored May 14, 2024
1 parent 6fbf658 commit 6e7b790
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .storybook/seed-fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,13 @@ export const seedFake = (db) => {
},
image_url:
'https://connect.getseam.com/assets/images/devices/noiseaware_indoor_sensor_front.png',
noiseaware_metadata: {
device_model: 'indoor',
noise_level_nrs: 1,
noise_level_decibel: 42,
device_name: 'Living room',
device_id: 'noiseaware_123',
},
},
errors: [],
})
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"luxon": "^3.3.0",
"queue": "^7.0.0",
"react-hook-form": "^7.46.1",
"seamapi": "^8.22.0",
"seamapi": "^8.22.1",
"uuid": "^9.0.0",
"zoned-time": "^1.1.2"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { NestedSpecificDeviceDetailsProps } from 'lib/seam/components/Devic
import { DeviceInfo } from 'lib/seam/components/DeviceDetails/DeviceInfo.js'
import { DeviceModel } from 'lib/seam/components/DeviceDetails/DeviceModel.js'
import { DeviceImage } from 'lib/ui/device/DeviceImage.js'
import { NoiseLevelStatus } from 'lib/ui/device/NoiseLevelStatus.js'
import { OnlineStatus } from 'lib/ui/device/OnlineStatus.js'
import { ContentHeader } from 'lib/ui/layout/ContentHeader.js'
import { NoiseThresholdsList } from 'lib/ui/noise-sensor/NoiseThresholdsList.js'
Expand Down Expand Up @@ -37,6 +38,7 @@ export function NoiseSensorDeviceDetails({
<div className='seam-properties'>
<span className='seam-label'>{t.status}:</span>{' '}
<OnlineStatus device={device} />
<NoiseLevelStatus device={device} />
<DeviceModel device={device} />
</div>
</div>
Expand Down
58 changes: 58 additions & 0 deletions src/lib/ui/device/NoiseLevelStatus.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { NoiseSensorDevice } from 'seamapi'

import { NoiseLevelsIcon } from 'lib/icons/NoiseLevels.js'

interface NoiseLevelStatusProps {
device: NoiseSensorDevice
}

export function NoiseLevelStatus({
device,
}: NoiseLevelStatusProps): JSX.Element {
return (
<>
<span className='seam-label'>{t.noiseLevel}:</span>

<div className='seam-device-status'>
<NoiseLevelsIcon />
<span className='seam-text'>{getNoiseLevel(device)}</span>
</div>
</>
)
}

function toNoiseString(value: number | string | undefined): string {
if (typeof value === 'string' || typeof value === 'number') {
return `${Number(value).toFixed(0)} ${t.decibel}`
}
return t.unknown
}

function getNoiseLevel(device: NoiseSensorDevice): string {
const isMinut = device.device_type === 'minut_sensor'
const isNoiseAware = device.device_type === 'noiseaware_activity_zone'

if (isMinut) {
return toNoiseString(
device?.properties?.minut_metadata?.latest_sensor_values?.sound?.value
)
}

if (isNoiseAware) {
if (device?.properties?.noiseaware_metadata?.noise_level_nrs === -1) {
return t.noiseAwareDefault
}
return toNoiseString(
device?.properties?.noiseaware_metadata?.noise_level_decibel
)
}

return toNoiseString(undefined)
}

const t = {
noiseLevel: 'Noise level',
unknown: 'Unknown',
decibel: 'dB',
noiseAwareDefault: '0-40 dB',
}

0 comments on commit 6e7b790

Please sign in to comment.