statusd.py now works with json modul, fix bug in set_values()
read and write the api file now uses the json modul. correct a bug in function set_values().
This commit is contained in:
parent
0e405f894d
commit
943c060b60
1 changed files with 13 additions and 38 deletions
45
statusd.py
45
statusd.py
|
@ -11,6 +11,7 @@ import socket
|
||||||
import ssl
|
import ssl
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
import json
|
||||||
from time import time, ctime, sleep
|
from time import time, ctime, sleep
|
||||||
from sys import exit, byteorder
|
from sys import exit, byteorder
|
||||||
|
|
||||||
|
@ -101,25 +102,6 @@ def receive_buffer_is_valid(raw_data):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def replace_entry(line, new):
|
|
||||||
'''
|
|
||||||
The function becomes two strings and replaces the second part of the
|
|
||||||
first string from ":" until end with the second string. Than appends a
|
|
||||||
"," and returns the result.
|
|
||||||
|
|
||||||
!!! Todo: needs exception handling !!!
|
|
||||||
|
|
||||||
param 1: string
|
|
||||||
param 2: string
|
|
||||||
return: string
|
|
||||||
'''
|
|
||||||
array = line.split(':')
|
|
||||||
logging.debug('Replace {} with {}'.format(array[1], new))
|
|
||||||
array[1] = ''.join((new, ','))
|
|
||||||
line = ':'.join((array[0], array[1]))
|
|
||||||
return line
|
|
||||||
|
|
||||||
|
|
||||||
def change_status(raw_data, api):
|
def change_status(raw_data, api):
|
||||||
'''
|
'''
|
||||||
Becomes the received byte and the path to API file. Grabs the content of
|
Becomes the received byte and the path to API file. Grabs the content of
|
||||||
|
@ -139,20 +121,12 @@ def change_status(raw_data, api):
|
||||||
logging.debug('API file is writable')
|
logging.debug('API file is writable')
|
||||||
with open(api, 'w') as api_file:
|
with open(api, 'w') as api_file:
|
||||||
logging.debug('API file open successfull')
|
logging.debug('API file open successfull')
|
||||||
for line in data.splitlines():
|
data["state"]["open"] = status
|
||||||
if line.strip().startswith('"state":'):
|
data["state"]["lastchange"] = timestamp
|
||||||
edit = True
|
|
||||||
elif edit == True and line.strip().startswith('"open":'):
|
|
||||||
line = replace_entry(line, status)
|
|
||||||
elif edit == True and line.strip().startswith('"lastchange":'):
|
|
||||||
line = replace_entry(line, timestamp)
|
|
||||||
edit = False
|
|
||||||
try:
|
try:
|
||||||
api_file.write(line)
|
json.dump(data, api_file, indent=4)
|
||||||
api_file.write('\n')
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error('Failed to write line to API file')
|
logging.error('Failed to change API file')
|
||||||
logging.error('Line: {}'.format(line))
|
|
||||||
logging.error('{}'.format(e))
|
logging.error('{}'.format(e))
|
||||||
logging.debug('API file changed')
|
logging.debug('API file changed')
|
||||||
else:
|
else:
|
||||||
|
@ -176,12 +150,12 @@ def read_api(api):
|
||||||
with open(api, 'r') as api_file:
|
with open(api, 'r') as api_file:
|
||||||
logging.debug('API opened successfull')
|
logging.debug('API opened successfull')
|
||||||
try:
|
try:
|
||||||
api_data = api_file.read()
|
api_json_data = json.load(api_file)
|
||||||
logging.debug('API read successfull')
|
logging.debug('API file read successfull')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error('Failed to read API file(): {}'.format(e))
|
logging.error('Failed to read API file(): {}'.format(e))
|
||||||
return False
|
return False
|
||||||
return (api_data)
|
return (api_json_data)
|
||||||
logging.error('Failed to read API file')
|
logging.error('Failed to read API file')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -194,10 +168,11 @@ def set_values(raw_data):
|
||||||
return: tuple
|
return: tuple
|
||||||
'''
|
'''
|
||||||
timestamp = str(time()).split('.')[0]
|
timestamp = str(time()).split('.')[0]
|
||||||
if raw_data == 'b\x01':
|
if raw_data == b'\x01':
|
||||||
status = "true"
|
status = "true"
|
||||||
else:
|
else:
|
||||||
status = "false"
|
status = "false"
|
||||||
|
logging.debug('Set values for timestamp: {} and status: {}'.format(timestamp, status))
|
||||||
return (status, timestamp)
|
return (status, timestamp)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue