diff --git a/source/server/apistatusd.py b/source/server/apistatusd.py index aff8193..4fe1eda 100755 --- a/source/server/apistatusd.py +++ b/source/server/apistatusd.py @@ -108,38 +108,6 @@ def receive_buffer_is_valid(raw_data): logging.debug('Argument is not valid: {}'.format(raw_data)) return False -def change_status(status, timestamp, filename): - ''' - Write the new status together with a timestamp into the Space API JSON. - param 1: byte object - param 2: string - return: boolean - ''' - logging.debug('Change status API') - # todo: use walrus operator := when migrating to python >= 3.8 - data = read_api(filename) - if data is False: - return False - - if os.access(filename, os.W_OK): - logging.debug('API file is writable') - with open(filename, 'w') as api_file: - logging.debug('API file open successfull') - data["state"]["open"] = status - data["state"]["lastchange"] = timestamp - try: - json.dump(data, api_file, indent=4) - except Exception as e: - logging.error('Failed to change API file') - logging.error('{}'.format(e)) - return False - logging.debug('API file changed') - else: - logging.error('API file is not writable. Wrong permissions?') - return False - logging.info('API file successfull changed to {}'.format(status)) - return True - def read_api(api): ''' Reads the Space API JSON into a dict. Returns the dict on success and @@ -156,15 +124,47 @@ def read_api(api): logging.debug('API is readable') with open(api, 'r') as api_file: - logging.debug('API file successfully opened') + logging.debug('API file successfully readable opened') try: api_json_data = json.load(api_file) - logging.debug('API file read successfull') + logging.debug('API file successfully read') except Exception as e: logging.error('Failed to read API file: {}'.format(e)) return False return api_json_data +def change_status(status, timestamp, filename): + ''' + Write the new status together with a timestamp into the Space API JSON. + param 1: byte object + param 2: string + return: boolean + ''' + logging.debug('Change status API') + # todo: use walrus operator := when migrating to python >= 3.8 + data = read_api(filename) + if data is False: + return False + + if os.access(filename, os.W_OK): + logging.debug('API file is writable') + with open(filename, 'w') as api_file: + logging.debug('API file successfull writable opened') + data["state"]["open"] = status + data["state"]["lastchange"] = timestamp + try: + json.dump(data, api_file, indent=4) + except Exception as e: + logging.error('Failed to change API file') + logging.error('{}'.format(e)) + return False + logging.debug('API file changed') + else: + logging.error('API file is not writable. Wrong permissions?') + return False + logging.info('API file successfull changed to {}'.format(status)) + return True + def get_status_and_time(raw_data): ''' Create a timestamp, changes the value of the given byte into a string