Skip to content

Commit 50561f3

Browse files
Rick ZakiRick Zaki
authored andcommitted
needed to separate formatting temps from converting dashboard was using format method to convert and send Fahrenheit values to chart, then passing the same method into chart formatter causing the Fahrenheit value to be passed in as Celsius and converted again.
1 parent a58f944 commit 50561f3

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

webapp/frontend/src/app/modules/dashboard/dashboard.component.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,18 @@ export class DashboardComponent implements OnInit, AfterViewInit, OnDestroy
159159

160160
for(const tempHistory of deviceSummary.temp_history){
161161
const newDate = new Date(tempHistory.date);
162+
let temperature;
163+
switch (this.config.temperature_unit) {
164+
case 'celsius':
165+
temperature = tempHistory.temp;
166+
break
167+
case 'fahrenheit':
168+
temperature = TemperaturePipe.celsiusToFahrenheit(tempHistory.temp)
169+
break
170+
}
162171
deviceSeriesMetadata.data.push({
163172
x: newDate,
164-
y: TemperaturePipe.formatTemperature(tempHistory.temp, this.config.temperature_unit, false)
173+
y: temperature
165174
})
166175
}
167176
deviceTemperatureSeries.push(deviceSeriesMetadata)

webapp/frontend/src/app/shared/temperature.pipe.spec.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ describe('TemperaturePipe', () => {
1111
const testCases = [
1212
{
1313
'c': -273.15,
14-
'f': -460,
14+
'f': -459.66999999999996,
1515
},{
1616
'c': -34.44,
17-
'f': -30,
17+
'f': -29.991999999999997,
1818
},{
1919
'c': -23.33,
20-
'f': -10,
20+
'f': -9.993999999999993,
2121
},{
2222
'c': -17.78,
23-
'f': -0,
23+
'f': -0.0040000000000048885,
2424
},{
2525
'c': 0,
2626
'f': 32,
@@ -29,10 +29,10 @@ describe('TemperaturePipe', () => {
2929
'f': 50,
3030
},{
3131
'c': 26.67,
32-
'f': 80,
32+
'f': 80.006,
3333
},{
3434
'c': 37,
35-
'f': 99,
35+
'f': 98.6,
3636
},{
3737
'c': 60,
3838
'f': 140,
@@ -42,8 +42,7 @@ describe('TemperaturePipe', () => {
4242
it(`should correctly convert ${test.c}, Celsius to Fahrenheit (testcase: ${index + 1})`, () => {
4343
// test
4444
const numb = TemperaturePipe.celsiusToFahrenheit(test.c)
45-
const roundNumb = Math.round(numb);
46-
expect(roundNumb).toEqual(test.f);
45+
expect(numb).toEqual(test.f);
4746
});
4847
})
4948
});
@@ -55,6 +54,11 @@ describe('TemperaturePipe', () => {
5554
'unit': 'celsius',
5655
'includeUnits': true,
5756
'result': '26.67°C'
57+
},{
58+
'c': 26.6767,
59+
'unit': 'celsius',
60+
'includeUnits': true,
61+
'result': '26.677°C'
5862
},{
5963
'c': 26.67,
6064
'unit': 'celsius',
@@ -64,12 +68,17 @@ describe('TemperaturePipe', () => {
6468
'c': 26.67,
6569
'unit': 'fahrenheit',
6670
'includeUnits': true,
67-
'result': '80.006°F',
71+
'result': '26.67°F',
72+
},{
73+
'c': 26.6767,
74+
'unit': 'fahrenheit',
75+
'includeUnits': true,
76+
'result': '26.677°F',
6877
},{
6978
'c': 26.67,
7079
'unit': 'fahrenheit',
7180
'includeUnits': false,
72-
'result': '80.006',
81+
'result': '26.67',
7382
}
7483
]
7584
testCases.forEach((test, index) => {

webapp/frontend/src/app/shared/temperature.pipe.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,35 @@ import {formatNumber} from '@angular/common';
66
})
77
export class TemperaturePipe implements PipeTransform {
88
static celsiusToFahrenheit(celsiusTemp: number): number {
9-
return celsiusTemp * 9.0 / 5.0 + 32;
9+
return celsiusTemp * 9/5 + 32;
1010
}
11-
static formatTemperature(celsiusTemp: number, unit: string, includeUnits: boolean): number|string {
12-
let convertedTemp
13-
let convertedUnitSuffix
11+
static formatTemperature(temp: number, unit: string, includeUnits: boolean): number|string {
12+
let unitSuffix
1413
switch (unit) {
1514
case 'celsius':
16-
convertedTemp = celsiusTemp
17-
convertedUnitSuffix = '°C'
15+
unitSuffix = '°C'
1816
break
1917
case 'fahrenheit':
20-
convertedTemp = TemperaturePipe.celsiusToFahrenheit(celsiusTemp)
21-
convertedUnitSuffix = '°F'
18+
unitSuffix = '°F'
2219
break
2320
}
2421
if(includeUnits){
25-
return formatNumber(convertedTemp, 'en-US') + convertedUnitSuffix
22+
return formatNumber(temp, 'en-US') + unitSuffix
2623
} else {
27-
return formatNumber(convertedTemp, 'en-US',)
24+
return formatNumber(temp, 'en-US',)
2825
}
2926
}
3027

3128
transform(celsiusTemp: number, unit = 'celsius', includeUnits = false): number|string {
29+
let temperature;
30+
switch (unit) {
31+
case 'celsius':
32+
temperature = celsiusTemp;
33+
break
34+
case 'fahrenheit':
35+
temperature = TemperaturePipe.celsiusToFahrenheit(celsiusTemp)
36+
break
37+
}
3238
return TemperaturePipe.formatTemperature(celsiusTemp, unit, includeUnits)
3339
}
3440

0 commit comments

Comments
 (0)