-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
191 lines (168 loc) · 6.03 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
/**
* @author carlosperate
* @copyright 2015 carlosperate https://github.com/carlosperate
* @license Licensed under the The MIT License (MIT), a copy can be found in
* the electron project directory LICENSE file.
*
* @fileoverview Electron entry point continues here. Creates windows and
* handles system events.
*/
const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const Menu = electron.Menu;
const fs = require('fs')
const { ipcMain } = require("electron");
const server = require('./electron/servermgr.js');
const projectLocator = require('./electron/projectlocator.js');
const path = require('path')
const jetpack = require('fs-jetpack');
const packageData = require('fs-jetpack').cwd(app.getAppPath()).read('package.json', 'json');
const saveLog = require('./electron/serial_monitor');
const serialPort = require('serialport');
const tag = '[ArdublocklyElec] ';
// Global reference of the window object must be maintain, or the window will
// be closed automatically when the JavaScript object is garbage collected.
var mainWindow = null;
//var logWindow = null;
// Set up the app data directory within the Ardublockly home directory
(function setAppData() {
var HOME = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME']
var ardLib = ''
console.log('HOME ', HOME)
var appDataPath = jetpack.dir(HOME).cwd('.hdlr-appdata')
console.log('appDataPath ', appDataPath.path())
jetpack.dirAsync(HOME + '/Arduino')
.then(jetpack.dirAsync(HOME + '/Arduino/libraries'))
.then(jetpack.copyAsync('/usr/lib/huayra-duino-lr/resources/app/arduino-libraries', HOME + '/Arduino/libraries',
{ overwrite: (srcInspectData, destInspectData) => {
return srcInspectData.modifyTime > destInspectData.modifyTime;
}}))
jetpack.dirAsync(HOME+'/Ejemplos/huayra-duino-lr')
.then(jetpack.copyAsync('/usr/lib/huayra-duino-lr/resources/app/examples', HOME+'/Ejemplos/huayra-duino-lr',
{ overwrite: (srcInspectData, destInspectData) => {
return srcInspectData.modifyTime > destInspectData.modifyTime;
}}))
app.setPath('appData', appDataPath.path());
app.setPath('userData', appDataPath.path());
app.setPath('cache', appDataPath.path('GenCache'));
app.setPath('userCache', appDataPath.path('AppCache'));
app.setPath('temp', appDataPath.path('temp'));
})();
// Ensure this is a single instance application
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit()
} else {
app.on('second-instance', () => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
})
};
function setMainMenu() {
const template = [
{
label: 'Obtener Datos',
submenu: [
{
label: 'Activar Log de Puerto Serie',
click() {
saveLog.startLog();
}
},
{
label: 'Detener Log de Puerto Serie',
click() {
saveLog.stopLog();
}
}
]
}
];
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
}
// Electron application entry point
app.on('ready', function() {
server.startServer();
mainWindow = new BrowserWindow({width: 1000, height: 740,
webPreferences: {
nodeIntegration: false, // is default value after Electron v5
contextIsolation: true, // protect against prototype pollution
enableRemoteModule: false, // turn off remote
preload: path.join(__dirname, "preload.js") // use a preload script
}})
mainWindow.menuBarVisible = false
mainWindow.type = 'desktop'
mainWindow.title = 'HuayraDuino'
mainWindow.minWidth = 530
mainWindow.minHeight = 780
mainWindow.resizable = true
mainWindow.icon = path.join(__dirname, './favicon.png')
//mainWindow.webContents.session.clearCache()
//setMainMenu();
mainWindow.webContents.on('did-fail-load',
function(event, errorCode, errorDescription) {
console.warn(tag + 'Page failed to load (' + errorCode + '). The ' +
'server is probably not yet running. Trying again in 200 ms.');
setTimeout(function() {
mainWindow.webContents.reload();
}, 150);
}
);
mainWindow.webContents.on('did-finish-load', function() {
mainWindow.show();
});
mainWindow.on('close', function() {
mainWindow = null;
});
/*
logWindow = new BrowserWindow({width: 400, height: 500, webPreferences: { nodeIntegration: true } })
logWindow.menuBarVisible = false
logWindow.type = 'desktop'
logWindow.title = 'log Serial Port'
logWindow.minWidth = 330
logWindow.minHeight = 440
logWindow.resizable = true
logWindow.webContents.on('did-finish-load', function() {
logWindow.show();
});
logWindow.on('close', function() {
logWindow = null;
});
logWindow.loadURL('file://' + path.join(__dirname, 'log.html'));
saveLog.startLog(logWindow);
*/
// Set the download directory to the home folder
mainWindow.webContents.session.setDownloadPath(
process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME']);
mainWindow.loadURL('http://localhost:8000/ardublockly');
});
app.on('window-all-closed', function() {
server.stopServer();
saveLog.stopLog();
app.quit();
});
ipcMain.on("toMain", (event, args) => {
//var dataLog = '';
//console.log(args)
if( args == true)
{
saveLog.startLog(mainWindow);
//console.log('start logging')
}
else{
saveLog.stopLog();
//console.log('stop logging')
}
/*
fs.readFile("preload.js", (error, data) => {
// Do something with file contents
// Send result back to renderer process
console.log('pase por el preload')
mainWindow.webContents.send("fromMain", dataLog);
});
*/
});