forked from Krautspace/doorstatus
Fix apistatusd.py: missing )
and code cleanup
This commit is contained in:
parent
c4c78aa5ba
commit
fd8c5da35a
1 changed files with 8 additions and 26 deletions
|
@ -130,18 +130,6 @@ def display_peercert(cert):
|
||||||
else:
|
else:
|
||||||
logging.debug(f'{key}: {cert[key]}')
|
logging.debug(f'{key}: {cert[key]}')
|
||||||
logging.debug('-----------------')
|
logging.debug('-----------------')
|
||||||
try:
|
|
||||||
logging.debug('Peer certificate commonName: {}'.format(
|
|
||||||
cert['subject'][5][0][1]))
|
|
||||||
logging.debug('Peer certificate serialNumber: {}'.format(
|
|
||||||
cert['serialNumber']))
|
|
||||||
logging.debug('Peer certificate notBefore: {}'.format(
|
|
||||||
cert['notBefore']))
|
|
||||||
logging.debug('Peer certificate notAfter: {}'.format(
|
|
||||||
cert['notAfter']))
|
|
||||||
except Exception as error:
|
|
||||||
logging.debug('Cert display failed')
|
|
||||||
logging.debug(error)
|
|
||||||
|
|
||||||
def receive_buffer_is_valid(raw_data):
|
def receive_buffer_is_valid(raw_data):
|
||||||
'''
|
'''
|
||||||
|
@ -224,7 +212,7 @@ def get_status_and_time(raw_data):
|
||||||
param 1: byte object
|
param 1: byte object
|
||||||
return: tuple (boolean, integer)
|
return: tuple (boolean, integer)
|
||||||
'''
|
'''
|
||||||
status = True if raw_data.decode('utf-8', 'strict') == '1' else False
|
status = raw_data.decode('utf-8', 'strict') == '1'
|
||||||
timestamp = int(str(time()).split('.')[0])
|
timestamp = int(str(time()).split('.')[0])
|
||||||
|
|
||||||
logging.debug('Set values for timestamp: {} and status: {}'.format(
|
logging.debug('Set values for timestamp: {} and status: {}'.format(
|
||||||
|
@ -274,10 +262,6 @@ class Toot(threading.Thread):
|
||||||
return: boolean
|
return: boolean
|
||||||
'''
|
'''
|
||||||
timeformat = '%d.%m.%Y %H:%M Uhr'
|
timeformat = '%d.%m.%Y %H:%M Uhr'
|
||||||
# check if status is valid
|
|
||||||
if self.status not in (True, False):
|
|
||||||
logging.error('Invalid status to toot')
|
|
||||||
return False
|
|
||||||
# convert seconds into timestring
|
# convert seconds into timestring
|
||||||
try:
|
try:
|
||||||
timestring = strftime(timeformat, localtime(self.timestamp))
|
timestring = strftime(timeformat, localtime(self.timestamp))
|
||||||
|
@ -285,9 +269,9 @@ class Toot(threading.Thread):
|
||||||
logging.error('Can not convert timestamp into timestring')
|
logging.error('Can not convert timestamp into timestring')
|
||||||
return False
|
return False
|
||||||
# set status message
|
# set status message
|
||||||
if self.status == True:
|
if self.status:
|
||||||
self.data['status'] = 'Krautspace is open since: {}'.format(timestring)
|
self.data['status'] = 'Krautspace is open since: {}'.format(timestring)
|
||||||
elif self.status == False:
|
else:
|
||||||
self.data['status'] = 'Krautspace is closed since: {}'.format(timestring)
|
self.data['status'] = 'Krautspace is closed since: {}'.format(timestring)
|
||||||
logging.debug('Message: {}'.format(self.data['status']))
|
logging.debug('Message: {}'.format(self.data['status']))
|
||||||
# build mastodon api url
|
# build mastodon api url
|
||||||
|
@ -448,19 +432,17 @@ def main():
|
||||||
ClientSocket.settimeout(float(config['general']['timeout']))
|
ClientSocket.settimeout(float(config['general']['timeout']))
|
||||||
logging.debug('set ssl handshake timeout to {}s'.format(ClientSocket.gettimeout()))
|
logging.debug('set ssl handshake timeout to {}s'.format(ClientSocket.gettimeout()))
|
||||||
Connection = context.wrap_socket(ClientSocket, server_side=True)
|
Connection = context.wrap_socket(ClientSocket, server_side=True)
|
||||||
logging.info('SSL Connection established')
|
logging.debug('SSL Connection established')
|
||||||
Connection.settimeout(float(config['general']['timeout']))
|
display_peercert(Connection.getpeercert(binary_form=False))
|
||||||
logging.debug('Connection timeout set to {}'.format(Connection.gettimeout())
|
|
||||||
cert = Connection.getpeercert(binary_form=False)
|
|
||||||
display_peercert(cert)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error('Unexpected error: {}'.format(e))
|
logging.error('Unexpected error: {}'.format(e))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# empfangen und antworten
|
# empfangen und antworten
|
||||||
raw_data = Connection.recv(1)
|
raw_data = Connection.recv(1)
|
||||||
if receive_buffer_is_valid(raw_data) is True:
|
if receive_buffer_is_valid(raw_data):
|
||||||
status, timestamp = get_status_and_time(raw_data)
|
status, timestamp = get_status_and_time(raw_data)
|
||||||
if change_status(status, timestamp, api_template, config['api']['api']) is True:
|
if change_status(status, timestamp, api_template, config['api']['api']):
|
||||||
answer = raw_data
|
answer = raw_data
|
||||||
if config['mastodon']['send'].lower() == 'true':
|
if config['mastodon']['send'].lower() == 'true':
|
||||||
logging.debug('Toot is set to true')
|
logging.debug('Toot is set to true')
|
||||||
|
|
Loading…
Reference in a new issue