add some initial API code
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1 +1 @@
|
|||||||
"use strict";
|
"use strict";const{contextBridge:i,ipcRenderer:o}=require("electron");i.exposeInMainWorld("nanodevices",{list(){o.invoke("nanodevices:list")},connect(e){o.invoke("nanodevices:connect",e)},disconnect(){o.invoke("nanodevices:disconnect")},on_device_attached(e){o.on("nanodevice-attached",(c,n)=>e(n))},on_device_detached(e){o.on("nanodevice-detached",(c,n)=>e(n))}});i.exposeInMainWorld("nanodevice",{get_value(e,c,n){o.invoke("nano:get",e,c,n)},set_value(e,c,n,t){o.invoke("nano:set",e,c,n,t)},on_value(e){o.on("nano-onvalue",(c,n)=>e(n))}});
|
||||||
|
|||||||
19
src/backend/nano.js
Normal file
19
src/backend/nano.js
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
|
||||||
|
const nano = {
|
||||||
|
get(pid, tid, vid) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
resolve({ pid: pid, tid: tid, vid: vid, value: 0.0 });
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
set(pid, tid, vid, value) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
resolve({ pid: pid, tid: tid, vid: vid, value: value });
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
onValueReceived(listener) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default nano;
|
||||||
23
src/backend/nanodevices.js
Normal file
23
src/backend/nanodevices.js
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
|
||||||
|
const nanodevices = {
|
||||||
|
list() {
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
|
||||||
|
async connect(devicename) {
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
|
||||||
|
async disconnect() {
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
|
||||||
|
onAttach(listener) {
|
||||||
|
},
|
||||||
|
|
||||||
|
onDetach(listener) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default nanodevices;
|
||||||
41
src/main.js
41
src/main.js
@@ -1,17 +1,21 @@
|
|||||||
import { app, BrowserWindow } from 'electron';
|
import { app, BrowserWindow } from 'electron';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import 'electron-squirrel-startup';
|
import ess from 'electron-squirrel-startup';
|
||||||
|
import { ipcMain } from 'electron';
|
||||||
|
import nanodevices from './backend/nanodevices.js';
|
||||||
|
import nano from './backend/nano.js';
|
||||||
|
|
||||||
// TODO Handle creating/removing shortcuts on Windows when installing/uninstalling.
|
|
||||||
//if (require('electron-squirrel-startup')) {
|
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
|
||||||
// app.quit();
|
if (ess) {
|
||||||
//}
|
app.quit();
|
||||||
|
}
|
||||||
|
|
||||||
const createWindow = () => {
|
const createWindow = () => {
|
||||||
// Create the browser window.
|
// Create the browser window.
|
||||||
const mainWindow = new BrowserWindow({
|
const mainWindow = new BrowserWindow({
|
||||||
width: 800,
|
width: 1100,
|
||||||
height: 600,
|
height: 912,
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
preload: path.join(__dirname, 'preload.js'),
|
preload: path.join(__dirname, 'preload.js'),
|
||||||
},
|
},
|
||||||
@@ -26,12 +30,33 @@ const createWindow = () => {
|
|||||||
|
|
||||||
// Open the DevTools.
|
// Open the DevTools.
|
||||||
mainWindow.webContents.openDevTools();
|
mainWindow.webContents.openDevTools();
|
||||||
|
|
||||||
|
return mainWindow;
|
||||||
};
|
};
|
||||||
|
|
||||||
// This method will be called when Electron has finished
|
// This method will be called when Electron has finished
|
||||||
// initialization and is ready to create browser windows.
|
// initialization and is ready to create browser windows.
|
||||||
// Some APIs can only be used after this event occurs.
|
// Some APIs can only be used after this event occurs.
|
||||||
app.on('ready', createWindow);
|
app.whenReady().then(() => {
|
||||||
|
ipcMain.handle('nanodevices:list', nanodevices.list);
|
||||||
|
ipcMain.handle('nanodevices:connect', nanodevices.connect);
|
||||||
|
ipcMain.handle('nanodevices:disconnect', nanodevices.disconnect);
|
||||||
|
ipcMain.handle('nano:get', nano.get);
|
||||||
|
ipcMain.handle('nano:set', nano.set);
|
||||||
|
const mainWindow = createWindow();
|
||||||
|
nanodevices.onAttach((device) => {
|
||||||
|
console.log("Attached device", device);
|
||||||
|
mainWindow.webContents.send('nanodevice-attached', device);
|
||||||
|
});
|
||||||
|
nanodevices.onDetach((device) => {
|
||||||
|
console.log("Detached device", device);
|
||||||
|
mainWindow.webContents.send('nanodevice-detached', device);
|
||||||
|
});
|
||||||
|
nano.onValueReceived((value) => {
|
||||||
|
console.log("Value received", value);
|
||||||
|
mainWindow.webContents.send('nano-onvalue', value);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// Quit when all windows are closed, except on macOS. There, it's common
|
// Quit when all windows are closed, except on macOS. There, it's common
|
||||||
// for applications and their menu bar to stay active until the user quits
|
// for applications and their menu bar to stay active until the user quits
|
||||||
|
|||||||
@@ -1,2 +1,22 @@
|
|||||||
// See the Electron documentation for details on how to use preload scripts:
|
// See the Electron documentation for details on how to use preload scripts:
|
||||||
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts
|
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts
|
||||||
|
|
||||||
|
const { contextBridge, ipcRenderer } = require('electron')
|
||||||
|
|
||||||
|
|
||||||
|
// expose an API to choose available devices
|
||||||
|
contextBridge.exposeInMainWorld('nanodevices', {
|
||||||
|
list() { ipcRenderer.invoke('nanodevices:list'); },
|
||||||
|
connect(devicename) { ipcRenderer.invoke('nanodevices:connect', devicename); },
|
||||||
|
disconnect() { ipcRenderer.invoke('nanodevices:disconnect'); },
|
||||||
|
on_device_attached(listener) { ipcRenderer.on('nanodevice-attached', (_event, value) => listener(value)); },
|
||||||
|
on_device_detached(listener) { ipcRenderer.on('nanodevice-detached', (_event, value) => listener(value)); },
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// expose an API to communicate with the nano device
|
||||||
|
contextBridge.exposeInMainWorld('nanodevice', {
|
||||||
|
get_value(pid, tid, vid){ ipcRenderer.invoke('nano:get', pid, tid, vid); },
|
||||||
|
set_value(pid, tid, vid, value){ ipcRenderer.invoke('nano:set', pid, tid, vid, value); },
|
||||||
|
on_value(listener){ ipcRenderer.on('nano-onvalue', (_event, value) => listener(value)); }
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user