github.com/simpleiot/simpleiot@v0.18.3/frontend/public/main.js (about)

     1  // import { BLE } from "./ble.js"
     2  
     3  // eslint-disable-next-line no-undef
     4  const app = Elm.Main.init({
     5  	flags: JSON.parse(localStorage.getItem("storage")),
     6  })
     7  
     8  app.ports.save_.subscribe((storage) => {
     9  	localStorage.setItem("storage", JSON.stringify(storage))
    10  	app.ports.load_.send(storage)
    11  })
    12  
    13  app.ports.out.subscribe(({ action, data }) =>
    14  	actions[action]
    15  		? actions[action](data)
    16  		: console.warn(`I didn't recognize action "${action}".`)
    17  )
    18  
    19  // maps actions to functions!
    20  const actions = {
    21  	LOG: (message) => console.log(`From Elm:`, message),
    22  	CLIPBOARD: (message) => {
    23  		if (navigator.clipboard) {
    24  			writeClipboard(message)
    25  		} else {
    26  			console.log("clipboard not available")
    27  		}
    28  	},
    29  }
    30  
    31  console.log("Simple IoT Javascript code")
    32  
    33  const writeClipboard = (data) => {
    34  	navigator.clipboard
    35  		.writeText(data)
    36  		.then(() => {
    37  			// FIXME, should probably send something back to elm
    38  			// Success!
    39  		})
    40  		.catch((err) => {
    41  			console.log("Something went wrong", err)
    42  		})
    43  }
    44  
    45  /*
    46  export const main = (app) => {
    47  	const ble = new BLE(async () => {
    48  		const state = await ble.getState()
    49  		app.ports.portIn.send(state)
    50  	})
    51    */
    52  /*
    53   * Websocket code needs cleaned up
    54   *
    55   * Var loc = window.location,
    56   * new_uri;
    57   * if (loc.protocol === "https:") {
    58   * ws_uri = "wss:";
    59   * } else {
    60   * ws_uri = "ws:";
    61   * }
    62   * ws_uri += "//" + loc.host;
    63   * ws_uri += "/ws";
    64   * var conn = new WebSocket(ws_uri);
    65   * conn.onclose = function(evt) {
    66   * console.log("WS connection closed");
    67   * };
    68   * conn.onmessage = function(evt) {
    69   * var obj = JSON.parse(evt.data);
    70   * app.ports.portIn.send(obj);
    71   * };
    72   */
    73  /*
    74  
    75  	app.ports.portOut.subscribe(async function (data) {
    76  		let state
    77  		switch (data.cmd) {
    78  			case "scan":
    79  				try {
    80  					await ble.request()
    81  					state = await ble.getState()
    82  					app.ports.portIn.send(state)
    83  				} catch (e) {
    84  					console.log("scanning error: ", e)
    85  				}
    86  				break
    87  			case "disconnect":
    88  				try {
    89  					await ble.disconnect()
    90  					state = await ble.getState()
    91  					app.ports.portIn.send(state)
    92  				} catch (e) {
    93  					console.log("disconnect error: ", e)
    94  				}
    95  				break
    96  			case "configureWifi":
    97  				try {
    98  					await ble.configureWifi(data)
    99  					state = await ble.getState()
   100  					app.ports.portIn.send(state)
   101  				} catch (e) {
   102  					console.log("configure GW WiFi error: ", e)
   103  				}
   104  				break
   105  			case "configureTimer":
   106  				try {
   107  					await ble.configureTimer(data)
   108  					state = await ble.getState()
   109  					app.ports.portIn.send(state)
   110  				} catch (e) {
   111  					console.log("configure GW configure Timer error: ", e)
   112  				}
   113  				break
   114  
   115  			case "fireTimer":
   116  				try {
   117  					await ble.fireTimer(data)
   118  					state = await ble.getState()
   119  					app.ports.portIn.send(state)
   120  				} catch (e) {
   121  					console.log("configure GW fire Timer error: ", e)
   122  				}
   123  				break
   124  
   125  			default:
   126  				console.log("unknown cmd: ", data.cmd)
   127  		}
   128  	})
   129  }
   130    */