// frontend/src/modals/bellcloud/devices/EditClockSettingsModal.jsx // Edits: master switch, ODD/EVEN outputs, pulse and pause durations (sliders), and the // face sync policy (firmware 3.2.0+: force = run a full lap when the face is ahead, // smart = wait if it is at most N minutes ahead). import { useState } from 'react' import Modal from '@/components/ui/Modal' import Button from '@/components/ui/Button' import FormField from '@/components/ui/FormField' import SegmentedControl from '@/components/ui/SegmentedControl' import api from '@/lib/api' // Pulse options in milliseconds — shown as sliders with labelled steps const PULSE_STEPS = [200, 300, 400, 500, 600, 800, 1000, 1200, 1500, 2000] const PAUSE_STEPS = [500, 1000, 1500, 2000, 3000, 4000, 5000] function msLabel(ms) { if (!ms) return '—' return ms >= 1000 ? `${(ms / 1000).toFixed(1)}s` : `${ms}ms` } // Slider that snaps to a fixed array of allowed values function StepSlider({ steps, value, onChange, label }) { const idx = steps.indexOf(value) const currentIdx = idx >= 0 ? idx : 0 return (
{label} {msLabel(steps[currentIdx])}
onChange(steps[Number(e.target.value)])} style={{ width: '100%', accentColor: 'var(--color-primary)', cursor: 'pointer' }} />
{msLabel(steps[0])} {msLabel(steps[steps.length - 1])}
) } export default function EditClockSettingsModal({ open, onClose, onSaved, attr, sub, id, sendMqttCommand }) { const clock = attr?.clockSettings || {} const maxOutputs = sub?.maxOutputs || 8 const bellOutputsUsed = (attr?.bellOutputs || []).filter(o => o > 0) const existingClockOutputs = clock.clockOutputs || [] const existingTimings = clock.clockTimings || [] const [hasClock, setHasClock] = useState(attr?.hasClock ?? false) const [clockOut1, setClockOut1] = useState(String(existingClockOutputs[0] ?? 0)) const [clockOut2, setClockOut2] = useState(String(existingClockOutputs[1] ?? 0)) const [runPulse, setRunPulse] = useState(existingTimings[0] || PULSE_STEPS[3]) // default 500ms const [pauseMs, setPauseMs] = useState(existingTimings[1] || PAUSE_STEPS[1]) // default 1000ms const [faceSync, setFaceSync] = useState(clock.faceSyncMode || 'force') const [maxWait, setMaxWait] = useState(String(clock.smartMaxWaitMin ?? 61)) const [saving, setSaving] = useState(false) const [error, setError] = useState('') const out1 = Number(clockOut1) const out2 = Number(clockOut2) const conflictMsg = out1 > 0 && out1 === out2 ? 'ODD and EVEN outputs cannot be the same.' : null const validationMsg = hasClock && (out1 === 0 || out2 === 0) ? 'Both outputs must be assigned when the clock is enabled.' : null const maxWaitNum = Number(maxWait) const maxWaitMsg = faceSync === 'smart' && (!Number.isInteger(maxWaitNum) || maxWaitNum < 1 || maxWaitNum > 359) ? 'Maximum wait must be 1–359 minutes.' : null const canSave = !conflictMsg && !validationMsg && !maxWaitMsg const handleSave = async () => { if (!canSave) return setSaving(true) setError('') try { // Push to the device first — clock.set_config batches enable/outputs/timings // into one round trip. Firestore is only written once the device confirms. await sendMqttCommand('clock.set_config', { enabled: hasClock, c1: out1, c2: out2, pulse_duration: runPulse, pause_duration: pauseMs, face_sync_mode: faceSync, ...(faceSync === 'smart' ? { smart_max_wait_min: maxWaitNum } : {}), }) await api.put(`/devices/${id}`, { device_attributes: { hasClock, clockSettings: { clockOutputs: [out1, out2].filter(o => o > 0), clockTimings: [runPulse, pauseMs], faceSyncMode: faceSync, ...(faceSync === 'smart' ? { smartMaxWaitMin: maxWaitNum } : {}), }, }, }) 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) } } const outputOptions = Array.from({ length: maxOutputs }, (_, j) => { const out = j + 1 return { out, isBell: bellOutputsUsed.includes(out) } }) return ( } >
{/* Master switch */}

Clock Controller

Enable or disable the clock mechanism entirely

{/* Outputs */}
setClockOut1(e.target.value)} > {outputOptions.map(({ out, isBell }) => ( ))} setClockOut2(e.target.value)} > {outputOptions.map(({ out, isBell }) => ( ))}
{/* Sliders */}
{/* Face sync policy */}

Face Ahead of Real Time

The hands only move forward. When they are ahead (e.g. after DST ends), Force runs a full lap to catch up; Smart stops them until real time catches up.

{faceSync === 'smart' && ( setMaxWait(e.target.value)} inputProps={{ min: 1, max: 359, step: 1 }} hint="Default 61 covers the DST change. Further ahead than this, the face runs the lap." /> )}
{/* Errors / warnings */} {(conflictMsg || validationMsg || maxWaitMsg) && (

{conflictMsg || validationMsg || maxWaitMsg}

)} {error && (

{error}

)}
) }