Skip to content

Commit

Permalink
Merge pull request #45 from Buildings-IOT/get-device-by-id
Browse files Browse the repository at this point in the history
Get device by
  • Loading branch information
slevertbiot committed Mar 15, 2022
2 parents c9d18e0 + 8848512 commit c6aef47
Show file tree
Hide file tree
Showing 10 changed files with 160,092 additions and 24 deletions.
5 changes: 4 additions & 1 deletion udmd/api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ globalConfig.json
.DS_Store

# environment variables
.env
.env

# mongo output
.mongodb
29 changes: 19 additions & 10 deletions udmd/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The project uses Typescript and depends on Node and npm already being installed

---

### Setup the Poject to be Run
## Setup the Poject to be Run

1. Run the build script

Expand All @@ -18,32 +18,41 @@ The project uses Typescript and depends on Node and npm already being installed

2. Edit the values in .env as needed

### Running the Project
---

## Running the Project

1. Run the project, which will bind to port 4300;
```
npm run dev
```
2. Navigate to [http://localhost:4300/](http://localhost:4300/) to see the GraphQL interface. The app will automatically reload if you change any of the source files.

---

## Populate Data for the Project - MacOs specific

### Populate Data for the Project
**Assumptions:**

**Assumption:** mongodb and mongosh are already installed. Not all the steps have been verified to work.
1. mongodb is installed and running
2. udmi db has been create

1. Create a 'udmi' db with a 'device' collection by following the instructions here: [Creating a DB](https://www.mongodb.com/basics/create-database).
2. Connect to the local mongo db using Mongo-Shell if not already connected
Install the required mongodb tools
```
mongosh -host mongo-db udmi
brew tap mongodb/brew
brew install mongodb-database-tools
```
3. Load the data using the following command:

Execute the data import
```
load("/util/UDMI-devices.js");
mongoimport --uri="mongodb://127.0.0.1:27017/udmi" -c=device --file=util/devices.json --mode=upsert --jsonArray
```

### Notes
---

## Notes
- Run `npm build` to build the project. The build artifacts will be stored in the `dist/` directory.
- Run `npm test` to execute the unit tests via [Jest](https://jestjs.io).
- Run `npm run testInteractive` to continuosly execute the unit tests via [Jest](https://jestjs.io). The tests will be run every time a file is saved.
- Followed instructions here [Using with MongoDB](https://jestjs.io/docs/mongodb) and [jest-mongodb](https://github.com/shelfio/jest-mongodb) to configure and run in memory mongodb for testing
- Creating a MongoDB - Create a 'udmi' db with a 'device' collection by following the instructions here: [Creating a DB](https://www.mongodb.com/basics/create-database).
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ exports[`Devices device 1`] = `
Object {
"data": Object {
"device": Object {
"firmware": "v-1",
"id": "00000000-0000-0000-0000-000000000001",
"lastPayload": "2022-08-30",
"make": "make-1",
"model": "BBBB-1",
"name": "CDS-1",
"operational": true,
"section": "SIN-MBC1",
"serialNumber": "serialNo-1",
"site": "SG-SIN-MBC1",
"tags": Array [],
},
Expand Down
16 changes: 15 additions & 1 deletion udmd/api/src/__tests__/device/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@ export function createDevices(count: number): Device[] {
const section: string = `SIN-MBC${n}`;
const lastPayload: string = '2022-08-30';
const operational: boolean = n % 3 == 0 ? false : true;
devices.push({ id, name, make, model, site, section, lastPayload, operational, tags: [] });
const serialNumber: string = `serialNo-${n}`;
const firmware: string = `v-${n}`;
devices.push({
id,
name,
make,
model,
site,
section,
lastPayload,
operational,
serialNumber,
firmware: firmware,
tags: [],
});
n++;
}

Expand Down
2 changes: 2 additions & 0 deletions udmd/api/src/__tests__/device/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const QUERY_DEVICE = gql`
section
lastPayload
operational
firmware
serialNumber
tags
}
}
Expand Down
16 changes: 15 additions & 1 deletion udmd/api/src/device/dao/static/StaticDeviceDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,21 @@ export class StaticDeviceDAO implements DeviceDAO {
const section: string = deviceSection;
const lastPayload: string = new Date(new Date().getTime() - randomInt(1000000000)).toISOString();
const operational: boolean = n % 3 == 0 ? false : true;
devices.push({ id, name, make, model, site, section, lastPayload, operational, tags: [] });
const serialNumber: string = `serialNo-${n}`;
const firmware: string = `v-${n}`;
devices.push({
id,
name,
make,
model,
site,
section,
lastPayload,
operational,
firmware: firmware,
serialNumber,
tags: [],
});
n++;
}

Expand Down
2 changes: 2 additions & 0 deletions udmd/api/src/device/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface Device {
section: string;
lastPayload: string;
operational: boolean;
firmware: string;
serialNumber: string;
tags?: string[];
}

Expand Down
4 changes: 4 additions & 0 deletions udmd/api/src/device/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ type Device {
lastPayload: String
# the operational state of the device
operational: Boolean
# the firmware version fo the device
firmware: String
# the serial number of the device
serialNumber: String
# the tags assigned to a device
tags: [String!]!
}
Expand Down
39 changes: 28 additions & 11 deletions udmd/api/util/UDMI-Devices.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const { v4: uuid } = require('uuid');

const deviceTemplates = [
{ make: 'Cisco', models: ['Mediator'], name: 'cis' },
{ make: 'BitBox USA', models: ['BitBox'], name: 'bb-usa' },
{ make: 'Automated Logic', models: ['LGR', 'G5CE'], name: 'aut-log' },
{ make: 'Enlightened', models: ['Light Gateway'], name: 'enl' },
{ make: 'Tridium', models: ['JACE 8000'], name: 'tri' },
{ make: 'Delta Controls', models: ['Entelibus Manager 100', 'CopperCube'], name: 'dc' },
{ make: 'Acquisuite', models: ['Obvious AcquiSuite A88 12-1'], name: 'acq' },
{ make: 'Schneider Electric / APC', models: ['PowerLogic ION', 'AP9630', 'AP9631', 'AP9635'], name: 'apc' },
{ make: 'Cisco', models: ['Mediator'], name: 'cis', firmwareVersion: ['v1.2'] },
{ make: 'BitBox USA', models: ['BitBox'], name: 'bb-usa', firmwareVersion: ['v2.3'] },
{ make: 'Automated Logic', models: ['LGR', 'G5CE'], name: 'aut-log', firmwareVersion: ['drv_melgr_vanilla_6-02-034', 'drv_melgr_vaisala_6-00a-001', 'drv_melgr_mb_combo_6-00-01'] },
{ make: 'Enlightened', models: ['Light Gateway'], name: 'enl', firmwareVersion: ['drv_fwex_101-00-2051'] },
{ make: 'Tridium', models: ['JACE 8000'], name: 'tri', firmwareVersion: ['4.2.36.36'] },
{ make: 'Delta Controls', models: ['CopperCube'], name: 'dc', firmwareVersion: ['535847'] },
{ make: 'Delta Controls', models: ['Entelibus Manager 100'], name: 'dc', firmwareVersion: ['4.3.7.64'] },
{ make: 'Acquisuite', models: ['Obvious AcquiSuite A88 12-1'], name: 'acq', firmwareVersion: ['v3.4'] },
{ make: 'Schneider Electric / APC', models: ['PowerLogic ION', 'AP9630', 'AP9631', 'AP9635'], name: 'apc', firmwareVersion: ['v4.5'] },
];

const sites = [
Expand All @@ -22,18 +23,20 @@ while (n <= 10000) {
const deviceTemplate = getRandom(deviceTemplates);
const deviceModel = getRandom(deviceTemplate.models);
const deviceSite = getRandom(sites);
const deviceSection = getRandom(deviceSite.sections);

const id = uuid();
const name = `${deviceTemplate.name}-${n}`;
const make = `${deviceTemplate.make}`;
const model = deviceModel;
const site = deviceSite.site;
const section = deviceSection;
const lastPayload = new Date(new Date() - getRandomInt(1000000000)).toISOString();
const operational = n % 3 == 0 ? false : true;
const section = getRandom(deviceSite.sections);
const serialNumber = generateSerial();
const tags = [];
const firmware = getRandom(deviceTemplate.firmwareVersion);

db.device.insertOne({ id, name, make, model, site, section, lastPayload, operational, "tags": [] });
db.device.insertOne({ id, name, make, model, site, section, lastPayload, operational, serialNumber, firmware, tags });
n++;
}

Expand All @@ -43,4 +46,18 @@ function getRandom(array) {

function getRandomInt(max) {
return Math.floor(Math.random() * max);
}

function generateSerial() {
var chars = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ',
serialLength = 10,
randomSerial = "";

for (var i = 0; i < serialLength; i = i + 1) {
const randomNumber = getRandomInt(chars.length);
randomSerial += chars.substring(randomNumber, randomNumber + 1);
}

return randomSerial;

}
Loading

0 comments on commit c6aef47

Please sign in to comment.