-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
244 lines (206 loc) · 6.61 KB
/
main.js
File metadata and controls
244 lines (206 loc) · 6.61 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
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
const { autoUpdater } = require( 'electron-updater' );
const log = require( 'electron-log' );
const { BrowserWindow, app, Notification, dialog } = require( 'electron' );
const { SerialPort } = require( 'serialport' );
const { ReadlineParser } = require( '@serialport/parser-readline' );
const path = require( 'path' );
const url = require( 'url' );
const regex = /(ST|US),GS,\s+([0-9.]+)(lb|kb)/g;
const currentEnvironment = process.env.NODE_ENV;
const isOnline = require( 'is-online' );
const manufacturerList = ['Prolific','FTDI'];
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
log.info( 'App starting...' );
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow, units, status, port, currentWeight, prevWeight, countdown;
let windowOptions = {
center: true,
minWidth: 1000,
minHeight: 768,
width: 1366,
height: 768,
};
let stableTimer = process.env.stableTimer | 1000;
function readLine( line ){
// log.info( 'Line ' + line );
let parsedLine = '0.000';
let stableString;
while((m = regex.exec( line )) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if(m.index === regex.lastIndex) {
regex.lastIndex++;
}
units = m[ 3 ];
parsedLine = m[ 2 ];
stableString = m[ 1 ];
}
// if(stableString !== undefined) {
// status = stableString;
// }
// log.info( "Status ", status );
return parsedLine;
}
function closeWindow( closeMessage = 'you did something wrong.' ){
new Notification({
title: 'Weigh Station Error',
body: closeMessage
}).show()
mainWindow.close();
log.info( 'Window closed because ' + closeMessage );
}
// Check that the PC has internet once the window is open every couple seconds
setInterval( () => isOnline().then( online => {
if(online !== true) {
closeWindow( 'you have lost internet.' );
}
} ), 1000
);
let stableCountdown = setInterval( () => {
if(currentWeight && currentWeight !== "0.000") {
// Weight is still the same so deduct countdown timer to check for stable
if(currentWeight === prevWeight) {
if(countdown <= 0) {
// Stable
if(status !== 'ST')
{
status = 'ST';
log.info( 'STABLE');
}
}
else {
countdown = countdown - 25;
log.info( countdown );
}
}
else {
log.info( "Current Weight: ", currentWeight );
countdown = stableTimer;
prevWeight = currentWeight;
status = 'US';
}
}
}, 10
);
/** Serial Port Stuff **/
async function initSerialPort(){
log.info( "Init" );
status = 'US';
await SerialPort.list().then( ( ports, err ) => {
if(err) {
log.error( err.message );
return;
}
if(ports.length === 0) {
log.info( 'NO PORTS' )
}
const scalePort = ports.find( function( port ){
return manufacturerList.includes(port[ 'manufacturer' ]); // Manufacturer of the port is in the list of allowed
} )
if(!scalePort) {
// closeWindow( "Couldn't find a scale." );
log.error( "Couldn't find a scale." );
return;
}
log.info( 'Found ' + scalePort.path );
port = new SerialPort( { path: scalePort.path, baudRate: 9600 } );
if(port.isOpen) {
log.info( 'port is open.' );
}
port.on( 'open', function(){
parser = port.pipe( new ReadlineParser() );
parser.on( 'data', function( data ){
currentWeight = readLine( data );
let code = `if(document.getElementById("weight") !== null){
document.getElementById("weight").value = "${currentWeight}";
document.getElementById("status").innerHTML = "${status}";
document.getElementById("units").innerHTML = "${units}";
};`;
if(mainWindow.webContents.getURL().includes( 'tools/weighStation' )) {
mainWindow.webContents.executeJavaScript( code );
}
} );
} );
port.on( 'close', function(){
log.info( 'Port has been closed.' );
closeWindow( 'Lost connection to scale.' );
} );
port.on( 'error', function( e ){
log.error( e );
} );
} );
}
/** BrowserWindow setup and such **/
function createWindow(){
// Create the browser window.
mainWindow = new BrowserWindow( windowOptions );
// When in development environment, open the Redux DevTools Extension and the Chrome DevTools.
// Need to have the Chrome Extension at the location below.
// If on MAC and have Redux DevTools installed, then it should be at this location.
if(currentEnvironment === 'DEV') {
BrowserWindow.addDevToolsExtension( "../../Library/Application Support/Google/Chrome/Default/Extensions/lmhkpmbekcpmknklioeibfkpmmfibljd/2.15.1_0/" );
// Open the DevTools.
mainWindow.webContents.openDevTools();
mainWindow.loadURL( 'http://localhost:3000' );
}
else if(currentEnvironment === 'WINDEV') {
// Open the DevTools.
mainWindow.webContents.openDevTools();
mainWindow.loadURL( 'http://localhost:3000' );
}
else {
// and load the index.html of the app.
mainWindow.loadURL( 'https://oms.fulfillment.app/' );
}
// Emitted when the window is closed.
mainWindow.on( 'closed', function(){
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
} );
mainWindow.webContents.on( 'did-finish-load', function(){
initSerialPort();
} );
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on( 'ready', function(){
autoUpdater.checkForUpdates();
createWindow();
} );
autoUpdater.on('update-available', () => {
dialog.showMessageBox({
type: 'info',
title: 'New Update Available',
message: 'A new version of the application is available.',
buttons: ['Download Now']
}, (response) => {
if (response === 0) {
autoUpdater.downloadUpdate().then( r => autoUpdater.quitAndInstall() );
}
});
});
// Quit when all windows are closed.
app.on( 'window-all-closed', function(){
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
app.quit();
} );
app.on( 'activate', function(){
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if(mainWindow === null) {
createWindow();
}
} );
app.on( 'will-quit', function(){
if(port && port.isOpen)
port.close();
} );
app.on( 'quit', function(){
if(port && port.isOpen)
port.close();
} );