'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: ''}); const [messages, setMessages] = useState([]); const [txt, setTxt] = useState(''); const [connected, setConnected] = useState(false); const [ws, setWs] = useState(null); const tcp_port = cfg.tcp.split(':')[2] || 4001; const ws_port = cfg.ws.split(':')[2] || 4002; const refresh = () => fetch('/api/config/get').then(r => r.json()).then(r => setCfg(r)); const watchWebsocket = function() { // Connect to websocker port, to implement WS console var l = window.location, proto = l.protocol.replace('http', 'ws'); var tid, url = `${proto}//${l.hostname}:${ws_port}/ws`; console.log(url); var reconnect = function() { 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(''); }; return html`

UART \u27F7 network bridge

UART settings

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

RX pin ${cfg.rx}, TX pin ${cfg.tx}, baud ${cfg.baud}

TCP listener: port ${tcp_port}

Use netcat to connect:

$ nc ${location.hostname} ${tcp_port}

Websocket listener: port ${ws_port}

UART console. Status: ${connected ? 'connected' : 'disconnected'}
    ${messages.map(message => h(Message, {message}))}
  
setTxt(ev.target.value)} />
`; }; window.onload = () => render(h(App), document.body);