Commit bdc3ac73 authored by p's avatar p
Browse files

added event for robbery alarm

parent 8938fcfd
import paho.mqtt.client as paho
from utils import mssql_connect
# import paho.mqtt.client as paho
from utils import mssql_connect, mqtt_connect
SQL_INSERT = 'INSERT INTO [data] (phrase) VALUES (%s)'
......@@ -7,13 +7,10 @@ mssql_client = mssql_connect()
cursor = mssql_client.cursor()
BROKER = 'openlab.kpi.fei.tuke.sk'
MQTT_USERNAME = 'openlab'
MQTT_PASSWORD = 'opanlab'
BROKER_PORT = 80
TRANSPORT = 'websockets'
VOICE_RECOGNITION_TOPIC = 'openlab/voice/recognition/recognized'
CLIENT_ID = 'TUKE-ATvI2-123987'
mqtt_client = None
def start_receive(on_message):
......@@ -22,14 +19,10 @@ def start_receive(on_message):
:param on_message: function which receive all messages.
"""
global mqtt_client
mqtt_client = paho.Client(CLIENT_ID, transport=TRANSPORT)
mqtt_client.on_message = on_message
print('connecting to broker', BROKER)
mqtt_client.connect(BROKER, port=BROKER_PORT)
mqtt_client.loop_start()
mqtt_client.subscribe(VOICE_RECOGNITION_TOPIC)
client = mqtt_connect(broker=BROKER, port=BROKER_PORT, id=CLIENT_ID, on_message=on_message, transport=TRANSPORT)
client.subscribe(VOICE_RECOGNITION_TOPIC)
print('sucessfully subscribed to', VOICE_RECOGNITION_TOPIC)
return client
def stop_receive():
......@@ -48,6 +41,6 @@ def on_message(client, userdata, message):
print(cursor.rowcount, "record inserted.")
start_receive(on_message)
mqtt_client = start_receive(on_message)
input('Press Enter to close...\n')
stop_receive()
......@@ -17,13 +17,15 @@ CREATE TABLE [bank_branch] (
lon VARCHAR(64) NOT NULL,
PRIMARY KEY (id));
INSERT INTO [bank_branch] (id, address, zip, city, lat, lon) VALUES ('1', 'Letná 9', '04001', 'Košice', '48.7304366', '21.2433415')
CREATE TABLE [robbery_event] (
id INT IDENTITY(1,1) NOT NULL,
id_bank_branch INT NOT NULL,
timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
rate INT NOT NULL,
audio_match INT NOT NULL,
video_match INT NOT NULL,
audio_match INT NULL,
video_match INT NULL,
audio_uri VARCHAR(128) NULL,
video_uri VARCHAR(128) NULL,
keywords VARCHAR(256) NULL,
......
import nltk
import os
import time
import json
import pandas as pd
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import TfidfVectorizer
from utils import mssql_connect
from utils import mssql_connect, mqtt_connect
nltk.data.path.append(os.path.join(os.getcwd(), 'nltk_data'))
......@@ -40,15 +41,29 @@ def predict(phrase):
return result
def alarm(update_values):
values = []
for phrase, rate, id in update_values:
if rate <= 2: continue
values.append((1, str(rate), stemmed_phrase(phrase)))
cursor.executemany(SQL_INSERT, values)
mssql_client.commit()
SQL_SELECT = 'SELECT TOP 100 * FROM [data] WHERE bank_robbery IS NULL ORDER BY timestamp'
SQL_UPDATE = 'UPDATE [data] SET bank_robbery = %s WHERE id = %s'
SQL_INSERT = 'INSERT INTO [robbery_event] (id_bank_branch, rate, keywords) VALUES (%s, %s, %s)'
# INSERT INTO [data] (phrase) VALUES (%s)
mssql_client = mssql_connect()
cursor = mssql_client.cursor(as_dict=True)
while True:
cursor.execute(SQL_SELECT)
update_values = [(str(predict(row['phrase'])), str(row['id'])) for row in cursor]
cursor.executemany(SQL_UPDATE, update_values)
update_values = [(row['phrase'], predict(row['phrase']), row['id']) for row in cursor]
cursor.executemany(SQL_UPDATE, [(str(rate), str(id)) for phrase, rate, id in update_values])
mssql_client.commit()
print(cursor.rowcount, 'record updated.')
alarm(update_values)
time.sleep(10)
import pymssql
import paho.mqtt.client as paho
def mssql_connect():
......@@ -7,3 +8,12 @@ def mssql_connect():
user='SA',
password='<YourStrong!Passw0rd>',
database='bank_robbery')
def mqtt_connect(broker, port, id, on_message, transport='websockets'):
mqtt_client = paho.Client(id, transport=transport)
mqtt_client.on_message = on_message
print('connecting to broker', broker)
mqtt_client.connect(broker, port=port)
mqtt_client.loop_start()
return mqtt_client
Supports Markdown
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