forked from Watson-Personal-Assistant/kr-node-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
homeSecurity.js
130 lines (113 loc) · 2.87 KB
/
homeSecurity.js
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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const actions = require('./action');
const conditions = require('./condition');
const Agent = require('./sdk/messages');
const app = express();
app.use(bodyParser.json());
var KnowledgeObject = require('./sdk/object');
var KnowledgeRelation = require('./sdk/relation');
// Create objects in local memory.
var person = new KnowledgeObject('Person',
{
'name': 'John',
"latitude": 12.456,
"longitude": 67.99
}
);
var house = new KnowledgeObject('House',
{
"latitude": 12.345,
"longitude": 67.890,
"name": "home"
}
);
var door = new KnowledgeObject('Door',
{
"isOpen": false,
"name": "Front door"
}
);
// Save the objects to the world model.
Promise.all(
[
person.create(),
house.create(),
door.create()
]
).then(
function (results) {
console.log('All objects created\n\n');
// Create relations in local memory.
var personToHouse = new KnowledgeRelation('ownership', person, house);
var houseToDoor = new KnowledgeRelation('has-as-part', house, door);
// Save relationships to the world model.
Promise.all(
[
personToHouse.create(),
houseToDoor.create()
]).then(
function (results) {
console.log('All relations created\n\n');
runAgent();
}
);
}
);
// Create the agents to connect to the Message Hub and subscribe to events.
var doorOpenAgent = new Agent('object-update',
conditions.main,
actions.main);
function runAgent() {
Promise.all([
doorOpenAgent.connect(),
]).then(function () {
doorOpenAgent.subscribe();
console.log('Subscription created\n\n');
}, cleanup); //cleanup if the sub fails
}
// Delete objects from the world model.
function cleanup() {
Promise.all(
[
person.delete(),
house.delete(),
door.delete()
]);
}
// Open the door.
app.get('/openDoor', function (req, res) {
KnowledgeObject.retrieve(door.id).then((doorObj) => {
if (!doorObj.attributes.isOpen) {
doorObj.attributes['isOpen'] = true;
doorObj.update();
res.status(200);
res.send("opened door");
} else {
res.status(200);
res.send("door was already open");
}
});
});
//Close the door
app.get('/closeDoor', function (req, res) {
KnowledgeObject.retrieve(door.id).then((doorObj) => {
if (doorObj.attributes.isOpen) {
doorObj.attributes['isOpen'] = false;
doorObj.update();
res.status(200);
res.send("closed door");
} else {
res.status(200);
res.send("door was already closed");
}
});
});
// Server Startup
const port = process.env.PORT || process.env.RULE_PORT || 8080;
app.listen(port, () => {
console.log(`Agent REST service is alive!\nListening on port ${port}\n\n`)
});
module.exports.App = app;