2021-04-26 18:51:21 +00:00
|
|
|
# Copyright (C) 2020 Daniel Richardson richardson.daniel@hotmail.co.uk
|
|
|
|
#
|
|
|
|
# This file is part of stockTicker project for justinodunn.
|
|
|
|
#
|
|
|
|
# stockTicker can not be copied and/or distributed without the express
|
|
|
|
# permission of Daniel Richardson
|
|
|
|
|
|
|
|
from flask import Flask, render_template, request
|
2021-04-26 19:27:34 +00:00
|
|
|
from stockTicker import StockTicker
|
2021-04-26 18:51:21 +00:00
|
|
|
from werkzeug.utils import secure_filename
|
|
|
|
import os
|
|
|
|
import datetime
|
|
|
|
import threading
|
|
|
|
import csv
|
2021-05-01 10:39:20 +00:00
|
|
|
import pexpect
|
2021-05-05 21:14:04 +00:00
|
|
|
import time
|
2021-05-27 19:10:57 +00:00
|
|
|
import json
|
2021-05-01 10:39:20 +00:00
|
|
|
from multiprocessing import Process
|
|
|
|
from subprocess import Popen, PIPE
|
2021-08-11 19:58:01 +00:00
|
|
|
import numpy as np
|
2021-05-01 10:39:20 +00:00
|
|
|
#stock_ticker = StockTicker()
|
2021-05-05 14:44:43 +00:00
|
|
|
api_caller = pexpect.spawn("sudo -E python3 api_caller.py")
|
|
|
|
|
2021-04-26 19:27:34 +00:00
|
|
|
|
2021-04-26 18:51:21 +00:00
|
|
|
command = 300
|
|
|
|
tickerList = 0
|
|
|
|
DelayTime = 20
|
|
|
|
LastCommand = ''
|
|
|
|
speedTime = 25
|
|
|
|
|
|
|
|
LOGO_FOLDER = 'logos/'
|
|
|
|
CSV_FOLDER = 'csv/new/'
|
|
|
|
ALLOWED_EXTENSIONS = {'csv', 'png'}
|
|
|
|
|
2021-07-31 10:01:10 +00:00
|
|
|
ticker = pexpect.spawn("sudo -E taskset -c 3 python3 stockTicker.py")
|
2021-06-21 16:41:18 +00:00
|
|
|
ticker.sendline('A') # run by default
|
2021-07-08 18:42:21 +00:00
|
|
|
|
2021-05-01 10:39:20 +00:00
|
|
|
|
2021-04-26 18:51:21 +00:00
|
|
|
def allowed_file(filename):
|
|
|
|
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
|
|
|
|
|
|
def process_file(path, filename):
|
|
|
|
default_csv = csv.writer(open('csv/tickers.csv', 'w'))
|
|
|
|
new_csv = csv.reader(open((path), 'r'))
|
|
|
|
|
|
|
|
for row in new_csv:
|
|
|
|
default_csv.writerow(row)
|
|
|
|
|
2021-05-05 15:22:01 +00:00
|
|
|
|
2021-05-01 11:08:15 +00:00
|
|
|
|
2021-04-26 18:51:21 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
@app.route("/", methods=['GET', 'POST'])
|
2021-08-18 19:50:02 +00:00
|
|
|
def index():
|
2021-04-26 18:51:21 +00:00
|
|
|
global command
|
2021-05-05 15:22:01 +00:00
|
|
|
|
|
|
|
|
2021-04-26 18:51:21 +00:00
|
|
|
now = datetime.datetime.now()
|
|
|
|
timeString = now.strftime("%Y-%m-%d %H:%M")
|
2021-05-05 21:00:12 +00:00
|
|
|
logos_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos')
|
|
|
|
LogoList = os.listdir(logos_path)
|
2021-04-26 18:51:21 +00:00
|
|
|
templateData = {
|
|
|
|
'title' : 'Stock Ticker Control Panel',
|
|
|
|
'time': timeString,
|
|
|
|
'runtime': command,
|
|
|
|
'tickers': tickerList,
|
|
|
|
'logofiles':LogoList,
|
|
|
|
'lastcommand':LastCommand,
|
|
|
|
'delay':DelayTime,
|
|
|
|
'speedtime':speedTime
|
|
|
|
}
|
|
|
|
return render_template('index.html', **templateData)
|
|
|
|
|
|
|
|
@app.route("/Runtime", methods=['POST'])
|
|
|
|
def Runtime():
|
2021-07-08 18:42:21 +00:00
|
|
|
|
2021-04-26 18:51:21 +00:00
|
|
|
command = request.form['text']
|
2021-07-08 18:42:21 +00:00
|
|
|
|
|
|
|
|
2021-04-26 18:51:21 +00:00
|
|
|
LastCommand = 'Change runtime'
|
2021-08-18 19:50:02 +00:00
|
|
|
return index()
|
2021-04-26 18:51:21 +00:00
|
|
|
|
|
|
|
@app.route("/Delay", methods=['POST'])
|
|
|
|
def Delay():
|
|
|
|
global DelayTime
|
|
|
|
DelayTime = request.form['text']
|
2021-07-08 18:42:21 +00:00
|
|
|
|
2021-04-26 18:51:21 +00:00
|
|
|
global LastCommand
|
|
|
|
LastCommand = 'Change Delay'
|
2021-08-18 19:50:02 +00:00
|
|
|
return index()
|
2021-04-26 18:51:21 +00:00
|
|
|
|
|
|
|
@app.route("/Speed", methods=['POST'])
|
|
|
|
def Speed():
|
2021-05-05 15:22:01 +00:00
|
|
|
global speed
|
|
|
|
speed = request.form['text']
|
2021-07-08 18:42:21 +00:00
|
|
|
|
2021-05-05 15:22:01 +00:00
|
|
|
ticker.sendline(speed)
|
|
|
|
|
|
|
|
f = open('csv/settings.csv', 'r')
|
|
|
|
CSV = csv.reader(f)
|
2021-05-06 19:59:27 +00:00
|
|
|
next(CSV)
|
2021-05-05 15:22:01 +00:00
|
|
|
for line in CSV:
|
|
|
|
_, brightness = line
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
f = open('csv/settings.csv', 'w+')
|
2021-05-06 19:59:27 +00:00
|
|
|
f.write('speed,brightness\n')
|
2021-05-05 15:22:01 +00:00
|
|
|
f.write(str(speed) + ',' + brightness)
|
|
|
|
f.close()
|
2021-04-26 18:51:21 +00:00
|
|
|
global LastCommand
|
|
|
|
LastCommand = 'Change Speed'
|
2021-08-18 19:50:02 +00:00
|
|
|
return index()
|
2021-05-03 10:39:31 +00:00
|
|
|
|
|
|
|
@app.route("/Brightness", methods=['POST'])
|
|
|
|
def Brightness():
|
|
|
|
global brightness
|
2021-05-04 21:49:33 +00:00
|
|
|
brightness = int(request.form['text'])-1
|
2021-07-08 18:42:21 +00:00
|
|
|
|
2021-05-04 21:49:33 +00:00
|
|
|
ticker.sendline(str(brightness))
|
2021-05-05 15:22:01 +00:00
|
|
|
|
|
|
|
f = open('csv/settings.csv', 'r')
|
|
|
|
CSV = csv.reader(f)
|
2021-05-06 19:59:27 +00:00
|
|
|
next(CSV)
|
2021-05-05 15:22:01 +00:00
|
|
|
for line in CSV:
|
|
|
|
speed, _ = line
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
f = open('csv/settings.csv', 'w+')
|
2021-05-06 19:59:27 +00:00
|
|
|
f.write('speed,brightness\n')
|
2021-05-05 15:22:01 +00:00
|
|
|
f.write(str(speed) + ',' + str(brightness))
|
|
|
|
f.close()
|
|
|
|
|
2021-05-03 10:39:31 +00:00
|
|
|
global LastCommand
|
|
|
|
LastCommand = 'Change Brightness'
|
2021-08-18 19:50:02 +00:00
|
|
|
return index()
|
2021-04-26 18:51:21 +00:00
|
|
|
|
2021-05-21 10:20:39 +00:00
|
|
|
@app.route("/DisplayText", methods=['POST'])
|
|
|
|
def DisplayText():
|
2021-05-14 10:31:14 +00:00
|
|
|
text = request.form['text']
|
|
|
|
f = open('csv/scroll_text.csv', 'w+')
|
|
|
|
f.write(text)
|
|
|
|
f.close()
|
2021-05-21 10:20:39 +00:00
|
|
|
ticker.sendline('K')
|
2021-05-14 10:31:14 +00:00
|
|
|
ticker.sendline('T')
|
2021-08-18 19:50:02 +00:00
|
|
|
return index()
|
2021-05-14 10:31:14 +00:00
|
|
|
|
2021-05-27 19:10:57 +00:00
|
|
|
@app.route("/SetNews", methods=['POST'])
|
|
|
|
def SetNews():
|
|
|
|
text = request.form['text']
|
|
|
|
|
|
|
|
#args = ['q', 'sources', 'category', 'country']
|
|
|
|
args = ['q', 'category', 'country']
|
|
|
|
arg_dict = {}
|
|
|
|
lst = text.split(',')
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
for i, arg in enumerate(args):
|
|
|
|
if len(lst[i])>0:
|
|
|
|
arg_dict[arg] = lst[i]
|
|
|
|
|
2021-07-08 18:42:21 +00:00
|
|
|
|
2021-05-27 19:10:57 +00:00
|
|
|
|
|
|
|
json.dump( arg_dict, open( "csv/news_settings.json", 'w+' ))
|
2021-07-08 18:42:21 +00:00
|
|
|
|
2021-05-27 19:10:57 +00:00
|
|
|
except Exception as e:
|
|
|
|
#reset settings
|
2021-07-08 18:42:21 +00:00
|
|
|
|
2021-05-27 19:10:57 +00:00
|
|
|
f = open( "csv/news_settings.json", 'w+' )
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
api_caller.sendline('R')
|
|
|
|
|
2021-08-18 19:50:02 +00:00
|
|
|
return index()
|
2021-05-27 19:10:57 +00:00
|
|
|
|
2021-06-09 18:06:21 +00:00
|
|
|
@app.route("/SetWeather", methods=['POST'])
|
|
|
|
def SetWeather():
|
|
|
|
text = request.form['text']
|
|
|
|
|
|
|
|
#args = ['q', 'sources', 'category', 'country']
|
|
|
|
|
|
|
|
f = open( "csv/weather_location.txt", 'w+' )
|
|
|
|
f.write(text)
|
|
|
|
f.close()
|
|
|
|
api_caller.sendline('R')
|
|
|
|
|
2021-08-18 19:50:02 +00:00
|
|
|
return index()
|
2021-06-09 18:06:21 +00:00
|
|
|
|
2021-05-21 10:20:39 +00:00
|
|
|
@app.route("/DisplayGIF", methods=['POST'])
|
|
|
|
def DisplayGIF():
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
if 'file' not in request.files:
|
|
|
|
print('No file attached in request')
|
|
|
|
return hello()
|
|
|
|
fle = request.files['file']
|
|
|
|
if fle.filename == '':
|
|
|
|
print('No file selected')
|
|
|
|
return hello()
|
|
|
|
if fle:
|
2021-07-08 18:42:21 +00:00
|
|
|
|
2021-07-15 18:49:53 +00:00
|
|
|
filename = 'user_gif.gif'
|
2021-07-05 18:51:40 +00:00
|
|
|
fle.save(os.path.join(os.path.dirname(os.path.abspath(__file__)),os.path.join('display_images', filename)))
|
2021-05-21 10:20:39 +00:00
|
|
|
global LastCommand
|
|
|
|
LastCommand = 'Add a new logo file'
|
|
|
|
ticker.sendline('K')
|
|
|
|
ticker.sendline('G')
|
|
|
|
return hello()
|
2021-08-18 19:50:02 +00:00
|
|
|
return index()
|
2021-05-21 10:20:39 +00:00
|
|
|
|
2021-07-15 18:49:53 +00:00
|
|
|
@app.route("/DisplayImage", methods=['POST'])
|
|
|
|
def DisplayImage():
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
if 'file' not in request.files:
|
|
|
|
print('No file attached in request')
|
|
|
|
return hello()
|
|
|
|
fle = request.files['file']
|
|
|
|
if fle.filename == '':
|
|
|
|
print('No file selected')
|
|
|
|
return hello()
|
|
|
|
if fle:
|
|
|
|
|
|
|
|
filename = 'user_image.ppm'
|
|
|
|
fle.save(os.path.join(os.path.dirname(os.path.abspath(__file__)),os.path.join('display_images', filename)))
|
|
|
|
global LastCommand
|
|
|
|
LastCommand = 'Add a new logo file'
|
|
|
|
ticker.sendline('K')
|
|
|
|
ticker.sendline('I')
|
|
|
|
return hello()
|
2021-08-18 19:50:02 +00:00
|
|
|
return index()
|
2021-07-15 18:49:53 +00:00
|
|
|
|
2021-05-14 10:31:14 +00:00
|
|
|
|
2021-04-26 18:51:21 +00:00
|
|
|
@app.route("/Ticker", methods=['POST'])
|
|
|
|
def Ticker():
|
|
|
|
if request.method == 'POST':
|
|
|
|
if 'file' not in request.files:
|
|
|
|
print('No file attached in request')
|
|
|
|
return hello()
|
2021-05-21 10:20:39 +00:00
|
|
|
fle = request.files['file']
|
|
|
|
if fle.filename == '':
|
2021-04-26 18:51:21 +00:00
|
|
|
print('No file selected')
|
|
|
|
return hello()
|
2021-05-21 10:20:39 +00:00
|
|
|
if fle and allowed_file(fle.filename):
|
|
|
|
filename = secure_filename(fle.filename)
|
|
|
|
fle.save(os.path.join(CSV_FOLDER, filename))
|
2021-04-26 18:51:21 +00:00
|
|
|
process_file(os.path.join(CSV_FOLDER, filename), filename)
|
|
|
|
global LastCommand
|
|
|
|
LastCommand = 'Change CSV file'
|
|
|
|
return hello()
|
2021-08-18 19:50:02 +00:00
|
|
|
return index()
|
2021-04-26 18:51:21 +00:00
|
|
|
|
|
|
|
@app.route("/AddLogo", methods=['POST'])
|
|
|
|
def AddLogo():
|
|
|
|
if request.method == 'POST':
|
|
|
|
if 'file' not in request.files:
|
|
|
|
print('No file attached in request')
|
|
|
|
return hello()
|
2021-05-21 10:20:39 +00:00
|
|
|
fle = request.files['file']
|
|
|
|
if fle.filename == '':
|
2021-04-26 18:51:21 +00:00
|
|
|
print('No file selected')
|
|
|
|
return hello()
|
2021-05-21 10:20:39 +00:00
|
|
|
if fle and allowed_file(fle.filename):
|
|
|
|
filename = secure_filename(fle.filename)
|
|
|
|
fle.save(os.path.join(LOGO_FOLDER, filename))
|
2021-04-26 18:51:21 +00:00
|
|
|
global LastCommand
|
|
|
|
LastCommand = 'Add a new logo file'
|
|
|
|
return hello()
|
2021-08-18 19:50:02 +00:00
|
|
|
return index()
|
|
|
|
|
|
|
|
professional = False
|
2021-04-26 18:51:21 +00:00
|
|
|
|
2021-08-11 19:58:01 +00:00
|
|
|
|
|
|
|
@app.route("/start", methods = ['PUT', 'POST'])
|
|
|
|
def start():
|
2021-08-18 19:50:02 +00:00
|
|
|
print(professional)
|
2021-08-11 19:58:01 +00:00
|
|
|
data = str(request.data)
|
|
|
|
|
|
|
|
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']
|
2021-08-18 19:50:02 +00:00
|
|
|
|
|
|
|
if professional:
|
|
|
|
all_settings = ['Stocks', 'Crypto', 'Forex', 'Current Weather', 'News']
|
|
|
|
|
|
|
|
|
2021-08-11 19:58:01 +00:00
|
|
|
positions = []
|
|
|
|
display_settings = []
|
|
|
|
|
|
|
|
for setting in all_settings:
|
|
|
|
pos = data.find(setting)
|
|
|
|
if pos != -1:
|
2021-08-18 19:50:02 +00:00
|
|
|
if professional:
|
|
|
|
setting += ' Prof'
|
2021-08-11 19:58:01 +00:00
|
|
|
display_settings.append(setting)
|
|
|
|
positions.append(pos)
|
|
|
|
|
|
|
|
# sort settings by display
|
|
|
|
inds = np.argsort(positions)
|
|
|
|
display_settings = np.array(display_settings)[inds]
|
|
|
|
|
|
|
|
with open('csv/display_settings.json', 'w+') as f:
|
|
|
|
json.dump(list(display_settings), f)
|
|
|
|
|
|
|
|
ticker.sendline('K')
|
|
|
|
|
2021-08-18 19:50:02 +00:00
|
|
|
ticker.sendline('A')
|
|
|
|
return index()
|
|
|
|
|
|
|
|
@app.route("/display_format", methods = ['PUT', 'POST', 'GET'])
|
|
|
|
def display_format():
|
|
|
|
global professional
|
|
|
|
data = str(request.data)
|
|
|
|
if "Standard" in data:
|
|
|
|
professional = False
|
|
|
|
elif "Professional" in data:
|
|
|
|
professional = True
|
|
|
|
|
|
|
|
print(professional)
|
|
|
|
return index()
|
|
|
|
|
2021-09-16 19:23:04 +00:00
|
|
|
@app.route("/feature_settings", methods = ['PUT', 'POST', 'GET'])
|
|
|
|
def feature_settings():
|
|
|
|
|
|
|
|
data = str(request.data)
|
|
|
|
print('feature settings')
|
|
|
|
print(data)
|
|
|
|
return index()
|
|
|
|
|
|
|
|
|
2021-08-11 19:58:01 +00:00
|
|
|
@app.route("/stop")
|
|
|
|
def stop():
|
2021-08-18 19:50:02 +00:00
|
|
|
print('stop')
|
2021-08-11 19:58:01 +00:00
|
|
|
ticker.sendline('K')
|
2021-08-18 19:50:02 +00:00
|
|
|
return index()
|
2021-08-11 19:58:01 +00:00
|
|
|
|
|
|
|
@app.route("/shutdown")
|
|
|
|
def shutdown():
|
|
|
|
os.system("sudo shutdown now")
|
2021-08-18 19:50:02 +00:00
|
|
|
return index()
|
2021-08-11 19:58:01 +00:00
|
|
|
|
|
|
|
@app.route("/matrix")
|
2021-04-26 18:51:21 +00:00
|
|
|
def matrix():
|
2021-08-11 19:58:01 +00:00
|
|
|
|
2021-05-21 13:24:37 +00:00
|
|
|
if "Run Stocks" in request.form:
|
|
|
|
ticker.sendline('K')
|
2021-05-03 10:39:31 +00:00
|
|
|
ticker.sendline('S')
|
2021-07-06 19:15:05 +00:00
|
|
|
elif "Run Crypto" in request.form:
|
|
|
|
ticker.sendline('K')
|
|
|
|
ticker.sendline('C')
|
2021-05-01 10:39:20 +00:00
|
|
|
|
2021-07-06 19:15:05 +00:00
|
|
|
elif "Run Forex" in request.form:
|
|
|
|
ticker.sendline('K')
|
2021-07-08 16:48:03 +00:00
|
|
|
ticker.sendline('F')
|
2021-05-01 10:39:20 +00:00
|
|
|
|
2021-06-07 20:03:03 +00:00
|
|
|
elif "Run News" in request.form:
|
2021-07-06 19:15:05 +00:00
|
|
|
|
2021-05-21 13:24:37 +00:00
|
|
|
ticker.sendline('K')
|
|
|
|
ticker.sendline('N')
|
|
|
|
|
2021-06-07 20:03:03 +00:00
|
|
|
elif "Run Weather" in request.form:
|
|
|
|
|
|
|
|
|
|
|
|
ticker.sendline('K')
|
|
|
|
ticker.sendline('W')
|
|
|
|
|
|
|
|
elif "Run Daily Weather" in request.form:
|
|
|
|
|
|
|
|
ticker.sendline('K')
|
|
|
|
ticker.sendline('D')
|
|
|
|
|
2021-06-19 09:53:09 +00:00
|
|
|
elif "Past NHL" in request.form:
|
|
|
|
ticker.sendline('K')
|
|
|
|
ticker.sendline('P')
|
|
|
|
|
|
|
|
elif "Future NHL" in request.form:
|
|
|
|
ticker.sendline('K')
|
2021-07-08 16:48:03 +00:00
|
|
|
ticker.sendline('l')
|
2021-06-19 09:53:09 +00:00
|
|
|
|
|
|
|
elif "Live NHL" in request.form:
|
2021-06-15 18:35:04 +00:00
|
|
|
ticker.sendline('K')
|
|
|
|
ticker.sendline('L')
|
|
|
|
|
2021-06-19 09:53:09 +00:00
|
|
|
elif "Premier league table" in request.form:
|
2021-07-08 18:42:21 +00:00
|
|
|
|
2021-06-15 18:35:04 +00:00
|
|
|
ticker.sendline('K')
|
|
|
|
ticker.sendline('t')
|
|
|
|
|
2021-07-14 17:02:54 +00:00
|
|
|
elif "Professional" in request.form:
|
|
|
|
|
|
|
|
ticker.sendline('K')
|
|
|
|
ticker.sendline('b')
|
|
|
|
|
2021-06-21 16:39:44 +00:00
|
|
|
elif "All" in request.form:
|
2021-07-08 18:42:21 +00:00
|
|
|
|
2021-06-21 16:39:44 +00:00
|
|
|
ticker.sendline('K')
|
|
|
|
ticker.sendline('A')
|
|
|
|
|
2021-07-27 17:33:02 +00:00
|
|
|
elif "Multiple" in request.form:
|
|
|
|
|
|
|
|
ticker.sendline('K')
|
|
|
|
ticker.sendline('+')
|
|
|
|
|
2021-06-07 20:03:03 +00:00
|
|
|
elif "Stop Display" in request.form:
|
2021-07-06 19:15:05 +00:00
|
|
|
|
2021-07-08 18:42:21 +00:00
|
|
|
|
|
|
|
LastCommand = 'Stop display at next checkpoint'
|
|
|
|
ticker.sendline('K')
|
|
|
|
|
2021-05-21 13:24:37 +00:00
|
|
|
elif "Shutdown the pi" in request.form:
|
2021-06-07 20:03:03 +00:00
|
|
|
|
2021-04-26 18:51:21 +00:00
|
|
|
try:
|
2021-05-01 11:08:15 +00:00
|
|
|
os.system("sudo shutdown now")
|
2021-04-26 18:51:21 +00:00
|
|
|
except:
|
|
|
|
print("couldn't shutdown")
|
2021-08-18 19:50:02 +00:00
|
|
|
return index()
|
2021-04-26 18:51:21 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-05-31 11:22:56 +00:00
|
|
|
|
|
|
|
app.run(host='0.0.0.0', port=1024, debug=False) # the debuggger causes flickering
|
|
|
|
|
2021-04-26 19:27:34 +00:00
|
|
|
#sudo ./demo -D1 final.ppm -t 50 -m 25 --led-gpio-mapping=adafruit-hat --led-rows=32 --led-cols=256 --led-slowdown-gpio=4
|