Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mqtt_ticket_redis
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
rakesh.pv
mqtt_ticket_redis
Commits
fd13b4c5
Commit
fd13b4c5
authored
Feb 14, 2023
by
rakesh.pv
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
publish part
parent
7d02d3f7
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
89 additions
and
75 deletions
+89
-75
main.py
+4
-73
scripts/config/mqtt_connection.py
+0
-0
scripts/core/handlers/check_availablity.py
+0
-0
scripts/core/handlers/fetch_available_seats_from_redis_db.py
+1
-1
scripts/core/services/main.py
+73
-0
scripts/database/redis/redis_connction.py
+0
-0
scripts/model/user_model.py
+10
-0
tessst.py
+1
-1
No files found.
main.py
View file @
fd13b4c5
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
uvicorn
if
__name__
==
'__main__'
:
uvicorn
.
run
(
"scripts.core.services.main:app"
,
reload
=
True
)
\ No newline at end of file
scripts/config/mqtt_connection.py
0 → 100644
View file @
fd13b4c5
check
.py
→
scripts/core/handlers/check_availablity
.py
View file @
fd13b4c5
File moved
redis_db.py
→
scripts/core/handlers/fetch_available_seats_from_
redis_db.py
View file @
fd13b4c5
import
pickle
from
redis_connction
import
conn
from
scripts.database.redis.
redis_connction
import
conn
def
redis_db_create
(
connection
,
ticket_class
,
seats
,
total_count
):
...
...
scripts/core/services/main.py
0 → 100644
View file @
fd13b4c5
import
paho.mqtt.client
as
mqtt
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
)
from
fastapi
import
FastAPI
,
HTTPException
from
scripts.core.handlers.check_availablity
import
seat_availability
,
check_seat_avail
from
scripts.database.redis.redis_connction
import
conn
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
if
x
==
"Silver"
:
aa
=
seat_availability
(
conn
,
"Silver"
)
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 that are already booked are : {booked_seats}
\n
"
f
"seat number you are going to book are {seat_numbers}"
)
booked_seats
.
pop
()
payload
=
{
class_type
:
seat_numbers
}
client
.
publish
(
"ticket"
,
str
(
payload
))
client
.
subscribe
(
"ticket/#"
)
return
{
"message"
:
"Ticket booking successful"
}
redis_connction.py
→
scripts/database/redis/
redis_connction.py
View file @
fd13b4c5
File moved
scripts/model/user_model.py
0 → 100644
View file @
fd13b4c5
from
pydantic
import
BaseModel
class
user
(
BaseModel
):
class_type
:
str
number_of_tickets
:
int
seat_numbers
:
list
[
int
]
user_name
:
str
user_age
:
int
user_phone
:
int
tessst.py
View file @
fd13b4c5
import
pickle
from
redis_connction
import
conn
from
scripts.database.redis.
redis_connction
import
conn
g
=
pickle
.
loads
(
conn
.
get
(
"Gold"
))
g
[
0
][
1
]
=
0
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment