mirror of
https://github.com/cesanta/mongoose.git
synced 2024-11-24 02:59:01 +08:00
MQTT dashboard nitpicks
This commit is contained in:
parent
79e138ef28
commit
bbd27a0a9c
@ -230,7 +230,7 @@ export function Setting(props) {
|
||||
if (props.type == 'select') input = SelectValue;
|
||||
return html`
|
||||
<div class=${props.cls || 'grid grid-cols-2 gap-2 my-1'}>
|
||||
<label class="flex items-center text-sm text-gray-700 mr-2 font-medium">${props.title}<//>
|
||||
<label class="flex items-center text-sm text-gray-700 mr-2 font-medium ${props.title || 'hidden'}">${props.title}<//>
|
||||
<div class="flex items-center">${h(input, props)}<//>
|
||||
<//>`;
|
||||
};
|
||||
|
File diff suppressed because one or more lines are too long
@ -1,95 +1,70 @@
|
||||
'use strict';
|
||||
import { h, render, useState, useEffect, useRef, html } from './bundle.js';
|
||||
import { Icons, Setting, Button, tipColors, Colored, Notification} from './components.js';
|
||||
import {h, html, render, useEffect, useRef, useState} from './bundle.js';
|
||||
import {Button, Colored, Icons, Notification, Setting, tipColors} from './components.js';
|
||||
|
||||
const Logo = props => html`<svg class=${props.class} xmlns="http://www.w3.org/2000/svg" viewBox="0 0 12.87 12.85"><defs><style>.ll-cls-1{fill:none;stroke:#000;stroke-miterlimit:10;stroke-width:0.5px;}</style></defs><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><path class="ll-cls-1" d="M12.62,1.82V8.91A1.58,1.58,0,0,1,11,10.48H4a1.44,1.44,0,0,1-1-.37A.69.69,0,0,1,2.84,10l-.1-.12a.81.81,0,0,1-.15-.48V5.57a.87.87,0,0,1,.86-.86H4.73V7.28a.86.86,0,0,0,.86.85H9.42a.85.85,0,0,0,.85-.85V3.45A.86.86,0,0,0,10.13,3,.76.76,0,0,0,10,2.84a.29.29,0,0,0-.12-.1,1.49,1.49,0,0,0-1-.37H2.39V1.82A1.57,1.57,0,0,1,4,.25H11A1.57,1.57,0,0,1,12.62,1.82Z"/><path class="ll-cls-1" d="M10.48,10.48V11A1.58,1.58,0,0,1,8.9,12.6H1.82A1.57,1.57,0,0,1,.25,11V3.94A1.57,1.57,0,0,1,1.82,2.37H8.9a1.49,1.49,0,0,1,1,.37l.12.1a.76.76,0,0,1,.11.14.86.86,0,0,1,.14.47V7.28a.85.85,0,0,1-.85.85H8.13V5.57a.86.86,0,0,0-.85-.86H3.45a.87.87,0,0,0-.86.86V9.4a.81.81,0,0,0,.15.48l.1.12a.69.69,0,0,0,.13.11,1.44,1.44,0,0,0,1,.37Z"/></g></g></svg>`;
|
||||
const DefaultTopic = 'mg_mqtt_dashboard';
|
||||
const DefaultUrl = location.protocol == 'https:'
|
||||
? 'wss://broker.hivemq.com:8443/mqtt'
|
||||
: 'ws://broker.hivemq.com:8000/mqtt';
|
||||
const DefaultDeviceConfig = {pins: [], log_level: 0};
|
||||
let MqttClient;
|
||||
|
||||
const url = 'ws://broker.hivemq.com:8000/mqtt'
|
||||
const default_topic = 'topic_mg_device'
|
||||
let client;
|
||||
|
||||
function Header( {topic, setTopicFn} ) {
|
||||
const [inputValue, setInputValue] = useState(topic);
|
||||
const [saveResult, setSaveResult] = useState(null);
|
||||
const onInput = (ev) => setInputValue(ev.target.value);
|
||||
function Header({topic, setTopic, url, setUrl, connected}) {
|
||||
const forbiddenChars = ['$', '*', '+', '#', '/'];
|
||||
|
||||
const onClick = () => {
|
||||
const isValidTopic = (value) => {
|
||||
return !forbiddenChars.some(char => value.includes(char));
|
||||
};
|
||||
|
||||
if (isValidTopic(inputValue)) {
|
||||
localStorage.setItem('topic', inputValue)
|
||||
setTopicFn(inputValue);
|
||||
const isValidTopic = val => !forbiddenChars.some(char => val.includes(char));
|
||||
if (isValidTopic(topic)) {
|
||||
localStorage.setItem('topic', topic)
|
||||
setTopicFn(topic);
|
||||
window.location.reload();
|
||||
} else {
|
||||
setSaveResult("Error: The topic cannot contain these characters: " + forbiddenChars)
|
||||
setSaveResult('Error: The topic cannot contain these characters: ' + forbiddenChars);
|
||||
}
|
||||
};
|
||||
|
||||
return html`
|
||||
<div class="bg-white sticky top-0 z-[48] w-full border-b-2 border-gray-300 py-3 shadow-md transition-all duration-300 transform">
|
||||
<div class="mx-auto flex justify-between items-center px-5">
|
||||
<div class="text-gray-800 font-semibold tracking-wide">
|
||||
<h1 class="text-2xl">MQTT Dashboard</h1>
|
||||
</div>
|
||||
<div class="flex space-x-4">
|
||||
<div class="text-lg font-medium text-gray-700 text-center">
|
||||
<div class="text-gray-500">MQTT Server:</div>
|
||||
<code class="bg-gray-100 font-normal text-sm rounded w-full flex-1 py-0.5 px-2 text-gray-700" style="font-size: 0.8em; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 1px; background-color: #f9f9f9; border: 1px solid #ddd; border-radius: 5px;">
|
||||
${url}
|
||||
</code>
|
||||
</div>
|
||||
<div style="border-left: 1px solid #ddd; height: 50px;"></div>
|
||||
<div class="text-lg font-medium text-gray-700 text-center">
|
||||
<div class="text-gray-500">Topic:</div>
|
||||
<div class="flex w-full items-center rounded border shadow-sm bg-white">
|
||||
<input type="text" value=${inputValue} onchange=${onInput} step="1" class="bg-white font-normal text-sm rounded w-full flex-1 py-0.5 px-2 text-gray-700 placeholder:text-gray-400 focus:outline-none disabled:cursor-not-allowed disabled:bg-gray-100 disabled:text-gray-500"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-1 mt-3 flex place-content-end"><${Button} icon=${Icons.save} onclick=${onClick} title="Change topic" /><//>
|
||||
${saveResult && html`<${Notification} ok=${saveResult === "Success!"}
|
||||
text=${saveResult} close=${() => setSaveResult(null)} />`}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
<div class="top-0 py-2 border-b-2 bg-stone-100">
|
||||
<div class="flex px-4">
|
||||
<div class="flex grow gap-4">
|
||||
<h1 class="text-2xl font-semibold">MQTT Dashboard</h1>
|
||||
<small class="text-gray-500 mt-2.5">powered by Mongoose<//>
|
||||
<//>
|
||||
<div class="flex gap-2 items-center">
|
||||
<${Setting} value=${url} addonLeft="MQTT Server" cls="py-1 w-96" disabled=${connected} />
|
||||
<${Setting} value=${topic} addonLeft="Root Topic" cls="py-1" disabled=${connected} />
|
||||
<${Button} icon=${Icons.link} onclick=${onClick} title=${connected ? 'Disconnect' : 'Connect'} />
|
||||
<//>
|
||||
<//>
|
||||
<//>`;
|
||||
};
|
||||
|
||||
function Devices({ devices, onClickFn }) {
|
||||
function Sidebar({devices, onclick}) {
|
||||
const Td = props => html`
|
||||
<td class="whitespace-nowrap border-b border-slate-200 py-2 px-4 pr-3 text-sm text-slate-900">${props.text}</td>`;
|
||||
<td class="whitespace-nowrap border-b border-slate-200 py-2 px-4 pr-3 text-sm text-slate-900">${
|
||||
props.text}</td>`;
|
||||
|
||||
const Device = ({d}) => html`
|
||||
<tr class="hover:bg-slate-100 cursor-pointer" onClick=${() => onClickFn(d.id)}>
|
||||
<td class="border-b-2 border-slate-300 py-2 text-center align-middle">
|
||||
<div class="flex flex-col items-center justify-center h-full">
|
||||
<span>ID ${d.id}</span>
|
||||
<${Colored} colors=${d.online ? tipColors.green : tipColors.red} text=${d.online ? 'online' : 'offline'} />
|
||||
</div>
|
||||
</td>
|
||||
</tr>`;
|
||||
<div class="hover:bg-stone-100 cursor-pointer flex gap-3 px-4 py-2 justify-between"
|
||||
onclick=${ev => onclick(d.id)}>
|
||||
<span>${d.id}</span>
|
||||
<${Colored}
|
||||
colors=${d.online ? tipColors.green : tipColors.red}
|
||||
text=${d.online ? 'online' : 'offline'} />
|
||||
<//>`;
|
||||
|
||||
return html`
|
||||
<div class="m-4 divide-y divide-gray-200 overflow-auto rounded bg-white">
|
||||
<div class="font-semibold flex items-center text-gray-600 px-3 justify-center whitespace-nowrap">
|
||||
<div class="font-bold flex items-center text-gray-600">
|
||||
Devices list
|
||||
</div>
|
||||
<div class="overflow-auto divide-y border-r xbasis-60 w-60 flex-none">
|
||||
<div class="font-light uppercase flex items-center text-gray-600 px-4 py-2 bg-stone-100">
|
||||
Devices
|
||||
<//>
|
||||
<div class="inline-block min-w-full align-middle" style="max-height: 82vh; overflow: auto;">
|
||||
<table class="w-full table table-bordered">
|
||||
<tbody>
|
||||
${(devices ? devices : []).map(d => h(Device, {d}))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>`;
|
||||
<//>`;
|
||||
};
|
||||
|
||||
function FirmwareStatus({title, info, children}) {
|
||||
const state = ['UNAVAILABLE', 'FIRST_BOOT', 'NOT_COMMITTED', 'COMMITTED'][(info.status || 0) % 4];
|
||||
const state =
|
||||
['UNAVAILABLE', 'FIRST_BOOT', 'NOT_COMMITTED',
|
||||
'COMMITTED'][(info.status || 0) % 4];
|
||||
const valid = info.status > 0;
|
||||
return html`
|
||||
<div class="bg-white divide-y border rounded">
|
||||
@ -100,7 +75,8 @@ function FirmwareStatus({title, info, children}) {
|
||||
<div class="my-1">Status: ${state}<//>
|
||||
<div class="my-1">CRC32: ${valid ? info.crc32.toString(16) : 'n/a'}<//>
|
||||
<div class="my-1">Size: ${valid ? info.size : 'n/a'}<//>
|
||||
<div class="my-1">Flashed at: ${valid ? new Date(info.timestamp * 1000).toLocaleString() : 'n/a'}<//>
|
||||
<div class="my-1">Flashed at: ${
|
||||
valid ? new Date(info.timestamp * 1000).toLocaleString() : 'n/a'}<//>
|
||||
${children}
|
||||
<//>
|
||||
<//>`;
|
||||
@ -121,7 +97,9 @@ function UploadFileButton(props) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
const finish = ok => {
|
||||
setUpload(null);
|
||||
const res = props.onupload ? props.onupload(ok, fileName, fileData.length) : null;
|
||||
const res = props.onupload ?
|
||||
props.onupload(ok, fileName, fileData.length) :
|
||||
null;
|
||||
if (res && typeof (res.catch) === 'function') {
|
||||
res.catch(() => false).then(() => ok ? resolve() : reject());
|
||||
} else {
|
||||
@ -132,23 +110,32 @@ function UploadFileButton(props) {
|
||||
const sendChunk = function(offset) {
|
||||
var chunk = fileData.subarray(offset, offset + chunkSize) || '';
|
||||
var ok;
|
||||
setStatusByID('Uploading ' + fileName + ', bytes ' + offset + '..' +
|
||||
(offset + chunk.length) + ' of ' + fileData.length, props.id);
|
||||
const params = {chunk: btoa(String.fromCharCode.apply(null, chunk)), offset: offset, total: fileData.length}
|
||||
props.publishFn("ota.upload", params)
|
||||
setStatusByID(
|
||||
'Uploading ' + fileName + ', bytes ' + offset + '..' +
|
||||
(offset + chunk.length) + ' of ' + fileData.length,
|
||||
props.id);
|
||||
const params = {
|
||||
chunk: btoa(String.fromCharCode.apply(null, chunk)),
|
||||
offset: offset,
|
||||
total: fileData.length
|
||||
};
|
||||
props.publishFn('ota.upload', params)
|
||||
.then(function(res) {
|
||||
if (res.result === "ok" && chunk.length > 0) sendChunk(offset + chunk.length);
|
||||
ok = res.result === "ok";
|
||||
if (res.result === 'ok' && chunk.length > 0)
|
||||
sendChunk(offset + chunk.length);
|
||||
ok = res.result === 'ok';
|
||||
return res;
|
||||
})
|
||||
.then(function(res) {
|
||||
if (!ok) setStatusByID('Error: ' + res.error, props.id), finish(ok); // Fail
|
||||
if (!ok)
|
||||
setStatusByID('Error: ' + res.error, props.id),
|
||||
finish(ok); // Fail
|
||||
if (chunk.length > 0) return; // More chunks to send
|
||||
setStatus(x => x + '. Done, resetting device...');
|
||||
finish(ok); // All chunks sent
|
||||
})
|
||||
.catch(e => {
|
||||
setStatusByID("Error: timed out", props.id);
|
||||
setStatusByID('Error: timed out', props.id);
|
||||
finish(false)
|
||||
});
|
||||
};
|
||||
@ -170,7 +157,8 @@ function UploadFileButton(props) {
|
||||
};
|
||||
|
||||
const onclick = function(ev) {
|
||||
let fn; setUpload(x => fn = x);
|
||||
let fn;
|
||||
setUpload(x => fn = x);
|
||||
if (!fn) input.current.click(); // No upload in progress, show file dialog
|
||||
return fn;
|
||||
};
|
||||
@ -182,9 +170,12 @@ function UploadFileButton(props) {
|
||||
|
||||
return html`
|
||||
<div class="inline-flex flex-col ${props.class}">
|
||||
<input class="hidden" type="file" ref=${input} onchange=${onchange} accept=${props.accept} />
|
||||
<${Button} title=${props.title} icon=${Icons.download} onclick=${onclick} ref=${btn} colors=${props.colors} disabled=${props.disabled} />
|
||||
<div class="py-2 text-sm text-slate-400 ${status || 'hidden'}">${status[props.id]}<//>
|
||||
<input class="hidden" type="file" ref=${input} onchange=${onchange} accept=${
|
||||
props.accept} />
|
||||
<${Button} title=${props.title} icon=${Icons.download} onclick=${
|
||||
onclick} ref=${btn} colors=${props.colors} disabled=${props.disabled} />
|
||||
<div class="py-2 text-sm text-slate-400 ${status || 'hidden'}">${
|
||||
status[props.id]}<//>
|
||||
<//>`;
|
||||
};
|
||||
|
||||
@ -192,29 +183,40 @@ function FirmwareUpdate({ publishFn, disabled, info, deviceID }) {
|
||||
const [clean, setClean] = useState(false)
|
||||
const refresh = () => {};
|
||||
useEffect(refresh, []);
|
||||
const oncommit = ev => { publishFn("ota.commit") };
|
||||
const onreboot = ev => { publishFn("device.reset")};
|
||||
const onrollback = ev => { publishFn("ota.rollback") };
|
||||
const oncommit = ev => {
|
||||
publishFn('ota.commit')
|
||||
};
|
||||
const onreboot = ev => {
|
||||
publishFn('device.reset')
|
||||
};
|
||||
const onrollback = ev => {
|
||||
publishFn('ota.rollback')
|
||||
};
|
||||
const onerase = ev => {};
|
||||
const onupload = function(ok, name, size) {
|
||||
if (!ok) return false;
|
||||
return new Promise(r => setTimeout(ev => { refresh(); r(); }, 3000)).then(r => setClean(true));
|
||||
return new Promise(r => setTimeout(ev => {
|
||||
refresh();
|
||||
r();
|
||||
}, 3000)).then(r => setClean(true));
|
||||
};
|
||||
|
||||
const defaultInfo = {status: 0, crc32: 0, size: 0, timestamp: 0}
|
||||
const defaultInfo = {status: 0, crc32: 0, size: 0, timestamp: 0};
|
||||
return html`
|
||||
<div class="bg-slate-200 border rounded-md text-ellipsis overflow-auto">
|
||||
<div class="bg-slate-100 px-4 py-2 flex items-center justify-between font-light uppercase text-gray-600">
|
||||
<div class="bg-slate-200 border rounded text-ellipsis overflow-auto mt-4">
|
||||
<div class="bg-white px-4 py-2 flex items-center justify-between font-light uppercase text-gray-600">
|
||||
Over-the-air firmware updates
|
||||
</div>
|
||||
<div class="gap-1 grid grid-cols-3 lg:grid-cols-3">
|
||||
<${FirmwareStatus} title="Current firmware image" info=${info[0] ? info[0] : defaultInfo}>
|
||||
<${FirmwareStatus} title="Current firmware image"
|
||||
info=${info[0] ? info[0] : defaultInfo}>
|
||||
<div class="flex flex-wrap gap-2">
|
||||
<${Button} title="Commit this firmware" onclick=${oncommit}
|
||||
icon=${Icons.thumbUp} disabled=${disabled} cls="w-full" />
|
||||
<//>
|
||||
<//>
|
||||
<${FirmwareStatus} title="Previous firmware image" info=${info[1] ? info[1] : defaultInfo}>
|
||||
<${FirmwareStatus} title="Previous firmware image" info=${
|
||||
info[1] ? info[1] : defaultInfo}>
|
||||
<${Button} title="Rollback to this firmware" onclick=${onrollback}
|
||||
icon=${Icons.backward} disabled=${disabled} cls="w-full" />
|
||||
<//>
|
||||
@ -224,93 +226,105 @@ function FirmwareUpdate({ publishFn, disabled, info, deviceID }) {
|
||||
<//>
|
||||
<div class="px-4 py-3 flex flex-col gap-2 grow">
|
||||
<${UploadFileButton}
|
||||
title="Upload new firmware: choose .bin file:" publishFn=${publishFn} onupload=${onupload} clean=${clean} setCleanFn=${setClean} disabled=${disabled}
|
||||
title="Upload new firmware: choose .bin file:"
|
||||
publishFn=${publishFn} onupload=${onupload} clean=${clean}
|
||||
setCleanFn=${setClean} disabled=${disabled}
|
||||
id=${deviceID} url="api/firmware/upload" accept=".bin,.uf2" />
|
||||
<div class="grow"><//>
|
||||
<${Button} title="Reboot device" onclick=${onreboot} icon=${Icons.refresh} disabled=${disabled} cls="w-full" />
|
||||
<${Button} title="Erase last sector" onclick=${onerase} icon=${Icons.doc} disabled=${disabled} cls="w-full hidden" />
|
||||
<${Button} title="Reboot device" onclick=${onreboot}
|
||||
icon=${Icons.refresh} disabled=${disabled} cls="w-full" />
|
||||
<${Button} title="Erase last sector" onclick=${onerase}
|
||||
icon=${Icons.doc} disabled=${disabled} cls="w-full hidden" />
|
||||
<//>
|
||||
<//>
|
||||
<//>
|
||||
<//>`;
|
||||
};
|
||||
|
||||
function Config( {deviceData, setDeviceConfig, publishFn} ) {
|
||||
const [localConfig, setLocalConfig] = useState();
|
||||
function DeviceControlPanel({device, setDeviceConfig, publishFn, connected}) {
|
||||
const cfg = device && device.config ? device.config : DefaultDeviceConfig;
|
||||
const [localConfig, setLocalConfig] = useState(cfg);
|
||||
const [saveResult, setSaveResult] = useState(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
useEffect(() => {
|
||||
if (deviceData) {
|
||||
if (deviceData.config) {
|
||||
setLocalConfig(deviceData.config)
|
||||
} else {
|
||||
let config = {}
|
||||
setLocalConfig(config)
|
||||
}
|
||||
}
|
||||
}, [deviceData]);
|
||||
|
||||
useEffect(() => setLocalConfig(cfg), [device]);
|
||||
|
||||
const logOptions = [[0, 'Disable'], [1, 'Error'], [2, 'Info'], [3, 'Debug']];
|
||||
const mksetfn = k => (v => setLocalConfig(x => Object.assign({}, x, {[k]: v})));
|
||||
|
||||
const onSave = () => {
|
||||
setSaving(true)
|
||||
publishFn("config.set", localConfig).then(r => {
|
||||
setDeviceConfig(deviceData.id, localConfig)
|
||||
setSaveResult("Success!")
|
||||
setSaving(false)
|
||||
const onSave = ev => publishFn('config.set', localConfig).then(r => {
|
||||
setDeviceConfig(device.id, localConfig)
|
||||
setSaveResult('Success!')
|
||||
}).catch(e => {
|
||||
setDeviceConfig(deviceData.id, deviceData.config)
|
||||
setSaveResult("Failed!")
|
||||
setSaving(false)
|
||||
setDeviceConfig(device.id, device.config)
|
||||
setSaveResult('Failed!')
|
||||
})
|
||||
};
|
||||
|
||||
const onLedToggle = function(ev) {
|
||||
localConfig.led_status = !localConfig.led_status;
|
||||
onSave();
|
||||
};
|
||||
|
||||
if (!deviceData || !localConfig) {
|
||||
return ``;
|
||||
if (!device || !localConfig) {
|
||||
return html`
|
||||
<div class="flex grow text-gray-400 justify-center items-center text-xl">
|
||||
No device selected. Click on a device on a sidebar
|
||||
<//>`;
|
||||
}
|
||||
|
||||
// To delete device, set an empty retained message
|
||||
const onforget = function(ev) {
|
||||
MqttClient.publish(device.topic, '', {retain: true});
|
||||
location.reload();
|
||||
};
|
||||
|
||||
const mksetpin = (pin, val) => setLocalConfig(x => {
|
||||
let pins = x.pins.slice();
|
||||
pins[pin] = val ? 0 : 1;
|
||||
return Object.assign({}, x, {pins});
|
||||
});
|
||||
|
||||
const settingstyle = 'inline-block mr-8 my-1 bg-gray-100 rounded px-2';
|
||||
const Pin = (value, pin) => html`
|
||||
<div class="${settingstyle} w-32">
|
||||
<${Setting}
|
||||
setfn=${ev => mksetpin(pin, value)}
|
||||
type="switch" value=${value} xcls="py-1 w-32 overflow-auto"
|
||||
disabled=${!device.online || !connected} title="Pin ${pin}" />
|
||||
<//>`;
|
||||
|
||||
return html`
|
||||
<div class="m-4 space-y-2">
|
||||
<h3 class="text-lg font-semibold mb-3">Device ${deviceData.id} Configuration Panel</h3>
|
||||
<div class="divide-y border rounded bg-slate-200">
|
||||
<div class="bg-slate-100 px-4 py-2 flex items-center font-light uppercase text-gray-600">
|
||||
<span class="mr-2">
|
||||
Status: ${html`<${Colored} colors=${deviceData.online ? tipColors.green : tipColors.red} text=${deviceData.online ? 'online' : 'offline'} />`}
|
||||
</span>
|
||||
<div class="flex-1 xgrow overflow-auto bg-gray-200 xpx-4 xpy-2 p-3">
|
||||
<div class="divide-y border rounded bg-white xbg-slate-100 my-y">
|
||||
|
||||
<div class="px-4 py-2 flex justify-between items-center font-light uppercase text-gray-600 rounded">
|
||||
<div class="flex gap-2 items-center">
|
||||
<span>Device ${device.id}<//>
|
||||
${html`<${Colored}
|
||||
colors=${device.online ? tipColors.green : tipColors.red}
|
||||
text=${ device.online ? 'online' : 'offline'} />`}
|
||||
<//>
|
||||
<div class="grid grid-cols-2 gap-1">
|
||||
<div class="py-1 divide-y border rounded bg-white flex flex-col">
|
||||
<div class="font-light uppercase flex items-center text-gray-600 px-4 py-2">
|
||||
LED Settings
|
||||
<${Button} title="Forget this device"
|
||||
disabled=${device.online || !connected}
|
||||
icon=${Icons.fail} onclick=${onforget}/>
|
||||
<//>
|
||||
<div class="py-2 px-5 flex-1 flex flex-col relative">
|
||||
<${Setting} title="LED status" value=${localConfig.led_status} setfn=${onLedToggle} type="switch" disabled=${!deviceData.online} />
|
||||
<${Setting} title="LED Pin" type="number" value=${localConfig.led_pin} setfn=${mksetfn('led_pin')} disabled=${!deviceData.online} />
|
||||
|
||||
<div class="p-4 xgrid gap-3">
|
||||
${localConfig.pins.map((val, pin) => Pin(val, pin))}
|
||||
<div><//>
|
||||
|
||||
<div class="${settingstyle} w-48">
|
||||
<${Setting} title="Log Level" type="select"
|
||||
value=${localConfig.log_level} setfn=${mksetfn('log_level')}
|
||||
options=${logOptions} disabled=${!device.online}/>
|
||||
<//>
|
||||
</div>
|
||||
<div class="py-1 divide-y border rounded bg-white flex flex-col">
|
||||
<div class="font-light uppercase flex items-center text-gray-600 px-4 py-2">
|
||||
Log & Display
|
||||
<//>
|
||||
<div class="py-2 px-5 flex-1 flex flex-col relative">
|
||||
<${Setting} title="Log Level" type="select" value=${localConfig.log_level} setfn=${mksetfn('log_level')} options=${logOptions} disabled=${!deviceData.online}/>
|
||||
<${Setting} title="Brightness" type="number" value=${localConfig.brightness} setfn=${mksetfn('brightness')} disabled=${!deviceData.online} />
|
||||
|
||||
<div class="mt-3 flex justify-end">
|
||||
${saveResult && html`<${Notification} ok=${saveResult === "Success!"}
|
||||
${saveResult && html`<${Notification} ok=${saveResult === 'Success!'}
|
||||
text=${saveResult} close=${() => setSaveResult(null)} />`}
|
||||
<${Button} icon=${Icons.save} onclick=${onSave} title=${saving ? "Saving..." : "Save Settings"} disabled=${!deviceData.online || saving} />
|
||||
</div>
|
||||
<${Button} icon=${Icons.save} onclick=${onSave}
|
||||
title="Save Settings" disabled=${!device.online} />
|
||||
<//>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<${FirmwareUpdate} deviceID=${deviceData.id} publishFn=${publishFn} disabled=${!deviceData.online} info=${[localConfig.crnt_fw, localConfig.prev_fw]} />
|
||||
<//>
|
||||
<//>
|
||||
|
||||
<${FirmwareUpdate} deviceID=${device.id} publishFn=${publishFn}
|
||||
disabled=${!device.online}
|
||||
info=${[localConfig.crnt_fw, localConfig.prev_fw]} />
|
||||
</div>`;
|
||||
}
|
||||
|
||||
@ -318,11 +332,11 @@ const App = function() {
|
||||
const [devices, setDevices] = useState([]);
|
||||
const [currentDevID, setCurrentDevID] = useState(localStorage.getItem('currentDevID') || '');
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [topic, setTopic] = useState(localStorage.getItem('topic') || default_topic);
|
||||
const [url, setUrl] = useState(localStorage.getItem('url') || DefaultUrl);
|
||||
const [topic, setTopic] = useState(localStorage.getItem('topic') || DefaultTopic);
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
const [connected, setConnected] = useState(false);
|
||||
const responseHandlers = useRef({});
|
||||
let connSuccessful = false;
|
||||
|
||||
function addResponseHandler(correlationId, handler) {
|
||||
responseHandlers[correlationId] = handler;
|
||||
@ -332,32 +346,27 @@ const App = function() {
|
||||
delete responseHandlers[correlationId];
|
||||
}
|
||||
|
||||
const onRefresh = () => {
|
||||
window.location.reload();
|
||||
}
|
||||
const onRefresh = ev => window.location.reload();
|
||||
|
||||
const initConn = () => {
|
||||
client = mqtt.connect(url, {
|
||||
connectTimeout: 5000,
|
||||
reconnectPeriod: 0
|
||||
});
|
||||
MqttClient = mqtt.connect(url, {connectTimeout: 5000, reconnectPeriod: 0});
|
||||
|
||||
client.on('connect', () => {
|
||||
console.log('Connected to the broker');
|
||||
MqttClient.on('connect', () => {
|
||||
//console.log('Connected to the broker');
|
||||
setLoading(false);
|
||||
setError(null); // Reset error state upon successful connection
|
||||
connSuccessful = true;
|
||||
setConnected(true);
|
||||
|
||||
const statusTopic = topic + '/+/status'
|
||||
const txTopic = topic + '/+/tx'
|
||||
|
||||
const subscribe = (topic) => {
|
||||
client.subscribe(topic, (err) => {
|
||||
MqttClient.subscribe(topic, (err) => {
|
||||
if (err) {
|
||||
console.error('Error subscribing to topic:', err);
|
||||
setError('Error subscribing to topic');
|
||||
} else {
|
||||
console.log('Successfully subscribed to ', topic);
|
||||
//console.log('Successfully subscribed to ', topic);
|
||||
}
|
||||
});
|
||||
};
|
||||
@ -366,40 +375,34 @@ const App = function() {
|
||||
subscribe(txTopic)
|
||||
});
|
||||
|
||||
client.on('message', (topic, message) => {
|
||||
MqttClient.on('message', (topic, message) => {
|
||||
// console.log(`Received message from ${topic}: ${message.toString()}`);
|
||||
if (message.length == 0) return;
|
||||
let response;
|
||||
try {
|
||||
response = JSON.parse(message.toString());
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
|
||||
if (topic.endsWith("/status")) {
|
||||
const deviceID = topic.split('/')[1]
|
||||
let device = {};
|
||||
device.id = deviceID;
|
||||
const params = response.params
|
||||
if (topic.endsWith('/status')) {
|
||||
let device = {topic: topic, id: topic.split('/')[1], config: DefaultDeviceConfig};
|
||||
const params = response.params;
|
||||
if (!params) {
|
||||
console.error("Invalid response")
|
||||
return
|
||||
console.error('Invalid response');
|
||||
return;
|
||||
}
|
||||
device.online = params.status === "online"
|
||||
device.online = params.status === 'online'
|
||||
if (device.online) {
|
||||
device.config = {}
|
||||
device.config.led_status = params.led_status
|
||||
device.config.led_pin = params.led_pin
|
||||
device.config.brightness = params.brightness
|
||||
device.config.log_level = params.log_level
|
||||
device.config.crnt_fw = params.crnt_fw
|
||||
device.config.prev_fw = params.prev_fw
|
||||
device.config = params;
|
||||
if (!device.config.pins) device.config.pins = [];
|
||||
}
|
||||
setDevice(device)
|
||||
} else if (topic.endsWith("/tx")) {
|
||||
} else if (topic.endsWith('/tx')) {
|
||||
if (!response.id) {
|
||||
console.error("Invalid response")
|
||||
return
|
||||
console.error('Invalid response');
|
||||
return;
|
||||
}
|
||||
const handler = responseHandlers[response.id];
|
||||
if (handler) {
|
||||
@ -409,13 +412,13 @@ const App = function() {
|
||||
}
|
||||
});
|
||||
|
||||
client.on('error', (err) => {
|
||||
MqttClient.on('error', (err) => {
|
||||
console.error('Connection error:', err);
|
||||
setError('Connection cannot be established.');
|
||||
});
|
||||
|
||||
client.on('close', () => {
|
||||
if (!connSuccessful) {
|
||||
MqttClient.on('close', () => {
|
||||
if (!connected) {
|
||||
console.error('Failed to connect to the broker.');
|
||||
setError('Connection cannot be established.');
|
||||
setLoading(false);
|
||||
@ -423,9 +426,7 @@ const App = function() {
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
initConn();
|
||||
}, []);
|
||||
useEffect(() => initConn(), []);
|
||||
|
||||
const handlePublish = (methodName, parameters, timeout = 5000) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
@ -445,27 +446,21 @@ const App = function() {
|
||||
|
||||
if (currentDevID) {
|
||||
const rxTopic = topic + `/${currentDevID}/rx`;
|
||||
const rpcPayload = {
|
||||
method: methodName,
|
||||
id: randomID
|
||||
};
|
||||
|
||||
const rpcPayload = {method: methodName, id: randomID};
|
||||
if (parameters) {
|
||||
rpcPayload.params = parameters;
|
||||
}
|
||||
|
||||
client.publish(rxTopic, JSON.stringify(rpcPayload));
|
||||
MqttClient.publish(rxTopic, JSON.stringify(rpcPayload));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const getDeviceByID = (deviceID) => {
|
||||
return devices.find(d => d.id === deviceID);
|
||||
}
|
||||
const getDeviceByID = (deviceID) => devices.find(d => d.id === deviceID);
|
||||
|
||||
const setDevice = (devData) => {
|
||||
setDevices(prevDevices => {
|
||||
const devIndex = prevDevices.findIndex(device => device.id === devData.id);
|
||||
const devIndex =
|
||||
prevDevices.findIndex(device => device.id === devData.id);
|
||||
if (devIndex !== -1) {
|
||||
if (!devData.online && !devData.config) {
|
||||
const updatedDevices = [...prevDevices];
|
||||
@ -484,10 +479,7 @@ const App = function() {
|
||||
setDevices(prevDevices => {
|
||||
return prevDevices.map(device => {
|
||||
if (device.id === deviceID) {
|
||||
return {
|
||||
...device,
|
||||
config: config
|
||||
};
|
||||
return {...device, config: config};
|
||||
}
|
||||
return device;
|
||||
});
|
||||
@ -500,7 +492,7 @@ const App = function() {
|
||||
setCurrentDevID(device.id);
|
||||
localStorage.setItem('currentDevID', device.id);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (error) {
|
||||
return html`
|
||||
@ -513,7 +505,8 @@ const App = function() {
|
||||
Unable to connect to the MQTT broker.
|
||||
</p>
|
||||
<div class="text-center relative">
|
||||
<${Button} title="Retry" onclick=${onRefresh} icon=${Icons.refresh} class="absolute top-4 right-4" />
|
||||
<${Button} title="Retry" onclick=${onRefresh}
|
||||
icon=${Icons.refresh} class="absolute top-4 right-4" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -521,15 +514,17 @@ const App = function() {
|
||||
}
|
||||
|
||||
return html`
|
||||
<div class="min-h-screen bg-slate-100 flex flex-col space-y-2">
|
||||
<${Header} topic=${topic} setTopicFn=${setTopic}/>
|
||||
<div class="transition-all duration-300 transform flex flex-grow gap-4">
|
||||
<div class="flex-none bg-white rounded shadow ">
|
||||
<${Devices} devices=${devices} onClickFn=${onDeviceClick} />
|
||||
<//>
|
||||
<div class="flex-1 flex-grow bg-white rounded shadow h-full">
|
||||
<${Config} deviceData=${getDeviceByID(currentDevID)} setDeviceConfig=${setDeviceConfig} publishFn=${handlePublish}/>
|
||||
<//>
|
||||
<div class="h-full flex flex-col">
|
||||
<${Header}
|
||||
topic=${topic} setTopic=${setTopic} url=${url}
|
||||
setUrl=${setUrl} connected=${connected}
|
||||
/>
|
||||
<div class="flex grow overflow-auto">
|
||||
<${Sidebar} devices=${devices} onclick=${onDeviceClick} />
|
||||
<${DeviceControlPanel}
|
||||
device=${getDeviceByID(currentDevID)} connected=${connected}
|
||||
setDeviceConfig=${setDeviceConfig} publishFn=${handlePublish}
|
||||
/>
|
||||
<//>
|
||||
<//>`;
|
||||
};
|
||||
|
@ -10,29 +10,27 @@ static void signal_handler(int signo) {
|
||||
}
|
||||
|
||||
// Mocked device pins
|
||||
static bool s_pins[100];
|
||||
static bool s_pins[NUM_PINS];
|
||||
|
||||
void hal_gpio_write(int pin, bool status) {
|
||||
if (pin >= 0 && pin < (int) (sizeof(s_pins) / sizeof(s_pins[0]))) {
|
||||
bool hal_gpio_write(int pin, bool status) {
|
||||
bool ok = false;
|
||||
if (pin >= 0 && pin < NUM_PINS) {
|
||||
s_pins[pin] = status;
|
||||
ok = true;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool hal_gpio_read(int pin) {
|
||||
return (pin >= 0 && pin < (int) (sizeof(s_pins) / sizeof(s_pins[0])))
|
||||
? s_pins[pin]
|
||||
: false;
|
||||
}
|
||||
|
||||
int hal_led_pin(void) {
|
||||
return 0;
|
||||
return (pin >= 0 && pin < NUM_PINS) ? s_pins[pin] : false;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
struct mg_mgr mgr;
|
||||
int i;
|
||||
|
||||
// Parse command-line flags
|
||||
for (int i = 1; i < argc; i++) {
|
||||
for (i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "-u") == 0 && argv[i + 1] != NULL) {
|
||||
g_url = argv[++i];
|
||||
} else if (strcmp(argv[i], "-i") == 0 && argv[i + 1] != NULL) {
|
||||
@ -62,8 +60,8 @@ int main(int argc, char *argv[]) {
|
||||
mg_mgr_poll(&mgr, 50);
|
||||
}
|
||||
|
||||
web_free();
|
||||
mg_mgr_free(&mgr);
|
||||
web_destroy();
|
||||
MG_INFO(("Exiting on signal %d", s_signo));
|
||||
|
||||
return 0;
|
||||
|
@ -4,31 +4,50 @@
|
||||
#include "net.h"
|
||||
|
||||
char *g_url = MQTT_SERVER_URL;
|
||||
char *g_root_topic = MQTT_ROOT_TOPIC;
|
||||
char *g_device_id;
|
||||
char *g_root_topic;
|
||||
|
||||
static uint8_t s_qos = 1; // MQTT QoS
|
||||
static struct mg_connection *s_conn; // Client connection
|
||||
static struct mg_rpc *s_rpc_head = NULL;
|
||||
static struct mg_connection *s_conn; // MQTT Client connection
|
||||
static struct mg_rpc *s_rpc = NULL; // List of registered RPC methods
|
||||
|
||||
struct device_config {
|
||||
bool led_status;
|
||||
int led_pin;
|
||||
int brightness;
|
||||
int log_level;
|
||||
int pins[NUM_PINS]; // State of the GPIO pins
|
||||
int log_level; // Device logging level, 0-4
|
||||
};
|
||||
|
||||
static struct device_config s_device_config;
|
||||
|
||||
// This is for newlib and TLS (mbedTLS)
|
||||
uint64_t mg_now(void) {
|
||||
return mg_millis();
|
||||
}
|
||||
// Device ID generation function. Create an ID that is unique
|
||||
// for a given device, and does not change between device restarts.
|
||||
static void set_device_id(void) {
|
||||
char buf[15] = "";
|
||||
|
||||
static void generate_device_id(void) {
|
||||
char tmp[DEVICE_ID_LEN + 1];
|
||||
tmp[DEVICE_ID_LEN] = '\0';
|
||||
mg_random_str(tmp, DEVICE_ID_LEN);
|
||||
g_device_id = strdup(tmp);
|
||||
#ifdef _WIN32
|
||||
unsigned serial = 0;
|
||||
if (GetVolumeInformationA("c:\\", NULL, 0, &serial, NULL, NULL, NULL, 0)) {
|
||||
mg_snprintf(buf, sizeof(buf), "%lx", serial);
|
||||
}
|
||||
#elif defined(__APPLE__)
|
||||
FILE *fp = popen(
|
||||
"ioreg -l | grep IOPlatformSerialNumber | cut -d'\"' -f4 | tr -d $'\n'",
|
||||
"r");
|
||||
if (fp != NULL) {
|
||||
fread(buf, 1, sizeof(buf), fp);
|
||||
fclose(fp);
|
||||
}
|
||||
#elif defined(__linux__)
|
||||
char *id = mg_file_read(&mg_fs_posix, "/etc/machine-id", NULL);
|
||||
if (id != NULL) {
|
||||
mg_snprintf(buf, sizeof(buf), "%s", id);
|
||||
free(id);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (buf[0] == '\0') mg_snprintf(buf, sizeof(buf), "%s", "MyDeviceID");
|
||||
|
||||
buf[sizeof(buf) - 1] = '\0';
|
||||
g_device_id = strdup(buf);
|
||||
}
|
||||
|
||||
static size_t print_fw_status(void (*out)(char, void *), void *ptr,
|
||||
@ -40,45 +59,51 @@ static size_t print_fw_status(void (*out)(char, void *), void *ptr,
|
||||
MG_ESC("timestamp"), mg_ota_timestamp(fw));
|
||||
}
|
||||
|
||||
static size_t print_ints(void (*out)(char, void *), void *ptr, va_list *ap) {
|
||||
int *array = va_arg(*ap, int *);
|
||||
size_t i, len = 0, num_elems = va_arg(*ap, size_t);
|
||||
for (i = 0; i < num_elems; i++) {
|
||||
len += mg_xprintf(out, ptr, "%s%d", i ? "," : "", array[i]);
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
static void publish_status(struct mg_connection *c) {
|
||||
char *status_topic = mg_mprintf("%s/%s/status", g_root_topic, g_device_id);
|
||||
struct mg_str pubt = mg_str(status_topic);
|
||||
char topic[100];
|
||||
struct mg_mqtt_opts pub_opts;
|
||||
struct mg_iobuf io = {0, 0, 0, 512};
|
||||
|
||||
// Print JSON notification into the io buffer
|
||||
mg_xprintf(mg_pfn_iobuf, &io,
|
||||
"{%m:%m,%m:{%m:%m,%m:%d,%m:[%M],%m:%M,%m:%M}}", //
|
||||
MG_ESC("method"), MG_ESC("status.notify"), MG_ESC("params"), //
|
||||
MG_ESC("status"), MG_ESC("online"), //
|
||||
MG_ESC(("log_level")), s_device_config.log_level, //
|
||||
MG_ESC(("pins")), print_ints, s_device_config.pins, NUM_PINS, //
|
||||
MG_ESC(("crnt_fw")), print_fw_status, MG_FIRMWARE_CURRENT, //
|
||||
MG_ESC(("prev_fw")), print_fw_status, MG_FIRMWARE_PREVIOUS);
|
||||
|
||||
memset(&pub_opts, 0, sizeof(pub_opts));
|
||||
pub_opts.topic = pubt;
|
||||
s_device_config.led_status = hal_gpio_read(s_device_config.led_pin);
|
||||
char *device_status_json = mg_mprintf(
|
||||
"{%m:%m,%m:{%m:%m,%m:%s,%m:%hhu,%m:%hhu,%m:%hhu,%m:%M,%m:%M}}",
|
||||
MG_ESC("method"), MG_ESC("status.notify"), MG_ESC("params"),
|
||||
MG_ESC("status"), MG_ESC("online"), MG_ESC("led_status"),
|
||||
s_device_config.led_status ? "true" : "false", MG_ESC("led_pin"),
|
||||
s_device_config.led_pin, MG_ESC("brightness"), s_device_config.brightness,
|
||||
MG_ESC(("log_level")), s_device_config.log_level, MG_ESC(("crnt_fw")),
|
||||
print_fw_status, MG_FIRMWARE_CURRENT, MG_ESC(("prev_fw")),
|
||||
print_fw_status, MG_FIRMWARE_PREVIOUS);
|
||||
struct mg_str data = mg_str(device_status_json);
|
||||
pub_opts.message = data;
|
||||
pub_opts.qos = s_qos, pub_opts.retain = true;
|
||||
mg_snprintf(topic, sizeof(topic), "%s/%s/status", g_root_topic, g_device_id);
|
||||
pub_opts.topic = mg_str(topic);
|
||||
pub_opts.message = mg_str_n((char *) io.buf, io.len);
|
||||
pub_opts.qos = s_qos;
|
||||
pub_opts.retain = true;
|
||||
mg_mqtt_pub(c, &pub_opts);
|
||||
MG_INFO(("%lu PUBLISHED %.*s -> %.*s", c->id, (int) data.len, data.ptr,
|
||||
(int) pubt.len, pubt.ptr));
|
||||
free(device_status_json);
|
||||
free(status_topic);
|
||||
MG_INFO(("%lu PUBLISHED %s -> %.*s", c->id, topic, io.len, io.buf));
|
||||
mg_iobuf_free(&io);
|
||||
}
|
||||
|
||||
static void publish_response(struct mg_connection *c, char *buf, size_t len) {
|
||||
char *tx_topic = mg_mprintf("%s/%s/tx", g_root_topic, g_device_id);
|
||||
struct mg_str pubt = mg_str(tx_topic);
|
||||
struct mg_mqtt_opts pub_opts;
|
||||
char topic[100];
|
||||
mg_snprintf(topic, sizeof(topic), "%s/%s/tx", g_root_topic, g_device_id);
|
||||
memset(&pub_opts, 0, sizeof(pub_opts));
|
||||
pub_opts.topic = pubt;
|
||||
struct mg_str data = mg_str_n(buf, len);
|
||||
pub_opts.message = data;
|
||||
pub_opts.topic = mg_str(topic);
|
||||
pub_opts.message = mg_str_n(buf, len);
|
||||
pub_opts.qos = s_qos;
|
||||
mg_mqtt_pub(c, &pub_opts);
|
||||
MG_INFO(("%lu PUBLISHED %.*s -> %.*s", c->id, (int) data.len, data.ptr,
|
||||
(int) pubt.len, pubt.ptr));
|
||||
free(tx_topic);
|
||||
MG_INFO(("%lu PUBLISHED %s -> %.*s", c->id, topic, len, buf));
|
||||
}
|
||||
|
||||
static void subscribe(struct mg_connection *c) {
|
||||
@ -94,29 +119,26 @@ static void subscribe(struct mg_connection *c) {
|
||||
}
|
||||
|
||||
static void rpc_config_set(struct mg_rpc_req *r) {
|
||||
bool tmp_status, ok;
|
||||
int tmp_brightness, tmp_level, tmp_pin;
|
||||
struct device_config dc = s_device_config;
|
||||
dc.log_level = (int) mg_json_get_long(r->frame, "$.params.log_level", -1);
|
||||
|
||||
ok = mg_json_get_bool(r->frame, "$.params.led_status", &tmp_status);
|
||||
if (ok) s_device_config.led_status = tmp_status;
|
||||
|
||||
tmp_brightness = (int) mg_json_get_long(r->frame, "$.params.brightness", -1);
|
||||
if (tmp_brightness >= 0) s_device_config.brightness = tmp_brightness;
|
||||
|
||||
tmp_level = (int) mg_json_get_long(r->frame, "$.params.log_level", -1);
|
||||
if (tmp_level >= 0) {
|
||||
s_device_config.log_level = tmp_level;
|
||||
mg_log_set(s_device_config.log_level);
|
||||
if (dc.log_level < 0 || dc.log_level > MG_LL_VERBOSE) {
|
||||
mg_rpc_err(r, -32602, "Log level must be from 0 to 4");
|
||||
} else {
|
||||
int i, val;
|
||||
for (i = 0; i < NUM_PINS; i++) {
|
||||
char path[20];
|
||||
mg_snprintf(path, sizeof(path), "$.params.pins[%lu]", i);
|
||||
val = (int) mg_json_get_long(r->frame, path, -1);
|
||||
if (val >= 0 && val != dc.pins[i]) {
|
||||
dc.pins[i] = val;
|
||||
hal_gpio_write((int) i, val);
|
||||
}
|
||||
|
||||
tmp_pin = (int) mg_json_get_long(r->frame, "$.params.led_pin", -1);
|
||||
if (tmp_pin != -1) s_device_config.led_pin = tmp_pin;
|
||||
|
||||
if (tmp_pin != -1 && ok) {
|
||||
hal_gpio_write(s_device_config.led_pin, s_device_config.led_status);
|
||||
}
|
||||
|
||||
mg_rpc_ok(r, "%m", MG_ESC("ok"));
|
||||
mg_log_set(dc.log_level);
|
||||
s_device_config = dc;
|
||||
mg_rpc_ok(r, "true");
|
||||
}
|
||||
}
|
||||
|
||||
static void rpc_ota_commit(struct mg_rpc_req *r) {
|
||||
@ -143,31 +165,29 @@ static void rpc_ota_rollback(struct mg_rpc_req *r) {
|
||||
static void rpc_ota_upload(struct mg_rpc_req *r) {
|
||||
long ofs = mg_json_get_long(r->frame, "$.params.offset", -1);
|
||||
long tot = mg_json_get_long(r->frame, "$.params.total", -1);
|
||||
int len;
|
||||
char *file_chunk = mg_json_get_b64(r->frame, "$.params.chunk", &len);
|
||||
if (!file_chunk) {
|
||||
int len = 0;
|
||||
char *buf = mg_json_get_b64(r->frame, "$.params.chunk", &len);
|
||||
if (buf == NULL) {
|
||||
mg_rpc_err(r, 1, "Error processing the binary chunk.");
|
||||
return;
|
||||
}
|
||||
struct mg_str data = mg_str_n(file_chunk, len);
|
||||
} else {
|
||||
if (ofs < 0 || tot < 0) {
|
||||
mg_rpc_err(r, 1, "offset and total not set");
|
||||
} else if (ofs == 0 && mg_ota_begin((size_t) tot) == false) {
|
||||
mg_rpc_err(r, 1, "mg_ota_begin(%ld) failed\n", tot);
|
||||
} else if (data.len > 0 && mg_ota_write(data.ptr, data.len) == false) {
|
||||
mg_rpc_err(r, 1, "mg_ota_write(%lu) @%ld failed\n", data.len, ofs);
|
||||
} else if (len > 0 && mg_ota_write(buf, len) == false) {
|
||||
mg_rpc_err(r, 1, "mg_ota_write(%lu) @%ld failed\n", len, ofs);
|
||||
mg_ota_end();
|
||||
} else if (data.len == 0 && mg_ota_end() == false) {
|
||||
} else if (len == 0 && mg_ota_end() == false) {
|
||||
mg_rpc_err(r, 1, "mg_ota_end() failed\n", tot);
|
||||
} else {
|
||||
mg_rpc_ok(r, "%m", MG_ESC("ok"));
|
||||
if (data.len == 0) {
|
||||
// Successful mg_ota_end() called, schedule device reboot
|
||||
if (len == 0) { // Successful mg_ota_end() called, schedule device reboot
|
||||
mg_timer_add(s_conn->mgr, 500, 0, (void (*)(void *)) mg_device_reset,
|
||||
NULL);
|
||||
}
|
||||
}
|
||||
free(file_chunk);
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
@ -187,11 +207,12 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
} else if (ev == MG_EV_MQTT_MSG) {
|
||||
// When we get echo response, print it
|
||||
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
|
||||
if (mm->data.len < 1024)
|
||||
MG_INFO(("%lu RECEIVED %.*s <- %.*s", c->id, (int) mm->data.len,
|
||||
mm->data.ptr, (int) mm->topic.len, mm->topic.ptr));
|
||||
struct mg_iobuf io = {0, 0, 0, 512};
|
||||
struct mg_rpc_req r = {&s_rpc_head, 0, mg_pfn_iobuf, &io, 0, mm->data};
|
||||
struct mg_rpc_req r = {&s_rpc, NULL, mg_pfn_iobuf,
|
||||
&io, NULL, {mm->data.ptr, mm->data.len}};
|
||||
size_t clipped_len = mm->data.len > 512 ? 512 : mm->data.len;
|
||||
MG_INFO(("%lu RECEIVED %.*s <- %.*s", c->id, clipped_len, mm->data.ptr,
|
||||
mm->topic.len, mm->topic.ptr));
|
||||
mg_rpc_process(&r);
|
||||
if (io.buf) {
|
||||
publish_response(c, (char *) io.buf, io.len);
|
||||
@ -206,51 +227,54 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
}
|
||||
|
||||
// Timer function - recreate client connection if it is closed
|
||||
static void timer_fn(void *arg) {
|
||||
static void timer_reconnect(void *arg) {
|
||||
struct mg_mgr *mgr = (struct mg_mgr *) arg;
|
||||
if (s_conn == NULL) {
|
||||
char *status_topic = mg_mprintf("%s/%s/status", g_root_topic, g_device_id);
|
||||
char *msg = mg_mprintf("{%m:%m,%m:{%m:%m}}", MG_ESC("method"),
|
||||
MG_ESC("status.notify"), MG_ESC("params"),
|
||||
struct mg_mqtt_opts opts;
|
||||
char topic[100], message[100];
|
||||
mg_snprintf(topic, sizeof(topic), "%s/%s/status", g_root_topic,
|
||||
g_device_id);
|
||||
mg_snprintf(message, sizeof(message), "{%m:%m,%m:{%m:%m}}",
|
||||
MG_ESC("method"), MG_ESC("status.notify"), MG_ESC("params"),
|
||||
MG_ESC("status"), MG_ESC("offline"));
|
||||
|
||||
struct mg_mqtt_opts opts = {.clean = true,
|
||||
.qos = s_qos,
|
||||
.topic = mg_str(status_topic),
|
||||
.version = 4,
|
||||
.keepalive = MQTT_KEEP_ALIVE_INTERVAL,
|
||||
.retain = true,
|
||||
.message = mg_str(msg)};
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
opts.clean = true;
|
||||
opts.qos = s_qos;
|
||||
opts.topic = mg_str(topic);
|
||||
opts.version = 4;
|
||||
opts.keepalive = MQTT_KEEPALIVE_SEC;
|
||||
opts.retain = true;
|
||||
opts.message = mg_str(message);
|
||||
s_conn = mg_mqtt_connect(mgr, g_url, &opts, fn, NULL);
|
||||
free(msg);
|
||||
free(status_topic);
|
||||
}
|
||||
}
|
||||
|
||||
static void timer_keepalive(void *arg) {
|
||||
static void timer_ping(void *arg) {
|
||||
mg_mqtt_send_header(s_conn, MQTT_CMD_PINGREQ, 0, 0);
|
||||
(void) arg;
|
||||
}
|
||||
|
||||
void web_init(struct mg_mgr *mgr) {
|
||||
int pingreq_interval_ms = MQTT_KEEP_ALIVE_INTERVAL * 1000 - 500;
|
||||
if (!g_device_id) generate_device_id();
|
||||
if (!g_root_topic) g_root_topic = MQTT_ROOT_TOPIC;
|
||||
int i, ping_interval_ms = MQTT_KEEPALIVE_SEC * 1000 - 500;
|
||||
set_device_id();
|
||||
s_device_config.log_level = (int) mg_log_level;
|
||||
s_device_config.led_pin = hal_led_pin();
|
||||
for (i = 0; i < NUM_PINS; i++) {
|
||||
s_device_config.pins[i] = hal_gpio_read(i);
|
||||
}
|
||||
|
||||
// Configure JSON-RPC functions we're going to handle
|
||||
mg_rpc_add(&s_rpc_head, mg_str("config.set"), rpc_config_set, NULL);
|
||||
mg_rpc_add(&s_rpc_head, mg_str("ota.commit"), rpc_ota_commit, NULL);
|
||||
mg_rpc_add(&s_rpc_head, mg_str("device.reset"), rpc_device_reset, NULL);
|
||||
mg_rpc_add(&s_rpc_head, mg_str("ota.rollback"), rpc_ota_rollback, NULL);
|
||||
mg_rpc_add(&s_rpc_head, mg_str("ota.upload"), rpc_ota_upload, NULL);
|
||||
mg_rpc_add(&s_rpc, mg_str("config.set"), rpc_config_set, NULL);
|
||||
mg_rpc_add(&s_rpc, mg_str("ota.commit"), rpc_ota_commit, NULL);
|
||||
mg_rpc_add(&s_rpc, mg_str("ota.rollback"), rpc_ota_rollback, NULL);
|
||||
mg_rpc_add(&s_rpc, mg_str("ota.upload"), rpc_ota_upload, NULL);
|
||||
mg_rpc_add(&s_rpc, mg_str("device.reset"), rpc_device_reset, NULL);
|
||||
|
||||
mg_timer_add(mgr, 3000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, mgr);
|
||||
mg_timer_add(mgr, pingreq_interval_ms, MG_TIMER_REPEAT, timer_keepalive, mgr);
|
||||
mg_timer_add(mgr, 3000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_reconnect,
|
||||
mgr);
|
||||
mg_timer_add(mgr, ping_interval_ms, MG_TIMER_REPEAT, timer_ping, mgr);
|
||||
}
|
||||
|
||||
void web_destroy() {
|
||||
mg_rpc_del(&s_rpc_head, NULL); // Deallocate RPC handlers
|
||||
void web_free(void) {
|
||||
mg_rpc_del(&s_rpc, NULL); // Deallocate RPC handlers
|
||||
free(g_device_id);
|
||||
}
|
||||
|
@ -8,25 +8,21 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef MQTT_DASHBOARD
|
||||
#define MQTT_DASHBOARD 1
|
||||
#endif
|
||||
|
||||
#define DEVICE_ID_LEN 10
|
||||
#define MQTT_KEEP_ALIVE_INTERVAL 60
|
||||
#define MQTT_KEEPALIVE_SEC 60
|
||||
#define MQTT_SERVER_URL "mqtt://broker.hivemq.com:1883"
|
||||
#define MQTT_ROOT_TOPIC "topic_mg_device"
|
||||
#define MQTT_ROOT_TOPIC "mg_mqtt_dashboard"
|
||||
#define NUM_PINS 30
|
||||
|
||||
extern char *g_url;
|
||||
extern char *g_device_id;
|
||||
extern char *g_root_topic;
|
||||
|
||||
void web_init(struct mg_mgr *mgr);
|
||||
void web_destroy();
|
||||
void web_free(void);
|
||||
|
||||
void hal_gpio_write(int pin, bool status);
|
||||
bool hal_gpio_write(int pin, bool status);
|
||||
bool hal_gpio_read(int pin);
|
||||
int hal_led_pin(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user