Skip to content

Commit

Permalink
Fix firmware upgrade by clearing cache #23
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonacox committed Nov 23, 2022
1 parent efe4656 commit 76a2344
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions proxy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import ssl
from transform import get_static, inject_js

BUILD = "t19"
BUILD = "t20"
ALLOWLIST = [
'/api/status', '/api/site_info/site_name', '/api/meters/site',
'/api/meters/solar', '/api/sitemaster', '/api/powerwalls',
Expand All @@ -36,7 +36,7 @@
'/api/operation', '/api/site_info/grid_codes', '/api/solars', '/api/solars/brands',
'/api/customer', '/api/meters', '/api/installer', '/api/networks',
'/api/system/networks', '/api/meters/readings', '/api/synchrometer/ct_voltage_references',
'/api/troubleshooting/problems'
'/api/troubleshooting/problems', '/api/auth/toggle/supported'
]
web_root = os.path.join(os.path.dirname(__file__), "web")

Expand Down Expand Up @@ -66,6 +66,7 @@
proxystats['start'] = int(time.time()) # Timestamp for Start
proxystats['clear'] = int(time.time()) # Timestamp of lLast Stats Clear
proxystats['uptime'] = ""
authcookie = ""

if https_mode == "yes":
# run https mode with self-signed cert
Expand Down Expand Up @@ -126,9 +127,14 @@ def address_string(self):
host, hostport = self.client_address[:2]
return host
def do_GET(self):
global authcookie, web_cache, proxystats
self.send_response(200)
message = "ERROR!"
contenttype = 'application/json'
if authcookie != pw.auth['AuthCookie']:
log.debug("Powerwall Auth Required - clearing internal web cache")
web_cache = {}
authcookie = pw.auth['AuthCookie']

if self.path == '/aggregates' or self.path == '/api/meters/aggregates':
# Meters - JSON
Expand Down Expand Up @@ -267,6 +273,19 @@ def do_GET(self):
elif self.path in ALLOWLIST:
# Allowed API Call
message = pw.poll(self.path)
elif self.path == '/cache':
# Display internal web cache
log.debug("Size of web_cache: {}".format(len(web_cache)))
c = []
for i in web_cache:
c.append(i)
message = json.dumps(c)
elif self.path == '/cache/clear':
# Clear internal web cache
web_cache = {}
log.debug("Clear internal web cache")
contenttype = 'text/plain; charset=utf-8'
message = 'OK'
else:
# Set auth headers required for web application
self.send_header("Set-Cookie", "AuthCookie={};{}".format(pw.auth['AuthCookie'], cookiesuffix))
Expand All @@ -281,6 +300,7 @@ def do_GET(self):
return

# Proxy request to Powerwall web server and cache.
self.send_header("Set-Cookie", "Cache-Control: no-cache")
cache_item = web_cache.get(self.path, None)
if not cache_item:
proxy_path = self.path
Expand All @@ -298,8 +318,10 @@ def do_GET(self):
fcontent = r.content
ftype = r.headers['content-type']
web_cache[self.path] = (fcontent, ftype)
log.debug("Added {} to web_cache".format(self.path))
else:
fcontent, ftype = cache_item
log.debug("Served {} from web_cache".format(self.path))

# Inject transformations
if self.path.split('?')[0] == "/":
Expand Down

0 comments on commit 76a2344

Please sign in to comment.