Commit 0634fbe6 by rakesh.pv

Add new file

parents
from fastapi import FastAPI, Header, HTTPException
import paho.mqtt.client as mqtt
from fastapi import FastAPI, HTTPException
app = FastAPI()
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe("ticket_booking")
def on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("192.168.0.220", 1883, 60)
app = FastAPI()
booked_seats = []
@app.post("/book_ticket")
async def book_ticket(
class_type: str,
number_of_tickets: int,
seat_numbers: list[int],
user_name: str,
user_age: int,
user_phone: int,
):
if class_type not in ["gold", "silver"]:
raise HTTPException(status_code=400, detail="Invalid class type")
if len(seat_numbers) != number_of_tickets:
raise HTTPException(status_code=400, detail="Number of seat numbers must match the number of tickets")
for seat_number in seat_numbers:
if seat_number in booked_seats:
raise HTTPException(status_code=400, detail=f"Seat number {seat_number} is already booked")
booked_seats.append(seat_number)
payload = f"{class_type} class ticket : booking: {number_of_tickets} tickets by {user_name} for seat numbers {seat_numbers} having telephone number {user_phone} whose age is {user_age}",
gold_payload = {class_type: seat_numbers}
silver_payload = {class_type: seat_numbers}
client.publish("ticket", str(payload))
client.subscribe("ticket/#")
if class_type == "silver":
client.publish("silver_ticket_booking", str(gold_payload))
client.subscribe("silver_ticket_booking")
elif class_type == "gold":
client.publish("gold_ticket_booking", str(silver_payload))
client.subscribe("silver_ticket_booking")
return {"message": "Ticket booking successful"}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment