can-opener-js 2.0.0 → 2.1.0
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/index.cjs +183 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +183 -40
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -325,6 +325,45 @@ function groupStepsByBus(steps) {
|
|
|
325
325
|
}
|
|
326
326
|
return groups;
|
|
327
327
|
}
|
|
328
|
+
var EndpointLeasePool = class {
|
|
329
|
+
tails = /* @__PURE__ */ new Map();
|
|
330
|
+
async acquire(names) {
|
|
331
|
+
const sorted = Array.from(new Set(names)).sort();
|
|
332
|
+
const releases = [];
|
|
333
|
+
for (const name of sorted) {
|
|
334
|
+
const previous = this.tails.get(name) ?? Promise.resolve();
|
|
335
|
+
let release;
|
|
336
|
+
const next = new Promise((resolve) => {
|
|
337
|
+
release = resolve;
|
|
338
|
+
});
|
|
339
|
+
const tail = previous.then(() => next);
|
|
340
|
+
this.tails.set(name, tail);
|
|
341
|
+
await previous;
|
|
342
|
+
releases.push(() => {
|
|
343
|
+
release();
|
|
344
|
+
if (this.tails.get(name) === tail) {
|
|
345
|
+
this.tails.delete(name);
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
return () => {
|
|
350
|
+
for (let index = releases.length - 1; index >= 0; index -= 1) {
|
|
351
|
+
releases[index]?.();
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
function actionEndpointNames(action) {
|
|
357
|
+
const names = /* @__PURE__ */ new Set();
|
|
358
|
+
for (const step of action.steps) {
|
|
359
|
+
if (step.requestId !== void 0) continue;
|
|
360
|
+
const endpoint = step.endpoint ?? action.endpoint;
|
|
361
|
+
if (endpoint !== void 0) {
|
|
362
|
+
names.add(endpoint);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return Array.from(names).sort();
|
|
366
|
+
}
|
|
328
367
|
var ActionController = class {
|
|
329
368
|
constructor(dbc, transport, capabilities) {
|
|
330
369
|
this.dbc = dbc;
|
|
@@ -335,6 +374,7 @@ var ActionController = class {
|
|
|
335
374
|
transport;
|
|
336
375
|
capabilities;
|
|
337
376
|
scheduled = /* @__PURE__ */ new Set();
|
|
377
|
+
endpointLeases = new EndpointLeasePool();
|
|
338
378
|
async send(signal, opts) {
|
|
339
379
|
const frame = this.applyMask(
|
|
340
380
|
this.dbc.encodeSignal(signal.name, opts.value),
|
|
@@ -377,23 +417,28 @@ var ActionController = class {
|
|
|
377
417
|
this.scheduled.clear();
|
|
378
418
|
}
|
|
379
419
|
async run(action, options = {}) {
|
|
380
|
-
|
|
381
|
-
const wakeAction = this.resolveEcuWakeAction();
|
|
382
|
-
if (wakeAction !== void 0) {
|
|
383
|
-
return this.runAfterEcuWake(action, wakeAction);
|
|
384
|
-
}
|
|
385
|
-
}
|
|
420
|
+
const release = await this.endpointLeases.acquire(actionEndpointNames(action));
|
|
386
421
|
try {
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
422
|
+
if (options.wakeFirst === true && action.name !== ECU_WAKE_ACTION) {
|
|
423
|
+
const wakeAction = this.resolveEcuWakeAction();
|
|
424
|
+
if (wakeAction !== void 0) {
|
|
425
|
+
return await this.runAfterEcuWake(action, wakeAction);
|
|
426
|
+
}
|
|
391
427
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
428
|
+
try {
|
|
429
|
+
return await this.runSteps(action);
|
|
430
|
+
} catch (error) {
|
|
431
|
+
if (!isSilentBusError(error) || action.name === ECU_WAKE_ACTION) {
|
|
432
|
+
throw error;
|
|
433
|
+
}
|
|
434
|
+
const wakeAction = this.resolveEcuWakeAction();
|
|
435
|
+
if (wakeAction === void 0) {
|
|
436
|
+
throw error;
|
|
437
|
+
}
|
|
438
|
+
return await this.runAfterEcuWake(action, wakeAction);
|
|
395
439
|
}
|
|
396
|
-
|
|
440
|
+
} finally {
|
|
441
|
+
release();
|
|
397
442
|
}
|
|
398
443
|
}
|
|
399
444
|
/**
|
|
@@ -749,6 +794,27 @@ function applyValueNormalization(value, normalize, sourceUnit) {
|
|
|
749
794
|
}
|
|
750
795
|
return result;
|
|
751
796
|
}
|
|
797
|
+
function isWithinNumericConstraints(value, constraints) {
|
|
798
|
+
if (constraints === void 0 || typeof value !== "number") {
|
|
799
|
+
return true;
|
|
800
|
+
}
|
|
801
|
+
if (!Number.isFinite(value)) {
|
|
802
|
+
return false;
|
|
803
|
+
}
|
|
804
|
+
if (constraints.min !== void 0 && value < constraints.min) {
|
|
805
|
+
return false;
|
|
806
|
+
}
|
|
807
|
+
if (constraints.max !== void 0 && value > constraints.max) {
|
|
808
|
+
return false;
|
|
809
|
+
}
|
|
810
|
+
if (constraints.step !== void 0 && constraints.step > 0) {
|
|
811
|
+
const steps = (value - (constraints.min ?? 0)) / constraints.step;
|
|
812
|
+
if (Math.abs(steps - Math.round(steps)) > 1e-9) {
|
|
813
|
+
return false;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
return true;
|
|
817
|
+
}
|
|
752
818
|
|
|
753
819
|
// src/controllers/QueryController.ts
|
|
754
820
|
var DEFAULT_QUERY_SUBSCRIPTION_FREQUENCY_HZ = 1;
|
|
@@ -888,6 +954,10 @@ var QueryController = class {
|
|
|
888
954
|
return this.capabilities.resolveEndpoint(name);
|
|
889
955
|
}
|
|
890
956
|
decodeProfileQuery(query, payload) {
|
|
957
|
+
const value = this.decodeProfileQueryValue(query, payload);
|
|
958
|
+
return isWithinNumericConstraints(value, query.constraints) ? value : void 0;
|
|
959
|
+
}
|
|
960
|
+
decodeProfileQueryValue(query, payload) {
|
|
891
961
|
const decoder = query.decoder;
|
|
892
962
|
if (decoder === void 0) {
|
|
893
963
|
return applyValueNormalization(stripExpectedPrefix(query.expect, payload), query.normalize);
|
|
@@ -3665,11 +3735,11 @@ function parseDocument(content, name) {
|
|
|
3665
3735
|
}
|
|
3666
3736
|
function channelBuses(manifest, composed) {
|
|
3667
3737
|
const names = new Set(Object.keys(composed.channels));
|
|
3668
|
-
for (const endpoint of Object.values(composed.endpoints)) {
|
|
3669
|
-
names.add(endpoint.channel ?? DEFAULT_CHANNEL);
|
|
3670
|
-
}
|
|
3671
3738
|
for (const dbc of Object.values(manifest.dbc ?? {})) {
|
|
3672
|
-
|
|
3739
|
+
const channel = dbc.channel ?? DEFAULT_CHANNEL;
|
|
3740
|
+
if (composed.channels[channel] !== void 0 || /^can\d+$/u.test(channel)) {
|
|
3741
|
+
names.add(channel);
|
|
3742
|
+
}
|
|
3673
3743
|
}
|
|
3674
3744
|
const buses = /* @__PURE__ */ new Map();
|
|
3675
3745
|
for (const name of names) {
|
|
@@ -3760,12 +3830,16 @@ function lowerAddressing(addressing, path) {
|
|
|
3760
3830
|
function lowerProperty(path, property, context) {
|
|
3761
3831
|
const signal = property.read?.signal;
|
|
3762
3832
|
const query = property.read?.query;
|
|
3833
|
+
const readConstraints = lowerConstraints(property.read?.constraints);
|
|
3834
|
+
const loweredSignal = signal != null ? lowerSignal(path, signal, context) : void 0;
|
|
3835
|
+
const loweredQuery = query != null ? lowerPropertyQuery(path, query, context) : void 0;
|
|
3763
3836
|
return {
|
|
3764
3837
|
path,
|
|
3765
3838
|
...vssLeaf(path),
|
|
3766
3839
|
...property.aliases !== void 0 && property.aliases.length > 0 ? { aliases: property.aliases } : {},
|
|
3767
|
-
...
|
|
3768
|
-
...
|
|
3840
|
+
...readConstraints !== void 0 ? { readConstraints } : {},
|
|
3841
|
+
...loweredSignal !== void 0 ? { signal: readConstraints !== void 0 ? { ...loweredSignal, constraints: readConstraints } : loweredSignal } : {},
|
|
3842
|
+
...loweredQuery !== void 0 ? { query: readConstraints !== void 0 ? { ...loweredQuery, constraints: readConstraints } : loweredQuery } : {},
|
|
3769
3843
|
...property.write != null ? { write: lowerWrite(path, property.write, context) } : {}
|
|
3770
3844
|
};
|
|
3771
3845
|
}
|
|
@@ -3853,31 +3927,45 @@ function lowerQuery(name, query, decoder, context) {
|
|
|
3853
3927
|
...unavailable !== void 0 ? { unavailable } : {}
|
|
3854
3928
|
};
|
|
3855
3929
|
}
|
|
3930
|
+
function lowerConstraints(raw) {
|
|
3931
|
+
const constraints = raw;
|
|
3932
|
+
if (constraints == null) {
|
|
3933
|
+
return void 0;
|
|
3934
|
+
}
|
|
3935
|
+
return {
|
|
3936
|
+
...constraints.min != null ? { min: constraints.min } : {},
|
|
3937
|
+
...constraints.max != null ? { max: constraints.max } : {},
|
|
3938
|
+
...constraints.step != null ? { step: constraints.step } : {}
|
|
3939
|
+
};
|
|
3940
|
+
}
|
|
3856
3941
|
function lowerWrite(path, write, context) {
|
|
3857
|
-
const constraints = write.constraints;
|
|
3942
|
+
const constraints = lowerConstraints(write.constraints);
|
|
3858
3943
|
return {
|
|
3859
3944
|
..."cases" in write ? {
|
|
3860
3945
|
cases: Object.fromEntries(
|
|
3861
3946
|
Object.entries(write.cases).map(([target, body]) => [target, lowerBody(`${path}=${target}`, body, context)])
|
|
3862
3947
|
)
|
|
3863
3948
|
} : { direct: lowerBody(path, write, context) },
|
|
3864
|
-
...constraints
|
|
3865
|
-
constraints: {
|
|
3866
|
-
...constraints.min != null ? { min: constraints.min } : {},
|
|
3867
|
-
...constraints.max != null ? { max: constraints.max } : {},
|
|
3868
|
-
...constraints.step != null ? { step: constraints.step } : {}
|
|
3869
|
-
}
|
|
3870
|
-
} : {}
|
|
3949
|
+
...constraints !== void 0 ? { constraints } : {}
|
|
3871
3950
|
};
|
|
3872
3951
|
}
|
|
3873
3952
|
function lowerBody(name, body, context) {
|
|
3874
|
-
const
|
|
3953
|
+
const callerEndpoint = body.endpoint ?? void 0;
|
|
3954
|
+
if (callerEndpoint !== void 0) {
|
|
3955
|
+
resolveEndpoint(name, callerEndpoint, context);
|
|
3956
|
+
}
|
|
3875
3957
|
if (body.repeat != null) {
|
|
3876
|
-
|
|
3958
|
+
if (callerEndpoint === void 0) {
|
|
3959
|
+
throw new VirtualVehicleError(`${name} requires an endpoint`);
|
|
3960
|
+
}
|
|
3961
|
+
return { name, endpoint: callerEndpoint, steps: [], unavailable: "repeat writes are not supported" };
|
|
3962
|
+
}
|
|
3963
|
+
if (body.steps == null && callerEndpoint === void 0) {
|
|
3964
|
+
throw new VirtualVehicleError(`${name} requires an endpoint`);
|
|
3877
3965
|
}
|
|
3878
3966
|
const steps = [];
|
|
3879
3967
|
let unavailable;
|
|
3880
|
-
const expand = (items, depth) => {
|
|
3968
|
+
const expand = (items, activeEndpointName, depth) => {
|
|
3881
3969
|
for (const step of items) {
|
|
3882
3970
|
if ("call" in step) {
|
|
3883
3971
|
const sequence = context.composed.sequences[step.call];
|
|
@@ -3887,19 +3975,28 @@ function lowerBody(name, body, context) {
|
|
|
3887
3975
|
if (depth >= MAX_CALL_DEPTH) {
|
|
3888
3976
|
throw new VirtualVehicleError(`${name} nests more than ${MAX_CALL_DEPTH} sequence calls`);
|
|
3889
3977
|
}
|
|
3890
|
-
|
|
3978
|
+
const sequenceEndpoint = sequence.endpoint ?? void 0;
|
|
3979
|
+
if (sequenceEndpoint !== void 0) {
|
|
3980
|
+
resolveEndpoint(`sequences.${step.call}`, sequenceEndpoint, context);
|
|
3981
|
+
}
|
|
3982
|
+
expand(sequence.steps, sequenceEndpoint ?? activeEndpointName, depth + 1);
|
|
3891
3983
|
} else if ("wait_ms" in step) {
|
|
3892
3984
|
const previous = steps.at(-1);
|
|
3893
3985
|
if (previous !== void 0) {
|
|
3894
3986
|
previous.delayMs = (previous.delayMs ?? 0) + step.wait_ms;
|
|
3895
3987
|
}
|
|
3896
3988
|
} else {
|
|
3989
|
+
if (activeEndpointName === void 0) {
|
|
3990
|
+
throw new VirtualVehicleError(`${name} has a request with no endpoint`);
|
|
3991
|
+
}
|
|
3992
|
+
const endpoint = resolveEndpoint(name, activeEndpointName, context);
|
|
3897
3993
|
const request = lowerRequest(step.request, endpoint, name);
|
|
3898
3994
|
if ("unavailable" in request) {
|
|
3899
3995
|
unavailable ??= request.unavailable;
|
|
3900
3996
|
continue;
|
|
3901
3997
|
}
|
|
3902
3998
|
steps.push({
|
|
3999
|
+
...activeEndpointName !== callerEndpoint ? { endpoint: activeEndpointName } : {},
|
|
3903
4000
|
send: request.send,
|
|
3904
4001
|
...request.fields.length > 0 ? { fields: request.fields } : {},
|
|
3905
4002
|
...step.expect != null ? { expect: lowerPattern(step.expect, name) } : {}
|
|
@@ -3907,10 +4004,14 @@ function lowerBody(name, body, context) {
|
|
|
3907
4004
|
}
|
|
3908
4005
|
}
|
|
3909
4006
|
};
|
|
3910
|
-
expand(
|
|
4007
|
+
expand(
|
|
4008
|
+
body.steps ?? [{ request: body.request, ...body.expect != null ? { expect: body.expect } : {} }],
|
|
4009
|
+
callerEndpoint,
|
|
4010
|
+
0
|
|
4011
|
+
);
|
|
3911
4012
|
return {
|
|
3912
4013
|
name,
|
|
3913
|
-
endpoint:
|
|
4014
|
+
...callerEndpoint !== void 0 ? { endpoint: callerEndpoint } : {},
|
|
3914
4015
|
steps,
|
|
3915
4016
|
...unavailable !== void 0 ? { unavailable } : {}
|
|
3916
4017
|
};
|
|
@@ -4251,18 +4352,47 @@ var CapabilityRegistry = class {
|
|
|
4251
4352
|
for (const config of buses) {
|
|
4252
4353
|
this.buses.set(config.bus, config);
|
|
4253
4354
|
}
|
|
4254
|
-
for (const profile of profiles) {
|
|
4355
|
+
for (const [index, profile] of profiles.entries()) {
|
|
4356
|
+
const endpointNames = new Map(profile.endpoints.map((endpoint) => [endpoint.name, `${index}:${endpoint.name}`]));
|
|
4357
|
+
const endpointName = (name) => endpointNames.get(name) ?? name;
|
|
4358
|
+
const scopeAction = (action) => ({
|
|
4359
|
+
...action,
|
|
4360
|
+
...action.endpoint !== void 0 ? { endpoint: endpointName(action.endpoint) } : {},
|
|
4361
|
+
steps: action.steps.map((step) => ({
|
|
4362
|
+
...step,
|
|
4363
|
+
...step.endpoint !== void 0 ? { endpoint: endpointName(step.endpoint) } : {}
|
|
4364
|
+
}))
|
|
4365
|
+
});
|
|
4366
|
+
const scopeQuery = (query) => ({ ...query, endpoint: endpointName(query.endpoint) });
|
|
4255
4367
|
for (const endpoint of profile.endpoints) {
|
|
4368
|
+
const name = endpointName(endpoint.name);
|
|
4256
4369
|
this.endpoints.set(endpoint.name, endpoint);
|
|
4370
|
+
this.endpoints.set(name, { ...endpoint, name });
|
|
4257
4371
|
}
|
|
4258
4372
|
for (const property of profile.properties) {
|
|
4259
|
-
|
|
4373
|
+
const scoped = { ...property };
|
|
4374
|
+
if (property.query !== void 0) {
|
|
4375
|
+
scoped.query = scopeQuery(property.query);
|
|
4376
|
+
}
|
|
4377
|
+
if (property.write !== void 0) {
|
|
4378
|
+
const write = { ...property.write };
|
|
4379
|
+
if (write.direct !== void 0) {
|
|
4380
|
+
write.direct = scopeAction(write.direct);
|
|
4381
|
+
}
|
|
4382
|
+
if (write.cases !== void 0) {
|
|
4383
|
+
write.cases = Object.fromEntries(
|
|
4384
|
+
Object.entries(write.cases).map(([key, action]) => [key, scopeAction(action)])
|
|
4385
|
+
);
|
|
4386
|
+
}
|
|
4387
|
+
scoped.write = write;
|
|
4388
|
+
}
|
|
4389
|
+
this.properties.set(property.path, mergeProperty(this.properties.get(property.path), scoped));
|
|
4260
4390
|
}
|
|
4261
4391
|
for (const query of profile.queries) {
|
|
4262
|
-
this.queries.set(query.name, query);
|
|
4392
|
+
this.queries.set(query.name, scopeQuery(query));
|
|
4263
4393
|
}
|
|
4264
4394
|
for (const action of profile.actions) {
|
|
4265
|
-
this.actions.set(action.name, action);
|
|
4395
|
+
this.actions.set(action.name, scopeAction(action));
|
|
4266
4396
|
}
|
|
4267
4397
|
}
|
|
4268
4398
|
for (const endpoint of this.endpoints.values()) {
|
|
@@ -5050,7 +5180,13 @@ var VirtualVehicle = class {
|
|
|
5050
5180
|
name: monitor.name,
|
|
5051
5181
|
canId,
|
|
5052
5182
|
...bus,
|
|
5053
|
-
decode: (frame) =>
|
|
5183
|
+
decode: (frame) => {
|
|
5184
|
+
if ((frame.dlc ?? frame.data.length) < length) {
|
|
5185
|
+
return void 0;
|
|
5186
|
+
}
|
|
5187
|
+
const value = decodeField(frame.data, decode);
|
|
5188
|
+
return isWithinNumericConstraints(value, monitor.constraints) ? value : void 0;
|
|
5189
|
+
}
|
|
5054
5190
|
};
|
|
5055
5191
|
}
|
|
5056
5192
|
const signal = this.dbc.resolveMessageSignal(monitor.message, monitor.signal);
|
|
@@ -5058,7 +5194,14 @@ var VirtualVehicle = class {
|
|
|
5058
5194
|
name: monitor.name,
|
|
5059
5195
|
canId: signal.canId,
|
|
5060
5196
|
...bus,
|
|
5061
|
-
decode: (frame) =>
|
|
5197
|
+
decode: (frame) => {
|
|
5198
|
+
const value = applyValueNormalization(
|
|
5199
|
+
decodeDbcSignal(frame, signal, monitor.value),
|
|
5200
|
+
monitor.normalize,
|
|
5201
|
+
signal.unit
|
|
5202
|
+
);
|
|
5203
|
+
return isWithinNumericConstraints(value, monitor.constraints) ? value : void 0;
|
|
5204
|
+
}
|
|
5062
5205
|
};
|
|
5063
5206
|
}
|
|
5064
5207
|
};
|