Repository navigation
Expand file tree
/
Copy pathZettaView.py
More file actions
executable file
·152 lines (120 loc) · 5.08 KB
/
Copy pathZettaView.py
File metadata and controls
executable file
·152 lines (120 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
""" ZettaView: A simple HTML Log/Sequencer viewer for RCS Zetta """
__product__ = "ZettaView"
__version__ = "1.0.0"
__author__ = "Anthony Eden"
__copyright__ = "Copyright 2018, Media Realm"
__url__ = "https://mediarealm.com.au/"
import sys, os
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/libs")
from flask import Flask
from flask import render_template
from flask import jsonify
import ZettaStatusFeed
import datetime
import time
import json
base_dir = '.'
if hasattr(sys, '_MEIPASS'):
base_dir = os.path.join(sys._MEIPASS)
template_folder = os.path.join(base_dir, 'templates')
static_folder = os.path.join(base_dir, 'static')
app = Flask(__name__, template_folder = template_folder, static_folder = static_folder)
class DateTimeEncoder(json.JSONEncoder):
# From https://stackoverflow.com/a/27058505
def default(self, o):
if isinstance(o, datetime.datetime):
return o.isoformat()
return json.JSONEncoder.default(self, o)
@app.before_first_request
def connect_statusfeed():
# Connect to the status feed
ZettaStatusFeed.connect(CONFIG['ZettaServer'])
@app.before_first_request
def get_stations():
app.stations = ZettaStatusFeed.listStations()
for station, stationData in enumerate(app.stations):
app.stations[station]['Data'] = {}
app.stations[station]['DataLastUpdated'] = 0
app.stations[station]['gapTimeLastValue'] = 0
app.stations[station]['gapTimeCountdownTarget'] = None
app.stations[station]['gapTimeCountdownTargetFormatted'] = None
@app.route('/')
def view_stations():
# Get a list of stations and render a list as HTML
stations = []
for station in app.stations:
stations.append({
"Name": station['Station']['Name'],
"ID": station['Station']['ID'],
})
return render_template('stations.html', stations=stations)
@app.route('/view/<station_id>/')
def view_station(station_id=None):
# Having selected a station, render the log/sequencer viewer
return render_template('station.html', station_id=station_id)
@app.route('/data/<station_id>/')
def data_station(station_id=None):
# Generate and send the current data for this station
station = None
station_index = None
for stationi, station_data in enumerate(app.stations):
if str(station_data['Station']['ID']) == str(station_id):
station = station_data
station_index = stationi
break
if station is None:
return app.response_class(
response=json.dumps({"Error": "Station not found"}, cls=DateTimeEncoder),
status=404,
mimetype='application/json'
)
timingData = ZettaStatusFeed.stationTiming(station_id)
nextStop = ZettaStatusFeed.stationQueueNextStop(station_id)
if nextStop is not None:
nextStop = nextStop.strftime("%Y-%m-%d %H:%M:%S")
else:
nextStop = None
etm = timingData['GapTimeTarget']
if etm is not None:
etm = etm.strftime("%Y-%m-%d %H:%M:%S")
else:
etm = None
if station['gapTimeLastValue'] != timingData['GapTimeInSeconds'] and timingData['GapTimeInSeconds'] is not None:
print "Update gap time"
app.stations[station_index]['gapTimeLastValue'] = timingData['GapTimeInSeconds']
app.stations[station_index]['gapTimeCountdownTarget'] = datetime.datetime.now() - datetime.timedelta(seconds = timingData['GapTimeInSeconds'])
app.stations[station_index]['gapTimeCountdownTargetFormatted'] = app.stations[station_index]['gapTimeCountdownTarget'].strftime("%Y-%m-%d %H:%M:%S")
eventQueue = ZettaStatusFeed.stationQueue(station_id)
currentEvent = eventQueue[0]['Event']
currentEventId = currentEvent['EventID']
data = {
"station_id": station_id,
"stationName": station['Station']['Name'],
"status": ZettaStatusFeed.stationStatus(station_id),
"mode": ZettaStatusFeed.stationMode(station_id),
"nextStop": nextStop,
"gapTime": timingData['GapTimeInSeconds'],
"gapTimeCountdownTarget": station['gapTimeCountdownTargetFormatted'],
"etm": etm,
"eventQueue": eventQueue,
}
station['DataLastUpdated'] = time.time()
station['Data'] = data
return app.response_class(
response=json.dumps(data, cls=DateTimeEncoder),
status=200,
mimetype='application/json'
)
if __name__ == '__main__':
if getattr(sys, 'frozen', False):
application_path = os.path.dirname(sys.executable)
elif __file__:
application_path = os.path.dirname(__file__)
config_filename = os.path.join(application_path, "config.json")
try:
Config_JSON = open(config_filename).read()
CONFIG = json.loads(Config_JSON)
except Exception, e:
print "ERROR LOADING CONFIG.JSON:", e
else:
app.run(host = '0.0.0.0', port = CONFIG['LocalPortNumber'])