Browse Source

refactored Gauge class to inherit from NumericValue

master
Youen Toupin 3 years ago
parent
commit
98a59085d4
  1. 46
      WebApp/src/components/widgets/gauge.tsx
  2. 28
      WebApp/src/components/widgets/numeric-value.tsx
  3. 8
      WebApp/src/pages/dashboard/dashboard-page.tsx

46
WebApp/src/components/widgets/gauge.tsx

@ -1,18 +1,11 @@
import m from 'mithril'; import m from 'mithril';
import { Widget } from 'components/widgets/widget'; import { NumericValue } from 'components/widgets/numeric-value';
import { Pipe } from 'utilities/pipe'; import { Pipe } from 'utilities/pipe';
require('./gauge.css'); require('./gauge.css');
export class Gauge extends Widget { export class Gauge extends NumericValue {
private gaugeValue: Pipe<number>;
private gaugeMaxValue: number;
private unit: string;
private decimals: number;
protected bars: SVGGeometryElement[] = []; protected bars: SVGGeometryElement[] = [];
private integralValueElement: HTMLElement;
private decimalValueElement: HTMLElement;
private displayedValue = 0.0; private displayedValue = 0.0;
private targetValue = 0.0; private targetValue = 0.0;
@ -27,13 +20,6 @@ export class Gauge extends Widget {
constructor(vnode: any) { constructor(vnode: any) {
super(vnode); super(vnode);
this.gaugeValue = vnode.attrs.gaugeValue || new Pipe(0.0);
this.gaugeMaxValue = vnode.attrs.gaugeMaxValue || 1.0;
this.unit = vnode.attrs.unit || '';
this.decimals = vnode.attrs.decimals || 0;
this.gaugeValue.onChange(() => this.updateGauge());
} }
onbeforeremove(vnode: m.Vnode<{}, {}>) { onbeforeremove(vnode: m.Vnode<{}, {}>) {
@ -41,26 +27,15 @@ export class Gauge extends Widget {
super.onbeforeremove(vnode); super.onbeforeremove(vnode);
} }
view(vnode: m.Vnode<{}, {}>): m.Children {
return <div class="widget gauge" style={'flex: ' + this.widgetWidth}> protected graphicalRepresentation(vnode: m.Vnode<{}, {}>): m.Children {
<p><span class="integral-value"></span><span class="decimal-value"></span><span class="unit">{this.unit}</span></p> return <svg></svg>;
<svg>
</svg>
</div>;
} }
updateGauge() { protected onValueChange(value: number) {
let now = Date.now() / 1000.0; let now = Date.now() / 1000.0;
let value = Math.max(0.0, Math.min(this.gaugeMaxValue, this.gaugeValue.get())); let ratio = value / (this.maxValue || 1.0);
let valueStr = value.toFixed(this.decimals);
let parts = valueStr.split('.');
this.integralValueElement.innerText = parts[0];
this.decimalValueElement.innerText = this.decimals > 0 ? '.' + parts[1] : '';
let ratio = value / this.gaugeMaxValue;
let numBars = this.bars.length; let numBars = this.bars.length;
let threshold = 0.33 / numBars; let threshold = 0.33 / numBars;
@ -124,11 +99,4 @@ export class Gauge extends Widget {
bar.classList.toggle('lit', isLit); bar.classList.toggle('lit', isLit);
} }
} }
oncreate(vnode: any) {
this.integralValueElement = vnode.dom.querySelector('span.integral-value');
this.decimalValueElement = vnode.dom.querySelector('span.decimal-value');
this.updateGauge();
}
} }

28
WebApp/src/components/widgets/numeric-value.tsx

@ -5,9 +5,11 @@ import { Pipe } from 'utilities/pipe';
require('./numeric-value.css'); require('./numeric-value.css');
export class NumericValue extends Widget { export class NumericValue extends Widget {
private value: Pipe<number>; protected value: Pipe<number>;
private unit: string; protected minValue?: number;
private decimals: number; protected maxValue?: number;
protected unit: string;
protected decimals: number;
private integralValueElement: HTMLElement; private integralValueElement: HTMLElement;
private decimalValueElement: HTMLElement; private decimalValueElement: HTMLElement;
@ -16,32 +18,46 @@ export class NumericValue extends Widget {
super(vnode); super(vnode);
this.value = vnode.attrs.value || new Pipe(0.0); this.value = vnode.attrs.value || new Pipe(0.0);
this.minValue = vnode.attrs.minValue || null;
this.maxValue = vnode.attrs.maxValue || null;
this.unit = vnode.attrs.unit || ''; this.unit = vnode.attrs.unit || '';
this.decimals = vnode.attrs.decimals || 0; this.decimals = vnode.attrs.decimals || 0;
this.value.onChange(() => this.onValueChange()); this.value.onChange(() => this.onValueChange_());
} }
view(vnode: m.Vnode<{}, {}>): m.Children { view(vnode: m.Vnode<{}, {}>): m.Children {
return <div class="widget gauge" style={'flex: ' + this.widgetWidth}> return <div class="widget gauge" style={'flex: ' + this.widgetWidth}>
<p><span class="integral-value"></span><span class="decimal-value"></span><span class="unit">{this.unit}</span></p> <p><span class="integral-value"></span><span class="decimal-value"></span><span class="unit">{this.unit}</span></p>
{this.graphicalRepresentation(vnode)}
</div>; </div>;
} }
onValueChange() { protected graphicalRepresentation(vnode: m.Vnode<{}, {}>): m.Children {
return [];
}
protected onValueChange(newValue: number) {
}
private onValueChange_() {
let value = this.value.get(); let value = this.value.get();
if(this.minValue !== null) value = Math.max(value, this.minValue);
if(this.maxValue !== null) value = Math.min(value, this.maxValue);
let valueStr = value.toFixed(this.decimals); let valueStr = value.toFixed(this.decimals);
let parts = valueStr.split('.'); let parts = valueStr.split('.');
this.integralValueElement.innerText = parts[0]; this.integralValueElement.innerText = parts[0];
this.decimalValueElement.innerText = this.decimals > 0 ? '.' + parts[1] : ''; this.decimalValueElement.innerText = this.decimals > 0 ? '.' + parts[1] : '';
this.onValueChange(value);
} }
oncreate(vnode: any) { oncreate(vnode: any) {
this.integralValueElement = vnode.dom.querySelector('span.integral-value'); this.integralValueElement = vnode.dom.querySelector('span.integral-value');
this.decimalValueElement = vnode.dom.querySelector('span.decimal-value'); this.decimalValueElement = vnode.dom.querySelector('span.decimal-value');
this.onValueChange(); this.onValueChange_();
} }
} }

8
WebApp/src/pages/dashboard/dashboard-page.tsx

@ -45,7 +45,7 @@ export class DashboardPage extends Page {
m.redraw(); m.redraw();
this.status = newStatus; this.status = newStatus;
let power = this.status.batteryVoltage * this.status.motorCurrent; let power = Math.max(0.0, this.status.batteryVoltage * this.status.motorCurrent);
this.power.set(power); this.power.set(power);
const minVoltage = 36.0; const minVoltage = 36.0;
@ -92,9 +92,9 @@ export class DashboardPage extends Page {
</div> </div>
<div class="widgets-row" style="height: 40%"> <div class="widgets-row" style="height: 40%">
<GaugeLinear widgetWidth={0.2} gaugeValue={this.power} gaugeMaxValue={999.0} bottomWidth={0.4} unit="W" /> <GaugeLinear widgetWidth={0.2} value={this.power} minValue={0} maxValue={999.0} bottomWidth={0.4} unit="W" />
<GaugeCircular widgetWidth={0.6} gaugeValue={this.speed} gaugeMaxValue={50.0} decimals={1} unit="km/h" /> <GaugeCircular widgetWidth={0.6} value={this.speed} minValue={0} maxValue={50.0} decimals={1} unit="km/h" />
<GaugeBattery widgetWidth={0.2} gaugeValue={this.battery} gaugeMaxValue={100.0} unit="%" /> <GaugeBattery widgetWidth={0.2} value={this.battery} minValue={0} maxValue={100.0} unit="%" />
</div> </div>
<div class="widgets-row"> <div class="widgets-row">

Loading…
Cancel
Save