forked from MagicMirrorOrg/MagicMirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider-utils.js
More file actions
181 lines (167 loc) · 4.85 KB
/
Copy pathprovider-utils.js
File metadata and controls
181 lines (167 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Shared utility functions for weather providers
*/
const SunCalc = require("suncalc");
/**
* Convert OpenWeatherMap icon codes to internal weather types
* @param {string} weatherType - OpenWeatherMap icon code (e.g., "01d", "02n")
* @returns {string|null} Internal weather type
*/
function convertWeatherType (weatherType) {
const weatherTypes = {
"01d": "day-sunny",
"02d": "day-cloudy",
"03d": "cloudy",
"04d": "cloudy-windy",
"09d": "showers",
"10d": "rain",
"11d": "thunderstorm",
"13d": "snow",
"50d": "fog",
"01n": "night-clear",
"02n": "night-cloudy",
"03n": "night-cloudy",
"04n": "night-cloudy",
"09n": "night-showers",
"10n": "night-rain",
"11n": "night-thunderstorm",
"13n": "night-snow",
"50n": "night-alt-cloudy-windy"
};
return weatherTypes.hasOwnProperty(weatherType) ? weatherTypes[weatherType] : null;
}
/**
* Apply timezone offset to a date
* @param {Date} date - The date to apply offset to
* @param {number} offsetMinutes - Timezone offset in minutes
* @returns {Date} Date with applied offset
*/
function applyTimezoneOffset (date, offsetMinutes) {
const utcTime = date.getTime() + (date.getTimezoneOffset() * 60000);
return new Date(utcTime + (offsetMinutes * 60000));
}
/**
* Limit decimal places for coordinates (truncate, not round)
* @param {number} value - The coordinate value
* @param {number} decimals - Maximum number of decimal places
* @returns {number} Value with limited decimal places
*/
function limitDecimals (value, decimals) {
const str = value.toString();
if (str.includes(".")) {
const parts = str.split(".");
if (parts[1].length > decimals) {
return parseFloat(`${parts[0]}.${parts[1].substring(0, decimals)}`);
}
}
return value;
}
/**
* Get sunrise and sunset times for a given date and location
* @param {Date} date - The date to calculate for
* @param {number} lat - Latitude
* @param {number} lon - Longitude
* @returns {object} Object with sunrise and sunset Date objects
*/
function getSunTimes (date, lat, lon) {
const sunTimes = SunCalc.getTimes(date, lat, lon);
return {
sunrise: sunTimes.sunrise,
sunset: sunTimes.sunset
};
}
/**
* Check if a given time is during daylight hours
* @param {Date} date - The date/time to check
* @param {Date} sunrise - Sunrise time
* @param {Date} sunset - Sunset time
* @returns {boolean} True if during daylight hours
*/
function isDayTime (date, sunrise, sunset) {
if (!sunrise || !sunset) {
return true; // Default to day if times unavailable
}
return date >= sunrise && date < sunset;
}
/**
* Format timezone offset as string (e.g., "+01:00", "-05:30")
* @param {number} offsetMinutes - Timezone offset in minutes (use -new Date().getTimezoneOffset() for local)
* @returns {string} Formatted offset string
*/
function formatTimezoneOffset (offsetMinutes) {
const hours = Math.floor(Math.abs(offsetMinutes) / 60);
const minutes = Math.abs(offsetMinutes) % 60;
const sign = offsetMinutes >= 0 ? "+" : "-";
return `${sign}${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}`;
}
/**
* Get date string in YYYY-MM-DD format (local time)
* @param {Date} date - The date to format
* @returns {string} Date string in YYYY-MM-DD format
*/
function getDateString (date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return `${year}-${month}-${day}`;
}
/**
* Convert wind speed from km/h to m/s
* @param {number} kmh - Wind speed in km/h
* @returns {number} Wind speed in m/s
*/
function convertKmhToMs (kmh) {
return kmh / 3.6;
}
/**
* Convert cardinal wind direction string to degrees
* @param {string} direction - Cardinal direction (e.g., "N", "NNE", "SW")
* @returns {number|null} Direction in degrees (0-360) or null if unknown
*/
function cardinalToDegrees (direction) {
const directions = {
N: 0,
NNE: 22.5,
NE: 45,
ENE: 67.5,
E: 90,
ESE: 112.5,
SE: 135,
SSE: 157.5,
S: 180,
SSW: 202.5,
SW: 225,
WSW: 247.5,
W: 270,
WNW: 292.5,
NW: 315,
NNW: 337.5
};
return directions[direction] ?? null;
}
/**
* Validate and limit coordinate precision
* @param {object} config - Configuration object with lat/lon properties
* @param {number} maxDecimals - Maximum decimal places to preserve
* @throws {Error} If coordinates are missing or invalid
*/
function validateCoordinates (config, maxDecimals = 4) {
if (config.lat == null || config.lon == null
|| !Number.isFinite(config.lat) || !Number.isFinite(config.lon)) {
throw new Error("Latitude and longitude are required");
}
config.lat = limitDecimals(config.lat, maxDecimals);
config.lon = limitDecimals(config.lon, maxDecimals);
}
module.exports = {
convertWeatherType,
applyTimezoneOffset,
limitDecimals,
getSunTimes,
isDayTime,
formatTimezoneOffset,
getDateString,
convertKmhToMs,
cardinalToDegrees,
validateCoordinates
};