From a143f9feb5a2c4ac23ae9bd78555e3a2c1015c8e Mon Sep 17 00:00:00 2001 From: bernd2k <36308246+bernd2k@users.noreply.github.com> Date: Mon, 6 Jan 2020 20:10:42 +0100 Subject: [PATCH] adds simplification of loading in status.json --- raspberry_pi/main.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/raspberry_pi/main.py b/raspberry_pi/main.py index 7943cd6..58fc512 100644 --- a/raspberry_pi/main.py +++ b/raspberry_pi/main.py @@ -1,7 +1,6 @@ import json import os import time -import urllib.request, json import datetime import tweepy from mastodon import Mastodon @@ -74,7 +73,7 @@ MASTODON_API_BASE_URL = data_config["mastodon_api"]["api_base_url"] - +# storing the current and the previous states of the door state = 1 state_b4 = 1 @@ -100,16 +99,19 @@ while True: time_now = time.time() - - file_status_path = dir_path + os.sep + "status.json" + + + if state != state_b4: + file_status_path = dir_path + os.sep + "status.json" + + if os.path.exists(file_status_path) == True: + with open(file_status_path, 'r') as f: + data_status = json.load(f) if state == 1 and state_b4 == 0: time_opening = datetime.datetime.fromtimestamp(int(time_now)).strftime('%Y-%m-%d %H:%M:%S') print("open", time_opening) - - if os.path.exists(file_status_path) == True: - with open(file_status_path, 'r') as f: - data_status = json.load(f) + number = len(data_status["opening_text"]) number = textselect(0, number - 1) @@ -152,10 +154,6 @@ while True: time_closing = datetime.datetime.fromtimestamp(int(time_now)).strftime('%Y-%m-%d %H:%M:%S') print("closed", time_closing) - if os.path.exists(file_status_path) == True: - with open(file_status_path, 'r') as f: - data_status = json.load(f) - number = len(data_status["closing_text"]) number = textselect(0, number - 1)