-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
113 lines (103 loc) · 2.87 KB
/
main.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const { app, BrowserWindow, ipcMain } = require("electron/main");
const path = require("node:path");
const {
connect,
getLaunchPoints,
getInputList,
powerOff,
sendKeyboardKey,
} = require("./lg.js");
const {
handleKey,
handleAppLaunch,
handleInputChange,
handleScroll,
handleMouse,
handleClick,
} = require("./controllers/controller.js");
let win,
lgTv,
foregroundApp,
audioStatus = {
volume: 0,
mute: false,
},
subscribedToEvents = false;
const createWindow = () => {
win = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, "preload.js"),
},
width: 1200,
height: 800,
});
// win.loadURL("http://localhost:5173");
win.loadFile("./new-view/dist/index.html");
};
app.whenReady().then(() => {
const subscribeToEvents = () => {
if (subscribedToEvents) return;
subscribedToEvents = true;
lgTv.subscribe(
"ssap://com.webos.applicationManager/getForegroundAppInfo",
(_err, res) => {
foregroundApp = res;
win.webContents.send("get-foreground-app-id", res.appId);
}
);
lgTv.subscribe("ssap://audio/getStatus", (_err, res) => {
audioStatus = res;
win.webContents.send("get-audio-status", res);
});
};
const sendInfo = async () => {
const launchPoints = await getLaunchPoints();
const inputList = await getInputList();
win.webContents.send("get-launch-points", launchPoints);
win.webContents.send("get-input-list", inputList);
win.webContents.send("get-foreground-app-id", foregroundApp?.appId);
win.webContents.send("get-audio-status", audioStatus);
setTimeout(subscribeToEvents, 100);
};
ipcMain.handle("connectToTv", async (_event, ip) => {
try {
lgTv = await connect(ip);
win.webContents.send("connection-success", true);
} catch (error) {
return error;
}
});
ipcMain.on("handleAction", (_event, action) => {
if (action.type == "key") {
handleKey(action.payload.key);
} else if (action.type == "scroll") {
handleScroll(action.payload.deltaY);
} else if (action.type == "move") {
handleMouse(action.payload.x, action.payload.y);
} else if (action.type == "click") {
handleClick();
} else if (action.type == "launchPoint") {
handleAppLaunch(action.payload.launchPoint);
} else if (action.type == "input") {
handleInputChange(action.payload.input);
} else if (action.type == "fetchData") {
sendInfo();
} else if (action.type == "powerOff") {
powerOff();
} else if (action.type == "keyboard-key") {
sendKeyboardKey(action.payload.key);
}
});
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
if (lgTv) lgTv.disconnect();
if (process.platform !== "darwin") {
app.quit();
}
});