diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9e4a3ba --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "mos.port": "COM6", + "mos.board": "ESP32" +} diff --git a/fs/main.js b/fs/main.js index 0947450..bcf884d 100644 --- a/fs/main.js +++ b/fs/main.js @@ -1,71 +1,97 @@ -import htm from './htm.min.js'; -import {Component, h, render} from './preact.min.js'; +import htm from "./htm.min.js"; +import { Component, h, render } from "./preact.min.js"; const html = htm.bind(h); +function DropdownList({ networks, onSelect }) { + return html` + + `; +} + class App extends Component { - state = {connected: false, ssid: '', pass: '', spin: false, frames: []}; + state = { + connected: false, + ssid: "", + pass: "", + spin: false, + frames: [], + wifiNets: [], + }; + componentDidMount() { const logframe = (marker, frame) => { - this.setState( - state => ({ - connected: state.connected, - frames: state.frames.concat(marker + JSON.stringify(frame)) - })); + this.setState((state) => ({ + connected: state.connected, + frames: state.frames.concat(marker + JSON.stringify(frame)), + })); }; - // Setup JSON-RPC engine - var rpc = mkrpc('ws://' + location.host + '/rpc'); - rpc.onopen = ev => { + // Setup JSON-RPC engine and Wifi list + var rpc = mkrpc("ws://" + location.host + "/rpc"); + rpc.onopen = (ev) => { // When RPC is connected, fetch list of supported RPC services - this.setState({connected: true}); - rpc.call('RPC.List').then(res => console.log(res)); + this.setState({ connected: true }); + rpc.call("Wifi.Scan","",10000).then((res) => { + this.setState({ wifiNets: res.result.map((item) => item.ssid) }); + }); + rpc.call("RPC.List").then((res) => console.log(res)); }; - rpc.onclose = ev => this.setState({connected: false}); - rpc.onout = ev => logframe('-> ', ev); - rpc.onin = ev => logframe('<- ', ev); + rpc.onclose = (ev) => this.setState({ connected: false }); + rpc.onout = (ev) => logframe("-> ", ev); + rpc.onin = (ev) => logframe("<- ", ev); this.rpc = rpc; } render(props, state) { - const onssid = ev => this.setState({ssid: ev.target.value}); - const onpass = ev => this.setState({pass: ev.target.value}); - const onclick = ev => { + const onssid = (ev) => this.setState({ ssid: ev }); + const onpass = (ev) => this.setState({ pass: ev.target.value }); + + //const wifiOptions = ["2Apple", "Banana", "Orange"]; + // const ssidlist = this.rpc.call("RPC.Scan").then((res) => console.log(res)); + const onclick = (ev) => { // Button press. Update device's configuration - var sta = {enable: true, ssid: state.ssid, pass: state.pass}; - var config = {wifi: {sta: sta, ap: {enable: false}}}; + var sta = { enable: true, ssid: state.ssid, pass: state.pass }; + var config = { wifi: { sta: sta, ap: { enable: false } } }; // var config = {debug: {level: +state.ssid}}; - this.setState({spin: true}); - this.rpc.call('Config.Set', {config, save: true, reboot: true}) - .catch(err => alert('Error: ' + err)) - .then(ev => this.setState({spin: false})); + this.setState({ spin: true }); + this.rpc + .call("Config.Set", { config, save: true, reboot: true }) + .catch((err) => alert("Error: " + err)) + .then((ev) => this.setState({ spin: false })); }; - return html` -
${state.frames.join('\n')}-
${state.frames.join("\n")}+