some code cleanup
This commit is contained in:
parent
f5ff252f95
commit
5fcf43b0c1
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}, {})
|
||||
}
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
'use strict'
|
||||
/*
|
||||
Open Rowing Monitor, https://github.com/laberning/openrowingmonitor
|
||||
|
||||
*/
|
||||
Loading…
Reference in New Issue