Adds driverConfig.json, persistence, scaling, etc.
This commit is contained in:
162
html_templates/Scaling.html
Normal file
162
html_templates/Scaling.html
Normal file
@@ -0,0 +1,162 @@
|
||||
|
||||
|
||||
<div class="row row-flex">
|
||||
|
||||
<div class="col-md-6 box-me entry-top-level" id="setrawmax">
|
||||
<div class="pad15">
|
||||
<h2>Raw Max (mA)</h2>
|
||||
<form class="form-inline">
|
||||
<div class="form-group">
|
||||
<input class="form-control val_box"
|
||||
type="number"
|
||||
step="any"
|
||||
value="<%=channels['pondlevel.setrawmax'].value %>">
|
||||
</div>
|
||||
<a href="#"
|
||||
data-confirm-message="Are you sure you want to do this?"
|
||||
data-refreshpause="1"
|
||||
data-command=""
|
||||
data-staticsend=25.0
|
||||
data-channelId="<%= channels["pondlevel.setrawmax"].channelId %>"
|
||||
data-techname="<%=channels["pondlevel.setrawmax"].techName %>"
|
||||
data-name="<%= channels["pondlevel.setrawmax"].name%>"
|
||||
data-nodechannelcurrentId="<%= channels["pondlevel.setrawmax"].nodechannelcurrentId %>"
|
||||
id="<%= channels["pondlevel.setrawmax"].channelId %>"
|
||||
class="btn btn-large btn-theme animated setstatic material-icons">send</a>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-6 box-me entry-top-level" id="setrawmin">
|
||||
<div class="pad15">
|
||||
<h2>Raw Min (mA)</h2>
|
||||
<form class="form-inline">
|
||||
<div class="form-group">
|
||||
<input class="form-control val_box"
|
||||
type="number"
|
||||
step="any"
|
||||
value="<%=channels['pondlevel.setrawmin'].value %>">
|
||||
</div>
|
||||
<a href="#"
|
||||
data-confirm-message="Are you sure you want to do this?"
|
||||
data-refreshpause="1"
|
||||
data-command=""
|
||||
data-staticsend=25.0
|
||||
data-channelId="<%= channels["pondlevel.setrawmin"].channelId %>"
|
||||
data-techname="<%=channels["pondlevel.setrawmin"].techName %>"
|
||||
data-name="<%= channels["pondlevel.setrawmin"].name%>"
|
||||
data-nodechannelcurrentId="<%= channels["pondlevel.setrawmin"].nodechannelcurrentId %>"
|
||||
id="<%= channels["pondlevel.setrawmin"].channelId %>"
|
||||
class="btn btn-large btn-theme animated setstatic material-icons">send</a>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-6 box-me entry-top-level" id="seteumax">
|
||||
<div class="pad15">
|
||||
<h2>EU Max (Ft.)</h2>
|
||||
<form class="form-inline">
|
||||
<div class="form-group">
|
||||
<input class="form-control val_box"
|
||||
type="number"
|
||||
step="any"
|
||||
value="<%=channels['pondlevel.seteumax'].value %>">
|
||||
</div>
|
||||
<a href="#"
|
||||
data-confirm-message="Are you sure you want to do this?"
|
||||
data-refreshpause="1"
|
||||
data-command=""
|
||||
data-staticsend=25.0
|
||||
data-channelId="<%= channels["pondlevel.seteumax"].channelId %>"
|
||||
data-techname="<%=channels["pondlevel.seteumax"].techName %>"
|
||||
data-name="<%= channels["pondlevel.seteumax"].name%>"
|
||||
data-nodechannelcurrentId="<%= channels["pondlevel.seteumax"].nodechannelcurrentId %>"
|
||||
id="<%= channels["pondlevel.seteumax"].channelId %>"
|
||||
class="btn btn-large btn-theme animated setstatic material-icons">send</a>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-6 box-me entry-top-level" id="seteumin">
|
||||
<div class="pad15">
|
||||
<h2>EU Min (Ft.)</h2>
|
||||
<form class="form-inline">
|
||||
<div class="form-group">
|
||||
<input class="form-control val_box"
|
||||
type="number"
|
||||
step="any"
|
||||
value="<%=channels['pondlevel.seteumin'].value %>">
|
||||
</div>
|
||||
<a href="#"
|
||||
data-confirm-message="Are you sure you want to do this?"
|
||||
data-refreshpause="1"
|
||||
data-command=""
|
||||
data-staticsend=25.0
|
||||
data-channelId="<%= channels["pondlevel.seteumin"].channelId %>"
|
||||
data-techname="<%=channels["pondlevel.seteumin"].techName %>"
|
||||
data-name="<%= channels["pondlevel.seteumin"].name%>"
|
||||
data-nodechannelcurrentId="<%= channels["pondlevel.seteumin"].nodechannelcurrentId %>"
|
||||
id="<%= channels["pondlevel.seteumin"].channelId %>"
|
||||
class="btn btn-large btn-theme animated setstatic material-icons">send</a>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<style>
|
||||
|
||||
.box-me {
|
||||
position: relative;
|
||||
padding: 0.5em;
|
||||
padding-bottom: 1.5em;
|
||||
border: 1px solid #eee;
|
||||
/*margin: 1em 0;*/
|
||||
}
|
||||
|
||||
.box-me .gauge-box {
|
||||
margin-top: -0.25em;
|
||||
}
|
||||
|
||||
.pad15 {
|
||||
margin: 15px 15px;
|
||||
}
|
||||
|
||||
.box-me h2 {
|
||||
text-transform: uppercase;
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
font-weight: 400;
|
||||
letter-spacing: 1px;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
.row-flex {
|
||||
display: -webkit-box;
|
||||
display: -webkit-flex;
|
||||
display: -ms-flexbox;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.row-flex > [class*='col-'] {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
</style>
|
||||
|
||||
|
||||
<script>
|
||||
|
||||
$('.val_box').each(function(topLevel){
|
||||
$(this).change(function(){
|
||||
var id = "#" + $(this).closest(".entry-top-level").attr('id');
|
||||
if (id !== "#undefined"){
|
||||
var val = $(id).find('.val_box').val();
|
||||
var tag = $(id).find('.setstatic').attr('data-staticsend', val);
|
||||
console.log($(id).find('.setstatic').attr('data-staticsend'));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
</script>
|
||||
@@ -1,10 +1,11 @@
|
||||
{
|
||||
"name": "pondlevel",
|
||||
"driverFilename": "pondlevel.py",
|
||||
"driverId": "0130",
|
||||
"name": "pondlevel",
|
||||
"driverFilename": "pondlevel.py",
|
||||
"driverId": "0130",
|
||||
"additionalDriverFiles": [
|
||||
"calibration_db.py"
|
||||
],
|
||||
"version": 1,
|
||||
"calibration_db.py",
|
||||
"persistence.py"
|
||||
],
|
||||
"version": 1,
|
||||
"s3BucketName": "pondlevel"
|
||||
}
|
||||
}
|
||||
|
||||
21
python_driver/persistence.py
Normal file
21
python_driver/persistence.py
Normal file
@@ -0,0 +1,21 @@
|
||||
"""Data persistance functions."""
|
||||
# if more advanced persistence is needed, use a sqlite database
|
||||
import json
|
||||
|
||||
|
||||
def load(filename="persist.json"):
|
||||
"""Load persisted settings from the specified file."""
|
||||
try:
|
||||
with open(filename, 'r') as persist_file:
|
||||
return json.load(persist_file)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def store(persist_obj, filename="persist.json"):
|
||||
"""Store the persisting settings into the specified file."""
|
||||
try:
|
||||
with open(filename, 'w') as persist_file:
|
||||
return json.dump(persist_obj, persist_file)
|
||||
except Exception:
|
||||
return False
|
||||
@@ -4,8 +4,8 @@ import threading
|
||||
from device_base import deviceBase
|
||||
import calibration_db as db
|
||||
import time
|
||||
import random
|
||||
import json
|
||||
import persistence
|
||||
|
||||
_ = None
|
||||
|
||||
@@ -18,6 +18,16 @@ send_time = 300
|
||||
|
||||
temp_pl = pond_level['value']
|
||||
|
||||
persist_data = persistence.load()
|
||||
|
||||
|
||||
def scale_to_eu(inp_measurement, raw_max, raw_min, eu_max, eu_min):
|
||||
"""Scale the inp_measurement to engineering units."""
|
||||
m = (eu_max - eu_min) / (raw_max - raw_min)
|
||||
b = eu_max - raw_max * m
|
||||
scaled = inp_measurement * m + b
|
||||
return scaled
|
||||
|
||||
|
||||
class start(threading.Thread, deviceBase):
|
||||
"""Start class required by Meshify."""
|
||||
@@ -33,6 +43,11 @@ class start(threading.Thread, deviceBase):
|
||||
self.forceSend = False
|
||||
threading.Thread.start(self)
|
||||
|
||||
self.PL_RAWMAX = 20.0
|
||||
self.PL_RAWMIN = 4.0
|
||||
self.PL_EUMAX = 23.068
|
||||
self.PL_EUMIN = 0.0
|
||||
|
||||
# this is a required function for all drivers, its goal is to upload some piece of data
|
||||
# about your device so it can be seen on the web
|
||||
def register(self):
|
||||
@@ -43,11 +58,26 @@ class start(threading.Thread, deviceBase):
|
||||
"""Actually run the driver."""
|
||||
global pond_level, temp_pl, send_delta, send_time
|
||||
|
||||
wait_sec = 10
|
||||
try:
|
||||
self.PL_RAWMAX = persist_data['raw_max']
|
||||
self.PL_RAWMIN = persist_data['raw_min']
|
||||
self.PL_EUMAX = persist_data['eu_max']
|
||||
self.PL_EUMIN = persist_data['eu_min']
|
||||
except KeyError:
|
||||
print("No persistence data for scaling parameters.")
|
||||
except TypeError:
|
||||
print("No persistence data for scaling parameters.")
|
||||
|
||||
wait_sec = 30
|
||||
for i in range(0, wait_sec):
|
||||
print("pondlevel driver will start in {} seconds".format(wait_sec - i))
|
||||
time.sleep(1)
|
||||
print("BOOM! Starting pondlevel driver...")
|
||||
self.sendtodb("setrawmax", self.PL_RAWMAX, 0)
|
||||
self.sendtodb("setrawmin", self.PL_RAWMIN, 0)
|
||||
self.sendtodb("seteumax", self.PL_EUMAX, 0)
|
||||
self.sendtodb("seteumin", self.PL_EUMIN, 0)
|
||||
|
||||
send_loops = 0
|
||||
while True:
|
||||
send_now = False
|
||||
@@ -55,11 +85,9 @@ class start(threading.Thread, deviceBase):
|
||||
print "FORCE SEND: TRUE"
|
||||
send_now = True
|
||||
|
||||
up_down = 1
|
||||
if bool(random.getrandbits(1)):
|
||||
up_down = -1
|
||||
|
||||
temp_pl = pond_level['value'] + up_down * random.random()
|
||||
mcu_status = self.mcu.getDict() # Gets a dictionary of the IO states
|
||||
cloop_val = float(mcu_status['cloop'])
|
||||
temp_pl = scale_to_eu(cloop_val, self.PL_RAWMAX, self.PL_RAWMIN, self.PL_EUMAX, self.PL_EUMIN)
|
||||
|
||||
if abs(temp_pl - pond_level['value']) > send_delta:
|
||||
print("Sending {} due to value change".format(temp_pl))
|
||||
@@ -103,3 +131,41 @@ class start(threading.Thread, deviceBase):
|
||||
return True
|
||||
except KeyError:
|
||||
return "Misformed JSON"
|
||||
|
||||
def store_scaling_data(self):
|
||||
"""Store scaling data in the persist file."""
|
||||
scale_obj = {
|
||||
'raw_max': self.PL_RAWMAX,
|
||||
'raw_min': self.PL_RAWMIN,
|
||||
'eu_max': self.PL_EUMAX,
|
||||
'eu_min': self.PL_EUMIN
|
||||
}
|
||||
persistence.store(scale_obj)
|
||||
|
||||
def pondlevel_setrawmin(self, name, value):
|
||||
"""Set the raw min scaling value."""
|
||||
self.PL_RAWMIN = float(value)
|
||||
self.sendtodb("setrawmin", self.PL_RAWMIN, 0)
|
||||
self.store_scaling_data()
|
||||
return(True)
|
||||
|
||||
def pondlevel_setrawmax(self, name, value):
|
||||
"""Set the raw max scaling value."""
|
||||
self.PL_RAWMAX = float(value)
|
||||
self.sendtodb("setrawmax", self.PL_RAWMAX, 0)
|
||||
self.store_scaling_data()
|
||||
return(True)
|
||||
|
||||
def pondlevel_seteumin(self, name, value):
|
||||
"""Set the gpm min scaling value."""
|
||||
self.PL_EUMIN = float(value)
|
||||
self.sendtodb("seteumin", self.PL_EUMIN, 0)
|
||||
self.store_scaling_data()
|
||||
return(True)
|
||||
|
||||
def pondlevel_seteumax(self, name, value):
|
||||
"""Set the gpm max scaling value."""
|
||||
self.PL_EUMAX = float(value)
|
||||
self.sendtodb("seteumax", self.PL_EUMAX, 0)
|
||||
self.store_scaling_data()
|
||||
return(True)
|
||||
|
||||
Reference in New Issue
Block a user