Initial commit
This commit is contained in:
commit
2d18417793
7 changed files with 218 additions and 0 deletions
BIN
backend/__pycache__/dbHelper.cpython-38.pyc
Normal file
BIN
backend/__pycache__/dbHelper.cpython-38.pyc
Normal file
Binary file not shown.
BIN
backend/__pycache__/mqttClient.cpython-38.pyc
Normal file
BIN
backend/__pycache__/mqttClient.cpython-38.pyc
Normal file
Binary file not shown.
96
backend/dbHelper.py
Normal file
96
backend/dbHelper.py
Normal file
|
@ -0,0 +1,96 @@
|
|||
import pymysql
|
||||
import json
|
||||
|
||||
class MaDbHelper:
|
||||
def __init__(self):
|
||||
self.mysqlConn = pymysql.connect(host='localhost',
|
||||
user='root',
|
||||
passwd='MlmAccess',
|
||||
db='MlmAccess',
|
||||
port=4306)
|
||||
|
||||
self.mysqlCur = self.mysqlConn.cursor()
|
||||
|
||||
def _initDb(self):
|
||||
"""
|
||||
Generates required tables and users
|
||||
"""
|
||||
|
||||
"""
|
||||
CREATE TABLE vmq_auth_acl
|
||||
(
|
||||
mountpoint VARCHAR(10) NOT NULL,
|
||||
client_id VARCHAR(128) NOT NULL,
|
||||
username VARCHAR(128) NOT NULL,
|
||||
password VARCHAR(128),
|
||||
publish_acl TEXT,
|
||||
subscribe_acl TEXT,
|
||||
CONSTRAINT vmq_auth_acl_primary_key PRIMARY KEY (mountpoint, client_id, username)
|
||||
)
|
||||
"""
|
||||
|
||||
def addUser(self, username, password, publishAclPatterns, subscribeAclPatterns):
|
||||
|
||||
if self.userExists(username):
|
||||
return False
|
||||
|
||||
query = "INSERT INTO `vmq_auth_acl` (`mountpoint`, `client_id`, `username`, `password`, `publish_acl`, `subscribe_acl`) VALUES (%s, %s, %s, PASSWORD(%s), %s, %s);"
|
||||
|
||||
self.mysqlCur.execute(
|
||||
query,
|
||||
(
|
||||
"",
|
||||
username,
|
||||
username,
|
||||
password,
|
||||
self._convertAclPatternList(publishAclPatterns),
|
||||
self._convertAclPatternList(subscribeAclPatterns)
|
||||
)
|
||||
)
|
||||
|
||||
self.mysqlConn.commit()
|
||||
|
||||
return True
|
||||
|
||||
def userExists(self, username):
|
||||
query = "SELECT username FROM `vmq_auth_acl` WHERE username=%s"
|
||||
|
||||
self.mysqlCur.execute(
|
||||
query,
|
||||
(username)
|
||||
)
|
||||
|
||||
result = self.mysqlCur.fetchone()
|
||||
return result is not None
|
||||
|
||||
|
||||
def updateUser(self, username, password, publishAclPatterns, subscribeAclPatterns):
|
||||
if self.userExists(username):
|
||||
return self._updateUser(username, password, publishAclPatterns, subscribeAclPatterns)
|
||||
else:
|
||||
return False
|
||||
|
||||
def _updateUser(self, username, password, publishAclPatterns, subscribeAclPatterns):
|
||||
|
||||
query = """
|
||||
UPDATE `vmq_auth_acl`
|
||||
SET `password`=PASSWORD(%s), `publish_acl`=%s, `subscribe_acl`=%s
|
||||
WHERE `username`=%s;
|
||||
"""
|
||||
|
||||
self.mysqlCur.execute(
|
||||
query,
|
||||
(
|
||||
password,
|
||||
self._convertAclPatternList(publishAclPatterns),
|
||||
self._convertAclPatternList(subscribeAclPatterns),
|
||||
username
|
||||
)
|
||||
)
|
||||
|
||||
self.mysqlConn.commit()
|
||||
|
||||
def _convertAclPatternList(self, patternList):
|
||||
patternMapLabda = lambda pattern: {"pattern": pattern}
|
||||
patternList = list(map(patternMapLabda, patternList))
|
||||
return json.dumps(patternList)
|
48
backend/mlmAccess.py
Normal file
48
backend/mlmAccess.py
Normal file
|
@ -0,0 +1,48 @@
|
|||
import paho.mqtt.client as mqtt
|
||||
from dbHelper import MaDbHelper
|
||||
|
||||
class MlmAccess:
|
||||
def __init__(self):
|
||||
self._initDb()
|
||||
self._initMqtt()
|
||||
|
||||
def _initDb(self):
|
||||
self.db = MaDbHelper()
|
||||
initUsers = [
|
||||
("backend", "backend", ["mlmAccess/#"], ["mlmAccess/#"]),
|
||||
("pair-actor", "pair-actor", ["mlmAccess/pair/request/actor"], ["mlmAccess/pair/response/actor"])
|
||||
]
|
||||
|
||||
for username, password, publishAclPatterns, subscribeAclPatterns in initUsers:
|
||||
if not self.db.addUser(username, password, publishAclPatterns, subscribeAclPatterns):
|
||||
self.db.updateUser(username, password, publishAclPatterns, subscribeAclPatterns)
|
||||
|
||||
def _initMqtt(self):
|
||||
self.mqtt = client = mqtt.Client(client_id="backend")
|
||||
self.mqtt.username_pw_set("backend", "backend")
|
||||
|
||||
self.mqtt.on_connect = self._mqttOnConnect
|
||||
self.mqtt.on_message = self._mqttOnMessage
|
||||
|
||||
print("connecting to broker")
|
||||
print(self.mqtt.connect("localhost", 1883, 60))
|
||||
|
||||
self.mqtt.loop_forever()
|
||||
|
||||
def _mqttOnConnect(self, client, userdata, flags, rc):
|
||||
print("Connected with result code "+str(rc))
|
||||
|
||||
if rc != 0:
|
||||
print("")
|
||||
|
||||
print(client.subscribe("mlmAccess/pair/request/actor"))
|
||||
|
||||
def _mqttOnMessage(self, client, userdata, message):
|
||||
print("message received " ,str(message.payload.decode("utf-8")))
|
||||
print("message topic=",message.topic)
|
||||
print("message qos=",message.qos)
|
||||
print("message retain flag=",message.retain)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
MlmAccess()
|
Loading…
Add table
Add a link
Reference in a new issue