@csselectronics/can-decode 0.0.0-stage → 0.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.
Files changed (43) hide show
  1. package/LICENSE +36 -0
  2. package/README.md +29 -2
  3. package/index.d.ts +101 -0
  4. package/index.js +2 -0
  5. package/models/CanTypes.d.ts +133 -0
  6. package/models/CanTypes.js +2 -0
  7. package/models/DecodedSignal.d.ts +11 -0
  8. package/models/DecodedSignal.js +1 -0
  9. package/models/dbc/BusDbcAssignment.d.ts +45 -0
  10. package/models/dbc/BusDbcAssignment.js +2 -0
  11. package/models/dbc/DbcDefinition.d.ts +117 -0
  12. package/models/dbc/DbcDefinition.js +2 -0
  13. package/models/dbc/DbcUnion.d.ts +92 -0
  14. package/models/dbc/DbcUnion.js +2 -0
  15. package/models/dbc/IDbcDefinition.d.ts +72 -0
  16. package/models/dbc/IDbcDefinition.js +1 -0
  17. package/package.json +33 -4
  18. package/services/pipeline/DecodingEngine.d.ts +107 -0
  19. package/services/pipeline/DecodingEngine.js +2 -0
  20. package/services/pipeline/DecodingService.d.ts +52 -0
  21. package/services/pipeline/DecodingService.js +2 -0
  22. package/services/pipeline/dbc/DbcAssignmentService.d.ts +84 -0
  23. package/services/pipeline/dbc/DbcAssignmentService.js +2 -0
  24. package/services/pipeline/dbc/DbcManagerService.d.ts +95 -0
  25. package/services/pipeline/dbc/DbcManagerService.js +2 -0
  26. package/utils/DbcParser.d.ts +26 -0
  27. package/utils/DbcParser.js +2 -0
  28. package/utils/JsonParser.d.ts +25 -0
  29. package/utils/JsonParser.js +2 -0
  30. package/utils/SignalDecoder.d.ts +38 -0
  31. package/utils/SignalDecoder.js +2 -0
  32. package/utils/SignalEncoder.d.ts +46 -0
  33. package/utils/SignalEncoder.js +2 -0
  34. package/utils/SignalIdParser.d.ts +41 -0
  35. package/utils/SignalIdParser.js +2 -0
  36. package/utils/dbcKey.d.ts +1 -0
  37. package/utils/dbcKey.js +2 -0
  38. package/utils/downsampleMinMax.d.ts +8 -0
  39. package/utils/downsampleMinMax.js +2 -0
  40. package/utils/signalQuery.d.ts +45 -0
  41. package/utils/signalQuery.js +2 -0
  42. package/utils/signalReductions.d.ts +23 -0
  43. package/utils/signalReductions.js +2 -0
@@ -0,0 +1,46 @@
1
+ import type { CanSignal } from '../models/CanTypes.js';
2
+ /**
3
+ * Write a raw integer into a little-endian (Intel) bit span.
4
+ * Inverse of extractLittleEndian: iterates the same byte range,
5
+ * clearing the signal's bits in each byte and ORing in the shifted chunk.
6
+ */
7
+ export declare function putBitsLittleEndian(data: Uint8Array, bitpos: number, bitlen: number, raw: number): void;
8
+ /**
9
+ * Write a raw integer into a big-endian (Motorola) bit span.
10
+ * Inverse of extractBigEndian: uses the same DBC→internal involution
11
+ * and walks bits MSB-first, clearing one bit at a time.
12
+ */
13
+ export declare function putBitsBigEndian(data: Uint8Array, bitpos: number, bitlen: number, raw: number): void;
14
+ /** Write an IEEE 754 32-bit float into a byte-aligned slot. */
15
+ export declare function putFloat32(data: Uint8Array, bitpos: number, bigendian: boolean, value: number): void;
16
+ /** Write an IEEE 754 64-bit double into a byte-aligned slot. */
17
+ export declare function putFloat64(data: Uint8Array, bitpos: number, bigendian: boolean, value: number): void;
18
+ /**
19
+ * Convert a physical (scaled) value to the unsigned raw bit pattern,
20
+ * clamping to the signal's representable range and applying two's
21
+ * complement for signed signals. Floats/doubles are handled separately
22
+ * by encodeSignalInto — do not call this for them.
23
+ */
24
+ export declare function physicalToRaw(signal: CanSignal, physical: number): number;
25
+ /**
26
+ * Encode a signal's physical value into the given data buffer.
27
+ * Dispatches to the correct writer (float/double/big/little endian)
28
+ * and clears only the signal's bits, leaving the rest of the buffer intact.
29
+ *
30
+ * Callers that want empty bytes to read as 0xFF should pre-fill the buffer
31
+ * with 0xFF before calling encodeSignalInto for each signal.
32
+ */
33
+ export declare function encodeSignalInto(data: Uint8Array, signal: CanSignal, physical: number): void;
34
+ /**
35
+ * Compute the theoretical physical min/max of a signal from its
36
+ * bit length, signedness, factor, and offset — used as a fallback
37
+ * when the DBC does not specify an explicit min/max (min === max).
38
+ *
39
+ * Returns {min: 0, max: 0} for float/double signals. The modal treats
40
+ * that as "no fallback range" and seeds the input with 0 rather than
41
+ * writing ±Infinity into a number field.
42
+ */
43
+ export declare function theoreticalRange(signal: CanSignal): {
44
+ min: number;
45
+ max: number;
46
+ };
@@ -0,0 +1,2 @@
1
+ /*! @csselectronics/can-decode v0.1.0 (c) CSS Electronics ApS, see LICENSE */
2
+ var B=Object.defineProperty;var F=(e,t)=>B(e,"name",{value:t,configurable:!0});function d(e,t){return t?8*Math.floor(e/8)+(7-e%8):e}F(d,"dbcBitPosToInternal");function p(e,t,n,o){const r=Math.floor(t/8),f=t%8,s=t+n,i=Math.floor((s-1)/8);let c=o>>>0;n<32&&(c=c&(1<<n)-1);let u=0;for(let b=r;b<=i&&b<e.length;b++){const a=b===r?f:0,m=s-(b*8+a),l=Math.min(8-a,m),x=(1<<l)-1,y=c>>>u&x,h=x<<a;e[b]=(e[b]&~h|y<<a)&255,u+=l}}F(p,"putBitsLittleEndian");function M(e,t,n,o){const r=Math.floor(t/8),f=t%8;let s=o>>>0;if(n<32&&(s=s&(1<<n)-1),f===7&&n%8===0){const c=n/8;for(let u=0;u<c&&r+u<e.length;u++)e[r+u]=s>>>8*(c-1-u)&255;return}let i=d(t,!0);for(let c=0;c<n;c++,i++){const u=Math.floor(i/8);if(u>=e.length)break;const b=7-i%8,a=s>>>n-1-c&1,m=1<<b;e[u]=e[u]&~m|a<<b}}F(M,"putBitsBigEndian");function w(e,t,n,o){const r=Math.floor(t/8),f=new ArrayBuffer(4);new DataView(f).setFloat32(0,o,!n);const s=new Uint8Array(f);for(let i=0;i<4;i++)r+i<e.length&&(e[r+i]=s[i])}F(w,"putFloat32");function I(e,t,n,o){const r=Math.floor(t/8),f=new ArrayBuffer(8);new DataView(f).setFloat64(0,o,!n);const s=new Uint8Array(f);for(let i=0;i<8;i++)r+i<e.length&&(e[r+i]=s[i])}F(I,"putFloat64");function A(e,t){const n=e.bitlen;let o=(t-e.offset)/e.factor;if(o=Math.round(o),e.signed){const r=n===32?2147483647:(1<<n-1)-1,f=n===32?-2147483648:-(1<<n-1);o>r?o=r:o<f&&(o=f),o<0&&(o=n===32?o+4294967296>>>0:o+(1<<n))}else{const r=n===32?4294967295:(1<<n)-1;o>r?o=r:o<0&&(o=0)}return o}F(A,"physicalToRaw");function k(e,t,n){if(t.isFloat||t.isDouble){const r=t.isIdentityScale?n:(n-t.offset)/t.factor;t.isFloat?w(e,t.bitpos,t.bigendian,r):I(e,t.bitpos,t.bigendian,r);return}const o=A(t,n);t.bigendian?M(e,t.bitpos,t.bitlen,o):p(e,t.bitpos,t.bitlen,o)}F(k,"encodeSignalInto");function S(e){if(e.isFloat||e.isDouble)return{min:0,max:0};const t=e.bitlen;let n,o;e.signed?(o=t===32?2147483647:(1<<t-1)-1,n=t===32?-2147483648:-(1<<t-1)):(o=t===32?4294967295:(1<<t)-1,n=0);const r=F(i=>parseFloat(i.toPrecision(15)),"clean"),f=r(n*e.factor+e.offset),s=r(o*e.factor+e.offset);return f<=s?{min:f,max:s}:{min:s,max:f}}F(S,"theoreticalRange");export{k as encodeSignalInto,A as physicalToRaw,M as putBitsBigEndian,p as putBitsLittleEndian,w as putFloat32,I as putFloat64,S as theoreticalRange};
@@ -0,0 +1,41 @@
1
+ import type { CanBusName } from '@csselectronics/websocket-client/types/CanBusTypes';
2
+ /**
3
+ * Parsed components of a signal ID string.
4
+ *
5
+ * Signal IDs follow the format `${frame.key}_${signal.name}`, where
6
+ * `frame.key` has the form `${bus}_${arbId}_IDE<n>_DIR<n>_RTR<n>_TP<n>`
7
+ * (see `CanFrame` / `parseMessageKey` in `@csselectronics/websocket-client`). So the full
8
+ * signal-key shape is:
9
+ * `${bus}_${arbId}_IDE<n>_DIR<n>_RTR<n>_TP<n>_${signalName}`
10
+ *
11
+ * Synthetic UNMATCHED signals have `signalName = ''`, giving a trailing
12
+ * underscore: `${bus}_${arbId}_IDE<n>_DIR<n>_RTR<n>_TP<n>_`.
13
+ */
14
+ export interface ParsedSignalId {
15
+ /** Bus name (e.g., "CAN1") */
16
+ busName: CanBusName;
17
+ /** Arbitration ID as number (clean canId, no IDE flag bit) */
18
+ arbId: number;
19
+ /** IDE flag (true = 29-bit extended, false = 11-bit standard) */
20
+ ide: boolean;
21
+ /** Direction flag (true = TX, false = RX) */
22
+ isTx: boolean;
23
+ /** Remote-transmission-request flag */
24
+ isRtr: boolean;
25
+ /** TP-assembled synthetic flag */
26
+ isSynthetic: boolean;
27
+ /** Signal name (may contain underscores; empty for UNMATCHED entries) */
28
+ signalName: string;
29
+ /** Frame key for matching against `CanFrame.key` (the `${bus}_${arbId}_IDE<n>_DIR<n>_RTR<n>_TP<n>` prefix). */
30
+ frameKey: string;
31
+ }
32
+ /**
33
+ * Parse a signal ID string into its components.
34
+ *
35
+ * The signal ID is `${frame.key}_${signal.name}`. `frame.key` has 6
36
+ * underscore-separated segments (bus, arbId, IDE<n>, DIR<n>, RTR<n>,
37
+ * TP<n>); the signal name follows after the 6th underscore. Returns
38
+ * `null` for malformed input (fewer than 6 underscores or unparseable
39
+ * IDE/DIR/RTR/TP tokens).
40
+ */
41
+ export declare function parseSignalId(signalId: string): ParsedSignalId | null;
@@ -0,0 +1,2 @@
1
+ /*! @csselectronics/can-decode v0.1.0 (c) CSS Electronics ApS, see LICENSE */
2
+ var o=Object.defineProperty;var t=(s,r)=>o(s,"name",{value:r,configurable:!0});import{parseMessageKey as u}from"@csselectronics/websocket-client/models/CanFrame";function c(s){let r=-1;for(let n=0;n<6;n++)if(r=s.indexOf("_",r+1),r<0)return null;const i=s.substring(0,r),a=s.substring(r+1),e=u(i);return e.isError||Number.isNaN(e.arbId)?null:{busName:e.bus,arbId:e.arbId,ide:e.ide,isTx:e.isTx,isRtr:e.isRtr,isSynthetic:e.isSynthetic,signalName:a,frameKey:i}}t(c,"parseSignalId");export{c as parseSignalId};
@@ -0,0 +1 @@
1
+ export declare function encodeIdeKey(canId: number, isExtended: boolean): number;
@@ -0,0 +1,2 @@
1
+ /*! @csselectronics/can-decode v0.1.0 (c) CSS Electronics ApS, see LICENSE */
2
+ var r=Object.defineProperty;var o=(e,n)=>r(e,"name",{value:n,configurable:!0});const u=2147483648;function E(e,n){return n?(e|2147483648)>>>0:e}o(E,"encodeIdeKey");export{E as encodeIdeKey};
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Min-max envelope downsampling.
3
+ *
4
+ * Divides input into equal-width buckets and emits the real min and max
5
+ * sample from each (preserving original timestamps). No interpolation.
6
+ * Output is time-sorted ascending. No-op when input length <= maxPoints.
7
+ */
8
+ export declare function downsampleMinMax(timestamps: ArrayLike<number>, values: ArrayLike<number>, maxPoints: number, count?: number): [number[], number[]];
@@ -0,0 +1,2 @@
1
+ /*! @csselectronics/can-decode v0.1.0 (c) CSS Electronics ApS, see LICENSE */
2
+ var A=Object.defineProperty;var s=(n,u)=>A(n,"name",{value:u,configurable:!0});function m(n,u,x,y){const t=y??n.length;if(t<=x){const o=new Array(t),r=new Array(t);for(let e=0;e<t;e++)o[e]=n[e],r[e]=u[e];return[o,r]}const d=Math.ceil(x/2),k=t/d,h=[],i=[];for(let o=0;o<d;o++){const r=Math.floor(o*k),e=Math.min(Math.floor((o+1)*k),t);if(r>=e)continue;let c=r,b=r,l=u[r],M=u[r];for(let f=r+1;f<e;f++){const a=u[f];a<l&&(l=a,c=f),a>M&&(M=a,b=f)}c===b?(h.push(n[c]),i.push(l)):c<b?(h.push(n[c],n[b]),i.push(l,M)):(h.push(n[b],n[c]),i.push(M,l))}return[h,i]}s(m,"downsampleMinMax");export{m as downsampleMinMax};
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Multi-signal batched query with single iteration per bus.
3
+ *
4
+ * Groups signals by bus, queries the given frame store once per bus,
5
+ * extracts values in a single pass, and downsamples if needed.
6
+ */
7
+ import type { FiltFrameStorageService } from '@csselectronics/can-pipeline/services/pipeline/storage/FiltFrameStorageService';
8
+ export interface SignalQueryResult {
9
+ /** Timestamps (epoch seconds, or epoch milliseconds when epochToMs is true) */
10
+ times: number[];
11
+ /** Signal values */
12
+ values: number[];
13
+ /**
14
+ * Every in-window sample, not downsampled (only with the `raw` option). Views
15
+ * into reused scratch buffers: read before the next query, never retain.
16
+ */
17
+ raw?: {
18
+ times: Float64Array;
19
+ values: Float64Array;
20
+ };
21
+ }
22
+ /** The one storage read `querySignals` needs; the full and filtered frame stores both provide it. */
23
+ export type SignalQueryStorage = Pick<FiltFrameStorageService, 'getFramesInTimeWindowForBus'>;
24
+ /** Reusable scratch buffers for querySignals() to avoid GC pressure. */
25
+ export interface QueryScratch {
26
+ timestamps: Float64Array[];
27
+ values: Float64Array[];
28
+ counts: Int32Array;
29
+ }
30
+ /**
31
+ * Query multiple signals from a frame store in a single pass per bus.
32
+ *
33
+ * @param storage - Frame store to query (filtered or full)
34
+ * @param signalIds - Signal IDs to extract (e.g. "CAN1_123_SignalName")
35
+ * @param fromEpoch - Start of time window (epoch seconds, inclusive)
36
+ * @param toEpoch - End of time window (epoch seconds, inclusive)
37
+ * @param maxPoints - Downsample threshold (e.g. 4000)
38
+ * @param options.raw - Also return each signal's full-resolution samples as `raw` views
39
+ * @returns Map from signalId to { times, values } (epoch seconds)
40
+ */
41
+ export declare function querySignals(storage: SignalQueryStorage, signalIds: string[], fromEpoch: number, toEpoch: number, maxPoints: number, options?: {
42
+ scratch?: QueryScratch;
43
+ epochToMs?: boolean;
44
+ raw?: boolean;
45
+ }): Map<string, SignalQueryResult>;
@@ -0,0 +1,2 @@
1
+ /*! @csselectronics/can-decode v0.1.0 (c) CSS Electronics ApS, see LICENSE */
2
+ var W=Object.defineProperty;var B=(w,h)=>W(w,"name",{value:h,configurable:!0});import{parseSignalId as k}from"./SignalIdParser.js";import{downsampleMinMax as C}from"./downsampleMinMax.js";const L={times:new Float64Array(0),values:new Float64Array(0)};function Y(w,h,K,R,F,A){const a=A?.scratch,V=A?.epochToMs?1e3:1,S=A?.raw===!0;let N=0;const d=new Map;for(const y of h){const i=k(y);if(!i)continue;let o=d.get(i.busName);o||(o=[],d.set(i.busName,o)),o.push({signalId:y,frameKey:i.frameKey})}const v=new Map;for(const[y,i]of d){const o=w.getFramesInTimeWindowForBus(K,R,y),b=new Map;for(const{signalId:e,frameKey:t}of i){let r=b.get(t);r||(r=[],b.set(t,r)),r.push(e)}if(!o||o.length===0){for(const{signalId:e}of i)v.set(e,S?{times:[],values:[],raw:L}:{times:[],values:[]});continue}const n=i.map(e=>e.signalId),f=Math.min(o.length,131072),I=S?N:0;N+=n.length;let m;const l=new Array(n.length),c=new Array(n.length);if(a){a.counts.length<n.length&&(a.counts=new Int32Array(n.length)),m=a.counts,m.fill(0,0,n.length);for(let e=0;e<n.length;e++){const t=I+e;a.timestamps[t]?.length>=f||(a.timestamps[t]=new Float64Array(f),a.values[t]=new Float64Array(f)),l[e]=a.timestamps[t],c[e]=a.values[t]}}else{m=new Int32Array(n.length);for(let e=0;e<n.length;e++)l[e]=new Float64Array(f),c[e]=new Float64Array(f)}const Q=new Map;for(let e=0;e<n.length;e++)Q.set(n[e],e);for(let e=0;e<o.length;e++){const t=o.get(e),r=b.get(t.key);if(r)for(const g of r){const u=t.getSignalValue(g);if(u===void 0||Number.isNaN(u))continue;const s=Q.get(g);let p=m[s];if(p>=l[s].length){const x=l[s].length*2,M=new Float64Array(x),T=new Float64Array(x);M.set(l[s]),T.set(c[s]),l[s]=M,c[s]=T,a&&(a.timestamps[I+s]=M,a.values[I+s]=T)}l[s][p]=t.epoch*V,c[s][p]=u,m[s]=p+1}}for(let e=0;e<n.length;e++){const t=m[e];let r;if(t===0)r={times:[],values:[]};else if(F>0&&t>F){const[g,u]=C(l[e],c[e],F,t);r={times:g,values:u}}else{const g=new Array(t),u=new Array(t);for(let s=0;s<t;s++)g[s]=l[e][s],u[s]=c[e][s];r={times:g,values:u}}S&&(r.raw={times:l[e].subarray(0,t),values:c[e].subarray(0,t)}),v.set(n[e],r)}}return v}B(Y,"querySignals");export{Y as querySignals};
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Signal value reduction utilities.
3
+ *
4
+ * Computes scalar aggregations (AVG, MIN, MAX, LAST) from a time-series
5
+ * values array. Used by Chart view (AVG display) and Dashboard widgets
6
+ * (Stat/Gauge panels).
7
+ */
8
+ export type ReductionType = 'AVG' | 'MIN' | 'MAX' | 'LAST';
9
+ /**
10
+ * Compute a scalar reduction over a values array.
11
+ *
12
+ * @param values - Signal values (may contain null/undefined gaps)
13
+ * @param calculation - Reduction type
14
+ * @param windowStart - If provided with `timestamps`, only values where
15
+ * `timestamps[i] >= windowStart` are included
16
+ * @param timestamps - Corresponding timestamps (required when windowStart
17
+ * or windowEnd is set)
18
+ * @param windowEnd - If provided with `timestamps`, only values where
19
+ * `timestamps[i] <= windowEnd` are included (excludes
20
+ * query tail-padding past the visible right edge)
21
+ * @returns The computed scalar, or null if no valid values exist
22
+ */
23
+ export declare function computeReduction(values: ArrayLike<number | null | undefined>, calculation: ReductionType, windowStart?: number, timestamps?: ArrayLike<number>, windowEnd?: number): number | null;
@@ -0,0 +1,2 @@
1
+ /*! @csselectronics/can-decode v0.1.0 (c) CSS Electronics ApS, see LICENSE */
2
+ var s=Object.defineProperty;var c=(i,f)=>s(i,"name",{value:f,configurable:!0});function m(i,f,d,l,y){const t=i.length;if(t===0)return null;const A=d!==void 0&&l!==void 0,b=y!==void 0&&l!==void 0,r=c(n=>A&&l[n]<d||b&&l[n]>y,"outsideWindow");switch(f){case"LAST":{for(let n=t-1;n>=0;n--){if(r(n))continue;const e=i[n];if(e!=null)return e}return null}case"AVG":{let n=0,e=0;for(let u=0;u<t;u++){if(r(u))continue;const o=i[u];o!=null&&(n+=o,e++)}return e>0?n/e:null}case"MIN":{let n=1/0;for(let e=0;e<t;e++){if(r(e))continue;const u=i[e];u!=null&&u<n&&(n=u)}return n===1/0?null:n}case"MAX":{let n=-1/0;for(let e=0;e<t;e++){if(r(e))continue;const u=i[e];u!=null&&u>n&&(n=u)}return n===-1/0?null:n}}}c(m,"computeReduction");export{m as computeReduction};