@mark1russell7/electrical 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.
- package/LICENSE +21 -0
- package/README.md +156 -2
- package/build/add-on.d.ts +11 -0
- package/build/add-on.d.ts.map +1 -0
- package/build/add-on.js +22 -0
- package/build/add-on.js.map +1 -0
- package/build/check-V0Af5oUe.js +329 -0
- package/build/check-V0Af5oUe.js.map +1 -0
- package/build/index.d.ts +68 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +77 -0
- package/build/index.js.map +1 -0
- package/build/layer-BFIDp4a1.js +6 -0
- package/build/layer-BFIDp4a1.js.map +1 -0
- package/build/layer-BxRcnHeC.d.ts +51 -0
- package/build/layer-BxRcnHeC.d.ts.map +1 -0
- package/build/schema.d.ts +14 -0
- package/build/schema.d.ts.map +1 -0
- package/build/schema.js +55 -0
- package/build/schema.js.map +1 -0
- package/build/states-7rPTIEPc.d.ts +126 -0
- package/build/states-7rPTIEPc.d.ts.map +1 -0
- package/package.json +74 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mark Russell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,157 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mark1russell7/electrical
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Electrical circuits as data: outlets, circuits, feeds and loads. The package has the format of an electrical layer, its checks and the circuit formulas.
|
|
4
|
+
|
|
5
|
+
It is the electrical add-on of [`@mark1russell7/room-view`](../room-view/README.md). The core of room-view knows only space. This package puts the electrical records on the anchors of the core.
|
|
6
|
+
|
|
7
|
+
The package has no browser part. Each function is a plain function, and each formula is a Vex program.
|
|
8
|
+
|
|
9
|
+
## The entries
|
|
10
|
+
|
|
11
|
+
| Entry | What it has |
|
|
12
|
+
| --- | --- |
|
|
13
|
+
| `@mark1russell7/electrical` | The records of a layer, the circuit formulas, the states, the checks and the move of an old scene. |
|
|
14
|
+
| `@mark1russell7/electrical/schema` | `electricalSchema`, the zod schema of a layer, and `parseElectrical`. |
|
|
15
|
+
| `@mark1russell7/electrical/add-on` | `electrical`, the add-on value for the contract of the core. |
|
|
16
|
+
|
|
17
|
+
The root entry imports only Vex at run time. Thus a page that shows only the load of a circuit gets no schema library.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npm install @mark1russell7/electrical
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## A layer
|
|
26
|
+
|
|
27
|
+
An electrical layer is one record with three lists. A scene keeps it in `Scene.layers`, with the key `electrical/1`.
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { circuitStates, type ElectricalLayer } from "@mark1russell7/electrical";
|
|
31
|
+
|
|
32
|
+
const layer: ElectricalLayer = {
|
|
33
|
+
schema: "electrical/1",
|
|
34
|
+
circuits: [{ id: "office", breaker: 15, voltage: 120 }],
|
|
35
|
+
outlets: [
|
|
36
|
+
{ id: "north-1", anchor: { on: "wall", wall: "north", face: "left", offset: 1.2, height: 0.3 }, circuit: "office" },
|
|
37
|
+
{ id: "strip", anchor: { on: "object", object: "desk" }, circuit: "office" },
|
|
38
|
+
],
|
|
39
|
+
feeds: [
|
|
40
|
+
{ id: "pc", object: "tower", outlet: "strip", watts: 450 },
|
|
41
|
+
{ id: "screen", object: "monitor", outlet: "strip", watts: 60 },
|
|
42
|
+
{ id: "printer", object: "printer", outlet: "north-1" },
|
|
43
|
+
],
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
circuitStates(layer);
|
|
47
|
+
// => [{ circuit: "office", watts: 510, limit: 1440, ratio: 0.354, level: "ok", missing: ["printer"],
|
|
48
|
+
// outlets: ["north-1", "strip"], feeds: ["pc", "screen", "printer"], objects: ["tower", "monitor", "printer"] }]
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
- An **outlet** is a point that gives power, on an anchor of the core: a wall, a floor, a ceiling or a placed object.
|
|
52
|
+
- A **circuit** gives power to outlets. It can have the rating of its breaker, its voltage, a limit and a load that no feed gives.
|
|
53
|
+
- A **feed** is the power of a placed object from an outlet. An object with two power supplies has two feeds.
|
|
54
|
+
|
|
55
|
+
The units are fixed: a breaker in amperes, a voltage in volts, and a limit and a load in watts. The ids of one layer are unique, so `recordName("electrical/1", id)` of the core names one record.
|
|
56
|
+
|
|
57
|
+
`parseElectrical` of the entry `/schema` reads the JSON of a layer. It gives the layer, or the faults of its form with the place of each fault. `electricalSchema` is the zod schema.
|
|
58
|
+
|
|
59
|
+
## The limit and the level of a circuit
|
|
60
|
+
|
|
61
|
+
A continuous load can use only a part of the rating of a breaker: 80% by default. Thus a breaker of 15 A at 120 V has a limit of 1440 W.
|
|
62
|
+
|
|
63
|
+
- The limit of a circuit is its field `limit`. Without it, the limit is the voltage times the breaker times the continuous part.
|
|
64
|
+
- The known load of a circuit is the sum of the watts of its feeds, plus its `otherLoad`.
|
|
65
|
+
- A feed with no watts adds nothing to the load. The state of the circuit names it in `missing`, because the true load can be above the known load.
|
|
66
|
+
|
|
67
|
+
| Level | Meaning |
|
|
68
|
+
| --- | --- |
|
|
69
|
+
| `unknown` | No load of the circuit has watts, or the circuit has no limit above 0. |
|
|
70
|
+
| `ok` | The known load is below the near part of the limit. |
|
|
71
|
+
| `near` | The known load is the near part of the limit, or more. The default near part is 80%. |
|
|
72
|
+
| `over` | The known load is above the limit. |
|
|
73
|
+
|
|
74
|
+
`circuitStates` gives the state of each circuit, and `outletLevels` gives each outlet the level of its circuit. The options `continuousLoad` and `near` change the two parts.
|
|
75
|
+
|
|
76
|
+
## The add-on
|
|
77
|
+
|
|
78
|
+
`electrical` is the add-on value for the contract of the core. `checkLayers` of the core examines the form of the layer, each anchor and the meaning of the records:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { electrical } from "@mark1russell7/electrical/add-on";
|
|
82
|
+
import { checkLayers, readLayer } from "@mark1russell7/room-view";
|
|
83
|
+
|
|
84
|
+
const findings = checkLayers(scene, [electrical]);
|
|
85
|
+
const read = readLayer(scene, electrical);
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`electricalAddOn(options)` makes the add-on with different parts. `checkElectrical` of the root entry gives the faults of the meaning of one layer:
|
|
89
|
+
|
|
90
|
+
| Code | Level | The fault |
|
|
91
|
+
| --- | --- | --- |
|
|
92
|
+
| `id-duplicate` | error | Two records of the layer have one `id`. |
|
|
93
|
+
| `outlet-circuit` | error | An outlet is on a circuit that the layer does not have. |
|
|
94
|
+
| `feed-outlet` | error | A feed is from an outlet that the layer does not have. |
|
|
95
|
+
| `feed-object` | error | A feed gives power to an object that the scene does not have. |
|
|
96
|
+
| `feed-watts` | error | The watts of a feed are below 0, or they are not finite. |
|
|
97
|
+
| `circuit-number` | error | A breaker, a voltage or a limit is not above 0, or an `otherLoad` is below 0. |
|
|
98
|
+
| `circuit-no-limit` | warning | A circuit has no limit, and it does not have a breaker and a voltage. |
|
|
99
|
+
| `circuit-limits` | warning | The field `limit` of a circuit and its rated limit are not equal. |
|
|
100
|
+
| `circuit-over` | warning | The known load of a circuit is above its limit. |
|
|
101
|
+
|
|
102
|
+
## The formulas
|
|
103
|
+
|
|
104
|
+
The circuit math is Vex programs, so a program is data. `explain` of Vex shows each step, and a host can keep a program as JSON.
|
|
105
|
+
|
|
106
|
+
Four functions build general programs. They read no field, so a model with different records uses them with its own field names:
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
import { loadVars, ratedLimit } from "@mark1russell7/electrical";
|
|
110
|
+
import { evaluate, ref, space } from "@mark1russell7/vex";
|
|
111
|
+
import { NumDomain } from "@mark1russell7/vex-domains";
|
|
112
|
+
|
|
113
|
+
const supplies = space.record({ rack: { volts: 240, amps: 30 } });
|
|
114
|
+
evaluate(ratedLimit(ref("volts"), ref("amps")), { space: supplies, origin: "rack", domains: [NumDomain], vars: loadVars() });
|
|
115
|
+
// => { ok: true, value: 5760 }
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
| Function | The program |
|
|
119
|
+
| --- | --- |
|
|
120
|
+
| `ratedLimit(voltage, breaker)` | The limit of a continuous load, in watts. |
|
|
121
|
+
| `loadCurrent(load, voltage)` | The current of a load, in amperes. |
|
|
122
|
+
| `loadRatio(load, limit)` | A load as a part of its limit. A limit that is not above 0 gives an error value. |
|
|
123
|
+
| `ratioLevel(ratio)` | The level of a ratio: `over`, `near` or `ok`. |
|
|
124
|
+
|
|
125
|
+
`electricalSpace` makes a tree of a layer: a circuit, then its outlets and its other load, then the feeds of each outlet. The programs of a circuit evaluate at its node, and `evaluateCircuit` evaluates one of them:
|
|
126
|
+
|
|
127
|
+
| Program | The value at a circuit |
|
|
128
|
+
| --- | --- |
|
|
129
|
+
| `CIRCUIT_LIMIT` | The limit, in watts. |
|
|
130
|
+
| `RATED_LIMIT` | The limit from the breaker and the voltage, in watts. |
|
|
131
|
+
| `CIRCUIT_LOAD` | The known load, in watts. |
|
|
132
|
+
| `KNOWN_LOADS` | The count of the loads with watts. |
|
|
133
|
+
| `MISSING_LOADS` | The keys of the feeds with no watts. |
|
|
134
|
+
| `CIRCUIT_RATIO` | The known load as a part of the limit. |
|
|
135
|
+
| `CIRCUIT_CURRENT` | The current of the known load, in amperes. |
|
|
136
|
+
| `CIRCUIT_LEVEL` | The level. |
|
|
137
|
+
| `LIMITS_DIFFER` | `true` when the field `limit` and the rated limit are not equal. |
|
|
138
|
+
|
|
139
|
+
## From a `scene/1`
|
|
140
|
+
|
|
141
|
+
A `scene/1` of room-view keeps outlets and circuits in its room, and the total load of each circuit. `fromSceneV1` moves these records to a layer:
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
import { fromSceneV1 } from "@mark1russell7/electrical";
|
|
145
|
+
import { SAMPLE_SCENE } from "@mark1russell7/room-view/sample";
|
|
146
|
+
|
|
147
|
+
const layer = fromSceneV1(SAMPLE_SCENE);
|
|
148
|
+
const scene = { ...SAMPLE_SCENE, layers: { "electrical/1": layer } };
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
- An outlet keeps its `id`, and its place on the wall becomes a wall anchor.
|
|
152
|
+
- The sum of the loads of a circuit becomes its `otherLoad`. Thus the circuit keeps its level.
|
|
153
|
+
- The outlet of an object becomes a feed with no watts, because a `scene/1` does not have the power of each object.
|
|
154
|
+
|
|
155
|
+
## License
|
|
156
|
+
|
|
157
|
+
MIT
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { r as ElectricalLayer } from "./layer-BxRcnHeC.js";
|
|
2
|
+
import { n as LoadOptions } from "./states-7rPTIEPc.js";
|
|
3
|
+
import { AddOn } from "@mark1russell7/room-view";
|
|
4
|
+
/**
|
|
5
|
+
* This function makes the electrical add-on with options of the limits and the levels.
|
|
6
|
+
* The checks of the add-on use the options, for example a different continuous part.
|
|
7
|
+
*/
|
|
8
|
+
export declare function electricalAddOn(options?: LoadOptions): AddOn<ElectricalLayer>;
|
|
9
|
+
/** The electrical add-on with the default options: a continuous part of 80%, and the level `near` from 80% of a limit. */
|
|
10
|
+
export declare const electrical: AddOn<ElectricalLayer>;
|
|
11
|
+
//# sourceMappingURL=add-on.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"add-on.d.ts","names":[],"sources":["../src/add-on.ts"],"mappings":";;;;;;;wBAcgB,gBAAgB,UAAS,cAAmB,MAAM;;qBAWrD,YAAY,MAAM"}
|
package/build/add-on.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { A as withOutletAnchors, k as outletAnchors, n as checkElectrical } from "./check-V0Af5oUe.js";
|
|
2
|
+
import { t as ELECTRICAL_SCHEMA } from "./layer-BFIDp4a1.js";
|
|
3
|
+
import { parseElectrical } from "./schema.js";
|
|
4
|
+
// src/add-on.ts
|
|
5
|
+
/**
|
|
6
|
+
* This function makes the electrical add-on with options of the limits and the levels.
|
|
7
|
+
* The checks of the add-on use the options, for example a different continuous part.
|
|
8
|
+
*/
|
|
9
|
+
function electricalAddOn(options = {}) {
|
|
10
|
+
return {
|
|
11
|
+
schema: ELECTRICAL_SCHEMA,
|
|
12
|
+
parse: parseElectrical,
|
|
13
|
+
anchors: outletAnchors,
|
|
14
|
+
withAnchors: withOutletAnchors,
|
|
15
|
+
check: (layer, space) => checkElectrical(layer, space, options)
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/** The electrical add-on with the default options: a continuous part of 80%, and the level `near` from 80% of a limit. */
|
|
19
|
+
const electrical = electricalAddOn();
|
|
20
|
+
export { electrical, electricalAddOn };
|
|
21
|
+
|
|
22
|
+
//# sourceMappingURL=add-on.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"add-on.js","names":[],"sources":["../src/add-on.ts"],"sourcesContent":["import type { AddOn } from \"@mark1russell7/room-view\";\nimport { outletAnchors, withOutletAnchors } from \"./anchors.ts\";\nimport { checkElectrical } from \"./check.ts\";\nimport { ELECTRICAL_SCHEMA, type ElectricalLayer } from \"./layer.ts\";\nimport { parseElectrical } from \"./schema.ts\";\nimport type { LoadOptions } from \"./states.ts\";\n\n// The electrical add-on, as a value of the contract of the core. A host gives it to `checkLayers` and `readLayer`.\n// This module is the entry `/add-on` of the package. It reads a layer with the zod schema, so it is not in the root entry.\n\n/**\n * This function makes the electrical add-on with options of the limits and the levels.\n * The checks of the add-on use the options, for example a different continuous part.\n */\nexport function electricalAddOn(options: LoadOptions = {}): AddOn<ElectricalLayer> {\n return {\n schema: ELECTRICAL_SCHEMA,\n parse: parseElectrical,\n anchors: outletAnchors,\n withAnchors: withOutletAnchors,\n check: (layer, space) => checkElectrical(layer, space, options),\n };\n}\n\n/** The electrical add-on with the default options: a continuous part of 80%, and the level `near` from 80% of a limit. */\nexport const electrical: AddOn<ElectricalLayer> = electricalAddOn();\n"],"mappings":";;;;;;;;AAcA,SAAgB,gBAAgB,UAAuB,CAAC,GAA2B;CACjF,OAAO;EACL,QAAQ;EACR,OAAO;EACP,SAAS;EACT,aAAa;EACb,QAAQ,OAAO,UAAU,gBAAgB,OAAO,OAAO,OAAO;CAChE;AACF;;AAGA,MAAa,aAAqC,gBAAgB"}
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { app, axes, each, evaluate, let_, lit, ref, space, v } from "@mark1russell7/vex";
|
|
2
|
+
import { BoolDomain, NumDomain, TextDomain } from "@mark1russell7/vex-domains";
|
|
3
|
+
// src/anchors.ts
|
|
4
|
+
/** This function gives the anchor of each outlet of a layer, by the `id` of the outlet. The first outlet of an `id` is used. */
|
|
5
|
+
function outletAnchors(layer) {
|
|
6
|
+
const anchors = /* @__PURE__ */ new Map();
|
|
7
|
+
for (const outlet of layer.outlets ?? []) if (!anchors.has(outlet.id)) anchors.set(outlet.id, outlet.anchor);
|
|
8
|
+
return anchors;
|
|
9
|
+
}
|
|
10
|
+
/** This function gives a layer with new anchors of its outlets, by the `id` of each outlet. An outlet with no new anchor keeps its anchor. */
|
|
11
|
+
function withOutletAnchors(layer, anchors) {
|
|
12
|
+
if (layer.outlets === void 0) return layer;
|
|
13
|
+
return {
|
|
14
|
+
...layer,
|
|
15
|
+
outlets: layer.outlets.map((outlet) => ({
|
|
16
|
+
...outlet,
|
|
17
|
+
anchor: anchors.get(outlet.id) ?? outlet.anchor
|
|
18
|
+
}))
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
// src/formulas.ts
|
|
22
|
+
/** The name of the variable with the continuous part: the part of the rating of a breaker that a continuous load can use. */
|
|
23
|
+
const CONTINUOUS_LOAD = "continuousLoad";
|
|
24
|
+
/** The default continuous part. A continuous load can use 80% of the rating of a breaker. */
|
|
25
|
+
const DEFAULT_CONTINUOUS_LOAD = .8;
|
|
26
|
+
/** The name of the variable with the near part: a load with this part of its limit, or more, has the level `near`. */
|
|
27
|
+
const NEAR_LOAD = "nearLoad";
|
|
28
|
+
/** The default near part. */
|
|
29
|
+
const DEFAULT_NEAR_LOAD = .8;
|
|
30
|
+
/** These are the levels of the load of a circuit. */
|
|
31
|
+
const LOAD_LEVELS = [
|
|
32
|
+
"unknown",
|
|
33
|
+
"ok",
|
|
34
|
+
"near",
|
|
35
|
+
"over"
|
|
36
|
+
];
|
|
37
|
+
/**
|
|
38
|
+
* This function gives the program of the limit of a continuous load, in watts: the voltage times the breaker times the continuous part.
|
|
39
|
+
* The default part is the variable `continuousLoad`.
|
|
40
|
+
*/
|
|
41
|
+
function ratedLimit(voltage, breaker, part = v(CONTINUOUS_LOAD)) {
|
|
42
|
+
return app("multiply", app("multiply", voltage, breaker), part);
|
|
43
|
+
}
|
|
44
|
+
/** This function gives the program of the current of a load, in amperes: the load in watts divided by the voltage. */
|
|
45
|
+
function loadCurrent(load, voltage) {
|
|
46
|
+
return app("divide", load, voltage);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* This function gives the program of a load as a part of its limit.
|
|
50
|
+
* A limit that is not above 0 gives an error value, not a ratio.
|
|
51
|
+
*/
|
|
52
|
+
function loadRatio(load, limit) {
|
|
53
|
+
return app("divide", load, app("max", limit, lit(0)));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* This function gives the program of the level of a ratio: `over` above 1, `near` from the near part, or `ok`.
|
|
57
|
+
* The default near part is the variable `nearLoad`. The program evaluates the ratio one time, with the name `loadRatio`.
|
|
58
|
+
*/
|
|
59
|
+
function ratioLevel(ratio, near = v(NEAR_LOAD)) {
|
|
60
|
+
const value = v("loadRatio");
|
|
61
|
+
return let_({ loadRatio: ratio }, app("if", app("gt", value, lit(1)), lit("over"), app("if", app("gte", value, near), lit("near"), lit("ok"))));
|
|
62
|
+
}
|
|
63
|
+
/** These are the kinds of the nodes of the electrical tree. The node `other` has the other load of its circuit. */
|
|
64
|
+
const NODE_KINDS = [
|
|
65
|
+
"circuit",
|
|
66
|
+
"outlet",
|
|
67
|
+
"feed",
|
|
68
|
+
"other"
|
|
69
|
+
];
|
|
70
|
+
/** This function gives the test of the kind of a node. */
|
|
71
|
+
function isKind(kind) {
|
|
72
|
+
return app("eq", ref("kind"), lit(kind));
|
|
73
|
+
}
|
|
74
|
+
/** The loads below a circuit: the feeds of its outlets, and its other load. Each load has its power in the field `watts`. */
|
|
75
|
+
const CIRCUIT_LOADS = axes.where(axes.descendants, app("or", isKind("feed"), isKind("other")));
|
|
76
|
+
/** The watts of each load of a circuit. A feed with no watts gives an error item. */
|
|
77
|
+
const WATTS = each(CIRCUIT_LOADS, ref("watts"));
|
|
78
|
+
/** The rated limit of a circuit, in watts, from its voltage and its breaker. A circuit without one of the two gives `#N/A`. */
|
|
79
|
+
const RATED_LIMIT = ratedLimit(ref("voltage"), ref("breaker"));
|
|
80
|
+
/** The limit of a circuit, in watts: its field `limit`, or its rated limit. A circuit with no limit gives `#N/A`. */
|
|
81
|
+
const CIRCUIT_LIMIT = app("ifError", ref("limit"), RATED_LIMIT);
|
|
82
|
+
/** The known load of a circuit, in watts: the sum of the watts of its loads. A feed with no watts adds nothing. */
|
|
83
|
+
const CIRCUIT_LOAD = app("sum", WATTS);
|
|
84
|
+
/** The count of the loads of a circuit that have watts. */
|
|
85
|
+
const KNOWN_LOADS = app("count", WATTS);
|
|
86
|
+
/** This test tells if the record at the focus has watts. A feed with no watts gives false. */
|
|
87
|
+
const HAS_WATTS = app("ifError", app("eq", ref("watts"), ref("watts")), lit(false));
|
|
88
|
+
/** The keys of the feeds of a circuit that have no watts. With one or more, the true load can be above the known load. */
|
|
89
|
+
const MISSING_LOADS = app("keys", each(axes.where(CIRCUIT_LOADS, app("not", HAS_WATTS)), ref("kind")));
|
|
90
|
+
/** The known load of a circuit as a part of its limit. A circuit with no limit above 0 gives an error value. */
|
|
91
|
+
const CIRCUIT_RATIO = loadRatio(CIRCUIT_LOAD, CIRCUIT_LIMIT);
|
|
92
|
+
/** The current of the known load of a circuit, in amperes. A circuit with no voltage gives `#N/A`. */
|
|
93
|
+
const CIRCUIT_CURRENT = loadCurrent(CIRCUIT_LOAD, ref("voltage"));
|
|
94
|
+
/**
|
|
95
|
+
* The level of a circuit, for its known load.
|
|
96
|
+
*
|
|
97
|
+
* - `unknown`: no load of the circuit has watts, or the circuit has no limit above 0.
|
|
98
|
+
* - `over`: the load is above the limit.
|
|
99
|
+
* - `near`: the load is the near part of the limit, or more.
|
|
100
|
+
* - `ok`: each other circuit.
|
|
101
|
+
*/
|
|
102
|
+
const CIRCUIT_LEVEL = app("if", app("eq", KNOWN_LOADS, lit(0)), lit("unknown"), app("ifError", ratioLevel(CIRCUIT_RATIO), lit("unknown")));
|
|
103
|
+
/**
|
|
104
|
+
* This program tells if the field `limit` of a circuit and its rated limit are not equal.
|
|
105
|
+
* A circuit without a limit, a voltage or a breaker gives `#N/A`: it has no two limits to compare.
|
|
106
|
+
*/
|
|
107
|
+
const LIMITS_DIFFER = app("gt", app("abs", app("subtract", ref("limit"), RATED_LIMIT)), app("multiply", RATED_LIMIT, lit(1e-9)));
|
|
108
|
+
// src/states.ts
|
|
109
|
+
/** This function gives the key of a node of the electrical tree: its kind, a colon and the `id` of its record. */
|
|
110
|
+
function nodeKey(kind, id) {
|
|
111
|
+
return `${kind}:${id}`;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* This function gives the tree of a layer, as a space of Vex. The programs of a circuit evaluate at the key of its node.
|
|
115
|
+
*
|
|
116
|
+
* - A circuit is a root. Its fields are `breaker`, `voltage` and `limit`.
|
|
117
|
+
* - An outlet is a child of its circuit, and a feed is a child of its outlet. A feed has the field `watts`.
|
|
118
|
+
* - The other load of a circuit is a child of the circuit, with the kind `other` and the field `watts`.
|
|
119
|
+
*
|
|
120
|
+
* A record that names no parent of the layer is a root, so no circuit has it. The first record of an `id` is used.
|
|
121
|
+
*/
|
|
122
|
+
function electricalSpace(layer) {
|
|
123
|
+
const records = {};
|
|
124
|
+
const parents = {};
|
|
125
|
+
const add = (key, parent, record) => {
|
|
126
|
+
if (Object.hasOwn(records, key)) return;
|
|
127
|
+
records[key] = record;
|
|
128
|
+
parents[key] = parent !== void 0 && Object.hasOwn(records, parent) ? parent : null;
|
|
129
|
+
};
|
|
130
|
+
for (const circuit of layer.circuits ?? []) {
|
|
131
|
+
const key = nodeKey("circuit", circuit.id);
|
|
132
|
+
if (Object.hasOwn(records, key)) continue;
|
|
133
|
+
add(key, void 0, {
|
|
134
|
+
kind: "circuit",
|
|
135
|
+
...circuit.breaker === void 0 ? {} : { breaker: circuit.breaker },
|
|
136
|
+
...circuit.voltage === void 0 ? {} : { voltage: circuit.voltage },
|
|
137
|
+
...circuit.limit === void 0 ? {} : { limit: circuit.limit }
|
|
138
|
+
});
|
|
139
|
+
if (circuit.otherLoad !== void 0) add(nodeKey("other", circuit.id), key, {
|
|
140
|
+
kind: "other",
|
|
141
|
+
watts: circuit.otherLoad
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
for (const outlet of layer.outlets ?? []) add(nodeKey("outlet", outlet.id), outlet.circuit === void 0 ? void 0 : nodeKey("circuit", outlet.circuit), { kind: "outlet" });
|
|
145
|
+
for (const feed of layer.feeds ?? []) add(nodeKey("feed", feed.id), nodeKey("outlet", feed.outlet), {
|
|
146
|
+
kind: "feed",
|
|
147
|
+
...feed.watts === void 0 ? {} : { watts: feed.watts }
|
|
148
|
+
});
|
|
149
|
+
return space.tree(records, parents);
|
|
150
|
+
}
|
|
151
|
+
/** This function gives the variables of the programs, from the options. */
|
|
152
|
+
function loadVars(options = {}) {
|
|
153
|
+
return {
|
|
154
|
+
[CONTINUOUS_LOAD]: options.continuousLoad ?? .8,
|
|
155
|
+
[NEAR_LOAD]: options.near ?? .8
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* This function evaluates a program of `formulas.ts` at a circuit of a tree. It gives the value, or the error value.
|
|
160
|
+
* `tree` is the result of `electricalSpace`, and `circuit` is the `id` of the circuit.
|
|
161
|
+
*/
|
|
162
|
+
function evaluateCircuit(program, tree, circuit, options = {}) {
|
|
163
|
+
return evaluate(program, {
|
|
164
|
+
space: tree,
|
|
165
|
+
origin: nodeKey("circuit", circuit),
|
|
166
|
+
domains: [
|
|
167
|
+
NumDomain,
|
|
168
|
+
BoolDomain,
|
|
169
|
+
TextDomain
|
|
170
|
+
],
|
|
171
|
+
vars: loadVars(options)
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
/** This function gives the number of a result, or `undefined` for an error value. */
|
|
175
|
+
function numberOf(result) {
|
|
176
|
+
return result.ok && typeof result.value === "number" ? result.value : void 0;
|
|
177
|
+
}
|
|
178
|
+
/** This function gives the texts of a result that is a list of texts. An error value gives no texts. */
|
|
179
|
+
function textsOf(result) {
|
|
180
|
+
const value = result.ok ? result.value : [];
|
|
181
|
+
return new Set(Array.isArray(value) ? value.filter((item) => typeof item === "string") : []);
|
|
182
|
+
}
|
|
183
|
+
/** This function gives the first record of each `id` of a list, in the sequence of the list. */
|
|
184
|
+
function firsts(list) {
|
|
185
|
+
const seen = /* @__PURE__ */ new Set();
|
|
186
|
+
return list.filter((item) => {
|
|
187
|
+
if (seen.has(item.id)) return false;
|
|
188
|
+
seen.add(item.id);
|
|
189
|
+
return true;
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* This function gives the state of each circuit of a layer, in the sequence of the circuits.
|
|
194
|
+
*
|
|
195
|
+
* The level is the level of the known load. A feed with no watts adds nothing to the load, and `missing` names it.
|
|
196
|
+
* A circuit with no known load has the level `unknown`, and so has a circuit with no limit above 0.
|
|
197
|
+
*/
|
|
198
|
+
function circuitStates(layer, options = {}) {
|
|
199
|
+
const tree = electricalSpace(layer);
|
|
200
|
+
const parent = (key) => tree.parentOf?.(key);
|
|
201
|
+
const states = [];
|
|
202
|
+
for (const circuit of firsts(layer.circuits ?? [])) {
|
|
203
|
+
const key = nodeKey("circuit", circuit.id);
|
|
204
|
+
const run = (program) => evaluateCircuit(program, tree, circuit.id, options);
|
|
205
|
+
const level = run(CIRCUIT_LEVEL);
|
|
206
|
+
const missing = textsOf(run(MISSING_LOADS));
|
|
207
|
+
const outlets = firsts(layer.outlets ?? []).filter((outlet) => parent(nodeKey("outlet", outlet.id)) === key).map((outlet) => outlet.id);
|
|
208
|
+
const feeds = firsts(layer.feeds ?? []).filter((feed) => {
|
|
209
|
+
const outlet = parent(nodeKey("feed", feed.id));
|
|
210
|
+
return outlet !== void 0 && parent(outlet) === key;
|
|
211
|
+
});
|
|
212
|
+
states.push({
|
|
213
|
+
circuit: circuit.id,
|
|
214
|
+
watts: numberOf(run(CIRCUIT_LOAD)) ?? 0,
|
|
215
|
+
limit: numberOf(run(CIRCUIT_LIMIT)),
|
|
216
|
+
ratio: numberOf(run(CIRCUIT_RATIO)),
|
|
217
|
+
level: level.ok && LOAD_LEVELS.includes(level.value) ? level.value : "unknown",
|
|
218
|
+
missing: feeds.filter((feed) => missing.has(nodeKey("feed", feed.id))).map((feed) => feed.id),
|
|
219
|
+
outlets,
|
|
220
|
+
feeds: feeds.map((feed) => feed.id),
|
|
221
|
+
objects: [...new Set(feeds.map((feed) => feed.object))]
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
return states;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* This function gives the level of each outlet of a layer: the level of its circuit. The key is the `id` of the outlet.
|
|
228
|
+
* An outlet with no circuit of the layer has no level, and the map does not have it.
|
|
229
|
+
*/
|
|
230
|
+
function outletLevels(layer, options = {}) {
|
|
231
|
+
const levels = /* @__PURE__ */ new Map();
|
|
232
|
+
for (const state of circuitStates(layer, options)) for (const outlet of state.outlets) levels.set(outlet, state.level);
|
|
233
|
+
return levels;
|
|
234
|
+
}
|
|
235
|
+
// src/check.ts
|
|
236
|
+
/** These are the codes of the faults of an electrical layer. */
|
|
237
|
+
const ELECTRICAL_CODES = [
|
|
238
|
+
"id-duplicate",
|
|
239
|
+
"outlet-circuit",
|
|
240
|
+
"feed-outlet",
|
|
241
|
+
"feed-object",
|
|
242
|
+
"feed-watts",
|
|
243
|
+
"circuit-number",
|
|
244
|
+
"circuit-no-limit",
|
|
245
|
+
"circuit-limits",
|
|
246
|
+
"circuit-over"
|
|
247
|
+
];
|
|
248
|
+
/** This function gives a number with 3 decimals at most. */
|
|
249
|
+
function show(value) {
|
|
250
|
+
return String(Math.round(value * 1e3) / 1e3);
|
|
251
|
+
}
|
|
252
|
+
/** This function tells if a value is a finite number above 0. */
|
|
253
|
+
function isAboveZero(value) {
|
|
254
|
+
return Number.isFinite(value) && value > 0;
|
|
255
|
+
}
|
|
256
|
+
/** This function tells if a value is a finite number, 0 or more. */
|
|
257
|
+
function isZeroOrMore(value) {
|
|
258
|
+
return Number.isFinite(value) && value >= 0;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* This function gives each fault of the meaning of an electrical layer, in the space of a scene.
|
|
262
|
+
*
|
|
263
|
+
* - An error: an `id` of two records, a circuit, an outlet or an object that is not there, and a number that is not valid.
|
|
264
|
+
* - A warning: a circuit with no limit, a limit that is not the rated limit, and a known load above the limit.
|
|
265
|
+
*
|
|
266
|
+
* The function does not examine the anchors: `checkLayers` of the core does. A warning is a fault that a viewer can show.
|
|
267
|
+
*/
|
|
268
|
+
function checkElectrical(layer, space, options = {}) {
|
|
269
|
+
const faults = [];
|
|
270
|
+
const error = (code, at, message, other) => {
|
|
271
|
+
faults.push({
|
|
272
|
+
level: "error",
|
|
273
|
+
code,
|
|
274
|
+
at,
|
|
275
|
+
message,
|
|
276
|
+
...other === void 0 ? {} : { other }
|
|
277
|
+
});
|
|
278
|
+
};
|
|
279
|
+
const warning = (code, at, message) => {
|
|
280
|
+
faults.push({
|
|
281
|
+
level: "warning",
|
|
282
|
+
code,
|
|
283
|
+
at,
|
|
284
|
+
message
|
|
285
|
+
});
|
|
286
|
+
};
|
|
287
|
+
const outlets = layer.outlets ?? [];
|
|
288
|
+
const circuits = layer.circuits ?? [];
|
|
289
|
+
const feeds = layer.feeds ?? [];
|
|
290
|
+
const kinds = /* @__PURE__ */ new Map();
|
|
291
|
+
const count = (word, id) => {
|
|
292
|
+
kinds.set(id, [...kinds.get(id) ?? [], word]);
|
|
293
|
+
};
|
|
294
|
+
for (const outlet of outlets) count("an outlet", outlet.id);
|
|
295
|
+
for (const circuit of circuits) count("a circuit", circuit.id);
|
|
296
|
+
for (const feed of feeds) count("a feed", feed.id);
|
|
297
|
+
for (const [id, words] of kinds) if (words.length > 1) error("id-duplicate", id, `The id "${id}" is the id of ${words.length} records of the layer: ${words.join(", ")}. Each record of a layer has its own id.`);
|
|
298
|
+
const circuitIds = new Set(circuits.map((circuit) => circuit.id));
|
|
299
|
+
const outletIds = new Set(outlets.map((outlet) => outlet.id));
|
|
300
|
+
for (const outlet of outlets) if (outlet.circuit !== void 0 && !circuitIds.has(outlet.circuit)) error("outlet-circuit", outlet.id, `The outlet "${outlet.id}" is on the circuit "${outlet.circuit}", which the layer does not have.`, outlet.circuit);
|
|
301
|
+
for (const feed of feeds) {
|
|
302
|
+
if (!outletIds.has(feed.outlet)) error("feed-outlet", feed.id, `The feed "${feed.id}" is from the outlet "${feed.outlet}", which the layer does not have.`, feed.outlet);
|
|
303
|
+
if (space.object(feed.object) === void 0) error("feed-object", feed.id, `The feed "${feed.id}" gives power to the object "${feed.object}", which the scene does not have.`, feed.object);
|
|
304
|
+
if (feed.watts !== void 0 && !isZeroOrMore(feed.watts)) error("feed-watts", feed.id, `The feed "${feed.id}" has a power of ${show(feed.watts)} W. It must be a finite number, 0 or more.`);
|
|
305
|
+
}
|
|
306
|
+
const tree = electricalSpace(layer);
|
|
307
|
+
const states = new Map(circuitStates(layer, options).map((state) => [state.circuit, state]));
|
|
308
|
+
const examined = /* @__PURE__ */ new Set();
|
|
309
|
+
for (const circuit of circuits) {
|
|
310
|
+
const name = `the circuit "${circuit.id}"`;
|
|
311
|
+
if (circuit.breaker !== void 0 && !isAboveZero(circuit.breaker)) error("circuit-number", circuit.id, `The breaker of ${name} is ${show(circuit.breaker)} A. It must be above 0.`);
|
|
312
|
+
if (circuit.voltage !== void 0 && !isAboveZero(circuit.voltage)) error("circuit-number", circuit.id, `The voltage of ${name} is ${show(circuit.voltage)} V. It must be above 0.`);
|
|
313
|
+
if (circuit.limit !== void 0 && !isAboveZero(circuit.limit)) error("circuit-number", circuit.id, `The limit of ${name} is ${show(circuit.limit)} W. It must be above 0.`);
|
|
314
|
+
if (circuit.otherLoad !== void 0 && !isZeroOrMore(circuit.otherLoad)) error("circuit-number", circuit.id, `The other load of ${name} is ${show(circuit.otherLoad)} W. It must be a finite number, 0 or more.`);
|
|
315
|
+
if (examined.has(circuit.id)) continue;
|
|
316
|
+
examined.add(circuit.id);
|
|
317
|
+
const state = states.get(circuit.id);
|
|
318
|
+
if (state === void 0) continue;
|
|
319
|
+
if (state.limit === void 0) warning("circuit-no-limit", circuit.id, `The circuit "${circuit.id}" has no limit, and it does not have a breaker and a voltage. Its level is not known.`);
|
|
320
|
+
const differ = evaluateCircuit(LIMITS_DIFFER, tree, circuit.id, options);
|
|
321
|
+
const rated = evaluateCircuit(RATED_LIMIT, tree, circuit.id, options);
|
|
322
|
+
if (differ.ok && differ.value === true && rated.ok && typeof rated.value === "number" && circuit.limit !== void 0) warning("circuit-limits", circuit.id, `The circuit "${circuit.id}" has the limit ${show(circuit.limit)} W. Its breaker of ${show(circuit.breaker ?? 0)} A at ${show(circuit.voltage ?? 0)} V gives ${show(rated.value)} W for a continuous load.`);
|
|
323
|
+
if (state.level === "over" && state.limit !== void 0) warning("circuit-over", circuit.id, `The circuit "${circuit.id}" has a known load of ${show(state.watts)} W, above its limit of ${show(state.limit)} W.`);
|
|
324
|
+
}
|
|
325
|
+
return faults;
|
|
326
|
+
}
|
|
327
|
+
export { withOutletAnchors as A, NODE_KINDS as C, ratedLimit as D, loadRatio as E, ratioLevel as O, NEAR_LOAD as S, loadCurrent as T, DEFAULT_NEAR_LOAD as _, evaluateCircuit as a, LOAD_LEVELS as b, outletLevels as c, CIRCUIT_LIMIT as d, CIRCUIT_LOAD as f, DEFAULT_CONTINUOUS_LOAD as g, CONTINUOUS_LOAD as h, electricalSpace as i, outletAnchors as k, CIRCUIT_CURRENT as l, CIRCUIT_RATIO as m, checkElectrical as n, loadVars as o, CIRCUIT_LOADS as p, circuitStates as r, nodeKey as s, ELECTRICAL_CODES as t, CIRCUIT_LEVEL as u, KNOWN_LOADS as v, RATED_LIMIT as w, MISSING_LOADS as x, LIMITS_DIFFER as y };
|
|
328
|
+
|
|
329
|
+
//# sourceMappingURL=check-V0Af5oUe.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"check-V0Af5oUe.js","names":[],"sources":["../src/anchors.ts","../src/formulas.ts","../src/states.ts","../src/check.ts"],"sourcesContent":["import type { Anchor } from \"@mark1russell7/room-view\";\nimport type { ElectricalLayer } from \"./layer.ts\";\n\n// The anchors of an electrical layer: each outlet is on one anchor of the core.\n\n/** This function gives the anchor of each outlet of a layer, by the `id` of the outlet. The first outlet of an `id` is used. */\nexport function outletAnchors(layer: ElectricalLayer): ReadonlyMap<string, Anchor> {\n const anchors = new Map<string, Anchor>();\n for (const outlet of layer.outlets ?? []) if (!anchors.has(outlet.id)) anchors.set(outlet.id, outlet.anchor);\n return anchors;\n}\n\n/** This function gives a layer with new anchors of its outlets, by the `id` of each outlet. An outlet with no new anchor keeps its anchor. */\nexport function withOutletAnchors(layer: ElectricalLayer, anchors: ReadonlyMap<string, Anchor>): ElectricalLayer {\n if (layer.outlets === undefined) return layer;\n return { ...layer, outlets: layer.outlets.map((outlet) => ({ ...outlet, anchor: anchors.get(outlet.id) ?? outlet.anchor })) };\n}\n","import { app, axes, each, let_, lit, ref, v, type Axis, type Expr } from \"@mark1russell7/vex\";\n\n// The circuit math, as Vex programs. A program is data: `explain` of Vex shows each step, and a host can keep it as JSON.\n// The first part is general: it reads no field, so a package with a different tree builds its programs from it.\n// The second part is the programs of the electrical tree, which `electricalSpace` makes from a layer.\n\n/** The name of the variable with the continuous part: the part of the rating of a breaker that a continuous load can use. */\nexport const CONTINUOUS_LOAD = \"continuousLoad\";\n\n/** The default continuous part. A continuous load can use 80% of the rating of a breaker. */\nexport const DEFAULT_CONTINUOUS_LOAD = 0.8;\n\n/** The name of the variable with the near part: a load with this part of its limit, or more, has the level `near`. */\nexport const NEAR_LOAD = \"nearLoad\";\n\n/** The default near part. */\nexport const DEFAULT_NEAR_LOAD = 0.8;\n\n/** These are the levels of the load of a circuit. */\nexport const LOAD_LEVELS: readonly [\"unknown\", \"ok\", \"near\", \"over\"] = [\"unknown\", \"ok\", \"near\", \"over\"];\n\n/** A level of the load of a circuit. A viewer gives each level a color and a shape. */\nexport type LoadLevel = (typeof LOAD_LEVELS)[number];\n\n/**\n * This function gives the program of the limit of a continuous load, in watts: the voltage times the breaker times the continuous part.\n * The default part is the variable `continuousLoad`.\n */\nexport function ratedLimit(voltage: Expr, breaker: Expr, part: Expr = v(CONTINUOUS_LOAD)): Expr {\n return app(\"multiply\", app(\"multiply\", voltage, breaker), part);\n}\n\n/** This function gives the program of the current of a load, in amperes: the load in watts divided by the voltage. */\nexport function loadCurrent(load: Expr, voltage: Expr): Expr {\n return app(\"divide\", load, voltage);\n}\n\n/**\n * This function gives the program of a load as a part of its limit.\n * A limit that is not above 0 gives an error value, not a ratio.\n */\nexport function loadRatio(load: Expr, limit: Expr): Expr {\n return app(\"divide\", load, app(\"max\", limit, lit(0)));\n}\n\n/**\n * This function gives the program of the level of a ratio: `over` above 1, `near` from the near part, or `ok`.\n * The default near part is the variable `nearLoad`. The program evaluates the ratio one time, with the name `loadRatio`.\n */\nexport function ratioLevel(ratio: Expr, near: Expr = v(NEAR_LOAD)): Expr {\n const value = v(\"loadRatio\");\n return let_({ loadRatio: ratio }, app(\"if\", app(\"gt\", value, lit(1)), lit(\"over\"), app(\"if\", app(\"gte\", value, near), lit(\"near\"), lit(\"ok\"))));\n}\n\n/** These are the kinds of the nodes of the electrical tree. The node `other` has the other load of its circuit. */\nexport const NODE_KINDS: readonly [\"circuit\", \"outlet\", \"feed\", \"other\"] = [\"circuit\", \"outlet\", \"feed\", \"other\"];\n\n/** A kind of node of the electrical tree. */\nexport type NodeKind = (typeof NODE_KINDS)[number];\n\n/** This function gives the test of the kind of a node. */\nfunction isKind(kind: NodeKind): Expr {\n return app(\"eq\", ref(\"kind\"), lit(kind));\n}\n\n/** The loads below a circuit: the feeds of its outlets, and its other load. Each load has its power in the field `watts`. */\nexport const CIRCUIT_LOADS: Axis = axes.where(axes.descendants, app(\"or\", isKind(\"feed\"), isKind(\"other\")));\n\n/** The watts of each load of a circuit. A feed with no watts gives an error item. */\nconst WATTS: Expr = each(CIRCUIT_LOADS, ref(\"watts\"));\n\n/** The rated limit of a circuit, in watts, from its voltage and its breaker. A circuit without one of the two gives `#N/A`. */\nexport const RATED_LIMIT: Expr = ratedLimit(ref(\"voltage\"), ref(\"breaker\"));\n\n/** The limit of a circuit, in watts: its field `limit`, or its rated limit. A circuit with no limit gives `#N/A`. */\nexport const CIRCUIT_LIMIT: Expr = app(\"ifError\", ref(\"limit\"), RATED_LIMIT);\n\n/** The known load of a circuit, in watts: the sum of the watts of its loads. A feed with no watts adds nothing. */\nexport const CIRCUIT_LOAD: Expr = app(\"sum\", WATTS);\n\n/** The count of the loads of a circuit that have watts. */\nexport const KNOWN_LOADS: Expr = app(\"count\", WATTS);\n\n/** This test tells if the record at the focus has watts. A feed with no watts gives false. */\nconst HAS_WATTS: Expr = app(\"ifError\", app(\"eq\", ref(\"watts\"), ref(\"watts\")), lit(false));\n\n/** The keys of the feeds of a circuit that have no watts. With one or more, the true load can be above the known load. */\nexport const MISSING_LOADS: Expr = app(\"keys\", each(axes.where(CIRCUIT_LOADS, app(\"not\", HAS_WATTS)), ref(\"kind\")));\n\n/** The known load of a circuit as a part of its limit. A circuit with no limit above 0 gives an error value. */\nexport const CIRCUIT_RATIO: Expr = loadRatio(CIRCUIT_LOAD, CIRCUIT_LIMIT);\n\n/** The current of the known load of a circuit, in amperes. A circuit with no voltage gives `#N/A`. */\nexport const CIRCUIT_CURRENT: Expr = loadCurrent(CIRCUIT_LOAD, ref(\"voltage\"));\n\n/**\n * The level of a circuit, for its known load.\n *\n * - `unknown`: no load of the circuit has watts, or the circuit has no limit above 0.\n * - `over`: the load is above the limit.\n * - `near`: the load is the near part of the limit, or more.\n * - `ok`: each other circuit.\n */\nexport const CIRCUIT_LEVEL: Expr = app(\"if\", app(\"eq\", KNOWN_LOADS, lit(0)), lit(\"unknown\"), app(\"ifError\", ratioLevel(CIRCUIT_RATIO), lit(\"unknown\")));\n\n/** The largest difference of two limits that are equal, as a part of the rated limit. It is above the error of the arithmetic. */\nconst EQUAL_LIMITS = 1e-9;\n\n/**\n * This program tells if the field `limit` of a circuit and its rated limit are not equal.\n * A circuit without a limit, a voltage or a breaker gives `#N/A`: it has no two limits to compare.\n */\nexport const LIMITS_DIFFER: Expr = app(\"gt\", app(\"abs\", app(\"subtract\", ref(\"limit\"), RATED_LIMIT)), app(\"multiply\", RATED_LIMIT, lit(EQUAL_LIMITS)));\n","import { evaluate, space, type Expr, type Result, type Space } from \"@mark1russell7/vex\";\nimport { BoolDomain, NumDomain, TextDomain } from \"@mark1russell7/vex-domains\";\nimport {\n CIRCUIT_LEVEL,\n CIRCUIT_LIMIT,\n CIRCUIT_LOAD,\n CIRCUIT_RATIO,\n CONTINUOUS_LOAD,\n DEFAULT_CONTINUOUS_LOAD,\n DEFAULT_NEAR_LOAD,\n LOAD_LEVELS,\n MISSING_LOADS,\n NEAR_LOAD,\n type LoadLevel,\n type NodeKind,\n} from \"./formulas.ts\";\nimport type { ElectricalLayer } from \"./layer.ts\";\n\n// The state of each circuit of a layer. The layer becomes a tree, and the programs of `formulas.ts` evaluate at each circuit.\n\n/** This function gives the key of a node of the electrical tree: its kind, a colon and the `id` of its record. */\nexport function nodeKey(kind: NodeKind, id: string): string {\n return `${kind}:${id}`;\n}\n\n/**\n * This function gives the tree of a layer, as a space of Vex. The programs of a circuit evaluate at the key of its node.\n *\n * - A circuit is a root. Its fields are `breaker`, `voltage` and `limit`.\n * - An outlet is a child of its circuit, and a feed is a child of its outlet. A feed has the field `watts`.\n * - The other load of a circuit is a child of the circuit, with the kind `other` and the field `watts`.\n *\n * A record that names no parent of the layer is a root, so no circuit has it. The first record of an `id` is used.\n */\nexport function electricalSpace(layer: ElectricalLayer): Space {\n const records: Record<string, Readonly<Record<string, unknown>>> = {};\n const parents: Record<string, string | null> = {};\n const add = (key: string, parent: string | undefined, record: Readonly<Record<string, unknown>>): void => {\n if (Object.hasOwn(records, key)) return;\n records[key] = record;\n parents[key] = parent !== undefined && Object.hasOwn(records, parent) ? parent : null;\n };\n for (const circuit of layer.circuits ?? []) {\n const key = nodeKey(\"circuit\", circuit.id);\n if (Object.hasOwn(records, key)) continue;\n add(key, undefined, {\n kind: \"circuit\",\n ...(circuit.breaker === undefined ? {} : { breaker: circuit.breaker }),\n ...(circuit.voltage === undefined ? {} : { voltage: circuit.voltage }),\n ...(circuit.limit === undefined ? {} : { limit: circuit.limit }),\n });\n if (circuit.otherLoad !== undefined) add(nodeKey(\"other\", circuit.id), key, { kind: \"other\", watts: circuit.otherLoad });\n }\n for (const outlet of layer.outlets ?? []) {\n add(nodeKey(\"outlet\", outlet.id), outlet.circuit === undefined ? undefined : nodeKey(\"circuit\", outlet.circuit), { kind: \"outlet\" });\n }\n for (const feed of layer.feeds ?? []) {\n add(nodeKey(\"feed\", feed.id), nodeKey(\"outlet\", feed.outlet), { kind: \"feed\", ...(feed.watts === undefined ? {} : { watts: feed.watts }) });\n }\n return space.tree(records, parents);\n}\n\n/** The options of the limits and the levels. */\nexport interface LoadOptions {\n /** The part of the rating of a breaker that a continuous load can use. The default is 0.8. */\n readonly continuousLoad?: number | undefined;\n /** A circuit with this part of its limit, or more, has the level `near`. The default is 0.8. */\n readonly near?: number | undefined;\n}\n\n/** This function gives the variables of the programs, from the options. */\nexport function loadVars(options: LoadOptions = {}): Readonly<Record<string, number>> {\n return { [CONTINUOUS_LOAD]: options.continuousLoad ?? DEFAULT_CONTINUOUS_LOAD, [NEAR_LOAD]: options.near ?? DEFAULT_NEAR_LOAD };\n}\n\n/**\n * This function evaluates a program of `formulas.ts` at a circuit of a tree. It gives the value, or the error value.\n * `tree` is the result of `electricalSpace`, and `circuit` is the `id` of the circuit.\n */\nexport function evaluateCircuit(program: Expr, tree: Space, circuit: string, options: LoadOptions = {}): Result<unknown> {\n return evaluate(program, { space: tree, origin: nodeKey(\"circuit\", circuit), domains: [NumDomain, BoolDomain, TextDomain], vars: loadVars(options) });\n}\n\n/** The state of one circuit of a layer. */\nexport interface CircuitState {\n /** The `id` of the circuit. */\n readonly circuit: string;\n /** The known load of the circuit, in watts: the sum of the watts of its feeds, plus its other load. */\n readonly watts: number;\n /** The limit of the circuit, in watts. A circuit with no limit has none. */\n readonly limit?: number | undefined;\n /** The known load as a part of the limit. A circuit with no limit above 0 has none. */\n readonly ratio?: number | undefined;\n /** The level of the known load. */\n readonly level: LoadLevel;\n /** The `id` of each feed of the circuit with no watts. With one or more, the true load can be above `watts`. */\n readonly missing: readonly string[];\n /** The `id` of each outlet of the circuit. */\n readonly outlets: readonly string[];\n /** The `id` of each feed from an outlet of the circuit. */\n readonly feeds: readonly string[];\n /** The `id` of each placed object with a feed from an outlet of the circuit. */\n readonly objects: readonly string[];\n}\n\n/** This function gives the number of a result, or `undefined` for an error value. */\nfunction numberOf(result: Result<unknown>): number | undefined {\n return result.ok && typeof result.value === \"number\" ? result.value : undefined;\n}\n\n/** This function gives the texts of a result that is a list of texts. An error value gives no texts. */\nfunction textsOf(result: Result<unknown>): ReadonlySet<string> {\n const value = result.ok ? result.value : [];\n return new Set(Array.isArray(value) ? value.filter((item): item is string => typeof item === \"string\") : []);\n}\n\n/** This function gives the first record of each `id` of a list, in the sequence of the list. */\nfunction firsts<Item extends { readonly id: string }>(list: readonly Item[]): readonly Item[] {\n const seen = new Set<string>();\n return list.filter((item) => {\n if (seen.has(item.id)) return false;\n seen.add(item.id);\n return true;\n });\n}\n\n/**\n * This function gives the state of each circuit of a layer, in the sequence of the circuits.\n *\n * The level is the level of the known load. A feed with no watts adds nothing to the load, and `missing` names it.\n * A circuit with no known load has the level `unknown`, and so has a circuit with no limit above 0.\n */\nexport function circuitStates(layer: ElectricalLayer, options: LoadOptions = {}): readonly CircuitState[] {\n const tree = electricalSpace(layer);\n const parent = (key: string): string | undefined => tree.parentOf?.(key);\n const states: CircuitState[] = [];\n for (const circuit of firsts(layer.circuits ?? [])) {\n const key = nodeKey(\"circuit\", circuit.id);\n const run = (program: Expr): Result<unknown> => evaluateCircuit(program, tree, circuit.id, options);\n const level = run(CIRCUIT_LEVEL);\n const missing = textsOf(run(MISSING_LOADS));\n // The tree tells the circuit of each record, so the lists and the programs agree.\n const outlets = firsts(layer.outlets ?? [])\n .filter((outlet) => parent(nodeKey(\"outlet\", outlet.id)) === key)\n .map((outlet) => outlet.id);\n const feeds = firsts(layer.feeds ?? []).filter((feed) => {\n const outlet = parent(nodeKey(\"feed\", feed.id));\n return outlet !== undefined && parent(outlet) === key;\n });\n states.push({\n circuit: circuit.id,\n watts: numberOf(run(CIRCUIT_LOAD)) ?? 0,\n limit: numberOf(run(CIRCUIT_LIMIT)),\n ratio: numberOf(run(CIRCUIT_RATIO)),\n level: level.ok && LOAD_LEVELS.includes(level.value as LoadLevel) ? (level.value as LoadLevel) : \"unknown\",\n missing: feeds.filter((feed) => missing.has(nodeKey(\"feed\", feed.id))).map((feed) => feed.id),\n outlets,\n feeds: feeds.map((feed) => feed.id),\n objects: [...new Set(feeds.map((feed) => feed.object))],\n });\n }\n return states;\n}\n\n/**\n * This function gives the level of each outlet of a layer: the level of its circuit. The key is the `id` of the outlet.\n * An outlet with no circuit of the layer has no level, and the map does not have it.\n */\nexport function outletLevels(layer: ElectricalLayer, options: LoadOptions = {}): ReadonlyMap<string, LoadLevel> {\n const levels = new Map<string, LoadLevel>();\n for (const state of circuitStates(layer, options)) {\n for (const outlet of state.outlets) levels.set(outlet, state.level);\n }\n return levels;\n}\n","import type { AddOnFault, SceneSpace } from \"@mark1russell7/room-view\";\nimport { LIMITS_DIFFER, RATED_LIMIT } from \"./formulas.ts\";\nimport type { ElectricalLayer } from \"./layer.ts\";\nimport { circuitStates, electricalSpace, evaluateCircuit, type LoadOptions } from \"./states.ts\";\n\n// The checks of the meaning of an electrical layer. The schema examines the form, and the core examines each anchor.\n\n/** These are the codes of the faults of an electrical layer. */\nexport const ELECTRICAL_CODES: readonly [\n \"id-duplicate\",\n \"outlet-circuit\",\n \"feed-outlet\",\n \"feed-object\",\n \"feed-watts\",\n \"circuit-number\",\n \"circuit-no-limit\",\n \"circuit-limits\",\n \"circuit-over\",\n] = [\"id-duplicate\", \"outlet-circuit\", \"feed-outlet\", \"feed-object\", \"feed-watts\", \"circuit-number\", \"circuit-no-limit\", \"circuit-limits\", \"circuit-over\"];\n\n/** A code of a fault of an electrical layer. */\nexport type ElectricalCode = (typeof ELECTRICAL_CODES)[number];\n\n/** A fault of an electrical layer. `checkLayers` of the core gives it the name of the layer. */\nexport interface ElectricalFault extends AddOnFault {\n readonly code: ElectricalCode;\n}\n\n/** This function gives a number with 3 decimals at most. */\nfunction show(value: number): string {\n return String(Math.round(value * 1000) / 1000);\n}\n\n/** This function tells if a value is a finite number above 0. */\nfunction isAboveZero(value: number): boolean {\n return Number.isFinite(value) && value > 0;\n}\n\n/** This function tells if a value is a finite number, 0 or more. */\nfunction isZeroOrMore(value: number): boolean {\n return Number.isFinite(value) && value >= 0;\n}\n\n/**\n * This function gives each fault of the meaning of an electrical layer, in the space of a scene.\n *\n * - An error: an `id` of two records, a circuit, an outlet or an object that is not there, and a number that is not valid.\n * - A warning: a circuit with no limit, a limit that is not the rated limit, and a known load above the limit.\n *\n * The function does not examine the anchors: `checkLayers` of the core does. A warning is a fault that a viewer can show.\n */\nexport function checkElectrical(layer: ElectricalLayer, space: SceneSpace, options: LoadOptions = {}): readonly ElectricalFault[] {\n const faults: ElectricalFault[] = [];\n const error = (code: ElectricalCode, at: string, message: string, other?: string): void => {\n faults.push({ level: \"error\", code, at, message, ...(other === undefined ? {} : { other }) });\n };\n const warning = (code: ElectricalCode, at: string, message: string): void => {\n faults.push({ level: \"warning\", code, at, message });\n };\n const outlets = layer.outlets ?? [];\n const circuits = layer.circuits ?? [];\n const feeds = layer.feeds ?? [];\n\n const kinds = new Map<string, string[]>();\n const count = (word: string, id: string): void => {\n kinds.set(id, [...(kinds.get(id) ?? []), word]);\n };\n for (const outlet of outlets) count(\"an outlet\", outlet.id);\n for (const circuit of circuits) count(\"a circuit\", circuit.id);\n for (const feed of feeds) count(\"a feed\", feed.id);\n for (const [id, words] of kinds) {\n if (words.length > 1) error(\"id-duplicate\", id, `The id \"${id}\" is the id of ${words.length} records of the layer: ${words.join(\", \")}. Each record of a layer has its own id.`);\n }\n\n const circuitIds = new Set(circuits.map((circuit) => circuit.id));\n const outletIds = new Set(outlets.map((outlet) => outlet.id));\n for (const outlet of outlets) {\n if (outlet.circuit !== undefined && !circuitIds.has(outlet.circuit)) {\n error(\"outlet-circuit\", outlet.id, `The outlet \"${outlet.id}\" is on the circuit \"${outlet.circuit}\", which the layer does not have.`, outlet.circuit);\n }\n }\n for (const feed of feeds) {\n if (!outletIds.has(feed.outlet)) error(\"feed-outlet\", feed.id, `The feed \"${feed.id}\" is from the outlet \"${feed.outlet}\", which the layer does not have.`, feed.outlet);\n if (space.object(feed.object) === undefined) error(\"feed-object\", feed.id, `The feed \"${feed.id}\" gives power to the object \"${feed.object}\", which the scene does not have.`, feed.object);\n if (feed.watts !== undefined && !isZeroOrMore(feed.watts)) error(\"feed-watts\", feed.id, `The feed \"${feed.id}\" has a power of ${show(feed.watts)} W. It must be a finite number, 0 or more.`);\n }\n\n const tree = electricalSpace(layer);\n const states = new Map(circuitStates(layer, options).map((state) => [state.circuit, state] as const));\n const examined = new Set<string>();\n for (const circuit of circuits) {\n const name = `the circuit \"${circuit.id}\"`;\n if (circuit.breaker !== undefined && !isAboveZero(circuit.breaker)) error(\"circuit-number\", circuit.id, `The breaker of ${name} is ${show(circuit.breaker)} A. It must be above 0.`);\n if (circuit.voltage !== undefined && !isAboveZero(circuit.voltage)) error(\"circuit-number\", circuit.id, `The voltage of ${name} is ${show(circuit.voltage)} V. It must be above 0.`);\n if (circuit.limit !== undefined && !isAboveZero(circuit.limit)) error(\"circuit-number\", circuit.id, `The limit of ${name} is ${show(circuit.limit)} W. It must be above 0.`);\n if (circuit.otherLoad !== undefined && !isZeroOrMore(circuit.otherLoad)) {\n error(\"circuit-number\", circuit.id, `The other load of ${name} is ${show(circuit.otherLoad)} W. It must be a finite number, 0 or more.`);\n }\n // The tree has the first record of an `id`, so the programs examine only that record.\n if (examined.has(circuit.id)) continue;\n examined.add(circuit.id);\n const state = states.get(circuit.id);\n if (state === undefined) continue;\n if (state.limit === undefined) {\n warning(\"circuit-no-limit\", circuit.id, `The circuit \"${circuit.id}\" has no limit, and it does not have a breaker and a voltage. Its level is not known.`);\n }\n const differ = evaluateCircuit(LIMITS_DIFFER, tree, circuit.id, options);\n const rated = evaluateCircuit(RATED_LIMIT, tree, circuit.id, options);\n if (differ.ok && differ.value === true && rated.ok && typeof rated.value === \"number\" && circuit.limit !== undefined) {\n warning(\n \"circuit-limits\",\n circuit.id,\n `The circuit \"${circuit.id}\" has the limit ${show(circuit.limit)} W. Its breaker of ${show(circuit.breaker ?? 0)} A at ${show(circuit.voltage ?? 0)} V gives ${show(rated.value)} W for a continuous load.`,\n );\n }\n if (state.level === \"over\" && state.limit !== undefined) {\n warning(\"circuit-over\", circuit.id, `The circuit \"${circuit.id}\" has a known load of ${show(state.watts)} W, above its limit of ${show(state.limit)} W.`);\n }\n }\n return faults;\n}\n"],"mappings":";;;;AAMA,SAAgB,cAAc,OAAqD;CACjF,MAAM,0BAAU,IAAI,IAAoB;CACxC,KAAK,MAAM,UAAU,MAAM,WAAW,CAAC,GAAG,IAAI,CAAC,QAAQ,IAAI,OAAO,EAAE,GAAG,QAAQ,IAAI,OAAO,IAAI,OAAO,MAAM;CAC3G,OAAO;AACT;;AAGA,SAAgB,kBAAkB,OAAwB,SAAuD;CAC/G,IAAI,MAAM,YAAY,KAAA,GAAW,OAAO;CACxC,OAAO;EAAE,GAAG;EAAO,SAAS,MAAM,QAAQ,KAAK,YAAY;GAAE,GAAG;GAAQ,QAAQ,QAAQ,IAAI,OAAO,EAAE,KAAK,OAAO;EAAO,EAAE;CAAE;AAC9H;;;ACTA,MAAa,kBAAkB;;AAG/B,MAAa,0BAA0B;;AAGvC,MAAa,YAAY;;AAGzB,MAAa,oBAAoB;;AAGjC,MAAa,cAA0D;CAAC;CAAW;CAAM;CAAQ;AAAM;;;;;AASvG,SAAgB,WAAW,SAAe,SAAe,OAAa,EAAE,eAAe,GAAS;CAC9F,OAAO,IAAI,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UAAU,YAAY,QAAQ,UAAU,KAAA,GACzG,QACE,kBACA,QAAQ,IACR,gBAAgB,QAAQ,GAAG,kBAAkB,KAAK,QAAQ,KAAK,EAAE,qBAAqB,KAAK,QAAQ,WAAW,CAAC,EAAE,QAAQ,KAAK,QAAQ,WAAW,CAAC,EAAE,WAAW,KAAK,MAAM,KAAK,EAAE,0BACnL;EAEF,IAAI,MAAM,UAAU,UAAU,MAAM,UAAU,KAAA,GAC5C,QAAQ,gBAAgB,QAAQ,IAAI,gBAAgB,QAAQ,GAAG,wBAAwB,KAAK,MAAM,KAAK,EAAE,yBAAyB,KAAK,MAAM,KAAK,EAAE,IAAI;CAE5J;CACA,OAAO;AACT"}
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { a as Outlet, i as Feed, n as ELECTRICAL_SCHEMA, r as ElectricalLayer, t as Circuit } from "./layer-BxRcnHeC.js";
|
|
2
|
+
import { A as ratioLevel, C as NEAR_LOAD, D as loadCurrent, E as RATED_LIMIT, O as loadRatio, S as MISSING_LOADS, T as NodeKind, _ as DEFAULT_NEAR_LOAD, a as evaluateCircuit, b as LOAD_LEVELS, c as outletLevels, d as CIRCUIT_LIMIT, f as CIRCUIT_LOAD, g as DEFAULT_CONTINUOUS_LOAD, h as CONTINUOUS_LOAD, i as electricalSpace, k as ratedLimit, l as CIRCUIT_CURRENT, m as CIRCUIT_RATIO, n as LoadOptions, o as loadVars, p as CIRCUIT_LOADS, r as circuitStates, s as nodeKey, t as CircuitState, u as CIRCUIT_LEVEL, v as KNOWN_LOADS, w as NODE_KINDS, x as LoadLevel, y as LIMITS_DIFFER } from "./states-7rPTIEPc.js";
|
|
3
|
+
import { AddOnFault, Anchor, SceneSpace, WallFace } from "@mark1russell7/room-view";
|
|
4
|
+
/** This function gives the anchor of each outlet of a layer, by the `id` of the outlet. The first outlet of an `id` is used. */
|
|
5
|
+
export declare function outletAnchors(layer: ElectricalLayer): ReadonlyMap<string, Anchor>;
|
|
6
|
+
/** This function gives a layer with new anchors of its outlets, by the `id` of each outlet. An outlet with no new anchor keeps its anchor. */
|
|
7
|
+
export declare function withOutletAnchors(layer: ElectricalLayer, anchors: ReadonlyMap<string, Anchor>): ElectricalLayer;
|
|
8
|
+
/** These are the codes of the faults of an electrical layer. */
|
|
9
|
+
export declare const ELECTRICAL_CODES: readonly ["id-duplicate", "outlet-circuit", "feed-outlet", "feed-object", "feed-watts", "circuit-number", "circuit-no-limit", "circuit-limits", "circuit-over"];
|
|
10
|
+
/** A code of a fault of an electrical layer. */
|
|
11
|
+
export type ElectricalCode = (typeof ELECTRICAL_CODES)[number];
|
|
12
|
+
/** A fault of an electrical layer. `checkLayers` of the core gives it the name of the layer. */
|
|
13
|
+
export interface ElectricalFault extends AddOnFault {
|
|
14
|
+
readonly code: ElectricalCode;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* This function gives each fault of the meaning of an electrical layer, in the space of a scene.
|
|
18
|
+
*
|
|
19
|
+
* - An error: an `id` of two records, a circuit, an outlet or an object that is not there, and a number that is not valid.
|
|
20
|
+
* - A warning: a circuit with no limit, a limit that is not the rated limit, and a known load above the limit.
|
|
21
|
+
*
|
|
22
|
+
* The function does not examine the anchors: `checkLayers` of the core does. A warning is a fault that a viewer can show.
|
|
23
|
+
*/
|
|
24
|
+
export declare function checkElectrical(layer: ElectricalLayer, space: SceneSpace, options?: LoadOptions): readonly ElectricalFault[];
|
|
25
|
+
/**
|
|
26
|
+
* The electrical records of a `scene/1`: the outlets and the circuits of its room, the outlet of each object, and the loads.
|
|
27
|
+
* A scene record of the core has this form.
|
|
28
|
+
*/
|
|
29
|
+
export interface SceneV1 {
|
|
30
|
+
readonly room: {
|
|
31
|
+
readonly outlets?: readonly {
|
|
32
|
+
readonly id: string;
|
|
33
|
+
readonly wall: string;
|
|
34
|
+
readonly face: WallFace;
|
|
35
|
+
readonly offset: number;
|
|
36
|
+
readonly height: number;
|
|
37
|
+
readonly circuit?: string | undefined;
|
|
38
|
+
}[] | undefined;
|
|
39
|
+
readonly circuits?: readonly {
|
|
40
|
+
readonly id: string;
|
|
41
|
+
readonly name?: string | undefined;
|
|
42
|
+
readonly breaker?: number | undefined;
|
|
43
|
+
readonly voltage?: number | undefined;
|
|
44
|
+
readonly limit: number;
|
|
45
|
+
}[] | undefined;
|
|
46
|
+
};
|
|
47
|
+
readonly objects?: readonly {
|
|
48
|
+
readonly id: string;
|
|
49
|
+
readonly outlet?: string | undefined;
|
|
50
|
+
}[] | undefined;
|
|
51
|
+
readonly loads?: readonly {
|
|
52
|
+
readonly circuit: string;
|
|
53
|
+
readonly watts: number;
|
|
54
|
+
}[] | undefined;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* This function moves the electrical records of a `scene/1` to an electrical layer.
|
|
58
|
+
*
|
|
59
|
+
* - An outlet keeps its `id`, and its place on the wall becomes a wall anchor.
|
|
60
|
+
* - A circuit keeps its facts. The sum of its loads becomes its `otherLoad`.
|
|
61
|
+
* - The outlet of an object becomes a feed with no watts, with the `id` `feed-` and the `id` of the object.
|
|
62
|
+
*
|
|
63
|
+
* The ids of a layer are unique. Thus a circuit with the `id` of an outlet gets the `id` `circuit-` and its old `id`, and each record that names it follows.
|
|
64
|
+
* A load of a circuit that the room does not have has no place in a layer. `checkScene` of the core gives an error for it.
|
|
65
|
+
*/
|
|
66
|
+
export declare function fromSceneV1(scene: SceneV1): ElectricalLayer;
|
|
67
|
+
export { CIRCUIT_CURRENT, CIRCUIT_LEVEL, CIRCUIT_LIMIT, CIRCUIT_LOAD, CIRCUIT_LOADS, CIRCUIT_RATIO, CONTINUOUS_LOAD, Circuit, CircuitState, DEFAULT_CONTINUOUS_LOAD, DEFAULT_NEAR_LOAD, ELECTRICAL_SCHEMA, ElectricalLayer, Feed, KNOWN_LOADS, LIMITS_DIFFER, LOAD_LEVELS, LoadLevel, LoadOptions, MISSING_LOADS, NEAR_LOAD, NODE_KINDS, NodeKind, Outlet, RATED_LIMIT, circuitStates, electricalSpace, evaluateCircuit, loadCurrent, loadRatio, loadVars, nodeKey, outletLevels, ratedLimit, ratioLevel };
|
|
68
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/anchors.ts","../src/check.ts","../src/convert.ts"],"mappings":";;;;wBAMgB,cAAc,OAAO,kBAAkB,oBAAoB;;wBAO3D,kBAAkB,OAAO,iBAAiB,SAAS,oBAAoB,UAAU;;qBCLpF;;YAaD,yBAAyB;;iBAGpB,wBAAwB;WAC9B,MAAM;;;;;;;;;;wBA0BD,gBAAgB,OAAO,iBAAiB,OAAO,YAAY,UAAS,uBAA4B;;;;;iBC1C/F;WACN;aACE;eAEM;eACA;eACA,MAAM;eACN;eACA;eACA;;aAGN;eAEM;eACA;eACA;eACA;eACA;;;WAIR;aAA8B;aAAqB;;WACnD;aAA4B;aAA0B;;;;;;;;;;;;;wBAajD,YAAY,OAAO,UAAU"}
|
package/build/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { t as ELECTRICAL_SCHEMA } from "./layer-BFIDp4a1.js";
|
|
2
|
+
import { A as withOutletAnchors, C as NODE_KINDS, D as ratedLimit, E as loadRatio, O as ratioLevel, S as NEAR_LOAD, T as loadCurrent, _ as DEFAULT_NEAR_LOAD, a as evaluateCircuit, b as LOAD_LEVELS, c as outletLevels, d as CIRCUIT_LIMIT, f as CIRCUIT_LOAD, g as DEFAULT_CONTINUOUS_LOAD, h as CONTINUOUS_LOAD, i as electricalSpace, k as outletAnchors, l as CIRCUIT_CURRENT, m as CIRCUIT_RATIO, n as checkElectrical, o as loadVars, p as CIRCUIT_LOADS, r as circuitStates, s as nodeKey, t as ELECTRICAL_CODES, u as CIRCUIT_LEVEL, v as KNOWN_LOADS, w as RATED_LIMIT, x as MISSING_LOADS, y as LIMITS_DIFFER } from "./check-V0Af5oUe.js";
|
|
3
|
+
// src/convert.ts
|
|
4
|
+
/**
|
|
5
|
+
* This function moves the electrical records of a `scene/1` to an electrical layer.
|
|
6
|
+
*
|
|
7
|
+
* - An outlet keeps its `id`, and its place on the wall becomes a wall anchor.
|
|
8
|
+
* - A circuit keeps its facts. The sum of its loads becomes its `otherLoad`.
|
|
9
|
+
* - The outlet of an object becomes a feed with no watts, with the `id` `feed-` and the `id` of the object.
|
|
10
|
+
*
|
|
11
|
+
* The ids of a layer are unique. Thus a circuit with the `id` of an outlet gets the `id` `circuit-` and its old `id`, and each record that names it follows.
|
|
12
|
+
* A load of a circuit that the room does not have has no place in a layer. `checkScene` of the core gives an error for it.
|
|
13
|
+
*/
|
|
14
|
+
function fromSceneV1(scene) {
|
|
15
|
+
const used = /* @__PURE__ */ new Set();
|
|
16
|
+
const take = (id, prefix) => {
|
|
17
|
+
const base = used.has(id) ? `${prefix}-${id}` : id;
|
|
18
|
+
let found = base;
|
|
19
|
+
for (let copy = 2; used.has(found); copy += 1) found = `${base}-${copy}`;
|
|
20
|
+
used.add(found);
|
|
21
|
+
return found;
|
|
22
|
+
};
|
|
23
|
+
const outletIds = /* @__PURE__ */ new Map();
|
|
24
|
+
const outletIdOf = (old) => {
|
|
25
|
+
const id = take(old.id, "outlet");
|
|
26
|
+
if (!outletIds.has(old.id)) outletIds.set(old.id, id);
|
|
27
|
+
return id;
|
|
28
|
+
};
|
|
29
|
+
const oldOutlets = (scene.room.outlets ?? []).map((old) => ({
|
|
30
|
+
old,
|
|
31
|
+
id: outletIdOf(old)
|
|
32
|
+
}));
|
|
33
|
+
const circuitIds = /* @__PURE__ */ new Map();
|
|
34
|
+
const circuits = (scene.room.circuits ?? []).map((old) => {
|
|
35
|
+
const id = take(old.id, "circuit");
|
|
36
|
+
const first = !circuitIds.has(old.id);
|
|
37
|
+
if (first) circuitIds.set(old.id, id);
|
|
38
|
+
const loads = first ? (scene.loads ?? []).filter((load) => load.circuit === old.id) : [];
|
|
39
|
+
return {
|
|
40
|
+
id,
|
|
41
|
+
...old.name === void 0 ? {} : { name: old.name },
|
|
42
|
+
...old.breaker === void 0 ? {} : { breaker: old.breaker },
|
|
43
|
+
...old.voltage === void 0 ? {} : { voltage: old.voltage },
|
|
44
|
+
limit: old.limit,
|
|
45
|
+
...loads.length === 0 ? {} : { otherLoad: loads.reduce((sum, load) => sum + load.watts, 0) }
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
const outlets = oldOutlets.map(({ old, id }) => ({
|
|
49
|
+
id,
|
|
50
|
+
anchor: {
|
|
51
|
+
on: "wall",
|
|
52
|
+
wall: old.wall,
|
|
53
|
+
face: old.face,
|
|
54
|
+
offset: old.offset,
|
|
55
|
+
height: old.height
|
|
56
|
+
},
|
|
57
|
+
...old.circuit === void 0 ? {} : { circuit: circuitIds.get(old.circuit) ?? old.circuit }
|
|
58
|
+
}));
|
|
59
|
+
const feeds = [];
|
|
60
|
+
for (const object of scene.objects ?? []) {
|
|
61
|
+
if (object.outlet === void 0) continue;
|
|
62
|
+
feeds.push({
|
|
63
|
+
id: take(`feed-${object.id}`, "feed"),
|
|
64
|
+
object: object.id,
|
|
65
|
+
outlet: outletIds.get(object.outlet) ?? object.outlet
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
schema: ELECTRICAL_SCHEMA,
|
|
70
|
+
...outlets.length === 0 ? {} : { outlets },
|
|
71
|
+
...circuits.length === 0 ? {} : { circuits },
|
|
72
|
+
...feeds.length === 0 ? {} : { feeds }
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export { CIRCUIT_CURRENT, CIRCUIT_LEVEL, CIRCUIT_LIMIT, CIRCUIT_LOAD, CIRCUIT_LOADS, CIRCUIT_RATIO, CONTINUOUS_LOAD, DEFAULT_CONTINUOUS_LOAD, DEFAULT_NEAR_LOAD, ELECTRICAL_CODES, ELECTRICAL_SCHEMA, KNOWN_LOADS, LIMITS_DIFFER, LOAD_LEVELS, MISSING_LOADS, NEAR_LOAD, NODE_KINDS, RATED_LIMIT, checkElectrical, circuitStates, electricalSpace, evaluateCircuit, fromSceneV1, loadCurrent, loadRatio, loadVars, nodeKey, outletAnchors, outletLevels, ratedLimit, ratioLevel, withOutletAnchors };
|
|
76
|
+
|
|
77
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/convert.ts"],"sourcesContent":["import type { WallFace } from \"@mark1russell7/room-view\";\nimport { ELECTRICAL_SCHEMA, type Circuit, type ElectricalLayer, type Feed, type Outlet } from \"./layer.ts\";\n\n// The way from the electrical records of a `scene/1` to an electrical layer.\n\n/**\n * The electrical records of a `scene/1`: the outlets and the circuits of its room, the outlet of each object, and the loads.\n * A scene record of the core has this form.\n */\nexport interface SceneV1 {\n readonly room: {\n readonly outlets?:\n | readonly {\n readonly id: string;\n readonly wall: string;\n readonly face: WallFace;\n readonly offset: number;\n readonly height: number;\n readonly circuit?: string | undefined;\n }[]\n | undefined;\n readonly circuits?:\n | readonly {\n readonly id: string;\n readonly name?: string | undefined;\n readonly breaker?: number | undefined;\n readonly voltage?: number | undefined;\n readonly limit: number;\n }[]\n | undefined;\n };\n readonly objects?: readonly { readonly id: string; readonly outlet?: string | undefined }[] | undefined;\n readonly loads?: readonly { readonly circuit: string; readonly watts: number }[] | undefined;\n}\n\n/**\n * This function moves the electrical records of a `scene/1` to an electrical layer.\n *\n * - An outlet keeps its `id`, and its place on the wall becomes a wall anchor.\n * - A circuit keeps its facts. The sum of its loads becomes its `otherLoad`.\n * - The outlet of an object becomes a feed with no watts, with the `id` `feed-` and the `id` of the object.\n *\n * The ids of a layer are unique. Thus a circuit with the `id` of an outlet gets the `id` `circuit-` and its old `id`, and each record that names it follows.\n * A load of a circuit that the room does not have has no place in a layer. `checkScene` of the core gives an error for it.\n */\nexport function fromSceneV1(scene: SceneV1): ElectricalLayer {\n const used = new Set<string>();\n const take = (id: string, prefix: string): string => {\n const base = used.has(id) ? `${prefix}-${id}` : id;\n let found = base;\n for (let copy = 2; used.has(found); copy += 1) found = `${base}-${copy}`;\n used.add(found);\n return found;\n };\n\n // The outlets come first, so each outlet keeps its `id`: a mark and a selection name an outlet.\n const outletIds = new Map<string, string>();\n const outletIdOf = (old: { readonly id: string }): string => {\n const id = take(old.id, \"outlet\");\n if (!outletIds.has(old.id)) outletIds.set(old.id, id);\n return id;\n };\n const oldOutlets = (scene.room.outlets ?? []).map((old) => ({ old, id: outletIdOf(old) }));\n\n const circuitIds = new Map<string, string>();\n const circuits = (scene.room.circuits ?? []).map((old): Circuit => {\n const id = take(old.id, \"circuit\");\n const first = !circuitIds.has(old.id);\n if (first) circuitIds.set(old.id, id);\n const loads = first ? (scene.loads ?? []).filter((load) => load.circuit === old.id) : [];\n return {\n id,\n ...(old.name === undefined ? {} : { name: old.name }),\n ...(old.breaker === undefined ? {} : { breaker: old.breaker }),\n ...(old.voltage === undefined ? {} : { voltage: old.voltage }),\n limit: old.limit,\n ...(loads.length === 0 ? {} : { otherLoad: loads.reduce((sum, load) => sum + load.watts, 0) }),\n };\n });\n\n const outlets = oldOutlets.map(\n ({ old, id }): Outlet => ({\n id,\n anchor: { on: \"wall\", wall: old.wall, face: old.face, offset: old.offset, height: old.height },\n ...(old.circuit === undefined ? {} : { circuit: circuitIds.get(old.circuit) ?? old.circuit }),\n }),\n );\n\n const feeds: Feed[] = [];\n for (const object of scene.objects ?? []) {\n if (object.outlet === undefined) continue;\n feeds.push({ id: take(`feed-${object.id}`, \"feed\"), object: object.id, outlet: outletIds.get(object.outlet) ?? object.outlet });\n }\n\n return {\n schema: ELECTRICAL_SCHEMA,\n ...(outlets.length === 0 ? {} : { outlets }),\n ...(circuits.length === 0 ? {} : { circuits }),\n ...(feeds.length === 0 ? {} : { feeds }),\n };\n}\n"],"mappings":";;;;;;;;;;;;;AA6CA,SAAgB,YAAY,OAAiC;CAC3D,MAAM,uBAAO,IAAI,IAAY;CAC7B,MAAM,QAAQ,IAAY,WAA2B;EACnD,MAAM,OAAO,KA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AED,MAAM,UAAU,WAAW,KACxB,EAAE,KAAK,UAAkB;EACxB;EACA,QAAQ;GAAE,IAAI;GAAQ,MAAM,IAAI;GAAM,MAAM,IAAI;GAAM,QAAQ,IAAI;GAAQ,QAAQ,IAAI;EAAO;EAC7F,GAAI,IAAI,YAAY,KAAA,IAAY,CAAC,IAAI,EAAE,SAAS,WAAW,IAAI,IAAI,OAAO,KAAK,IAAI,QAAQ;CAC7F,EACF;CAEA,MAAM,QAAgB,CAAC;CACvB,KAAK,MAAM,UAAU,MAAM,WAAW,CAAC,GAAG;EACxC,IAAI,OAAO,WAAW,KAAA,GAAW;EACjC,MAAM,KAAK;GAAE,IAAI,KAAK,QAAQ,OAAO,MAAM,MAAM;GAAG,QAAQ,OAAO;GAAI,QAAQ,UAAU,IAAI,OAAO,MAAM,KAAK,OAAO;EAAO,CAAC;CAChI;CAEA,OAAO;EACL,QAAQ;EACR,GAAI,QAAQ,WAAW,IAAI,CAAC,IAAI,EAAE,QAAQ;EAC1C,GAAI,SAAS,WAAW,IAAI,CAAC,IAAI,EAAE,SAAS;EAC5C,GAAI,MAAM,WAAW,IAAI,CAAC,IAAI,EAAE,MAAM;CACxC;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layer-BFIDp4a1.js","names":[],"sources":["../src/layer.ts"],"sourcesContent":["import type { Anchor } from \"@mark1russell7/room-view\";\n\n// The records of the electrical add-on. They are one layer of a scene, with the key `electrical/1` in `Scene.layers`.\n// The units are fixed: a breaker in amperes, a voltage in volts, and a limit and a load in watts.\n\n/** The name of the format of an electrical layer. It is the key of the layer in `Scene.layers`. */\nexport const ELECTRICAL_SCHEMA = \"electrical/1\";\n\n/**\n * An outlet: a point that gives power, on an anchor of the core.\n * An outlet can be on a wall, on a floor or on an object, for example a power strip on a desk.\n */\nexport interface Outlet {\n readonly id: string;\n readonly anchor: Anchor;\n /** The `id` of the circuit of the outlet. */\n readonly circuit?: string | undefined;\n}\n\n/** A circuit: it gives power to outlets, and it has a limit. */\nexport interface Circuit {\n readonly id: string;\n readonly name?: string | undefined;\n /** The rating of the breaker of the circuit, in amperes, for example 15. */\n readonly breaker?: number | undefined;\n /** The voltage of the circuit, in volts, for example 120. */\n readonly voltage?: number | undefined;\n /**\n * The largest continuous load, in watts.\n * Without it, the limit is the voltage times the breaker times the continuous part. Thus 15 A at 120 V gives 1440 W.\n */\n readonly limit?: number | undefined;\n /** The load of the circuit that no feed gives, in watts. Examples are the lamps of a ceiling and the reading of a meter. */\n readonly otherLoad?: number | undefined;\n}\n\n/** A feed: the power of a placed object from an outlet. An object with two power supplies has two feeds. */\nexport interface Feed {\n readonly id: string;\n /** The `id` of the placed object of the scene. */\n readonly object: string;\n /** The `id` of the outlet. */\n readonly outlet: string;\n /** The power that the object takes from the outlet, in watts. Without it, the power is not known. */\n readonly watts?: number | undefined;\n}\n\n/**\n * The electrical layer of a scene: the outlets, the circuits and the feeds.\n * Each record has an `id`, and the ids of one layer are unique. Thus `recordName(\"electrical/1\", id)` names one record.\n */\nexport interface ElectricalLayer {\n readonly schema: typeof ELECTRICAL_SCHEMA;\n readonly outlets?: readonly Outlet[] | undefined;\n readonly circuits?: readonly Circuit[] | undefined;\n readonly feeds?: readonly Feed[] | undefined;\n}\n"],"mappings":";;AAMA,MAAa,oBAAoB"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Anchor } from "@mark1russell7/room-view";
|
|
2
|
+
/** The name of the format of an electrical layer. It is the key of the layer in `Scene.layers`. */
|
|
3
|
+
declare const ELECTRICAL_SCHEMA = "electrical/1";
|
|
4
|
+
/**
|
|
5
|
+
* An outlet: a point that gives power, on an anchor of the core.
|
|
6
|
+
* An outlet can be on a wall, on a floor or on an object, for example a power strip on a desk.
|
|
7
|
+
*/
|
|
8
|
+
interface Outlet {
|
|
9
|
+
readonly id: string;
|
|
10
|
+
readonly anchor: Anchor;
|
|
11
|
+
/** The `id` of the circuit of the outlet. */
|
|
12
|
+
readonly circuit?: string | undefined;
|
|
13
|
+
}
|
|
14
|
+
/** A circuit: it gives power to outlets, and it has a limit. */
|
|
15
|
+
interface Circuit {
|
|
16
|
+
readonly id: string;
|
|
17
|
+
readonly name?: string | undefined;
|
|
18
|
+
/** The rating of the breaker of the circuit, in amperes, for example 15. */
|
|
19
|
+
readonly breaker?: number | undefined;
|
|
20
|
+
/** The voltage of the circuit, in volts, for example 120. */
|
|
21
|
+
readonly voltage?: number | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* The largest continuous load, in watts.
|
|
24
|
+
* Without it, the limit is the voltage times the breaker times the continuous part. Thus 15 A at 120 V gives 1440 W.
|
|
25
|
+
*/
|
|
26
|
+
readonly limit?: number | undefined;
|
|
27
|
+
/** The load of the circuit that no feed gives, in watts. Examples are the lamps of a ceiling and the reading of a meter. */
|
|
28
|
+
readonly otherLoad?: number | undefined;
|
|
29
|
+
}
|
|
30
|
+
/** A feed: the power of a placed object from an outlet. An object with two power supplies has two feeds. */
|
|
31
|
+
interface Feed {
|
|
32
|
+
readonly id: string;
|
|
33
|
+
/** The `id` of the placed object of the scene. */
|
|
34
|
+
readonly object: string;
|
|
35
|
+
/** The `id` of the outlet. */
|
|
36
|
+
readonly outlet: string;
|
|
37
|
+
/** The power that the object takes from the outlet, in watts. Without it, the power is not known. */
|
|
38
|
+
readonly watts?: number | undefined;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The electrical layer of a scene: the outlets, the circuits and the feeds.
|
|
42
|
+
* Each record has an `id`, and the ids of one layer are unique. Thus `recordName("electrical/1", id)` names one record.
|
|
43
|
+
*/
|
|
44
|
+
interface ElectricalLayer {
|
|
45
|
+
readonly schema: typeof ELECTRICAL_SCHEMA;
|
|
46
|
+
readonly outlets?: readonly Outlet[] | undefined;
|
|
47
|
+
readonly circuits?: readonly Circuit[] | undefined;
|
|
48
|
+
readonly feeds?: readonly Feed[] | undefined;
|
|
49
|
+
}
|
|
50
|
+
export { Outlet as a, Feed as i, ELECTRICAL_SCHEMA as n, ElectricalLayer as r, Circuit as t };
|
|
51
|
+
//# sourceMappingURL=layer-BxRcnHeC.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layer-BxRcnHeC.d.ts","names":[],"sources":["../src/layer.ts"],"mappings":";;cAMa;;;;;UAMI;WACN;WACA,QAAQ;;WAER;;;UAIM;WACN;WACA;;WAEA;;WAEA;;;;;WAKA;;WAEA;;;UAIM;WACN;;WAEA;;WAEA;;WAEA;;;;;;UAOM;WACN,eAAe;WACf,mBAAmB;WACnB,oBAAoB;WACpB,iBAAiB"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { r as ElectricalLayer } from "./layer-BxRcnHeC.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { LayerParse } from "@mark1russell7/room-view";
|
|
4
|
+
/**
|
|
5
|
+
* The schema of an electrical layer. It examines the form of the record: the fields, their types and the finite numbers.
|
|
6
|
+
* It refuses a field that the format does not have. `checkElectrical` examines the meaning of a layer with a correct form.
|
|
7
|
+
*/
|
|
8
|
+
export declare const electricalSchema: z.ZodType<ElectricalLayer>;
|
|
9
|
+
/**
|
|
10
|
+
* This function reads the JSON of an electrical layer. It gives the layer, or the faults of its form.
|
|
11
|
+
* Each fault names its place in the record, for example `outlets.0.anchor`.
|
|
12
|
+
*/
|
|
13
|
+
export declare function parseElectrical(value: unknown): LayerParse<ElectricalLayer>;
|
|
14
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","names":[],"sources":["../src/schema.ts"],"mappings":";;;;;;;qBAoCa,kBAAkB,EAAE,QAAQ;;;;;wBAWzB,gBAAgB,iBAAiB,WAAW"}
|
package/build/schema.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { t as ELECTRICAL_SCHEMA } from "./layer-BFIDp4a1.js";
|
|
2
|
+
import { anchorSchema } from "@mark1russell7/room-view/schema";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
// src/schema.ts
|
|
5
|
+
const id = z.string().min(1);
|
|
6
|
+
const outlet = z.strictObject({
|
|
7
|
+
id,
|
|
8
|
+
anchor: anchorSchema,
|
|
9
|
+
circuit: id.optional()
|
|
10
|
+
});
|
|
11
|
+
const circuit = z.strictObject({
|
|
12
|
+
id,
|
|
13
|
+
name: z.string().optional(),
|
|
14
|
+
breaker: z.number().optional(),
|
|
15
|
+
voltage: z.number().optional(),
|
|
16
|
+
limit: z.number().optional(),
|
|
17
|
+
otherLoad: z.number().optional()
|
|
18
|
+
});
|
|
19
|
+
const feed = z.strictObject({
|
|
20
|
+
id,
|
|
21
|
+
object: id,
|
|
22
|
+
outlet: id,
|
|
23
|
+
watts: z.number().optional()
|
|
24
|
+
});
|
|
25
|
+
/**
|
|
26
|
+
* The schema of an electrical layer. It examines the form of the record: the fields, their types and the finite numbers.
|
|
27
|
+
* It refuses a field that the format does not have. `checkElectrical` examines the meaning of a layer with a correct form.
|
|
28
|
+
*/
|
|
29
|
+
const electricalSchema = z.strictObject({
|
|
30
|
+
schema: z.literal(ELECTRICAL_SCHEMA),
|
|
31
|
+
outlets: z.array(outlet).readonly().optional(),
|
|
32
|
+
circuits: z.array(circuit).readonly().optional(),
|
|
33
|
+
feeds: z.array(feed).readonly().optional()
|
|
34
|
+
});
|
|
35
|
+
/**
|
|
36
|
+
* This function reads the JSON of an electrical layer. It gives the layer, or the faults of its form.
|
|
37
|
+
* Each fault names its place in the record, for example `outlets.0.anchor`.
|
|
38
|
+
*/
|
|
39
|
+
function parseElectrical(value) {
|
|
40
|
+
const read = electricalSchema.safeParse(value);
|
|
41
|
+
if (read.success) return {
|
|
42
|
+
ok: true,
|
|
43
|
+
layer: read.data
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
ok: false,
|
|
47
|
+
errors: read.error.issues.map((issue) => {
|
|
48
|
+
const place = issue.path.length === 0 ? "the record" : `"${issue.path.map(String).join(".")}"`;
|
|
49
|
+
return `The layer "${ELECTRICAL_SCHEMA}" has a fault at ${place}: ${issue.message}`;
|
|
50
|
+
})
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export { electricalSchema, parseElectrical };
|
|
54
|
+
|
|
55
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","names":[],"sources":["../src/schema.ts"],"sourcesContent":["import type { LayerParse } from \"@mark1russell7/room-view\";\nimport { anchorSchema } from \"@mark1russell7/room-view/schema\";\nimport { z } from \"zod\";\nimport { ELECTRICAL_SCHEMA, type ElectricalLayer } from \"./layer.ts\";\n\n// The zod schema of an electrical layer, and the read of its JSON. This module is the entry `/schema` of the package.\n// The root entry has no zod, so a page that reads only the circuit math gets no schema library.\n\nconst id = z.string().min(1);\n\nconst outlet = z.strictObject({\n id,\n anchor: anchorSchema,\n circuit: id.optional(),\n});\n\nconst circuit = z.strictObject({\n id,\n name: z.string().optional(),\n breaker: z.number().optional(),\n voltage: z.number().optional(),\n limit: z.number().optional(),\n otherLoad: z.number().optional(),\n});\n\nconst feed = z.strictObject({\n id,\n object: id,\n outlet: id,\n watts: z.number().optional(),\n});\n\n/**\n * The schema of an electrical layer. It examines the form of the record: the fields, their types and the finite numbers.\n * It refuses a field that the format does not have. `checkElectrical` examines the meaning of a layer with a correct form.\n */\nexport const electricalSchema: z.ZodType<ElectricalLayer> = z.strictObject({\n schema: z.literal(ELECTRICAL_SCHEMA),\n outlets: z.array(outlet).readonly().optional(),\n circuits: z.array(circuit).readonly().optional(),\n feeds: z.array(feed).readonly().optional(),\n});\n\n/**\n * This function reads the JSON of an electrical layer. It gives the layer, or the faults of its form.\n * Each fault names its place in the record, for example `outlets.0.anchor`.\n */\nexport function parseElectrical(value: unknown): LayerParse<ElectricalLayer> {\n const read = electricalSchema.safeParse(value);\n if (read.success) return { ok: true, layer: read.data };\n return {\n ok: false,\n errors: read.error.issues.map((issue) => {\n const place = issue.path.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AAK,GAAG,EAAE;GAC5F,OAAO,cAAc,kBAAkB,mBAAmB,MAAM,IAAI,MAAM;EAC5E,CAAC;CACH;AACF"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { r as ElectricalLayer } from "./layer-BxRcnHeC.js";
|
|
2
|
+
import { Axis, Expr, Result, Space } from "@mark1russell7/vex";
|
|
3
|
+
/** The name of the variable with the continuous part: the part of the rating of a breaker that a continuous load can use. */
|
|
4
|
+
declare const CONTINUOUS_LOAD = "continuousLoad";
|
|
5
|
+
/** The default continuous part. A continuous load can use 80% of the rating of a breaker. */
|
|
6
|
+
declare const DEFAULT_CONTINUOUS_LOAD = .8;
|
|
7
|
+
/** The name of the variable with the near part: a load with this part of its limit, or more, has the level `near`. */
|
|
8
|
+
declare const NEAR_LOAD = "nearLoad";
|
|
9
|
+
/** The default near part. */
|
|
10
|
+
declare const DEFAULT_NEAR_LOAD = .8;
|
|
11
|
+
/** These are the levels of the load of a circuit. */
|
|
12
|
+
declare const LOAD_LEVELS: readonly ["unknown", "ok", "near", "over"];
|
|
13
|
+
/** A level of the load of a circuit. A viewer gives each level a color and a shape. */
|
|
14
|
+
type LoadLevel = (typeof LOAD_LEVELS)[number];
|
|
15
|
+
/**
|
|
16
|
+
* This function gives the program of the limit of a continuous load, in watts: the voltage times the breaker times the continuous part.
|
|
17
|
+
* The default part is the variable `continuousLoad`.
|
|
18
|
+
*/
|
|
19
|
+
declare function ratedLimit(voltage: Expr, breaker: Expr, part?: Expr): Expr;
|
|
20
|
+
/** This function gives the program of the current of a load, in amperes: the load in watts divided by the voltage. */
|
|
21
|
+
declare function loadCurrent(load: Expr, voltage: Expr): Expr;
|
|
22
|
+
/**
|
|
23
|
+
* This function gives the program of a load as a part of its limit.
|
|
24
|
+
* A limit that is not above 0 gives an error value, not a ratio.
|
|
25
|
+
*/
|
|
26
|
+
declare function loadRatio(load: Expr, limit: Expr): Expr;
|
|
27
|
+
/**
|
|
28
|
+
* This function gives the program of the level of a ratio: `over` above 1, `near` from the near part, or `ok`.
|
|
29
|
+
* The default near part is the variable `nearLoad`. The program evaluates the ratio one time, with the name `loadRatio`.
|
|
30
|
+
*/
|
|
31
|
+
declare function ratioLevel(ratio: Expr, near?: Expr): Expr;
|
|
32
|
+
/** These are the kinds of the nodes of the electrical tree. The node `other` has the other load of its circuit. */
|
|
33
|
+
declare const NODE_KINDS: readonly ["circuit", "outlet", "feed", "other"];
|
|
34
|
+
/** A kind of node of the electrical tree. */
|
|
35
|
+
type NodeKind = (typeof NODE_KINDS)[number];
|
|
36
|
+
/** The loads below a circuit: the feeds of its outlets, and its other load. Each load has its power in the field `watts`. */
|
|
37
|
+
declare const CIRCUIT_LOADS: Axis;
|
|
38
|
+
/** The rated limit of a circuit, in watts, from its voltage and its breaker. A circuit without one of the two gives `#N/A`. */
|
|
39
|
+
declare const RATED_LIMIT: Expr;
|
|
40
|
+
/** The limit of a circuit, in watts: its field `limit`, or its rated limit. A circuit with no limit gives `#N/A`. */
|
|
41
|
+
declare const CIRCUIT_LIMIT: Expr;
|
|
42
|
+
/** The known load of a circuit, in watts: the sum of the watts of its loads. A feed with no watts adds nothing. */
|
|
43
|
+
declare const CIRCUIT_LOAD: Expr;
|
|
44
|
+
/** The count of the loads of a circuit that have watts. */
|
|
45
|
+
declare const KNOWN_LOADS: Expr;
|
|
46
|
+
/** The keys of the feeds of a circuit that have no watts. With one or more, the true load can be above the known load. */
|
|
47
|
+
declare const MISSING_LOADS: Expr;
|
|
48
|
+
/** The known load of a circuit as a part of its limit. A circuit with no limit above 0 gives an error value. */
|
|
49
|
+
declare const CIRCUIT_RATIO: Expr;
|
|
50
|
+
/** The current of the known load of a circuit, in amperes. A circuit with no voltage gives `#N/A`. */
|
|
51
|
+
declare const CIRCUIT_CURRENT: Expr;
|
|
52
|
+
/**
|
|
53
|
+
* The level of a circuit, for its known load.
|
|
54
|
+
*
|
|
55
|
+
* - `unknown`: no load of the circuit has watts, or the circuit has no limit above 0.
|
|
56
|
+
* - `over`: the load is above the limit.
|
|
57
|
+
* - `near`: the load is the near part of the limit, or more.
|
|
58
|
+
* - `ok`: each other circuit.
|
|
59
|
+
*/
|
|
60
|
+
declare const CIRCUIT_LEVEL: Expr;
|
|
61
|
+
/**
|
|
62
|
+
* This program tells if the field `limit` of a circuit and its rated limit are not equal.
|
|
63
|
+
* A circuit without a limit, a voltage or a breaker gives `#N/A`: it has no two limits to compare.
|
|
64
|
+
*/
|
|
65
|
+
declare const LIMITS_DIFFER: Expr;
|
|
66
|
+
/** This function gives the key of a node of the electrical tree: its kind, a colon and the `id` of its record. */
|
|
67
|
+
declare function nodeKey(kind: NodeKind, id: string): string;
|
|
68
|
+
/**
|
|
69
|
+
* This function gives the tree of a layer, as a space of Vex. The programs of a circuit evaluate at the key of its node.
|
|
70
|
+
*
|
|
71
|
+
* - A circuit is a root. Its fields are `breaker`, `voltage` and `limit`.
|
|
72
|
+
* - An outlet is a child of its circuit, and a feed is a child of its outlet. A feed has the field `watts`.
|
|
73
|
+
* - The other load of a circuit is a child of the circuit, with the kind `other` and the field `watts`.
|
|
74
|
+
*
|
|
75
|
+
* A record that names no parent of the layer is a root, so no circuit has it. The first record of an `id` is used.
|
|
76
|
+
*/
|
|
77
|
+
declare function electricalSpace(layer: ElectricalLayer): Space;
|
|
78
|
+
/** The options of the limits and the levels. */
|
|
79
|
+
interface LoadOptions {
|
|
80
|
+
/** The part of the rating of a breaker that a continuous load can use. The default is 0.8. */
|
|
81
|
+
readonly continuousLoad?: number | undefined;
|
|
82
|
+
/** A circuit with this part of its limit, or more, has the level `near`. The default is 0.8. */
|
|
83
|
+
readonly near?: number | undefined;
|
|
84
|
+
}
|
|
85
|
+
/** This function gives the variables of the programs, from the options. */
|
|
86
|
+
declare function loadVars(options?: LoadOptions): Readonly<Record<string, number>>;
|
|
87
|
+
/**
|
|
88
|
+
* This function evaluates a program of `formulas.ts` at a circuit of a tree. It gives the value, or the error value.
|
|
89
|
+
* `tree` is the result of `electricalSpace`, and `circuit` is the `id` of the circuit.
|
|
90
|
+
*/
|
|
91
|
+
declare function evaluateCircuit(program: Expr, tree: Space, circuit: string, options?: LoadOptions): Result<unknown>;
|
|
92
|
+
/** The state of one circuit of a layer. */
|
|
93
|
+
interface CircuitState {
|
|
94
|
+
/** The `id` of the circuit. */
|
|
95
|
+
readonly circuit: string;
|
|
96
|
+
/** The known load of the circuit, in watts: the sum of the watts of its feeds, plus its other load. */
|
|
97
|
+
readonly watts: number;
|
|
98
|
+
/** The limit of the circuit, in watts. A circuit with no limit has none. */
|
|
99
|
+
readonly limit?: number | undefined;
|
|
100
|
+
/** The known load as a part of the limit. A circuit with no limit above 0 has none. */
|
|
101
|
+
readonly ratio?: number | undefined;
|
|
102
|
+
/** The level of the known load. */
|
|
103
|
+
readonly level: LoadLevel;
|
|
104
|
+
/** The `id` of each feed of the circuit with no watts. With one or more, the true load can be above `watts`. */
|
|
105
|
+
readonly missing: readonly string[];
|
|
106
|
+
/** The `id` of each outlet of the circuit. */
|
|
107
|
+
readonly outlets: readonly string[];
|
|
108
|
+
/** The `id` of each feed from an outlet of the circuit. */
|
|
109
|
+
readonly feeds: readonly string[];
|
|
110
|
+
/** The `id` of each placed object with a feed from an outlet of the circuit. */
|
|
111
|
+
readonly objects: readonly string[];
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* This function gives the state of each circuit of a layer, in the sequence of the circuits.
|
|
115
|
+
*
|
|
116
|
+
* The level is the level of the known load. A feed with no watts adds nothing to the load, and `missing` names it.
|
|
117
|
+
* A circuit with no known load has the level `unknown`, and so has a circuit with no limit above 0.
|
|
118
|
+
*/
|
|
119
|
+
declare function circuitStates(layer: ElectricalLayer, options?: LoadOptions): readonly CircuitState[];
|
|
120
|
+
/**
|
|
121
|
+
* This function gives the level of each outlet of a layer: the level of its circuit. The key is the `id` of the outlet.
|
|
122
|
+
* An outlet with no circuit of the layer has no level, and the map does not have it.
|
|
123
|
+
*/
|
|
124
|
+
declare function outletLevels(layer: ElectricalLayer, options?: LoadOptions): ReadonlyMap<string, LoadLevel>;
|
|
125
|
+
export { ratioLevel as A, NEAR_LOAD as C, loadCurrent as D, RATED_LIMIT as E, loadRatio as O, MISSING_LOADS as S, NodeKind as T, DEFAULT_NEAR_LOAD as _, evaluateCircuit as a, LOAD_LEVELS as b, outletLevels as c, CIRCUIT_LIMIT as d, CIRCUIT_LOAD as f, DEFAULT_CONTINUOUS_LOAD as g, CONTINUOUS_LOAD as h, electricalSpace as i, ratedLimit as k, CIRCUIT_CURRENT as l, CIRCUIT_RATIO as m, LoadOptions as n, loadVars as o, CIRCUIT_LOADS as p, circuitStates as r, nodeKey as s, CircuitState as t, CIRCUIT_LEVEL as u, KNOWN_LOADS as v, NODE_KINDS as w, LoadLevel as x, LIMITS_DIFFER as y };
|
|
126
|
+
//# sourceMappingURL=states-7rPTIEPc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"states-7rPTIEPc.d.ts","names":[],"sources":["../src/formulas.ts","../src/states.ts"],"mappings":";;;cAOa;;cAGA;;cAGA;;cAGA;;cAGA;;KAGD,oBAAoB;;;;;iBAMhB,WAAW,SAAS,MAAM,SAAS,MAAM,OAAM,OAA4B;;iBAK3E,YAAY,MAAM,MAAM,SAAS,OAAO;;;;;iBAQxC,UAAU,MAAM,MAAM,OAAO,OAAO;;;;;iBAQpC,WAAW,OAAO,MAAM,OAAM,OAAsB;;cAMvD;;KAGD,mBAAmB;;cAQlB,eAAe;;cAMf,aAAa;;cAGb,eAAe;;cAGf,cAAc;;cAGd,aAAa;;cAMb,eAAe;;cAGf,eAAe;;cAGf,iBAAiB;;;;;;;;;cAUjB,eAAe;;;;;cASf,eAAe;;iBC3FZ,QAAQ,MAAM,UAAU;;;;;;;;;;iBAaxB,gBAAgB,OAAO,kBAAkB;;UA6BxC;;WAEN;;WAEA;;;iBAIK,SAAS,UAAS,cAAmB,SAAS;;;;;iBAQ9C,gBAAgB,SAAS,MAAM,MAAM,OAAO,iBAAiB,UAAS,cAAmB;;UAKxF;;WAEN;;WAEA;;WAEA;;WAEA;;WAEA,OAAO;;WAEP;;WAEA;;WAEA;;WAEA;;;;;;;;iBA8BK,cAAc,OAAO,iBAAiB,UAAS,uBAA4B;;;;;iBAoC3E,aAAa,OAAO,iBAAiB,UAAS,cAAmB,oBAAoB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,77 @@
|
|
|
1
1
|
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/package",
|
|
2
3
|
"name": "@mark1russell7/electrical",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "Electrical circuits as data: outlets, circuits, feeds and loads, the continuous-load check, and the circuit formulas as Vex programs. An add-on of room-view.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/mark1russell7/room-view.git",
|
|
10
|
+
"directory": "packages/electrical"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://mark1russell7.github.io/room-view/",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/mark1russell7/room-view/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"electrical",
|
|
18
|
+
"circuits",
|
|
19
|
+
"breakers",
|
|
20
|
+
"outlets",
|
|
21
|
+
"loads",
|
|
22
|
+
"continuous-load",
|
|
23
|
+
"room",
|
|
24
|
+
"vex"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"main": "./build/index.js",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./build/index.d.ts",
|
|
32
|
+
"import": "./build/index.js",
|
|
33
|
+
"default": "./build/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./schema": {
|
|
36
|
+
"types": "./build/schema.d.ts",
|
|
37
|
+
"import": "./build/schema.js",
|
|
38
|
+
"default": "./build/schema.js"
|
|
39
|
+
},
|
|
40
|
+
"./add-on": {
|
|
41
|
+
"types": "./build/add-on.d.ts",
|
|
42
|
+
"import": "./build/add-on.js",
|
|
43
|
+
"default": "./build/add-on.js"
|
|
44
|
+
},
|
|
45
|
+
"./package.json": "./package.json"
|
|
46
|
+
},
|
|
47
|
+
"files": [
|
|
48
|
+
"build"
|
|
49
|
+
],
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@mark1russell7/room-view": "0.1.4",
|
|
52
|
+
"@mark1russell7/vex": "^0.19.1",
|
|
53
|
+
"@mark1russell7/vex-domains": "^0.19.1",
|
|
54
|
+
"zod": "^4.6.7"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@mark1russell7/cue": "^2.0.0",
|
|
58
|
+
"@types/node": "^26.6.5",
|
|
59
|
+
"@vitest/coverage-v8": "^5.0.3",
|
|
60
|
+
"tsdown": "^0.23.1",
|
|
61
|
+
"typescript": "^7.0.2",
|
|
62
|
+
"vitest": "^5.0.3"
|
|
63
|
+
},
|
|
64
|
+
"engines": {
|
|
65
|
+
"node": ">=22"
|
|
66
|
+
},
|
|
67
|
+
"publishConfig": {
|
|
68
|
+
"access": "public"
|
|
69
|
+
},
|
|
70
|
+
"scripts": {
|
|
71
|
+
"build": "tsdown",
|
|
72
|
+
"test": "vitest run",
|
|
73
|
+
"test:watch": "vitest",
|
|
74
|
+
"typecheck": "tsc -p tsconfig.json && tsc -p tsconfig.test.json"
|
|
75
|
+
},
|
|
76
|
+
"types": "./build/index.d.ts"
|
|
6
77
|
}
|