Skip to content

Commit

Permalink
Merge pull request #94 from DewGew/api_update
Browse files Browse the repository at this point in the history
  • Loading branch information
DewGew authored Feb 5, 2024
2 parents 5d34605 + 25948ee commit 62ce6b8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 34 deletions.
6 changes: 2 additions & 4 deletions VERSION.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
24.6
- Fixed "Deprecated RType () for API request" error
- Fixed camerastream not working
24.7
- Added REST API read more in wiki
- Add Security device
- UI improvments
- Add more selectable device types
92 changes: 62 additions & 30 deletions modules/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,30 @@ def modifyServerSettings(request):

dbsettings = Settings.query.get_or_404(1)

dbsettings.client_id = request.args.get('aogclient', '')
dbsettings.client_secret = request.args.get('aogsecret', '')
dbsettings.api_key = request.args.get('aogapi', '')
dbsettings.tempunit = request.args.get('tempunit', '')
dbsettings.language = request.args.get('language', '')
dbsettings.use_ssl = (request.args.get('ssl', '') == 'true')
dbsettings.ssl_cert = request.args.get('sslcert', '')
dbsettings.ssl_key = request.args.get('sslkey', '')
client_id = request.args.get('aogclient', None)
if client_id:
dbsettings.client_id = client_id
client_secret = request.args.get('aogsecret', None)
if client_secret:
dbsettings.client_secret = client_secret
api_key = request.args.get('aogapi', None)
if api_key:
dbsettings.api_key = api_key
tempunit = request.args.get('tempunit', None)
if tempunit:
dbsettings.tempunit = tempunit
language = request.args.get('language', None)
if language:
dbsettings.language = language
use_ssl = request.args.get('ssl', None)
if use_ssl:
dbsettings.use_ssl =(True if use_ssl else False)
ssl_cert = request.args.get('sslcert', None)
if ssl_cert:
dbsettings.ssl_cert = ssl_cert
ssl_key = request.args.get('sslkey', None)
if ssl_key:
dbsettings.ssl_key = ssl_key

db.session.add(dbsettings)
db.session.commit()
Expand All @@ -37,12 +53,24 @@ def modifyUserSettings(username, request):

dbuser = User.query.filter_by(username=username).first()

dbuser.domo_url = request.args.get('domourl', '')
dbuser.domouser = request.args.get('domouser', '')
dbuser.domopass = request.args.get('domopass', '')
dbuser.roomplan = request.args.get('roomplan', '')
dbuser.password = request.args.get('uipassword', '')
dbuser.googleassistant = (request.args.get('googleassistant', '') == 'true')
domo_url = request.args.get('domourl', None)
if domo_url:
dbuser.domo_url = domo_url
domouser = request.args.get('domouser', None)
if domouser:
dbuser.domouser = domouser
domopass = request.args.get('domopass', None)
if domopass:
dbuser.domopass = domopass
roomplan = request.args.get('roomplan', None)
if roomplan:
dbuser.roomplan = roomplan
password = request.args.get('uipassword', None)
if password:
dbuser.password = password
googleassistant = request.args.get('googleassistant', None)
if googleassistant:
dbuser.googleassistant = (True if googleassistant == 'true' else False)

db.session.add(dbuser)
db.session.commit()
Expand Down Expand Up @@ -70,42 +98,46 @@ def gateway():
else:
getDomoticzDevices(flask_login.current_user.username)
flash("Devices synced with domoticz")
return "Devices synced with domoticz", 200
result = '{"title": "RequestedSync", "status": "OK"}'

elif custom == "restart":

logger.info('Restarts smarthome server')
os.execv(sys.executable, ['python'] + sys.argv)
if dbuser.admin:
logger.info('Restarts smarthome server')
os.execv(sys.executable, ['python'] + sys.argv)

elif custom == "setArmLevel":
armLevel = request.args.get('armLevel', '')
seccode = request.args.get('seccode', '')
result = queryDomoticz(flask_login.current_user.username, '?type=command&param=setsecstatus&secstatus=' + armLevel + '&seccode=' + hashlib.md5(str.encode(seccode)).hexdigest())

elif custom == "server_settings":

modifyServerSettings(request)
if dbuser.admin:
modifyServerSettings(request)
result = '{"title": "ServerSettingsChanged", "status": "OK"}'

elif custom == "user_settings":

modifyUserSettings(flask_login.current_user.username, request)

result = '{"title": "UserSettingsChanged", "status": "OK"}'

elif custom == "removeuser":
userToRemove = request.args.get('user', '')
if dbuser.admin:
userToRemove = request.args.get('user', '')

removeuser = User.query.filter_by(username=userToRemove).first()
removeuser = User.query.filter_by(username=userToRemove).first()

db.session.delete(removeuser)
db.session.commit()
remove_user(userToRemove)
logger.info("User " + userToRemove + " is deleted")
db.session.delete(removeuser)
db.session.commit()
remove_user(userToRemove)
logger.info("User " + userToRemove + " is deleted")

return "User removed", 200
result = '{"title": "UserRemoved", "status": "OK"}'

elif '?type' in requestedUrl[1]:

result = queryDomoticz(flask_login.current_user.username, requestedUrl[1])

try:
return json.loads(result)
return json.loads(result), 200
except Exception:
return "No results returned", 404
return {"title": "No results returned", "status": "ERR"}, 404

0 comments on commit 62ce6b8

Please sign in to comment.