@khalido/home 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +25 -0
- package/CHANGELOG.md +47 -0
- package/LICENSE +21 -0
- package/README.md +77 -2
- package/dist/clock.d.ts +17 -0
- package/dist/clock.js +43 -0
- package/dist/config.d.ts +273 -0
- package/dist/config.js +392 -0
- package/dist/connector.d.ts +34 -0
- package/dist/connector.js +47 -0
- package/dist/connectors/amber/client.d.ts +61 -0
- package/dist/connectors/amber/client.js +106 -0
- package/dist/connectors/amber/index.d.ts +30 -0
- package/dist/connectors/amber/index.js +94 -0
- package/dist/connectors/cast/codec.d.ts +38 -0
- package/dist/connectors/cast/codec.js +102 -0
- package/dist/connectors/cast/index.d.ts +60 -0
- package/dist/connectors/cast/index.js +428 -0
- package/dist/connectors/cast/schemas.d.ts +167 -0
- package/dist/connectors/cast/schemas.js +87 -0
- package/dist/connectors/cast/serve.d.ts +18 -0
- package/dist/connectors/cast/serve.js +57 -0
- package/dist/connectors/cast/state.d.ts +62 -0
- package/dist/connectors/cast/state.js +133 -0
- package/dist/connectors/cast/tls.d.ts +3 -0
- package/dist/connectors/cast/tls.js +67 -0
- package/dist/connectors/cast/writes.d.ts +80 -0
- package/dist/connectors/cast/writes.js +390 -0
- package/dist/connectors/daikin/client.d.ts +65 -0
- package/dist/connectors/daikin/client.js +221 -0
- package/dist/connectors/daikin/index.d.ts +41 -0
- package/dist/connectors/daikin/index.js +108 -0
- package/dist/connectors/enphase/client.d.ts +65 -0
- package/dist/connectors/enphase/client.js +173 -0
- package/dist/connectors/enphase/index.d.ts +43 -0
- package/dist/connectors/enphase/index.js +174 -0
- package/dist/connectors/enphase/schemas.d.ts +51 -0
- package/dist/connectors/enphase/schemas.js +34 -0
- package/dist/connectors/hue/client.d.ts +48 -0
- package/dist/connectors/hue/client.js +95 -0
- package/dist/connectors/hue/events.d.ts +41 -0
- package/dist/connectors/hue/events.js +156 -0
- package/dist/connectors/hue/index.d.ts +42 -0
- package/dist/connectors/hue/index.js +205 -0
- package/dist/connectors/hue/schemas.d.ts +65 -0
- package/dist/connectors/hue/schemas.js +48 -0
- package/dist/connectors/hue/state.d.ts +43 -0
- package/dist/connectors/hue/state.js +84 -0
- package/dist/connectors/printers/client.d.ts +28 -0
- package/dist/connectors/printers/client.js +74 -0
- package/dist/connectors/printers/codec.d.ts +76 -0
- package/dist/connectors/printers/codec.js +246 -0
- package/dist/connectors/printers/found.d.ts +15 -0
- package/dist/connectors/printers/found.js +35 -0
- package/dist/connectors/printers/index.d.ts +52 -0
- package/dist/connectors/printers/index.js +282 -0
- package/dist/connectors/printers/state.d.ts +60 -0
- package/dist/connectors/printers/state.js +99 -0
- package/dist/connectors/spotify/changes.d.ts +32 -0
- package/dist/connectors/spotify/changes.js +84 -0
- package/dist/connectors/spotify/client.d.ts +199 -0
- package/dist/connectors/spotify/client.js +270 -0
- package/dist/connectors/spotify/index.d.ts +92 -0
- package/dist/connectors/spotify/index.js +754 -0
- package/dist/connectors/up/client.d.ts +183 -0
- package/dist/connectors/up/client.js +308 -0
- package/dist/connectors/up/index.d.ts +91 -0
- package/dist/connectors/up/index.js +213 -0
- package/dist/connectors/up/spend.d.ts +78 -0
- package/dist/connectors/up/spend.js +213 -0
- package/dist/connectors/up/webhook.d.ts +13 -0
- package/dist/connectors/up/webhook.js +32 -0
- package/dist/connectors/weather/blocks.d.ts +51 -0
- package/dist/connectors/weather/blocks.js +106 -0
- package/dist/connectors/weather/bom.d.ts +37 -0
- package/dist/connectors/weather/bom.js +237 -0
- package/dist/connectors/weather/ftp.d.ts +5 -0
- package/dist/connectors/weather/ftp.js +46 -0
- package/dist/connectors/weather/google.d.ts +31 -0
- package/dist/connectors/weather/google.js +364 -0
- package/dist/connectors/weather/index.d.ts +53 -0
- package/dist/connectors/weather/index.js +280 -0
- package/dist/connectors/weather/open-meteo.d.ts +15 -0
- package/dist/connectors/weather/open-meteo.js +285 -0
- package/dist/connectors/weather/rain.d.ts +19 -0
- package/dist/connectors/weather/rain.js +51 -0
- package/dist/connectors/weather/source.d.ts +149 -0
- package/dist/connectors/weather/source.js +77 -0
- package/dist/connectors/weather/sun.d.ts +48 -0
- package/dist/connectors/weather/sun.js +65 -0
- package/dist/discover.d.ts +197 -0
- package/dist/discover.js +452 -0
- package/dist/energy.d.ts +61 -0
- package/dist/energy.js +162 -0
- package/dist/finders-entry.d.ts +1 -0
- package/dist/finders-entry.js +2 -0
- package/dist/finders.d.ts +48 -0
- package/dist/finders.js +176 -0
- package/dist/health.d.ts +28 -0
- package/dist/health.js +35 -0
- package/dist/home.d.ts +81 -0
- package/dist/home.js +114 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +11 -0
- package/dist/lane.d.ts +17 -0
- package/dist/lane.js +74 -0
- package/dist/locate.d.ts +27 -0
- package/dist/locate.js +59 -0
- package/dist/manifest.d.ts +31 -0
- package/dist/manifest.js +4 -0
- package/dist/manifests.d.ts +5 -0
- package/dist/manifests.js +23 -0
- package/dist/node-fetch.d.ts +9 -0
- package/dist/node-fetch.js +25 -0
- package/dist/poll.d.ts +10 -0
- package/dist/poll.js +33 -0
- package/docs/cast.md +133 -0
- package/docs/cli.md +41 -0
- package/docs/config.md +36 -0
- package/docs/decisions.md +150 -0
- package/docs/devices.md +128 -0
- package/docs/enphase-notes.md +24 -0
- package/docs/google.md +92 -0
- package/docs/ideas.md +69 -0
- package/docs/matter.md +55 -0
- package/docs/performance.md +12 -0
- package/docs/philosophy.md +23 -0
- package/docs/up.md +84 -0
- package/docs/using.md +65 -0
- package/docs/weather-probe.md +63 -0
- package/examples/home.jsonc +74 -0
- package/examples/home.svg +76 -0
- package/home.schema.json +474 -0
- package/llms.txt +32 -0
- package/package.json +143 -4
- package/skills/draw-home/README.md +25 -0
- package/skills/draw-home/SKILL.md +80 -0
- package/skills/use-home/SKILL.md +86 -0
- package/src/cli/cache.ts +29 -0
- package/src/cli/cast.ts +172 -0
- package/src/cli/config.ts +52 -0
- package/src/cli/context.ts +118 -0
- package/src/cli/discover.ts +182 -0
- package/src/cli/enphase.ts +62 -0
- package/src/cli/hue.ts +124 -0
- package/src/cli/locate.ts +47 -0
- package/src/cli/main.ts +182 -0
- package/src/cli/printers.ts +21 -0
- package/src/cli/probe.ts +274 -0
- package/src/cli/spotify.ts +76 -0
- package/src/cli/status.ts +228 -0
- package/src/cli/up.ts +350 -0
- package/src/clock.ts +62 -0
- package/src/config.ts +578 -0
- package/src/connector.ts +77 -0
- package/src/connectors/amber/README.md +40 -0
- package/src/connectors/amber/client.ts +157 -0
- package/src/connectors/amber/index.ts +118 -0
- package/src/connectors/cast/README.md +44 -0
- package/src/connectors/cast/codec.ts +126 -0
- package/src/connectors/cast/index.ts +552 -0
- package/src/connectors/cast/schemas.ts +101 -0
- package/src/connectors/cast/serve.ts +82 -0
- package/src/connectors/cast/state.ts +203 -0
- package/src/connectors/cast/tls.ts +100 -0
- package/src/connectors/cast/writes.ts +572 -0
- package/src/connectors/daikin/README.md +78 -0
- package/src/connectors/daikin/client.ts +295 -0
- package/src/connectors/daikin/index.ts +145 -0
- package/src/connectors/enphase/README.md +64 -0
- package/src/connectors/enphase/client.ts +244 -0
- package/src/connectors/enphase/index.ts +241 -0
- package/src/connectors/enphase/schemas.ts +45 -0
- package/src/connectors/hue/README.md +50 -0
- package/src/connectors/hue/client.ts +157 -0
- package/src/connectors/hue/events.ts +182 -0
- package/src/connectors/hue/index.ts +261 -0
- package/src/connectors/hue/schemas.ts +60 -0
- package/src/connectors/hue/state.ts +124 -0
- package/src/connectors/printers/README.md +46 -0
- package/src/connectors/printers/client.ts +114 -0
- package/src/connectors/printers/codec.ts +292 -0
- package/src/connectors/printers/found.ts +46 -0
- package/src/connectors/printers/index.ts +385 -0
- package/src/connectors/printers/state.ts +182 -0
- package/src/connectors/spotify/README.md +83 -0
- package/src/connectors/spotify/changes.ts +127 -0
- package/src/connectors/spotify/client.ts +387 -0
- package/src/connectors/spotify/index.ts +937 -0
- package/src/connectors/up/README.md +35 -0
- package/src/connectors/up/client.ts +518 -0
- package/src/connectors/up/index.ts +370 -0
- package/src/connectors/up/spend.ts +280 -0
- package/src/connectors/up/webhook.ts +62 -0
- package/src/connectors/weather/README.md +84 -0
- package/src/connectors/weather/blocks.ts +173 -0
- package/src/connectors/weather/bom.ts +344 -0
- package/src/connectors/weather/ftp.ts +48 -0
- package/src/connectors/weather/google.ts +425 -0
- package/src/connectors/weather/index.ts +396 -0
- package/src/connectors/weather/open-meteo.ts +309 -0
- package/src/connectors/weather/rain.ts +73 -0
- package/src/connectors/weather/source.ts +236 -0
- package/src/connectors/weather/sun.ts +114 -0
- package/src/discover.ts +638 -0
- package/src/energy.ts +221 -0
- package/src/finders-entry.ts +2 -0
- package/src/finders.ts +250 -0
- package/src/health.ts +74 -0
- package/src/home.ts +205 -0
- package/src/index.ts +62 -0
- package/src/lane.ts +110 -0
- package/src/locate.ts +85 -0
- package/src/manifest.ts +43 -0
- package/src/manifests.ts +27 -0
- package/src/node-fetch.ts +41 -0
- package/src/poll.ts +33 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { localTime, realClock } from "../../clock.js";
|
|
2
|
+
import { field } from "../../manifest.js";
|
|
3
|
+
import { poller } from "../../poll.js";
|
|
4
|
+
import { blockText, checkBlocks, dayBlocks, } from "./blocks.js";
|
|
5
|
+
import { openMeteo } from "./open-meteo.js";
|
|
6
|
+
import { rainSoon } from "./rain.js";
|
|
7
|
+
import { DEFAULT_POLL_MINUTES, RAIN_HORIZON_MINUTES, sourcePollMinutes, mergeWeather, } from "./source.js";
|
|
8
|
+
import { sunNow } from "./sun.js";
|
|
9
|
+
export { mergeWeather, nextPoll, } from "./source.js";
|
|
10
|
+
export { sunNow } from "./sun.js";
|
|
11
|
+
export { rainSoon } from "./rain.js";
|
|
12
|
+
export { BLOCKS, blockText, dayBlocks, } from "./blocks.js";
|
|
13
|
+
export { bom } from "./bom.js";
|
|
14
|
+
export { openMeteo } from "./open-meteo.js";
|
|
15
|
+
export { google } from "./google.js";
|
|
16
|
+
/** The sun moves about a degree every four minutes; recompute it this often between source reads. */
|
|
17
|
+
const SUN_MS = 5 * 60_000;
|
|
18
|
+
/** However soon a source asks to be called again, never sooner than this. */
|
|
19
|
+
const MIN_POLL_MS = 60_000;
|
|
20
|
+
const SLICE_NAMES = [
|
|
21
|
+
"now",
|
|
22
|
+
"forecast",
|
|
23
|
+
"hourly",
|
|
24
|
+
"sky",
|
|
25
|
+
"rain",
|
|
26
|
+
];
|
|
27
|
+
/** Outside weather as a home integration. Each source polls on its own cadence; the sun is computed, never fetched. */
|
|
28
|
+
export function weather(config) {
|
|
29
|
+
const sources = config.sources ?? [openMeteo()];
|
|
30
|
+
const clock = config.clock ?? realClock;
|
|
31
|
+
const pollMinutes = config.pollMinutes ?? DEFAULT_POLL_MINUTES;
|
|
32
|
+
if (!(pollMinutes >= 1))
|
|
33
|
+
throw new Error(`weather: pollMinutes must be at least 1, got ${config.pollMinutes}`);
|
|
34
|
+
if (config.blocks)
|
|
35
|
+
checkBlocks(config.blocks); // a bad block fails here, not at every report
|
|
36
|
+
const place = { latitude: config.latitude, longitude: config.longitude };
|
|
37
|
+
const readings = new Map();
|
|
38
|
+
const fresh = new Map(); // whether a source's latest read brought anything new, rather than its last good slices again
|
|
39
|
+
let readAt; // the last read that brought something new: the part's `lastGoodAt`, so a re-report is not a read
|
|
40
|
+
const polls = new Map(sources.map((s) => [s.name, poller(clock)]));
|
|
41
|
+
let answer; // reports now: for a refresh no source will answer
|
|
42
|
+
return {
|
|
43
|
+
name: "weather",
|
|
44
|
+
/**
|
|
45
|
+
* Read every source now, whatever its schedule, forecasts included (`force`). A source with a floor (a billed one,
|
|
46
|
+
* Google) is not forced, and is skipped when its last read is younger than its floor.
|
|
47
|
+
*/
|
|
48
|
+
refresh() {
|
|
49
|
+
for (const poll of polls.values())
|
|
50
|
+
poll.nudge();
|
|
51
|
+
if (sources.every((s) => readings.get(s.name)?.stop))
|
|
52
|
+
answer?.(); // every source has stopped: none will read, so say so now
|
|
53
|
+
},
|
|
54
|
+
async run(signal, update) {
|
|
55
|
+
const emit = () => {
|
|
56
|
+
const at = clock.now();
|
|
57
|
+
const slices = mergeWeather(sources.map((source) => ({
|
|
58
|
+
source,
|
|
59
|
+
reading: readings.get(source.name),
|
|
60
|
+
})), at, pollMinutes);
|
|
61
|
+
const report = Object.fromEntries(sources.map((s) => {
|
|
62
|
+
const r = readings.get(s.name);
|
|
63
|
+
return [
|
|
64
|
+
s.name,
|
|
65
|
+
{
|
|
66
|
+
read: r !== undefined,
|
|
67
|
+
problem: r?.problem,
|
|
68
|
+
stopped: r?.stop,
|
|
69
|
+
anomaly: r?.anomaly,
|
|
70
|
+
},
|
|
71
|
+
];
|
|
72
|
+
}));
|
|
73
|
+
// Connected while some source's latest read brought something new; its older slices alone are a failed read, kept as state.
|
|
74
|
+
const status = sources.some((s) => fresh.get(s.name))
|
|
75
|
+
? "connected"
|
|
76
|
+
: sources.every((s) => readings.get(s.name)?.stop)
|
|
77
|
+
? "unavailable"
|
|
78
|
+
: "disconnected";
|
|
79
|
+
const notes = (k) => Object.values(report)
|
|
80
|
+
.map((r) => r[k])
|
|
81
|
+
.filter(Boolean)
|
|
82
|
+
.join("; ") || undefined;
|
|
83
|
+
const detail = [notes("stopped"), notes("problem")].filter(Boolean).join("; ") ||
|
|
84
|
+
undefined;
|
|
85
|
+
const lastGoodAt = status === "connected" && readAt !== undefined
|
|
86
|
+
? new Date(readAt).toISOString()
|
|
87
|
+
: undefined;
|
|
88
|
+
update({
|
|
89
|
+
status,
|
|
90
|
+
detail,
|
|
91
|
+
anomaly: notes("anomaly"),
|
|
92
|
+
lastGoodAt,
|
|
93
|
+
state: {
|
|
94
|
+
...slices,
|
|
95
|
+
sun: sunNow(place.latitude, place.longitude, at),
|
|
96
|
+
rainSoon: rainSoon(slices.rain, at),
|
|
97
|
+
blocks: dayBlocks(slices.hourly, at, config.timeZone, config.blocks),
|
|
98
|
+
timeZone: config.timeZone,
|
|
99
|
+
sources: report,
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
const poll = async (source) => {
|
|
104
|
+
const floorMs = (source.minPollMinutes ?? 0) * 60_000;
|
|
105
|
+
let force = false;
|
|
106
|
+
while (!signal.aborted) {
|
|
107
|
+
const at = clock.now();
|
|
108
|
+
const reading = await source.read({
|
|
109
|
+
place,
|
|
110
|
+
at,
|
|
111
|
+
pollMinutes: sourcePollMinutes(source, pollMinutes),
|
|
112
|
+
force,
|
|
113
|
+
signal,
|
|
114
|
+
});
|
|
115
|
+
if (signal.aborted)
|
|
116
|
+
return; // an aborted read is not a problem to report
|
|
117
|
+
readings.set(source.name, reading);
|
|
118
|
+
const isFresh = SLICE_NAMES.some((k) => reading[k]?.fetchedAt
|
|
119
|
+
? Date.parse(reading[k].fetchedAt) >= at
|
|
120
|
+
: false);
|
|
121
|
+
fresh.set(source.name, isFresh);
|
|
122
|
+
if (isFresh)
|
|
123
|
+
readAt = Math.max(readAt ?? at, at);
|
|
124
|
+
emit();
|
|
125
|
+
if (reading.stop)
|
|
126
|
+
return;
|
|
127
|
+
for (;;) {
|
|
128
|
+
const woken = await polls
|
|
129
|
+
.get(source.name)
|
|
130
|
+
.wait(Math.max(reading.nextAt - clock.now(), MIN_POLL_MS), signal);
|
|
131
|
+
if (woken && clock.now() - at < floorMs) {
|
|
132
|
+
emit(); // a refresh never beats a billed source's floor, but it gets an answer: what there is now
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
force = woken && !floorMs;
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
answer = emit;
|
|
141
|
+
const sun = async () => {
|
|
142
|
+
while (!signal.aborted) {
|
|
143
|
+
await clock.sleep(SUN_MS, signal);
|
|
144
|
+
if (!signal.aborted)
|
|
145
|
+
emit();
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
await Promise.all([...sources.map(poll), sun()]);
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/** "20:20" in the home's time zone (the runtime's when unset), or "?". */
|
|
153
|
+
const hhmmIn = (iso, timeZone) => iso ? localTime(Date.parse(iso), timeZone) : "?";
|
|
154
|
+
const day = (d, rainLeft) => d
|
|
155
|
+
? [
|
|
156
|
+
d.minC === undefined && d.maxC === undefined
|
|
157
|
+
? ""
|
|
158
|
+
: `${d.minC ?? "?"}–${d.maxC ?? "?"} °C`,
|
|
159
|
+
d.text?.replace(/\.$/, "").toLowerCase() ?? "",
|
|
160
|
+
d.rainChance === undefined
|
|
161
|
+
? ""
|
|
162
|
+
: rainLeft !== undefined && rainLeft < d.rainChance
|
|
163
|
+
? `${rainLeft}% chance of rain the rest of the day`
|
|
164
|
+
: `${d.rainChance}% chance of rain`,
|
|
165
|
+
]
|
|
166
|
+
.filter(Boolean)
|
|
167
|
+
.join(" ")
|
|
168
|
+
: "?";
|
|
169
|
+
/** The highest hourly chance left today, all 24 hours of it, when the hourly forecast is the same source and day as the forecast: sources are never mixed. */
|
|
170
|
+
const rainLeftToday = (w, at) => {
|
|
171
|
+
if (!w.hourly || w.hourly.source !== w.forecast?.source)
|
|
172
|
+
return undefined;
|
|
173
|
+
const rest = dayBlocks(w.hourly, at, w.timeZone, [
|
|
174
|
+
{ name: "rest", from: 0, to: 24 },
|
|
175
|
+
])?.today[0];
|
|
176
|
+
return rest?.date === w.forecast.today?.date ? rest?.rainChance : undefined;
|
|
177
|
+
};
|
|
178
|
+
/** "rain in 30 min, ends in 55 min, 70%, 0.8 mm, moderate (google)", "raining, ends in 25 min", or "no rain for 2 h"; nothing when the forecast covers none of the window. */
|
|
179
|
+
export function rainText(rain, r) {
|
|
180
|
+
if (!r || !rain)
|
|
181
|
+
return "";
|
|
182
|
+
const covered = r.coveredMin >= RAIN_HORIZON_MINUTES
|
|
183
|
+
? `${RAIN_HORIZON_MINUTES / 60} h`
|
|
184
|
+
: `${r.coveredMin} min`;
|
|
185
|
+
if (!r.wet)
|
|
186
|
+
return `no rain for ${covered} (${rain.source})`;
|
|
187
|
+
const end = r.endsInMin !== undefined
|
|
188
|
+
? `ends in ${r.endsInMin} min`
|
|
189
|
+
: r.startsInMin === 0
|
|
190
|
+
? `still at ${covered}`
|
|
191
|
+
: "";
|
|
192
|
+
const parts = [
|
|
193
|
+
r.startsInMin === 0 ? "raining" : `rain in ${r.startsInMin} min`,
|
|
194
|
+
end,
|
|
195
|
+
field(r.chance, (v) => `${v}%`),
|
|
196
|
+
field(r.mm, (v) => `${v} mm`),
|
|
197
|
+
r.intensity && r.intensity !== "NO_INTENSITY"
|
|
198
|
+
? r.intensity.toLowerCase()
|
|
199
|
+
: "",
|
|
200
|
+
];
|
|
201
|
+
return `${parts.filter(Boolean).join(", ")} (${rain.source})`;
|
|
202
|
+
}
|
|
203
|
+
/** The weather in one line, every part named by its source and time, never mixed. */
|
|
204
|
+
export function weatherText(w, sun) {
|
|
205
|
+
const n = w.now;
|
|
206
|
+
const hhmm = (iso) => hhmmIn(iso, w.timeZone);
|
|
207
|
+
const readings = n
|
|
208
|
+
? [
|
|
209
|
+
field(n.tempC, (v) => `${v} °C`),
|
|
210
|
+
field(n.apparentC, (v) => `feels ${v}`),
|
|
211
|
+
field(n.humidity, (v) => `${v}%`),
|
|
212
|
+
field(n.rainMm, (v) => `${v} mm since 9am to ${hhmm(n.rainTo)}`),
|
|
213
|
+
]
|
|
214
|
+
.filter(Boolean)
|
|
215
|
+
.join(", ")
|
|
216
|
+
: "";
|
|
217
|
+
const now = n
|
|
218
|
+
? `${n.station ? `${n.station.name} (${n.station.distanceKm.toFixed(1)} km)` : n.source} ${hhmm(n.at)}${n.modelled ? " modelled" : ""}: ${readings}`
|
|
219
|
+
: "";
|
|
220
|
+
const f = w.forecast;
|
|
221
|
+
const forecast = f
|
|
222
|
+
? `today ${day(f.today, rainLeftToday(w, Date.parse(sun.at)))}, tomorrow ${day(f.tomorrow)} (${f.source}${f.modelled ? " modelled" : ""})`
|
|
223
|
+
: "";
|
|
224
|
+
const k = w.sky;
|
|
225
|
+
const skyParts = k
|
|
226
|
+
? [
|
|
227
|
+
field(k.cloudPct, (v) => `cloud ${v}%`),
|
|
228
|
+
field(k.uvIndex, (v) => `UV ${v}`),
|
|
229
|
+
field(k.radiationWm2, (v) => `${v} W/m²`),
|
|
230
|
+
]
|
|
231
|
+
.filter(Boolean)
|
|
232
|
+
.join(", ")
|
|
233
|
+
: "";
|
|
234
|
+
const sky = k ? `${skyParts} (${k.source})` : "";
|
|
235
|
+
const sunText = `sun ${Math.round(sun.altitudeDeg)}°, ${sun.rising ? "rising" : "setting"}, ${sun.daylight ? `sets ${hhmm(sun.next.sunset)}` : `rises ${hhmm(sun.next.sunrise)}`}`;
|
|
236
|
+
return [now, forecast, sky, rainText(w.rain, w.rainSoon), sunText]
|
|
237
|
+
.filter(Boolean)
|
|
238
|
+
.join(" · ");
|
|
239
|
+
}
|
|
240
|
+
/** "today: afternoon …; evening …" and the same for tomorrow, when there is an hourly forecast. */
|
|
241
|
+
const blockLines = (state) => state.blocks
|
|
242
|
+
? ["today", "tomorrow"]
|
|
243
|
+
.filter((d) => state.blocks[d].length)
|
|
244
|
+
.map((d) => `${d}: ${state.blocks[d].map((b) => blockText(b, state.timeZone)).join("; ")} (${state.blocks.source})`)
|
|
245
|
+
: [];
|
|
246
|
+
export const manifest = {
|
|
247
|
+
name: "weather",
|
|
248
|
+
title: "Weather",
|
|
249
|
+
iotClass: "cloud_polling",
|
|
250
|
+
pollSeconds: DEFAULT_POLL_MINUTES * 60,
|
|
251
|
+
access: "BOM anonymous FTP, Open-Meteo, Google with GOOGLE_WEATHER_KEY; sun computed",
|
|
252
|
+
env: [
|
|
253
|
+
{
|
|
254
|
+
name: "HOME_LATITUDE",
|
|
255
|
+
example: "-33.8688",
|
|
256
|
+
what: "the home's position for weather and the sun (this one is Sydney CBD)",
|
|
257
|
+
},
|
|
258
|
+
{ name: "HOME_LONGITUDE", example: "151.2093", what: "" },
|
|
259
|
+
{
|
|
260
|
+
name: "HOME_TIMEZONE",
|
|
261
|
+
example: "Australia/Sydney",
|
|
262
|
+
optional: true,
|
|
263
|
+
what: "the home's IANA time zone for weather times and day blocks; this machine's when unset",
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
name: "HOME_BOM_FORECAST",
|
|
267
|
+
example: "Sydney",
|
|
268
|
+
optional: true,
|
|
269
|
+
what: "BOM précis location by name or aac; unset, the forecast comes from Open-Meteo",
|
|
270
|
+
},
|
|
271
|
+
{
|
|
272
|
+
name: "GOOGLE_WEATHER_KEY",
|
|
273
|
+
example: "",
|
|
274
|
+
optional: true,
|
|
275
|
+
what: "Maps Platform key with the Weather API enabled",
|
|
276
|
+
},
|
|
277
|
+
],
|
|
278
|
+
lines: ({ state }) => state ? [weatherText(state, state.sun), ...blockLines(state)] : [],
|
|
279
|
+
record: ({ state }) => state ? { ...state, line: weatherText(state, state.sun) } : {},
|
|
280
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type Condition, type SourceReading, type WeatherSource } from "./source.ts";
|
|
2
|
+
/** WMO codes in a word: 0–1 clear, 2 partly cloudy, 3 cloudy, then by family. */
|
|
3
|
+
export declare function wmoCondition(code: number): Condition | undefined;
|
|
4
|
+
/** Map one forecast response to slices. Everything here is a model, so every slice says `modelled: true`. `forecastAt` is when the daily part was read. */
|
|
5
|
+
export declare function toSlices(body: unknown, fetchedAt: number, forecastAt?: number): Omit<SourceReading, "nextAt">;
|
|
6
|
+
export type OpenMeteoOptions = {
|
|
7
|
+
fetch?: typeof fetch;
|
|
8
|
+
base?: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Open-Meteo as a weather source: modelled `now`, `forecast`, `hourly` (today and tomorrow) and `sky` (the only source with radiation). `current` is
|
|
12
|
+
* 15-minutely, so it is read every poll; the daily and hourly parts come from models that update hourly at best, so
|
|
13
|
+
* they are read hourly (one call with everything), and the polls between ask for `current` alone.
|
|
14
|
+
*/
|
|
15
|
+
export declare function openMeteo(options?: OpenMeteoOptions): WeatherSource;
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { getJson } from "../../connector.js";
|
|
3
|
+
import { HOURLY_MINUTES, iso, localDate, nextPoll, num, USER_AGENT, } from "./source.js";
|
|
4
|
+
// Open-Meteo's forecast API (https://open-meteo.com/en/docs): keyless, JSON, data CC BY 4.0. No `models`, so the default
|
|
5
|
+
// best_match answers; its BOM model (bom_access_global) has returned only nulls since BOM paused open data in mid 2025.
|
|
6
|
+
const CURRENT = "temperature_2m,apparent_temperature,relative_humidity_2m,wind_speed_10m,wind_direction_10m,wind_gusts_10m,cloud_cover,uv_index,shortwave_radiation,weather_code";
|
|
7
|
+
const HOURLY = "temperature_2m,apparent_temperature,precipitation_probability,precipitation,weather_code,wind_speed_10m,wind_gusts_10m,cloud_cover,uv_index,shortwave_radiation";
|
|
8
|
+
/** How many hours ahead `sky.hours` holds. */
|
|
9
|
+
const SKY_HOURS = 6;
|
|
10
|
+
const DAILY = "temperature_2m_max,temperature_2m_min,precipitation_probability_max,precipitation_sum,weather_code";
|
|
11
|
+
const series = z.array(z.number().nullable()).optional();
|
|
12
|
+
const OpenMeteoResponse = z.looseObject({
|
|
13
|
+
utc_offset_seconds: z.number().optional(),
|
|
14
|
+
current: z.record(z.string(), z.unknown()).optional(),
|
|
15
|
+
hourly: z
|
|
16
|
+
.looseObject({
|
|
17
|
+
time: z.array(z.string()),
|
|
18
|
+
temperature_2m: series,
|
|
19
|
+
apparent_temperature: series,
|
|
20
|
+
precipitation_probability: series,
|
|
21
|
+
precipitation: series,
|
|
22
|
+
weather_code: series,
|
|
23
|
+
wind_speed_10m: series,
|
|
24
|
+
wind_gusts_10m: series,
|
|
25
|
+
cloud_cover: series,
|
|
26
|
+
uv_index: series,
|
|
27
|
+
shortwave_radiation: series,
|
|
28
|
+
})
|
|
29
|
+
.optional(),
|
|
30
|
+
daily: z
|
|
31
|
+
.looseObject({
|
|
32
|
+
time: z.array(z.string()),
|
|
33
|
+
temperature_2m_max: series,
|
|
34
|
+
temperature_2m_min: series,
|
|
35
|
+
precipitation_probability_max: series,
|
|
36
|
+
precipitation_sum: series,
|
|
37
|
+
weather_code: series,
|
|
38
|
+
})
|
|
39
|
+
.optional(),
|
|
40
|
+
});
|
|
41
|
+
/** WMO weather codes as Open-Meteo documents them (the table at the end of its docs page). */
|
|
42
|
+
const WMO = {
|
|
43
|
+
0: "Clear sky",
|
|
44
|
+
1: "Mainly clear",
|
|
45
|
+
2: "Partly cloudy",
|
|
46
|
+
3: "Overcast",
|
|
47
|
+
45: "Fog",
|
|
48
|
+
48: "Depositing rime fog",
|
|
49
|
+
51: "Light drizzle",
|
|
50
|
+
53: "Moderate drizzle",
|
|
51
|
+
55: "Dense drizzle",
|
|
52
|
+
56: "Light freezing drizzle",
|
|
53
|
+
57: "Dense freezing drizzle",
|
|
54
|
+
61: "Slight rain",
|
|
55
|
+
63: "Moderate rain",
|
|
56
|
+
65: "Heavy rain",
|
|
57
|
+
66: "Light freezing rain",
|
|
58
|
+
67: "Heavy freezing rain",
|
|
59
|
+
71: "Slight snow",
|
|
60
|
+
73: "Moderate snow",
|
|
61
|
+
75: "Heavy snow",
|
|
62
|
+
77: "Snow grains",
|
|
63
|
+
80: "Slight rain showers",
|
|
64
|
+
81: "Moderate rain showers",
|
|
65
|
+
82: "Violent rain showers",
|
|
66
|
+
85: "Slight snow showers",
|
|
67
|
+
86: "Heavy snow showers",
|
|
68
|
+
95: "Thunderstorm",
|
|
69
|
+
96: "Thunderstorm with slight hail",
|
|
70
|
+
99: "Thunderstorm with heavy hail",
|
|
71
|
+
};
|
|
72
|
+
/** WMO codes in a word: 0–1 clear, 2 partly cloudy, 3 cloudy, then by family. */
|
|
73
|
+
export function wmoCondition(code) {
|
|
74
|
+
if (!(code in WMO))
|
|
75
|
+
return undefined;
|
|
76
|
+
if (code <= 1)
|
|
77
|
+
return "clear";
|
|
78
|
+
if (code === 2)
|
|
79
|
+
return "partly cloudy";
|
|
80
|
+
if (code === 3)
|
|
81
|
+
return "cloudy";
|
|
82
|
+
if (code < 50)
|
|
83
|
+
return "fog";
|
|
84
|
+
if (code < 60)
|
|
85
|
+
return "drizzle";
|
|
86
|
+
if (code < 70)
|
|
87
|
+
return "rain";
|
|
88
|
+
if (code < 80)
|
|
89
|
+
return "snow";
|
|
90
|
+
if (code < 85)
|
|
91
|
+
return "showers";
|
|
92
|
+
if (code < 95)
|
|
93
|
+
return "snow"; // snow showers
|
|
94
|
+
return "storm";
|
|
95
|
+
}
|
|
96
|
+
/** Open-Meteo's local times ("2026-10-13T20:15") to UTC ISO, using the response's offset; undefined when it does not parse. Open-Meteo applies that one offset to every time it returns, across a daylight-saving change too (checked live, 2026-10-16), so it is right for all of them. */
|
|
97
|
+
const utc = (local, offset) => {
|
|
98
|
+
const t = Date.parse(`${local}Z`);
|
|
99
|
+
return Number.isNaN(t)
|
|
100
|
+
? undefined
|
|
101
|
+
: iso(new Date(t - offset * 1000).toISOString());
|
|
102
|
+
};
|
|
103
|
+
const value = (s, i) => s?.[i] ?? undefined;
|
|
104
|
+
/** Map one forecast response to slices. Everything here is a model, so every slice says `modelled: true`. `forecastAt` is when the daily part was read. */
|
|
105
|
+
export function toSlices(body, fetchedAt, forecastAt = fetchedAt) {
|
|
106
|
+
const r = OpenMeteoResponse.parse(body);
|
|
107
|
+
const offset = r.utc_offset_seconds ?? 0;
|
|
108
|
+
const odd = [];
|
|
109
|
+
const meta = (at) => ({
|
|
110
|
+
source: "open-meteo",
|
|
111
|
+
modelled: true,
|
|
112
|
+
at,
|
|
113
|
+
fetchedAt: new Date(fetchedAt).toISOString(),
|
|
114
|
+
});
|
|
115
|
+
const c = r.current;
|
|
116
|
+
const cur = (k) => num(c?.[k], `current.${k}`, odd);
|
|
117
|
+
const code = cur("weather_code");
|
|
118
|
+
const nowAt = typeof c?.time === "string" ? utc(c.time, offset) : undefined;
|
|
119
|
+
if (c && !nowAt)
|
|
120
|
+
odd.push(`current.time not understood: ${String(c.time)}`);
|
|
121
|
+
const now = nowAt
|
|
122
|
+
? {
|
|
123
|
+
...meta(nowAt),
|
|
124
|
+
tempC: cur("temperature_2m"),
|
|
125
|
+
apparentC: cur("apparent_temperature"),
|
|
126
|
+
humidity: cur("relative_humidity_2m"),
|
|
127
|
+
windKmh: cur("wind_speed_10m"),
|
|
128
|
+
windDeg: cur("wind_direction_10m"),
|
|
129
|
+
gustKmh: cur("wind_gusts_10m"),
|
|
130
|
+
}
|
|
131
|
+
: undefined;
|
|
132
|
+
const h = r.hourly;
|
|
133
|
+
const unknownCodes = new Set();
|
|
134
|
+
const hours = (h?.time ?? []).flatMap((t, i) => {
|
|
135
|
+
const at = utc(t, offset);
|
|
136
|
+
if (!at)
|
|
137
|
+
odd.push(`hourly time not understood: ${t}`);
|
|
138
|
+
if (!at)
|
|
139
|
+
return [];
|
|
140
|
+
const wmo = value(h?.weather_code, i);
|
|
141
|
+
if (wmo !== undefined && !(wmo in WMO))
|
|
142
|
+
unknownCodes.add(wmo);
|
|
143
|
+
const hour = {
|
|
144
|
+
at,
|
|
145
|
+
tempC: value(h?.temperature_2m, i),
|
|
146
|
+
apparentC: value(h?.apparent_temperature, i),
|
|
147
|
+
rainChance: value(h?.precipitation_probability, i),
|
|
148
|
+
rainMm: value(h?.precipitation, i),
|
|
149
|
+
windKmh: value(h?.wind_speed_10m, i),
|
|
150
|
+
gustKmh: value(h?.wind_gusts_10m, i),
|
|
151
|
+
cloudPct: value(h?.cloud_cover, i),
|
|
152
|
+
uvIndex: value(h?.uv_index, i),
|
|
153
|
+
radiationWm2: value(h?.shortwave_radiation, i),
|
|
154
|
+
code: wmo === undefined ? undefined : String(wmo),
|
|
155
|
+
condition: wmo === undefined ? undefined : wmoCondition(wmo),
|
|
156
|
+
};
|
|
157
|
+
return [hour];
|
|
158
|
+
});
|
|
159
|
+
for (const code of unknownCodes)
|
|
160
|
+
odd.push(`unknown WMO code ${code} in the hourly forecast`);
|
|
161
|
+
// The hourly part is read once an hour; the sky's hours run from the hour it was read in.
|
|
162
|
+
const skyHours = hours
|
|
163
|
+
.filter((x) => Date.parse(x.at) + 3_600_000 > forecastAt)
|
|
164
|
+
.slice(0, SKY_HOURS)
|
|
165
|
+
.map((x) => ({
|
|
166
|
+
at: x.at,
|
|
167
|
+
cloudPct: x.cloudPct,
|
|
168
|
+
uvIndex: x.uvIndex,
|
|
169
|
+
radiationWm2: x.radiationWm2,
|
|
170
|
+
}));
|
|
171
|
+
const sky = now
|
|
172
|
+
? {
|
|
173
|
+
...meta(now.at),
|
|
174
|
+
cloudPct: cur("cloud_cover"),
|
|
175
|
+
uvIndex: cur("uv_index"),
|
|
176
|
+
radiationWm2: cur("shortwave_radiation"),
|
|
177
|
+
hours: skyHours,
|
|
178
|
+
}
|
|
179
|
+
: undefined;
|
|
180
|
+
const d = r.daily;
|
|
181
|
+
const day = (date) => {
|
|
182
|
+
const i = d?.time.indexOf(date) ?? -1;
|
|
183
|
+
if (!d || i < 0)
|
|
184
|
+
return undefined;
|
|
185
|
+
const wmo = value(d.weather_code, i);
|
|
186
|
+
return {
|
|
187
|
+
date,
|
|
188
|
+
minC: value(d.temperature_2m_min, i),
|
|
189
|
+
maxC: value(d.temperature_2m_max, i),
|
|
190
|
+
rainChance: value(d.precipitation_probability_max, i),
|
|
191
|
+
rainMm: value(d.precipitation_sum, i),
|
|
192
|
+
text: wmo === undefined ? undefined : WMO[wmo],
|
|
193
|
+
code: wmo === undefined ? undefined : String(wmo),
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
const today = localDate(fetchedAt, offset);
|
|
197
|
+
const tomorrow = localDate(fetchedAt + 86_400_000, offset);
|
|
198
|
+
const forecastIso = new Date(forecastAt).toISOString();
|
|
199
|
+
const forecast = d
|
|
200
|
+
? {
|
|
201
|
+
...meta(forecastIso),
|
|
202
|
+
fetchedAt: forecastIso,
|
|
203
|
+
today: day(today),
|
|
204
|
+
tomorrow: day(tomorrow),
|
|
205
|
+
}
|
|
206
|
+
: undefined;
|
|
207
|
+
const hourly = hours.length
|
|
208
|
+
? {
|
|
209
|
+
...meta(forecastIso),
|
|
210
|
+
fetchedAt: forecastIso,
|
|
211
|
+
hours: hours.map(({ radiationWm2: _, ...x }) => x),
|
|
212
|
+
}
|
|
213
|
+
: undefined;
|
|
214
|
+
for (const [i, wmo] of (d?.weather_code ?? []).entries())
|
|
215
|
+
if (wmo !== null && !(wmo in WMO))
|
|
216
|
+
odd.push(`unknown WMO code ${wmo} on ${d?.time[i]}`);
|
|
217
|
+
if (code !== undefined && !(code in WMO))
|
|
218
|
+
odd.push(`unknown WMO code ${code} now`);
|
|
219
|
+
return {
|
|
220
|
+
now,
|
|
221
|
+
forecast,
|
|
222
|
+
hourly,
|
|
223
|
+
sky,
|
|
224
|
+
anomaly: odd.length ? odd.join("; ") : undefined,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Open-Meteo as a weather source: modelled `now`, `forecast`, `hourly` (today and tomorrow) and `sky` (the only source with radiation). `current` is
|
|
229
|
+
* 15-minutely, so it is read every poll; the daily and hourly parts come from models that update hourly at best, so
|
|
230
|
+
* they are read hourly (one call with everything), and the polls between ask for `current` alone.
|
|
231
|
+
*/
|
|
232
|
+
export function openMeteo(options = {}) {
|
|
233
|
+
const fetcher = options.fetch ?? fetch;
|
|
234
|
+
const base = options.base ?? "https://api.open-meteo.com/v1/forecast";
|
|
235
|
+
let last = {};
|
|
236
|
+
let full;
|
|
237
|
+
let fullDue = 0;
|
|
238
|
+
return {
|
|
239
|
+
name: "open-meteo",
|
|
240
|
+
owns: {
|
|
241
|
+
now: 30 * 60_000,
|
|
242
|
+
forecast: 6 * 3_600_000,
|
|
243
|
+
hourly: 6 * 3_600_000,
|
|
244
|
+
sky: 30 * 60_000,
|
|
245
|
+
}, // `current.time` starts its 15-minute interval
|
|
246
|
+
async read({ place, at, pollMinutes, force, signal, }) {
|
|
247
|
+
const everything = force || !full || at >= fullDue;
|
|
248
|
+
const q = new URLSearchParams({
|
|
249
|
+
latitude: String(place.latitude),
|
|
250
|
+
longitude: String(place.longitude),
|
|
251
|
+
timezone: "auto",
|
|
252
|
+
current: CURRENT,
|
|
253
|
+
});
|
|
254
|
+
if (everything)
|
|
255
|
+
for (const [k, v] of Object.entries({
|
|
256
|
+
hourly: HOURLY,
|
|
257
|
+
daily: DAILY,
|
|
258
|
+
forecast_days: "2",
|
|
259
|
+
}))
|
|
260
|
+
q.set(k, v);
|
|
261
|
+
try {
|
|
262
|
+
const body = (await getJson(fetcher, `${base}?${q}`, {
|
|
263
|
+
init: { headers: { "user-agent": USER_AGENT } },
|
|
264
|
+
signal,
|
|
265
|
+
}));
|
|
266
|
+
if (everything)
|
|
267
|
+
((full = { body, at }), (fullDue = nextPoll(at, HOURLY_MINUTES)));
|
|
268
|
+
last = toSlices({
|
|
269
|
+
...full.body,
|
|
270
|
+
current: body.current,
|
|
271
|
+
utc_offset_seconds: body.utc_offset_seconds,
|
|
272
|
+
}, at, full.at);
|
|
273
|
+
return { ...last, nextAt: nextPoll(at, pollMinutes) };
|
|
274
|
+
}
|
|
275
|
+
catch (error) {
|
|
276
|
+
return {
|
|
277
|
+
...last,
|
|
278
|
+
anomaly: undefined,
|
|
279
|
+
problem: `open-meteo: ${error.message}`,
|
|
280
|
+
nextAt: nextPoll(at, pollMinutes),
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
};
|
|
285
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type WeatherRain } from "./source.ts";
|
|
2
|
+
/** What the rain forecast says about the next stretch. The one owner: every consumer calls `rainSoon`. */
|
|
3
|
+
export type RainSoon = {
|
|
4
|
+
wet: boolean;
|
|
5
|
+
startsInMin?: number;
|
|
6
|
+
endsInMin?: number;
|
|
7
|
+
chance?: number;
|
|
8
|
+
mm?: number;
|
|
9
|
+
intensity?: string;
|
|
10
|
+
type?: string;
|
|
11
|
+
coveredMin: number;
|
|
12
|
+
};
|
|
13
|
+
/** Google's intensities, lightest first. An unknown one is noted by the source, never ranked. */
|
|
14
|
+
export declare const RAIN_INTENSITIES: string[];
|
|
15
|
+
/**
|
|
16
|
+
* Rain in the next `windowMin` minutes from `at` (epoch ms). A segment counts while its end is still ahead, so one that began a few minutes ago is used.
|
|
17
|
+
* A segment with no `type` is unknown, not dry: it adds nothing to `coveredMin`. Returns nothing when the forecast covers none of the window.
|
|
18
|
+
*/
|
|
19
|
+
export declare function rainSoon(rain: WeatherRain | undefined, at: number, windowMin?: number): RainSoon | undefined;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { RAIN_HORIZON_MINUTES } from "./source.js";
|
|
2
|
+
const WET = (type) => type !== undefined && type !== "NONE";
|
|
3
|
+
/** Google's intensities, lightest first. An unknown one is noted by the source, never ranked. */
|
|
4
|
+
export const RAIN_INTENSITIES = ["NO_INTENSITY", "LIGHT", "MODERATE", "HEAVY"];
|
|
5
|
+
const minutes = (ms) => Math.round(ms / 60_000);
|
|
6
|
+
/**
|
|
7
|
+
* Rain in the next `windowMin` minutes from `at` (epoch ms). A segment counts while its end is still ahead, so one that began a few minutes ago is used.
|
|
8
|
+
* A segment with no `type` is unknown, not dry: it adds nothing to `coveredMin`. Returns nothing when the forecast covers none of the window.
|
|
9
|
+
*/
|
|
10
|
+
export function rainSoon(rain, at, windowMin = RAIN_HORIZON_MINUTES) {
|
|
11
|
+
if (!rain)
|
|
12
|
+
return undefined;
|
|
13
|
+
const end = at + windowMin * 60_000;
|
|
14
|
+
const segs = rain.segments
|
|
15
|
+
.map((s) => ({ ...s, f: Date.parse(s.from), t: Date.parse(s.to) }))
|
|
16
|
+
.filter((s) => s.t > at && s.f < end && s.type !== undefined)
|
|
17
|
+
.sort((a, b) => a.f - b.f);
|
|
18
|
+
const covered = segs.reduce((sum, s) => sum + Math.min(s.t, end) - Math.max(s.f, at), 0);
|
|
19
|
+
if (covered <= 0)
|
|
20
|
+
return undefined;
|
|
21
|
+
const coveredMin = minutes(covered);
|
|
22
|
+
const first = segs.findIndex((s) => WET(s.type));
|
|
23
|
+
if (first < 0)
|
|
24
|
+
return { wet: false, coveredMin };
|
|
25
|
+
let last = first;
|
|
26
|
+
while (last + 1 < segs.length &&
|
|
27
|
+
WET(segs[last + 1].type) &&
|
|
28
|
+
segs[last + 1].f <= segs[last].t)
|
|
29
|
+
last++;
|
|
30
|
+
const run = segs.slice(first, last + 1);
|
|
31
|
+
const chances = run.map((s) => s.chance).filter((c) => c !== undefined);
|
|
32
|
+
const mms = run.map((s) => s.mm);
|
|
33
|
+
const heaviest = run
|
|
34
|
+
.map((s) => s.intensity)
|
|
35
|
+
.filter((i) => i !== undefined)
|
|
36
|
+
.sort((a, b) => RAIN_INTENSITIES.indexOf(b) - RAIN_INTENSITIES.indexOf(a))[0];
|
|
37
|
+
const runEnd = run[run.length - 1].t;
|
|
38
|
+
const after = segs[last + 1]; // a known dry segment right after the run proves it ended
|
|
39
|
+
return {
|
|
40
|
+
wet: true,
|
|
41
|
+
startsInMin: Math.max(0, minutes(run[0].f - at)),
|
|
42
|
+
endsInMin: after && after.f <= runEnd ? minutes(runEnd - at) : undefined,
|
|
43
|
+
chance: chances.length ? Math.max(...chances) : undefined,
|
|
44
|
+
mm: mms.every((m) => m !== undefined)
|
|
45
|
+
? mms.reduce((sum, m) => sum + m, 0)
|
|
46
|
+
: undefined,
|
|
47
|
+
intensity: heaviest,
|
|
48
|
+
type: run[0].type,
|
|
49
|
+
coveredMin,
|
|
50
|
+
};
|
|
51
|
+
}
|