|
@@ -14,13 +14,10 @@ import json
|
|
|
import sys
|
|
|
|
|
|
from sqlalchemy.sql.elements import False_
|
|
|
-
|
|
|
# time
|
|
|
# import datetime
|
|
|
from datetime import timedelta, datetime
|
|
|
# db
|
|
|
-import pymysql
|
|
|
-from pymysql import cursors
|
|
|
import dataset
|
|
|
from passlib import context
|
|
|
import models
|
|
@@ -33,7 +30,7 @@ from fastapi_jwt_auth import AuthJWT
|
|
|
from fastapi_jwt_auth.exceptions import AuthJWTException
|
|
|
from fastapi.security import OAuth2AuthorizationCodeBearer, OAuth2PasswordRequestForm
|
|
|
import numpy as np
|
|
|
-import mysql.connector
|
|
|
+import pymysql
|
|
|
pymysql.install_as_MySQLdb()
|
|
|
db_settings = {
|
|
|
"host": "db.ptt.cx",
|
|
@@ -85,7 +82,7 @@ async def index(request: Request):
|
|
|
|
|
|
@app.get('/login', response_class=HTMLResponse)
|
|
|
async def login(request: Request):
|
|
|
- return templates.TemplateResponse(name='login_test.html', context={'request': request})
|
|
|
+ return templates.TemplateResponse(name='login.html', context={'request': request})
|
|
|
|
|
|
|
|
|
@app.post("/login")
|
|
@@ -151,8 +148,36 @@ async def tower(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
return RedirectResponse('/login')
|
|
|
- # current_user = Authorize.get_jwt_subject()
|
|
|
- return templates.TemplateResponse(name='tower.html', context={'request': request})
|
|
|
+ current_user = Authorize.get_jwt_subject()
|
|
|
+ result = get_user_under_organization(current_user)
|
|
|
+ result.append({'Data' : get_tower_info('dev001')})
|
|
|
+ return templates.TemplateResponse(name='tower.html', context={"request":request})
|
|
|
+
|
|
|
+@app.get('/tower/org', response_class=HTMLResponse)
|
|
|
+async def tower(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
+ try:
|
|
|
+ Authorize.jwt_required()
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return RedirectResponse('/login')
|
|
|
+ current_user = Authorize.get_jwt_subject()
|
|
|
+ result = get_user_under_organization(current_user)
|
|
|
+ return json.dumps(result,ensure_ascii=False)
|
|
|
+
|
|
|
+@app.get('/tower/', response_class=HTMLResponse)
|
|
|
+async def tower(request: Request,company:str,factory:str,department:str,towerGroup:str, Authorize: AuthJWT = Depends()):
|
|
|
+ try:
|
|
|
+ Authorize.jwt_required()
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return RedirectResponse('/login')
|
|
|
+ #current_user = Authorize.get_jwt_subject()
|
|
|
+ tower_arr = get_tower(company,factory,department,towerGroup)
|
|
|
+ result = []
|
|
|
+ for tower in tower_arr:
|
|
|
+ result.append({'tower_name': tower,'tower_data': get_tower_info(tower)})
|
|
|
+ print(result)
|
|
|
+ return json.dumps(result,ensure_ascii=False)
|
|
|
|
|
|
|
|
|
@app.get('/optim', response_class=HTMLResponse)
|
|
@@ -165,7 +190,6 @@ async def optim(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
return RedirectResponse('/login')
|
|
|
- # current_user = Authorize.get_jwt_subject()
|
|
|
return templates.TemplateResponse(name='optim.html',context={'request': request,"temp":temp})
|
|
|
|
|
|
|
|
@@ -222,14 +246,13 @@ async def get_member(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
return RedirectResponse('/login')
|
|
|
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/Water_tower?charset=utf8mb4')
|
|
|
- statement = 'SELECT id,username,isAdmin,roleType FROM users'
|
|
|
+ statement = 'SELECT id,username,isAdmin FROM users'
|
|
|
json_dic = {}
|
|
|
for row in db.query(statement):
|
|
|
#print(row['id'],row['username'])
|
|
|
- json_dic[row['username']] = {'isAdmin':row['isAdmin'],'roleType':row['roleType']}
|
|
|
+ json_dic[row['username']] = {'isAdmin':row['isAdmin'],'roleType':check_role_acl(row['username'])}
|
|
|
result = json.dumps(json_dic,ensure_ascii=False)
|
|
|
current_user = Authorize.get_jwt_subject()
|
|
|
- print(current_user)
|
|
|
return result
|
|
|
|
|
|
@app.get('/member/edit', response_class=HTMLResponse)
|
|
@@ -284,9 +307,8 @@ async def delete_member(request: Request):
|
|
|
return {'msg': ' 成功刪除'}
|
|
|
|
|
|
@app.get('/member_authority/{edit_one}', response_class=HTMLResponse)
|
|
|
-async def member_authority(request:Request,edit_one: str,Authorize: AuthJWT = Depends()):
|
|
|
+async def member_authority(request:Request,edit_one: int,Authorize: AuthJWT = Depends()):
|
|
|
"""設定成員權限"""
|
|
|
-
|
|
|
try:
|
|
|
Authorize.jwt_required()
|
|
|
except Exception as e:
|
|
@@ -297,30 +319,28 @@ async def member_authority(request:Request,edit_one: str,Authorize: AuthJWT = De
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/Water_tower?charset=utf8mb4')
|
|
|
statement = check_isAdmin(current_user)
|
|
|
if statement == "no user":
|
|
|
- return {'msg':statement }
|
|
|
+ return templates.TemplateResponse(name='notice.html', context={"request":request,'msg':'no user' })
|
|
|
elif statement == 0:
|
|
|
- return {'msg':'你沒有權限' }
|
|
|
+ return templates.TemplateResponse(name='notice.html', context={"request":request,'msg':"沒有權限" })
|
|
|
current_user_roleType = check_role_type(current_user)
|
|
|
-
|
|
|
- edit_one_roleType = check_role_type(edit_one)
|
|
|
- if edit_one_roleType == None:
|
|
|
- return {'msg':'不存在使用者'}
|
|
|
- elif current_user_roleType>edit_one_roleType or current_user_roleType==edit_one_roleType:
|
|
|
- return {'msg': ' 你沒有權限'}
|
|
|
+
|
|
|
+ if edit_one == None:
|
|
|
+ return templates.TemplateResponse(name='notice.html', context={'"request":request,msg':'no role' })
|
|
|
+ elif int(current_user_roleType)>int(edit_one) or int(current_user_roleType)==int(edit_one):
|
|
|
+ return templates.TemplateResponse(name='notice.html', context={"request":request,'msg':"沒有權限" })
|
|
|
|
|
|
result = check_role_acl(edit_one)
|
|
|
|
|
|
if result == []:
|
|
|
cmd = 'SELECT id FROM module'
|
|
|
for row in db.query(cmd):
|
|
|
- dic_tmp = {'id':get_user_id(edit_one),'isView':0,'isAdd':0 ,'isEdit':0,'isDel':0,'role_id' : check_role_type(edit_one)}
|
|
|
+ dic_tmp = {'id':0,'isView':0,'isAdd':0 ,'isEdit':0,'isDel':0,'role_id' : edit_one}
|
|
|
context[get_modul_name(row['id']) ] = dic_tmp
|
|
|
else:
|
|
|
for dic in result:
|
|
|
modul_name = get_modul_name(dic['module_id'])
|
|
|
del dic['module_id']
|
|
|
context[modul_name ] = dic
|
|
|
- print(context)
|
|
|
return templates.TemplateResponse(name='member_authority_test.html', context=context)
|
|
|
|
|
|
@app.post('/member_authority')
|
|
@@ -334,35 +354,35 @@ async def member_authority(request: Request):
|
|
|
if compare_jwt_token(row['token'],edit_one.access_token):
|
|
|
current_user = row['username']
|
|
|
if current_user == '':
|
|
|
- return {'msg':'尚未登入'}
|
|
|
+ return templates.TemplateResponse(name='notice.html', context={"request":request,'msg':'尚未登入'})
|
|
|
|
|
|
statement = check_isAdmin(current_user)
|
|
|
if statement == "no user":
|
|
|
- return {'msg':statement }
|
|
|
+ return templates.TemplateResponse(name='notice.html', context={"request":request,'msg':statement })
|
|
|
elif statement == 0:
|
|
|
- return {'msg':'你沒有權限' }
|
|
|
+ return templates.TemplateResponse(name='notice.html', context={"request":request,'msg':'你沒有權限' })
|
|
|
|
|
|
current_user_roleType = check_role_type(current_user)
|
|
|
edit_one_roleType = edit_one.role_id
|
|
|
|
|
|
- if edit_one.id == None:
|
|
|
- return {'msg':'不存在使用者'}
|
|
|
- elif current_user_roleType>edit_one_roleType or current_user_roleType==edit_one_roleType:
|
|
|
- return {'msg': ' 你沒有權限'}
|
|
|
+
|
|
|
+ if current_user_roleType>edit_one_roleType or current_user_roleType==edit_one_roleType:
|
|
|
+ return templates.TemplateResponse(name='notice.html', context={"request":request,'msg': ' 你沒有權限'})
|
|
|
else :
|
|
|
row = ['ai_prediction' ,'channel' ,'device', 'event', 'index' ,'performance', 'record', 'setting_device' ,'setting_system','tower']
|
|
|
- if check_role_acl(get_user_name(edit_one.id)) == []:
|
|
|
+ if check_role_acl(edit_one.role_id) == []:
|
|
|
for module in row :
|
|
|
new_dict = edit_one.get_acl_from_module_name(module)
|
|
|
+ new_dict["id"]= pymysql.NULL
|
|
|
table = db['role_acl']
|
|
|
table.insert(new_dict)
|
|
|
else:
|
|
|
for module in row :
|
|
|
new_dict = edit_one.get_acl_from_module_name(module)
|
|
|
table = db['role_acl']
|
|
|
- table.update(new_dict, ['id'],['module_id'])
|
|
|
+ table.update(new_dict, ['id'])
|
|
|
|
|
|
- return {'msg': ' 成功更改'}
|
|
|
+ return templates.TemplateResponse(name='notice.html', context={"request":request,'msg': '成功更改權限'})
|
|
|
|
|
|
|
|
|
|
|
@@ -584,20 +604,20 @@ def check_isAdmin(user_name:str):
|
|
|
return "no user"
|
|
|
return isAdmin
|
|
|
|
|
|
-def check_role_type(user_name:str):
|
|
|
+def check_role_type(user_name:str)->int:
|
|
|
"""查看使用者權限"""
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/Water_tower?charset=utf8mb4')
|
|
|
- cmd = 'SELECT role.id FROM `users` JOIN `role` ON `users`.roleType = `role`.name where `users`.username = "'+user_name+'"'
|
|
|
+ cmd = 'SELECT user_role.role_id FROM `users` JOIN `user_role` ON `users`.id = `user_role`.user_id where `users`.username = "'+user_name+'"'
|
|
|
role_type = None
|
|
|
for row in db.query(cmd) :
|
|
|
- role_type = row['id']
|
|
|
+ role_type = row['role_id']
|
|
|
return role_type
|
|
|
|
|
|
|
|
|
-def check_role_acl(user_name:str):
|
|
|
+def check_role_acl(role:int):
|
|
|
"""查看權限"""
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/Water_tower?charset=utf8mb4')
|
|
|
- cmd = 'SELECT role_acl.* FROM `users` JOIN `role_acl` ON `users`.id = `role_acl`.user_id where `users`.username = "'+user_name+'"'
|
|
|
+ cmd = 'SELECT * FROM role_acl where role_id = '+str(role)
|
|
|
|
|
|
result = []
|
|
|
for row in db.query(cmd) :
|
|
@@ -608,6 +628,100 @@ def check_role_acl(user_name:str):
|
|
|
result.append(dic)
|
|
|
return result
|
|
|
|
|
|
+def get_user_under_organization(user_name:str):
|
|
|
+ """查看所屬公司"""
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/Water_tower?charset=utf8mb4')
|
|
|
+ user_role = check_role_type(user_name)
|
|
|
+ #print(user_name,user_role)
|
|
|
+ cmd = 'SELECT * FROM organization'
|
|
|
+ result = []
|
|
|
+ if int(user_role) == 1 :
|
|
|
+ num=0
|
|
|
+ for row in db.query(cmd) :
|
|
|
+ company = row['Company']
|
|
|
+ factory = row['Factory']
|
|
|
+ department = row['Department']
|
|
|
+ result.append({})
|
|
|
+ cmd2 = 'SELECT TowerGroupCode FROM device WHERE CompanyCode = "' + company + '" AND FactoryCode = "' + factory + '" AND DepartmentCode = "' + department + '"'
|
|
|
+ group = []
|
|
|
+ for row2 in db.query(cmd2):
|
|
|
+ if row2['TowerGroupCode'] not in group :
|
|
|
+ group.append(row2['TowerGroupCode'])
|
|
|
+ result[num] = {'company':company,'factory':factory,'department':department,'group':group,'able':1}
|
|
|
+ num = num +1
|
|
|
+
|
|
|
+
|
|
|
+ elif int(user_role) == 2:
|
|
|
+ cmd2 = 'SELECT company FROM user WHERE user.username = '+user_name
|
|
|
+ company_able:str
|
|
|
+ num = 0
|
|
|
+ for row in db.query(cmd2) :
|
|
|
+ company_able = row['company']
|
|
|
+ for row in db.query(cmd) :
|
|
|
+ company = row['Company']
|
|
|
+ factory = row['Factory']
|
|
|
+ department = row['Department']
|
|
|
+
|
|
|
+ cmd3 = 'SELECT TowerGroupCode FROM device WHERE CompanyCode = "' + company + '" AND FactoryCode = "' + factory + '" AND DepartmentCode = "' + department + '"'
|
|
|
+ group = []
|
|
|
+ for row2 in db.query(cmd3):
|
|
|
+ group.append(row2['TowerGroupCode'])
|
|
|
+ if company == company_able:
|
|
|
+ result[num] = {'company':company,'factory':factory,'department':department,'group':group,'able':1}
|
|
|
+ else:
|
|
|
+ result[num] = {'company':company,'factory':factory,'department':department,'group':group,'able':0}
|
|
|
+ num = num +1
|
|
|
+
|
|
|
+ elif int(user_role) == 3:
|
|
|
+ cmd2 = 'SELECT company,factory FROM users WHERE users.username = '+user_name
|
|
|
+ company_able:str
|
|
|
+ factory_able:str
|
|
|
+ num = 0
|
|
|
+ for row in db.query(cmd2) :
|
|
|
+ company = row['company']
|
|
|
+ factory = row['factory']
|
|
|
+ for row in db.query(cmd) :
|
|
|
+ company = row['Company']
|
|
|
+ factory = row['Factory']
|
|
|
+ department = row['Department']
|
|
|
+
|
|
|
+ cmd3 = 'SELECT TowerGroupCode FROM device WHERE CompanyCode = "' + company + '" AND FactoryCode = "' + factory + '" AND DepartmentCode = "' + department + '"'
|
|
|
+ group = []
|
|
|
+ for row2 in db.query(cmd3):
|
|
|
+ group.append(row2['TowerGroupCode'])
|
|
|
+ if company == company_able and factory==factory_able:
|
|
|
+ result[num] = {'company':company,'factory':factory,'department':department,'group':group,'able':1}
|
|
|
+ else:
|
|
|
+ result[num] = {'company':company,'factory':factory,'department':department,'group':group,'able':0}
|
|
|
+ num=num+1
|
|
|
+ elif int(user_role) == 4:
|
|
|
+ cmd2 = 'SELECT company,factory,department FROM users WHERE username = '+user_name
|
|
|
+ company_able:str
|
|
|
+ factory_able:str
|
|
|
+ department_able:str
|
|
|
+ num = 0
|
|
|
+ for row in db.query(cmd2) :
|
|
|
+ company_able = row['company']
|
|
|
+ factory_able = row['factory']
|
|
|
+ department_able = row['Department']
|
|
|
+ for row in db.query(cmd) :
|
|
|
+ company = row['Company']
|
|
|
+ factory = row['Factory']
|
|
|
+ department = row['Department']
|
|
|
+
|
|
|
+ cmd3 = 'SELECT TowerGroupCode FROM device WHERE CompanyCode = "' + company + '" AND FactoryCode = "' + factory + '" AND DepartmentCode = "' + department + '"'
|
|
|
+ group = []
|
|
|
+ for row2 in db.query(cmd3):
|
|
|
+ group.append(row2['TowerGroupCode'])
|
|
|
+ if company == company_able and factory==factory_able and department==department_able:
|
|
|
+ result[num] = {'company':company,'factory':factory,'department':department,'group':group,'able':1}
|
|
|
+ else:
|
|
|
+ result[num] = {'company':company,'factory':factory,'department':department,'group':group,'able':1}
|
|
|
+ num = num +1
|
|
|
+ else :
|
|
|
+ result =[ {'msg':"error"}]
|
|
|
+ return result
|
|
|
+
|
|
|
def get_user_id(user_name:str):
|
|
|
"""獲取user id"""
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/Water_tower?charset=utf8mb4')
|
|
@@ -618,7 +732,7 @@ def get_user_id(user_name:str):
|
|
|
return id
|
|
|
|
|
|
def get_user_name(user_id:int):
|
|
|
- """獲取user id"""
|
|
|
+ """獲取user name"""
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/Water_tower?charset=utf8mb4')
|
|
|
cmd = 'SELECT username FROM `users` where id = "'+user_id+'"'
|
|
|
id = None
|
|
@@ -634,4 +748,30 @@ def get_modul_name(modul_id:str):
|
|
|
for row in db.query(cmd) :
|
|
|
modul_name = row['moduleName']
|
|
|
return modul_name
|
|
|
-
|
|
|
+
|
|
|
+def get_tower_info(tower_id:str):
|
|
|
+ """獲取水塔資料"""
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/Water_tower?charset=utf8mb4')
|
|
|
+ cmd = 'SELECT * FROM `record_dcs` where device_id = "'+tower_id+'"'
|
|
|
+ result ={'DCS':{},'Fan':{},'Moter':{}}
|
|
|
+ for row in db.query(cmd) :
|
|
|
+ result['DCS'][row['key']]=row['value']
|
|
|
+
|
|
|
+ cmd = 'SELECT * FROM `record_tower` where device_id = "'+tower_id+'"'
|
|
|
+ for row in db.query(cmd) :
|
|
|
+ result['Fan'][row['key']]=row['value']
|
|
|
+
|
|
|
+ cmd = 'SELECT * FROM `vibration` where device_id = "'+tower_id+'"'
|
|
|
+ for row in db.query(cmd) :
|
|
|
+ result['Moter'][row['channelName']]={'Vrms':row['DataValue'],'CV' :row['CVIndex']}
|
|
|
+
|
|
|
+ return result
|
|
|
+
|
|
|
+def get_tower(company:str,factory:str,department:str,towerGroup:str):
|
|
|
+ towergroup_arr =[]
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/Water_tower?charset=utf8mb4')
|
|
|
+ cmd = 'SELECT id FROM `device` where CompanyCode = "'+company+'" AND FactoryCode = "' +factory+'" AND DepartmentCode = "'+department+'" AND TowerGroupCode = "' + towerGroup + '"'
|
|
|
+ for row in db.query(cmd) :
|
|
|
+ towergroup_arr.append(row['id'])
|
|
|
+
|
|
|
+ return towergroup_arr
|