Skip to content

Commit

Permalink
Add START/STOP Button to iSpindel graphing sessions (#219)
Browse files Browse the repository at this point in the history
* Adds iSpindel support for the server. iSpindel Server Address should be configured for picobrew.com and Path/URI to /API/iSpindel

* Update session_parser.py

Removed unnecessary comment

* Update config.example.yaml

Fixed typo

* Update config.py

Fixed all caps on ZYMATIC

* Updating to latest master from chiefwgms

* Minor fixes

* Update routes_frontend for latest files

* Fxing iSpindel_graph_socketio javascript call since it exists in index.html

* Updating the iSpindel file error handling to be similar to ferm. Fixing str type declaration on iSpindle ID.

* Update model.py

* Update model.py

* Adding iSpindel START/STOP button

* Adding iSpindel START/STOP to server

* Updated iSpindel START/STOP PR with changes based on review

* Update app/main/routes_frontend.py

Co-authored-by: Pete <[email protected]>
  • Loading branch information
BuckoWA and chiefwigms authored Jan 27, 2021
1 parent d230496 commit 5073c38
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 39 deletions.
1 change: 1 addition & 0 deletions app/main/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def __init__(self):
self.file = None
self.filepath = None
self.alias = ''
self.active = False
self.uninit = True
self.voltage = '-'
self.start_time = None
Expand Down
7 changes: 6 additions & 1 deletion app/main/routes_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def index():
iSpindel_sessions=load_active_iSpindel_sessions())



@main.route('/brew_history')
def brew_history():
return render_template_with_defaults('brew_history.html', sessions=load_brew_sessions(), invalid=get_invalid_sessions('brew'))
Expand Down Expand Up @@ -173,14 +174,16 @@ def update_device_session(uid, session_type):
update = request.get_json()
if session_type == 'ferm':
session = active_ferm_sessions[uid]
elif session_type == 'iSpindel':
session = active_iSpindel_sessions[uid]

if update['active'] == False:
session.active = False
if session.file != None:
session.file.seek(0, os.SEEK_END)
if session.file.tell() > 0:
# mark for completion and archive session file
session.file.seek(session.file.tell() - 1, os.SEEK_SET) # Remove trailing , from last data set
session.file.seek(session.file.tell() - 1, os.SEEK_SET) # Remove trailing comma from last data set
session.file.write('\n]')
session.cleanup()
else:
Expand Down Expand Up @@ -514,6 +517,8 @@ def load_active_iSpindel_sessions():
iSpindel_sessions = []
for uid in active_iSpindel_sessions:
iSpindel_sessions.append({'alias': active_iSpindel_sessions[uid].alias,
'uid' : uid,
'active': active_iSpindel_sessions[uid].active,
'graph': get_iSpindel_graph_data(uid, active_iSpindel_sessions[uid].voltage,
active_iSpindel_sessions[uid].data)})
return iSpindel_sessions
Expand Down
58 changes: 32 additions & 26 deletions app/main/routes_iSpindel_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,41 @@ def process_iSpindel_data():
data = request.get_json()
uid = str(data['ID'])

if uid not in active_iSpindel_sessions or active_iSpindel_sessions[uid].uninit:
if (uid not in active_iSpindel_sessions or active_iSpindel_sessions[uid].uninit) and active_iSpindel_sessions[uid].active:
create_new_session(uid)

time = ((datetime.utcnow() - datetime(1970, 1, 1)).total_seconds() * 1000)
session_data = []
log_data = ''
point = {
'time': time,
'temp': data['temperature'],
'gravity': data['gravity'],
}
if active_iSpindel_sessions[uid].active:
time = ((datetime.utcnow() - datetime(1970, 1, 1)).total_seconds() * 1000)
session_data = []
log_data = ''
point = {
'time': time,
'temp': data['temperature'],
'gravity': data['gravity'],
}

session_data.append(point)
log_data += '\t{},\n'.format(json.dumps(point))

active_iSpindel_sessions[uid].data.extend(session_data)
active_iSpindel_sessions[uid].voltage = str(data['battery']) + 'V'

graph_update = json.dumps({'voltage': data['battery'], 'data': session_data})
socketio.emit('iSpindel_session_update|{}'.format(data['ID']), graph_update)

if (datetime.now().date() - active_iSpindel_sessions[uid].start_time.date()).days > 14:
active_iSpindel_sessions[uid].file.write('{}\n]\n'.format(log_data[:-2]))
active_iSpindel_sessions[uid].cleanup()
return ('', 200)
session_data.append(point)
log_data += '\n\t{},'.format(json.dumps(point))

active_iSpindel_sessions[uid].data.extend(session_data)
active_iSpindel_sessions[uid].voltage = str(data['battery']) + 'V'

graph_update = json.dumps({'voltage': data['battery'], 'data': session_data})
socketio.emit('iSpindel_session_update|{}'.format(data['ID']), graph_update)


# end fermentation only when user specifies fermentation is complete
if (active_iSpindel_sessions[uid].uninit == False and active_iSpindel_sessions[uid].active == False):
active_iSpindel_sessions[uid].file.write('{}\n\n]'.format(log_data[:-2]))
active_iSpindel_sessions[uid].cleanup()
return('', 200)
else:
active_iSpindel_sessions[uid].active = True
active_iSpindel_sessions[uid].file.write(log_data)
active_iSpindel_sessions[uid].file.flush()
return('', 200)
else:
active_iSpindel_sessions[uid].file.write(log_data)
active_iSpindel_sessions[uid].file.flush()
return ('', 200)
return('', 200)

# -------- Utility --------
def create_new_session(uid):
Expand All @@ -68,4 +74,4 @@ def create_new_session(uid):
active_iSpindel_sessions[uid].start_time = datetime.now() # Not now, but X samples * 60*RATE sec ago
active_iSpindel_sessions[uid].filepath = iSpindel_active_sessions_path().joinpath('{0}#{1}.json'.format(active_iSpindel_sessions[uid].start_time.strftime('%Y%m%d_%H%M%S'), uid))
active_iSpindel_sessions[uid].file = open(active_iSpindel_sessions[uid].filepath, 'w')
active_iSpindel_sessions[uid].file.write('[\n')
active_iSpindel_sessions[uid].file.write('[')
5 changes: 3 additions & 2 deletions app/main/routes_picoferm_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,13 @@ def process_log_ferm_dataset(args):
}
session_data.append(point)
time = time + time_delta
log_data += '\t{},\n'.format(json.dumps(point))
log_data += '\n\t{},'.format(json.dumps(point))
active_ferm_sessions[uid].data.extend(session_data)
active_ferm_sessions[uid].voltage = str(args['voltage']) + 'V'
graph_update = json.dumps({'voltage': args['voltage'], 'data': session_data})
socketio.emit('ferm_session_update|{}'.format(args['uid']), graph_update)


# end fermentation only when user specifies fermentation is complete
if active_ferm_sessions[uid].uninit == False and active_ferm_sessions[uid].active == False:
active_ferm_sessions[uid].file.write('{}\n\n]'.format(log_data[:-2]))
Expand All @@ -139,4 +140,4 @@ def create_new_session(uid):
active_ferm_sessions[uid].start_time = datetime.now() # Not now, but X samples * 60*RATE sec ago
active_ferm_sessions[uid].filepath = ferm_active_sessions_path().joinpath('{0}#{1}.json'.format(active_ferm_sessions[uid].start_time.strftime('%Y%m%d_%H%M%S'), uid))
active_ferm_sessions[uid].file = open(active_ferm_sessions[uid].filepath, 'w')
active_ferm_sessions[uid].file.write('[\n')
active_ferm_sessions[uid].file.write('[')
14 changes: 7 additions & 7 deletions app/main/session_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,27 +307,27 @@ def restore_active_iSpindel_sessions():
active_iSpindel_session_files = list(iSpindel_active_sessions_path().glob(file_glob_pattern))
for file in active_iSpindel_session_files:
# print('DEBUG: restore_active_sessions() found {} as an active session'.format(file))
ferm_session = load_iSpindel_session(file)
iSpindel_session = load_iSpindel_session(file)
# print('DEBUG: restore_active_sessions() {}'.format(ferm_session))
uid = ferm_session['uid']
uid = iSpindel_session['uid']
if uid not in active_iSpindel_sessions:
active_iSpindel_sessions[uid] = iSpindelSession()

session = active_iSpindel_sessions[uid]
session.file = open(file, 'a')
session.file.flush()
session.filepath = file
session.start_time = datetime.strptime(ferm_session['date'], '%Y%m%d_%H%M%S')
session.start_time = datetime.strptime(iSpindel_session['date'], '%Y%m%d_%H%M%S')
session.active = True

session.uninit = False
session.data = ferm_session['data']
session.graph = ferm_session['graph']
session.data = iSpindel_session['data']
session.graph = iSpindel_session['graph']

active_iSpindel_sessions[uid] = session


def restore_active_sessions():
# initialize active sessions during start up
restore_active_brew_sessions()
restore_active_ferm_sessions()
restore_active_iSpindel_sessions()
restore_active_iSpindel_sessions()
2 changes: 1 addition & 1 deletion app/static/js/iSpindel_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Highcharts.chart(graph_data.chart_id, {
title: {
text: 'Specific Gravity'
},
min: 0.9,
min: 1.0,
//max: 1.1
opposite: true
}],
Expand Down
2 changes: 1 addition & 1 deletion app/static/js/iSpindel_graph_socketio.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ yAxis: [
title: {
text: 'Specific Gravity'
},
min: 0.9,
min: 1.0,
//max: 1.1
opposite: true
}],
Expand Down
52 changes: 51 additions & 1 deletion app/static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,54 @@ function start_fermentation(uid) {
//setTimeout(function () { window.location.href = "pico_recipes";}, 2000);
},
});
}
}

function start_iSpindel_fermentation(uid) {
var session = {}
session.active = true
$.ajax({
url: '/device/' + uid + '/sessions/iSpindel',
type: 'PUT',
data: JSON.stringify(session),
dataType: "json",
processData: false,
contentType: "application/json; charset=UTF-8",
success: function (data) {
showAlert("Success!", "success");

$("#bstart_" + uid).toggleClass('d-block d-none')
$("#bstop_" + uid).toggleClass('d-block d-none')
// setTimeout(function () { window.location.href = "/"; }, 2000);
},
error: function (request, status, error) {
showAlert("Error: " + request.responseText, "danger");
//setTimeout(function () { window.location.href = "pico_recipes";}, 2000);
},
});

};

function stop_iSpindel_fermentation(uid) {
var session = {}
session.active = false
$.ajax({
url: '/device/' + uid + '/sessions/iSpindel',
type: 'PUT',
data: JSON.stringify(session),
dataType: "json",
processData: false,
contentType: "application/json; charset=UTF-8",
success: function (data) {
showAlert("Success!", "success");
$("#bstart_" + uid).toggleClass('d-block d-none')
$("#bstop_" + uid).toggleClass('d-block d-none')

// until socketio publishes a new "empty" state just force a refresh (which will clear the graphs)
setTimeout(function () { window.location.href = "/"; }, 2000);
},
error: function (request, status, error) {
showAlert("Error: " + request.responseText, "danger");
//setTimeout(function () { window.location.href = "pico_recipes";}, 2000);
},
});
}
19 changes: 19 additions & 0 deletions app/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ <h5 class="card-header" id="h_{{iSpindel_session.graph.chart_id}}">
<img src="/static/img/tilt.svg" atl="tilt or ispindle">
</span>
{% if iSpindel_session.alias is defined and iSpindel_session.alias|length %}{{iSpindel_session.alias}}{% else %}{{iSpindel_session.graph.chart_id}}{% endif %}
{% if iSpindel_session.active %}
<button class="fermentation-start d-none btn btn-sm btn-success float-right mr-5" type="button" id="bstart_{{iSpindel_session.uid}}"
onclick="event.stopPropagation();event.preventDefault();start_iSpindel_fermentation('{{iSpindel_session.uid}}');">
<i class="fas fa-play-circle"></i> Start Fermentation
</button>
<button class="fermentation-stop d-block btn btn-sm btn-danger float-right mr-5" type="button" id="bstop_{{iSpindel_session.uid}}"
onclick="event.stopPropagation();event.preventDefault();stop_iSpindel_fermentation('{{iSpindel_session.uid}}');">
<i class="fas fa-stop-circle"></i> Stop Fermentation
</button>
{% else %}
<button class="fermentation-start d-block btn btn-sm btn-success float-right mr-5" type="button" id="bstart_{{iSpindel_session.uid}}"
onclick="event.stopPropagation();event.preventDefault();start_iSpindel_fermentation('{{iSpindel_session.uid}}');">
<i class="fas fa-play-circle"></i> Start Fermentation
</button>
<button class="fermentation-stop d-none btn btn-sm btn-danger float-right mr-5" type="button" id="bstop_{{iSpindel_session.uid}}"
onclick="event.stopPropagation();event.preventDefault();stop_iSpindel_fermentation('{{iSpindel_session.uid}}');">
<i class="fas fa-stop-circle"></i> Stop Fermentation
</button>
{% endif %}
</a>
</h5>
<div id="c_{{iSpindel_session.graph.chart_id}}" class="collapse show" aria-labelledby="h_{{iSpindel_session.graph.chart_id}}">
Expand Down

0 comments on commit 5073c38

Please sign in to comment.