1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
#!/usr/bin/env python
# coding: utf-8
# vim: ai ts=4 sts=4 et sw=4
import time
import math
import json
from octopus import *
import timeseries
from gevent.wsgi import WSGIServer
from flask import render_template, Flask, session, redirect, url_for, escape, request
import gevent
import gevent.event
import gevent.monkey
gevent.monkey.patch_all()
class SyncDB(timeseries.DBSimpleInt):
def __init__(self,*args):
timeseries.DBSimpleInt.__init__(self, *args)
self.semaphore = gevent.coros.Semaphore()
def get(self, key):
with self.semaphore:
return timeseries.DBSimpleInt.get(self, key)
def set(self, key, value):
with self.semaphore:
return timeseries.DBSimpleInt.set(self, key, value)
class Octopus(gevent.Greenlet):
OCTOPUS_IO_PIN = 33
ADC_REV_MODE = 2
ADC_REV_MV = 5000
ADC_MAX_STEP = 1024
OV_FACTOR = 4.0
def __init__(self,db):
gevent.Greenlet.__init__(self)
self.db = db
self.op = octopus_context()
octopus_init(self.op)
octopus_open(self.op)
octopus_adc_init(self.op, Octopus.OCTOPUS_IO_PIN)
octopus_adc_ref(self.op, Octopus.ADC_REV_MODE)
def _run(self):
while True:
start = time.time()
value = octopus_adc_get(self.op, Octopus.OCTOPUS_IO_PIN)
value_mv = ((Octopus.ADC_REV_MV/Octopus.ADC_MAX_STEP) * value) / Octopus.OV_FACTOR
value_dba = value_mv / 10.0 # messgerät gibt je dbA 10mV aus
print "{3:s} {4:d} 0x{0:=x}, {1:3.2f}mV, {2:2.2f}dbA".format(value,
round(value_mv,2),
round(value_dba,2),
time.strftime("%H:%M:%S." + "%03d" % ( round( ( start - math.floor(start) )*1000) )),
timeseries.time_ms())
self.value = value_dba
if value_dba > self.db[timeseries.time_ms()]:
self.db[timeseries.time_ms()] = value_dba
stop = time.time()
gevent.sleep( abs( 0.5 - ( stop - start ) ) )
db = SyncDB(10*timeseries.C.INT_SEC, timeseries.C.INT_DAY, "data", "octopus")
octopus = Octopus(db)
app = Flask(__name__)
app.debug = True
@app.route("/")
def app_index():
return render_template("index.html")
@app.route("/now")
def app_now():
value = db[timeseries.time_ms()] or db[timeseries.time_ms() - db.interval]
return json.dumps({'ts':timeseries.time_ms(), 'value':octopus.value})
@app.route("/range/")
@app.route("/range/<int:r_from>")
@app.route("/range/<int:r_from>/<int:r_to>")
@app.route("/range/<int:r_from>/<int:r_to>/<int:count>")
def app_range(r_from=None, r_to=None, count=1000):
if not r_from:
r_to = timeseries.time_ms()
r_from = r_to - (1000 * db.interval)
if not r_to:
r_to = timeseries.time_ms()
assert count < 10000
assert r_to - r_from <= (timeseries.C.INT_DAY * 5), "r_to - r_from = %s < %s"%(r_to-r_from, timeseries.C.INT_DAY * 5)
assert r_to - r_from > db.interval, "r_to - r_from = %s > "%(r_to-r_from, db.interval)
values = []
step = int( math.ceil( (r_to - r_from) / float(count) ) )
for ts in range(r_from, r_to, step):
value = 0
i = ts
while i < ts + step:
value = max(db[i], value)
i += db.interval
if value == 0:
value = None
values.append(value)
gevent.sleep(0)
values.reverse()
return json.dumps(values)
if __name__ == "__main__":
octopus.start()
http_server = WSGIServer(('', 8000), app)
http_server.start()
http_server._stopped_event.wait()
octopus.wait()
|