我正在开发一个简单的项目,其中我做了一个我放在Orion-LD上的实体列表。现在我想做一个服务器,它允许在实体属性的状态发生变化时接收通知。我选择使用Node.JS Express,但它不是强制性的。我在这里留下用于实现我的服务器的简单代码。
const express = require('express');
const request = require('request');
const app = express();
const PORT = 3000;
//Subscription on batteryLevel changes of device 9845A
app.get('/subscribe', (req, res) => {
const data = {
"description": "Subscription",
"subject": {
"entities": [
{
"id": "urn:ngsi-ld:Device:9845A",
"type": "Device"
}
],
"condition": {
"attrs": ["batteryLevel"]
}
},
"notification": {
"http": {
"url": `http://localhost:${PORT}/notification`
},
"attrsFormat" : "keyValues"
}
};
const options = {
headers: {
'Content-Type': 'application/json',
},
json: data
};
request.post('http://localhost:1026/v2/subscriptions', options, (error, response, request) => {
console.log(response.body);
res.end("Succesfully subscribed!");
});
});
// Listen on notification
app.post('/notification', (req, res) => {
console.log('Notification received:', req.body);
res.sendStatus(200);
});
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}`)
});
以下是docker-compose.yaml
的实现:
version: "3.9"
services:
# Orion-LD Context Broker
orion:
container_name: fiware-orion
image: fiware/orion:latest
hostname: orion
depends_on:
- mongo-db
expose:
- "1026"
ports:
- "1026:1026"
command: -dbhost mongo-db -logLevel DEBUG
networks:
- default
# Database
mongo-db:
container_name: fiware-mongo
image: mongo:4.4
hostname: mongo-db
expose:
- "27017"
ports:
- "27017:27017"
command: --nojournal
volumes:
- ./data/mongo-db:/data
networks:
- default
# Server
node-server:
container_name: fiware-node
build: .
hostname: node-server
depends_on:
- orion
expose:
- "3000"
ports:
- "3000:3000"
volumes:
- ./data/node:/data
networks:
default:
aliases:
- iot-sensors
- context-provider
# RDF Store
virtuoso:
container_name: fiware-virtuoso
image: openlink/virtuoso-opensource-7:latest
ports:
- "1111:1111"
- "8890:8890"
environment:
SPARQL_UPDATE: true
DBA_PASSWORD: dba
volumes:
- ./data/virtuoso:/data
networks:
default:
这是与Node.js Express相关的Dockerfile
:
FROM node:latest
WORKDIR /app
COPY server/package*.json server/server.js ./
RUN npm install
EXPOSE 3000
CMD [ "node", "server.js" ]
问题是,如果我正确订阅了一个现有的实体,它会在订阅中报告一个"status": "failed"
,因此我无法收到有关状态的通知。阅读Orion LD日志,它似乎无法与我的服务器在端口3000
通信,但我不知道为什么。有人能帮助我吗?
1条答案
按热度按时间kyvafyod1#
我解决了这个问题。问题是我试图通过localhost访问节点服务器,但它与Orion在同一个容器中,因此无法访问它。所以我简单地通过访问我为节点服务定义的
fiware-node:3000
地址来解决它。