Skip to content

Commit

Permalink
Merge pull request #3 from pietjan12/feature/rabbitmq-startstop-msg
Browse files Browse the repository at this point in the history
Feature/rabbitmq startstop msg
  • Loading branch information
nicburgt authored Dec 7, 2020
2 parents aa1ed9a + 704e823 commit f120f2c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
Binary file modified back-end/database/dev-db.db
Binary file not shown.
2 changes: 1 addition & 1 deletion back-end/database/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ..init_db import Base


class CamType(enum.Enum):
class CamType(enum.IntEnum):
webcam = 1
ip_cam = 2
local_file = 3
Expand Down
2 changes: 1 addition & 1 deletion back-end/rabbitmq/rabbitMQ.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from kombu import Exchange, Queue

# Default RabbitMQ server URI
rabbit_url = 'amqp://rabbitmq:rabbitmq@localhost:5672//'
rabbit_url = 'amqp://rabbitmq:rabbitmq@192.168.99.100:5672//'

# Define queues that will be used in the future
feed_queue = Queue('feed-queue', Exchange("feed-exchange", type="direct"), routing_key='feed')
Expand Down
4 changes: 2 additions & 2 deletions back-end/resources/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def post(self):
# relationship
if detectiontypes is not None:
for item in detectiontypes:
dt = DetectionTypes(detectionType=item)
dt = DetectionTypes(detectiontype=item)
config.detections.append(dt)

# commit the changes
Expand Down Expand Up @@ -101,7 +101,7 @@ def put(self, config_ID):
configuration.detections.clear()
for item in detectiontypes:
dt = DetectionTypes(detectionType=item)
config.detections.append(dt)
configuration.detections.append(dt)

configuration.name = json_data['name']
configuration.resolution = json_data['resolution']
Expand Down
15 changes: 9 additions & 6 deletions back-end/resources/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
from flask_restful import Resource
from database.models.models import Feed
from database.init_db import db_session
from database.schemas.Schemas import FeedSchema
from database.schemas.Schemas import FeedSchema, DetectionTypesSchema
# rabbitMQ
from rabbitmq.rabbitMQ import rabbit_url, feed_queue
from kombu import Connection
import json


class VideoFeedListAPI(Resource):
Expand Down Expand Up @@ -94,6 +95,9 @@ def put(self, feed_ID):
if feed_ID is None:
return abort(400, description="missing required parameter")
else:
# get a scoped DB session
scoped_session = db_session()

feed = Feed.query.filter_by(id=feed_ID).first()
if feed is None:
abort(404, description=f"Feed {feed_ID} not found")
Expand All @@ -104,20 +108,19 @@ def put(self, feed_ID):
with Connection(rabbit_url, heartbeat=4) as conn:
# flip boolean
feed.active = not feed.active
# fetch schema for dumping database model class to json
dt_schema = DetectionTypesSchema()
# Produce a message to RabbitMQ so detection manager can consume and start the approriate feed
# with the given data.
producer = conn.Producer(serializer='json')
producer.publish(
# TODO: fix json serializer, for now we mock it.
#{feed.id, feed.feed_type, feed.url, feed.active},
{ 'id' : '1', 'feed_type': 'IP_CAM', 'url': 'http://test.com/file.mp4', 'active': 'true'},
{'id': feed.id, 'feed_type': json.dumps(feed.feed_type), 'url': feed.url, 'active': feed.active, 'detections': dt_schema.dump(feed.configuration.detections, many=True), 'drawables': feed.configuration.drawables},
retry=True,
exchange=feed_queue.exchange,
routing_key=feed_queue.routing_key,
declare=[feed_queue], # declares exchange, queue and binds.
)
# set feed to active, TODO : add active/inactive to the queue

db_session.commit()
scoped_session.commit()

conn.release()

0 comments on commit f120f2c

Please sign in to comment.