1
- import { Subject } from 'rxjs' ;
1
+ import { Subject , BehaviorSubject } from 'rxjs' ;
2
2
3
- export default class ReaderWriter {
3
+ export default class Daemon {
4
4
constructor ( ) {
5
5
this . socket = null ;
6
6
this . pluginURL = null ;
7
- this . messageSubject = new Subject ( ) ;
8
- this . serialMonitorSubject = new Subject ( ) ;
9
- this . devicesList = {
7
+ this . messageBus = new Subject ( ) ;
8
+ this . serialMonitor = new Subject ( ) ;
9
+ this . devicesList = new BehaviorSubject ( {
10
10
serial : [ ] ,
11
11
network : [ ]
12
- } ;
13
- this . messageSubject . subscribe ( this . updateDevicesList . bind ( this ) ) ;
12
+ } ) ;
13
+ this . messageBus . subscribe ( this . updateDevicesList . bind ( this ) ) ;
14
14
this . openingSerial = null ;
15
15
this . closingSerial = null ;
16
16
}
17
17
18
- initSocket ( socket ) {
19
- this . socket = socket ;
18
+ initSocket ( ) {
20
19
this . socket . on ( 'message' , this . parseMessage . bind ( this ) ) ;
21
20
}
22
21
23
22
initPluginUrl ( pluginUrl ) {
24
23
this . pluginURL = pluginUrl ;
25
24
}
26
25
26
+ /**
27
+ * Compares 2 devices list checking they contains the same ports in the same order
28
+ * @param {Array<device> } a the first list
29
+ * @param {Array<device> } b the second list
30
+ */
31
+ static devicesListAreEquals ( a , b ) {
32
+ if ( ! a || ! b || a . length !== b . length ) {
33
+ return false ;
34
+ }
35
+ return a . every ( ( item , index ) => b [ index ] . Name === item . Name ) ;
36
+ }
37
+
27
38
updateDevicesList ( devicesInfo ) {
28
39
// Result of a list command
29
40
if ( devicesInfo . Ports ) {
30
- if ( devicesInfo . Network ) {
31
- this . devicesList . network = devicesInfo . Ports ;
41
+ const lastDevices = this . devicesList . getValue ( ) ;
42
+ if ( devicesInfo . Network && ! Daemon . devicesListAreEquals ( lastDevices . network , devicesInfo . Ports ) ) {
43
+ this . devicesList . next ( {
44
+ serial : lastDevices . serial ,
45
+ network : devicesInfo . Ports
46
+ } ) ;
32
47
}
33
- else {
34
- this . devicesList . serial = devicesInfo . Ports ;
48
+ else if ( ! devicesInfo . Network && ! Daemon . devicesListAreEquals ( lastDevices . serial , devicesInfo . Ports ) ) {
49
+ this . devicesList . next ( {
50
+ serial : devicesInfo . Ports ,
51
+ network : lastDevices . network
52
+ } ) ;
35
53
}
36
54
}
37
55
}
38
56
39
57
parseMessage ( message ) {
40
- let jsonMessage ;
41
58
try {
42
- jsonMessage = JSON . parse ( message ) ;
59
+ this . messageBus . next ( JSON . parse ( message ) ) ;
43
60
}
44
61
catch ( SyntaxError ) {
45
- return ;
46
- }
47
-
48
- if ( jsonMessage ) {
49
- this . messageSubject . next ( jsonMessage ) ;
62
+ this . messageBus . next ( message ) ;
50
63
}
51
64
}
52
65
@@ -72,7 +85,7 @@ export default class ReaderWriter {
72
85
return reject ( new Error ( 'Failed to open serial' ) ) ;
73
86
}
74
87
} ;
75
- this . openSubscription = this . messageSubject . subscribe ( checkOpen ) ;
88
+ this . openSubscription = this . messageBus . subscribe ( checkOpen ) ;
76
89
} ) . finally ( ( ) => {
77
90
this . openSubscription . unsubscribe ( ) ;
78
91
this . openingSerial = null ;
@@ -105,7 +118,7 @@ export default class ReaderWriter {
105
118
return reject ( new Error ( 'Failed to close serial' ) ) ;
106
119
}
107
120
} ;
108
- this . closeSubscription = this . messageSubject . subscribe ( checkClosed ) ;
121
+ this . closeSubscription = this . messageBus . subscribe ( checkClosed ) ;
109
122
} ) . finally ( ( ) => {
110
123
this . closeSubscription . unsubscribe ( ) ;
111
124
this . closingSerial = null ;
@@ -117,11 +130,11 @@ export default class ReaderWriter {
117
130
readSerial ( ) {
118
131
const onMessage = message => {
119
132
if ( message . D ) {
120
- this . serialMonitorSubject . next ( message . D ) ;
133
+ this . serialMonitor . next ( message . D ) ;
121
134
}
122
135
} ;
123
136
if ( ! this . readSerialSubscription ) {
124
- this . readSerialSubscription = this . messageSubject . subscribe ( onMessage ) ;
137
+ this . readSerialSubscription = this . messageBus . subscribe ( onMessage ) ;
125
138
}
126
139
}
127
140
}
0 commit comments