@operato/ops-contract 0.9.48 → 0.9.49
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/README.md +12 -5
- package/dist/asset-acceptance.d.ts +27 -0
- package/dist/asset-acceptance.js +33 -0
- package/dist/capability.d.ts +1 -1
- package/dist/capability.js +16 -1
- package/dist/contract.d.ts +26 -0
- package/dist/contract.js +15 -1
- package/dist/domain-catalog.d.ts +1 -1
- package/dist/domain-catalog.js +26 -2
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/intralogistics-profile.d.ts +80 -0
- package/dist/intralogistics-profile.js +113 -0
- package/dist/route-network.d.ts +84 -0
- package/dist/route-network.js +132 -0
- package/dist/transport-policy.d.ts +79 -0
- package/dist/transport-policy.js +68 -0
- package/dist/travel.d.ts +83 -0
- package/dist/travel.js +32 -0
- package/dist-cjs/index.cjs +385 -4
- package/package.json +1 -1
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { classClosure } from "./contract.js";
|
|
2
|
+
export const CONNECTION_DIRECTION = ['one-way', 'two-way'];
|
|
3
|
+
export const FIXED_ROUTE_BINDING = ['mandatory', 'preferred'];
|
|
4
|
+
/** Default capacity of a connection when none is declared. */
|
|
5
|
+
export const DEFAULT_CONNECTION_CAPACITY = 1;
|
|
6
|
+
export const ROUTE_NETWORK_ISSUE = {
|
|
7
|
+
connectionDangling: 'route-connection-dangling',
|
|
8
|
+
connectionSelf: 'route-connection-self',
|
|
9
|
+
connectionLength: 'route-connection-length',
|
|
10
|
+
connectionSpeed: 'route-connection-speed',
|
|
11
|
+
connectionCapacity: 'route-connection-capacity',
|
|
12
|
+
connectionDirection: 'route-connection-direction',
|
|
13
|
+
connectionDuplicate: 'route-connection-duplicate',
|
|
14
|
+
connectionUnknownClass: 'route-connection-unknown-class',
|
|
15
|
+
fixedEmpty: 'route-fixed-empty',
|
|
16
|
+
fixedUnknownConnection: 'route-fixed-unknown-connection',
|
|
17
|
+
fixedBroken: 'route-fixed-broken',
|
|
18
|
+
fixedClass: 'route-fixed-class',
|
|
19
|
+
fixedBinding: 'route-fixed-binding',
|
|
20
|
+
duplicateId: 'route-duplicate-id'
|
|
21
|
+
};
|
|
22
|
+
const positiveNumber = (v) => typeof v === 'number' && Number.isFinite(v) && v > 0;
|
|
23
|
+
/** The two ends a connection can be entered from, in its allowed directions. */
|
|
24
|
+
function traversals(c) {
|
|
25
|
+
return c.direction === 'two-way' ? [{ from: c.from, to: c.to }, { from: c.to, to: c.from }] : [{ from: c.from, to: c.to }];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Whether equipment of `classIds` may pass `c`. A connection without a class list admits everyone; with one, the equipment's class
|
|
29
|
+
* closure (base classes included) must meet it - an `amr-small` derived from `amr` passes an `amr` connection.
|
|
30
|
+
*/
|
|
31
|
+
function admits(c, classIds, classes) {
|
|
32
|
+
if (!c.equipmentClassIds || c.equipmentClassIds.length === 0)
|
|
33
|
+
return true;
|
|
34
|
+
const allowed = new Set(c.equipmentClassIds);
|
|
35
|
+
return classIds.every(id => [...classClosure([id], classes)].some(k => allowed.has(k)));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Checks the structure of a route network. Returns every issue found (empty = structurally sound).
|
|
39
|
+
*
|
|
40
|
+
* Not checked here: whether a port can be reached, dead ends, deadlock risk. Those need graph search and belong to the deciders
|
|
41
|
+
* (the app's rules, the kernel's execution policy).
|
|
42
|
+
*/
|
|
43
|
+
export function validateRouteNetwork(def) {
|
|
44
|
+
const issues = [];
|
|
45
|
+
const places = new Set([...(def.locations ?? []), ...(def.referencedLocations ?? [])].map(l => l.id));
|
|
46
|
+
const connections = def.connections ?? [];
|
|
47
|
+
const classIds = new Set((def.equipmentClasses ?? []).map(c => c.id));
|
|
48
|
+
const byId = new Map();
|
|
49
|
+
const seenEnds = new Set();
|
|
50
|
+
for (const c of connections) {
|
|
51
|
+
if (byId.has(c.id))
|
|
52
|
+
issues.push({ code: ROUTE_NETWORK_ISSUE.duplicateId, subject: c.id, params: { kind: 'connection' } });
|
|
53
|
+
byId.set(c.id, c);
|
|
54
|
+
for (const end of [c.from, c.to]) {
|
|
55
|
+
if (!places.has(end))
|
|
56
|
+
issues.push({ code: ROUTE_NETWORK_ISSUE.connectionDangling, subject: c.id, params: { location: end } });
|
|
57
|
+
}
|
|
58
|
+
if (c.from === c.to)
|
|
59
|
+
issues.push({ code: ROUTE_NETWORK_ISSUE.connectionSelf, subject: c.id, params: { location: c.from } });
|
|
60
|
+
if (!CONNECTION_DIRECTION.includes(c.direction)) {
|
|
61
|
+
issues.push({ code: ROUTE_NETWORK_ISSUE.connectionDirection, subject: c.id, params: { direction: String(c.direction) } });
|
|
62
|
+
}
|
|
63
|
+
if (!positiveNumber(c.lengthM))
|
|
64
|
+
issues.push({ code: ROUTE_NETWORK_ISSUE.connectionLength, subject: c.id, params: { lengthM: String(c.lengthM) } });
|
|
65
|
+
if (c.maxSpeedMps !== undefined && !positiveNumber(c.maxSpeedMps)) {
|
|
66
|
+
issues.push({ code: ROUTE_NETWORK_ISSUE.connectionSpeed, subject: c.id, params: { maxSpeedMps: String(c.maxSpeedMps) } });
|
|
67
|
+
}
|
|
68
|
+
if (c.capacity !== undefined && !(Number.isInteger(c.capacity) && c.capacity >= 1)) {
|
|
69
|
+
issues.push({ code: ROUTE_NETWORK_ISSUE.connectionCapacity, subject: c.id, params: { capacity: String(c.capacity) } });
|
|
70
|
+
}
|
|
71
|
+
/* A class list may only name declared classes - but only when the twin declares classes at all. A twin without class
|
|
72
|
+
definitions still names classes by id (classClosure takes the ids as they are), so absence of definitions is not an error. */
|
|
73
|
+
if (classIds.size > 0) {
|
|
74
|
+
for (const id of c.equipmentClassIds ?? []) {
|
|
75
|
+
if (!classIds.has(id))
|
|
76
|
+
issues.push({ code: ROUTE_NETWORK_ISSUE.connectionUnknownClass, subject: c.id, params: { equipmentClassId: id } });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/* Two connections over the same ends in the same direction are one path declared twice; a reader would not know which
|
|
80
|
+
length or speed holds. A one-way pair in opposite directions is two paths, not a duplicate. */
|
|
81
|
+
for (const t of traversals(c)) {
|
|
82
|
+
const key = `${t.from}\u0000${t.to}`;
|
|
83
|
+
if (seenEnds.has(key))
|
|
84
|
+
issues.push({ code: ROUTE_NETWORK_ISSUE.connectionDuplicate, subject: c.id, params: { from: t.from, to: t.to } });
|
|
85
|
+
seenEnds.add(key);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const routeIds = new Set();
|
|
89
|
+
for (const r of def.fixedRoutes ?? []) {
|
|
90
|
+
if (routeIds.has(r.id))
|
|
91
|
+
issues.push({ code: ROUTE_NETWORK_ISSUE.duplicateId, subject: r.id, params: { kind: 'fixed-route' } });
|
|
92
|
+
routeIds.add(r.id);
|
|
93
|
+
if (!FIXED_ROUTE_BINDING.includes(r.binding)) {
|
|
94
|
+
issues.push({ code: ROUTE_NETWORK_ISSUE.fixedBinding, subject: r.id, params: { binding: String(r.binding) } });
|
|
95
|
+
}
|
|
96
|
+
if (!r.connectionIds || r.connectionIds.length === 0) {
|
|
97
|
+
issues.push({ code: ROUTE_NETWORK_ISSUE.fixedEmpty, subject: r.id, params: {} });
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const legs = [];
|
|
101
|
+
for (const id of r.connectionIds) {
|
|
102
|
+
const c = byId.get(id);
|
|
103
|
+
if (!c)
|
|
104
|
+
issues.push({ code: ROUTE_NETWORK_ISSUE.fixedUnknownConnection, subject: r.id, params: { connection: id } });
|
|
105
|
+
else
|
|
106
|
+
legs.push(c);
|
|
107
|
+
}
|
|
108
|
+
if (legs.length !== r.connectionIds.length)
|
|
109
|
+
continue;
|
|
110
|
+
/* Walk the route: the first leg may be entered from either end it allows; every later leg must be entered where the
|
|
111
|
+
previous one left off, in a direction it allows. */
|
|
112
|
+
let at = new Set(traversals(legs[0]).map(t => t.to));
|
|
113
|
+
for (let i = 1; i < legs.length; i++) {
|
|
114
|
+
const next = new Set(traversals(legs[i]).filter(t => at.has(t.from)).map(t => t.to));
|
|
115
|
+
if (next.size === 0) {
|
|
116
|
+
issues.push({ code: ROUTE_NETWORK_ISSUE.fixedBroken, subject: r.id, params: { after: legs[i - 1].id, connection: legs[i].id } });
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
at = next;
|
|
120
|
+
}
|
|
121
|
+
for (const classId of r.equipmentClassIds ?? []) {
|
|
122
|
+
const blocked = legs.find(c => !admits(c, [classId], def.equipmentClasses));
|
|
123
|
+
if (blocked)
|
|
124
|
+
issues.push({ code: ROUTE_NETWORK_ISSUE.fixedClass, subject: r.id, params: { equipmentClassId: classId, connection: blocked.id } });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return issues;
|
|
128
|
+
}
|
|
129
|
+
/** Whether equipment of these classes may pass this connection (class closure included). */
|
|
130
|
+
export function connectionAdmits(c, equipmentClassIds, classes) {
|
|
131
|
+
return admits(c, equipmentClassIds, classes);
|
|
132
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { RefusalParams } from './contract.ts';
|
|
2
|
+
export type PolicyParams = Record<string, number | string | boolean>;
|
|
3
|
+
/** Which machine takes which job. */
|
|
4
|
+
export declare const DISPATCH_RULE: {
|
|
5
|
+
/** The first available machine in the kernel's own order - what its assignment does today. */
|
|
6
|
+
readonly firstIdle: "firstIdle";
|
|
7
|
+
/** The machine that reaches the pick-up soonest; ties by id. */
|
|
8
|
+
readonly minEta: "minEta";
|
|
9
|
+
};
|
|
10
|
+
export type DispatchRuleName = (typeof DISPATCH_RULE)[keyof typeof DISPATCH_RULE];
|
|
11
|
+
/** Which way it goes. */
|
|
12
|
+
export declare const ROUTING_RULE: {
|
|
13
|
+
/** Least travel time, reservations not considered. Connection time = length / effective speed. */
|
|
14
|
+
readonly shortestTime: "shortestTime";
|
|
15
|
+
/** Least arrival time over time windows: a connection held past its capacity is waited for or avoided. */
|
|
16
|
+
readonly timeWindow: "timeWindow";
|
|
17
|
+
};
|
|
18
|
+
export type RoutingRuleName = (typeof ROUTING_RULE)[keyof typeof ROUTING_RULE];
|
|
19
|
+
export interface TransportPolicySet {
|
|
20
|
+
id: string;
|
|
21
|
+
/** A published set is never edited; a change is a new version. */
|
|
22
|
+
version: string;
|
|
23
|
+
/** Which machine takes which job. */
|
|
24
|
+
dispatch: {
|
|
25
|
+
rule: DispatchRuleName;
|
|
26
|
+
params?: PolicyParams;
|
|
27
|
+
};
|
|
28
|
+
/** Which way it goes. */
|
|
29
|
+
routing: {
|
|
30
|
+
rule: RoutingRuleName;
|
|
31
|
+
params?: PolicyParams;
|
|
32
|
+
};
|
|
33
|
+
traffic: {
|
|
34
|
+
/** How far ahead connections are reserved, seconds. */
|
|
35
|
+
horizonS?: number;
|
|
36
|
+
/** What a machine does when its next connection is taken. */
|
|
37
|
+
onConflict: TrafficConflictAction;
|
|
38
|
+
};
|
|
39
|
+
charging: {
|
|
40
|
+
/** Go charge below this state of charge, percent. */
|
|
41
|
+
lowSoc: number;
|
|
42
|
+
/** Charge up to this, percent. */
|
|
43
|
+
targetSoc: number;
|
|
44
|
+
/** Charge while idle even above `lowSoc`. */
|
|
45
|
+
opportunistic?: boolean;
|
|
46
|
+
/** Time-of-use periods to avoid - period ids of the ops-master tariff (`tariff.ts`). */
|
|
47
|
+
avoidTariffPeriodIds?: string[];
|
|
48
|
+
};
|
|
49
|
+
/** Where to go instead when a destination is unavailable (equipment down, port full). In order of preference. */
|
|
50
|
+
alternates?: TransportAlternate[];
|
|
51
|
+
}
|
|
52
|
+
export declare const TRAFFIC_CONFLICT_ACTION: readonly ["wait", "replan", "yield"];
|
|
53
|
+
export type TrafficConflictAction = (typeof TRAFFIC_CONFLICT_ACTION)[number];
|
|
54
|
+
export interface TransportAlternate {
|
|
55
|
+
/** `locations.id` */
|
|
56
|
+
destination: string;
|
|
57
|
+
alternatives: string[];
|
|
58
|
+
}
|
|
59
|
+
export declare const TRANSPORT_POLICY_ISSUE: {
|
|
60
|
+
readonly ruleMissing: "policy-rule-missing";
|
|
61
|
+
readonly ruleUnknown: "policy-rule-unknown";
|
|
62
|
+
readonly conflictAction: "policy-conflict-action";
|
|
63
|
+
readonly horizon: "policy-horizon";
|
|
64
|
+
readonly soc: "policy-soc";
|
|
65
|
+
readonly alternateUnknown: "policy-alternate-unknown";
|
|
66
|
+
readonly alternateSelf: "policy-alternate-self";
|
|
67
|
+
readonly alternateDuplicate: "policy-alternate-duplicate";
|
|
68
|
+
};
|
|
69
|
+
export type TransportPolicyIssueCode = (typeof TRANSPORT_POLICY_ISSUE)[keyof typeof TRANSPORT_POLICY_ISSUE];
|
|
70
|
+
export interface TransportPolicyIssue {
|
|
71
|
+
code: TransportPolicyIssueCode;
|
|
72
|
+
params: RefusalParams;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Checks the shape of a policy set, rule names included (closed lists).
|
|
76
|
+
*
|
|
77
|
+
* `locationIds`, when given, are the places alternates may name; without it, alternates are not checked against places.
|
|
78
|
+
*/
|
|
79
|
+
export declare function validateTransportPolicy(policy: TransportPolicySet, locationIds?: ReadonlySet<string>): TransportPolicyIssue[];
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/** Which machine takes which job. */
|
|
2
|
+
export const DISPATCH_RULE = {
|
|
3
|
+
/** The first available machine in the kernel's own order - what its assignment does today. */
|
|
4
|
+
firstIdle: 'firstIdle',
|
|
5
|
+
/** The machine that reaches the pick-up soonest; ties by id. */
|
|
6
|
+
minEta: 'minEta'
|
|
7
|
+
};
|
|
8
|
+
/** Which way it goes. */
|
|
9
|
+
export const ROUTING_RULE = {
|
|
10
|
+
/** Least travel time, reservations not considered. Connection time = length / effective speed. */
|
|
11
|
+
shortestTime: 'shortestTime',
|
|
12
|
+
/** Least arrival time over time windows: a connection held past its capacity is waited for or avoided. */
|
|
13
|
+
timeWindow: 'timeWindow'
|
|
14
|
+
};
|
|
15
|
+
export const TRAFFIC_CONFLICT_ACTION = ['wait', 'replan', 'yield'];
|
|
16
|
+
export const TRANSPORT_POLICY_ISSUE = {
|
|
17
|
+
ruleMissing: 'policy-rule-missing',
|
|
18
|
+
ruleUnknown: 'policy-rule-unknown',
|
|
19
|
+
conflictAction: 'policy-conflict-action',
|
|
20
|
+
horizon: 'policy-horizon',
|
|
21
|
+
soc: 'policy-soc',
|
|
22
|
+
alternateUnknown: 'policy-alternate-unknown',
|
|
23
|
+
alternateSelf: 'policy-alternate-self',
|
|
24
|
+
alternateDuplicate: 'policy-alternate-duplicate'
|
|
25
|
+
};
|
|
26
|
+
const isPercent = (v) => typeof v === 'number' && Number.isFinite(v) && v >= 0 && v <= 100;
|
|
27
|
+
/**
|
|
28
|
+
* Checks the shape of a policy set, rule names included (closed lists).
|
|
29
|
+
*
|
|
30
|
+
* `locationIds`, when given, are the places alternates may name; without it, alternates are not checked against places.
|
|
31
|
+
*/
|
|
32
|
+
export function validateTransportPolicy(policy, locationIds) {
|
|
33
|
+
const issues = [];
|
|
34
|
+
const known = { dispatch: Object.values(DISPATCH_RULE), routing: Object.values(ROUTING_RULE) };
|
|
35
|
+
for (const part of ['dispatch', 'routing']) {
|
|
36
|
+
const rule = policy[part]?.rule;
|
|
37
|
+
if (typeof rule !== 'string' || rule.trim() === '')
|
|
38
|
+
issues.push({ code: TRANSPORT_POLICY_ISSUE.ruleMissing, params: { part } });
|
|
39
|
+
else if (!known[part].includes(rule))
|
|
40
|
+
issues.push({ code: TRANSPORT_POLICY_ISSUE.ruleUnknown, params: { part, rule } });
|
|
41
|
+
}
|
|
42
|
+
if (!TRAFFIC_CONFLICT_ACTION.includes(policy.traffic?.onConflict)) {
|
|
43
|
+
issues.push({ code: TRANSPORT_POLICY_ISSUE.conflictAction, params: { onConflict: String(policy.traffic?.onConflict) } });
|
|
44
|
+
}
|
|
45
|
+
const horizon = policy.traffic?.horizonS;
|
|
46
|
+
if (horizon !== undefined && !(typeof horizon === 'number' && Number.isFinite(horizon) && horizon > 0)) {
|
|
47
|
+
issues.push({ code: TRANSPORT_POLICY_ISSUE.horizon, params: { horizonS: String(horizon) } });
|
|
48
|
+
}
|
|
49
|
+
const { lowSoc, targetSoc } = policy.charging ?? {};
|
|
50
|
+
if (!isPercent(lowSoc) || !isPercent(targetSoc) || lowSoc >= targetSoc) {
|
|
51
|
+
issues.push({ code: TRANSPORT_POLICY_ISSUE.soc, params: { lowSoc: String(lowSoc), targetSoc: String(targetSoc) } });
|
|
52
|
+
}
|
|
53
|
+
const seen = new Set();
|
|
54
|
+
for (const a of policy.alternates ?? []) {
|
|
55
|
+
if (seen.has(a.destination))
|
|
56
|
+
issues.push({ code: TRANSPORT_POLICY_ISSUE.alternateDuplicate, params: { destination: a.destination } });
|
|
57
|
+
seen.add(a.destination);
|
|
58
|
+
if (a.alternatives.includes(a.destination))
|
|
59
|
+
issues.push({ code: TRANSPORT_POLICY_ISSUE.alternateSelf, params: { destination: a.destination } });
|
|
60
|
+
if (locationIds) {
|
|
61
|
+
for (const id of [a.destination, ...a.alternatives]) {
|
|
62
|
+
if (!locationIds.has(id))
|
|
63
|
+
issues.push({ code: TRANSPORT_POLICY_ISSUE.alternateUnknown, params: { location: id } });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return issues;
|
|
68
|
+
}
|
package/dist/travel.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { ISOTime } from './contract.ts';
|
|
2
|
+
export interface TravelState {
|
|
3
|
+
/** The last location on the route network this equipment passed. */
|
|
4
|
+
lastPassed?: string;
|
|
5
|
+
/** Heading in degrees, 0 <= h < 360, in the published network edition's frame. */
|
|
6
|
+
heading?: number;
|
|
7
|
+
/** Connections reserved ahead, in the order they will be travelled. */
|
|
8
|
+
reservedConnectionIds?: string[];
|
|
9
|
+
link?: TravelLink;
|
|
10
|
+
/** A position the equipment itself reported. Absent in simulation. */
|
|
11
|
+
pose?: ObservedPose;
|
|
12
|
+
}
|
|
13
|
+
export declare const TRAVEL_LINK_STATE: readonly ["online", "stale", "lost"];
|
|
14
|
+
export type TravelLinkState = (typeof TRAVEL_LINK_STATE)[number];
|
|
15
|
+
/** Who said the link state: the source itself, or a judgement from how long it has been silent. */
|
|
16
|
+
export declare const TRAVEL_LINK_JUDGED_BY: readonly ["source", "derived"];
|
|
17
|
+
export type TravelLinkJudgedBy = (typeof TRAVEL_LINK_JUDGED_BY)[number];
|
|
18
|
+
export interface TravelLink {
|
|
19
|
+
state: TravelLinkState;
|
|
20
|
+
/** When anything was last received from this equipment. */
|
|
21
|
+
lastSeenAt: ISOTime;
|
|
22
|
+
/** Where it was then. */
|
|
23
|
+
lastLocation?: string;
|
|
24
|
+
judgedBy: TravelLinkJudgedBy;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A measured position, as the equipment reported it - material for the twin's position correction.
|
|
28
|
+
*
|
|
29
|
+
* This does not break the rule that geometry stays out of the contract: the contract computes nothing from coordinates. It only
|
|
30
|
+
* carries what a source observed, tagged with the network edition whose map frame the numbers are in. Poses from different
|
|
31
|
+
* editions are not compared.
|
|
32
|
+
*/
|
|
33
|
+
export interface ObservedPose {
|
|
34
|
+
/** Metres. */
|
|
35
|
+
x: number;
|
|
36
|
+
/** Metres. */
|
|
37
|
+
y: number;
|
|
38
|
+
/** Radians. */
|
|
39
|
+
theta: number;
|
|
40
|
+
networkVersion: string;
|
|
41
|
+
at: ISOTime;
|
|
42
|
+
/** Position uncertainty the source reported, metres. */
|
|
43
|
+
deviationM?: number;
|
|
44
|
+
}
|
|
45
|
+
/** The equipment passed a location of the route network. Carries `lastPassed` and `heading`. */
|
|
46
|
+
export interface TravelPassedFact {
|
|
47
|
+
equipmentId: string;
|
|
48
|
+
location: string;
|
|
49
|
+
heading?: number;
|
|
50
|
+
}
|
|
51
|
+
/** The connections now reserved ahead - the whole list, replacing the previous one. An empty list releases all. */
|
|
52
|
+
export interface TravelReservationFact {
|
|
53
|
+
equipmentId: string;
|
|
54
|
+
connectionIds: string[];
|
|
55
|
+
}
|
|
56
|
+
/** The link state changed. */
|
|
57
|
+
export interface TravelLinkFact {
|
|
58
|
+
equipmentId: string;
|
|
59
|
+
link: TravelLink;
|
|
60
|
+
}
|
|
61
|
+
/** A pose the equipment reported. */
|
|
62
|
+
export interface TravelPoseFact {
|
|
63
|
+
equipmentId: string;
|
|
64
|
+
pose: ObservedPose;
|
|
65
|
+
}
|
|
66
|
+
export interface TravelLinkThresholds {
|
|
67
|
+
/** Silent longer than this: `stale`. */
|
|
68
|
+
staleAfterMs: number;
|
|
69
|
+
/** Silent longer than this: `lost`. Must be >= staleAfterMs. */
|
|
70
|
+
lostAfterMs: number;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The link state at `nowTime`.
|
|
74
|
+
*
|
|
75
|
+
* A state the source gave (`judgedBy: 'source'`) is returned as it is - a judgement never overrides what the source said. Otherwise
|
|
76
|
+
* the state is judged from how long the equipment has been silent and marked `derived`.
|
|
77
|
+
*
|
|
78
|
+
* Returns `undefined` when it cannot judge: no `nowTime` (the twin's own clock - a consumer that substitutes wall-clock time gets
|
|
79
|
+
* nonsense in a simulated twin), an unreadable time, or thresholds that do not make sense.
|
|
80
|
+
*/
|
|
81
|
+
export declare function linkStateAt(link: TravelLink, nowTime: ISOTime | undefined, thresholds: TravelLinkThresholds): TravelLink | undefined;
|
|
82
|
+
/** Whether a heading is a number in [0, 360). */
|
|
83
|
+
export declare function isHeading(v: unknown): v is number;
|
package/dist/travel.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export const TRAVEL_LINK_STATE = ['online', 'stale', 'lost'];
|
|
2
|
+
/** Who said the link state: the source itself, or a judgement from how long it has been silent. */
|
|
3
|
+
export const TRAVEL_LINK_JUDGED_BY = ['source', 'derived'];
|
|
4
|
+
/**
|
|
5
|
+
* The link state at `nowTime`.
|
|
6
|
+
*
|
|
7
|
+
* A state the source gave (`judgedBy: 'source'`) is returned as it is - a judgement never overrides what the source said. Otherwise
|
|
8
|
+
* the state is judged from how long the equipment has been silent and marked `derived`.
|
|
9
|
+
*
|
|
10
|
+
* Returns `undefined` when it cannot judge: no `nowTime` (the twin's own clock - a consumer that substitutes wall-clock time gets
|
|
11
|
+
* nonsense in a simulated twin), an unreadable time, or thresholds that do not make sense.
|
|
12
|
+
*/
|
|
13
|
+
export function linkStateAt(link, nowTime, thresholds) {
|
|
14
|
+
if (link.judgedBy === 'source')
|
|
15
|
+
return link;
|
|
16
|
+
if (!nowTime)
|
|
17
|
+
return undefined;
|
|
18
|
+
const now = Date.parse(nowTime);
|
|
19
|
+
const seen = Date.parse(link.lastSeenAt);
|
|
20
|
+
const { staleAfterMs, lostAfterMs } = thresholds;
|
|
21
|
+
if (!Number.isFinite(now) || !Number.isFinite(seen))
|
|
22
|
+
return undefined;
|
|
23
|
+
if (!(staleAfterMs >= 0 && lostAfterMs >= staleAfterMs))
|
|
24
|
+
return undefined;
|
|
25
|
+
const silentMs = now - seen;
|
|
26
|
+
const state = silentMs > lostAfterMs ? 'lost' : silentMs > staleAfterMs ? 'stale' : 'online';
|
|
27
|
+
return { ...link, state, judgedBy: 'derived' };
|
|
28
|
+
}
|
|
29
|
+
/** Whether a heading is a number in [0, 360). */
|
|
30
|
+
export function isHeading(v) {
|
|
31
|
+
return typeof v === 'number' && Number.isFinite(v) && v >= 0 && v < 360;
|
|
32
|
+
}
|