feat(devices): push General/Clock/Bells/Alerts config live over MQTT
Adopts useDeviceCommand across DeviceDetail and its tabs so config changes (log levels, clock settings, bell outputs, alert thresholds, backlight, attributes) are sent to the device immediately via control commands and only persisted to Firestore once the device acks success, instead of writing Firestore first and hoping the device eventually picks it up. - GeneralTab: log-level sliders now call log.set_serial/sd/mqtt directly and revert on failure; a background log.get_config + network.info pull reconciles Firestore against the device's actual state once per mount - ClockTab, BellsTab, ControlTab, and the Edit* modals: same live-command-then-persist pattern - EditLoggingModal is removed — its job (log level editing) moved inline into GeneralTab's sliders, so a modal round-trip is no longer needed - DeviceDetail wires the shared useDeviceCommand connection through to each tab and adds a Health tab entry Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -112,8 +112,23 @@ function SilenceBlock({ title, isOn, onToggle, from, onFromChange, to, onToChang
|
||||
)
|
||||
}
|
||||
|
||||
export default function EditAlertsModal({ open, onClose, onSaved, clock, attr, id }) {
|
||||
const totalBells = attr?.totalBells || 8
|
||||
// Console UI uses alertType 'disabled'|'single'|'multi'; firmware uses
|
||||
// alert_type 'OFF'|'SINGLE'|'HOURS'. Bell fields: console uses 0 = disabled,
|
||||
// firmware uses 255 = disabled.
|
||||
const ALERT_TYPE_TO_FIRMWARE = { disabled: 'OFF', single: 'SINGLE', multi: 'HOURS' }
|
||||
const toFirmwareBell = (n) => (n === 0 ? 255 : n)
|
||||
|
||||
export default function EditAlertsModal({ open, onClose, onSaved, clock, attr, id, sendMqttCommand }) {
|
||||
// Only bells that actually have a relay output assigned — same source of
|
||||
// truth as BellsTab's bell cards. Ignores attr.totalBells, which can drift
|
||||
// from the real configured count. 0 and 65535 both mean "no output assigned"
|
||||
// on the wire (uint16 "unset" sentinel) — treat both as disconnected here too,
|
||||
// in case older Firestore data still has 65535 values from before that was
|
||||
// normalized on write.
|
||||
const connectedBellIndexes = (attr?.bellOutputs || [])
|
||||
.map((output, i) => ({ output, bellNumber: i + 1 }))
|
||||
.filter(({ output }) => output > 0 && output !== 65535)
|
||||
.map(({ bellNumber }) => bellNumber)
|
||||
|
||||
const [alertType, setAlertType] = useState(clock?.ringAlerts || 'disabled')
|
||||
const [ringIntervals, setRingIntervals] = useState(clock?.ringIntervals ?? 1000)
|
||||
@@ -141,12 +156,28 @@ export default function EditAlertsModal({ open, onClose, onSaved, clock, attr, i
|
||||
setSaving(true)
|
||||
setError('')
|
||||
try {
|
||||
// Push to the device first — clock.set_alerts_config batches alert type/bells
|
||||
// and both silence windows into one round trip. Firestore is only written
|
||||
// once the device confirms.
|
||||
await sendMqttCommand('clock.set_alerts_config', {
|
||||
alert_type: ALERT_TYPE_TO_FIRMWARE[alertType],
|
||||
alert_interval: alertType === 'multi' ? ringIntervals : undefined,
|
||||
hour_bell: toFirmwareBell(Number(hourBell)),
|
||||
half_bell: toFirmwareBell(Number(halfBell)),
|
||||
quarter_bell: toFirmwareBell(Number(quarterBell)),
|
||||
daytime_silence: isDayOn,
|
||||
daytime_on: isDayOn ? daySilenceFrom : undefined,
|
||||
daytime_off: isDayOn ? daySilenceTo : undefined,
|
||||
night_silence: isNightOn,
|
||||
night_on: isNightOn ? nightFrom : undefined,
|
||||
night_off: isNightOn ? nightTo : undefined,
|
||||
})
|
||||
|
||||
await api.put(`/devices/${id}`, {
|
||||
device_attributes: {
|
||||
clockSettings: {
|
||||
...clock,
|
||||
ringAlerts: alertType,
|
||||
ringAlertsMasterOn: alertType !== 'disabled',
|
||||
ringIntervals: alertType === 'multi' ? ringIntervals : clock?.ringIntervals,
|
||||
hourAlertsBell: Number(hourBell),
|
||||
halfhourAlertsBell: Number(halfBell),
|
||||
@@ -163,6 +194,7 @@ export default function EditAlertsModal({ open, onClose, onSaved, clock, attr, i
|
||||
await onSaved()
|
||||
onClose()
|
||||
} catch (err) {
|
||||
// sendMqttCommand already surfaced a failure toast if the device rejected it.
|
||||
setError(err.message || 'Failed to save.')
|
||||
} finally {
|
||||
setSaving(false)
|
||||
@@ -171,7 +203,7 @@ export default function EditAlertsModal({ open, onClose, onSaved, clock, attr, i
|
||||
|
||||
const bellOptions = [
|
||||
{ value: '0', label: 'Disabled' },
|
||||
...Array.from({ length: totalBells }, (_, i) => ({ value: String(i + 1), label: `Bell ${i + 1}` })),
|
||||
...connectedBellIndexes.map((n) => ({ value: String(n), label: `Bell ${n}` })),
|
||||
]
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user