add example code generator

This commit is contained in:
marys
2026-06-03 10:36:11 +02:00
parent b4ad1d2fd7
commit 03b5f3c2ea
17 changed files with 308 additions and 56 deletions
+3 -3
View File
@@ -1,11 +1,11 @@
{
"name": "python-mqtt-client-template",
"version": "0.0.1",
"description": "A template that generates a Python FastStream NATS client.",
"description": "A template that generates a Python MQTT client using MQTT.",
"generator": {
"apiVersion": "v1",
"apiVersion": "v3",
"generator": ">=2.0.0 <4.0.0",
"supportedProtocols": ["faststream", "nats"]
"supportedProtocols": ["mqtt"]
},
"dependencies": {
"@asyncapi/generator-react-sdk": "^0.2.25"
+30 -6
View File
@@ -1,7 +1,31 @@
//1
import { File } from '@asyncapi/generator-react-sdk'
//2
// //1
// import { File } from '@asyncapi/generator-react-sdk'
// //2
// export default function ({ asyncapi }) {
// //3
// return <File name="client.py">{asyncapi.info().title()}</File>
// }
import { File } from '@asyncapi/generator-react-sdk';
export default function ({ asyncapi }) {
//3
return <File name="client.py">{asyncapi.info().title()}</File>
}
return (
<File name="client.py">
{`import paho.mqtt.client as mqtt
mqttBroker = "test.mosquitto.org"
class TemperatureServiceClient:
def __init__(self):
self.client = mqtt.Client()
self.client.connect(mqttBroker)
def sendTemperatureChange(self, id):
topic = "temperature/changed"
self.client.publish(topic, id)`}
</File>
)
}
+29
View File
@@ -0,0 +1,29 @@
// //1
// import { File } from '@asyncapi/generator-react-sdk'
// //2
// export default function ({ asyncapi }) {
// //3
// return <File name="client.py">{asyncapi.info().title()}</File>
// }
import { File } from '@asyncapi/generator-react-sdk';
export default function ({ asyncapi, params }) {
const channels = Object.keys(asyncapi.components());
let content = ''
for (const channel of channels) {
content += `# channel: ${channel}\n`
}
return (
<File name="client2.py"># {asyncapi.info().title()}
{`\n`}
# {asyncapi.info().description()}
{`\n`}
{channels.map((ch) => `# channel: ${ch}\n`)}
{content}
</File>
)
}
+24 -33
View File
@@ -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
+70
View File
@@ -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'
+18
View File
@@ -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)
+16
View File
@@ -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)