forked from Krautspace/doorstatus
kleinere umstrukturierung
auswertung der konfig fuer mastodon, default_config erweitert, set_values() in get_status_and_timestamp() umbenannt, statusstring und timestamp werden in main() aufgerufen und an andere funktionen uebergeben
This commit is contained in:
parent
c4d02a73c9
commit
edece83dd1
1 changed files with 17 additions and 8 deletions
|
@ -14,6 +14,7 @@ import os
|
|||
import socket
|
||||
import ssl
|
||||
import sys
|
||||
from mastodon import Mastodon
|
||||
from time import time, sleep
|
||||
import configparser
|
||||
|
||||
|
@ -102,7 +103,7 @@ def receive_buffer_is_valid(raw_data):
|
|||
return False
|
||||
|
||||
|
||||
def change_status(raw_data, api):
|
||||
def change_status(status, timestamp, filename):
|
||||
'''
|
||||
Write the new status together with a timestamp into the Space API JSON.
|
||||
param 1: byte object
|
||||
|
@ -112,14 +113,13 @@ def change_status(raw_data, api):
|
|||
|
||||
logging.debug('Change status API')
|
||||
# todo: use walrus operator := when migrating to python >= 3.8
|
||||
data = read_api(api)
|
||||
data = read_api(filename)
|
||||
if data is False:
|
||||
return False
|
||||
|
||||
status, timestamp = set_values(raw_data)
|
||||
if os.access(api, os.W_OK):
|
||||
if os.access(filename, os.W_OK):
|
||||
logging.debug('API file is writable')
|
||||
with open(api, 'w') as api_file:
|
||||
with open(filename, 'w') as api_file:
|
||||
logging.debug('API file open successfull')
|
||||
data["state"]["open"] = status
|
||||
data["state"]["lastchange"] = timestamp
|
||||
|
@ -133,7 +133,7 @@ def change_status(raw_data, api):
|
|||
else:
|
||||
logging.error('API file is not writable. Wrong permissions?')
|
||||
return False
|
||||
logging.info('Status successfull changed to {}'.format(status))
|
||||
logging.info('API file successfull changed to {}'.format(status))
|
||||
return True
|
||||
|
||||
|
||||
|
@ -164,7 +164,7 @@ def read_api(api):
|
|||
return api_json_data
|
||||
|
||||
|
||||
def set_values(raw_data):
|
||||
def get_status_and_time(raw_data):
|
||||
'''
|
||||
Create a timestamp, changes the value of the given byte into a string
|
||||
and returns both.
|
||||
|
@ -211,6 +211,11 @@ def main():
|
|||
'api': {
|
||||
'api': './api',
|
||||
'template': './api_template'
|
||||
},
|
||||
'mastodon': {
|
||||
'send': 'false',
|
||||
'host': 'localhost',
|
||||
'token': 'aaaaa-bbbbb-ccccc-ddddd-eeeee'
|
||||
}
|
||||
}
|
||||
configfile = './apistatusd.conf'
|
||||
|
@ -293,8 +298,12 @@ def main():
|
|||
|
||||
raw_data = conn.recv(1)
|
||||
if receive_buffer_is_valid(raw_data) is True:
|
||||
if change_status(raw_data, config['api']['api']) is True:
|
||||
status, timestamp = get_status_and_time(raw_data)
|
||||
if change_status(status, timestamp, config['api']['api']) is True:
|
||||
answer = raw_data
|
||||
if config['mastodon']['send'].lower() == 'true':
|
||||
logging.debug('Try to toot status')
|
||||
else: logging.debug('Toot is set to false')
|
||||
if conn:
|
||||
logging.debug('Send {} back'.format(raw_data))
|
||||
conn.send(answer)
|
||||
|
|
Loading…
Reference in a new issue