summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAshymad <czilukim@o2.pl>2018-05-05 18:41:20 +0200
committerAshymad <czilukim@o2.pl>2018-05-05 18:41:20 +0200
commit05961d53ec1564f976e337c4a83d0a76ca9444df (patch)
tree05650d7ca5692b75dcb55b59e959aa93243b132f
parent73d43cc2c017ffed44725574b4e8f0697a1c5aac (diff)
Receiving messages works.
-rwxr-xr-xmain.py22
-rwxr-xr-xsender.py2
-rw-r--r--static/src/index.js47
3 files changed, 47 insertions, 24 deletions
diff --git a/main.py b/main.py
index 880fa1c..a1eebb5 100755
--- a/main.py
+++ b/main.py
@@ -2,17 +2,13 @@
from flask import Flask, jsonify, request
import pika
+import uuid
app = Flask(__name__)
rmq_adr = '127.0.0.1'
rmq_exchange = 'raspi.live'
rmq_par = pika.ConnectionParameters(host=rmq_adr)
-connection = pika.BlockingConnection(rmq_par)
-channel = connection.channel()
-channel.exchange_declare(exchange=rmq_exchange,
- exchange_type='direct',
- durable=True)
@app.route('/api/binding/<int:binding_id>', methods=['PUT'])
def put_binding(binding_id):
@@ -21,22 +17,28 @@ def put_binding(binding_id):
if 'routing_key' not in request.json:
abort(400)
- result = channel.queue_declare(auto_delete=True,
+ connection = pika.BlockingConnection(rmq_par)
+ channel = connection.channel()
+ channel.exchange_declare(exchange=rmq_exchange,
+ exchange_type='direct',
+ durable=True)
+ queue_name = "gen_" + str(uuid.uuid4())
+ result = channel.queue_declare(queue_name,
+ auto_delete=True,
arguments={'x-expires': 1000*60*2})
- queue_name = result.method.queue
channel.queue_bind(exchange=rmq_exchange,
queue=queue_name,
routing_key=request.json['routing_key'])
+ connection.close()
return jsonify({
- 'exchange': rmq_exchange,
'queue_name': queue_name,
})
@app.route('/api/stomp')
def stomp_parameters():
return jsonify({
- 'login': 'guest',
- 'passcode': 'guest',
+ 'login': 'read',
+ 'passcode': 'only',
'url': 'ws://' + rmq_adr + ':15674/ws',
})
diff --git a/sender.py b/sender.py
index 08743d1..df1617c 100755
--- a/sender.py
+++ b/sender.py
@@ -29,7 +29,7 @@ with Connection('amqp://guest:guest@SKRADAK') as conn:
producer = Producer(channel, exchange=b_exch, routing_key=piid)
while loop:
- ranums = numpy.random.randint(-2**15, 2**15-1, size=(3, 800))
+ ranums = 4*(numpy.random.rand(3, 3)-0.5)
producer.publish(
{
'x': ranums[0,:].tolist(),
diff --git a/static/src/index.js b/static/src/index.js
index d6e7a69..65c31b5 100644
--- a/static/src/index.js
+++ b/static/src/index.js
@@ -6,24 +6,45 @@ import style from './index.css';
var beAPI = 'http://127.0.0.1:8888/api/';
var client;
+var subscription;
request
.get(beAPI + 'stomp')
- .end((err, res) => {
- if (err) {
- console.log(err)
- } else {
- client = Stomp.client(res.body.url)
- client.connect(res.body.login, res.body.passcode,
- () => {
- console.log("Connected via STOMP!")
- },
- (error) => {
- console.log(error)
- })
- }
+ .then((res) => {
+ client = Stomp.client(res.body.url)
+ client.connect(res.body.login, res.body.passcode,
+ on_connect,
+ (error) => {
+ console.log(error)
+ })
+ })
+ .catch((err) => {
+ console.log(err)
})
+var on_connect = function() {
+ request
+ .put(beAPI + 'binding/1234')
+ .send({'routing_key': 'pc.11373331246'})
+ .then((res) => {
+ subscription = client
+ .subscribe("/amq/queue/" + res.body.queue_name,
+ on_message)
+
+ })
+ .catch((err) => {
+ console.log(err)
+ })
+}
+
+var on_message = function(message) {
+ if (message.body) {
+ } else {
+ console.log("got empty message");
+ }
+}
+
+
var limit = 60 * 1,
duration = 750,
now = new Date(Date.now() - duration)