Commit 3ad0d9a4 authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

add-devive and device-settings ddm integrated

parent 3993a32e
from psycopg2 import sql from psycopg2 import sql, Binary
from app.helpers.misc import with_psql from app.helpers.misc import with_psql
import app.dao.application.application as ad import app.dao.application.application as ad
import json
@with_psql @with_psql
def create_datatable(cur, appkey, dev_id): def create_datatable(cur, appkey, dev_id):
...@@ -60,7 +61,7 @@ def create_table_ddm(cur, appkey): ...@@ -60,7 +61,7 @@ def create_table_ddm(cur, appkey):
name VARCHAR(30) NOT NULL, name VARCHAR(30) NOT NULL,
dev_id NUMERIC(3) PRIMARY KEY, dev_id NUMERIC(3) PRIMARY KEY,
description VARCHAR(200), description VARCHAR(200),
device_data_model json NOT NULL device_data_model bytea NOT NULL
)""" )"""
).format(sql.Identifier(tn))) ).format(sql.Identifier(tn)))
return (True,) return (True,)
...@@ -97,7 +98,7 @@ def create_ddm(cur, name, dev_id, appkey, desc, ddm): ...@@ -97,7 +98,7 @@ def create_ddm(cur, name, dev_id, appkey, desc, ddm):
(%s, %s, %s, %s) (%s, %s, %s, %s)
""" """
cur.execute( cur.execute(
sql.SQL(query).format(sql.Identifier(tn)), [name, dev_id, desc, ddm]) sql.SQL(query).format(sql.Identifier(tn)), [name, dev_id, desc, Binary(json.dumps(ddm).encode('utf-8'))])
return (True,) return (True,)
...@@ -127,10 +128,12 @@ def get(cur, appkey, dev_id): ...@@ -127,10 +128,12 @@ def get(cur, appkey, dev_id):
cur.execute( cur.execute(
sql.SQL(query).format(sql.Identifier(tn)), [dev_id]) sql.SQL(query).format(sql.Identifier(tn)), [dev_id])
dev = cur.fetchone() dev = cur.fetchone()
#print(json.loads(dev[3].tobytes()))
if (dev is None): if (dev is None):
return (False, 'There is no device with dev_id = {}'.format(dev_id)) return (False, 'There is no device with dev_id = {}'.format(dev_id))
else: else:
dev = [d for d in dev]
dev[3] = json.loads(dev[3].tobytes())
return (True, dev) return (True, dev)
@with_psql @with_psql
...@@ -162,7 +165,7 @@ def update_ddm(cur, appkey, devid, name, desc, ddm): ...@@ -162,7 +165,7 @@ def update_ddm(cur, appkey, devid, name, desc, ddm):
WHERE WHERE
dev_id = %s dev_id = %s
""".format(tn) """.format(tn)
cur.execute(query, (name, desc, ddm, devid)) cur.execute(query, (name, desc, Binary(json.dumps(ddm).encode('utf-8')), devid))
return (True,) return (True,)
......
...@@ -3,66 +3,73 @@ import struct ...@@ -3,66 +3,73 @@ import struct
from collections import OrderedDict from collections import OrderedDict
import json import json
# Aim: support 3 data models: MODELS = {
# 1) JSON (already supported) - no ddm 'json' : 'JSON',
# 2) msgpack - no ddm 'mpack' : 'MessagePack',
# 3) raw bytes - ddm 'raw' : 'raw bytes'
}
def test_dev():
# Aim: support 3 data models:
# 1) JSON (already supported) - no ddm
# 2) msgpack - no ddm
# 3) raw bytes - ddm
# to be stored in db # to be stored in db
ddm = { ddm = {
'model': 'json' 'model': 'json'
} }
data = { data = {
'some_int': 45, 'some_int': 45,
'some_float': 20.5, 'some_float': 20.5,
'some_bool': True, 'some_bool': True,
'some_str': 'foo:bar' 'some_str': 'foo:bar'
} }
print ('Normal JSON:') print ('Normal JSON:')
print (data) print (data)
# to be stored in db # to be stored in db
ddm = { ddm = {
'model': 'mpack' 'model': 'mpack'
} }
mpdata = msgpack.packb(data) mpdata = msgpack.packb(data)
print('msgpack packed:') print('msgpack packed:')
print(mpdata) print(mpdata)
print('msgpack unpacked:') print('msgpack unpacked:')
print(msgpack.unpackb(mpdata)) print(msgpack.unpackb(mpdata))
# to be stored in db # to be stored in db
ddm = { ddm = {
'model' : 'raw', 'model' : 'raw',
'endianness': '<', 'endianness': '<',
'format' : OrderedDict([('some_int', 'H'), ('some_float', 'f'), ('some_bool', '?'), ('some_str', '7s')]) 'format' : OrderedDict([('some_int', 'H'), ('some_float', 'f'), ('some_bool', '?'), ('some_str', '7s')])
} }
print(''.join(dict(ddm['format']).values())) print(''.join(dict(ddm['format']).values()))
for k, v in ddm['format'].items(): for k, v in ddm['format'].items():
if v[-1] == 's': if v[-1] == 's':
print (v, k) print (v, k)
# endianess: little # endianess: little
# uint16_t some_int = 45; // 2 bytes # uint16_t some_int = 45; // 2 bytes
# float some_float = 20.5; // 4 # float some_float = 20.5; // 4
# uint8_t some_bool = 1; // 1 # uint8_t some_bool = 1; // 1
# char some_str[8] = "foo:bar"; // 8 # char some_str[8] = "foo:bar"; // 8
# | uint16 | float | b | str | # | uint16 | float | b | str |
rdata = b'\x2d\x00\x00\x00\xa4\x41\x01\x66\x6f\x6f\x3a\x62\x61\x72' rdata = b'\x2d\x00\x00\x00\xa4\x41\x01\x66\x6f\x6f\x3a\x62\x61\x72'
print ('raw data:') print ('raw data:')
print (rdata) print (rdata)
print ('raw data unpacked:') print ('raw data unpacked:')
print (struct.unpack('<Hf?7s', rdata)) print (struct.unpack('<Hf?7s', rdata))
ru = struct.unpack('<Hf?7s', rdata) ru = struct.unpack('<Hf?7s', rdata)
dru = dict(zip(['some_int','some_float','some_bool','some_str'],ru)) dru = dict(zip(['some_int','some_float','some_bool','some_str'],ru))
dru['some_str'] = dru['some_str'].decode('utf-8') dru['some_str'] = dru['some_str'].decode('utf-8')
print ('raw data json:') print ('raw data json:')
print (dru) print (dru)
def read_data_ddm(data, ddm): def read_data_ddm(data, ddm):
if ddm['model'] == 'mpack': if ddm['model'] == 'mpack':
...@@ -78,22 +85,23 @@ def read_data_ddm(data, ddm): ...@@ -78,22 +85,23 @@ def read_data_ddm(data, ddm):
elif ddm['model'] == 'json': elif ddm['model'] == 'json':
return json.loads(data.decode('utf-8')) return json.loads(data.decode('utf-8'))
print ('test with json:') def test_done():
ddm = { print ('test with json:')
ddm = {
'model': 'json' 'model': 'json'
} }
print(read_data_ddm(json.dumps(data).encode('utf-8'), ddm)) print(read_data_ddm(json.dumps(data).encode('utf-8'), ddm))
print ('test with mpack:') print ('test with mpack:')
ddm = { ddm = {
'model': 'mpack' 'model': 'mpack'
} }
print(read_data_ddm(mpdata, ddm)) print(read_data_ddm(mpdata, ddm))
print ('test with raw:') print ('test with raw:')
ddm = { ddm = {
'model' : 'raw', 'model' : 'raw',
'endianness': '<', 'endianness': '<',
'format' : OrderedDict([('some_int', 'H'), ('some_float', 'f'), ('some_bool', '?'), ('some_str', '7s')]) 'format' : OrderedDict([('some_int', 'H'), ('some_float', 'f'), ('some_bool', '?'), ('some_str', '7s')])
} }
print(read_data_ddm(rdata, ddm)) print(read_data_ddm(rdata, ddm))
...@@ -7,6 +7,10 @@ import binascii ...@@ -7,6 +7,10 @@ import binascii
from datetime import datetime from datetime import datetime
from functools import wraps from functools import wraps
import collections
import json
def rand_str(length): def rand_str(length):
if length % 2 == 0: if length % 2 == 0:
return hexlify(os.urandom(length//2)) return hexlify(os.urandom(length//2))
...@@ -168,3 +172,33 @@ def local_weekday(day_offset = 0): ...@@ -168,3 +172,33 @@ def local_weekday(day_offset = 0):
def utc_local_diff(): def utc_local_diff():
return abs((datetime.now() - datetime.utcnow()).total_seconds()) return abs((datetime.now() - datetime.utcnow()).total_seconds())
def extract_ddm(request):
ddmin = {'model':request.form['ddm'], 'format':{}}
try:
ddmin['endianness'] = request.form['endianness']
except:
pass
# create dict with variables
for k,v in request.form.items():
if k.startswith("var"):
i = k.split("_")
if not int(i[1]) in ddmin['format']:
ddmin['format'][int(i[1])] = { i[0][3:] : v }
else:
ddmin['format'][int(i[1])][i[0][3:]] = v
# format size
for k,v in ddmin['format'].items():
if 'size' in v:
ddmin['format'][k]['type'] = v['size'] + 's'
ddmin['format'][k].pop('size')
# order dict
od = collections.OrderedDict(sorted(ddmin['format'].items()))
ddmin.pop('format')
ddmin['format'] = collections.OrderedDict()
# give it defined ddm format
for k,v in od.items():
ddmin['format'][v['name']] = v['type']
return ddmin
var var_ctr = 1;
var raw_types = {
"float" : {"type":"f", "size":4},
"bool" : {"type":"?", "size":1},
"string" : {"type":"s"},
"uint8_t" : {"type":"B", "size":1},
"uint16_t" : {"type":"H", "size":2},
"uint32_t" : {"type":"I", "size":4},
"uint64_t" : {"type":"Q", "size":8},
"int8_t" : {"type":"b", "size":1},
"int16_t" : {"type":"h", "size":2},
"int32_t" : {"type":"i", "size":4},
"int64_t" : {"type":"q", "size":8},
};
var json_types = ["number", "string", "boolean"];
function set_ctr(id) {
var_ctr = id
}
function add_variable_name(id, type, name) {
var div = document.createElement("div");
var lg = 6;
if (type == "raw") {
lg = 4
}
div.setAttribute("class", "col-lg-"+lg);
var div_fg = document.createElement("div");
div_fg.setAttribute("class", "form-group");
var label = document.createElement("label");
label.setAttribute("for", "varname_"+id);
label.innerHTML = "Variable name:";
var input = document.createElement("input");
input.setAttribute("type", "text");
input.setAttribute("maxlength", "30");
input.setAttribute("class", "form-control");
input.setAttribute("id", "varname_"+id);
input.setAttribute("name", "varname_"+id);
if (typeof name !== "undefined") {
input.setAttribute("value", name);
}
div_fg.appendChild(label);
div_fg.appendChild(input);
div.appendChild(div_fg);
return div;
}
function add_variable_type(id, type, typev) {
var div = document.createElement("div");
var lg = 5;
if (type == "raw") {
lg = 3;
}
div.setAttribute("class", "col-lg-"+lg);
var div_fg = document.createElement("div");
div_fg.setAttribute("class", "form-group");
var label = document.createElement("label");
label.setAttribute("for", "vartype_"+id);
label.innerHTML = "Type:";
var select = document.createElement("select");
select.setAttribute("id", "vartype_"+id);
select.setAttribute("name", "vartype_"+id);
select.setAttribute("class", "form-control");
select.setAttribute("onchange", "return onvartype('"+id+"');");
if (type == "raw") {
for (var t in raw_types) {
var option = document.createElement("option");
option.setAttribute("value", raw_types[t].type);
option.text = t;
select.appendChild(option);
}
} else if (type == "json" || type == "mpack") {
json_types.forEach(function(t) {
var option = document.createElement("option");
option.setAttribute("value", t);
option.text = t;
select.appendChild(option);
});
}
if (typeof typev !== "undefined") {
if (typev[typev.length-1] == 's') {
select.value = typev[typev.length-1];
} else {
select.value = typev;
}
}
div_fg.appendChild(label);
div_fg.appendChild(select);
div.appendChild(div_fg);
return div;
}
function add_variable_size(id, type, typev) {
var div = document.createElement("div");
div.setAttribute("class", "col-lg-3");
var div_fg = document.createElement("div");
div_fg.setAttribute("class", "form-group");
var label = document.createElement("label");
label.setAttribute("for", "varsize_"+id);
label.innerHTML = "Size:";
var input = document.createElement("input");
input.setAttribute("type", "number");
input.setAttribute("min", "1");
input.setAttribute("max", "256");
input.setAttribute("class", "form-control");
input.setAttribute("id", "varsize_"+id);
input.setAttribute("name", "varsize_"+id);
input.setAttribute("disabled", "true");
if (typeof typev !== "undefined") {
if (typev[typev.length-1] == 's') {
var size = typev.substring(0, typev.length-1);
input.setAttribute("value", size);
input.disabled = false;
} else {
for (var t in raw_types) {
if (raw_types[t]['type'] == typev) {
input.setAttribute("value", raw_types[t]['size']);
}
}
}
} else {
input.setAttribute("value", "4");
}
div_fg.appendChild(label);
div_fg.appendChild(input);
div.appendChild(div_fg);
return div;
}
function add_variable_rm(id) {
var div = document.createElement("div");
div.setAttribute("class", "col-lg-1");
var a = document.createElement("a");
a.setAttribute("href", "javascript:void(0)");
a.setAttribute("onclick", "return remove_variable('"+id+"');");
var span = document.createElement("span");
span.setAttribute("class", "fa fa-remove");
a.appendChild(span);
div.appendChild(a);
return div;
}
function add_variable(type, name, typev) {
var row = document.createElement("div");
row.setAttribute("class", "row");
row.setAttribute("id", "variable"+var_ctr);
var variable_name = add_variable_name(var_ctr, type, name);
var variable_type = add_variable_type(var_ctr, type, typev);
if (type == "raw") {
var variable_size = add_variable_size(var_ctr, type, typev);
}
var variable_rm = add_variable_rm(var_ctr);
row.appendChild(variable_name);
row.appendChild(variable_type);
if (type == "raw") {
row.appendChild(variable_size);
}
row.appendChild(variable_rm);
var_ctr++;
return row;
}
function remove_variable(id) {
$("#variable"+id).remove();
}
function add_variable_link() {
var div = document.createElement("div");
div.setAttribute("class", "col-lg-12");
div.setAttribute("id", "addvarlink");
var center = document.createElement("center");
var a = document.createElement("a");
a.setAttribute("href", "javascript:void(0)");
a.onclick = add_new_variable;
a.text = "Add variable";
center.appendChild(a);
div.appendChild(center);
return div;
}
function add_endianness(ddm) {
var div = document.createElement("div");
div.setAttribute("class", "form-group");
var label = document.createElement("label");
label.setAttribute("for", "endianness");
label.innerHTML = "Endianness:";
var select = document.createElement("select");
select.setAttribute("id", "endianness");
select.setAttribute("name", "endianness");
select.setAttribute("class", "form-control");
var little = document.createElement("option");
little.setAttribute("value", "<");
little.text = "Little Endian"
var big = document.createElement("option");
big.setAttribute("value", ">");
big.text = "Big Endian";
if (typeof ddm !== "undefined") {
select.value = ddm['endianness'];
}
select.appendChild(little);
select.appendChild(big);
div.appendChild(label);
div.appendChild(select);
return div;
}
function add_ddm_ext(type, ddm) {
var ddm_div = document.createElement("div");
ddm_div.setAttribute("id", "ddm_ext");
if (type == "raw") {
var endianness = add_endianness(ddm);
ddm_div.appendChild(endianness);
}
if (typeof ddm !== "undefined") {
for (var v in ddm['format']) {
var variable = add_variable(type, v, ddm['format'][v]);
ddm_div.appendChild(variable);
}
} else {
var variable = add_variable(type);
ddm_div.appendChild(variable);
}
var div = document.getElementById("ddm_div");
div.appendChild(ddm_div);
var add_var = add_variable_link();
div.appendChild(add_var);
}
function add_new_variable() {
var ddm_div = document.getElementById("ddm_ext");
var ddm_sel = document.getElementById("ddm");
var sel_op = ddm_sel.options[ddm_sel.selectedIndex].value;
ddm_div.appendChild(add_variable(sel_op));
}
function onddm() {
$("#ddm_ext").remove();
$("#addvarlink").remove();
var ddm_sel = document.getElementById("ddm");
var sel_op = ddm_sel.options[ddm_sel.selectedIndex].value;
add_ddm_ext(sel_op);
}
function onddmsetting() {
var ddm_sel = document.getElementById("ddm");
ddm_sel.value = device_data_model['model'];
add_ddm_ext(device_data_model['model'], device_data_model);
}
function onvartype(id) {
var type_sel = document.getElementById("vartype_"+id);
var sel_op = type_sel.options[type_sel.selectedIndex];
if (sel_op.value == "s") {
document.getElementById("varsize_"+id).disabled = false;
document.getElementById("varsize_"+id).setAttribute("value", "");
} else {
document.getElementById("varsize_"+id).value = raw_types[sel_op.text].size;
document.getElementById("varsize_"+id).setAttribute("disabled", "true");
}
}
$(document).ready(function () {
if (typeof device_data_model === "undefined") {
onddm();
} else {
onddmsetting();
}
});
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
{% endblock %} {% endblock %}
{% block location %} <a class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block" href="./applications">Applications</a> {% 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> <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> <a class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block" href="/application/{{ app[1] }}">{{ app[0] }}</a>
{% endblock %} {% endblock %}
...@@ -39,10 +39,10 @@ ...@@ -39,10 +39,10 @@
<br> <br>
<div class="form-group"> <div class="form-group">
<label for="ddm"> Data Model: </label> <label for="ddm"> Data Model: </label>
<select class="form-control" id="ddm" name="ddm" onclick="return onddm();"> <select class="form-control" id="ddm" name="ddm" onchange="return onddm();">
<option value="json"> JSON </option> {% for k,v in models.items() %}
<option value="mpack"> MessagePack </option> <option value="{{ k }}"> {{ v }} </option>
<option value="raw"> raw bytes </option> {% endfor %}
</select> </select>
</div> </div>
<div id="ddm_div"> <div id="ddm_div">
...@@ -60,249 +60,5 @@ ...@@ -60,249 +60,5 @@
{% endblock %} {% endblock %}
{% block script %} {% block script %}
<script type="text/javascript"> <script type="text/javascript" src="{{ url_for('static', filename='js/application-add-device.js') }}"></script>
var var_ctr = 1;
var var_cnt = {};
var types = {
"float" : {"type":"f", "size":4},
"bool" : {"type":"?", "size":1},
"string" : {"type":"s"},
"uint8_t" : {"type":"B", "size":1},
"uint16_t" : {"type":"H", "size":2},
"uint32_t" : {"type":"I", "size":4},
"uint64_t" : {"type":"Q", "size":8},
"int8_t" : {"type":"b", "size":1},
"int16_t" : {"type":"h", "size":2},
"int32_t" : {"type":"i", "size":4},
"int64_t" : {"type":"q", "size":8},
}
function add_variable_name(id) {
var div = document.createElement("div");
div.setAttribute("class", "col-lg-4");
var div_fg = document.createElement("div");
div_fg.setAttribute("class", "form-group");
var label = document.createElement("label");
label.setAttribute("for", "varname"+id);
label.innerHTML = "Variable name:";
var input = document.createElement("input");
input.setAttribute("type", "text");
input.setAttribute("maxlength", "30");
input.setAttribute("class", "form-control");
input.setAttribute("id", "varname"+id);
input.setAttribute("name", "varname"+id);
input.setAttribute("onkeyup", "return onvarname('"+id+"');");
div_fg.appendChild(label);
div_fg.appendChild(input);
div.appendChild(div_fg);
return div;
}
function add_variable_type(id) {
var div = document.createElement("div");
div.setAttribute("class", "col-lg-4");
var div_fg = document.createElement("div");
div_fg.setAttribute("class", "form-group");
var label = document.createElement("label");
label.setAttribute("for", "vartype"+id);
label.innerHTML = "Type:";
var select = document.createElement("select");
select.setAttribute("id", "vartype"+id);
select.setAttribute("name", "vartype"+id);
select.setAttribute("class", "form-control");
select.setAttribute("onchange", "return onvartype('"+id+"');");
for (var t in types) {
var option = document.createElement("option");
option.setAttribute("value", types[t].type);
option.text = t;
select.appendChild(option);
}
div_fg.appendChild(label);
div_fg.appendChild(select);
div.appendChild(div_fg);
return div;
}
function add_variable_size(id) {
var div = document.createElement("div");
div.setAttribute("class", "col-lg-3");
var div_fg = document.createElement("div");
div_fg.setAttribute("class", "form-group");
var label = document.createElement("label");
label.setAttribute("for", "varsize"+id);
label.innerHTML = "Size:";
var input = document.createElement("input");
input.setAttribute("type", "number");
input.setAttribute("min", "1");
input.setAttribute("max", "256");
input.setAttribute("class", "form-control");
input.setAttribute("id", "varsize"+id);
input.setAttribute("name", "varsize"+id);
input.setAttribute("disabled", "true");
input.setAttribute("onkeyup", "return onvarsize('"+id+"');");
div_fg.appendChild(label);
div_fg.appendChild(input);
div.appendChild(div_fg);
return div;
}
function add_variable_rm(id) {
var div = document.createElement("div");
div.setAttribute("class", "col-lg-1");
var a = document.createElement("a");
a.setAttribute("href", "javascript:void(0)");
a.setAttribute("onclick", "return remove_variable('"+id+"');");
var span = document.createElement("span");
span.setAttribute("class", "fa fa-remove");
a.appendChild(span);
div.appendChild(a);
return div;
}
function add_variable() {
var row = document.createElement("div");
row.setAttribute("class", "row");
row.setAttribute("id", "variable"+var_ctr);
var variable_name = add_variable_name(var_ctr);
var variable_type = add_variable_type(var_ctr);
var variable_size = add_variable_size(var_ctr);
var variable_rm = add_variable_rm(var_ctr);
row.appendChild(variable_name);
row.appendChild(variable_type);
row.appendChild(variable_size);
row.appendChild(variable_rm);
var_cnt[var_ctr] = { "name": "", "type":"f", "size":4 };
var_ctr++;
return row;
}
function remove_variable(id) {
$("#variable"+id).remove();
delete var_cnt[id];
}
function add_variable_link() {
var div = document.createElement("div");
div.setAttribute("class", "col-lg-12");
div.setAttribute("id", "addvarlink");
var center = document.createElement("center");
var a = document.createElement("a");
a.setAttribute("href", "javascript:void(0)");
a.onclick = add_new_variable;
a.text = "Add variable";
center.appendChild(a);
div.appendChild(center);
return div;
}
function add_endianness() {
var div = document.createElement("div");
div.setAttribute("class", "form-group");
var label = document.createElement("label");
label.setAttribute("for", "endianness");
label.innerHTML = "Endianness:";
var select = document.createElement("select");
select.setAttribute("id", "endianness");
select.setAttribute("name", "endianness");
select.setAttribute("class", "form-control");
var little = document.createElement("option");
little.setAttribute("value", "<");
little.text = "Little Endian"
var big = document.createElement("option");
big.setAttribute("value", ">");
big.text = "Big Endian";
select.appendChild(little);
select.appendChild(big);
div.appendChild(label);
div.appendChild(select);
return div;
}
function add_ddm_ext() {
var ddm_div = document.createElement("div");
ddm_div.setAttribute("id", "ddm_ext");
var endianness = add_endianness();
var variable = add_variable();
ddm_div.appendChild(endianness);
ddm_div.appendChild(variable);
var div = document.getElementById("ddm_div");
div.appendChild(ddm_div);
var add_var = add_variable_link();
div.appendChild(add_var);
}
function add_new_variable() {
var ddm_div = document.getElementById("ddm_ext");
ddm_div.appendChild(add_variable());
}
function onddm() {
var ddm_sel = document.getElementById("ddm");
var sel_op = ddm_sel.options[ddm_sel.selectedIndex].value;
if (sel_op == "raw" && !document.getElementById("ddm_ext")) {
add_ddm_ext();
} else if (sel_op != "raw" && document.getElementById("ddm_ext")) {
$("#ddm_ext").remove();
$("#addvarlink").remove();
}
}
function onvarname(id) {
var_cnt[id].name = document.getElementById("varname"+id).value;
}
function onvartype(id) {
var type_sel = document.getElementById("vartype"+id);
var sel_op = type_sel.options[type_sel.selectedIndex];
var_cnt[id].type = sel_op.value;
if (sel_op.value == "s") {
document.getElementById("varsize"+id).disabled = false;
document.getElementById("varsize"+id).setAttribute("value", "");
var_cnt[id].size = "";
} else {
var_cnt[id].size = types[sel_op.text].size;
document.getElementById("varsize"+id).value = types[sel_op.text].size;
document.getElementById("varsize"+id).setAttribute("disabled", "true");
}
}
function onvarsize(id) {
var_cnt[id].size = document.getElementById("varsize"+id).value;
}
</script>
{% endblock %} {% endblock %}
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
{% block title %} HPC&amp;A IoT - Device Settings {% endblock %} {% block title %} HPC&amp;A IoT - Device Settings {% endblock %}
{% block header %}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
{% endblock %}
{% block location %} {% block location %}
<a class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block" href="/applications">Applications</a> <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> <p class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block"> &nbsp;-&nbsp; </p>
...@@ -30,6 +34,53 @@ ...@@ -30,6 +34,53 @@
<label>Description:</label><br> <label>Description:</label><br>
<textarea id="devdesc" maxlength="200" class="form-control" name="devdesc" rows="5"></textarea> <textarea id="devdesc" maxlength="200" class="form-control" name="devdesc" rows="5"></textarea>
</div> </div>
<br>
<div class="form-group">
<label for="ddm"> Data Model: </label>
<select class="form-control" id="ddm" name="ddm" onchange="return onddm();">
{% for k,v in models.items() %}
<option value="{{ k }}"> {{ v }} </option>
{% endfor %}
</select>
<!--
<select class="form-control" id="ddm" name="ddm" onclick="return onddm();">
<option value="{{ dev[3]['model'] }}" default> {{ models[dev[3]['model']] }} </option>
{% for k,v in models.items() %}
{% if k != dev[3]['model'] %}
<option value="{{ k }}"> {{ v }} </option>
{% endif %}
{% endfor %}
</select>
-->
</div>
<div id="ddm_div">
<!--
{% if dev[3]['format'] != {} %}
<div id="ddm_ext">
{% if dev[3]['model'] == 'json' or dev[3]['model'] == 'mpack' %}
{% for name, type in dev[3]['format'].items() %}
<div class="row" id="variable_{{ loop.index }}">
<div class="col-lg-6">
<div class="form-group">
<label for="varname_{{ loop.index }}"> Variable name: </label>
<input type="text" maxlength="30" class="form-control" id="varname_{{ loop.index }}" name="varname_{{ loop.index }}" value="{{ name }}">
</div>
</div>
<div class="col-lg-5">
<div class="form-group">
<label for="vartype_{{ loop.index }}"> Type: </label>
<select id="vartype_{{ loop.index }}" name="vartype_{{ loop.index }}" class="form-control" onchange="onvartype('{{ loop.index }}')">
<option value="{{ type }}"> {{ type }} </option>
</select>
</div>
</div>
</div>
{% endfor %}
{% endif %}
</div>
{% endif %}
-->
</div>
</form> </form>
<br> <br>
<div class="row" style="margin-top: 30px;"> <div class="row" style="margin-top: 30px;">
...@@ -73,5 +124,7 @@ ...@@ -73,5 +124,7 @@
{% block script %} {% block script %}
<script type="text/javascript"> <script type="text/javascript">
document.getElementById("devdesc").value = "{{ dev[2] }}"; document.getElementById("devdesc").value = "{{ dev[2] }}";
var device_data_model = {{ dev[3]|safe }};
</script> </script>
<script type="text/javascript" src="{{ url_for('static', filename='js/application-add-device.js') }}"></script>
{% endblock %} {% endblock %}
...@@ -16,6 +16,7 @@ import app.dao.misc.misc as md ...@@ -16,6 +16,7 @@ import app.dao.misc.misc as md
import app.helpers.misc as misc import app.helpers.misc as misc
import app.helpers.mailer as mailer import app.helpers.mailer as mailer
import app.helpers.device_data_model as ddm
import os import os
import binascii import binascii
...@@ -142,7 +143,7 @@ def application_create(): ...@@ -142,7 +143,7 @@ def application_create():
flash('Error: {}'.format(res[1]), 'danger') flash('Error: {}'.format(res[1]), 'danger')
return render_template(request.url) return render_template(request.url)
res = dd.create_table(appkey) res = dd.create_table_ddm(appkey)
if not res[0]: if not res[0]:
ad.delete(appkey) ad.delete(appkey)
...@@ -190,8 +191,7 @@ def application_device(appkey, devid): ...@@ -190,8 +191,7 @@ def application_device(appkey, devid):
if session['name'] == ap[1][2]: if session['name'] == ap[1][2]:
dev = dd.get(appkey, devid) dev = dd.get(appkey, devid)
session['devid'] = int(dev[1][1]) print(dev);
session['devname'] = dev[1][0]
ld = data.get_last_range(appkey, devid, [MAX_PG_ENTRIES_DATA, 0]) ld = data.get_last_range(appkey, devid, [MAX_PG_ENTRIES_DATA, 0])
cnt = data.get_count(appkey, devid) cnt = data.get_count(appkey, devid)
...@@ -214,8 +214,11 @@ def application_add_device(appkey): ...@@ -214,8 +214,11 @@ def application_add_device(appkey):
if request.method == 'GET': if request.method == 'GET':
ap = ad.get(appkey) ap = ad.get(appkey)
dev_list = dd.get_list(appkey) dev_list = dd.get_list(appkey)
return render_template('new/public/add-device.html', app=ap[1], free_ids=misc.prep_id_range(dev_list[1])) return render_template('new/public/add-device.html', app=ap[1], free_ids=misc.prep_id_range(dev_list[1]), models=ddm.MODELS)
elif request.method == 'POST': elif request.method == 'POST':
ddmin = misc.extract_ddm(request)
if False:
res = dd.create(request.form['devname'], request.form['devid'], appkey, request.form['devdesc']) res = dd.create(request.form['devname'], request.form['devid'], appkey, request.form['devdesc'])
if not res[0]: if not res[0]:
...@@ -224,6 +227,19 @@ def application_add_device(appkey): ...@@ -224,6 +227,19 @@ def application_add_device(appkey):
else: else:
res = data.create_table(appkey, request.form['devid']) res = data.create_table(appkey, request.form['devid'])
if not res[0]:
dd.delete(session['appkey'], request.form['devid'])
flash('Error: {}'.format(res[1]), 'danger')
return render_template(request.url)
else:
return redirect(url_for('application', appkey=appkey))
if True:
res = dd.create_ddm(request.form['devname'], request.form['devid'], appkey, request.form['devdesc'], ddmin)
if not res[0]:
flash('Error: {}'.format(res[1]), 'danger')
return render_template(request.url)
else:
res = data.create_table_ddm(appkey, request.form['devid'])
if not res[0]: if not res[0]:
dd.delete(session['appkey'], request.form['devid']) dd.delete(session['appkey'], request.form['devid'])
flash('Error: {}'.format(res[1]), 'danger') flash('Error: {}'.format(res[1]), 'danger')
...@@ -368,16 +384,6 @@ def application_device_variables(appkey, devid): ...@@ -368,16 +384,6 @@ def application_device_variables(appkey, devid):
else: else:
return redirect(url_for('login')) return redirect(url_for('login'))
@app.route('/delete-dev')
def delete_dev():
if 'name' in session and 'devid' in session:
data.delete_table(session['appkey'], session['devid'])
res = dd.delete(session['appkey'], session['devid'])
return redirect(url_for('app_', appkey=session['appkey']))
else:
return redirect(utl_for('index'))
@app.route('/delete-account') @app.route('/delete-account')
def delete_account(): def delete_account():
...@@ -623,15 +629,17 @@ def application_device_settings(appkey, devid): ...@@ -623,15 +629,17 @@ def application_device_settings(appkey, devid):
if request.method == 'GET': if request.method == 'GET':
ap = ad.get(appkey) ap = ad.get(appkey)
dev = dd.get(appkey, devid) dev = dd.get(appkey, devid)
dev_list = dd.get_list(appkey)
return render_template('new/public/device-settings.html', app=ap[1], dev=dev[1], free_ids=misc.prep_id_range(dev_list[1])) return render_template('new/public/device-settings.html', app=ap[1], dev=dev[1], models=ddm.MODELS)
elif request.method == 'POST': elif request.method == 'POST':
res = dd.update(appkey, devid, request.form['devname'], request.form['devdesc']) ddmin = misc.extract_ddm(request)
print(ddmin)
#res = dd.update(appkey, devid, request.form['devname'], request.form['devdesc'])
res = dd.update_ddm(appkey, devid, request.form['devname'], request.form['devdesc'], ddmin)
if not res[0]: if not res[0]:
flash('Error: {}'.format(res[1]), 'danger') flash('Error: {}'.format(res[1]), 'danger')
return render_template(request.url) return redirect(request.url)
return redirect(request.url) return redirect(request.url)
else: else:
......
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