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
|
|
|
|
from multiprocessing import Process
|
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
#stock_ticker = StockTicker()
|
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-05-01 10:39:20 +00:00
|
|
|
#ticker.stdin.write(b'from server')
|
|
|
|
#print(ticker.stdout.readlines())
|
|
|
|
#(output, errs)
|
2021-05-03 10:39:31 +00:00
|
|
|
ticker = pexpect.spawn("sudo -E python3 stockTicker.py")
|
2021-05-01 10:39:20 +00:00
|
|
|
|
|
|
|
#stock_ticker = StockTicker()
|
|
|
|
|
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-01 11:08:15 +00:00
|
|
|
|
|
|
|
|
2021-04-26 18:51:21 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
@app.route("/", methods=['GET', 'POST'])
|
|
|
|
def hello():
|
|
|
|
global command
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
timeString = now.strftime("%Y-%m-%d %H:%M")
|
|
|
|
LogoList = os.listdir('logos')
|
|
|
|
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():
|
|
|
|
global command
|
|
|
|
command = request.form['text']
|
|
|
|
print(command)
|
|
|
|
global LastCommand
|
|
|
|
LastCommand = 'Change runtime'
|
|
|
|
return hello()
|
|
|
|
|
|
|
|
@app.route("/Delay", methods=['POST'])
|
|
|
|
def Delay():
|
|
|
|
global DelayTime
|
|
|
|
DelayTime = request.form['text']
|
|
|
|
print(DelayTime)
|
|
|
|
global LastCommand
|
|
|
|
LastCommand = 'Change Delay'
|
|
|
|
return hello()
|
|
|
|
|
|
|
|
@app.route("/Speed", methods=['POST'])
|
|
|
|
def Speed():
|
|
|
|
global speedTime
|
|
|
|
speedTime = request.form['text']
|
|
|
|
print(speedTime)
|
2021-05-03 10:39:31 +00:00
|
|
|
ticker.sendline(speedTime)
|
2021-04-26 18:51:21 +00:00
|
|
|
global LastCommand
|
|
|
|
LastCommand = 'Change Speed'
|
|
|
|
return hello()
|
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-05-03 10:39:31 +00:00
|
|
|
print(brightness)
|
2021-05-04 21:49:33 +00:00
|
|
|
ticker.sendline(str(brightness))
|
2021-05-03 10:39:31 +00:00
|
|
|
global LastCommand
|
|
|
|
LastCommand = 'Change Brightness'
|
|
|
|
return hello()
|
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()
|
|
|
|
file = request.files['file']
|
|
|
|
if file.filename == '':
|
|
|
|
print('No file selected')
|
|
|
|
return hello()
|
|
|
|
if file and allowed_file(file.filename):
|
|
|
|
filename = secure_filename(file.filename)
|
|
|
|
file.save(os.path.join(CSV_FOLDER, filename))
|
|
|
|
process_file(os.path.join(CSV_FOLDER, filename), filename)
|
|
|
|
global LastCommand
|
|
|
|
LastCommand = 'Change CSV file'
|
|
|
|
return hello()
|
|
|
|
return hello()
|
|
|
|
|
|
|
|
@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()
|
|
|
|
file = request.files['file']
|
|
|
|
if file.filename == '':
|
|
|
|
print('No file selected')
|
|
|
|
return hello()
|
|
|
|
if file and allowed_file(file.filename):
|
|
|
|
filename = secure_filename(file.filename)
|
|
|
|
file.save(os.path.join(LOGO_FOLDER, filename))
|
|
|
|
global LastCommand
|
|
|
|
LastCommand = 'Add a new logo file'
|
|
|
|
return hello()
|
|
|
|
return hello()
|
|
|
|
|
|
|
|
@app.route("/matrix", methods=['POST'])
|
|
|
|
def matrix():
|
|
|
|
global LastCommand
|
|
|
|
if "Run Display" in request.form:
|
|
|
|
pass
|
2021-05-01 10:39:20 +00:00
|
|
|
print('run display')
|
2021-04-28 19:39:30 +00:00
|
|
|
#stock_ticker.runStockTicker(command, DelayTime, speedTime)
|
2021-05-01 10:39:20 +00:00
|
|
|
#stock_ticker.displayMatrix()
|
|
|
|
#os.system("sudo -E python3 test.py")
|
|
|
|
#
|
|
|
|
#child.sendline('sent from server')
|
|
|
|
|
|
|
|
#process = Popen(["sudo", "-E", "python3", "stockTicker.py"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
2021-05-03 10:39:31 +00:00
|
|
|
ticker.sendline('S')
|
2021-05-01 10:39:20 +00:00
|
|
|
|
|
|
|
print('back in flask')
|
|
|
|
|
|
|
|
|
2021-04-26 18:51:21 +00:00
|
|
|
LastCommand = 'Run display'
|
|
|
|
elif "Stop Display (at next refresh)" in request.form:
|
|
|
|
pass
|
2021-05-01 10:39:20 +00:00
|
|
|
#ticker.kill()
|
2021-04-26 18:51:21 +00:00
|
|
|
try:
|
2021-05-01 10:39:20 +00:00
|
|
|
#stock_ticker.stopStockTicker()
|
2021-04-26 18:51:21 +00:00
|
|
|
LastCommand = 'Stop display at next checkpoint'
|
2021-05-03 10:39:31 +00:00
|
|
|
ticker.sendline('K')
|
2021-04-26 18:51:21 +00:00
|
|
|
except:
|
|
|
|
print("none running")
|
|
|
|
elif "Shutdown the pi" in request.form:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
LastCommand = 'shutdown'
|
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")
|
|
|
|
return hello()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-05-01 10:39:20 +00:00
|
|
|
app.run(host='0.0.0.0', port=1024, debug=False) # the debuggger causes flickering
|
2021-04-26 18:51:21 +00:00
|
|
|
|
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
|