Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
90 lines (77 loc) · 2.54 KB
/
Copy pathmain.js
File metadata and controls
90 lines (77 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const { app, ipcMain, BrowserWindow } = require('electron');
const path = require('path');
const noble = require('noble-winrt');
const WIN_CONSTANTS = {
WIDTH: 1366,
HEIGHT: 728
}
let APP_WINDOW;
const SPEAKER_CONTENTS = {
PRIMARY_SERVICE_UUID: "757ed3e4-1828-4a0c-8362-c229c3a6da72",
POWER_ON_UUID: "c6d6dc0d-07f5-47ef-9b59-630622b01fd3",
MAC_ADDR: "88:C6:26:8C:DA:34",
ID: "88c6268cda34",
POWER_ON_CHARACTERISTIC: null
}
function _INIT_WINDOW() {
APP_WINDOW = new BrowserWindow({
width: WIN_CONSTANTS.WIDTH,
height: WIN_CONSTANTS.HEIGHT,
webPreferences: {
nodeIntegration: true,
preload: path.join(__dirname, './preload.js'),
enableRemoteModule: true,
nodeIntegrationInWorker: true
},
autoHideMenuBar: true
});
APP_WINDOW.loadFile(path.join(__dirname, './dist/index.html'));
APP_WINDOW.maximize();
}
app.whenReady().then(() => {
_INIT_WINDOW();
app.on('activate', () => {
if(BrowserWindow.getAllWindows().length === 0) {
_INIT_WINDOW();
}
});
});
app.on('window-all-closed', () => {
if(!process.platform !== "darwin") {
app.quit();
}
});
function _INIT_SCAN_FOR_SPEAKER() {
noble.on('stateChange', (state) => {
if(state === 'poweredOn') {
noble.startScanning([SPEAKER_CONTENTS.PRIMARY_SERVICE_UUID]);
}
});
noble.on('discover', (peripheral) => {
if(peripheral.id === SPEAKER_CONTENTS.ID) {
function storePowerOnCharacteristic(powerOnChar) {
SPEAKER_CONTENTS.POWER_ON_CHARACTERISTIC = powerOnChar;
}
peripheral.connect(error => {
peripheral.discoverSomeServicesAndCharacteristics(
SPEAKER_CONTENTS.PRIMARY_SERVICE_UUID,
SPEAKER_CONTENTS.POWER_ON_UUID,
(error, services, chars) => {
APP_WINDOW.webContents.send('stored-power-characteristic');
storePowerOnCharacteristic(chars[0]);
noble.stopScanning();
});
});
}
});
}
_INIT_SCAN_FOR_SPEAKER();
function _TURN_ON_SPEAKER() {
const powerOnBuffer = Buffer.from([0xEC, 0x5C, 0x68, 0x4A, 0xAD, 0x5C, 0x01]);
SPEAKER_CONTENTS.POWER_ON_CHARACTERISTIC.write(powerOnBuffer, true, error => {});
noble.startScanning();
}
ipcMain.on('turn-on-speaker', (event, arg) => {
_TURN_ON_SPEAKER();
APP_WINDOW.webContents.send('toggle-speaker-off');
});