Skip to content

Commit

Permalink
HTTP announce endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
gsteinLTU committed Jul 30, 2024
1 parent ea40aee commit b105600
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/procedures/iotscape/iotscape-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ IoTScapeServices.updateOrCreateServiceInfo = function (
let service = IoTScapeDevices._services[name];
IoTScapeServices._serviceDefinitions[name] = definition;

if(!rinfo) {
logger.log("Service " + name + " created without connection info");
return;
}

logger.log(
"Discovering " + name + ":" + id + " at " + rinfo.address + ":" +
rinfo.port,
Expand Down
16 changes: 10 additions & 6 deletions src/procedures/iotscape/iotscape.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,18 @@ IoTScape._send = function (service, id, command, caller) {
* @param {String} definition Service definition
* @param {RemoteInfo} remote Remote host information
*/
IoTScape._createService = async function (definition, remote) {
IoTScape._createService = async function (definition, remote = null) {
let parsed = null;

try {
parsed = JSON.parse(definition);
} catch (err) {
logger.log("Error parsing IoTScape service: " + err);
return;
if (typeof definition === "string") {
try {
parsed = JSON.parse(definition);
} catch (err) {
logger.log("Error parsing IoTScape service: " + err);
return;
}
} else {
parsed = definition;
}

// Ignore empty and request messages sent to this method
Expand Down
13 changes: 13 additions & 0 deletions src/procedures/iotscape/routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const express = require("express");
const router = express();
const logger = require("../utils/logger")("iotscape-routes");
const IoTScape = require("./iotscape");
const bodyParser = require("body-parser");

router.get(
"/port",
Expand All @@ -10,4 +13,14 @@ router.get(
},
);

router.post(
"/announce",
bodyParser.json({ limit: "1mb" }),
async (req, res) => {
logger.info(`HTTP announcement from ${req.ip}`);
IoTScape._createService(req.body);
res.status(200).send("OK");
},
)

module.exports = router;

0 comments on commit b105600

Please sign in to comment.