-
Notifications
You must be signed in to change notification settings - Fork 1
/
Index.ts
65 lines (57 loc) · 3.93 KB
/
Index.ts
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
import {SolidActor} from "./src/solid/Actor";
import {SolidClient} from "./src/solid/SolidClient";
import {Session} from "@rubensworks/solid-client-authn-isomorphic";
import {Quad, Writer} from "n3";
import {readText} from "koreografeye";
import {OrchestrationActor} from "./src/orchestration/OrchestrationActor";
import {fnoHasStateChanged, fnoUpdateOpenHABState, fnoUpdateSolidState} from "./src/plugins/SmartHomeUseCase";
import {Actor, PluginFunction} from "./src/orchestration/OrchestrationActorInterface";
import {OpenHABClient} from "./src/openHAB/OpenHABClient";
import {OpenHABActor} from "./src/openHAB/Actor";
import {OpenHABRDFTranslator} from "./src/openHAB/OpenHABRDFTranslator";
const writer = new Writer()
require('dotenv').config()
// run solid server: community-solid-server -c memory-no-setup.json
// also openHAB service must be run: sudo systemctl start openhab.service
const openHABURL = process.env.OPENHAB_URL! + '/'
const openHABToken = process.env.OPENHAB_API_TOKEN!
const solidClient = new SolidClient(new Session())
const solidActor = new SolidActor(solidClient, {resources: ['http://localhost:3000/state']})
const openHABClient = new OpenHABClient({
accessToken: openHABToken,
endPointUrl: openHABURL
})
const openHABActor = new OpenHABActor(openHABClient, new OpenHABRDFTranslator(), {resources: ['Bureau_rechts_Color','Bureau_links_Color']})
const rules = [
readText('./rules/openHABChangedRule.n3')!,
readText('./rules/solidChangedRule.n3')!,
readText('./rules/orchestratorToOpenHAB.n3')!,
readText('./rules/orchestratorToSolid.n3')!,
// readText('./rules/experimentalRule.n3')!,
]
const actors: Record<string, Actor> = {}
actors[solidActor.webID] = solidActor;
actors[openHABActor.webID] = openHABActor;
const plugins: Record<string, PluginFunction> = {
'http://example.org/hasStateChanged': fnoHasStateChanged,
'http://example.org/updateSolidState': fnoUpdateSolidState,
'http://example.org/updateOpenHABState': fnoUpdateOpenHABState
}
const orchestraterActor = new OrchestrationActor({actors, plugins, rules})
async function main() {
// init: sync state of solid pod with state of openHAB (start from state of openHAB)
const state: Quad[] = []
for (const resource of openHABActor.resources) {
state.push(... await openHABActor.readResource(resource))
}
await solidActor.writeResource('http://localhost:3000/state', state)
await orchestraterActor.writeResource("state", state)
// start
orchestraterActor.start();
// turn light on:
// curl -X PUT -H 'Content-type:text/turtle' -d "<Bureau_links_Color> <http://dbpedia.org/resource/Brightness> 10 .<Bureau_links_Color> <http://dbpedia.org/resource/Colorfulness> 50 .<Bureau_links_Color> <http://dbpedia.org/resource/Hue> 0 .<Bureau_links_Color> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://saref.etsi.org/core/OnState> .<Bureau_rechts_Color> <http://dbpedia.org/resource/Brightness> 10 .<Bureau_rechts_Color> <http://dbpedia.org/resource/Colorfulness> 60 .<Bureau_rechts_Color> <http://dbpedia.org/resource/Hue> 272 .<Bureau_rechts_Color> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://saref.etsi.org/core/OnState> ." http://localhost:3000/state
// turn light off:
// curl -X PUT -H 'Content-type:text/turtle' -d "<Bureau_links_Color> <http://dbpedia.org/resource/Brightness> 0 .<Bureau_links_Color> <http://dbpedia.org/resource/Colorfulness> 50 .<Bureau_links_Color> <http://dbpedia.org/resource/Hue> 0 .<Bureau_links_Color> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://saref.etsi.org/core/OffState> .<Bureau_rechts_Color> <http://dbpedia.org/resource/Brightness> 0 .<Bureau_rechts_Color> <http://dbpedia.org/resource/Colorfulness> 60 .<Bureau_rechts_Color> <http://dbpedia.org/resource/Hue> 272 .<Bureau_rechts_Color> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://saref.etsi.org/core/OffState> ." http://localhost:3000/state
console.log(writer.quadsToString(state))
}
main()