diff --git a/client/src/api/mqtt.ts b/client/src/api/mqtt.ts new file mode 100644 index 0000000..9b46b83 --- /dev/null +++ b/client/src/api/mqtt.ts @@ -0,0 +1,21 @@ +import { request } from './request' + +export const publishMqttMessage = (body: { + server: string + clientId?: string + username?: string + password?: string + qos?: number + retain?: boolean + message: string + topic: string +}) => + request( + `/api/mqtt/publish`, + { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify(body), + }, + 'void' + ) diff --git a/client/src/assets/components/_grid-sensors.scss b/client/src/assets/components/_grid-sensors.scss index 557d6ee..102b8d6 100644 --- a/client/src/assets/components/_grid-sensors.scss +++ b/client/src/assets/components/_grid-sensors.scss @@ -84,6 +84,10 @@ overflow: hidden; text-overflow: ellipsis; + &.hidden-title { + border-bottom: none; + } + .drag-handle { flex: 1; cursor: move; @@ -133,6 +137,21 @@ justify-content: center; height: 100%; } + + .mqtt-button { + display: flex; + align-items: center; + justify-content: center; + height: 100%; + + &.apply-stretch { + align-items: stretch; + + > button { + flex: 1; + } + } + } } } diff --git a/client/src/pages/dashboard/components/BoxSettings/BoxSettings.tsx b/client/src/pages/dashboard/components/BoxSettings/BoxSettings.tsx index 3af8b35..5402262 100644 --- a/client/src/pages/dashboard/components/BoxSettings/BoxSettings.tsx +++ b/client/src/pages/dashboard/components/BoxSettings/BoxSettings.tsx @@ -4,6 +4,7 @@ import { useConfirmModal } from '@/contexts/ConfirmModalsContext' import { DashboardDialData, DashboardGraphData, + DashboardMQTTButtonData, } from '@/utils/dashboard/parseDashboard' import { useForm } from '@/utils/hooks/useForm' import { useState } from 'preact/hooks' @@ -11,6 +12,7 @@ import { useQuery } from 'react-query' import { BoxDefinition } from '../../types' import { DialSettings } from './components/DialSettings' import { GraphSettings } from './components/GraphSettings' +import { MQTTButtonSettings } from './components/MQTTButtonSettings' type Props = { value: BoxDefinition @@ -63,6 +65,7 @@ export const BoxSettings = ({ value, onSave, onClose, onRemove }: Props) => { @@ -81,6 +84,13 @@ export const BoxSettings = ({ value, onSave, onClose, onRemove }: Props) => { sensors={sensors.data ?? []} /> )} + + {type === 'mqttButton' && ( + + )} } diff --git a/client/src/pages/dashboard/components/BoxSettings/components/MQTTButtonSettings.tsx b/client/src/pages/dashboard/components/BoxSettings/components/MQTTButtonSettings.tsx new file mode 100644 index 0000000..66cd703 --- /dev/null +++ b/client/src/pages/dashboard/components/BoxSettings/components/MQTTButtonSettings.tsx @@ -0,0 +1,67 @@ +import { FormCheckboxField } from '@/components/FormCheckboxField' +import { FormField } from '@/components/FormField' +import { DashboardMQTTButtonData } from '@/utils/dashboard/parseDashboard' +import { useForm } from '@/utils/hooks/useForm' +import { omit } from '@/utils/omit' + +type Props = { + value?: DashboardMQTTButtonData + onChange: (data: DashboardMQTTButtonData) => void +} + +export const MQTTButtonSettings = ({ value, onChange }: Props) => { + const { register } = useForm({ + defaultValue: () => ({ + server: '', + topic: '', + message: '', + retain: false, + ...(value && omit(value, ['type'])), + }), + onChange: (v) => onChange({ ...v, type: 'mqttButton' }), + }) + + return ( + <> + + + + + + + + + + + + + + + + + + + + + + +