black screen fix

This commit is contained in:
Neythen Treloar
2022-04-07 18:35:38 +01:00
parent 82cdcc0022
commit 3eaf0fb3ca
3 changed files with 441 additions and 327 deletions

241
server.py
View File

@@ -23,7 +23,7 @@ import copy
import urllib.request
import sys
#stock_ticker = StockTicker()
#print('API CALLER NOT STARTED')
#open('log.txt', 'w').close() #wipe logs
@@ -42,13 +42,16 @@ displaying_screensaver = False
uploading = False
screensaver_p = None
ticker_stopped = False
professional = json.load(open('csv/display_settings.json', 'r'))[0] == "Professional"
f = open('csv/display_settings.json', 'r')
professional = json.load(f)[0] == "Professional"
f.close()
command = 300
tickerList = 0
DelayTime = 20
LastCommand = ''
speedTime = 25 #print('save')
speedTime = 25
LOGO_FOLDER = 'logos/'
CSV_FOLDER = 'csv/new/'
@@ -57,7 +60,9 @@ ALLOWED_EXTENSIONS = {'csv', 'png'}
ticker = pexpect.spawn("sudo -E python3 stockTicker.py")
time.sleep(2) # give the ticker time to initialise
system_info = json.load(open('csv/system_info.json'))
f = open('csv/system_info.json')
system_info = json.load(f)
f.close()
ticker.sendline('*') # run startup gif by default
time.sleep(8)
@@ -65,7 +70,9 @@ if system_info['first_boot']: # let startup message display
ticker.sendline('-')
system_info['first_boot'] = False
json.dump(system_info, open('csv/system_info.json', 'w'))
f = open('csv/system_info.json', 'w')
json.dump(system_info,f)
f.close()
else:
ticker.sendline('A') # run by default
@@ -93,17 +100,23 @@ def index():
'Sports (Team Stats)','Custom Images', 'Custom GIFs', 'Custom Messages', 'Stocks', 'Crypto', 'Forex']
global professional
feature_settings = json.load(open('csv/display_settings.json', 'r'))
print('professional',professional)
f = open('csv/display_settings.json', 'r')
feature_settings = json.load(f)
f.close()
if not professional:
currently_displaying = json.load(open('csv/display_settings.json', 'r'))[1]
f = open('csv/display_settings.json', 'r')
currently_displaying = json.load(f)[1]
f.close()
not_displaying = [f for f in all_features if f not in currently_displaying[0]]
elif professional:
currently_displaying = json.load(open('csv/display_settings.json', 'r'))[1]
f = open('csv/display_settings.json', 'r')
currently_displaying = json.load(f)[1]
f.close()
not_displaying = [f for f in all_features if f not in currently_displaying[0] and f not in currently_displaying[1]]
with open('api_keys.txt', 'r') as f:
@@ -116,21 +129,61 @@ def index():
timeString = now.strftime("%Y-%m-%d %H:%M")
logos_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos')
LogoList = os.listdir(logos_path)
stocks_settings = json.load(open('csv/stocks_settings.json', 'r'))
crypto_settings = json.load(open('csv/crypto_settings.json', 'r'))
forex_settings = json.load(open('csv/forex_settings.json', 'r'))
current_weather= json.load(open('csv/current_weather.json', 'r'))
daily_weather = json.load(open('csv/daily_weather.json', 'r'))
news_settings = json.load(open('csv/news_settings.json', 'r'))
upcoming_games = json.load(open('csv/upcoming_games.json', 'r'))
live_games = json.load(open('csv/live_games.json', 'r'))
past_games = json.load(open('csv/past_games.json', 'r'))
team_stats = json.load(open('csv/league_tables.json', 'r'))
image_settings = json.load(open('csv/image_settings.json', 'r'))
GIF_settings = json.load(open('csv/GIF_settings.json', 'r'))
message_settings = json.load(open('csv/message_settings.json', 'r'))
general_settings = json.load(open('csv/general_settings.json', 'r'))
f = open('csv/stocks_settings.json', 'r')
stocks_settings = json.load(f)
f.close()
f= open('csv/crypto_settings.json', 'r')
crypto_settings = json.load(f)
f.close()
f = open('csv/forex_settings.json', 'r')
forex_settings = json.load(f)
f.close()
f = open('csv/current_weather.json', 'r')
current_weather= json.load(f)
f.close()
f = open('csv/daily_weather.json', 'r')
daily_weather = json.load(f)
f.close()
f = open('csv/news_settings.json', 'r')
news_settings = json.load(f)
f.close()
f = open('csv/upcoming_games.json', 'r')
upcoming_games = json.load(f)
f.close()
f = open('csv/live_games.json', 'r')
live_games = json.load(f)
f.close()
f = open('csv/past_games.json', 'r')
past_games = json.load(f)
f.close()
f = open('csv/league_tables.json', 'r')
team_stats = json.load(f)
f.close()
f = open('csv/image_settings.json', 'r')
image_settings = json.load(f)
f.close()
f = open('csv/GIF_settings.json', 'r')
GIF_settings = json.load(f)
f.close()
f = open('csv/message_settings.json', 'r')
message_settings = json.load(f)
f.close()
f = open('csv/general_settings.json', 'r')
general_settings = json.load(f)
f.close()
try: # incase this doesnt exist
api_keys = api_key2[1]
except:
@@ -165,15 +218,15 @@ def index():
'wifi_SSID':wifi_SSID
}
#print('database caller:', api_caller.isalive())
return render_template('index.html', **templateData)
def save_displaying(input_settings):
#print('save')
global professional
print('save_displaying_input:', input_settings)
all_settings = ['Stocks', 'Crypto', 'Forex', 'Current Weather', 'Daily Forecast', 'News', 'Sports (Upcoming Games)', 'Sports (Past Games)',
'Sports (Live Games)', 'Sports (Team Stats)', 'Custom Images', 'Custom GIFs', 'Custom Messages']
professional = len(input_settings) == 2
@@ -195,7 +248,7 @@ def save_displaying(input_settings):
s = "Professional" if professional else "Standard"
display_settings = [s] + [input_settings]
print(display_settings)
with open('csv/display_settings.json', 'w+') as f:
json.dump(list(display_settings), f)
@@ -229,7 +282,7 @@ def start():
@app.route("/stop")
def stop():
print('stop')
global displaying_screensaver
global ticker
#global api_caller
@@ -273,13 +326,13 @@ def restart():
@app.route("/reset")
def reset():
print('reset')
os.system("sudo ./setup_config_files.sh")
return index()
@app.route("/save", methods = ['PUT', 'POST', 'GET'])
def save():
print('save')
global uploading
@@ -288,8 +341,7 @@ def save():
input_settings = json.loads(data)
print(input_settings)
save_displaying(input_settings['displaying'])
@@ -310,10 +362,9 @@ def save():
images = request.files
names = list(request.files.keys())
print(names)
for name in names:
print(name)
images[name].save('user_uploads/' +name)
@@ -327,7 +378,7 @@ def save():
# saves files uploaded to the webpage for images and GIFs
@app.route("/upload", methods = ['PUT', 'POST', 'GET'])
def upload():
print('upload')
global uploading
uploading = True
try:
@@ -339,15 +390,21 @@ def upload():
images[name].save('user_uploads/' +name)
except Exception as e:
#print(e)
uploading = False
return index()
def remove_old_uploads():
#remove old files
image_settings = json.load(open('csv/image_settings.json', 'r'))
GIF_settings = json.load(open('csv/GIF_settings.json', 'r'))
f = open('csv/image_settings.json', 'r')
image_settings = json.load(f)
f.close()
f = open('csv/GIF_settings.json', 'r')
GIF_settings = json.load(f)
f.close()
for filename in os.listdir('user_uploads'):
if filename not in image_settings['images'] and filename not in GIF_settings['images']:
os.remove('user_uploads/'+filename)
@@ -360,15 +417,17 @@ def brightness():
data= request.data.decode('utf-8')
settings = json.loads(data)
print(settings)
brightness =settings['brightness']
ticker.sendline(str(int(brightness)-1))
general_settings = json.load(open('csv/general_settings.json', 'r'))
f = open('csv/general_settings.json', 'r')
general_settings = json.load(f)
f.close()
general_settings['brightness'] = int(brightness)
json.dump(general_settings, open('csv/general_settings.json', 'w+'))
f = open('csv/general_settings.json', 'w+')
json.dump(general_settings, f)
f.close()
return index()
@@ -383,7 +442,7 @@ def edit_wpa_sup(country, ssid, pwd):
#remove this line to append to end instead of overwriting all networks
wpa_lines = wpa_lines[0:3]
print(wpa_lines)
# create new file from scratch
wpa_lines = []
@@ -399,7 +458,7 @@ def edit_wpa_sup(country, ssid, pwd):
wpa_lines.append('}\n')
wpa_string = ''.join(wpa_lines)
print(wpa_string)
current_wpa = open('/etc/wpa_supplicant/wpa_supplicant.conf', 'w+')
current_wpa.write(wpa_string)
@@ -408,17 +467,20 @@ def edit_wpa_sup(country, ssid, pwd):
def set_wifi():
data= request.data.decode('utf-8')
print(str(data))
settings = json.loads(data)
country = settings['country'].upper()
ssid = settings['ssid']
pwd = settings['pwd']
general_settings = json.load(open('csv/general_settings.json', 'r'))
f = open('csv/general_settings.json', 'r')
general_settings = json.load(f)
f.close()
general_settings['country_code'] = country
json.dump(general_settings, open('csv/general_settings.json', 'w+'))
f = open('csv/general_settings.json', 'w+')
json.dump(general_settings, f)
f.close()
edit_wpa_sup(country, ssid, pwd)
# resstart netoworking
@@ -440,14 +502,14 @@ def set_wifi():
#return True
#except subprocess.CalledProcessError:
# grep did not match any lines
#print("No wireless networks connected")
#return False
def edit_hosts(hostname):
current_hosts = open('/etc/hosts')
hosts_lines = current_hosts.readlines()
print(hosts_lines[5])
hosts_lines[5] = '127.0.1.1 {}'.format(hostname)
@@ -463,16 +525,20 @@ def hostname():
data= request.data.decode('utf-8')
settings = json.loads(data)
hostname = settings['hostname']
print('hostname', hostname)
edit_hosts(hostname)
os.system("sudo hostnamectl set-hostname {}".format(hostname))
os.system("sudo systemctl restart avahi-daemon")
general_settings = json.load(open('csv/general_settings.json', 'r'))
f = open('csv/general_settings.json', 'r')
general_settings = json.load(f)
f.close()
general_settings['hostname'] = hostname
json.dump(general_settings, open('csv/general_settings.json', 'w+'))
f = open('csv/general_settings.json', 'w+')
json.dump(general_settings, f)
f.close()
return index()
@@ -484,14 +550,14 @@ def saveWeatherAPIKey():
settings = json.loads(data)
key = settings['api_key']
print(key)
with open('./api_keys.txt') as f:
lines = f.readlines()
if len(lines) == 1:
lines.append(str(key))
elif len(lines) == 2:
lines[1] = str(key)
print(lines)
with open('./api_keys.txt', 'w') as f:
for line in lines:
f.write(line)
@@ -560,7 +626,7 @@ def save_trade_settings(input_settings):
filename = input_settings['feature'].lower() + '_settings.json'
f = open('csv/' + filename, 'r')
current_settings = json.load(f)
print(input_settings)
f.close()
current_settings['speed'] = input_settings['speed'].lower()
@@ -593,13 +659,14 @@ def save_trade_settings(input_settings):
def save_weather_settings(input_settings):
print(input_settings)
filename = 'current_weather.json' if input_settings['feature'] == 'Current Weather' else 'daily_weather.json'
print(filename)
current_settings = json.load(open('csv/' + filename, 'r'))
f = open('csv/' + filename, 'r')
current_settings = json.load(f)
f.close()
current_settings['speed'] = input_settings['speed'].lower()
current_settings['speed2'] = input_settings['speed2'].lower()
@@ -620,7 +687,9 @@ def save_weather_settings(input_settings):
'''
current_settings = combine_dict(current_settings, input_settings['locations'], 'locations')
json.dump(current_settings, open('csv/' + filename, 'w+'))
f = open('csv/' + filename, 'w+')
json.dump(current_settings, f)
f.close()
#api_caller.sendline('w')
f = open('csv/last_updates.json', 'r')
@@ -636,10 +705,11 @@ def save_weather_settings(input_settings):
def save_news_settings(input_settings):
filename = 'news_settings.json'
f = open('csv/' + filename, 'r')
current_settings = json.load(f)
f.close()
current_settings = json.load(open('csv/' + filename, 'r'))
print(current_settings)
current_settings['speed'] = input_settings['speed'].lower()
current_settings['speed2'] = input_settings['speed2'].lower()
@@ -653,9 +723,10 @@ def save_news_settings(input_settings):
print(current_settings)
json.dump(current_settings, open('csv/' + filename, 'w+'))
f = open('csv/' + filename, 'w+')
json.dump(current_settings, f)
f.close()
#api_caller.sendline('n')
f = open('csv/last_updates.json', 'r')
last_updates = json.load(f)
@@ -683,8 +754,10 @@ def save_sports_settings(input_settings):
elif feature == 'Sports (Team Stats)':
filename = 'league_tables.json'
update_key = 'sports_t'
current_settings = json.load(open('csv/' + filename, 'r'))
f = open('csv/' + filename, 'r')
current_settings = json.load(f)
f.close()
current_settings['speed'] = input_settings['speed'].lower()
current_settings['speed2'] = input_settings['speed2'].lower()
@@ -694,7 +767,9 @@ def save_sports_settings(input_settings):
current_settings = combine_dict(current_settings, input_settings['leagues'], 'leagues')
json.dump(current_settings, open('csv/' + filename, 'w+'))
f = open('csv/' + filename, 'w+')
json.dump(current_settings, f)
f.close()
#api_caller.sendline('S')
f = open('csv/last_updates.json', 'r')
@@ -721,29 +796,31 @@ def save_image_settings(input_settings):
del current_settings['feature']
json.dump(current_settings, open('csv/' + filename, 'w+'))
f = open('csv/' + filename, 'w+')
json.dump(current_settings, f)
f.close()
remove_old_uploads()
def save_message_settings(input_settings):
current_settings = json.load(open('csv/message_settings.json', 'r'))
f = open('csv/message_settings.json', 'r')
current_settings = json.load(f)
f.close()
new_settings = copy.deepcopy(input_settings)
print(current_settings)
print()
print(input_settings)
print()
for i,IS in enumerate(input_settings['messages']):
# check if this is in current_settings
for CS in current_settings['messages']:
if IS['name'] == CS['name']:
new_settings['messages'][i] = CS
print(CS)
break
print(new_settings)
json.dump(new_settings, open('csv/message_settings.json', 'w+'))
f = open('csv/message_settings.json', 'w+')
json.dump(new_settings, f)
f.close()