简体   繁体   English

通知 python orion/quantumleap 订阅变更

[英]Notify python orion/quantumleap subscription changes

Is there any way to get notified in python when a Quantumleap o Orion subscription fires for a changed value?当 Quantumleap o Orion 订阅因更改的值而触发时,有什么方法可以在 python 中收到通知吗?

I think the simplest way to receive HTTP notifications coming from Orion is to setup an http server to listen those notifications (http requests) within you Python code.我认为接收来自Orion的 HTTP 通知的最简单方法是设置一个 http 服务器来监听您 Python 代码中的那些通知(http 请求)。 For example by using Flask .例如通过使用Flask

Best.最好。

I've dockerized the nickjj web server , switched the bind_host to 0.0.0.0 as specified in the documentation, and exposed the port 8008:8008 in docker-compose .我已经对nickjj web 服务器进行了 docker 化,将bind_host切换为文档中指定的0.0.0.0 ,并在docker-compose中公开了端口8008:8008

nickjj Python web server : nickjj Python web 服务器

from http.server import HTTPServer, BaseHTTPRequestHandler
from sys import argv

BIND_HOST = '0.0.0.0'
PORT = 8008

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.write_response(b'')

    def do_POST(self):
        content_length = int(self.headers.get('content-length', 0))
        body = self.rfile.read(content_length)

        self.write_response(body)

    def write_response(self, content):
        self.send_response(200)
        self.end_headers()
        self.wfile.write(content)

        print(self.headers)
        print(content.decode('utf-8'))


if len(argv) > 1:
    arg = argv[1].split(':')
    BIND_HOST = arg[0]
    PORT = int(arg[1])

print(f'Listening on http://{BIND_HOST}:{PORT}\n')

httpd = HTTPServer((BIND_HOST, PORT), SimpleHTTPRequestHandler)
httpd.serve_forever()

Dockerfile to build the web server service: Dockerfile搭建web服务器服务:

# Build the service from the base image
FROM python:3.9.16-alpine3.17

# During building, run the following commands to install dependecies
RUN apk add --no-cache openssl-dev curl-dev
RUN pip install --upgrade pip
# RUN pip install pycurl crate requests

# Set the work directory
WORKDIR /webserver

# Copy in the continer the following files
COPY ./webserver ./webserver

# Run this command at startup
CMD ["python","-u","webserver", "webserver:8008"]

docker-compose configuration for the web server, with curl for debug: docker-compose 配置为 web 服务器, curl用于调试:

webserver:
  image: webserver
  stdin_open: true # docker run -i
  tty: true        # docker run -t
  container_name: 'webserver'
  ports:
    - "8008:8008"
  networks:
    - default

I've provisioned the relative subscription in Orion:我已经在 Orion 中提供了相关订阅:

curl -iX POST \
    'http://localhost:1026/v2/subscriptions/' \
    -H 'Content-Type: application/json' \
    -H 'fiware-service: opcua_car' \
    -H 'fiware-servicepath: /demo' \
    -d '{
  "description": "Subscriptions for Python webserver",
  "subject": {
    "entities": [
      {
        "idPattern": ".*",
        "type": "PLC"
      }
    ],
    "condition": {
      "attrs": [
        "processStatus"
      ]
    }
  },
  "notification": {
    "http": {
      "url": "http://webserver:8008/"
    },
    "attrs": [
      "processStatus"
    ],
    "metadata": [
      "dateCreated",
      "dateModified"
    ]
  },
  "throttling": 1
}'

And I finally get my subscription notifications in Python.我终于在 Python 收到了我的订阅通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM