'use strict'; import {Component, h, html, render, useEffect, useState, useRef} from './preact.min.js'; const Message = m => html` ${m.message.data} `; const App = function(props) { const [cfg, setCfg] = useState({tcp: {}, ws: {}, mqtt: {}}); const [messages, setMessages] = useState([]); const [connected, setConnected] = useState(false); const [txt, setTxt] = useState(''); const [ws, setWs] = useState(null); const refresh = () => fetch('/api/config/get').then(r => r.json()).then(r => setCfg(r)); const getport = (url, v) => ((url || '').match(/.*:(\d+)/) || ['', v])[1]; const watchWebsocket = function() { // Connect to websocker port, to implement WS console var reconnect = function() { var port = getport(cfg.ws.url, 4002); var l = window.location, proto = l.protocol.replace('http', 'ws'); var tid, url = `${proto}//${l.hostname}:${port}/ws`; // console.log(url); var ws = new WebSocket(url); ws.onopen = () => { setConnected(true); setWs(ws); }; ws.onmessage = ev => { // console.log(ev, ev.data); setMessages(x => x.concat([{data: ev.data, uart: true}])); }; ws.onclose = function() { clearTimeout(tid); tid = setTimeout(reconnect, 1000); setConnected(false); setWs(null); }; }; reconnect(); }; useEffect(() => { refresh(); watchWebsocket(); }, []); const sendmessage = ev => { setMessages(x => x.concat([{data: txt + '\n', uart: false}])); if (ws) ws.send(txt + '\n'); setTxt(''); }; const onchange = ev => fetch('/api/config/set', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(cfg), }).then(r => ws && ws.close()); const set = obj => setCfg(x => Object.assign(x, obj)); const nset = (n,obj) => setCfg(x => Object.assign(x, {[n]: Object.assign(x[n],obj)})); const setTx = ev => set({tx: parseInt(ev.target.value)}); const setRx = ev => set({rx: parseInt(ev.target.value)}); const setBaud = ev => set({baud: parseInt(ev.target.value)}); const setTcpUrl = ev => nset('tcp', {url: `tcp://0.0.0.0:${ev.target.value}`}); const setWsUrl = ev => nset('ws',{url: `ws://0.0.0.0:${ev.target.value}`}); const setMqttUrl = ev => nset('mqtt',{url: ev.target.value}); const setTcpEna = ev => (nset('tcp',{enable: ev.target.checked}), onchange()); const setWsEna = ev => (nset('ws',{enable: ev.target.checked}), onchange()); const setMqttEna = ev =>(nset('mqtt', {enable: ev.target.checked}), onchange()); return html`

UART \u27F7 network bridge

${JSON.stringify(cfg, null, 2)}

UART configuration

Network configuration

Note: to connect over MQTT, open console, subscribe to b/tx and publish to b/rx
Note: to connect over TCP, use netcat utility:
$ nc ${location.hostname} ${getport(cfg.tcp.url, 4001)}
UART consoleworks over the local WS port. WebSocket status: \u25cf ${connected ? 'connected' : 'disconnected'}
setTxt(ev.target.value)} />
    ${messages.map(message => h(Message, {message}))}
  
`; }; window.onload = () => render(h(App), document.body);