Skip to content

Commit

Permalink
Merge pull request #59 from DewGew/intent_fulfilment_change
Browse files Browse the repository at this point in the history
Intent fulfilment change
  • Loading branch information
DewGew authored Nov 28, 2023
2 parents aa0751f + 831d4bd commit dc39fe8
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 244 deletions.
56 changes: 29 additions & 27 deletions modules/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

from flask import request, flash
import flask_login
import json, os, sys, hashlib
import json
import os
import sys
import hashlib
from werkzeug.security import generate_password_hash
from smarthome import report_state
from smarthome import rs
from modules.database import db, User, Settings
from modules.domoticz import getDomoticzDevices, queryDomoticz
from modules.helpers import logger, remove_user
Expand All @@ -14,7 +17,7 @@
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', '')
Expand All @@ -23,10 +26,10 @@ def modifyServerSettings(request):
dbsettings.use_ssl = (request.args.get('ssl', '') == 'true')
dbsettings.ssl_cert = request.args.get('sslcert', '')
dbsettings.ssl_key = request.args.get('sslkey', '')

db.session.add(dbsettings)
db.session.commit()

logger.info("Server settings saved")


Expand All @@ -35,31 +38,30 @@ 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.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')

db.session.add(dbuser)
db.session.commit()

logger.info("User settings updated")


@flask_login.login_required
def gateway():
dbsettings = Settings.query.get_or_404(1)

dbuser = User.query.filter_by(username=flask_login.current_user.username).first()
requestedUrl = request.url.split("/api")
custom = request.args.get('custom', '')
newsettings = {}

if custom == "sync":
if dbuser.googleassistant == True:
if report_state.enable_report_state():
if dbuser.googleassistant is True:
if rs.report_state_enabled():
payload = {"agentUserId": flask_login.current_user.username}
report_state.call_homegraph_api('sync', payload)
rs.call_homegraph_api('sync', payload)
result = '{"title": "RequestedSync", "status": "OK"}'
flash("Devices synced with domoticz")
else:
Expand All @@ -69,12 +71,12 @@ def gateway():
getDomoticzDevices(flask_login.current_user.username)
flash("Devices synced with domoticz")
return "Devices synced with domoticz", 200

if custom == "restart":

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', '')
Expand All @@ -83,27 +85,27 @@ def gateway():
elif custom == "server_settings":

modifyServerSettings(request)

elif custom == "user_settings":

modifyUserSettings(flask_login.current_user.username, request)

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

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

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

return "User removed", 200
else:

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

try:
return json.loads(result)
except:
except Exception:
return "No results returned", 404
Loading

0 comments on commit dc39fe8

Please sign in to comment.