Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to Prep for Handling PointSet Messages #151

Merged
merged 25 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1b557be
Restructured the Cloud Function to make it easier to understand
slevertbiot Apr 25, 2022
e74053b
Enhancing implementation
slevertbiot Apr 25, 2022
1611569
Fleshing out system messages
slevertbiot Apr 25, 2022
2c9b71a
Refactoring how we instantiate collection
slevertbiot Apr 28, 2022
6497e50
Re-ordering inputs to write function
slevertbiot Apr 28, 2022
cbbdc54
Adding basic test framework
slevertbiot Apr 28, 2022
700c9ae
Adding test to call handleUdmiEvent
slevertbiot Apr 28, 2022
0b940bc
Updating test to validate a spy
slevertbiot Apr 28, 2022
29fe918
Converting project to typescript
slevertbiot May 10, 2022
7ae93be
Splitting into distinct purpose files and tests
slevertbiot May 11, 2022
423181c
Adding tests and minor refactors
slevertbiot May 11, 2022
fd14043
Adding tests and minor refactors
slevertbiot May 11, 2022
06af484
Adding tests and minor refactors
slevertbiot May 11, 2022
beaa219
reverting some changes to package.json
slevertbiot May 11, 2022
8ae5818
reverting some changes to package.json
slevertbiot May 11, 2022
d049824
Removing comments that are not relevant
slevertbiot May 11, 2022
b383852
Adding build script to transpile the code into js
slevertbiot May 12, 2022
69f5181
Adding proper support for husky
slevertbiot May 12, 2022
68ad982
Reverting Changes to Readme
slevertbiot May 12, 2022
23d2970
Removing uneeded ignore
slevertbiot May 12, 2022
796421c
Quick refactor to get ready for pointset messages
slevertbiot May 13, 2022
cd19b89
Merging Master and refactor to simplify into of pointset handling
slevertbiot May 16, 2022
eddb34e
Adding refacotored code
slevertbiot May 16, 2022
a2baf6b
Removing old code
slevertbiot May 16, 2022
57124de
Adding sample messages and cleaning arguments
slevertbiot May 16, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion udmif/event-handler/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# UDMI Event Handler

The project uses Google Cloud Functions Infrastructure to allow faster iteration of a GCP Node Cloud function.
The Typescript project uses Google Cloud Functions Infrastructure to allow faster iteration of a GCP Node Cloud function.

---

Expand Down Expand Up @@ -41,6 +41,20 @@ curl -d "@sample.json" \

---

## Testing the code

```
npm run test
```

or

```
npm run testInteractive
```

---

## Deploying to GCP

TBD
Expand Down
2 changes: 1 addition & 1 deletion udmif/event-handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"dev": "nodemon --watch src --watch data -e js,ts,txt,response,json --exec node -r ts-node/register --inspect=0.0.0.0:6329 src/index.ts",
"start": "functions-framework --source=dist/ --target=handleUdmiEvent --signature-type=event",
"watch": "concurrently \"tsc -w\" \"nodemon --watch ./build/ --exec npm run start\"",
"test": "NODE_ENV=test jest --runInBand --silent",
"test": "NODE_ENV=test jest --runInBand --silent --detectOpenHandles",
"testInteractive": "NODE_ENV=test jest --runInBand --silent --watchAll",
"postinstall": "cd ../.. && husky install udmif/event-handler/.husky",
"build": "tsc -p ."
Expand Down
16 changes: 16 additions & 0 deletions udmif/event-handler/sample_messages/ahu1-pointset-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"ackId": "U0RQBhYsXUZIUTcZCGhRDk9eIz81IChFEgkIFAV8fXFDW3VdVBoHUQ0ZcnxpdWxbQlcKRlsoVVsRDXptXG3RyKa6RF9BcW5TGwYHQld8X1kTDW9YXV3854T0odijQnBmK9eb7vBIf8fdzfFuZiA9XxJLLD5-LTxFQV5AEkwmCURJUytDCypYEU4EISE-MD5F",
"message": {
"attributes": {
"deviceId": "AHU-1",
"deviceNumId": "2625324262579600",
"deviceRegistryId": "ZZ-TRI-FECTA",
"projectId": "labs-333619",
"subFolder": "pointset",
"subType": "config"
},
"data": "ewogICJwb2ludHMiIDogewogICAgImZpbHRlcl9kaWZmZXJlbnRpYWxfcHJlc3N1cmVfc2V0cG9pbnQiIDogeyB9LAogICAgImZpbHRlcl9hbGFybV9wcmVzc3VyZV9zdGF0dXMiIDogewogICAgICAicmVmIiA6ICJCVjExLnByZXNlbnRfdmFsdWUiCiAgICB9LAogICAgImZpbHRlcl9kaWZmZXJlbnRpYWxfcHJlc3N1cmVfc2Vuc29yIiA6IHsKICAgICAgInJlZiIgOiAiQVYxMi5wcmVzZW50X3ZhbHVlIgogICAgfQogIH0KfQ==",
"messageId": "4498757420280741",
"publishTime": "2022-04-25T17:05:34.229Z"
}
}
16 changes: 16 additions & 0 deletions udmif/event-handler/sample_messages/ahu1-pointset-model.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"ackId": "U0RQBhYsXUZIUTcZCGhRDk9eIz81IChFEgkIFAV8fXFDW3VdWRoHUQ0ZcnxpdWxbQlcFE1YtVVgRDHptXG35yqa6RF9BcW5TGwkDR1F7XVMfDm5bW13854T0odijQnBmK_-Z7vBIf6jw3a1tZiA9XxJLLD5-LTxFQV5AEkwmCURJUytDCypYEU4EISE-MD5F",
"message": {
"attributes": {
"deviceId": "AHU-1",
"deviceNumId": "2625324262579600",
"deviceRegistryId": "ZZ-TRI-FECTA",
"projectId": "labs-333619",
"subFolder": "pointset",
"subType": "model"
},
"data": "ewogICJwb2ludHMiIDogewogICAgImZpbHRlcl9hbGFybV9wcmVzc3VyZV9zdGF0dXMiIDogewogICAgICAidW5pdHMiIDogIk5vLXVuaXRzIiwKICAgICAgInJlZiIgOiAiQlYxMS5wcmVzZW50X3ZhbHVlIgogICAgfSwKICAgICJmaWx0ZXJfZGlmZmVyZW50aWFsX3ByZXNzdXJlX3NldHBvaW50IiA6IHsKICAgICAgInVuaXRzIiA6ICJCYXJzIgogICAgfSwKICAgICJmaWx0ZXJfZGlmZmVyZW50aWFsX3ByZXNzdXJlX3NlbnNvciIgOiB7CiAgICAgICJ1bml0cyIgOiAiRGVncmVlcy1DZWxzaXVzIiwKICAgICAgInJlZiIgOiAiQVYxMi5wcmVzZW50X3ZhbHVlIgogICAgfQogIH0KfQ==",
"messageId": "4498812252843677",
"publishTime": "2022-04-25T17:05:33.947Z"
}
}
18 changes: 18 additions & 0 deletions udmif/event-handler/sample_messages/ahu1-pointset-state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"ackId": "U0RQBhYsXUZIUTcZCGhRDk9eIz81IChFEgkIFAV8fXBBTnVdXXFoUQ0Zcn1gfWpcElMCE1J-WVEbB2hObXUfgpey0UtDVW5eFAMEQFF_WV4fD2pcVHwvqruQn67Et11AKDnW3_jdaXut_rt9ZiM9XxJLLD5-LTxFQV5AEkwmCURJUytDCypYEU4EISE-MD5F",
"message": {
"attributes": {
"cloudRegion": "us-central1",
"deviceId": "AHU-1",
"deviceNumId": "2655448794278295",
"deviceRegistryId": "ZZ-TRI-FECTA",
"deviceRegistryLocation": "us-central1",
"projectId": "labs-333619",
"subFolder": "pointset",
"subType": "state"
},
"data": "eyJwb2ludHMiOnsiZmlsdGVyX2FsYXJtX3ByZXNzdXJlX3N0YXR1cyI6e30sImZpbHRlcl9kaWZmZXJlbnRpYWxfcHJlc3N1cmVfc2V0cG9pbnQiOnt9LCJmaWx0ZXJfZGlmZmVyZW50aWFsX3ByZXNzdXJlX3NlbnNvciI6e319LCJ0aW1lc3RhbXAiOiIyMDIyLTA0LTI1VDE3OjAwOjI2WiJ9",
"messageId": "4472652165422089",
"publishTime": "2022-04-25T17:07:07.906Z"
}
}
15 changes: 15 additions & 0 deletions udmif/event-handler/sample_messages/ahu1-pointset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"ackId": "RFAGFixdRkhRNxkIaFEOT14jPzUgKEUSCQgUBXx9cEJKdV9VfGhRDRlyfWB9alwSUFMXUnsOURoHaE5tdR_GlbLRS0NVbl4UAwRBUHdYWxMMYFxecS-qu5CfrsS3XUAvOZXd-N1pe8fY5ORvZiM9XxJLLD5-LTxFQV5AEkwmCURJUytDCypYEU4EISE-MD5FUw",
"message": {
"attributes": {
"deviceId": "AHU-1",
"deviceNumId": "2625324262579600",
"deviceRegistryId": "ZZ-TRI-FECTA",
"deviceRegistryLocation": "us-central1",
"projectId": "labs-333619",
"subFolder": "pointset"
},
"data": "eyJ0aW1lc3RhbXAiOiIyMDIyLTA0LTI1VDE3OjAwOjI2WiIsInZlcnNpb24iOiIxLjMuMTQiLCJwb2ludHMiOnsiZmlsdGVyX2FsYXJtX3ByZXNzdXJlX3N0YXR1cyI6eyJwcmVzZW50X3ZhbHVlIjo3OH0sImZpbHRlcl9kaWZmZXJlbnRpYWxfcHJlc3N1cmVfc2V0cG9pbnQiOnsicHJlc2VudF92YWx1ZSI6NzF9LCJmaWx0ZXJfZGlmZmVyZW50aWFsX3ByZXNzdXJlX3NlbnNvciI6eyJwcmVzZW50X3ZhbHVlIjo4Mn19fQ==",
"publishTime": "2022-04-25T17:00:27.391Z"
}
}
102 changes: 0 additions & 102 deletions udmif/event-handler/sample_messages/decodedEvents.json

This file was deleted.

12 changes: 0 additions & 12 deletions udmif/event-handler/sample_messages/pointSetEvent.json

This file was deleted.

13 changes: 0 additions & 13 deletions udmif/event-handler/sample_messages/pointSetStatesEvent.json

This file was deleted.

12 changes: 0 additions & 12 deletions udmif/event-handler/sample_messages/systemEvent.json

This file was deleted.

13 changes: 0 additions & 13 deletions udmif/event-handler/sample_messages/systemStatesEvent.json

This file was deleted.

5 changes: 4 additions & 1 deletion udmif/event-handler/src/DeviceDao.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Collection } from 'mongodb';
import { DeviceKey, DeviceDocument } from './model';

const options = { upsert: true };

export interface DeviceDao {
upsert(filterQuery: any, updateQuery: any);
}
Expand All @@ -11,8 +13,9 @@ export class DefaultDeviceDao implements DeviceDao {
async upsert(deviceKey: DeviceKey, deviceDocument: DeviceDocument): Promise<void> {
// we're using upsert which will allow document updates if it already exists and a document cretion if it does not
console.debug('Attempting to write the following device document: ' + JSON.stringify(deviceDocument));
const options = { upsert: true };

const result = await this.collection.updateOne(deviceKey, { $set: deviceDocument }, options);

// let's log the result to give us some feedback on what occurred during the upsert
console.log(JSON.stringify(result));
}
Expand Down
38 changes: 0 additions & 38 deletions udmif/event-handler/src/DeviceDocumentBuilder.ts

This file was deleted.

65 changes: 65 additions & 0 deletions udmif/event-handler/src/DeviceDocumentFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { DeviceDocument, UdmiMessage } from './model';

const POINTSET_SUB_FOLDER = 'pointset';
const SYSTEM_SUB_FOLDER = 'system';
const MODEL = 'model';
const STATE = 'state';

export function createDeviceDocument(message: UdmiMessage): DeviceDocument {
if (isSystemState(message)) {
return createSystemStateDocument(message);
} else if (isSystemModel(message)) {
return createSystemModelDocument(message);
} else {
return createDefaultDeviceDocument(message);
}
}

function createSystemModelDocument(message: UdmiMessage): DeviceDocument {
const deviceDocument: DeviceDocument = createDefaultDeviceDocument(message);

deviceDocument.section = message.data.location.section;
deviceDocument.site = message.data.location.site;

return deviceDocument;
}

function createSystemStateDocument(message: UdmiMessage): DeviceDocument {
const deviceDocument: DeviceDocument = createDefaultDeviceDocument(message);

deviceDocument.make = message.data.hardware.make;
deviceDocument.model = message.data.hardware.model;
deviceDocument.operational = message.data.operational;
deviceDocument.serialNumber = message.data.serial_no;
deviceDocument.firmware = message.data.software.firmware;

return deviceDocument;
}

function createDefaultDeviceDocument(message: UdmiMessage): DeviceDocument {
const deviceDocument: DeviceDocument = {
name: message.attributes.deviceId,
id: message.attributes.deviceNumId,
};

if (message.data.timestamp) {
deviceDocument.lastPayload = message.data.timestamp;
}
return deviceDocument;
}

export function isSystemState(message): boolean {
return isSubFolder(message, SYSTEM_SUB_FOLDER) && isSubType(message, STATE);
}

export function isSystemModel(message): boolean {
return isSubFolder(message, SYSTEM_SUB_FOLDER) && isSubType(message, MODEL);
}

export function isSubFolder(message, folderName: string): boolean {
return message.attributes.subFolder === folderName;
}

export function isSubType(message, type: string): boolean {
return message.attributes.subType === type;
}
Loading