This repository has been archived by the owner on Jul 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
330 lines (290 loc) · 11.3 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
const osc = require('osc')
const oUDPport = 0;
const oServerIP = "";
const lib = require('./mainFunctions')
const { EmberClient } = require('./emberLib')
const electron = require('electron')
const ipcMain = require('electron').ipcMain
const nativeTheme = electron.nativeTheme;
nativeTheme.themeSource = 'dark';
const { app, BrowserWindow } = require('electron');
const mainFunctions = require('./mainFunctions');
const { dialog } = require('electron')
const fs = require('fs');
const { openFile, relaunch } = require('./mainFunctions');
var recDir = app.getPath('documents')+'/MCxOSC';
if (!fs.existsSync(recDir)) {
fs.mkdirSync(recDir)
}
var openDir = app.getPath('documents')+'/MCxOSC';
const recOptions = {
filters :[
{name: 'Session file', extensions: ['session']},
{name: 'All Files', extensions: ['*']}
],
title: "Save your session in a *.session file",
defaultPath: recDir + '/MySession.session',
}
const openOptions = {
filters :[
{name: 'Session file', extensions: ['session']},
{name: 'All Files', extensions: ['*']}
],
properties: ['openFile', 'multiSelections'],
title: "Choose a *.session file",
defaultPath: openDir,
}
const appVersion = app.getVersion()
function createWindow() {
let win = new BrowserWindow({
width: 1000,
height: 900,
webPreferences: {
nodeIntegration: true
},
icon: `${__dirname}/assets/icons/64x64.png`
})
win.loadFile('index.html')
//---Menu interactions Section---//
ipcMain.on('sendSaveAs', function (event, content) {
//console.log(content);
filename = dialog.showSaveDialog(null, recOptions, {}
).then(result => {
filename = result.filePath;
if (filename === undefined) {
console.log('the user clicked the btn but didn\'t created a file');
}
fs.writeFile(filename, content, (err) => {
if (err) {
console.log('an error ocurred with file creation ' + err.message);
}
console.log('WE CREATED YOUR FILE SUCCESFULLY');
win.webContents.send('sendFilename', filename);
})
})
})
ipcMain.on('sendSave', function (event, content, rSfilename) {
//console.log("sendsave filepath", rSfilename);
//console.log("sendsave content", content);
if (rSfilename === undefined) {
console.log('the user clicked the btn but didn\'t created a file');
}
fs.writeFile(rSfilename, content, (err) => {
if (err) {
console.log('an error ocurred with file creation ' + err.message);
}
console.log('WE CREATED YOUR FILE SUCCESFULLY');
//win.webContents.send('sendFilename', filename);
})
})
ipcMain.on('openFile', function (event) {
filename = dialog.showOpenDialog(null, openOptions, {})
.then(result => {
filename = result.filePaths;
//console.log(filename);
const file = filename[0];
//console.log(file);
const content = fs.readFileSync(file, 'utf-8');
//console.log(content);
var sendedContent = JSON.stringify(content);
//console.log('sendedContent:', sendedContent);
win.webContents.send("sendFileContent", sendedContent)
win.webContents.send('sendFilename', file)
})
})
//---End of Menu Interactions Section---//
/////////////////////////////////////////
//---Network Settings Section---//
//Setting local OSC UDP Port
ipcMain.on('sendUDPport', function (event, oUDPport) {
console.log('Port de reception OSC:', oUDPport);
oscCli = new osc.UDPPort({
localAddress: "0.0.0.0",
localPort: Number(oUDPport),
metadata: true
})
oscCli.open()
win.webContents.send('udpportOK');
})
//Setting Remote Ember+ provider IP and Port
ipcMain.on('sendEmberServerIP', function (event, arg) {
eServerIP = arg;
ipcMain.on('sendEmberServerPort', function (event, arg) {
eserverPort = arg;
//Initiating connection to Remote Ember+ provider
c = new EmberClient(eServerIP, eserverPort);
// c.on('error', (e) => {
// console.log("Connection to Ember+ server error/recheck IP and Port!");
//
// win.webContents.send('eServConnError');
//
// app.relaunch()
// app.exit(0)
// })
c.on('connected', (e) => {
console.log("Ember+ Server ",eServerIP, ":", eserverPort, " connection ok");
win.webContents.send('eServerOK');
})
// c.on('disconnected', (e) => {
// console.log("Disconnected from Ember+ Server");
// })
process.on('uncaughtException', function (err) {
console.log(err);
win.webContents.send('eServConnError');
});
//
async function main() {
//await c.connect()
const err = await c.connect()
if (err) { // err = true when the first connection attempt fails (depending on timeout)
console.log('Initial connection unsuccessful', err);
//win.webContents.send('eServConnError');
// app.relaunch()
// app.exit(0)
return
}
//console.log("connection ok");
await (await c.getDirectory(c.tree)).response
//Setting Remote OSC Server IP and Port
ipcMain.on('sendOSCserverIP', function (event, oServerIP) {
console.log('IP du server OSC distant:', oServerIP);
ipcMain.on('sendOSCserverPort', function (event, oServerPort) {
console.log('Port du server OSC distant:', oServerPort);
win.webContents.send('oServerOK');
//---End of Network Settings Section---//
////////////////////////////////////////
//---Connections Section---//
//Creating Ember+ subscription when connection "Go!" sends from Front-End
ipcMain.on('newConnection', async function (event, ePath, oAddr, myRow, eVarType, sFactor, eMin, eMax, oMin, oMax, eVarCurve) {
//console.log("sFactor", sFactor);
sFactor = Number(sFactor);
const req = await c.getElementByPath(ePath)
c.subscribe(req, () => {
var emberValue = req.contents.value;
//console.log("emberValue", emberValue);
event.sender.send('sendEmberValue', emberValue, myRow, 1);
var stringEpath = JSON.stringify(ePath);
//console.log("ePath", ePath);
console.log("suscribe to ", ePath);
//Sending received values from Ember+ to OSC
if (eVarType == "Integer" && eVarCurve == "lin") {
const value = mainFunctions.mapToScale(Number(emberValue), [Number(eMin), Number(eMax)], [Number(oMin), Number(oMax)], 2)
oscCli.send({
address: oAddr,
args: [
{
type: "f",
value: Number(value),
}
]
}, oServerIP, oServerPort);
console.log('EMBER+ -lin-> OSC : ', value)
} else if (eVarType == "Integer" && eVarCurve == "log") {
const value = mainFunctions.mapToScale(Number(emberValue), [Number(eMin), Number(eMax)], [Number(oMin), Number(oMax)], 2, true)
oscCli.send({
address: oAddr,
args: [
{
type: "f",
value: Number(value),
}
]
}, oServerIP, oServerPort);
console.log('EMBER+ -log-> OSC : ', value)
}
else if (eVarType == "String") {
oscCli.send({
address: oAddr,
args: [
{
type: "s",
value: emberValue.toString(),
}
]
}, oServerIP, oServerPort);
console.log('EMBER+ -string-> OSC : ', emberValue)
}
else if (eVarType == "Boolean" && emberValue == true) {
oscCli.send({
address: oAddr,
args: [
{
type: "f",
value: 1,
}
]
}, oServerIP, oServerPort);
console.log('EMBER+ -bool-> OSC : ', emberValue)
}
else if (eVarType == "Boolean" && emberValue == false) {
oscCli.send({
address: oAddr,
args: [
{
type: "f",
value: 0,
}
]
}, oServerIP, oServerPort);
console.log('EMBER+ -bool-> OSC : ', emberValue)
}
})
//Sending received values from OSC to Ember+
oscCli.on("message", function (oscBundle) {
console.log('oscBundle : ', oscBundle);
const oRaddr = JSON.stringify(oscBundle.address);
console.log("Adresse osc reçue", oRaddr);
const oRargs = mainFunctions.oscToEmber(oscBundle);
win.webContents.send('oReceivedAddr', oRaddr, oRargs);
})
})
ipcMain.on('reSendOrArgs', async function (event, rOrArgs, rEaddr, sFactor, eVarType, oMin, oMax, eMin, eMax, eVarCurve) {
const rereq = await c.getElementByPath(rEaddr);
if (eVarType == "Integer" && eVarCurve == "lin") {
const value = mainFunctions.mapToScale(Number(rOrArgs), [Number(eMin), Number(eMax)], [Number(oMin), Number(oMax)], 2)
c.setValue((rereq), value.toFixed(0));
console.log('OSC -lin-> EMBER+ : ', value.toFixed(0));
} else if (eVarType == "Integer" && eVarCurve == "log") {
const value = mainFunctions.mapToScale(Number(rOrArgs), [Number(eMin), Number(eMax)], [Number(oMin), Number(oMax)], 2, true, -1)
c.setValue((rereq), value.toFixed(0));
console.log('OSC -log-> EMBER+ : ', value.toFixed(0));
} else if(eVarType == "Boolean" && rOrArgs == "1"){
c.setValue((rereq), true);
console.log(("OSC -bool-> EMBER+", rOrArgs));
} else if(eVarType == "Boolean" && rOrArgs == "0"){
c.setValue((rereq), false);
console.log(("OSC -bool-> EMBER+", rOrArgs));
} else{
c.setValue((rereq), rOrArgs);
console.log(("OSC -string-> EMBER+", rOrArgs));
}
})
ipcMain.on("deleteConnection", async function (event, ePath, oAddr, myRow, eVarType, sFactor) {
const req = await c.getElementByPath(ePath)
c.unsubscribe(req)
console.log('unsuscribe to ', ePath);
})
})
})
}
main()
})
})
win.autoHideMenuBar = "true"
win.menuBarVisible = "false"
win.webContents.on('did-finish-load', () => {
console.log("appVersion :", appVersion);
win.webContents.send('appVersion', app.getVersion());
})
}
app.whenReady().then(createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (win === null) {
createWindow()
}
})