-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDemoSolidAgent.ts
More file actions
112 lines (94 loc) · 4.73 KB
/
Copy pathDemoSolidAgent.ts
File metadata and controls
112 lines (94 loc) · 4.73 KB
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
import {readText} from "koreografeye";
import {SolidClient} from "../solid/SolidClient";
import {GeneralSubscriptionClient} from "../subscribe/GeneralSubscriptionClient";
import {SolidActor} from "../solid/SolidActor";
import {OpenHABClient} from "../openHAB/OpenHABClient";
import {OpenHABActor} from "../openHAB/OpenHabActor";
import {OrchestrationActor} from "../orchestration/OrchestrationActor";
import {Actor, MessageClient, PluginFunction} from "../orchestration/OrchestrationActorInterface";
import {fnoHasStateChanged, fnoUpdateOpenHABState, fnoUpdateSolidState} from "../plugins/SmartHomeUseCase";
import {Session} from "@rubensworks/solid-client-authn-isomorphic";
import {OpenHABRDFTranslator} from "../openHAB/OpenHABRDFTranslator";
import {SolidNotificationClient} from "../subscribe/SolidNotificationClient";
import {Quad} from "n3";
import * as path from 'path';
import {SubscriptionEnum, SubscriptionType} from "../subscribe/SubscriptionTypes";
export class DemoSolidAgent {
// TODO: merge code to main + add to npm when koreografeye is fixed
private actors: Record<string, Actor> = {};
private plugins: Record<string, PluginFunction> = {
'http://example.org/hasStateChanged': fnoHasStateChanged,
'http://example.org/updateSolidState': fnoUpdateSolidState,
'http://example.org/updateOpenHABState': fnoUpdateOpenHABState
};
private rules = [
readText(path.join(__dirname,'../../rules/openHABChangedRule.n3'))!,
readText(path.join(__dirname,'../../rules/solidChangedRule.n3'))!,
readText(path.join(__dirname,'../../rules/orchestratorToOpenHAB.n3'))!,
readText(path.join(__dirname,'../../rules/orchestratorToSolid.n3'))!,
];
private solidClient: SolidClient;
private solidSubscriptionClient: MessageClient;
private solidActor: SolidActor;
private openHABClient: OpenHABClient;
private openHABSubscriptionClient: MessageClient;
private openHABTranslator: OpenHABRDFTranslator;
private openHABActor: OpenHABActor;
private orchestrationActor: OrchestrationActor;
private solidStateResource: string;
public constructor(config: {
openhab: {
openHABToken: string,
openHABURL: string,
openHABResources: string[],
subscriptionType?: SubscriptionType
}
solid: {
solidResources: string[],
session?: Session,
subscriptionType?: SubscriptionType
}
}) {
// destructuring
const {openHABToken, openHABURL, openHABResources, subscriptionType: openHABSubscriptionType} = config.openhab;
const {solidResources, subscriptionType: solidSubscriptionType} = config.solid
const session = config.solid.session ?? new Session()
const solidWebID = 'solid'
const openHABWebID = 'openHAB'
const orchestratorWebID = 'orchestrator'
// initialize openHAB actor
this.openHABClient = new OpenHABClient({accessToken: openHABToken, endPointUrl: openHABURL});
this.openHABSubscriptionClient = new GeneralSubscriptionClient(this.openHABClient, openHABWebID, openHABSubscriptionType?.interval);
this.openHABTranslator = new OpenHABRDFTranslator()
this.openHABActor = new OpenHABActor(this.openHABClient, this.openHABSubscriptionClient, this.openHABTranslator, {
resources: openHABResources,
webID: openHABWebID
})
// initialize solid actor
this.solidClient = new SolidClient(session);
this.solidSubscriptionClient = solidSubscriptionType?.type === SubscriptionEnum.PUSH ?
new SolidNotificationClient(session, this.solidClient, solidWebID) :
new GeneralSubscriptionClient(this.solidClient, solidWebID, solidSubscriptionType?.interval);
this.solidActor = new SolidActor(this.solidClient, this.solidSubscriptionClient, {resources: solidResources})
// initialize orchestrationActor
this.actors[solidWebID] = this.solidActor;
this.actors[openHABWebID] = this.openHABActor;
this.orchestrationActor = new OrchestrationActor({
actors: this.actors,
plugins: this.plugins,
rules: this.rules
})
this.solidStateResource = solidResources[0];
}
public async start() {
// sync state of solid pod with state of openHAB (start from state of openHAB)
const state: Quad[] = []
for (const resource of this.openHABActor.resources) {
state.push(...await this.openHABActor.readResource(resource))
}
await this.solidActor.writeResource(this.solidStateResource, state)
await this.orchestrationActor.writeResource("state", state)
// start
this.orchestrationActor.start();
}
}