Skip to content

Commit 0a490df

Browse files
committed
webserver now shares current storage activity status
1 parent 17822b3 commit 0a490df

4 files changed

Lines changed: 23 additions & 4 deletions

File tree

‎data-cloning/RedisStorage.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# HTTP Server host
22
import threading
33
from flask import Flask, abort
4+
from flask_cors import CORS
45
# Data Retrieval
56
import redis
67
import json, sys, sqlite3
@@ -45,6 +46,7 @@
4546

4647
# Flask app settings
4748
app = Flask(__name__)
49+
CORS(app)
4850

4951
# Host redis process location, contains data to collect
5052
# Use the Raspberry Pi IP, should be static
@@ -179,17 +181,23 @@ def WriteEventData(newFolderName):
179181
event_writer.writerow(event_data_row)
180182
print('EVENT DATA SAVED')
181183

184+
STORAGE_STATUS = False
185+
182186
@app.route('/serial/storage/<action>')
183187
def ReplicationControl(action):
188+
global STORAGE_STATUS
189+
184190
if action == 'START':
185191
print('REDIS REPLICATION START')
186192
Replicate()
193+
STORAGE_STATUS = True
187194
# Stop the storage loop
188195
return('REDIS REPLICATION STARTED')
189196

190197
if action == 'CLOSE':
191198
print('REDIS REPLICATION STOP')
192199
StopReplicate()
200+
STORAGE_STATUS = False
193201
# Start the infinite loop of pulling and storing data
194202
return('REDIS REPLICATION STOPPED')
195203

@@ -198,6 +206,10 @@ def ReplicationControl(action):
198206
fileName = Write()
199207
return('REDIS PERSISTED TO ' + fileName)
200208

209+
if action == 'STATUS':
210+
print('STATUS UPDATE REQUESTED')
211+
return str(STORAGE_STATUS)
212+
201213
return(404)
202214

203215
if __name__ == "__main__":

‎data-cloning/requirements.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ Jinja2==2.11.3
55
MarkupSafe==1.1.1
66
pyserial==3.5
77
redis==4.2.1
8-
Werkzeug==1.0.1
8+
Werkzeug==1.0.1
9+
flask-cors

‎data-socket/UIDataWebsocket.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ async def producer_handler(websocket, path):
4545
while True:
4646
# Package sensor and valve data
4747
# print("Looping...")
48+
# Debug limiter on loop speed:
49+
# await asyncio.sleep(.1)
4850
try:
4951
# From the sensor producer
5052
sensor_message = await sensor_producer(Loop_starter)
53+
# print(sensor_message)
5154
# And the valve producer
5255
# valve_message = await valve_producer(Loop_starter)
5356
# After the initial loop, change the condition
@@ -72,8 +75,9 @@ async def producer_handler(websocket, path):
7275
await websocket.ping()
7376
else:
7477
# Send the messages
78+
# print(sensor_message)
7579
await websocket.send(json.dumps({'message': sensor_message}))
76-
# Don't overload the websocket
80+
# Don't overload the websocket (race condition?)
7781
await asyncio.sleep(.1)
7882
# Confirm reception of the data frame
7983
response = await websocket.recv()
@@ -127,6 +131,7 @@ async def sensor_producer(Loop_starter):
127131

128132
try:
129133
if Loop_starter == 0:
134+
print("initial loop")
130135
# Grab the first item to establish the range for XREAD
131136
sensor_data = redis.xrange(sensor_stream_name, count=1)
132137
(sensor_label,sensor_data) = sensor_data[0]
@@ -140,10 +145,11 @@ async def sensor_producer(Loop_starter):
140145
sensor_data = redis.xread({ sensor_stream_name: f'{sensor_label.decode()}'}, block=1)
141146
(sensor_label,sensor_data) = sensor_data[0]
142147
except IndexError:
148+
# There is no new data to push
143149
# print("INDEX ERROR")
144150
return []
145151
except NameError:
146-
# print("Didn't initally loop")
152+
print("Didn't initally loop")
147153
return []
148154
except ws.exceptions.ConnectionClosed:
149155
print("Connection Closed")

‎docker-compose.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ services:
2626

2727
redis:
2828
container_name: redis-db
29-
image: redis
29+
image: redis:7.0.5
3030
ports:
3131
- 6379:6379

0 commit comments

Comments
 (0)