Commit 200052e2 authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

admin:user application automation completely integrated

parent 314f9ecd
{% extends 'logged_layout.html' %}
{% block title %} Administration - {{ user }} - {{ app[0] }} - New Automation - HPC&A IoT {% endblock %}
{% block location %}
<a class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block" href="/administration">Administration</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="/administration/users">Users</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="/administration/users/{{ user }}">{{ user }}</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="/administration/users/{{ user }}/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="/administration/users/{{ user }}/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="/administration/users/{{ user }}/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("/administration/users/{{ user }}/application/{{ app[1] }}/device/"+sel_op+"/variables").then(res => res.text()).then(data => $("#varname").html(data));
}
validate_form();
}
</script>
{% endblock %}
......@@ -210,6 +210,39 @@ def administration_users_user_application_automation(name, appkey):
return render_template('new/admin/user-application-automation.html', automations=ats[1], app=ap[1], user=name)
@app.route('/administration/users/<name>/application/<appkey>/new-automation', methods=['GET', 'POST'])
@restricted(access_level='admin')
def administration_users_user_application_new_automation(name, appkey):
if request.method == 'GET':
ap = ad.get(appkey)
devs = dd.get_list(appkey)
return render_template('new/admin/user-application-new-automation.html', devs=devs[1], app=ap[1], user=name)
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('administration_users_user_application_automation', name=name, 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)
@app.route('/administration/users/<name>/application/<appkey>/delete-<ntype>')
@restricted(access_level='admin')
def administration_users_user_application_notification_remove(name, appkey, ntype):
......
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