Compare commits
25 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 12be29340b | |||
| 9d01ead120 | |||
| 5bac4c720b | |||
| cdf195306e | |||
| 90f1a6245c | |||
| 532266c98e | |||
| f99c0e8148 | |||
| 2a7c74286c | |||
| 920c51b4f0 | |||
| 2b7fecb248 | |||
| ce48859079 | |||
| f93b94531c | |||
| 22da586aec | |||
| 5058df8b7d | |||
| 3a118c51b5 | |||
| 7ccc9385e6 | |||
| 1bf9c69c2b | |||
| 2098719c82 | |||
| 0e668e1993 | |||
| da05ae96d2 | |||
| 061b7a86a3 | |||
| eabc193d8b | |||
| 35f18853fc | |||
| a062d24b24 | |||
| a87656274b |
+28
@@ -0,0 +1,28 @@
|
|||||||
|
from flask import Blueprint, request, jsonify
|
||||||
|
from model.user import User
|
||||||
|
|
||||||
|
auth = Blueprint("auth", __name__)
|
||||||
|
|
||||||
|
|
||||||
|
@auth.route('/login', methods = ['POST'])
|
||||||
|
def login():
|
||||||
|
if request.is_json:
|
||||||
|
req = request.json
|
||||||
|
|
||||||
|
email = req.get('email')
|
||||||
|
password = req.get('password')
|
||||||
|
|
||||||
|
if not email or not password:
|
||||||
|
return "Request must have email and password", 400
|
||||||
|
|
||||||
|
if len(email.strip()) < 4 or '@' not in email or '.' not in email:
|
||||||
|
return "Email is incorrect", 400
|
||||||
|
|
||||||
|
if len(password.strip()) < 8:
|
||||||
|
return "Password is too short", 400
|
||||||
|
|
||||||
|
user = User(email, password)
|
||||||
|
return jsonify(user.toJson())
|
||||||
|
|
||||||
|
else:
|
||||||
|
return "Request is not a json", 400
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
from flask import Blueprint, request, jsonify
|
||||||
|
from model.user import User
|
||||||
|
#from module.db.repositories.robot_reposytory import update_robot get_by_id
|
||||||
|
from loguru import logger as log
|
||||||
|
robots = Blueprint("robots", __name__)
|
||||||
|
|
||||||
|
robots.route('/data', methods = ['POST'])
|
||||||
|
def data():
|
||||||
|
if request.headers.get("Authorization"):
|
||||||
|
if request.is_json:
|
||||||
|
req = request.json
|
||||||
|
|
||||||
|
id = req.get('robot_id')
|
||||||
|
tms = req.get('timestamp')
|
||||||
|
loc = req.get('location')
|
||||||
|
scanRes = req.get('scan_results')
|
||||||
|
battery = req.get('battery_level')
|
||||||
|
|
||||||
|
if update_robot(id, "received", battery, loc["zone"], loc["row"], loc["self"]):
|
||||||
|
res = {"status": "received", "message_id": "123"} #for that moment i don`t now what is message
|
||||||
|
return jsonify(res)
|
||||||
|
else:
|
||||||
|
log.error('failed to update robot data')
|
||||||
|
return("Server error")
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
from app import app
|
||||||
|
from flask_socketio import SocketIO, emit
|
||||||
|
from loguru import logger as log
|
||||||
|
#from module.db.repositories.robot_reposytory import update_robot get_by_id get_all
|
||||||
|
#from module.db.repositories.inventory_repository import get_all as getRecords
|
||||||
|
ws = socketIO(app)
|
||||||
|
|
||||||
|
@ws.on('connect')
|
||||||
|
def is_connect():
|
||||||
|
log.info('client is connected')
|
||||||
|
|
||||||
|
@ws.on('disconnect')
|
||||||
|
def is_disconnect():
|
||||||
|
log.info('client is disconnected')
|
||||||
|
|
||||||
|
@ws.on('robot_update')
|
||||||
|
def robotUpdate():
|
||||||
|
robots = get_all()
|
||||||
|
emit('response', jsonify({"type": "robot_update", "data": {robots.toJSON()}})
|
||||||
|
|
||||||
|
@ws.on('inventory_alert')
|
||||||
|
def robotUpdate():
|
||||||
|
records = getRecords()
|
||||||
|
emit('response', jsonify({"type": "inventory_alert", "data": {records.toJSON()}})
|
||||||
|
|
||||||
|
|
||||||
@@ -1,4 +1,15 @@
|
|||||||
|
from sys import exit
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
from api.auth import auth
|
||||||
|
from utils.loadDotEnv import initializeENV
|
||||||
|
from utils.PostgressConnect import PSQLConnect, PSQLCursor
|
||||||
|
|
||||||
|
if not initializeENV():
|
||||||
|
exit(-1)
|
||||||
|
|
||||||
|
#conn = PSQLConnect()
|
||||||
|
#cur = PSQLCursor(conn)
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
app.register_blueprint(auth, url_prefix='/api/auth')
|
||||||
|
|||||||
@@ -1 +1,6 @@
|
|||||||
flask==3.1.2
|
flask==3.1.2
|
||||||
|
flask-socketio
|
||||||
|
python-dotenv
|
||||||
|
psycopg2-binary
|
||||||
|
pyjwt
|
||||||
|
loguru
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
import json
|
||||||
|
from utils.token import generateKey
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class User:
|
||||||
|
id: int
|
||||||
|
name: str
|
||||||
|
role: str
|
||||||
|
token: str
|
||||||
|
|
||||||
|
def __init__(self, email: str, passwd: str):
|
||||||
|
#us = getUsModel() #возвращает словарь
|
||||||
|
self.id = 1#us['id']
|
||||||
|
self.name = 'Bob'#us['name']
|
||||||
|
self.role = 'Backend'#us['role']
|
||||||
|
self.token = generateKey(email, passwd)
|
||||||
|
|
||||||
|
def toJson(self):
|
||||||
|
return {"user": {"id": self.id, "name": self.name, "role": self.role}, "token": self.token}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import psycopg2
|
||||||
|
import os
|
||||||
|
|
||||||
|
def PSQLConnect():
|
||||||
|
conn = psycopg2.connect(os.getenv('POSTGRES_URL'))
|
||||||
|
return conn
|
||||||
|
|
||||||
|
def PSQLCursor(conn):
|
||||||
|
cur = conn.cursor()
|
||||||
|
return cur
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import os
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from loguru import logger as log
|
||||||
|
|
||||||
|
DOTENV_PATH = '.env'
|
||||||
|
|
||||||
|
def initializeENV() -> bool:
|
||||||
|
if os.path.exists(DOTENV_PATH):
|
||||||
|
load_dotenv(DOTENV_PATH)
|
||||||
|
log.info('.env is loaded')
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
log.error('.env isn`t loaded')
|
||||||
|
return False
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import jwt
|
||||||
|
import os
|
||||||
|
from time import time
|
||||||
|
|
||||||
|
def generateKey(email, passwd):
|
||||||
|
key = os.getenv('KEY')
|
||||||
|
encoded = jwt.encode({email: passwd, 'iat': time()}, key, algorithm="HS256")
|
||||||
|
return encoded
|
||||||
Reference in New Issue
Block a user