some code cleanup

This commit is contained in:
Lars Berning 2022-01-16 13:31:31 +01:00
parent f5ff252f95
commit 5fcf43b0c1
No known key found for this signature in database
GPG Key ID: 028E73C9E1D8A0B3
5 changed files with 23 additions and 22 deletions

View File

@ -28,7 +28,7 @@ export class PerformanceDashboard extends AppElement {
render () {
const metrics = this.calculateFormattedMetrics(this.appState.metrics)
return html`
<dashboard-metric .icon=${icon_route} .unit=${metrics?.distanceTotal?.unit} .value=${metrics?.distanceTotal?.value}></dashboard-metric>
<dashboard-metric .icon=${icon_route} .unit=${metrics?.distanceTotal?.unit || 'm'} .value=${metrics?.distanceTotal?.value}></dashboard-metric>
<dashboard-metric .icon=${icon_stopwatch} unit="/500m" .value=${metrics?.splitFormatted?.value}></dashboard-metric>
<dashboard-metric .icon=${icon_bolt} unit="watt" .value=${metrics?.power?.value}></dashboard-metric>
<dashboard-metric .icon=${icon_paddle} unit="/min" .value=${metrics?.strokesPerMinute?.value}></dashboard-metric>

View File

@ -36,7 +36,6 @@ export class App extends LitElement {
})
// notify the app about the triggered action
// todo: lets see if this solution sticks...
this.addEventListener('triggerAction', (event) => {
this.app.handleAction(event.detail)
})
@ -51,7 +50,7 @@ export class App extends LitElement {
// return a deep copy of the state to other components to minimize risk of side effects
getState = () => {
// todo: could use structuredClone once the browser support is wider
// could use structuredClone once the browser support is wider
// https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
return JSON.parse(JSON.stringify(this.appState))
}

View File

@ -6,6 +6,7 @@
*/
import NoSleep from 'nosleep.js'
import { filterObjectByKeys } from './helper'
const rowingMetricsFields = ['strokesTotal', 'distanceTotal', 'caloriesTotal', 'power', 'heartrate',
'heartrateBatteryLevel', 'splitFormatted', 'strokesPerMinute', 'durationTotalFormatted']
@ -42,7 +43,7 @@ export function createApp (app) {
}, 1000)
})
// todo: we have to use different types of messages to make processing easier
// todo: we should use different types of messages to make processing easier
socket.addEventListener('message', (event) => {
try {
const data = JSON.parse(event.data)
@ -50,15 +51,10 @@ export function createApp (app) {
let activeFields = rowingMetricsFields
// if we are in reset state only update heart rate
if (data.strokesTotal === 0) {
activeFields = ['heartrate']
activeFields = ['heartrate', 'heartrateBatteryLevel']
}
const filteredData = Object.keys(data)
.filter(key => activeFields.includes(key))
.reduce((obj, key) => {
obj[key] = data[key]
return obj
}, {})
const filteredData = filterObjectByKeys(data, activeFields)
let updatedState = { ...app.getState(), metrics: filteredData }
if (data.peripheralMode) {
@ -89,12 +85,7 @@ export function createApp (app) {
function resetFields () {
const appState = app.getState()
// drop all metrics except heartrate
appState.metrics = Object.keys(appState.metrics)
.filter(key => key === 'heartrate' || key === 'heartrateBatteryLevel')
.reduce((obj, key) => {
obj[key] = appState.metrics[key]
return obj
}, {})
appState.metrics = filterObjectByKeys(appState.metrics, ['heartrate', 'heartrateBatteryLevel'])
app.updateState(appState)
}

16
app/client/lib/helper.js Normal file
View File

@ -0,0 +1,16 @@
'use strict'
/*
Open Rowing Monitor, https://github.com/laberning/openrowingmonitor
Helper functions
*/
// Filters an object so that it only contains the attributes that are defined in a list
export function filterObjectByKeys (object, keys) {
return Object.keys(object)
.filter(key => keys.includes(key))
.reduce((obj, key) => {
obj[key] = object[key]
return obj
}, {})
}

View File

@ -1,5 +0,0 @@
'use strict'
/*
Open Rowing Monitor, https://github.com/laberning/openrowingmonitor
*/