This repository has been archived by the owner on Oct 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
131 lines (115 loc) · 3.35 KB
/
index.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
131
/*
* +===============================================
* | Author: Parham Alvani (parham.alvani@gmail.com)
* |
* | Creation Date: 20-07-2017
* |
* | File Name: index.js
* +===============================================
*/
/* Configuration */
const config = require('config')
/* Bamboo Device Manager */
const BambooDM = require('./src/dm')
const bambooDM = new BambooDM(config.mongo.url)
/* Bamboo Device Manager */
const BambooTM = require('./src/tm')
const bambooTM = new BambooTM(config.mongo.url)
const recursive = require('recursive-readdir')
const YAML = require('yamljs')
recursive('packages/standard', function (err, files) {
if (err) {
return
}
files.forEach((file) => {
let m = YAML.load(file)
bambooTM.createModel(`${m.package}.${m.name}`, m.settings, m.states, m.statistics, m.attributes)
})
})
/* Command Line Interface */
const vorpal = require('vorpal')()
const chalk = require('chalk')
vorpal
.command('agents', 'lists all connected agents')
.action(async function () {
let as = await bambooDM.all()
for (let a of as) {
this.log(`ID: ${chalk.rgb(255, 102, 204)(a._id)}`)
this.log(`Tenant: ${chalk.rgb(204, 255, 102)(a.tenant)}`)
this.log(`Name: ${chalk.rgb(102, 204, 255)(a.name)}`)
this.log(`Time: ${chalk.rgb(255, 255, 255)(a.time)}`)
this.log(`Things: ${chalk.rgb(255, 255, 255)(a.things)}`)
}
})
vorpal
.command('model [name]', 'thing model')
.action(async function (args) {
let m = await bambooTM.getModel(args.name)
if (m) {
this.log(`Name: ${m.name}`)
this.log(`Statistics: ${m.statistics}`)
this.log(`States: ${m.states}`)
this.log(`Settings: ${m.settings}`)
this.log(`Attributes: ${m.attributes}`)
}
})
vorpal.log(' * 18.20 at Sep 07 2016 7:20 IR721')
vorpal.delimiter(`${chalk.green('Bamboo')} - ${chalk.rgb(255, 177, 79)('Bamboolia')} > `).show()
/* Bamboo component initiation */
const BambooComponent = require('@ibamboo/component')
new BambooComponent({
mqttHost: config.connectivity.host,
mqttPort: config.connectivity.port,
name: 'bamboolia',
subscribes: ['discovery']
}).on('ready', () => {
vorpal.log(` * MQTT at ${config.connectivity.host}:${config.connectivity.port}`)
}).on('discovery', (message) => {
vorpal.log(message)
if (message.type === 'add') {
bambooDM.addAgent(message.tenant, message.name)
}
if (message.type === 'remove') {
bambooDM.removeAgent(message.tenant, message.name)
}
if (message.type === 'things') {
bambooDM.addThings(message.tenant, message.name, message.things)
}
})
/* HTTP server initiation */
const Hapi = require('hapi')
const server = new Hapi.Server()
server.connection({
host: config.http.host,
port: config.http.port
})
server.route([
{
method: 'GET',
path: '/agent',
handler: async function (request, reply) {
let as = await bambooDM.all()
let res = {}
for (let a of as) {
res[`${a.tenant}/${a.name}`] = {
time: new Date(a.time).toISOString(),
things: a.things
}
}
return reply(res)
}
}, {
method: 'GET',
path: '/model/{name}',
handler: async function (request, reply) {
let m = await bambooTM.getModel(request.params.name)
return reply(m)
}
}
])
server.start((err) => {
if (err) {
throw err
}
vorpal.log(` * HTTP at ${server.info.uri}`)
})