add example code generator
This commit is contained in:
+24
-33
@@ -1,44 +1,35 @@
|
||||
# .venv/bin/asyncapi-python-codegen asyncapi.yaml out --force
|
||||
# asyncapi generate fromTemplate ./test/fixtures/asyncapi.yaml ./ -o ./out/project
|
||||
# kouknout na FastStream
|
||||
# https://nats.io/blog/nats-supported-by-faststream/#writing-app-code
|
||||
# https://github.com/asyncapi/python-paho-template
|
||||
# https://www.asyncapi.com/docs/tools/generator/generator-template
|
||||
|
||||
asyncapi: 3.0.0
|
||||
|
||||
info:
|
||||
title: Temperature Service
|
||||
version: 1.0.0
|
||||
description: This service is in charge of processing all the events related to temperature.
|
||||
|
||||
description: Temperature sensor publishing readings
|
||||
servers:
|
||||
dev:
|
||||
host: test.mosquitto.org #in case you're using local mosquitto instance, change this value to localhost.
|
||||
protocol: mqtt
|
||||
|
||||
production:
|
||||
host: localhost:4222
|
||||
protocol: nats
|
||||
channels:
|
||||
temperatureChanged:
|
||||
address: temperature/changed
|
||||
temperature/read:
|
||||
address: temperature/read
|
||||
messages:
|
||||
temperatureChange:
|
||||
description: Message that is being sent when the temperature in the bedroom changes.
|
||||
payload:
|
||||
$ref: '#/components/schemas/temperatureId'
|
||||
description: Updates the bedroom temperature in the database when the temperatures drops or goes up.
|
||||
|
||||
TemperatureMessage:
|
||||
$ref: '#/components/messages/TemperatureMessage'
|
||||
operations:
|
||||
temperatureChange:
|
||||
receiveTemperature:
|
||||
action: receive
|
||||
summary: Message sent to the broker when the temperature is changed.
|
||||
channel:
|
||||
$ref: '#/channels/temperatureChanged'
|
||||
|
||||
$ref: '#/channels/temperature/read'
|
||||
description: Receive temperature readings
|
||||
components:
|
||||
schemas:
|
||||
temperatureId:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
properties:
|
||||
temperatureId:
|
||||
type: string
|
||||
messages:
|
||||
TemperatureMessage:
|
||||
name: TemperatureMessage
|
||||
title: Temperature Message
|
||||
payload:
|
||||
type: object
|
||||
properties:
|
||||
value:
|
||||
type: number
|
||||
timestamp:
|
||||
type: string
|
||||
sensor_id:
|
||||
type: string
|
||||
@@ -0,0 +1,70 @@
|
||||
# .venv/bin/asyncapi-python-codegen asyncapi.yaml out --force-write
|
||||
# kouknout na FastStream
|
||||
# https://nats.io/blog/nats-supported-by-faststream/#writing-app-code
|
||||
# https://github.com/asyncapi/python-paho-template
|
||||
# https://www.asyncapi.com/docs/tools/generator/generator-template
|
||||
# https://www.asyncapi.com/docs/tools/generator/generator-template --force-write
|
||||
# https://pypi.org/project/asyncapi-python/
|
||||
# https://github.com/G-USI/asyncapi-python
|
||||
|
||||
|
||||
asyncapi: 3.0.0
|
||||
info:
|
||||
title: Job Events
|
||||
version: 1.0.0
|
||||
|
||||
servers:
|
||||
nats_localhost:
|
||||
host: localhost:4222
|
||||
protocol: nats
|
||||
|
||||
channels:
|
||||
run_test:
|
||||
address: mtr_test.run_test
|
||||
messages:
|
||||
run_test:
|
||||
$ref: '#/components/messages/mtr_request'
|
||||
return_uuid:
|
||||
address: null
|
||||
messages:
|
||||
run_test:
|
||||
$ref: '#/components/messages/mtr_uuid'
|
||||
|
||||
operations:
|
||||
get_mtr_test:
|
||||
action: send
|
||||
channel:
|
||||
$ref: '#/channels/run_test'
|
||||
reply:
|
||||
address:
|
||||
location: "$message.header#/replayTo"
|
||||
channel:
|
||||
$ref: '#/channels/return_uuid'
|
||||
|
||||
components:
|
||||
schemas:
|
||||
mtr_request_schema:
|
||||
payload:
|
||||
type: object
|
||||
properties:
|
||||
to_server:
|
||||
type: string
|
||||
mtr_sleep:
|
||||
type: integer
|
||||
mtr_uuid_schema:
|
||||
payload:
|
||||
type: object
|
||||
properties:
|
||||
uuid:
|
||||
type: string
|
||||
messages:
|
||||
mtr_request:
|
||||
name: mtr_request
|
||||
payload:
|
||||
type: object
|
||||
# required:
|
||||
# - to_server
|
||||
$ref: '#/components/schemas/mtr_request_schema'
|
||||
mtr_uuid:
|
||||
payload:
|
||||
$ref: '#/components/schemas/mtr_uuid_schema'
|
||||
@@ -0,0 +1,18 @@
|
||||
# 1
|
||||
import paho.mqtt.client as mqtt
|
||||
# 2
|
||||
mqttBroker = "test.mosquitto.org"
|
||||
|
||||
class TemperatureServiceClient:
|
||||
def __init__(self):
|
||||
# 3
|
||||
self.client = mqtt.Client()
|
||||
# 4
|
||||
self.client.connect(mqttBroker)
|
||||
|
||||
|
||||
def sendTemperatureChange(self, id):
|
||||
# 5
|
||||
topic = "temperature/changed"
|
||||
# 6
|
||||
self.client.publish(topic, id)
|
||||
@@ -0,0 +1,16 @@
|
||||
from client import TemperatureServiceClient
|
||||
from random import randrange
|
||||
import time
|
||||
|
||||
client = TemperatureServiceClient()
|
||||
|
||||
id_length = 8
|
||||
min_value = 10**(id_length-1) # Minimum value with 8 digits (e.g., 10000000)
|
||||
max_value = 10**id_length - 1 # Maximum value with 8 digits (e.g., 99999999)
|
||||
|
||||
while True:
|
||||
randomId = randrange(min_value, max_value + 1)
|
||||
client.sendTemperatureChange(randomId)
|
||||
print("New temperature detected " + str(randomId) + " sent to temperature/changed")
|
||||
time.sleep(1)
|
||||
|
||||
Reference in New Issue
Block a user