Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 10 additions & 1 deletion node/app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,13 @@ To completely reset databox run:

```
npm run wipe-databox
```
```

# Export Destination

When actuated the app also tries to post data to an external URL using
the export service. The default URL is `https://postman-echo.com/post`.
If you change the URL then make sure you change it in the manifest
export-whitelist, upload the changed manifest and re-install the app,
as well as changing the URL in the code (`main.js`).

4 changes: 4 additions & 0 deletions node/app/src/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ RUN addgroup -S databox && adduser -S -g databox databox && \

ADD ./package.json package.json

# only if using git
#RUN apk --no-cache add git

RUN npm install --production

USER databox
Expand All @@ -19,4 +22,5 @@ LABEL databox.type="app"

EXPOSE 8080

#CMD ["sleep","3000000"]
CMD ["npm","run","start-prod"]
3 changes: 3 additions & 0 deletions node/app/src/Dockerfile-dev
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ RUN addgroup -S databox && adduser -S -g databox databox && \
apk --no-cache add build-base pkgconfig nodejs npm libzmq zeromq-dev libsodium-dev python && \
npm install zeromq@4.6.0 --zmq-external --verbose

# if using git
RUN apk --no-cache add git

#globally install node packages and add to path
ADD ./package.json package.json
RUN npm install -g
Expand Down
8 changes: 4 additions & 4 deletions node/app/src/databox-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"docker-registry": "databoxsystems",
"docker-image-tag": "0.5.2",
"databox-type": "app",
"version": "0.1.0",
"version": "1.1.0",
"description": "basic node hello world app",
"author": "Databox Inc.",
"licence": "MIT",
Expand Down Expand Up @@ -32,8 +32,8 @@
"volumes": [],
"export-whitelist": [
{
"url": "https://export.amar.io/",
"description": "Exports the data to amar.io"
"url": "https://postman-echo.com/post",
"description": "Test export"
}
]
}
}
68 changes: 45 additions & 23 deletions node/app/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ var bodyParser = require("body-parser");
var databox = require("node-databox");
var WebSocket = require("ws");

const DATABOX_ARBITER_ENDPOINT = process.env.DATABOX_ARBITER_ENDPOINT || 'tcp://127.0.0.1:4444';
const DATABOX_ZMQ_ENDPOINT = process.env.DATABOX_ZMQ_ENDPOINT || "tcp://127.0.0.1:5555";
const DATABOX_TESTING = !(process.env.DATABOX_VERSION);
const PORT = DATABOX_TESTING ? 8090 : process.env.PORT || '8080';

//this will ref the timeseriesblob client which will observe and write to the databox actuator (created in the driver)
let tsc;
//this will ref the store client which will observe and write to the databox actuator (created in the driver)
let store;
let helloWorldActuatorDataSourceID;

let exportClient = databox.NewExportClient( DATABOX_ARBITER_ENDPOINT, true )
const EXPORT_URL = 'https://postman-echo.com/post'

//server and websocket connection;
let ws, server = null;
Expand All @@ -20,37 +25,53 @@ const listenToActuator = (emitter) => {
console.log("started listening to actuator");

emitter.on('data', (data) => {
console.log("seen data from the hello world actuator!", JSON.parse(data.data));
console.log("seen data from the hello world actuator!", data);
if (ws) {
ws.send(data.data);
databox.export.longpoll('https://export.amar.io/', data.data)
.catch((err) => {
console.log("[error] export.longpoll ", err)
})
}
let json = JSON.stringify(data.data)
ws.send(json);
// Note, export service deprecated and not currently supported
exportClient.Longpoll( EXPORT_URL, data.data )
.then((res) => {
console.log('Export ok', res)
let poll = function() {
exportClient.Longpoll( EXPORT_URL, data.data, res.id )
.then((res) => {
console.log('Export poll ok', res)
if (res.state !== 'Finished')
setTimeout(poll, 10000)
})
.catch((err) => {
console.log('Export poll error', err)
})
}
if (res.id && res.state !== 'Finished')
setTimeout(poll, 1000)
})
.catch((err) => {
console.log('Export error', err)
})
}
});

emitter.on('error', (err) => {
console.warn(err);
console.warn("error from actuator", err);
});
}

if (DATABOX_TESTING) {
tsc = databox.NewTimeSeriesBlobClient(DATABOX_ZMQ_ENDPOINT, false);
tsc.Observe("helloWorldActuator").then((emitter) => {
store = databox.NewStoreClient(DATABOX_ZMQ_ENDPOINT, DATABOX_ARBITER_ENDPOINT, false);
helloWorldActuatorDataSourceID = "helloWorldActuator";
store.TSBlob.Observe(helloWorldActuatorDataSourceID).then((emitter) => {
listenToActuator(emitter);
});
} else {
let helloWorldActuator;

//listen in on the helloWorld Actuator, which we have asked permissions for in the manifest
databox.HypercatToSourceDataMetadata(process.env[`DATASOURCE_helloWorldActuator`]).then((data) => {
helloWorldActuator = data
return databox.NewTimeSeriesBlobClient(helloWorldActuator.DataSourceURL, false)
}).then((store) => {
tsc = store;
return store.Observe(helloWorldActuator.DataSourceMetadata.DataSourceID)
}).then((emitter) => {
let helloWorldActuator = databox.HypercatToDataSourceMetadata(process.env[`DATASOURCE_helloWorldActuator`]);
helloWorldActuatorDataSourceID = helloWorldActuator.DataSourceID;
let helloWorldStore = databox.GetStoreURLFromHypercat(process.env[`DATASOURCE_helloWorldActuator`]);
store = databox.NewStoreClient(helloWorldStore, DATABOX_ARBITER_ENDPOINT, false)
store.TSBlob.Observe(helloWorldActuatorDataSourceID, 0)
.then((emitter) => {
listenToActuator(emitter);
}).catch((err) => {
console.warn("Error Observing helloWorldActuator", err);
Expand All @@ -75,8 +96,9 @@ app.get("/ui", function (req, res) {

app.get('/ui/actuate', (req, res) => {

let data = { msg: `${Date.now()}: databox actuation event` };
return new Promise((resolve, reject) => {
tsc.Write("helloWorldActuator", { msg: `${Date.now()}: databox actuation event` }).then(() => {
store.TSBlob.Write(helloWorldActuatorDataSourceID, data).then(() => {
console.log("successfully actuated!");
resolve();
}).catch((err) => {
Expand All @@ -101,7 +123,7 @@ if (DATABOX_TESTING) {

} else {
console.log("[Creating https server]", PORT);
const credentials = databox.getHttpsCredentials();
const credentials = databox.GetHttpsCredentials();
server = https.createServer(credentials, app).listen(PORT);
}

Expand Down
Loading