Commit ac18fafa authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

automations integrated and tested

parent 2138bc35
......@@ -24,7 +24,7 @@
<h3 class="mb-0">Automation</h3>
</div>
<div class="card-body">
{% if alert_list %}
{% if automations %}
<table class="table">
<thead>
<th> Name </th>
......
......@@ -16,7 +16,7 @@
<div class="col">
<div class="card shadow">
<div class="card-header bg-transparent">
<h3 class="mb-0">Applications</h3>
<h3 class="mb-0">New Alert</h3>
</div>
<div class="card-body">
<form action="/application/{{ app[1] }}/new-alert" method="post">
......@@ -46,14 +46,15 @@
</select>
<br>
<input type='text' class='form-control notifelem' name='avalue' id='avalue' placeholder='Value' title='use n (e.g. 5) for integer notation, n.n (e.g. 5.0) for float, and true or false for boolean' required>
<p> Use n (e.g. 5) for integer notation, n.n (e.g. 5.0) for float, and true or false for boolean </p>
<p> Use n (e.g. 5) for integer notation, n.n (e.g. 5.0) for float, and true or false for boolean. <br>
Keep types consistently for each variable for both, alerts and automations. </p>
<br>
<h4> THEN </h4>
<h5> Send email to: </h5>
<input class="form-control" type="email" id="alertemail" name="alertemail" placeholder="Type email..." required>
<br>
<br><br>
<div class="form-group">
<button type="submit" id="submit" class="btn btn-primary">Create Alert</button>
</div>
......
{% extends 'logged_layout.html' %}
{% block title %} HPC&amp;A IoT - New Automation {% endblock %}
{% block location %}
<a class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block" href="/applications">Applications</a>
<p class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block"> &nbsp;-&nbsp; </p>
<a class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block" href="/application/{{ app[1] }}">{{ app[0] }}</a>
{% endblock %}
{% block body %}
<!-- Page content -->
<div class="container-fluid mt--7">
<!-- Table -->
<div class="row">
<div class="col">
<div class="card shadow">
<div class="card-header bg-transparent">
<h3 class="mb-0">New Automation</h3>
</div>
<div class="card-body">
<form action="/application/{{ app[1] }}/new-automation" method="post">
<div class="form-group">
<label>Name:</label><br>
<input type="text" maxlength="30" class="form-control" id="automationname" name="automationname" required><br>
</div>
<h3> Condition: </h3>
<h4> IF </h4>
<select class="form-control" id="devid" name="devid" onchange="return ondev();" required>
<option default value="-">Select Device</option>
{% for d in devs %}
<option value="{{ d[1] }}">{{ d[0] }}</option>
{% endfor %}
</select>
<br>
<select class="form-control" id="varname" name="varname" onchange="return validate_form();" required>
<option value="-">Select variable</option>
</select>
<br>
<select class='form-control' name='operation' id='operation' required>
<option default>></option>
<option>>=</option>
<option><</option>
<option><=</option>
<option>=</option>
</select>
<br>
<input type='text' class='form-control' name='avalue' id='avalue' placeholder='Value' title='use n (e.g. 5) for integer notation, n.n (e.g. 5.0) for float, and true or false for boolean' required>
<p> Use n (e.g. 5) for integer notation, n.n (e.g. 5.0) for float, and true or false for boolean. <br>
Keep types consistently for each variable for both, alerts and automations. </p>
<br>
<h4> THEN </h4>
<h5> Set config on </h5>
<select class="form-control" id="adevid" onchange="return validate_form();" name="adevid" required>
<option default value="-">Select Device</option>
{% for d in devs %}
<option value="{{ d[1] }}">{{ d[0] }}</option>
{% endfor %}
</select>
<br>
<input type='number' size='3' min='0' max='255' class='form-control' id='confid' name='confid' placeholder='Conf ID' required>
<br>
<textarea type='text' maxlength='50' class='form-control' id='arg' name='arg' rows='2' placeholder='Args' required></textarea>
<br><br>
<div class="form-group">
<button type="submit" id="submit" class="btn btn-primary">Create Automation</button>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
{% block script %}
<script type="text/javascript">
$("#submit").prop("disabled", true);
function validate_form() {
var dev_sel = document.getElementById("devid");
var sel_op = dev_sel.options[dev_sel.selectedIndex].value;
var var_sel = document.getElementById("varname");
var sel_var = var_sel.options[var_sel.selectedIndex].value;
var adev_sel = document.getElementById("adevid");
var sel_aop = adev_sel.options[adev_sel.selectedIndex].value;
if (sel_op == "-" || sel_var == "-" || sel_aop == "-") {
$("#submit").prop("disabled", true);
console.log("not validated: "+sel_op+" "+sel_var);
} else {
$("#submit").prop("disabled", false);
console.log("validated: "+sel_op+" "+sel_var);
}
}
function ondev() {
var dev_sel = document.getElementById("devid");
var sel_op = dev_sel.options[dev_sel.selectedIndex].value;
if (sel_op != "-") {
fetch(window.origin+"/application/{{ app[1] }}/device/"+sel_op+"/variables").then(res => res.text()).then(data => $("#varname").html(data));
}
validate_form();
}
</script>
{% endblock %}
......@@ -778,8 +778,8 @@ def application_new_alert(appkey):
return redirect(url_for('login'))
@app.route('/application/<appkey>/remove-alert')
def application_alert_remove(appkey):
@app.route('/application/<appkey>/remove-<ntype>')
def application_notification_remove(appkey, ntype):
if 'name' in session:
nq.delete(appkey, request.args.get('devid'), request.args.get('id'))
tr.delete(appkey, request.args.get('devid'), request.args.get('id'))
......@@ -787,10 +787,10 @@ def application_alert_remove(appkey):
res = nfs.delete(appkey, request.args.get('devid'), request.args.get('id'))
if res[0]:
flash('Alert removed', 'success')
flash('{} removed'.format(ntype.capitalize()), 'success')
return '', 200
else:
flash('Alert cannot be removed : {}'.format(res[1]), 'danger')
flash('{} cannot be removed : {}'.format(ntype.capitalize(), res[1]), 'danger')
return '', 500
else:
return redirect(url_for('login'))
......@@ -807,6 +807,41 @@ def application_automation(appkey):
return redirect(url_for('login'))
@app.route('/application/<appkey>/new-automation', methods=['GET', 'POST'])
def application_new_automation(appkey):
if 'name' in session:
if request.method == 'GET':
ap = ad.get(appkey)
devs = dd.get_list(appkey)
return render_template('new/public/new-automation.html', devs=devs[1], app=ap[1])
elif request.method == 'POST':
# create new notification
nid = misc.rand_str(app.config['NID_LENGTH']).decode('utf-8')
dev = dd.get(appkey, request.form['devid'])
adev = dd.get(appkey, request.form['adevid'])
try:
desc = 'IF '+dev[1][0]+'.'+request.form['varname']+' '+request.form['operation']+' '+request.form['avalue']+' THEN '+adev[1][0]+'.confID_'+request.form['confid']+' = '+request.form['arg']
# action format: '<devid>#<confid>#<arg>'
action = request.form['adevid']+'#'+request.form['confid']+'#'+request.form['arg']
res = nfs.create(nid, appkey, request.form['devid'], request.form['automationname'], desc, 'automation', action)
if res[0]:
# create new function and trigger
tr.create_function(appkey, request.form['devid'], nid, [request.form['varname'],request.form['operation'],request.form['avalue']])
tr.create(appkey, request.form['devid'], nid)
flash('Automation created', 'success')
return redirect(url_for('application_automation', appkey=appkey))
else:
flash('Error creating new alert: {}'.format(res[1]), 'danger')
return redirect(request.url)
except Exception as e:
flash('Error creating new alert: {}. Make sure you have filled all form fields.'.format(e), 'danger')
return redirect(request.url)
else:
return redirect(url_for('login'))
@app.route('/alerts')
def alerts():
if 'name' in session:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment