@zimo-elektronik/zcan 1.0.31 → 1.0.32
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.
- package/dist/@types/communication.d.ts +5 -3
- package/dist/@types/communication.js +22 -11
- package/dist/@types/communication.js.map +1 -1
- package/dist/@types/models.d.ts +1 -1
- package/dist/@types/models.js +1 -2
- package/dist/@types/models.js.map +1 -1
- package/dist/zcan/infoGroup.d.ts +1 -1
- package/dist/zcan/infoGroup.js +10 -8
- package/dist/zcan/infoGroup.js.map +1 -1
- package/package.json +1 -1
- package/src/@types/communication.ts +33 -20
- package/src/@types/models.ts +2 -2
- package/src/zcan/infoGroup.ts +13 -8
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BindOptions, RemoteInfo, SocketOptions } from 'dgram';
|
|
2
2
|
import { Buffer } from 'buffer';
|
|
3
|
-
import { MsgMode } from '
|
|
3
|
+
import { MsgMode } from '../util/enums';
|
|
4
4
|
import { Subject, Subscription } from 'rxjs';
|
|
5
5
|
export type Header = {
|
|
6
6
|
group: number;
|
|
@@ -27,10 +27,12 @@ export declare class Query<T extends Message> {
|
|
|
27
27
|
private result;
|
|
28
28
|
match: (msg: T) => boolean;
|
|
29
29
|
private mutex;
|
|
30
|
+
log: (msg: string) => void;
|
|
30
31
|
constructor(header: Header, subject: Subject<T>, match?: (msg: T) => boolean);
|
|
31
|
-
lock(millis?: number): boolean
|
|
32
|
+
lock(millis?: number): Promise<boolean>;
|
|
32
33
|
unlock(): void;
|
|
33
|
-
|
|
34
|
+
subscribe(): void;
|
|
35
|
+
run(retries?: number): Promise<T | undefined>;
|
|
34
36
|
}
|
|
35
37
|
export type NIDGenerator = () => Promise<number>;
|
|
36
38
|
export interface Socket {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MsgMode } from '../util/enums';
|
|
2
|
+
import { delay } from '../internal/utils';
|
|
2
3
|
export class Message {
|
|
3
4
|
header;
|
|
4
5
|
data;
|
|
@@ -18,15 +19,16 @@ export class Query {
|
|
|
18
19
|
result = undefined;
|
|
19
20
|
match = () => { return true; };
|
|
20
21
|
mutex = false;
|
|
22
|
+
log = () => { };
|
|
21
23
|
constructor(header, subject, match = (() => { return true; })) {
|
|
22
24
|
this.header = header;
|
|
23
25
|
this.subject = subject;
|
|
24
26
|
this.match = match;
|
|
25
27
|
}
|
|
26
|
-
lock(millis = 500) {
|
|
28
|
+
async lock(millis = 500) {
|
|
27
29
|
let centis = Math.abs(millis) / 10;
|
|
28
|
-
while (this.mutex) {
|
|
29
|
-
delay(10);
|
|
30
|
+
while (this.mutex && centis) {
|
|
31
|
+
await delay(10);
|
|
30
32
|
if (!centis--)
|
|
31
33
|
return false;
|
|
32
34
|
}
|
|
@@ -36,26 +38,35 @@ export class Query {
|
|
|
36
38
|
unlock() {
|
|
37
39
|
this.mutex = false;
|
|
38
40
|
}
|
|
39
|
-
|
|
40
|
-
if (this.tx === undefined)
|
|
41
|
-
return undefined;
|
|
41
|
+
subscribe() {
|
|
42
42
|
this.rx = this.subject.subscribe((msg) => {
|
|
43
|
-
if (
|
|
43
|
+
if (msg.header.mode !== MsgMode.ACK)
|
|
44
44
|
return;
|
|
45
45
|
if (msg.header.nid !== this.header.nid)
|
|
46
46
|
return;
|
|
47
|
-
this.
|
|
47
|
+
this.log('query.run.rx: ' + JSON.stringify(msg));
|
|
48
|
+
if (!this.match(msg))
|
|
49
|
+
return;
|
|
50
|
+
this.result = msg;
|
|
48
51
|
this.rx?.unsubscribe();
|
|
49
52
|
});
|
|
53
|
+
}
|
|
54
|
+
async run(retries = 5) {
|
|
55
|
+
if (this.tx === undefined)
|
|
56
|
+
return undefined;
|
|
57
|
+
if (this.rx === undefined)
|
|
58
|
+
this.subscribe();
|
|
50
59
|
let tick = 2 * Math.abs(retries);
|
|
51
60
|
while (this.result === undefined) {
|
|
52
|
-
if (tick % 2
|
|
61
|
+
if (tick % 2)
|
|
62
|
+
await delay(5);
|
|
63
|
+
else
|
|
53
64
|
this.tx(this.header);
|
|
54
65
|
if (!tick--) {
|
|
66
|
+
this.log('query.run.failed :(');
|
|
55
67
|
this.rx?.unsubscribe();
|
|
56
68
|
return undefined;
|
|
57
69
|
}
|
|
58
|
-
delay(5);
|
|
59
70
|
}
|
|
60
71
|
return this.result;
|
|
61
72
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"communication.js","sourceRoot":"","sources":["../../src/@types/communication.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"communication.js","sourceRoot":"","sources":["../../src/@types/communication.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,OAAO,EAAC,MAAM,eAAe,CAAC;AACtC,OAAO,EAAC,KAAK,EAAC,MAAM,mBAAmB,CAAC;AAiBxC,MAAM,OAAO,OAAO;IAElB,MAAM,CAAS;IACf,IAAI,CAAgB;IAEpB,YAAY,MAAc,EAAE,OAAsB,EAAE;QAElD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,IAAI,CAAC,IAAc;QAEjB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACvB,CAAC;CACF;AAED,MAAM,OAAO,KAAK;IAEhB,MAAM,CAAS;IACf,OAAO,CAAa;IACpB,EAAE,GAA6B,SAAS,CAAC;IACzC,EAAE,GAA6B,GAAG,EAAE,GAAE,CAAC,CAAC;IAChC,MAAM,GAAkB,SAAS,CAAC;IAC1C,KAAK,GAAwB,GAAG,EAAE,GAAE,OAAO,IAAI,CAAA,CAAA,CAAC,CAAC;IACzC,KAAK,GAAY,KAAK,CAAC;IAC/B,GAAG,GAA0B,GAAG,EAAE,GAAE,CAAC,CAAC;IAEtC,YAAY,MAAc,EAAE,OAAmB,EAAG,QAA6B,CAAC,GAAG,EAAE,GAAE,OAAO,IAAI,CAAC,CAAA,CAAC,CAAC;QAEnG,IAAI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AAI,CAAC,EAAE,KAAK,SAAS;YACtB,IAAI,CAAC,SAAS,EAAE,CAAC;QAEnB,IAAI,IAAI,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAEjC,OAAM,IAAI,CAAC,MAAM,KAAK,SAAS,EAC/B,CAAC;YACC,IAAG,IAAI,GAAG,CAAC;gBACT,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC;;gBAEf,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAEvB,IAAG,CAAC,IAAI,EAAE,EAAE,CAAC;gBACX,IAAI,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAC;gBAChC,IAAI,CAAC,EAAE,EAAE,WAAW,EAAE,CAAC;gBACvB,OAAO,SAAS,CAAC;YACnB,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF"}
|
package/dist/@types/models.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NameType, Direction, ExternalController, FunctionMode, OperatingMode, SystemStateMode, TrackMode, ImageType, FxConfigType, SpecialFunctionMode, Manual, DirectionDefault, ShuntingFunction, BidiType, ForwardOrReverse, AccessoryMode, FxModeType, ModInfoType, MsgMode } from '
|
|
1
|
+
import { NameType, Direction, ExternalController, FunctionMode, OperatingMode, SystemStateMode, TrackMode, ImageType, FxConfigType, SpecialFunctionMode, Manual, DirectionDefault, ShuntingFunction, BidiType, ForwardOrReverse, AccessoryMode, FxModeType, ModInfoType, MsgMode } from '../util/enums';
|
|
2
2
|
import { Header, Message, ZcanDataArray } from './communication';
|
|
3
3
|
import { Subject } from 'rxjs';
|
|
4
4
|
export interface Train {
|
package/dist/@types/models.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { MsgMode, } from 'src/util/enums';
|
|
2
1
|
import { Message } from './communication';
|
|
3
2
|
import { Subject } from 'rxjs';
|
|
4
3
|
export class ModInfoData extends Message {
|
|
5
4
|
static rx = new Subject();
|
|
6
5
|
static header(mode, nid) {
|
|
7
|
-
return { group: 0x08, cmd: 0x08, mode:
|
|
6
|
+
return { group: 0x08, cmd: 0x08, mode: mode, nid };
|
|
8
7
|
}
|
|
9
8
|
constructor(header, type, data = []) {
|
|
10
9
|
super(header);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"models.js","sourceRoot":"","sources":["../../src/@types/models.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"models.js","sourceRoot":"","sources":["../../src/@types/models.ts"],"names":[],"mappings":"AAqBA,OAAO,EAAS,OAAO,EAAgB,MAAM,iBAAiB,CAAC;AAC/D,OAAO,EAAS,OAAO,EAAgB,MAAM,MAAM,CAAC;AAmHpD,MAAM,OAAO,WAAY,SAAQ,OAAO;IAE/B,MAAM,CAAU,EAAE,GAAG,IAAI,OAAO,EAAe,CAAC;IAEhD,MAAM,CAAC,MAAM,CAAC,IAAa,EAAE,GAAW;QAE7C,OAAO,EAAC,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAC,CAAC;IACnD,CAAC;IAED,YAAY,MAAc,EAAE,IAAiB,EAAE,OAAsB,EAAE;QAErE,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,KAAK,CAAC,IAAI,CAAC,EAAC,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,EAAC,CAAC,CAAC;QACrC,IAAG,IAAI,CAAC,MAAM;YACZ,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC3B,CAAC;IAED,IAAI;QAEF,OAAS,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAiC,CAAC;IAO1D,CAAC"}
|
package/dist/zcan/infoGroup.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export default class InfoGroup {
|
|
|
9
9
|
readonly onModuleInfoChange: Subject<ModInfoData>;
|
|
10
10
|
private modInfoQ;
|
|
11
11
|
constructor(mx10: MX10);
|
|
12
|
-
getModuleInfo(nid: number, type: ModInfoType | number): ModInfoData | undefined
|
|
12
|
+
getModuleInfo(nid: number, type: ModInfoType | number): Promise<ModInfoData | undefined>;
|
|
13
13
|
parse(size: number, command: number, mode: number, nid: number, buffer: Buffer): void;
|
|
14
14
|
private parseModuleInfo;
|
|
15
15
|
private parseBidiInfo;
|
package/dist/zcan/infoGroup.js
CHANGED
|
@@ -10,13 +10,15 @@ export default class InfoGroup {
|
|
|
10
10
|
constructor(mx10) {
|
|
11
11
|
this.mx10 = mx10;
|
|
12
12
|
}
|
|
13
|
-
getModuleInfo(nid, type) {
|
|
14
|
-
this.
|
|
15
|
-
|
|
16
|
-
return;
|
|
17
|
-
|
|
13
|
+
async getModuleInfo(nid, type) {
|
|
14
|
+
if (this.modInfoQ !== undefined && !await this.modInfoQ.lock()) {
|
|
15
|
+
this.mx10.log.next("mx10.getModuleInfo: failed to acquire lock");
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
18
|
this.modInfoQ = new Query(ModInfoData.header(MsgMode.REQ, nid), this.onModuleInfoChange);
|
|
19
|
-
this.
|
|
19
|
+
this.modInfoQ.log = ((msg) => {
|
|
20
|
+
this.mx10.log.next(msg);
|
|
21
|
+
});
|
|
20
22
|
this.modInfoQ.tx = ((header) => {
|
|
21
23
|
const msg = new Message(header);
|
|
22
24
|
msg.push({ value: nid, length: 2 });
|
|
@@ -28,8 +30,7 @@ export default class InfoGroup {
|
|
|
28
30
|
this.mx10.log.next('mx10 query rx: ' + JSON.stringify(msg));
|
|
29
31
|
return (msg.type() === type);
|
|
30
32
|
});
|
|
31
|
-
|
|
32
|
-
const rv = this.modInfoQ.run();
|
|
33
|
+
const rv = await this.modInfoQ.run();
|
|
33
34
|
this.mx10.log.next("mx10.getModuleInfo.rv: " + JSON.stringify(rv));
|
|
34
35
|
this.modInfoQ.unlock();
|
|
35
36
|
this.modInfoQ = undefined;
|
|
@@ -53,6 +54,7 @@ export default class InfoGroup {
|
|
|
53
54
|
const type = buffer.readUInt16LE(2);
|
|
54
55
|
const info = buffer.readUInt32LE(4);
|
|
55
56
|
const msg = new ModInfoData(ModInfoData.header(mode, NID), type, [{ value: info, length: 4 }]);
|
|
57
|
+
this.mx10.log.next('onModuleInfoChange <- ' + JSON.stringify(msg));
|
|
56
58
|
this.onModuleInfoChange.next(msg);
|
|
57
59
|
}
|
|
58
60
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"infoGroup.js","sourceRoot":"","sources":["../../src/zcan/infoGroup.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,OAAO,EAAC,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAC,OAAO,EAAE,KAAK,EAAC,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAkC,WAAW,EAAC,MAAM,kBAAkB,CAAC;AAC9E,OAAO,EAAC,QAAQ,EAAE,SAAS,EAAE,gBAAgB,EAAe,OAAO,EAAC,MAAM,eAAe,CAAC;AAM1F,MAAM,CAAC,OAAO,OAAO,SAAS;IACpB,IAAI,CAAO;IAEH,gBAAgB,GAAG,IAAI,OAAO,EAAgB,CAAC;IAC/C,kBAAkB,GAAG,IAAI,OAAO,EAAe,CAAC;IAExD,QAAQ,GAAmC,SAAS,CAAC;IAE7D,YAAY,IAAU;QACpB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,aAAa,CAAC,GAAW,EAAE,IAA0B;
|
|
1
|
+
{"version":3,"file":"infoGroup.js","sourceRoot":"","sources":["../../src/zcan/infoGroup.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,OAAO,EAAC,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAC,OAAO,EAAE,KAAK,EAAC,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAkC,WAAW,EAAC,MAAM,kBAAkB,CAAC;AAC9E,OAAO,EAAC,QAAQ,EAAE,SAAS,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AAI,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;oBACjD,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,CAAC;oBACzD,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;oBAC/C,MAAM;gBACR;oBACE,IAAI,GAAG,IAAI,CAAC;YAChB,CAAC;YAED,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC;gBACzB,GAAG,EAAE,GAAG;gBACR,IAAI;gBACJ,IAAI;aACL,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAEO,aAAa,CAAC,IAAY;QAChC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC;YAC1B,OAAO,SAAS,CAAC,IAAI,CAAC;QACxB,CAAC;aAAM,CAAC;YACN,OAAO,SAAS,CAAC,IAAI,CAAC;QACxB,CAAC;IACH,CAAC;IAEO,cAAc,CAAC,IAAY;QACjC,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,IAAI,CAAC;IAC/B,CAAC;IAEO,WAAW,CAAC,IAAY;QAC9B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;YACvB,OAAO,gBAAgB,CAAC,OAAO,CAAC;QAClC,CAAC;aAAM,CAAC;YACN,OAAO,gBAAgB,CAAC,OAAO,CAAC;QAClC,CAAC;IACH,CAAC;IACO,qBAAqB,CAAC,IAAY;QACxC,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,IAAI,CAAC;IAC/B,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "https://github.com/ZIMO-Elektronik/zcan",
|
|
8
|
-
"version": "1.0.
|
|
8
|
+
"version": "1.0.32",
|
|
9
9
|
"scripts": {
|
|
10
10
|
"start": "node dist/main.js",
|
|
11
11
|
"start:dev": "nodemon --ext js,ts,json,env --exec \"node --experimental-specifier-resolution=node --loader ts-node/esm\" src/main.ts",
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {BindOptions, RemoteInfo, SocketOptions} from 'dgram';
|
|
2
2
|
import {Buffer} from 'buffer';
|
|
3
|
-
import {MsgMode} from '
|
|
4
|
-
import {delay
|
|
3
|
+
import {MsgMode} from '../util/enums';
|
|
4
|
+
import {delay} from '../internal/utils';
|
|
5
|
+
import {Subject, Subscription} from 'rxjs';
|
|
5
6
|
|
|
6
7
|
export type Header = {
|
|
7
8
|
group: number,
|
|
@@ -43,6 +44,7 @@ export class Query<T extends Message>
|
|
|
43
44
|
private result: T | undefined = undefined;
|
|
44
45
|
match: (msg: T) => boolean = () => {return true};
|
|
45
46
|
private mutex: boolean = false;
|
|
47
|
+
log: (msg: string) => void = () => {};
|
|
46
48
|
|
|
47
49
|
constructor(header: Header, subject: Subject<T> , match: (msg: T) => boolean = (() => {return true;}))
|
|
48
50
|
{
|
|
@@ -51,17 +53,17 @@ export class Query<T extends Message>
|
|
|
51
53
|
this.match = match;
|
|
52
54
|
}
|
|
53
55
|
|
|
54
|
-
lock(millis: number = 500): boolean
|
|
56
|
+
async lock(millis: number = 500): Promise<boolean>
|
|
55
57
|
{
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
let centis = Math.abs(millis) / 10;
|
|
59
|
+
while(this.mutex && centis)
|
|
60
|
+
{
|
|
61
|
+
await delay(10);
|
|
62
|
+
if(!centis--)
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
this.mutex = true;
|
|
66
|
+
return true;
|
|
65
67
|
}
|
|
66
68
|
|
|
67
69
|
unlock()
|
|
@@ -69,32 +71,43 @@ export class Query<T extends Message>
|
|
|
69
71
|
this.mutex = false;
|
|
70
72
|
}
|
|
71
73
|
|
|
72
|
-
|
|
74
|
+
subscribe()
|
|
73
75
|
{
|
|
74
|
-
if(this.tx === undefined)
|
|
75
|
-
return undefined;
|
|
76
|
-
|
|
77
76
|
this.rx = this.subject.subscribe((msg: T) =>
|
|
78
77
|
{
|
|
79
|
-
if(
|
|
78
|
+
if(msg.header.mode !== MsgMode.ACK)
|
|
80
79
|
return;
|
|
81
80
|
if(msg.header.nid !== this.header.nid)
|
|
82
81
|
return;
|
|
83
|
-
this.
|
|
82
|
+
this.log('query.run.rx: ' + JSON.stringify(msg));
|
|
83
|
+
if(!this.match(msg))
|
|
84
|
+
return;
|
|
85
|
+
this.result = msg;
|
|
84
86
|
this.rx?.unsubscribe();
|
|
85
87
|
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async run(retries: number = 5): Promise<T | undefined>
|
|
91
|
+
{
|
|
92
|
+
if(this.tx === undefined)
|
|
93
|
+
return undefined;
|
|
94
|
+
if(this.rx === undefined)
|
|
95
|
+
this.subscribe();
|
|
86
96
|
|
|
87
97
|
let tick = 2 * Math.abs(retries);
|
|
88
98
|
|
|
89
99
|
while(this.result === undefined)
|
|
90
100
|
{
|
|
91
|
-
if(tick % 2
|
|
101
|
+
if(tick % 2)
|
|
102
|
+
await delay(5);
|
|
103
|
+
else
|
|
92
104
|
this.tx(this.header);
|
|
105
|
+
|
|
93
106
|
if(!tick--) {
|
|
107
|
+
this.log('query.run.failed :(');
|
|
94
108
|
this.rx?.unsubscribe();
|
|
95
109
|
return undefined;
|
|
96
110
|
}
|
|
97
|
-
delay(5);
|
|
98
111
|
}
|
|
99
112
|
return this.result;
|
|
100
113
|
}
|
package/src/@types/models.ts
CHANGED
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
FxModeType,
|
|
19
19
|
ModInfoType,
|
|
20
20
|
MsgMode,
|
|
21
|
-
} from '
|
|
21
|
+
} from '../util/enums';
|
|
22
22
|
import {Header, Message, ZcanDataArray} from './communication';
|
|
23
23
|
import { delay, Subject, Subscription } from 'rxjs';
|
|
24
24
|
|
|
@@ -141,7 +141,7 @@ export class ModInfoData extends Message
|
|
|
141
141
|
|
|
142
142
|
public static header(mode: MsgMode, nid: number): Header
|
|
143
143
|
{
|
|
144
|
-
return {group: 0x08, cmd: 0x08, mode:
|
|
144
|
+
return {group: 0x08, cmd: 0x08, mode: mode, nid};
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
constructor(header: Header, type: ModInfoType, data: ZcanDataArray = [])
|
package/src/zcan/infoGroup.ts
CHANGED
|
@@ -21,15 +21,17 @@ export default class InfoGroup {
|
|
|
21
21
|
this.mx10 = mx10;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
getModuleInfo(nid: number, type: ModInfoType | number): ModInfoData | undefined
|
|
24
|
+
async getModuleInfo(nid: number, type: ModInfoType | number): Promise<ModInfoData | undefined>
|
|
25
25
|
{
|
|
26
|
-
this.
|
|
27
|
-
|
|
28
|
-
return;
|
|
29
|
-
|
|
26
|
+
if(this.modInfoQ !== undefined && !await this.modInfoQ.lock()) {
|
|
27
|
+
this.mx10.log.next("mx10.getModuleInfo: failed to acquire lock");
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
30
|
|
|
31
31
|
this.modInfoQ = new Query(ModInfoData.header(MsgMode.REQ, nid), this.onModuleInfoChange);
|
|
32
|
-
this.
|
|
32
|
+
this.modInfoQ.log = ((msg) => {
|
|
33
|
+
this.mx10.log.next(msg);
|
|
34
|
+
});
|
|
33
35
|
this.modInfoQ.tx = ((header) => {
|
|
34
36
|
const msg = new Message(header);
|
|
35
37
|
msg.push({value: nid, length: 2});
|
|
@@ -41,8 +43,8 @@ export default class InfoGroup {
|
|
|
41
43
|
this.mx10.log.next('mx10 query rx: ' + JSON.stringify(msg));
|
|
42
44
|
return (msg.type() === type);
|
|
43
45
|
})
|
|
44
|
-
this.
|
|
45
|
-
const rv = this.modInfoQ.run();
|
|
46
|
+
// this.modInfoQ.subscribe();
|
|
47
|
+
const rv = await this.modInfoQ.run();
|
|
46
48
|
this.mx10.log.next("mx10.getModuleInfo.rv: " + JSON.stringify(rv));
|
|
47
49
|
this.modInfoQ.unlock();
|
|
48
50
|
this.modInfoQ = undefined;
|
|
@@ -75,11 +77,14 @@ export default class InfoGroup {
|
|
|
75
77
|
nid: number,
|
|
76
78
|
buffer: Buffer,
|
|
77
79
|
) {
|
|
80
|
+
// const msgMode: MsgMode = mode as MsgMode;
|
|
81
|
+
// this.mx10.log.next('parseModuleInfo: ' + msgMode);
|
|
78
82
|
if (this.onModuleInfoChange.observed) {
|
|
79
83
|
const NID = buffer.readUInt16LE(0);
|
|
80
84
|
const type = buffer.readUInt16LE(2);
|
|
81
85
|
const info = buffer.readUInt32LE(4);
|
|
82
86
|
const msg = new ModInfoData(ModInfoData.header(mode, NID), type, [{value: info, length: 4}]);
|
|
87
|
+
this.mx10.log.next('onModuleInfoChange <- ' + JSON.stringify(msg));
|
|
83
88
|
this.onModuleInfoChange.next(msg);
|
|
84
89
|
}
|
|
85
90
|
}
|