-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/WebApp' into develop
- Loading branch information
Showing
35 changed files
with
11,473 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ src/logs/* | |
dist/ | ||
build/ | ||
src/WeConnect_SolarManager.egg-info/ | ||
src/pushNotifications.json | ||
*.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Python: Current File", | ||
"type": "python", | ||
"request": "launch", | ||
"program": "${file}", | ||
"console": "integratedTerminal", | ||
"justMyCode": true, | ||
"cwd": "${fileDirname}" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
requests >= 2.27.1 | ||
weconnect[Images] >= 0.54.2 | ||
weconnect[Images] >= 0.55.0 | ||
flask >= 2.3.1 | ||
cryptography == 3.3.2 | ||
pywebpush >= 1.14.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import json | ||
import os | ||
import configparser | ||
import logging | ||
import logging.handlers | ||
|
||
from pathlib import Path | ||
from pywebpush import webpush | ||
from datetime import datetime | ||
|
||
class Helper: | ||
|
||
@staticmethod | ||
def getLogEntries(): | ||
filePath = Path(os.path.join(os.path.dirname(__file__), "logs/SolarManager.log")) | ||
|
||
if filePath.is_file(): | ||
with open(filePath) as file: | ||
return [line.rstrip() for line in file] | ||
|
||
return [] | ||
|
||
@staticmethod | ||
def loadConfig() -> configparser: | ||
configFileName = "config.txt" | ||
userConfigFileName = "config.txt.user" | ||
userConfigFile = Path(os.path.join(os.path.dirname(__file__), userConfigFileName)) | ||
|
||
if userConfigFile.is_file(): | ||
configFileName = userConfigFileName | ||
|
||
configFilePath = os.path.join(os.path.dirname(__file__), configFileName) | ||
configParser = configparser.ConfigParser() | ||
configParser.read(configFilePath) | ||
|
||
return configParser | ||
|
||
@staticmethod | ||
def loadPushNotifications() -> json: | ||
fileName = "pushNotifications.json" | ||
filePath = Path(os.path.join(os.path.dirname(__file__), fileName)) | ||
|
||
if filePath.is_file(): | ||
with open(filePath) as json_file: | ||
return json.load(json_file) | ||
|
||
return json.loads('{"devices":[]}') | ||
|
||
@staticmethod | ||
def savePushNotifications(jsonObj: json) -> None: | ||
fileName = "pushNotifications.json" | ||
filePath = Path(os.path.join(os.path.dirname(__file__), fileName)) | ||
|
||
with open(filePath, "w") as outfile: | ||
json.dump(jsonObj, outfile) | ||
|
||
@staticmethod | ||
def sendPushNotification(title: str, message: str) -> None: | ||
configParser = Helper.loadConfig() | ||
pushNotifications = Helper.loadPushNotifications() | ||
|
||
for device in pushNotifications["devices"]: | ||
|
||
try: | ||
subscription_information = { | ||
"endpoint": device["endpoint"], | ||
"keys": { "auth": device["auth"], "p256dh": device["p256dh"] } | ||
} | ||
|
||
data = json.dumps({"title": title, "message": message, "tag": "", "dateTime": datetime.utcnow()}, default=str) | ||
|
||
webpush( | ||
subscription_info=subscription_information, | ||
data=data, | ||
vapid_private_key=configParser.get("WebApp", "WebPushPrivateKey"), | ||
vapid_claims={"sub": configParser.get("WebApp", "WebPushSubject")} | ||
) | ||
except Exception as e: | ||
LOG = logging.getLogger("SolarManager.Helper") | ||
LOG.error("An error occured while sending push notification: " + str(e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import json | ||
import os | ||
|
||
from flask import Flask, render_template, request | ||
|
||
from Helper import Helper | ||
|
||
configParser = Helper.loadConfig() | ||
port = configParser.getint("WebApp", "Port") | ||
publicKey = configParser.get("WebApp", "WebPushPublicKey") | ||
|
||
templateDir = os.path.join(os.path.dirname(__file__), "templates") | ||
staticDir = os.path.join(os.path.dirname(__file__), "static") | ||
app = Flask("WeConnect-SolarManager", template_folder=templateDir, static_folder=staticDir) | ||
|
||
def getLogs() -> str: | ||
logEntries = Helper.getLogEntries() | ||
return '<br/>'.join(logEntries) | ||
|
||
@app.route('/api/pushnotification', methods=['POST']) | ||
def pushNotification(): | ||
|
||
data = request.json | ||
endpoint = data["endpoint"] | ||
auth = data["auth"] | ||
p256dh = data["p256dh"] | ||
|
||
pushNotifications = Helper.loadPushNotifications() | ||
alreadyDefined = False | ||
|
||
for device in pushNotifications["devices"]: | ||
if device["endpoint"] == endpoint and device["auth"] == auth and device["p256dh"] == p256dh: | ||
alreadyDefined = True | ||
break | ||
|
||
if alreadyDefined == False: | ||
pushNotifications["devices"].append({"endpoint": endpoint, "auth": auth, "p256dh": p256dh}) | ||
Helper.savePushNotifications(pushNotifications) | ||
|
||
return json.dumps({'success':True}), 200, {'ContentType':'application/json'} | ||
|
||
@app.route('/') | ||
def index(): | ||
return render_template('index.html', PublicKey=publicKey, LogOutput=getLogs()) | ||
|
||
if __name__ == '__main__': | ||
app.run(debug=False, ssl_context='adhoc', port=port, host='0.0.0.0') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.