feat(signage): the weather all the way: a week, the next hours, feels like, humidity, wind, rain chance, sunrise / sunset, UV, in words; row, column, card, list of days or hourly layouts
services/weather.py asks Open-Meteo for 7 days, the next 24 hours and the details (mph for Fahrenheit); older answers without them still read. The widget's layout, the details it adds and the place's name; up to 7 days. Plan 6R.18, F-116. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -263,9 +263,14 @@ class Widget(_Strict):
|
||||
qr_target: Literal["ordering", "menu", "custom"] = "ordering"
|
||||
qr_url: Optional[str] = Field(default=None, max_length=300)
|
||||
qr_caption: Optional[str] = Field(default=None, max_length=80)
|
||||
weather_days: int = Field(default=1, ge=1, le=3) # how many days, from weather_from
|
||||
weather_from: int = Field(default=0, ge=0, le=2) # 0 today (now), 1 tomorrow, 2 the day after
|
||||
weather_days: int = Field(default=1, ge=1, le=7) # how many days, from weather_from (6R.18: up to a week)
|
||||
weather_from: int = Field(default=0, ge=0, le=6) # 0 today (now), 1 tomorrow, 2 the day after…
|
||||
weather_style: Literal["both", "icon", "text"] = "both" # picture and degrees, the picture, the degrees
|
||||
# 6R.18: how it is laid out, the details it adds, the hours of an hourly one, the place's name
|
||||
weather_layout: Literal["row", "column", "card", "list", "hourly"] = "row"
|
||||
weather_details: List[Literal["feels", "humidity", "wind", "rain", "sun", "uv", "words"]] = Field(default_factory=list, max_length=7)
|
||||
weather_hours: int = Field(default=6, ge=3, le=12)
|
||||
weather_place: bool = False
|
||||
board: Optional[MenuList] = None # menu: what it lists and how
|
||||
# its look (owner review 5, 2026-11-21): None = the slide's
|
||||
vertical: Literal["auto", "top", "middle", "bottom"] = "auto" # inside its place (a stack's part, its box)
|
||||
@@ -367,8 +372,8 @@ class Widget(_Strict):
|
||||
self.board = MenuList(select="all", columns=1)
|
||||
if self.kind != "menu":
|
||||
self.board = None
|
||||
if self.weather_from + self.weather_days > 3:
|
||||
self.weather_days = 3 - self.weather_from # the forecast has three days
|
||||
if self.weather_from + self.weather_days > 7:
|
||||
self.weather_days = 7 - self.weather_from # the forecast has seven days
|
||||
return self
|
||||
|
||||
@field_validator("qr_url")
|
||||
|
||||
@@ -6,7 +6,10 @@ editor from a town search). `weather_worker` (a signage module worker) asks Open
|
||||
key) every 30 minutes while it has internet and keeps the last answer in memory. A forecast older
|
||||
than 3 hours counts as none: the screens get `weather: null` and hide the widget.
|
||||
|
||||
current() → {"place", "temperature", "code", "is_day", "days": [{date, max, min, code}], "units", "at"} | None
|
||||
current() → {"place", "temperature", "code", "is_day", "feels_like", "humidity", "wind", "wind_dir",
|
||||
"precipitation", "days": [{date, max, min, code, rain, wind, uv, sunrise, sunset}] (7),
|
||||
"hours": [{time, temp, code, rain, is_day}] (the next 24), "units", "wind_unit", "at"} | None
|
||||
(6R.18: everything the detailed weather widget can show; older answers lack some keys)
|
||||
search(name) → places for the editor's search (name, country, region, latitude, longitude)
|
||||
"""
|
||||
from __future__ import annotations
|
||||
@@ -68,9 +71,12 @@ def _key(where: dict) -> tuple:
|
||||
|
||||
|
||||
async def fetch(where: dict, client: httpx.AsyncClient | None = None) -> dict:
|
||||
params = {"latitude": where["latitude"], "longitude": where["longitude"], "timezone": "auto", "forecast_days": 3,
|
||||
"current": "temperature_2m,weather_code,is_day", "daily": "temperature_2m_max,temperature_2m_min,weather_code",
|
||||
"temperature_unit": where["units"]}
|
||||
imperial = where["units"] == "fahrenheit"
|
||||
params = {"latitude": where["latitude"], "longitude": where["longitude"], "timezone": "auto", "forecast_days": 7,
|
||||
"current": "temperature_2m,weather_code,is_day,apparent_temperature,relative_humidity_2m,wind_speed_10m,wind_direction_10m,precipitation",
|
||||
"daily": "temperature_2m_max,temperature_2m_min,weather_code,precipitation_probability_max,wind_speed_10m_max,uv_index_max,sunrise,sunset",
|
||||
"hourly": "temperature_2m,weather_code,precipitation_probability,is_day",
|
||||
"temperature_unit": where["units"], "wind_speed_unit": "mph" if imperial else "kmh"}
|
||||
own = client is None
|
||||
client = client or httpx.AsyncClient(timeout=TIMEOUT_S)
|
||||
try:
|
||||
@@ -80,12 +86,26 @@ async def fetch(where: dict, client: httpx.AsyncClient | None = None) -> dict:
|
||||
finally:
|
||||
if own:
|
||||
await client.aclose()
|
||||
cur, daily = j.get("current") or {}, j.get("daily") or {}
|
||||
days = [{"date": d, "max": mx, "min": mn, "code": c}
|
||||
for d, mx, mn, c in zip(daily.get("time", []), daily.get("temperature_2m_max", []),
|
||||
daily.get("temperature_2m_min", []), daily.get("weather_code", []))]
|
||||
cur, daily, hourly = j.get("current") or {}, j.get("daily") or {}, j.get("hourly") or {}
|
||||
col = lambda src, key, n: (src.get(key) or [None] * n)[:n] + [None] * max(0, n - len(src.get(key) or [])) # noqa: E731
|
||||
n = len(daily.get("time", []))
|
||||
hm = lambda v: v[11:16] if isinstance(v, str) and len(v) >= 16 else None # noqa: E731 "2026-11-17T07:12" → "07:12"
|
||||
days = [{"date": d, "max": mx, "min": mn, "code": c, "rain": rp, "wind": wm, "uv": uv, "sunrise": hm(sr), "sunset": hm(ss)}
|
||||
for d, mx, mn, c, rp, wm, uv, sr, ss in zip(daily.get("time", []), col(daily, "temperature_2m_max", n), col(daily, "temperature_2m_min", n),
|
||||
col(daily, "weather_code", n), col(daily, "precipitation_probability_max", n),
|
||||
col(daily, "wind_speed_10m_max", n), col(daily, "uv_index_max", n),
|
||||
col(daily, "sunrise", n), col(daily, "sunset", n))]
|
||||
# the next 24 hours from now (the place's own time: Open-Meteo answers in it)
|
||||
times = hourly.get("time", [])
|
||||
start = next((i for i, x in enumerate(times) if x >= (cur.get("time") or "")[:13] + ":00"), 0)
|
||||
m = len(times)
|
||||
hours = [{"time": hm(t), "temp": tp, "code": c, "rain": rp, "is_day": bool(dy if dy is not None else 1)}
|
||||
for t, tp, c, rp, dy in list(zip(times, col(hourly, "temperature_2m", m), col(hourly, "weather_code", m),
|
||||
col(hourly, "precipitation_probability", m), col(hourly, "is_day", m)))[start:start + 24]]
|
||||
return {"place": where["name"], "temperature": cur.get("temperature_2m"), "code": cur.get("weather_code"),
|
||||
"is_day": bool(cur.get("is_day", 1)), "days": days, "units": where["units"], "at": cur.get("time")}
|
||||
"is_day": bool(cur.get("is_day", 1)), "feels_like": cur.get("apparent_temperature"), "humidity": cur.get("relative_humidity_2m"),
|
||||
"wind": cur.get("wind_speed_10m"), "wind_dir": cur.get("wind_direction_10m"), "precipitation": cur.get("precipitation"),
|
||||
"days": days, "hours": hours, "units": where["units"], "wind_unit": "mph" if imperial else "km/h", "at": cur.get("time")}
|
||||
|
||||
|
||||
async def refresh(db, client: httpx.AsyncClient | None = None) -> bool:
|
||||
|
||||
@@ -104,6 +104,8 @@ def test_the_weather_shows_with_internet_and_hides_without(client, owner_h, tv,
|
||||
assert ("signage.changed", [tv["id"]]) in sent
|
||||
w = client.get("/api/signage/screen", headers=tv["h"]).json()["venue"]["weather"]
|
||||
assert w["place"] == "Kalavryta" and w["code"] == 2 and [d["max"] for d in w["days"]] == [19.0, 16.5] and w["units"] == "celsius"
|
||||
# 6R.18: an answer without the detailed fields still reads (they are None)
|
||||
assert w["feels_like"] is None and w["days"][0]["sunrise"] is None and w["hours"] == [] and w["wind_unit"] == "km/h"
|
||||
# the internet goes: the last forecast stays while it is fresh, then the widget hides
|
||||
internet["up"] = False
|
||||
sent.clear()
|
||||
@@ -128,3 +130,28 @@ def test_who_may(client, make_user, login):
|
||||
waiter = login(make_user(role="waiter"))
|
||||
assert client.put("/api/signage/message", headers=waiter, json={"text": "x"}).status_code == 403
|
||||
assert client.get("/api/signage/weather", headers=waiter).status_code == 403
|
||||
|
||||
|
||||
def test_the_detailed_weather(monkeypatch):
|
||||
"""6R.18: humidity, feels like, wind, rain chance, sunrise / sunset, UV and the next 24 hours."""
|
||||
import asyncio
|
||||
import httpx
|
||||
from services import weather
|
||||
hours = [f"2026-11-17T{h:02d}:00" for h in range(24)] + [f"2026-11-18T{h:02d}:00" for h in range(24)]
|
||||
answer = {"current": {"time": "2026-11-17T12:15", "temperature_2m": 17.4, "weather_code": 2, "is_day": 1, "apparent_temperature": 15.9,
|
||||
"relative_humidity_2m": 62, "wind_speed_10m": 12.3, "wind_direction_10m": 200, "precipitation": 0},
|
||||
"daily": {"time": ["2026-11-17", "2026-11-18"], "temperature_2m_max": [19.0, 16.5], "temperature_2m_min": [9.1, 8.0],
|
||||
"weather_code": [2, 61], "precipitation_probability_max": [10, 80], "wind_speed_10m_max": [20, 35], "uv_index_max": [3.2, 1.0],
|
||||
"sunrise": ["2026-11-17T07:12", "2026-11-18T07:13"], "sunset": ["2026-11-17T17:08", "2026-11-18T17:07"]},
|
||||
"hourly": {"time": hours, "temperature_2m": list(range(48)), "weather_code": [0] * 48, "precipitation_probability": [5] * 48, "is_day": [1] * 48}}
|
||||
seen = {}
|
||||
|
||||
def handler(req):
|
||||
seen.update(dict(req.url.params))
|
||||
return httpx.Response(200, json=answer)
|
||||
c = httpx.AsyncClient(transport=httpx.MockTransport(handler))
|
||||
w = asyncio.run(weather.fetch({"name": "X", "latitude": 1, "longitude": 2, "units": "fahrenheit"}, c))
|
||||
assert seen["forecast_days"] == "7" and seen["wind_speed_unit"] == "mph" and "hourly" in seen
|
||||
assert w["feels_like"] == 15.9 and w["humidity"] == 62 and w["wind"] == 12.3 and w["wind_unit"] == "mph"
|
||||
assert w["days"][1] == {"date": "2026-11-18", "max": 16.5, "min": 8.0, "code": 61, "rain": 80, "wind": 35, "uv": 1.0, "sunrise": "07:13", "sunset": "17:07"}
|
||||
assert len(w["hours"]) == 24 and w["hours"][0]["time"] == "12:00" and w["hours"][0]["temp"] == 12 and w["hours"][-1]["time"] == "11:00"
|
||||
|
||||
@@ -41,7 +41,7 @@ import MoreSettings from './MoreSettings'
|
||||
import { MoreBelowFields as MoreBelow, MoreInlineFields as MoreInline } from './MoreWidgetFields'
|
||||
import { MediaPick, Section } from './SignageFields'
|
||||
import {
|
||||
CELL_KINDS, cellWidget, fillPlace, FONT_KEYS, WIDGET_COLOURS, moved, newWidget, pickedAfterMove, presetWidget, WEATHER_CHOICES, weatherChoice, WIDGET_KINDS, WIDGET_POSITIONS,
|
||||
CELL_KINDS, cellWidget, WEATHER_DETAILS, WEATHER_LAYOUTS, fillPlace, FONT_KEYS, WIDGET_COLOURS, moved, newWidget, pickedAfterMove, presetWidget, WEATHER_CHOICES, weatherChoice, WIDGET_KINDS, WIDGET_POSITIONS,
|
||||
} from './signageLogic'
|
||||
|
||||
const ICON = { logo: ImageIcon, name: Store, date: CalendarDays, clock: Clock, message: MessageSquare, text: Type, qr: QrCode, weather: CloudSun, menu: UtensilsCrossed, hstack: Columns3, vstack: Rows3,
|
||||
@@ -297,6 +297,10 @@ function InlineFields({ w, set }) {
|
||||
onChange={v => { const [from, days] = v.split('-').map(Number); set({ weather_from: from, weather_days: days }) }}
|
||||
options={WEATHER_CHOICES.map(([from, days]) => ({ value: `${from}-${days}`, label: t(`signage.widgets.weatherChoices.${from}-${days}`) }))} />
|
||||
</Field>
|
||||
<Field label={t('signage.widgets.weatherLayout')}>
|
||||
<Dropdown value={w.weather_layout || 'row'} onChange={weather_layout => set({ weather_layout })} data-testid="signage-widget-weather-layout"
|
||||
options={WEATHER_LAYOUTS.map(s => ({ value: s, label: t(`signage.widgets.weatherLayouts.${s}`) }))} />
|
||||
</Field>
|
||||
<Field label={t('signage.widgets.weatherStyle')}>
|
||||
<Dropdown value={w.weather_style || 'both'} onChange={weather_style => set({ weather_style })} data-testid="signage-widget-weather-style"
|
||||
options={['both', 'icon', 'text'].map(s => ({ value: s, label: t(`signage.widgets.weatherStyles.${s}`) }))} />
|
||||
@@ -315,7 +319,27 @@ function BelowFields({ w, set, media, onMedia, extras, path, depth, selected, on
|
||||
onChange={item => { onMedia(item); set({ media_id: item.id }) }} />
|
||||
}
|
||||
if (w.kind === 'message') return <MessageHint />
|
||||
if (w.kind === 'weather') return <p className="text-caption text-ink-3 -mt-2">{t('signage.widgets.weatherNeedsInternet')}</p>
|
||||
if (w.kind === 'weather') {
|
||||
return (
|
||||
<div className="grid gap-3">
|
||||
<div className={ROW}>
|
||||
<Field label={t('signage.widgets.weatherDetails')} className="col-span-2">
|
||||
<Dropdown multiple value={w.weather_details || []} onChange={weather_details => set({ weather_details })} allLabel={t('signage.widgets.weatherNoDetails')}
|
||||
data-testid="signage-widget-weather-details" options={WEATHER_DETAILS.map(d => ({ value: d, label: t(`signage.widgets.weatherDetail.${d}`) }))} />
|
||||
</Field>
|
||||
{w.weather_layout === 'hourly' && (
|
||||
<Field label={t('signage.widgets.weatherHours')}>
|
||||
<Dropdown value={w.weather_hours || 6} onChange={v => set({ weather_hours: Number(v) })} options={[3, 4, 6, 8, 10, 12].map(n => ({ value: n, label: String(n) }))} />
|
||||
</Field>
|
||||
)}
|
||||
<Field label={t('signage.widgets.weatherPlace')}>
|
||||
<Switch boxed className="h-control" checked={!!w.weather_place} onChange={weather_place => set({ weather_place })} label={t('signage.widgets.weatherPlaceLabel')} />
|
||||
</Field>
|
||||
</div>
|
||||
<p className="text-caption text-ink-3 -mt-1">{t('signage.widgets.weatherNeedsInternet')}</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
if (w.kind === 'qr') {
|
||||
return (
|
||||
<div className={ROW}>
|
||||
|
||||
@@ -174,7 +174,10 @@ export function cellWidget(kind) {
|
||||
}
|
||||
|
||||
/** The weather's days, as one choice: from (0 today, 1 tomorrow, 2 the day after) and how many. */
|
||||
export const WEATHER_CHOICES = [[0, 1], [0, 2], [0, 3], [1, 1], [2, 1]]
|
||||
export const WEATHER_CHOICES = [[0, 1], [0, 2], [0, 3], [0, 5], [0, 7], [1, 1], [2, 1], [1, 3]]
|
||||
/** The weather's layouts and details (6R.18). */
|
||||
export const WEATHER_LAYOUTS = ['row', 'column', 'card', 'list', 'hourly']
|
||||
export const WEATHER_DETAILS = ['words', 'feels', 'humidity', 'wind', 'rain', 'sun', 'uv']
|
||||
export const weatherChoice = w => `${w.weather_from || 0}-${w.weather_days || 1}`
|
||||
|
||||
/** The picked widget's index after moving the one at `from` to `to` (the list reordered). */
|
||||
|
||||
@@ -1553,7 +1553,10 @@
|
||||
"0-2": "Σήμερα και αύριο",
|
||||
"0-3": "Σήμερα και τις 2 επόμενες",
|
||||
"1-1": "Αύριο",
|
||||
"2-1": "Μεθαύριο"
|
||||
"2-1": "Μεθαύριο",
|
||||
"0-5": "Σήμερα και οι επόμενες 4 μέρες",
|
||||
"0-7": "Όλη η εβδομάδα",
|
||||
"1-3": "Αύριο και οι 2 επόμενες"
|
||||
},
|
||||
"menuEdit_one": "Προϊόντα και εμφάνιση ({{count}} προϊόν)",
|
||||
"menuEdit_other": "Προϊόντα και εμφάνιση ({{count}} προϊόντα)",
|
||||
@@ -1707,7 +1710,29 @@
|
||||
"toMedia": "Φτιάξτε έναν στη σελίδα Πολυμέσα",
|
||||
"add": "Προσθήκη εικόνων ή βίντεο",
|
||||
"empty": "Η συλλογή είναι άδεια ακόμη: μένει κρυφή."
|
||||
}
|
||||
},
|
||||
"weatherLayout": "Διάταξη",
|
||||
"weatherLayouts": {
|
||||
"row": "Σε σειρά",
|
||||
"column": "Σε στήλη",
|
||||
"card": "Κάρτα",
|
||||
"list": "Λίστα ημερών",
|
||||
"hourly": "Οι επόμενες ώρες"
|
||||
},
|
||||
"weatherDetails": "Δείχνει επίσης",
|
||||
"weatherNoDetails": "Τίποτα άλλο",
|
||||
"weatherDetail": {
|
||||
"words": "Τον καιρό με λέξεις",
|
||||
"feels": "Αίσθηση θερμοκρασίας",
|
||||
"humidity": "Υγρασία",
|
||||
"wind": "Άνεμο",
|
||||
"rain": "Πιθανότητα βροχής",
|
||||
"sun": "Ανατολή και δύση",
|
||||
"uv": "Δείκτη UV"
|
||||
},
|
||||
"weatherHours": "Ώρες",
|
||||
"weatherPlace": "Η τοποθεσία",
|
||||
"weatherPlaceLabel": "Δείχνει το όνομά της"
|
||||
},
|
||||
"tabs": {
|
||||
"playlists": "Λίστες",
|
||||
|
||||
@@ -492,7 +492,22 @@
|
||||
"title": "Βαθμονόμηση εικόνας: {{name}}",
|
||||
"hint": "Στον διαχειριστή (Ψηφιακή σήμανση → Οθόνες) μετακινήστε κάθε άκρη μέχρι τα βέλη να ακουμπούν τις άκρες της οθόνης."
|
||||
},
|
||||
"closed": "Κλειστά"
|
||||
"closed": "Κλειστά",
|
||||
"weather": {
|
||||
"feels": "Αίσθηση {{t}}",
|
||||
"today": "Σήμερα",
|
||||
"words": {
|
||||
"sun": "Ηλιοφάνεια",
|
||||
"moon": "Αίθριος",
|
||||
"cloud-sun": "Λίγα σύννεφα",
|
||||
"cloud-moon": "Λίγα σύννεφα",
|
||||
"cloud": "Συννεφιά",
|
||||
"fog": "Ομίχλη",
|
||||
"rain": "Βροχή",
|
||||
"snow": "Χιόνι",
|
||||
"storm": "Καταιγίδα"
|
||||
}
|
||||
}
|
||||
},
|
||||
"menuTablet": {
|
||||
"title": "Μενού",
|
||||
|
||||
@@ -1553,7 +1553,10 @@
|
||||
"0-2": "Today and tomorrow",
|
||||
"0-3": "Today and the next 2 days",
|
||||
"1-1": "Tomorrow",
|
||||
"2-1": "The day after tomorrow"
|
||||
"2-1": "The day after tomorrow",
|
||||
"0-5": "Today and the next 4 days",
|
||||
"0-7": "The whole week",
|
||||
"1-3": "Tomorrow and the 2 days after"
|
||||
},
|
||||
"menuEdit_one": "Products and look ({{count}} product)",
|
||||
"menuEdit_other": "Products and look ({{count}} products)",
|
||||
@@ -1707,7 +1710,29 @@
|
||||
"toMedia": "Make one on the Media page",
|
||||
"add": "Add pictures or videos",
|
||||
"empty": "Nothing in the gallery yet: it stays hidden."
|
||||
}
|
||||
},
|
||||
"weatherLayout": "Layout",
|
||||
"weatherLayouts": {
|
||||
"row": "In a row",
|
||||
"column": "In a column",
|
||||
"card": "A card",
|
||||
"list": "A list of days",
|
||||
"hourly": "The next hours"
|
||||
},
|
||||
"weatherDetails": "Also shows",
|
||||
"weatherNoDetails": "Nothing more",
|
||||
"weatherDetail": {
|
||||
"words": "The weather in words",
|
||||
"feels": "Feels like",
|
||||
"humidity": "Humidity",
|
||||
"wind": "Wind",
|
||||
"rain": "Chance of rain",
|
||||
"sun": "Sunrise and sunset",
|
||||
"uv": "UV index"
|
||||
},
|
||||
"weatherHours": "Hours",
|
||||
"weatherPlace": "The place",
|
||||
"weatherPlaceLabel": "Show its name"
|
||||
},
|
||||
"tabs": {
|
||||
"playlists": "Playlists",
|
||||
|
||||
@@ -492,7 +492,22 @@
|
||||
"title": "Calibrating the picture of {{name}}",
|
||||
"hint": "In the manager (Signage → Screens), move each edge until the arrows touch the edges of the screen."
|
||||
},
|
||||
"closed": "Closed"
|
||||
"closed": "Closed",
|
||||
"weather": {
|
||||
"feels": "Feels like {{t}}",
|
||||
"today": "Today",
|
||||
"words": {
|
||||
"sun": "Sunny",
|
||||
"moon": "Clear",
|
||||
"cloud-sun": "Partly cloudy",
|
||||
"cloud-moon": "Partly cloudy",
|
||||
"cloud": "Cloudy",
|
||||
"fog": "Fog",
|
||||
"rain": "Rain",
|
||||
"snow": "Snow",
|
||||
"storm": "Thunderstorm"
|
||||
}
|
||||
}
|
||||
},
|
||||
"menuTablet": {
|
||||
"title": "Menu",
|
||||
|
||||
+118
-19
@@ -3,7 +3,7 @@
|
||||
// they are inline styles, not tokens. Sizes scale with the screen (cqmin), so a slide looks the
|
||||
// same on any TV.
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { Camera, Cloud, CloudFog, CloudLightning, CloudMoon, CloudRain, CloudSnow, CloudSun, Globe, Lock, Mail, MapPin, MessageCircle, Moon, Music, Phone, Star, Sun, ThumbsUp, Wifi } from 'lucide-react'
|
||||
import { Camera, Cloud, CloudFog, CloudLightning, CloudMoon, CloudRain, CloudSnow, CloudSun, Droplets, Globe, Lock, Mail, MapPin, MessageCircle, Moon, Music, Phone, Star, Sun, SunDim, Sunrise, ThumbsUp, Thermometer, Umbrella, Wifi, Wind } from 'lucide-react'
|
||||
import { QRCodeSVG } from 'qrcode.react'
|
||||
import { BADGE_ICONS } from './badges'
|
||||
import { cq, CQ_BOX } from './cq'
|
||||
@@ -359,7 +359,7 @@ function widgetBody(w, ctx, fit, path) {
|
||||
</div>
|
||||
)
|
||||
}
|
||||
if (w.kind === 'weather') return weatherBody(w, venue?.weather, unit, text, formatDate)
|
||||
if (w.kind === 'weather') return weatherBody(w, venue?.weather, unit, text, formatDate, ctx.t)
|
||||
if (w.kind === 'menu') return boxed(<MenuWidget board={w.board || {}} menu={x.menu} now={now} badges={venue?.badges} />, 'menu')
|
||||
if (w.kind === 'stack') return boxed(<Stack w={w} path={path} ctx={ctx} />, 'stack')
|
||||
// 6R.15
|
||||
@@ -631,40 +631,139 @@ function contactBody(w, contact, unit, text) {
|
||||
)
|
||||
}
|
||||
|
||||
/** The weather: now or a day of the forecast (today, tomorrow, the day after), and the next days;
|
||||
* its picture and degrees, only the picture or only the degrees (2026-11-21). */
|
||||
function weatherBody(w, wx, unit, text, formatDate) {
|
||||
/**
|
||||
* The weather (5.4c; 6R.18 all the way): now or a day of the forecast and the next days, its
|
||||
* picture and degrees (or one of them), laid out in a row, a column, a card, a list of days or the
|
||||
* next hours; with the details chosen (feels like, humidity, wind, rain chance, sunrise / sunset,
|
||||
* UV, in words) and the place's name. Older forecasts without the details just leave them out.
|
||||
*/
|
||||
function weatherBody(w, wx, unit, text, formatDate, t) {
|
||||
if (!wx || wx.temperature == null) return null // no internet / no place: hidden (5.4c)
|
||||
const from = w.weather_from || 0
|
||||
const style = w.weather_style || 'both'
|
||||
const layout = w.weather_layout || 'row'
|
||||
const pic = style !== 'text'
|
||||
const deg = style !== 'icon'
|
||||
const unitMark = wx.units === 'fahrenheit' ? '°F' : '°C'
|
||||
const mark = wx.units === 'fahrenheit' ? '°F' : '°C'
|
||||
const days = wx.days || []
|
||||
const dayName = d => formatDate(`${d.date}T12:00:00Z`, 'weekday').slice(0, 3)
|
||||
const r = n => Math.round(n)
|
||||
const main = from === 0
|
||||
? { icon: weatherIcon(wx.code, wx.is_day), label: `${Math.round(wx.temperature)}${unitMark}` }
|
||||
: days[from] && { icon: weatherIcon(days[from].code, true), label: `${dayName(days[from])} ${Math.round(days[from].max)}° / ${Math.round(days[from].min)}°` }
|
||||
? { icon: weatherIcon(wx.code, wx.is_day), label: `${r(wx.temperature)}${mark}`, day: days[0] }
|
||||
: days[from] && { icon: weatherIcon(days[from].code, true), label: `${dayName(days[from])} ${r(days[from].max)}° / ${r(days[from].min)}°`, day: days[from] }
|
||||
if (!main) return null
|
||||
const rest = days.slice(from + 1, from + (w.weather_days || 1))
|
||||
const restText = d => `${Math.round(d.max)}° / ${Math.round(d.min)}°`
|
||||
const u = unit(6, (pic ? 6 + 1.5 : 0) + (deg ? textWidth(main.label.length, 4.4) : 0)
|
||||
+ rest.length * (1.5 + (pic ? 3.4 : 0) + textWidth(4 + (deg ? 11 : 0), 2.4)))
|
||||
const restText = d => `${r(d.max)}° / ${r(d.min)}°`
|
||||
// the details chosen, those the forecast has
|
||||
const want = new Set(w.weather_details || [])
|
||||
const details = [
|
||||
want.has('feels') && from === 0 && wx.feels_like != null && [Thermometer, t('signage.weather.feels', { t: `${r(wx.feels_like)}°` })],
|
||||
want.has('humidity') && from === 0 && wx.humidity != null && [Droplets, `${r(wx.humidity)}%`],
|
||||
want.has('wind') && (from === 0 ? wx.wind : main.day?.wind) != null && [Wind, `${r(from === 0 ? wx.wind : main.day.wind)} ${wx.wind_unit || 'km/h'}`],
|
||||
want.has('rain') && main.day?.rain != null && [Umbrella, `${r(main.day.rain)}%`],
|
||||
want.has('sun') && main.day?.sunrise && [Sunrise, `${main.day.sunrise} · ${main.day.sunset || ''}`],
|
||||
want.has('uv') && main.day?.uv != null && [SunDim, `UV ${r(main.day.uv)}`],
|
||||
].filter(Boolean)
|
||||
const words = want.has('words') ? t(`signage.weather.words.${main.icon}`) : ''
|
||||
const place = w.weather_place && wx.place ? wx.place : ''
|
||||
const MainIcon = ICONS[main.icon] || Cloud
|
||||
const tw = textWidth
|
||||
const detailW = details.length ? Math.max(...details.map(([, x]) => x.length)) + 2 : 0
|
||||
|
||||
if (layout === 'hourly') {
|
||||
const hours = (wx.hours || []).slice(0, w.weather_hours || 6)
|
||||
if (!hours.length) return null
|
||||
const u = unit((place ? 3.2 : 0) + 3 + 5 + 3.6, hours.length * 9)
|
||||
return (
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: u(1.5) }} data-testid="signage-w-weather" data-style={style} data-from={from}>
|
||||
{pic && <MainIcon style={{ width: u(6), height: u(6), flexShrink: 0 }} aria-hidden="true" />}
|
||||
{deg && <span style={text(u, 4.4)}>{main.label}</span>}
|
||||
{rest.map(d => {
|
||||
const DayIcon = ICONS[weatherIcon(d.code, true)] || Cloud
|
||||
<div style={{ display: 'grid', gap: u(0.6) }} data-testid="signage-w-weather" data-layout="hourly">
|
||||
{place && <span style={text(u, 2.6, 700)}>{place}</span>}
|
||||
<div style={{ display: 'flex', gap: u(1.5) }}>
|
||||
{hours.map(h => {
|
||||
const I = ICONS[weatherIcon(h.code, h.is_day)] || Cloud
|
||||
return (
|
||||
<span key={d.date} style={{ display: 'flex', alignItems: 'center', gap: u(0.6), ...text(u, 2.4, 600) }}>
|
||||
{dayName(d)} {pic && <DayIcon style={{ width: u(3.4), height: u(3.4) }} aria-hidden="true" />}
|
||||
{deg && restText(d)}
|
||||
<div key={h.time} style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: u(0.4), minWidth: u(7.5) }} data-testid="signage-weather-hour">
|
||||
<span style={{ ...text(u, 2.4, 600), opacity: 0.85 }}>{h.time}</span>
|
||||
{pic && <I style={{ width: u(4.6), height: u(4.6) }} aria-hidden="true" />}
|
||||
{deg && <span style={text(u, 3, 700)}>{r(h.temp)}°</span>}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
if (layout === 'list') {
|
||||
const list = days.slice(from, from + (w.weather_days || 3))
|
||||
const u = unit((place ? 3.4 : 0) + list.length * 4.4, tw(4, 3) + 5 + tw(11, 3) + (want.has('rain') ? tw(5, 3) : 0) + 4)
|
||||
return (
|
||||
<div style={{ display: 'grid', gap: u(0.5) }} data-testid="signage-w-weather" data-layout="list">
|
||||
{place && <span style={text(u, 2.8, 700)}>{place}</span>}
|
||||
<div style={{ display: 'grid', gridTemplateColumns: `auto auto auto${want.has('rain') ? ' auto' : ''}`, columnGap: u(1.6), rowGap: u(0.6), alignItems: 'center' }}>
|
||||
{list.map((d, n) => {
|
||||
const I = ICONS[weatherIcon(d.code, true)] || Cloud
|
||||
return (
|
||||
<span key={d.date} style={{ display: 'contents' }} data-testid="signage-weather-day">
|
||||
<span style={text(u, 3, n === 0 && from === 0 ? 800 : 600)}>{n === 0 && from === 0 ? t('signage.weather.today') : dayName(d)}</span>
|
||||
<I style={{ width: u(3.6), height: u(3.6) }} aria-hidden="true" />
|
||||
<span style={{ ...text(u, 3, 700), fontVariantNumeric: 'tabular-nums' }}>{restText(d)}</span>
|
||||
{want.has('rain') && <span style={{ ...text(u, 2.6, 500), display: 'flex', alignItems: 'center', gap: u(0.3) }}><Umbrella style={{ width: u(2.4), height: u(2.4) }} aria-hidden="true" />{d.rain ?? '–'}%</span>}
|
||||
</span>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
const detailEls = (size, gapX) => details.map(([I, x], n) => (
|
||||
<span key={n} style={{ display: 'flex', alignItems: 'center', gap: gapX, ...text(size.u, size.s, 600) }} data-testid="signage-weather-detail">
|
||||
<I style={{ width: size.u(size.s * 0.95), height: size.u(size.s * 0.95), flexShrink: 0 }} aria-hidden="true" />{x}
|
||||
</span>
|
||||
))
|
||||
const restEls = (u, small) => rest.map(d => {
|
||||
const DayIcon = ICONS[weatherIcon(d.code, true)] || Cloud
|
||||
return (
|
||||
<span key={d.date} style={{ display: 'flex', alignItems: 'center', gap: u(0.6), ...text(u, small, 600) }}>
|
||||
{dayName(d)} {pic && <DayIcon style={{ width: u(small * 1.4), height: u(small * 1.4) }} aria-hidden="true" />}
|
||||
{deg && restText(d)}
|
||||
</span>
|
||||
)
|
||||
})
|
||||
if (layout === 'column' || layout === 'card') {
|
||||
const card = layout === 'card'
|
||||
const lines = (place ? 1 : 0) + 1 + (words ? 0.8 : 0) + (card ? Math.ceil(details.length / 2) : details.length) * 0.7 + rest.length * 0.7
|
||||
const width = Math.max((pic ? 9 : 0) + (deg ? tw(main.label.length, 6) : 0), card ? detailW * 2 * 2.4 * 0.6 + 3 : tw(detailW, 2.6), tw(16, 2.4), place ? tw(place.length, 2.8) : 0)
|
||||
const u = unit(lines * 4.2 + 3, width + 2)
|
||||
return (
|
||||
<div style={{ display: 'grid', gap: u(0.8), justifyItems: 'inherit', ...(card ? { padding: u(1.5), borderRadius: u(1.5), background: w.background_colour ? undefined : 'rgba(0,0,0,0.28)' } : {}) }}
|
||||
data-testid="signage-w-weather" data-layout={layout} data-style={style} data-from={from}>
|
||||
{place && <span style={{ ...text(u, 2.8, 700), opacity: 0.9 }}>{place}</span>}
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: u(1.5) }}>
|
||||
{pic && <MainIcon style={{ width: u(8), height: u(8), flexShrink: 0 }} aria-hidden="true" />}
|
||||
{deg && <span style={{ ...text(u, from === 0 ? 6 : 3.6, 800), lineHeight: 1 }}>{main.label}</span>}
|
||||
</div>
|
||||
{words && <span style={{ ...text(u, 2.8, 600), opacity: 0.9 }} data-testid="signage-weather-words">{words}</span>}
|
||||
{details.length > 0 && (
|
||||
<div style={{ display: 'grid', gridTemplateColumns: card ? 'auto auto' : 'auto', columnGap: u(2), rowGap: u(0.5) }}>
|
||||
{detailEls({ u, s: 2.6 }, u(0.6))}
|
||||
</div>
|
||||
)}
|
||||
{rest.length > 0 && <div style={{ display: card ? 'flex' : 'grid', gap: u(card ? 1.6 : 0.4), flexWrap: 'wrap' }}>{restEls(u, 2.4)}</div>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
// a row (as before 6R.18), the place before it and the details after it
|
||||
const u = unit(6 + (place || details.length ? 0 : 0), (place ? tw(place.length, 2.8) + 1.5 : 0) + (pic ? 6 + 1.5 : 0) + (deg ? tw(main.label.length, 4.4) : 0)
|
||||
+ (words ? tw(words.length, 2.6) + 1.5 : 0) + details.reduce((n, [, x]) => n + tw(x.length + 2, 2.4) + 1.5, 0)
|
||||
+ rest.length * (1.5 + (pic ? 3.4 : 0) + tw(4 + (deg ? 11 : 0), 2.4)))
|
||||
return (
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: u(1.5) }} data-testid="signage-w-weather" data-style={style} data-from={from} data-layout="row">
|
||||
{place && <span style={{ ...text(u, 2.8, 700), opacity: 0.9 }}>{place}</span>}
|
||||
{pic && <MainIcon style={{ width: u(6), height: u(6), flexShrink: 0 }} aria-hidden="true" />}
|
||||
{deg && <span style={text(u, 4.4)}>{main.label}</span>}
|
||||
{words && <span style={text(u, 2.6, 600)}>{words}</span>}
|
||||
{details.length > 0 && detailEls({ u, s: 2.4 }, u(0.5))}
|
||||
{restEls(u, 2.4)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user