@@ -13,9 +13,17 @@ export default class Daemon {
13
13
network : [ ]
14
14
} ) ;
15
15
this . socketMessages
16
- . subscribe ( this . handleSocketMesage . bind ( this ) ) ;
16
+ . subscribe ( this . handleSocketMessage . bind ( this ) ) ;
17
17
this . openingSerial = null ;
18
18
this . closingSerial = null ;
19
+
20
+ const devicesListSubscription = this . devicesList . subscribe ( ( devices ) => {
21
+ if ( devices . serial && devices . serial . length > 0 ) {
22
+ this . closeAllPorts ( ) ;
23
+ devicesListSubscription . unsubscribe ( ) ;
24
+ }
25
+ } ) ;
26
+ window . addEventListener ( 'beforeunload' , this . closeAllPorts ) ;
19
27
}
20
28
21
29
initSocket ( ) {
@@ -38,24 +46,24 @@ export default class Daemon {
38
46
* @param {Array<device> } a the first list
39
47
* @param {Array<device> } b the second list
40
48
*/
41
- static devicesListAreEquals ( a , b ) {
49
+ devicesListAreEquals ( a , b ) {
42
50
if ( ! a || ! b || a . length !== b . length ) {
43
51
return false ;
44
52
}
45
- return a . every ( ( item , index ) => b [ index ] . Name === item . Name ) ;
53
+ return a . every ( ( item , index ) => ( b [ index ] . Name === item . Name && b [ index ] . IsOpen === item . IsOpen ) ) ;
46
54
}
47
55
48
- handleSocketMesage ( message ) {
56
+ handleSocketMessage ( message ) {
49
57
// Result of a list command
50
58
if ( message . Ports ) {
51
59
const lastDevices = this . devicesList . getValue ( ) ;
52
- if ( message . Network && ! Daemon . devicesListAreEquals ( lastDevices . network , message . Ports ) ) {
60
+ if ( message . Network && ! this . devicesListAreEquals ( lastDevices . network , message . Ports ) ) {
53
61
this . devicesList . next ( {
54
62
serial : lastDevices . serial ,
55
63
network : message . Ports
56
64
} ) ;
57
65
}
58
- else if ( ! message . Network && ! Daemon . devicesListAreEquals ( lastDevices . serial , message . Ports ) ) {
66
+ else if ( ! message . Network && ! this . devicesListAreEquals ( lastDevices . serial , message . Ports ) ) {
59
67
this . devicesList . next ( {
60
68
serial : message . Ports ,
61
69
network : lastDevices . network
@@ -68,6 +76,10 @@ export default class Daemon {
68
76
}
69
77
}
70
78
79
+ writeSerial ( port , data ) {
80
+ this . socket . emit ( 'command' , `send ${ port } ${ data } ` ) ;
81
+ }
82
+
71
83
openSerialMonitor ( port ) {
72
84
if ( this . serialMonitorOpened . getValue ( ) ) {
73
85
return ;
@@ -109,4 +121,15 @@ export default class Daemon {
109
121
} ) ;
110
122
this . socket . emit ( 'command' , `close ${ port } ` ) ;
111
123
}
124
+
125
+ closeAllPorts ( e ) {
126
+ if ( e ) {
127
+ e . preventDefault ( ) ;
128
+ }
129
+ const devices = this . devicesList . getValue ( ) . serial ;
130
+ devices . forEach ( device => {
131
+ this . socket . emit ( 'command' , `close ${ device . Name } ` ) ;
132
+ } ) ;
133
+ return ;
134
+ }
112
135
}
0 commit comments