From 359914825fec89b85c2c4bf526042c3b6e45a68e Mon Sep 17 00:00:00 2001 From: ceds92 Date: Fri, 28 Jun 2024 12:29:36 +1000 Subject: [PATCH] Automatically check for updates and minor enhancements - web app checks an api to see if there is a new version of Scanbot available. if there is, notify the user on the home screen - remove unused whitelist so warnings about it stop showing up --- pyproject.toml | 2 +- scanbot/App/src/Scanbot.js | 8 +++++++ scanbot/server/scanbot_interface.py | 37 +---------------------------- scanbot/server/server.py | 28 ++++++++++++++++++++-- 4 files changed, 36 insertions(+), 39 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index e837c4a..626beec 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "scanbot" -version = "4.4.2" +version = "4.5.0" description = "Collection of automated STM and nc-AFM commands compatible with Nanonis V5 SPM Controller" authors = ["Julian Ceddia "] readme = "README.md" diff --git a/scanbot/App/src/Scanbot.js b/scanbot/App/src/Scanbot.js index 6ae767b..972ec22 100644 --- a/scanbot/App/src/Scanbot.js +++ b/scanbot/App/src/Scanbot.js @@ -6,6 +6,7 @@ import "./styles/Scanbot.css"; function Scanbot() { const [configFound, setConfigFound] = useState(true) const [isConnected, setIsConnected] = useState(true) + const [is2Update, setIsUp2date] = useState(true) const CardRow = () => (
@@ -24,10 +25,16 @@ function Scanbot() { const hasConnection = await getResponse('/test_connection') setIsConnected(hasConnection) } + + const checkUpdates = async () => { + const isUpdated = await getResponse('/check_updates') + setIsUp2date(isUpdated) + } useEffect(() => { checkConfig() testConnection() + checkUpdates() // eslint-disable-next-line react-hooks/exhaustive-deps }, []); @@ -39,6 +46,7 @@ function Scanbot() { {isConnected ?
:

Warning: Could not connect to Nanonis. Please check configured IP and ports and make sure Nanonis V5 is running.

} {configFound ?
:

Warning: A configuration file has not been found - using default parameters. Accept configuration to remove this warning.

} + {is2Update ?
:

A newer version of Scanbot is available.

}
diff --git a/scanbot/server/scanbot_interface.py b/scanbot/server/scanbot_interface.py index 888ef38..c95e8a5 100644 --- a/scanbot/server/scanbot_interface.py +++ b/scanbot/server/scanbot_interface.py @@ -177,8 +177,6 @@ def loadConfig(self): from hk_commands import hk_commands self.hk_commands = hk_commands(self) - self.loadWhitelist() - def firebaseInit(self): try: import firebase_admin @@ -192,16 +190,6 @@ def firebaseInit(self): print("Firebase not initialised...") print(e) - def loadWhitelist(self): - self.whitelist = [] - try: - print("Loading whitelist.txt...") - with open('whitelist.txt', 'r') as f: - d = f.read() - self.whitelist = d.split('\n')[:-1] - except: - print('No whitelist found... create one with add_user') - def initGlobals(self): global_.tasks = [] global_.running = threading.Event() # event to stop threads @@ -217,8 +205,6 @@ def initCommandDict(self): 'get_portlist' : lambda args: self.portList, # Return the list of ports scanbot is configured to use 'set_upload_method': self.setUploadMethod, # Set which upload method to use when uploading pngs 'get_upload_method': lambda args: self.uploadMethod, # View the upload method - 'add_user' : self.addUser, # Add a user to the whitelist (by email - zulip only) - 'get_users' : lambda args: str(self.whitelist), # Get the list of users allowed to talk to scanbot (zulip only) 'set_path' : self.setPath, # Changes the directory pngs are saved in. Creates the directory if it doesn't exist. 'get_path' : lambda args: self.path, # Path to save scan pngs (saves the channel of focus which can be set using plot_channel) 'plot_channel' : self.plotChannel, # Read/select the current channel of focus @@ -627,22 +613,6 @@ def setUploadMethod(self,uploadMethod,_help=False): self.uploadMethod = uploadMethod self.reactToMessage('all_good') - def addUser(self,user,_help=False): - arg_dict = {'' : ['', 0, "(string) Add user email to whitelist (one at a time)"]} - - if(_help): return arg_dict - - if(len(user) != 1): self.reactToMessage("cross_mark"); return - if(' ' in user[0]): self.reactToMessage("cross_mark"); return # Replace this with proper email validation - try: - self.whitelist.append(user[0]) - with open('whitelist.txt', 'w') as f: - for w in self.whitelist: - f.write(w+'\n') - self.reactToMessage('all_good') - except Exception as e: - return str(e) - def setPath(self,path,_help=False): arg_dict = {'' : ['', 0, "(string) Sets upload path. Creates the directory if path does not exist"]} @@ -729,12 +699,7 @@ def handle_message(self, message, bot_handler=None): messageContent = message self.bot_message = [] self.bot_handler = bot_handler - if(bot_handler): # If there's a bot_handler, we're communicating via zulip - if message['sender_email'] not in self.whitelist and self.whitelist: - self.sendReply(message['sender_email']) - self.sendReply('access denied') - return - + if(bot_handler): # If there's a bot_handler, we're communicating via zulip self.bot_message = message messageContent = message['content'] diff --git a/scanbot/server/server.py b/scanbot/server/server.py index c73e5d0..8828a4d 100644 --- a/scanbot/server/server.py +++ b/scanbot/server/server.py @@ -13,6 +13,8 @@ import pickle import webbrowser from threading import Timer +import urllib.request +import json run_mode = 'react' @@ -20,12 +22,12 @@ # pip install pyinstaller # Comment out the below ARGS section # Run the following command from /scanbot/server: -# pyinstaller --onefile --icon=..\App\public\favicon.ico --add-data "..\App\build;static" --name scanbot_v4.4.3 server.py +# pyinstaller --onefile --icon=..\App\public\favicon.ico --add-data "..\App\build;static" --name scanbot_v4.5.0 server.py ################# ARGS ################## import argparse parser = argparse.ArgumentParser() -parser.add_argument('--version', action='version', version='scanbot 4.4.3', help='show the version number and exit') +parser.add_argument('--version', action='version', version='scanbot 4.5.0', help='show the version number and exit') parser.add_argument('-c', '--terminal', action='store_true', help='run scanbot in terminal') parser.add_argument('-z', '--zulip', action='store_true', help='run scanbot in terminal') args = parser.parse_args() @@ -342,6 +344,28 @@ def getDir(path): def open_browser(): webbrowser.open_new('http://127.0.0.1:5000/') +def getLatestVersion(url): + try: + with urllib.request.urlopen(url) as response: + data = json.loads(response.read().decode()) + version = data.get('version') + return version + except: + return '0.0.0' + +@app.route('/check_updates') +def check_updates(): + currentVersion = '4.5.0' + latestVersion = getLatestVersion('https://us-central1-scanbot-46390.cloudfunctions.net/getVersion') + + update = False + for n,v in enumerate(currentVersion.split('.')): + if(int(latestVersion.split('.')[n]) > int(v)): + update = True + break + + return {"status": not update}, 200 + # Running app if __name__ == '__main__': Timer(1, open_browser).start()