-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
234 lines (197 loc) · 8.43 KB
/
server.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env nodejs
process.title = "senti_databroker"
//#region Express
const dotenv = require('dotenv').load()
if (dotenv.error) {
console.warn(dotenv.error)
}
const express = require('express')
const cors = require('cors')
const helmet = require('helmet')
// const pino = require('pino')
// const fs = require('fs');
const log = require('./api/logging/index').log
const app = express()
module.exports.log = log
// })
// const logService = require('./mqtt/logService')
// const logServ = new logService()
// module.exports.logService = logServ
const testing = require('./api/logging/logger')
// const pino = require('pino')()
// const expressPino = require('express-pino-logger')({
// logger: pino()
// })
// app.use([expressPino])
const sentiAuthClient = require('senti-apicore').sentiAuthClient
const authClient = new sentiAuthClient(process.env.AUTHCLIENTURL, process.env.PASSWORDSALT)
module.exports.authClient = authClient
const sentiAclBackend = require('senti-apicore').sentiAclBackend
const sentiAclClient = require('senti-apicore').sentiAclClient
const aclBackend = new sentiAclBackend(process.env.ACLBACKENDTURL)
const aclClient = new sentiAclClient(aclBackend)
module.exports.aclClient = aclClient
//#region MQTT
const SecureStoreMqttHandler = require('./mqtt/secureStore')
const secureMqttClient = new SecureStoreMqttHandler(process.env.MQTT_HOST, process.env.MQTT_USER, process.env.MQTT_PASS, 'dataBroker')
secureMqttClient.connect()
module.exports.secureMqttClient = secureMqttClient
//#endregion
app.use('/', testing)
//#region V2 Device Data
const getDeviceDataV2 = require('./api/v2/devicedata/getDeviceData')
//#endregion
//#region Device
const getDevice = require('./api/device/getDevice')
const getDevices = require('./api/device/getDevices')
const updateDevice = require('./api/device/updateDevice')
const createDevice = require('./api/device/createDevice')
const deleteDevice = require('./api/device/deleteDevice')
// V2
const getDeviceV2 = require('./api/v2/device/getDevice')
const getDevicesV2 = require('./api/v2/device/getDevices')
const createDeviceV2 = require('./api/v2/device/createDevice')
const deleteDeviceV2 = require('./api/v2/device/deleteDevice')
const updateDeviceV2 = require('./api/v2/device/updateDevice')
const climaidInsightV2 = require('./api/v2/climaidInsight/climaidInsight')
const newsecV2 = require('./api/v2/newsec/newsec')
const rescanV2 = require('./api/v2/device/rescanData')
//#endregion
//#region Registries
// V2
const createRegistryV2 = require('./api/v2/registry/createRegistry')
const getRegistryV2 = require('./api/v2/registry/getRegistry')
const getRegistriesV2 = require('./api/v2/registry/getRegistries')
const updateRegistryV2 = require('./api/v2/registry/updateRegistry')
const deleteRegistryV2 = require('./api/v2/registry/deleteRegistry')
// V1
const getRegistries = require('./api/registry/getRegistries')
const getRegistryDevices = require('./api/registry/getRegistryDevices')
const getRegistry = require('./api/registry/getRegistry')
const createReg = require('./api/registry/createRegistry')
const updateReg = require('./api/registry/updateRegistry')
const deleteReg = require('./api/registry/deleteRegistry')
//#endregion
//#region Device Types
const getDT = require('./api/deviceType/getDeviceType')
const getDTs = require('./api/deviceType/getDeviceTypes')
const updateDT = require('./api/deviceType/updateDeviceType')
const createDT = require('./api/deviceType/createDeviceType')
const deleteDT = require('./api/deviceType/deleteDeviceType')
// V2
const getDeviceTypeV2 = require('./api/v2/devicetype/getDeviceType')
const getDeviceTypesV2 = require('./api/v2/devicetype/getDeviceTypes')
const createDeviceTypeV2 = require('./api/v2/devicetype/createDeviceType')
const updateDeviceTypeV2 = require('./api/v2/devicetype/updateDeviceType')
const deleteDeviceTypeV2 = require('./api/v2/devicetype/deleteDeviceType')
//#endregion
//#region Cloud Functions
const getCFs = require('./api/v2/cloudfunction/getCloudFunctions')
const getCF = require('./api/v2/cloudfunction/getCloudFunction')
const createCF = require('./api/v2/cloudfunction/createCloudFunction')
const updateCF = require('./api/v2/cloudfunction/updateCloudFunction')
const deleteCF = require('./api/v2/cloudfunction/deleteCloudFunction')
//#endregion
//#region Device Data
const getDeviceData = require('./api/deviceData/getDeviceData')
const getMessages = require('./api/deviceData/getMessages')
const getDataExternal = require('./api/deviceData/getDataExternal')
//#endregion
//#region Customer
const updateCustomer = require('./api/customer/updateCustomer')
const createCustomer = require('./api/customer/createCustomer')
const getCustomer = require('./api/customer/getCustomer')
const deleteCustomer = require('./api/customer/deleteCustomer')
//#endregion
//#region Org Metadata
const getOrg = require('./api/org/getOrgMetadata')
//#endregion
const port = process.env.NODE_PORT
app.use(helmet())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(cors())
/**
* Device Data V2
*/
app.use([getDeviceDataV2])
/**
* API Data V2
*/
app.use([getDataExternal])
/**
* Devices V2
*/
app.use([getDeviceV2, getDevicesV2, createDeviceV2, updateDeviceV2, deleteDeviceV2, rescanV2])
/**
* Device Types V2
*/
app.use([getDeviceTypeV2, getDeviceTypesV2, createDeviceTypeV2, updateDeviceTypeV2, deleteDeviceTypeV2])
/**
* Registries V2
*/
app.use([createRegistryV2, getRegistryV2, getRegistriesV2, updateRegistryV2, deleteRegistryV2])
app.use([climaidInsightV2])
app.use([newsecV2])
/**
* Cloud functions V2
*/
app.use([getCF, getCFs, createCF, updateCF, deleteCF])
/**
* Waterworks Endpoints
*/
const getWaterworksV2 = require('./api/v2/waterworks/waterworks')
const customBenchmark = require('./api/v2/waterworks/customBenchmark')
const customBenchmarkByTag = require('./api/v2/waterworks/customBenchmarkByTag')
const deviceUuidUsage = require('./api/v2/waterworks/deviceUuidUsage')
const totalUsageByDay = require('./api/v2/waterworks/totalUsageByDay')
const totalUsageByDayByOrgUUID = require('./api/v2/waterworks/totalUsageByDayByOrgUUID')
const usage = require('./api/v2/waterworks/usage')
const usageByDay = require('./api/v2/waterworks/usageByDay')
const usageByDayCustomUUIDs = require('./api/v2/waterworks/usageByDayCustomUUIDs')
const usageByHour = require('./api/v2/waterworks/usageByHour')
const usageByHourCustomUUIDs = require('./api/v2/waterworks/usageByHourCustomUUIDs')
const benchmarkByOrgUUID = require('./api/v2/waterworks/benchmarkByOrgUUID')
const dataBenchmarkByHourOrgUUID = require('./api/v2/waterworks/dataBenchmarkByHourOrgUUID')
const dataByField = require('./api/v2/waterworks/dataByField')
const dataByFieldByUUIDs = require('./api/v2/waterworks/dataByFieldByUUIDs')
const dataDeviceUsageByDayByDeviceUUID = require('./api/v2/waterworks/dataDeviceUsageByDayByDeviceUUID')
const dataDeviceUsageByHourByDeviceUUID = require('./api/v2/waterworks/dataDeviceUsageByHourByDeviceUUID')
const dataFieldByDeviceUUID = require('./api/v2/waterworks/dataFieldByDeviceUUID')
const dataTotalByDayFieldByUUIDs = require('./api/v2/waterworks/dataTotalByDayFieldByUUIDs')
app.use([getWaterworksV2, customBenchmark, customBenchmarkByTag,
deviceUuidUsage, totalUsageByDay, totalUsageByDayByOrgUUID, usage,
usageByDay, usageByDayCustomUUIDs, usageByHour, usageByHourCustomUUIDs,
benchmarkByOrgUUID, dataBenchmarkByHourOrgUUID, dataByField, dataByFieldByUUIDs,
dataDeviceUsageByDayByDeviceUUID, dataDeviceUsageByHourByDeviceUUID,
dataFieldByDeviceUUID, dataTotalByDayFieldByUUIDs
])
// V1 USE
app.use([getMessages, getDeviceData,
getDT, getDTs, createDT, updateDT, deleteDT,
getDevice, getDevices, createDevice, updateDevice, deleteDevice,
getRegistry, getRegistryDevices, getRegistries, createReg, updateReg, deleteReg,
createCustomer, getCustomer, updateCustomer, deleteCustomer,
getOrg])
//DEV DO NOT UNCOMMENT
// const HotMess = require('./api/DONOTRUN/SERIOUSLYDONT')
// app.use([HotMess])
var allRoutes = require('./api/logging/routeLogging')
const startAPIServer = () => {
console.clear()
allRoutes(app)
console.log('Senti'.green.bold + ' - Data'.cyan.bold + ' Broker'.cyan.bold)
app.listen(port, () => {
console.log('Server started on port: ' + port.toString().yellow.bold)
// log('Senti DataBroker started on port ' + port.toString(), 'info')
}).on('error', (err) => {
if (err.errno === 'EADDRINUSE') {
console.log('Server not started, port ' + port + ' is busy')
} else {
console.log(err)
}
})
}
startAPIServer()
console.log("Node Version: ", process.version)
//#endregion