Commit 7d02d3f7 by rakesh.pv

publishing:

checking from redis done
redis updation has to integrate here
parents
# Default ignored files
/shelf/
/workspace.xml
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N802" />
<option value="N806" />
</list>
</option>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (mqtt_ticket_publish_part)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/mqtt_ticket_publish_part.iml" filepath="$PROJECT_DIR$/.idea/mqtt_ticket_publish_part.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
import pickle
def seat_availability(connection, ticket_class):
try:
if ticket_class == "Gold":
class_list = pickle.loads(connection.get("Gold"))
elif ticket_class == "Silver":
class_list = pickle.loads(connection.get("Silver"))
else:
return None
data = {}
for i, row in enumerate(class_list):
data["Row {}".format(i + 1)] = ", ".join(map(str, row))
return data
except Exception as e:
print(e)
def check_seat_avail(connection, ticket_class, seats_needed):
class_lists = {
"gold": pickle.loads(connection.get("Gold")),
"silver": pickle.loads(connection.get("Silver"))
}
print(seats_needed)
if ticket_class not in class_lists:
return None
flag = 0
for seats in class_lists[ticket_class]:
if seats_needed in seats:
flag+=1
return True
if flag == 0:
return False
\ No newline at end of file
from fastapi import FastAPI, Header, HTTPException
import paho.mqtt.client as mqtt
from fastapi import FastAPI, HTTPException
from check import seat_availability, check_seat_avail
from redis_connction import conn
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.get("/class_availabilty")
async def check_seat():
x = input(" enter classs gold or silver")
if x == "Gold":
aa = seat_availability(conn, "Gold")
return aa
@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 not in booked_seats:
print("""ddsd""")
aa = check_seat_avail(conn, class_type, seat_number)
print("result is ", aa)
if not aa:
booked_seats.append(seat_number)
print(booked_seats)
if booked_seats:
raise HTTPException(status_code=400, detail=f"Seat number {booked_seats} is already booked")
booked_seats.pop()
# payload = {class_type: [seat_numbers]}
# client.publish("ticket", str(payload))
# client.subscribe("ticket/#")
return {"message": "Ticket booking successful"}
import redis
conn=redis.Redis('127.0.0.1',db=0)
\ No newline at end of file
import pickle
from redis_connction import conn
def redis_db_create(connection, ticket_class, seats, total_count):
try:
if ticket_class == "Gold":
connection.set("gold_available_seats", total_count)
elif ticket_class == "Silver":
connection.set("silver_available_seats", total_count)
else:
return False
list_ = []
for row in range(1, total_count, seats):
list_.append([seat for seat in range(row, row + seats)])
return True if connection.set(ticket_class, pickle.dumps(list_)) else False
except Exception as e:
print(e)
redis_db_create(conn, "Gold", 30, 300)
redis_db_create(conn, "Silver", 25, 125)
\ No newline at end of file
import pickle
from redis_connction import conn
g = pickle.loads(conn.get("Gold"))
g[0][1] = 0
conn.set("Gold", pickle.dumps(g))
\ No newline at end of file
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