Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intent fulfilment change #59

Merged
merged 5 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading