|
1 | 1 | const { autoUpdater } = require( 'electron-updater' ); |
2 | 2 | const log = require( 'electron-log' ); |
3 | 3 |
|
4 | | -const { BrowserWindow, app } = require( 'electron' ); |
| 4 | +const { BrowserWindow, app, Notification } = require( 'electron' ); |
5 | 5 | const { SerialPort } = require( 'serialport' ); |
6 | 6 | const { ReadlineParser } = require( '@serialport/parser-readline' ); |
7 | 7 | const path = require( 'path' ); |
8 | 8 | const url = require( 'url' ); |
9 | 9 | const regex = /(ST|US),GS,\s+([0-9.]+)(lb|kb)/g; |
10 | 10 | const currentEnvironment = process.env.NODE_ENV; |
11 | 11 | const isOnline = require( 'is-online' ); |
| 12 | +const manufacturerList = ['Prolific','FTDI']; |
12 | 13 |
|
13 | 14 | autoUpdater.logger = log; |
14 | 15 | autoUpdater.logger.transports.file.level = 'info'; |
@@ -53,6 +54,10 @@ function readLine( line ){ |
53 | 54 | } |
54 | 55 |
|
55 | 56 | function closeWindow( closeMessage = 'you did something wrong.' ){ |
| 57 | + new Notification({ |
| 58 | + title: 'Weigh Station Error', |
| 59 | + body: closeMessage |
| 60 | + }).show() |
56 | 61 | mainWindow.close(); |
57 | 62 | log.info( 'Window closed because ' + closeMessage ); |
58 | 63 | } |
@@ -109,11 +114,12 @@ async function initSerialPort(){ |
109 | 114 | } |
110 | 115 |
|
111 | 116 | const scalePort = ports.find( function( port ){ |
112 | | - return port[ 'manufacturer' ] === 'Prolific'; |
| 117 | + return manufacturerList.includes(port[ 'manufacturer' ]); // Manufacturer of the port is in the list of allowed |
113 | 118 | } ) |
114 | 119 |
|
115 | 120 | if(!scalePort) { |
116 | | - log.error( "Couldn't find scale port" ); |
| 121 | + closeWindow( "Couldn't find a scale." ); |
| 122 | + log.error( "Couldn't find a scale." ); |
117 | 123 | return; |
118 | 124 | } |
119 | 125 |
|
@@ -144,7 +150,7 @@ async function initSerialPort(){ |
144 | 150 |
|
145 | 151 | port.on( 'close', function(){ |
146 | 152 | log.info( 'Port has been closed.' ); |
147 | | - closeWindow( 'Port has been closed.' ); |
| 153 | + closeWindow( 'Lost connection to scale.' ); |
148 | 154 | } ); |
149 | 155 |
|
150 | 156 | port.on( 'error', function( e ){ |
@@ -214,11 +220,11 @@ app.on( 'activate', function(){ |
214 | 220 | } ); |
215 | 221 |
|
216 | 222 | app.on( 'will-quit', function(){ |
217 | | - if(port.isOpen) |
| 223 | + if(port && port.isOpen) |
218 | 224 | port.close(); |
219 | 225 | } ); |
220 | 226 |
|
221 | 227 | app.on( 'quit', function(){ |
222 | | - if(port.isOpen) |
| 228 | + if(port && port.isOpen) |
223 | 229 | port.close(); |
224 | 230 | } ); |
0 commit comments