Skip to content

Commit

Permalink
Hint for hard coded realmdb and js formatting
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Schleemilch <[email protected]>
  • Loading branch information
sschleemilch committed Jun 19, 2024
1 parent e0d7aa6 commit 9722e2b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 39 deletions.
11 changes: 3 additions & 8 deletions cdsp/information-layer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Setting up an information layer in the CDSP involves running a [Database-Router]

Please follow installation instructions of the chosen [handler](./handlers/)

## [Installation of Database-Router](./router)
## [Installation of Database-Router](./router/README.md#Install)

# Running "Hello World" example

Expand All @@ -31,15 +31,10 @@ The Hello World example in our case is quite simple. We feed an updated value fo
### IoTDB
- Not yet supported

## Start the Database Router
Start router by executing in [router src](./router/src/) directory the command:

```bash
node websocket-server.js
```
## [Start](./router/README.md#Run) the Database Router

## Look out for the Websocket Server message in the console
Now you can changed the value of *Vehicle_Cabin_HVAC_AmbientAirTemperature* in ATLAS cloud to let's say `23`. After changing you should immediately see this line in console:
Now you can changed the value of `Vehicle_Cabin_HVAC_AmbientAirTemperature` in ATLAS cloud to let's say `23`. After changing you should immediately see this line in console:

```
the value of "Vehicle_Cabin_HVAC_AmbientAirTemperature" changed to 23
Expand Down
73 changes: 44 additions & 29 deletions cdsp/information-layer/handlers/realmdb/src/realm-handler.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,92 @@
const Realm = require('realm');
const config = require('../config/config');
const vehicleConfig = require('../config/vehicle-config'); // Import vehicle config file
const { v4: uuidv4 } = require('uuid'); // Importing UUID generator
const Realm = require("realm");
const config = require("../config/config");
const vehicleConfig = require("../config/vehicle-config"); // Import vehicle config file
const { v4: uuidv4 } = require("uuid"); // Importing UUID generator

const MediaElementSchema = {
primaryKey: '_id',
name: 'Vehicles',
primaryKey: "_id",
name: "Vehicles",
properties: {
_id: 'int',
Vehicle_Cabin_HVAC_AmbientAirTemperature: 'double'
}
_id: "int",
Vehicle_Cabin_HVAC_AmbientAirTemperature: "double",
},
};

const app = new Realm.App({ id: config.realmAppId });
const credentials = Realm.Credentials.apiKey(config.realmApiKey);

const realmConfig = {
schema: [MediaElementSchema],
path: 'myrealm12.realm',
path: "myrealm12.realm",
sync: {
user: null, // will be assigned after authentication
flexible: true,
error: error => {
console.error('Realm sync error:', error);
}
}
error: (error) => {
console.error("Realm sync error:", error);
},
},
};

const authenticateAndConnectToRealm = async (sendMessageToClients, onMediaElementChange) => {
const authenticateAndConnectToRealm = async (
sendMessageToClients,
onMediaElementChange,
) => {
try {
const user = await app.logIn(credentials);
console.log('Successfully authenticated with Realm');
console.log("Successfully authenticated with Realm");

realmConfig.sync.user = user;
const realm = await Realm.open(realmConfig);
console.log('Realm connection established successfully');
console.log("Realm connection established successfully");

const MediaElements = realm.objects('Vehicles').subscribe();
const MediaElements = realm.objects("Vehicles").subscribe();
console.log(MediaElements);

const objectId = vehicleConfig.Vin; // Retrieve objectId from config file
const mediaElement = realm.objectForPrimaryKey('Vehicles', objectId);
const mediaElement = realm.objectForPrimaryKey("Vehicles", objectId);
console.log(mediaElement);

// Set WebSocket server ID as the same objectId
const websocketId = String(objectId);

mediaElement.addListener((mediaElement, changes) => onMediaElementChange(mediaElement, changes, sendMessageToClients, websocketId));
mediaElement.addListener((mediaElement, changes) =>
onMediaElementChange(
mediaElement,
changes,
sendMessageToClients,
websocketId,
),
);
} catch (error) {
console.error('Failed to authenticate with Realm:', error);
console.error("Failed to authenticate with Realm:", error);
}
};

const onMediaElementChange = (mediaElement, changes, sendMessageToClients, websocketId) => {
const onMediaElementChange = (
mediaElement,
changes,
sendMessageToClients,
websocketId,
) => {
if (changes.deleted) {
console.log(`MediaElement is deleted: ${changes.deleted}`);
} else {
changes.changedProperties.forEach(prop => {
changes.changedProperties.forEach((prop) => {
console.log(`* the value of "${prop}" changed to ${mediaElement[prop]}`);

// Generate a meaningful UUID for WebSocket response
const uuid = uuidv4();

const message = {
type: 'update',
tree: 'VSS',
type: "update",
tree: "VSS",
id: websocketId, // Use the WebSocket server ID
uuid: uuid, // Use generated UUID
dateTime: new Date().toISOString(),
node: {
name: prop, // Sending the property name as node name
value: mediaElement[prop] // Sending the property value as node value
}
value: mediaElement[prop], // Sending the property value as node value
},
};
sendMessageToClients(message);
});
Expand All @@ -80,5 +95,5 @@ const onMediaElementChange = (mediaElement, changes, sendMessageToClients, webso

module.exports = {
authenticateAndConnectToRealm,
onMediaElementChange
onMediaElementChange,
};
15 changes: 13 additions & 2 deletions cdsp/information-layer/router/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
With this component one can configure which database it shall connect to *(As this configuration capability is not yet implemented you can skip the configuration step, it's always choosen RealmDB for now)*.
With this component one can configure which database it shall connect to.

# Installation
> [!WARNING]
> As the configuration capability is not yet implemented, RealmDB is hard coded as a database!
# Install

Execute in this directory:

```bash
npm install
```

# Run

Start router by executing in [src](./src/) directory the command:

```bash
node websocket-server.js
```

0 comments on commit 9722e2b

Please sign in to comment.