@zimo-elektronik/zcan 1.0.57 → 1.0.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,7 @@ import MX10 from '../MX10';
4
4
  import {Buffer} from 'buffer';
5
5
  import {PingResponseExtended} from '../common/models';
6
6
  import { Query } from '../docs_entrypoint';
7
- import { MsgPortClose } from './networkMsg';
7
+ import { MsgPing, MsgPortClose } from './networkMsg';
8
8
  import { MsgMode } from '../common/enums';
9
9
 
10
10
  /**
@@ -13,22 +13,51 @@ import { MsgMode } from '../common/enums';
13
13
  */
14
14
  export default class NetworkGroup
15
15
  {
16
- public readonly onPingResponse = new Subject<PingResponseExtended>();
16
+ public readonly onPing = new Subject<MsgPing>();
17
17
  public readonly onPortClose = new Subject<MsgPortClose>();
18
18
 
19
+ private pingQ: Query<MsgPing> | undefined = undefined;
19
20
  private portCloseQ: Query<MsgPortClose> | undefined = undefined;
20
21
 
21
22
  private mx10: MX10;
22
- pingTimeout: NodeJS.Timeout | null = null;
23
23
 
24
24
  constructor(mx10: MX10)
25
25
  {
26
26
  this.mx10 = mx10;
27
27
  }
28
28
 
29
- ping(mode = 0b10)
29
+ // ping(mode = 0b10)
30
+ // {
31
+ // this.mx10.sendData(0x0a, 0x00, [{value: this.mx10.mx10NID, length: 2}], mode);
32
+ // }
33
+
34
+ async ping(nid = 0xc000)
30
35
  {
31
- this.mx10.sendData(0x0a, 0x00, [{value: this.mx10.mx10NID, length: 2}], mode);
36
+ if(this.pingQ !== undefined && !await this.pingQ.lock()) {
37
+ this.mx10.logInfo.next("mx10.ping: failed to acquire lock");
38
+ return undefined;
39
+ }
40
+ this.pingQ = new Query(MsgPing.header(MsgMode.CMD, nid), this.onPing);
41
+ this.pingQ.log = ((msg) => {
42
+ this.mx10.logInfo.next(msg);
43
+ });
44
+ this.pingQ.tx = ((header) => {
45
+ const msg = new MsgPing(header, nid);
46
+ this.mx10.logInfo.next('ping query tx: ' + JSON.stringify(msg));
47
+ this.mx10.sendMsg(msg, true);
48
+ });
49
+ this.pingQ.match = ((msg) => {
50
+ this.mx10.logInfo.next('ping query rx: ' + JSON.stringify(msg));
51
+ if(nid & 0xff)
52
+ return msg.header.nid === nid;
53
+ return ((msg.header.nid||0) & 0xff00) === (nid & 0xff00);
54
+ });
55
+ this.pingQ.subscribe(false);
56
+ const rv = await this.pingQ.run(100);
57
+ this.mx10.logInfo.next("mx10.ping.rv: " + JSON.stringify(rv));
58
+ this.pingQ.unlock();
59
+ this.pingQ = undefined;
60
+ return rv;
32
61
  }
33
62
 
34
63
  async portClose()
@@ -64,47 +93,59 @@ export default class NetworkGroup
64
93
  {
65
94
  switch (command) {
66
95
  case 0x00:
67
- this.pingResponse(size, mode, nid, buffer);
96
+ this.parsePing(size, mode, nid, buffer);
68
97
  break;
69
98
  }
70
99
  }
71
100
 
72
101
  // 0x0A.0x00
73
- pingResponse(size: number, mode: number, nid: number, _buffer: Buffer)
102
+ parsePing(size: number, mode: number, nid: number, buffer: Buffer)
74
103
  {
75
- if(!this.onPingResponse.observed)
104
+ if(!this.onPing.observed)
76
105
  return;
106
+
107
+ this.mx10.logInfo.next('parsePing from 0x' + nid.toString(16) + ': ' + JSON.stringify(buffer));
108
+
109
+ if(mode < MsgMode.EVT) {
110
+ const nid = buffer.readUInt16LE(0);
111
+ this.onPing.next(new MsgPing(MsgPing.header(mode, nid)));
112
+ } else {
113
+ const masterUid = buffer.readUInt32LE(0);
114
+ const type = buffer.readUInt16LE(4);
115
+ const session = buffer.readUInt16LE(6);
116
+ this.onPing.next(new MsgPing(MsgPing.header(mode, nid), masterUid, type, session));
117
+ }
77
118
 
78
- if (size === 8) {
79
- // TODO IMPLEMENT DETAIL READOUT
119
+ // if (size === 8) {
120
+ // // TODO IMPLEMENT DETAIL READOUT
80
121
 
81
- if (!this.mx10.mx10NID) {
82
- this.mx10.mx10NID = nid;
83
- }
84
- this.mx10.connected = true;
122
+ // if (!this.mx10.mx10NID) {
123
+ // this.mx10.mx10NID = nid;
124
+ // }
125
+ // this.mx10.connected = true;
85
126
 
86
- // TODO reconnect when uuid has changed
87
- // const uuid = buffer.readUInt32LE(0);
127
+ // // TODO reconnect when uuid has changed
128
+ // // const uuid = buffer.readUInt32LE(0);
88
129
 
89
- // this.mx10.reconnectLogic();
130
+ // // this.mx10.reconnectLogic();
90
131
 
91
- if (this.pingTimeout)
92
- clearTimeout(this.pingTimeout);
132
+ // if (this.pingTimeout)
133
+ // clearTimeout(this.pingTimeout);
93
134
 
94
- this.pingTimeout = setTimeout(() => {
95
- this.mx10.connected = false;
96
- this.onPingResponse.next({
97
- connected: this.mx10.connected,
98
- });
99
- this.mx10.logInfo.next('No ping for 2 seconds, disconnected');
100
- }, 2000);
101
- } else {
102
- throw new Error(
103
- 'LENGTH ERROR: readCmdGrp_0x0A-0x0A, read length as: ' +
104
- size.toString(),
105
- );
106
- }
135
+ // this.pingTimeout = setTimeout(() => {
136
+ // this.mx10.connected = false;
137
+ // this.onPingResponse.next({
138
+ // connected: this.mx10.connected,
139
+ // });
140
+ // this.mx10.logInfo.next('No ping for 2 seconds, disconnected');
141
+ // }, 2000);
142
+ // } else {
143
+ // throw new Error(
144
+ // 'LENGTH ERROR: readCmdGrp_0x0A-0x0A, read length as: ' +
145
+ // size.toString(),
146
+ // );
147
+ // }
107
148
 
108
- this.onPingResponse.next({connected: this.mx10.connected});
149
+ // this.onPingResponse.next({connected: this.mx10.connected});
109
150
  }
110
151
  }
@@ -31,4 +31,25 @@ export class MsgPortClose extends Message
31
31
  super(header);
32
32
  super.push({value: myNid, length: 2});
33
33
  }
34
+ }
35
+
36
+ export class MsgPing extends Message
37
+ {
38
+ public static header(mode: MsgMode, nid: number): Header
39
+ {return {group: 0xa, cmd: 0x0, mode: mode, nid: nid}}
40
+
41
+ constructor(header: Header, masterUid?: number, type?: number, session?: number)
42
+ {
43
+ super(header);
44
+ if(header.mode < MsgMode.EVT)
45
+ return;
46
+ super.push({value: masterUid || 0, length: 4});
47
+ super.push({value: type || 0, length: 2});
48
+ super.push({value: session || 0, length: 2});
49
+ }
50
+
51
+ nid() {return this.header.nid as number;}
52
+ masterUid() {return this.data[0].value as number;}
53
+ type() {return this.data[0].value as number;}
54
+ session() {return this.data[0].value as number;}
34
55
  }