Browse Source

Upload files to ''

master
cse47122 4 years ago
parent
commit
f0c0ae5597
  1. 143
      fake-device-manager.js
  2. 162
      fake-device.js
  3. 23
      index.js
  4. 2438
      package-lock.json
  5. 30
      package.json

143
fake-device-manager.js

@ -1,96 +1,73 @@
const sizeof = require('object-sizeof') /* globals describe it */
const FakeDevice = require('./fake-device')
module.exports = function FakeDeviceManager ({ const expect = require('chai').expect
connection,
deviceCount = 10,
spawnInterval = 200,
metricsInterval = 200,
sendInterval = 15000,
measurements,
onUpdateStats
}) {
if (!connection) {
throw new Error('Missing connection param')
}
if (!measurements) {
throw new Error('Missing measurements param')
}
let devices = []
let stats = { const FakeDeviceManager = require('../src/fake-device-manager')
devices: 0, const measurements = {
measurements: 0, usage: {type: 'integer', min: 0, max: 100},
writes: 0, voltage: {type: 'integer', min: 0, max: 15},
size: 0, temperature: {type: 'integer', min: -20, max: 60}
errors: 0 }
}
/** // create fake influx connection
* Return device list // let connectionWriteData = false
*/ const connection = {
const getDevices = () => { send: (deviceId, data) => {
return devices return new Promise((resolve, reject) => {
// connectionWriteData = {deviceId, data}
resolve()
})
} }
}
/** describe('FakeDeviceManager', function () {
* Return stats data let fakeDeviceManager = null
*/
const getStats = () => {
return stats
}
/** it('should throw error if no params are passed', function () {
* Update stats on device data send event expect(FakeDeviceManager).to.throw(Error)
*/ })
const onDeviceSendData = (err, data) => {
if (err) { it('shold throw missing connection param', function () {
console.log(err) expect(() => FakeDeviceManager({})).to.throw('Missing connection param')
stats.errors++ })
return
} it('shold throw missing measurements param', function () {
stats.measurements += data.length expect(() => FakeDeviceManager({
data.forEach(chunk => { connection
stats.size += sizeof(chunk) })).to.throw('Missing measurements param')
}) })
stats.writes++
}
/** it('should create instance', function () {
* Start device spawn process fakeDeviceManager = FakeDeviceManager({
*/
const start = () => {
let __spawnInterval = setInterval(() => {
stats.devices++
devices.push(FakeDevice({
deviceId: stats.devices,
connection, connection,
measurements, measurements,
onSendData: onDeviceSendData, sendInterval: 500
autoStart: true, })
metricsInterval, expect(fakeDeviceManager).to.have.property('start')
sendInterval })
}))
// when device limit is reached stop spawn inteval
if (stats.devices === deviceCount) {
clearTimeout(__spawnInterval)
}
}, spawnInterval)
}
/** it('should spawn device instances', function (done) {
* Stop all spawned devices fakeDeviceManager.start()
*/ setTimeout(() => {
const stop = () => { let devices = fakeDeviceManager.getDevices()
for (let i = 0; i < stats.devices; i++) { done(!devices.length)
devices[i].stop() }, 500)
} })
}
return { it('should collect device stats', function (done) {
start, setTimeout(() => {
stop, let stats = fakeDeviceManager.getStats()
getDevices, if (!stats) return done('No data')
getStats if (
stats.devices &&
stats.measurements &&
stats.writes &&
stats.size
) {
done()
} else {
done('Invalid write data format')
} }
} }, 1000)
})
})

162
fake-device.js

@ -1,111 +1,81 @@
/** /* globals describe it */
* Create FakeDevice
*
* @param {any} {
* deviceId,
* metricsInterval = 100,
* sendInterval = 15000
* }
* @returns FakeDevice
*/
module.exports = function FakeDevice ({
deviceId,
connection,
metricsInterval = 100,
sendInterval = 30000,
onSendData,
autoStart = false,
measurements
}) {
if (!deviceId) {
throw new Error('Missing deviceId param')
}
if (!connection) {
throw new Error('Missing connection param')
}
if (!measurements) {
throw new Error('Missing measurements param')
}
let data = [] const expect = require('chai').expect
let __metricsInterval = null const FakeDevice = require('../src/fake-device')
let __sendInterval = null const measurements = {
usage: {type: 'integer', min: 0, max: 100},
voltage: {type: 'integer', min: 0, max: 15},
temperature: {type: 'integer', min: -20, max: 60}
}
/** // create fake influx connection
* Return data buffer let connectionWriteData = false
*/ const connection = {
const getData = () => { send: (deviceId, data) => {
return data return new Promise((resolve, reject) => {
connectionWriteData = {deviceId, data}
resolve()
})
} }
}
/** describe('FakeDevice', function () {
* Read data from sensors let fakeDevice = null
* Will generate fake random data
*/
const readData = () => {
function getRandomArbitrary (min, max) {
return Math.random() * (max - min) + min
}
let chunk = {} it('should throw error if no params are passed', function () {
Object.keys(measurements).forEach(measurement => { expect(FakeDevice).to.throw(Error)
switch (measurements[measurement].type) {
case 'integer':
chunk[measurement] = getRandomArbitrary(measurements[measurement].min, measurements[measurement].max)
break
default:
throw new Error('Invalid measurement type')
}
}) })
chunk['timestamp'] = new Date()
data.push(chunk) it('shold throw missing deviceId param', function () {
} expect(() => FakeDevice({})).to.throw('Missing deviceId param')
/**
* Send data to db and clear data buffer
* Calls onSendData function if set
*/
const send = () => {
// send data buffer to connection
connection.send(deviceId, data)
.then(() => {
if (!onSendData) return
onSendData(null, data)
// clear internal data buffer on successfull send
data = []
}) })
.catch(err => {
if (!onSendData) return it('shold throw missing connection param', function () {
onSendData(err) expect(() => FakeDevice({
deviceId: 1
})).to.throw('Missing connection param')
}) })
}
/** it('shold throw missing measurements param', function () {
* Stop collecting data expect(() => FakeDevice({
*/ deviceId: 1,
const stop = () => { connection
clearInterval(__metricsInterval) })).to.throw('Missing measurements param')
clearInterval(__sendInterval) })
}
/** it('should create instance', function () {
* Start collecting data fakeDevice = FakeDevice({
*/ deviceId: 1,
const start = () => { connection,
__metricsInterval = setInterval(readData, metricsInterval) sendInterval: 1000,
__sendInterval = setInterval(send, sendInterval) measurements
} })
expect(fakeDevice).to.have.property('start')
})
if (autoStart) { it('should generate data', function (done) {
start() fakeDevice.start()
} setTimeout(() => {
let data = fakeDevice.getData()
done(!data.length)
}, 500)
})
return { it('should send data to influx', function (done) {
stop, setTimeout(() => {
start, if (!connectionWriteData) return done('No data')
send, if (!connectionWriteData.deviceId) return done('Invalid write data - missing deviceId')
getData if (
connectionWriteData.data[0].usage &&
connectionWriteData.data[0].voltage &&
connectionWriteData.data[0].temperature &&
connectionWriteData.data[0].timestamp
) {
done()
} else {
done('Invalid write data format')
} }
} }, 1000)
})
})

23
index.js

@ -0,0 +1,23 @@
const DeviceManager = require('./src/fake-device-manager')
const FakeDeviceConnection = require('./src/fake-device-connection')()
const updateStats = require('./src/stats')
console.log('Generating data - send interval: 30s')
let deviceManager = DeviceManager({
deviceCount: 10,
metricsInterval: 30,
connection: FakeDeviceConnection,
onUpdateStats: updateStats,
measurements: {
temperature: {type: 'integer', min: -10, max: 50},
air_humidity: {type: 'integer', min: 30, max: 100},
ground_humidity: {type: 'integer', min: 30, max: 100},
airforce: {type: 'integer', min: 30, max: 100}
}
})
updateStats(deviceManager)
deviceManager.start()

2438
package-lock.json

File diff suppressed because it is too large

30
package.json

@ -0,0 +1,30 @@
{
"name": "iot-dummy-data-generator",
"version": "0.1.0",
"description": "Spawn dummy IoT devices and generate custom data that can be send to some data store.",
"main": "index.js",
"scripts": {
"test": "mocha --watch --reporter spec"
},
"author": "Vedran Jukic",
"license": "MIT",
"dependencies": {
"influx": "^5.0.3",
"object-sizeof": "^1.1.1",
"progress": "^1.1.8"
},
"devDependencies": {
"eslint": "^3.11.1",
"eslint-config-standard": "^6.2.1",
"eslint-plugin-promise": "^3.4.0",
"eslint-plugin-standard": "^2.0.1",
"chai": "^3.5.0",
"deepstream.io": "^2.0.0",
"deepstream.io-client-js": "^2.0.0",
"diff": "^3.0.1",
"jsdoc": "^3.4.3",
"mocha": "^3.1.2",
"sinon": "^1.17.6",
"winston": "^2.3.0"
}
}
Loading…
Cancel
Save