Skip to content

Commit

Permalink
Merge pull request #312 from cablelabs/issue-308
Browse files Browse the repository at this point in the history
Issue 308
  • Loading branch information
Nick Baroni authored Jun 25, 2019
2 parents d9d4eff + 8e5b7de commit c73a7ae
Show file tree
Hide file tree
Showing 28 changed files with 350 additions and 145 deletions.
2 changes: 1 addition & 1 deletion component.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lpwanserver",
"registry": "lpwanserver",
"version": "1.2.0",
"version": "1.2.1",
"build": "1"
}
1 change: 1 addition & 0 deletions docker/dev/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ WORKDIR /usr/src/app
COPY bin/rest bin/rest
COPY config config
COPY prisma/generated prisma/generated
COPY lib/prisma-cache lib/prisma-cache
COPY restApp.js ./restApp.js
COPY server.js ./server.js
COPY public public
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"ajv": "^6.10.0",
"async": "~2.6.1",
"bluebird": "^3.5.5",
"body-parser": "^1.18.3",
"chai": "^4.2.0",
"chai-http": "^4.2.0",
"connect-ensure-login": "~0.1.1",
Expand Down
12 changes: 9 additions & 3 deletions rest/models/ICompanyNetworkTypeLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ module.exports = class CompanyNetworkTypeLink {
appLogger.log('creating ' + JSON.stringify(device))
let appIndex = nsAppId.indexOf(device.applicationID)
appLogger.log('localAppId[' + appIndex + '] = ' + localAppId[appIndex])
existingDevice = await this.modelAPI.devices.create(device.name, device.description, localAppId[appIndex])
const devData = { ...R.pick(['name', 'description'], device), applicationId: localAppId[appIndex] }
existingDevice = await this.modelAPI.devices.create(devData)
}

let [ devNtls ] = await this.modelAPI.deviceNetworkTypeLinks.list({ deviceId: existingDevice.id })
Expand All @@ -265,8 +266,13 @@ module.exports = class CompanyNetworkTypeLink {

let tempApp = await this.modelAPI.applications.load(localAppId[appIndex])
let coId = tempApp.company.id

this.modelAPI.deviceNetworkTypeLinks.create(existingDevice.id, networkTypeId, localDpId[dpIndex], device, coId)
let devNtlData = {
deviceId: existingDevice.id,
networkTypeId,
deviceProfileId: localDpId[dpIndex],
networkSettings: device
}
this.modelAPI.deviceNetworkTypeLinks.create(devNtlData, { validateCompanyId: coId })
}
}
}
Expand Down
48 changes: 33 additions & 15 deletions rest/models/IDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ module.exports = class Device {
return DB.list(renameQueryKeys(query), opts)
}

create (name, description, applicationId, deviceModel) {
return DB.create({ name, description, applicationId, deviceModel })
create (data) {
return DB.create(data)
}

update ({ id, ...data }) {
Expand Down Expand Up @@ -151,19 +151,7 @@ module.exports = class Device {
async receiveIpDeviceUplink (devEUI, data) {
devEUI = normalizeDevEUI(devEUI)
let nwkType = await this.modelAPI.networkTypes.loadByName('IP')
let devNtl
// Check cache for devNtl ID
let devNtlId = await redisClient.getAsync(`ip-devNtl-${devEUI}`)
if (devNtlId) {
devNtl = await this.modelAPI.deviceNetworkTypeLinks.load(devNtlId)
if (!devNtl) await redisClient.delAsync(`ip-devNtl-${devEUI}`)
}
else {
const devNTLQuery = { networkType: { id: nwkType.id }, networkSettings_contains: devEUI }
let [ devNtls ] = await this.modelAPI.deviceNetworkTypeLinks.list(devNTLQuery)
devNtl = devNtls[0]
if (devNtl) await redisClient.setAsync(`ip-devNtl-${devEUI}`, devNtl.id)
}
const devNtl = await this.modelAPI.deviceNetworkTypeLinks.findByDevEUI(devEUI, nwkType.id)
if (!devNtl) return
// Get device
const device = await this.load(devNtl.device.id)
Expand Down Expand Up @@ -194,4 +182,34 @@ module.exports = class Device {
await redisClient.del(key)
return msgs.map(JSON.parse)
}

async importDevices ({ applicationId, deviceProfileId, devices }) {
// ensure app exists
await this.modelAPI.applications.load(applicationId)
// Load device profile
const deviceProfile = await this.modelAPI.deviceProfiles.load(deviceProfileId)
const nwkType = await this.modelAPI.networkTypes.load(deviceProfile.networkType.id)
if (!nwkType.name === 'IP') {
throw httpError(400, 'Device import currently only supports IP devices.')
}
// Catch all errors and return array
return Promise.all(devices.map(async ({ name, description, deviceModel, devEUI }, row) => {
try {
if (!devEUI) {
throw new Error('devEUI required for each imported device.')
}
const device = await this.create({ name: name || devEUI, applicationId, description, deviceModel })
await this.modelAPI.deviceNetworkTypeLinks.create({
deviceId: device.id,
networkTypeId: deviceProfile.networkType.id,
deviceProfileId,
networkSettings: { devEUI }
})
return { status: 'OK', deviceId: device.id, devEUI, row }
}
catch (err) {
return { status: 'ERROR', error: err.message, devEUI, row }
}
}))
}
}
29 changes: 23 additions & 6 deletions rest/models/IDeviceNetworkTypeLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { prisma } = require('../lib/prisma')
const { normalizeDevEUI, parseProp, stringifyProp } = require('../lib/utils')
const { redisClient } = require('../lib/redis')
const CacheFirstStrategy = require('../../lib/prisma-cache/src/cache-first-strategy')
const R = require('ramda')

//* *****************************************************************************
// Fragments for how the data should be returned from Prisma.
Expand Down Expand Up @@ -59,16 +60,15 @@ module.exports = class DeviceNetworkTypeLink {
return [ records.map(parseNetworkSettings), totalCount ]
}

async create (deviceId, networkTypeId, deviceProfileId, networkSettings, validateCompanyId, { remoteOrigin = false } = {}) {
async create (data, { validateCompanyId, remoteOrigin = false } = {}) {
try {
await this.validateCompanyForDevice(validateCompanyId, deviceId)
if (networkSettings && networkSettings.devEUI) {
networkSettings.devEUI = normalizeDevEUI(networkSettings.devEUI)
if (validateCompanyId) await this.validateCompanyForDevice(validateCompanyId, data.deviceId)
if (data.networkSettings && data.networkSettings.devEUI) {
data = R.assocPath(['networkSettings', 'devEUI'], normalizeDevEUI(data.networkSettings.devEUI), data)
}
const data = { deviceId, networkTypeId, deviceProfileId, networkSettings }
const rec = await DB.create(stringifyNetworkSettings(data))
if (!remoteOrigin) {
var logs = await this.modelAPI.networkTypeAPI.addDevice(networkTypeId, deviceId, networkSettings)
var logs = await this.modelAPI.networkTypeAPI.addDevice(data.networkTypeId, data.deviceId, data.networkSettings)
rec.remoteAccessLogs = logs
}
return rec
Expand Down Expand Up @@ -136,4 +136,21 @@ module.exports = class DeviceNetworkTypeLink {
const dnl = await this.load(dnlId)
await this.validateCompanyForDevice(companyId, dnl.device.id)
}

async findByDevEUI (devEUI, networkTypeId) {
let devNtl
// Check cache for devNtl ID
let devNtlId = await redisClient.getAsync(`ip-devNtl-${devEUI}`)
if (devNtlId) {
devNtl = await this.load(devNtlId)
if (!devNtl) await redisClient.delAsync(`ip-devNtl-${devEUI}`)
}
else {
const devNTLQuery = { networkType: { id: networkTypeId }, networkSettings_contains: devEUI }
let [ devNtls ] = await this.list(devNTLQuery)
devNtl = devNtls[0]
if (devNtl) await redisClient.setAsync(`ip-devNtl-${devEUI}`, devNtl.id)
}
return devNtl
}
}
11 changes: 9 additions & 2 deletions rest/networkProtocols/handlers/LoraOpenSource/LoraOpenSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,8 @@ module.exports = class LoraOpenSource extends NetworkProtocol {
}
else {
appLogger.log('creating ' + remoteDevice.name)
localDevice = await this.modelAPI.devices.create(remoteDevice.name, remoteDevice.description, localAppId)
const devData = { ...R.pick(['name', 'description'], remoteDevice), applicationId: localAppId }
localDevice = await this.modelAPI.devices.create(devData)
appLogger.log('Created ' + localDevice.name)
}
let [ devNtls ] = await this.modelAPI.deviceNetworkTypeLinks.list({ deviceId: localDevice.id, limit: 1 })
Expand All @@ -540,7 +541,13 @@ module.exports = class LoraOpenSource extends NetworkProtocol {
let company = cos[0]
appLogger.log('creating Network Link for ' + localDevice.name)
let networkSettings = this.buildDeviceNetworkSettings(remoteDevice)
await this.modelAPI.deviceNetworkTypeLinks.create(localDevice.id, network.networkType.id, deviceProfile.id, networkSettings, company.id, { remoteOrigin: true })
let devNtlData = {
deviceId: localDevice.id,
networkTypeId: network.networkType.id,
deviceProfileId: deviceProfile.id,
networkSettings
}
await this.modelAPI.deviceNetworkTypeLinks.create(devNtlData, { validateCompanyId: company.id, remoteOrigin: true })
}
await this.modelAPI.protocolData.upsert(network, makeDeviceDataKey(localDevice.id, 'devNwkId'), remoteDevice.devEUI)
return localDevice.id
Expand Down
11 changes: 9 additions & 2 deletions rest/networkProtocols/handlers/Loriot/Loriot.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ module.exports = class Loriot extends NetworkProtocol {
}
else {
appLogger.log('creating ' + remoteDevice.title)
localDevice = await modelAPI.devices.create(remoteDevice.title, remoteDevice.description, localAppId)
const devData = { name: remoteDevice.title, description: remoteDevice.description, applicationId: localAppId }
localDevice = await modelAPI.devices.create(devData)
appLogger.log('Created ' + localDevice.name)
}

Expand All @@ -204,7 +205,13 @@ module.exports = class Loriot extends NetworkProtocol {
appLogger.log(dp, 'info')
let networkSettings = this.buildDeviceNetworkSettings(remoteDevice, dp.localDeviceProfile)
appLogger.log(networkSettings)
const deviceNtl = await modelAPI.deviceNetworkTypeLinks.create(localDevice.id, network.networkType.id, dp.localDeviceProfile, networkSettings, company.id, { remoteOrigin: true })
let devNtlData = {
deviceId: localDevice.id,
networkTypeId: network.networkType.id,
deviceProfileId: dp.localDeviceProfile,
networkSettings
}
const deviceNtl = await modelAPI.deviceNetworkTypeLinks.create(devNtlData, { validateCompanyId: company.id, remoteOrigin: true })
appLogger.log(deviceNtl)
}
await this.modelAPI.protocolData.upsert(network, makeDeviceDataKey(localDevice.id, 'devNwkId'), remoteDevice._id)
Expand Down
17 changes: 15 additions & 2 deletions rest/networkProtocols/handlers/TheThingsNetwork/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,12 @@ module.exports = class TheThingsNetworkV2 extends NetworkProtocol {
}
else {
appLogger.log('creating ' + remoteDevice.lorawan_device.dev_eui)
existingDevice = await modelAPI.devices.create(remoteDevice.lorawan_device.dev_eui, remoteDevice.description, localAppId)
const devData = {
name: remoteDevice.lorawan_device.dev_eui,
description: remoteDevice.description,
applicationId: localAppId
}
existingDevice = await modelAPI.devices.create(devData)
appLogger.log('Created ' + existingDevice.name)
}

Expand All @@ -220,7 +225,15 @@ module.exports = class TheThingsNetworkV2 extends NetworkProtocol {
appLogger.log(dp, 'info')
let normalizedDevice = this.normalizeDeviceData(remoteDevice, dp.localDeviceProfile)
appLogger.log(normalizedDevice)
const existingDeviceNTL = await modelAPI.deviceNetworkTypeLinks.create(existingDevice.id, network.networkType.id, dp.localDeviceProfile, normalizedDevice, 2, { remoteOrigin: true })
const [ cos ] = await this.modelAPI.companies.list({ limit: 1 })
let company = cos[0]
let devNtlData = {
deviceId: existingDevice.id,
networkTypeId: network.networkType.id,
deviceProfileId: dp.localDeviceProfile,
networkSettings: normalizedDevice
}
const existingDeviceNTL = await modelAPI.deviceNetworkTypeLinks.create(devNtlData, { validateCompanyId: company.id, remoteOrigin: true })
appLogger.log(existingDeviceNTL)
await this.modelAPI.protocolData.upsert(network, makeDeviceDataKey(existingDevice.id, 'devNwkId'), remoteDevice.dev_id)
appLogger.log({ localDevice: existingDevice.id, remoteDevice: remoteDevice.dev_id })
Expand Down
10 changes: 5 additions & 5 deletions rest/restApplicationNetworkTypeLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ exports.initialize = function (app, server) {
* that the Application is being linked to.
* @apiSuccess {String} object.records.networkSettings The settings in a
* JSON string that correspond to the Network Type.
* @apiVersion 1.2.0
* @apiVersion 1.2.1
*/
app.get('/api/applicationNetworkTypeLinks', [restServer.isLoggedIn,
restServer.fetchCompany],
Expand Down Expand Up @@ -102,7 +102,7 @@ exports.initialize = function (app, server) {
* that the Application is being linked to.
* @apiSuccess {String} object.networkSettings The settings in a
* JSON string that correspond to the Network Type.
* @apiVersion 1.2.0
* @apiVersion 1.2.1
*/
app.get('/api/applicationNetworkTypeLinks/:id', [restServer.isLoggedIn], function (req, res, next) {
var id = req.params.id
Expand Down Expand Up @@ -139,7 +139,7 @@ exports.initialize = function (app, server) {
* "networkSettings": { ... },
* }
* @apiSuccess {String} id The new Application Network Type Link's id.
* @apiVersion 1.2.0
* @apiVersion 1.2.1
*/
app.post('/api/applicationNetworkTypeLinks', [restServer.isLoggedIn,
restServer.fetchCompany,
Expand Down Expand Up @@ -199,7 +199,7 @@ exports.initialize = function (app, server) {
* {
* "networkSettings": { ... },
* }
* @apiVersion 1.2.0
* @apiVersion 1.2.1
*/
app.put('/api/applicationNetworkTypeLinks/:id', [restServer.isLoggedIn,
restServer.fetchCompany,
Expand Down Expand Up @@ -268,7 +268,7 @@ exports.initialize = function (app, server) {
* prepended with "Bearer "
* @apiParam (URL Parameters) {String} id The Application Network Type
* Link's id
* @apiVersion 1.2.0
* @apiVersion 1.2.1
*/
app.delete('/api/applicationNetworkTypeLinks/:id', [restServer.isLoggedIn,
restServer.fetchCompany,
Expand Down
Loading

0 comments on commit c73a7ae

Please sign in to comment.