Files
@ 94b58da02abc
Branch filter:
Location: light9/light9/web/live/Effect.ts
94b58da02abc
7.4 KiB
video/MP2T
switch to udmx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | import debug from "debug";
import { Literal, NamedNode, Quad_Object, Quad_Predicate, Quad_Subject, Term } from "n3";
import { some } from "underscore";
import { Patch, patchContainsPreds, patchUpdate } from "../patch";
import { SyncedGraph } from "../SyncedGraph";
type Color = string;
export type ControlValue = number | Color | NamedNode;
const log = debug("effect");
function isUri(x: Term | number | string): x is NamedNode {
return typeof x == "object" && x.termType == "NamedNode";
}
function valuePred(graph: SyncedGraph, attr: NamedNode): NamedNode {
const U = graph.U();
const scaledAttributeTypes = [U(":color"), U(":brightness"), U(":uv")];
if (some(scaledAttributeTypes, (x: NamedNode) => attr.equals(x))) {
return U(":scaledValue");
} else {
return U(":value");
}
}
// effect settings data; r/w sync with the graph
export class Effect {
private settings: Array<{ device: NamedNode; deviceAttr: NamedNode; setting: NamedNode; value: ControlValue }> = [];
private ctxForEffect: NamedNode
constructor(
public graph: SyncedGraph,
public uri: NamedNode,
// called if the graph changes our values and not when the caller uses edit()
private onValuesChanged: (values: void) => void
) {
this.ctxForEffect = this.graph.Uri(this.uri.value.replace("light9.bigasterisk.com/effect", "light9.bigasterisk.com/show/dance2019/effect"));
graph.runHandler(this.rebuildSettingsFromGraph.bind(this), `effect sync ${uri.value}`);
}
addNewEffectToGraph() {
const U = this.graph.U();
const quad = (s: Quad_Subject, p: Quad_Predicate, o: Quad_Object) => this.graph.Quad(s, p, o, this.ctxForEffect);
const addQuads = [
quad(this.uri, U("rdf:type"), U(":Effect")),
quad(this.uri, U("rdfs:label"), this.graph.Literal(this.uri.value.replace(/.*\//, ""))),
quad(this.uri, U(":publishAttr"), U(":strength")),
];
const patch = { adds: addQuads, dels: [] } as Patch;
log("init new effect", patch);
this.settings = [];
this.graph.applyAndSendPatch(patch);
}
rebuildSettingsFromGraph(patch?: Patch) {
const U = this.graph.U();
if (patch && !patchContainsPreds(patch, [U(":setting"), U(":device"), U(":deviceAttr")])) {
// that's an approx list of preds , but it just means we'll miss some pathological settings edits
// return;
}
// log("syncFromGraph", this.uri);
// this repeats work- it gathers all settings when really some values changed (and we might even know about them). maybe push the value-fetching into a secnod phase of the run, and have the 1st phase drop out early
const newSettings = [];
const seenDevAttrPairs: Set<string> = new Set();
for (let setting of Array.from(this.graph.objects(this.uri, U(":setting")))) {
// log(` setting ${setting.value}`);
if (!isUri(setting)) throw new Error();
let value: ControlValue;
const device = this.graph.uriValue(setting, U(":device"));
const deviceAttr = this.graph.uriValue(setting, U(":deviceAttr"));
const pred = valuePred(this.graph, deviceAttr);
try {
value = this.graph.uriValue(setting, pred);
if (!(value as NamedNode).id.match(/^http/)) {
throw new Error("not uri");
}
} catch (error) {
try {
value = this.graph.floatValue(setting, pred);
} catch (error1) {
value = this.graph.stringValue(setting, pred); // this may find multi values and throw
}
}
// log(`change: graph contains ${deviceAttr.value} ${value}`);
newSettings.push({ device, deviceAttr, setting, value });
}
this.settings = newSettings;
log(`rebuild to ${this.settings.length}`);
this.onValuesChanged();
}
currentValue(device: NamedNode, deviceAttr: NamedNode): ControlValue | null {
for (let s of this.settings) {
if (device.equals(s.device) && deviceAttr.equals(s.deviceAttr)) {
return s.value;
}
}
return null;
}
// change this object now, but return the patch to be applied to the graph so it can be coalesced.
edit(device: NamedNode, deviceAttr: NamedNode, newValue: ControlValue | null): Patch {
log(`edit: value=${newValue}`);
let existingSetting: NamedNode | null = null;
let result = { adds: [], dels: [] };
for (let s of this.settings) {
if (device.equals(s.device) && deviceAttr.equals(s.deviceAttr)) {
if (existingSetting !== null) {
// this is corrupt. There was only supposed to be one setting per (dev,attr) pair. But we can fix it because we're going to update existingSetting to the user's requested value.
log(`${this.uri.value} had two settings for ${device.value} - ${deviceAttr.value} - deleting ${s.setting}`);
patchUpdate(result, this._removeEffectSetting(s.setting));
}
existingSetting = s.setting;
}
}
if (newValue !== null && this.shouldBeStored(deviceAttr, newValue)) {
if (existingSetting === null) {
patchUpdate(result, this._addEffectSetting(device, deviceAttr, newValue));
} else {
patchUpdate(result, this._patchExistingEffectSetting(existingSetting, deviceAttr, newValue));
}
} else {
if (existingSetting !== null) {
patchUpdate(result, this._removeEffectSetting(existingSetting));
}
}
return result;
}
shouldBeStored(deviceAttr: NamedNode, value: ControlValue | null): boolean {
// this is a bug for zoom=0, since collector will default it to
// stick at the last setting if we don't explicitly send the
// 0. rx/ry similar though not the exact same deal because of
// their remap.
return value != null && value !== 0 && value !== "#000000";
}
_addEffectSetting(device: NamedNode, deviceAttr: NamedNode, value: ControlValue): Patch {
log(" _addEffectSetting", deviceAttr.value, value);
const U = (x: string) => this.graph.Uri(x);
const quad = (s: Quad_Subject, p: Quad_Predicate, o: Quad_Object) => this.graph.Quad(s, p, o, this.ctxForEffect);
if (!this.uri) throw new Error("effect unset");
const setting = this.graph.nextNumberedResource(this.uri.value + "_set");
const addQuads = [
quad(this.uri, U(":setting"), setting),
quad(setting, U(":device"), device),
quad(setting, U(":deviceAttr"), deviceAttr),
quad(setting, valuePred(this.graph, deviceAttr), this._nodeForValue(value)),
];
const patch = { adds: addQuads, dels: [] } as Patch;
log(" save", patch);
this.settings.push({ device, deviceAttr, setting, value });
return patch;
}
_patchExistingEffectSetting(effectSetting: NamedNode, deviceAttr: NamedNode, value: ControlValue): Patch {
log(" patch existing", effectSetting.value);
return this.graph.getObjectPatch(
effectSetting, //
valuePred(this.graph, deviceAttr),
this._nodeForValue(value),
this.ctxForEffect
);
}
_removeEffectSetting(effectSetting: NamedNode): Patch {
const U = (x: string) => this.graph.Uri(x);
log(" _removeEffectSetting", effectSetting.value);
const toDel = [this.graph.Quad(this.uri, U(":setting"), effectSetting, this.ctxForEffect)];
for (let q of this.graph.subjectStatements(effectSetting)) {
toDel.push(q);
}
return { dels: toDel, adds: [] };
}
_nodeForValue(value: ControlValue): NamedNode | Literal {
if (value === null) {
throw new Error("no value");
}
if (isUri(value)) {
return value;
}
return this.graph.prettyLiteral(value);
}
}
|