refactoring so imperial temp works in graph
parent
365bf032ba
commit
551d69be8e
|
@ -119,7 +119,7 @@ Graph.defaults = {
|
||||||
title: false,
|
title: false,
|
||||||
width: 100,
|
width: 100,
|
||||||
height: 40,
|
height: 40,
|
||||||
margins: [5, 3, 2, 3], // top right bottom left
|
margins: [5, 3, 2, 5], // top right bottom left
|
||||||
datasets: [],
|
datasets: [],
|
||||||
}
|
}
|
||||||
Graph.datasetDefaults = {
|
Graph.datasetDefaults = {
|
||||||
|
|
|
@ -17,4 +17,4 @@ class Layout {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Layout;
|
export default Layout;
|
||||||
|
|
|
@ -2,12 +2,15 @@ const m = require("mithril");
|
||||||
|
|
||||||
const Weather = {
|
const Weather = {
|
||||||
current: null,
|
current: null,
|
||||||
|
history: [],
|
||||||
loadCurrent: function() {
|
loadCurrent: function() {
|
||||||
return m.request({
|
return m.request({
|
||||||
method: "GET",
|
method: "GET",
|
||||||
url: "/weather/current"
|
url: "/weather/current"
|
||||||
}).then(function(result){
|
}).then(function(result){
|
||||||
Weather.current = result;
|
Weather.current = result;
|
||||||
|
Weather.history.push(result);
|
||||||
|
Weather.history = Weather.history.slice(Math.max(Weather.history.length - 120, 0));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
import UserPrefs from './models/UserPrefs';
|
||||||
|
|
||||||
|
export function localeIsImperial() {
|
||||||
|
return !!(UserPrefs.userImperial);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function localeTemperature(celsius, precision=1)
|
||||||
|
{
|
||||||
|
if (UserPrefs.prefs.useImperial) {
|
||||||
|
const f = cToF(celsius);
|
||||||
|
return `${f.toFixed(precision)} °F`;
|
||||||
|
}
|
||||||
|
return `${celsius.toFixed(precision)} °C`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function localePressure(hpa)
|
||||||
|
{
|
||||||
|
if (UserPrefs.prefs.useImperial) {
|
||||||
|
const inHg = hpa * 0.00029529980164712;
|
||||||
|
return `${inHg.toFixed(2)} inHg`;
|
||||||
|
}
|
||||||
|
const millibar = hpa * 0.01;
|
||||||
|
return `${millibar.toFixed(0)} mb`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function cToF(celsius)
|
||||||
|
{
|
||||||
|
return celsius * 1.8 + 32;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function fToC(f)
|
||||||
|
{
|
||||||
|
return (f - 32) / 1.8;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function feelsLike(celsius, rh)
|
||||||
|
{
|
||||||
|
const f = cToF(celsius);
|
||||||
|
const hindex = -42.379 + (2.04901523 * f) + (10.14333127 * rh)
|
||||||
|
- (0.22475541 * f * rh) - (6.83783e-3 * f * f)
|
||||||
|
- (5.481717e-2 * rh * rh) + (1.22874e-3 * f * f * rh)
|
||||||
|
+ (8.5282e-4 * f * rh * rh) - (1.99e-6 * f * f * rh * rh);
|
||||||
|
return fToC(hindex);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function dewPoint(celsius, rh)
|
||||||
|
{
|
||||||
|
const a = 6.1121;
|
||||||
|
const b = 18.678;
|
||||||
|
const c = 257.14;
|
||||||
|
const d = 234.5;
|
||||||
|
const e = 2.7182818284;
|
||||||
|
|
||||||
|
const trh = Math.log(rh / 100) + (b*celsius/(c + celsius));
|
||||||
|
return (c * trh / (b-trh));
|
||||||
|
}
|
|
@ -3,59 +3,7 @@ import Layout from '../components/Layout';
|
||||||
import Weather from '../models/weather';
|
import Weather from '../models/weather';
|
||||||
import MeasurementList from '../components/MeasurementList';
|
import MeasurementList from '../components/MeasurementList';
|
||||||
import Measurement from '../components/Measurement';
|
import Measurement from '../components/Measurement';
|
||||||
import UserPrefs from '../models/UserPrefs';
|
import { localeTemperature, localePressure, feelsLike, dewPoint } from '../util';
|
||||||
|
|
||||||
|
|
||||||
function localeTemperature(celsius)
|
|
||||||
{
|
|
||||||
if (UserPrefs.prefs.useImperial) {
|
|
||||||
const f = cToF(celsius);
|
|
||||||
return `${f.toFixed(1)} °F`;
|
|
||||||
}
|
|
||||||
return `${celsius.toFixed(1)} °C`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function localePressure(hpa)
|
|
||||||
{
|
|
||||||
if (UserPrefs.prefs.useImperial) {
|
|
||||||
const inHg = hpa * 0.00029529980164712;
|
|
||||||
return `${inHg.toFixed(2)} inHg`;
|
|
||||||
}
|
|
||||||
const millibar = hpa * 0.01;
|
|
||||||
return `${millibar.toFixed(0)} mb`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function cToF(celsius)
|
|
||||||
{
|
|
||||||
return celsius * 1.8 + 32;
|
|
||||||
}
|
|
||||||
|
|
||||||
function fToC(f)
|
|
||||||
{
|
|
||||||
return (f - 32) / 1.8;
|
|
||||||
}
|
|
||||||
|
|
||||||
function feelsLike(celsius, rh)
|
|
||||||
{
|
|
||||||
const f = cToF(celsius);
|
|
||||||
const hindex = -42.379 + (2.04901523 * f) + (10.14333127 * rh)
|
|
||||||
- (0.22475541 * f * rh) - (6.83783e-3 * f * f)
|
|
||||||
- (5.481717e-2 * rh * rh) + (1.22874e-3 * f * f * rh)
|
|
||||||
+ (8.5282e-4 * f * rh * rh) - (1.99e-6 * f * f * rh * rh);
|
|
||||||
return fToC(hindex);
|
|
||||||
}
|
|
||||||
|
|
||||||
function dewPoint(celsius, rh)
|
|
||||||
{
|
|
||||||
const a = 6.1121;
|
|
||||||
const b = 18.678;
|
|
||||||
const c = 257.14;
|
|
||||||
const d = 234.5;
|
|
||||||
const e = 2.7182818284;
|
|
||||||
|
|
||||||
const trh = Math.log(rh / 100) + (b*celsius/(c + celsius));
|
|
||||||
return (c * trh / (b-trh));
|
|
||||||
}
|
|
||||||
|
|
||||||
class CurrentConditions {
|
class CurrentConditions {
|
||||||
oninit() {
|
oninit() {
|
||||||
|
|
|
@ -2,60 +2,55 @@ import m from 'mithril';
|
||||||
import Layout from '../components/Layout';
|
import Layout from '../components/Layout';
|
||||||
import Graph from '../components/Graph';
|
import Graph from '../components/Graph';
|
||||||
import Weather from '../models/weather';
|
import Weather from '../models/weather';
|
||||||
|
import { localePressure, localeTemperature } from '../util';
|
||||||
|
|
||||||
function generateDataset({title, field, minY, maxY}) {
|
function generateDataset({title, field, minY, maxY, yLabels}) {
|
||||||
if (Weather.history.length == 0) return null;
|
if (Weather.history.length == 0) return { title};
|
||||||
return {
|
return {
|
||||||
title: title,
|
title: title,
|
||||||
data: Weather.history.map((e) => ([e.time, e[field]])),
|
data: Weather.history.map((e) => ([e.time, e[field]])),
|
||||||
minX: Weather.history[0].time,
|
minX: Weather.history[0].time,
|
||||||
maxX: Weather.current.time,
|
maxX: Weather.current.time,
|
||||||
minY: minY,
|
minY: minY,
|
||||||
maxY: maxY
|
maxY: maxY,
|
||||||
|
yLabels: yLabels,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class HistoryView {
|
class HistoryView {
|
||||||
view() {
|
view() {
|
||||||
const title = 'Recent Conditions';
|
const title = 'Recent Conditions';
|
||||||
let now = (new Date()).getTime()/1000;
|
let now = (new Date()).getTime()/1000;
|
||||||
const datasets = [
|
const datasets = [
|
||||||
//generateDataset({title: "Temperature", field: 'temperature'}),
|
generateDataset({
|
||||||
{
|
|
||||||
title: "Temperature",
|
title: "Temperature",
|
||||||
data: [
|
field: "temperature",
|
||||||
[0, 25],
|
minY: 0,
|
||||||
[1, 26],
|
maxY: 45,
|
||||||
[2, 26.5],
|
yLabels: [...Array(10).keys()].map((i) => ({
|
||||||
[3, 25.9],
|
position: i*5,
|
||||||
[4, 24.2],
|
label: localeTemperature(i*5, 0),
|
||||||
[5, 24.3],
|
line: true,
|
||||||
[6, 24.1],
|
})),
|
||||||
],
|
}),
|
||||||
yLabels: [
|
generateDataset({
|
||||||
{ position: 25, label: '25', line: true},
|
|
||||||
{ position: 20, label: '20'},
|
|
||||||
{ position: 0, label: '0', line: true},
|
|
||||||
],
|
|
||||||
xLabels: [
|
|
||||||
{ position: 0, label: '0'},
|
|
||||||
{ position: 3, label: '3', line: true},
|
|
||||||
{ position: 6, label: '6', line: true},
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: "Pressure",
|
title: "Pressure",
|
||||||
data: [
|
field: "pressure",
|
||||||
[0, 1013],
|
minY: 100000,
|
||||||
[1, 1013],
|
maxY: 103000,
|
||||||
[2, 1014],
|
yLabels: [],
|
||||||
[3, 1013],
|
}),
|
||||||
[4, 1012],
|
generateDataset({
|
||||||
[5, 1012.5],
|
title: "Humidity",
|
||||||
[6, 1012.2],
|
field: "humidity",
|
||||||
],
|
minY: 0,
|
||||||
minY: 1000,
|
maxY: 100,
|
||||||
maxY: 1020,
|
yLabels: [...Array(11).keys()].map((v) => ({
|
||||||
}
|
position: v*10,
|
||||||
|
label: `${v*10} %`,
|
||||||
|
line: true,
|
||||||
|
})),
|
||||||
|
})
|
||||||
];
|
];
|
||||||
console.log({datasets});
|
console.log({datasets});
|
||||||
return (
|
return (
|
||||||
|
|
Loading…
Reference in New Issue