Skip to content

Commit

Permalink
Merge branch 'appVerUpdater' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
dimov-cz committed Jul 26, 2024
2 parents a361040 + 8667e46 commit 77282bf
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
83 changes: 83 additions & 0 deletions pcomfortcloud/app_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# - manage local file in tmp of system with last known version
# - provide last known version number
# - auto update version from string at https://raw.githubusercontent.com/dimov-cz/python-panasonic-comfort-cloud/versionupdater/currentVersion.txt

import os
import requests
import time

class AppVersion:
default_version = '1.20.1'
update_url = 'https://raw.githubusercontent.com/dimov-cz/python-panasonic-comfort-cloud/versionupdater/currentVersion.txt'



def __init__(self):
self.version = self.default_version

self.version_file = '/tmp/pcc_version.txt'
self.version = self._get_saved_version()

'''
Get last known version from file. Checks for update if last check was more than 1 hour ago.
'''
def get_version(self):
self._check_for_update()
return self.version.strip()

'''
Force update of version file. Return True if new version was found, False otherwise.
'''
def force_update(self):
return self._check_for_update(True)

'''
Get last known version from file.
If file does not exist, return default version.
'''
def _get_saved_version(self):
if os.path.exists(self.version_file):
with open(self.version_file, 'r') as f:
return f.read().strip()
else:
# check for update if no file exists - fist run case
if self._check_for_update():
return self.version

#fallback to default version if no file exists
return self.default_version

'''
Check for update and save new version to file if available.
Check for update only once per hour. Can be forced by setting force to True.
Returns True if new version was found, False otherwise.
'''
def _check_for_update(self, force=False):
if not force:
#test mtime of version file, dont update if last update was less than 1 hour ago
if os.path.exists(self.version_file):
mtime = os.path.getmtime(self.version_file)
if mtime + 3600 > time.time():
return False

try:
response = requests.get(self.update_url)
response.raise_for_status()
version = response.text.strip()
if os.path.exists(self.version_file) and version == self.version:
#update mtime is enough:
os.utime(self.version_file)
else:
with open(self.version_file, 'w') as f:
f.write(version)

self.version = version
return True
except requests.exceptions.RequestException:
# TODO report? exception? silent error?
# throw("Error while checking for update" + str(e))
pass

return False


5 changes: 4 additions & 1 deletion pcomfortcloud/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from . import urls
from . import constants
from .app_version import AppVersion

def _validate_response(response):
""" Verify that response is OK """
Expand Down Expand Up @@ -61,6 +62,7 @@ def __init__(self, username, password, tokenFileName='~/.panasonic-token', raw=F
self._devices = None
self._deviceIndexer = {}
self._raw = raw
self._appVersionUpdater = AppVersion()

if verifySsl == False:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
Expand Down Expand Up @@ -106,9 +108,10 @@ def logout(self):
""" Logout """

def _headers(self):
version = self._appVersionUpdater.get_version()
return {
"X-APP-TYPE": "1",
"X-APP-VERSION": "1.20.1",
"X-APP-VERSION": version,
"X-User-Authorization": self._vid,
"X-APP-TIMESTAMP": "1",
"X-APP-NAME": "Comfort Cloud",
Expand Down

0 comments on commit 77282bf

Please sign in to comment.