forked from arcenik/lacrosse-ws3500
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.py
executable file
·230 lines (186 loc) · 7.58 KB
/
daemon.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#! /usr/bin/env python3
###############################################################################
from flask import Flask, Response, redirect, render_template
from optparse import OptionParser
import serial
import logging
import threading
import time
import traceback
import sys
from lacrosse import WS3500
# from pprint import pprint as pp
from pprint import pformat as pf
###############################################################################
# globals
app = Flask("WS3500")
ser = None
run = True
lastdata = None
lasterror = None
logger = None
device = None
template = {
"ws3500_external_temp": {
"help": "External Temperature", "type": "gauge", "value": ""},
"ws3500_external_humidity": {
"help": "External Humidity", "type": "gauge", "value": ""},
"ws3500_internal_temp": {
"help": "Internal Temperature", "type": "gauge", "value": ""},
"ws3500_internal_humidity": {
"help": "Internal Humitidy", "type": "gauge", "value": ""},
"ws3500_dewpoint": {
"help": "Dew Point", "type": "gauge", "value": ""},
"ws3500_pressure": {
"help": "Pressure", "type": "gauge", "value": ""},
"ws3500_fetch_duration": {
"help": "Time taken to fetch data", "type": "gauge", "value": ""},
"ws3500_fetch_time": {
"help": "Timestamp when data was fetched",
"type": "gauge", "value": ""},
"ws3500_retries_count": {
"help": "Number of retries", "type": "gauge", "value": ""},
"ws3500_fails_differ_count": {
"help": "Number of read failed due to differents values",
"type": "gauge", "value": ""},
"ws3500_fails_zeroes_count": {
"help": "Number of read failed due to only zeroes returned",
"type": "gauge", "value": ""},
"ws3500_fails_ones_count": {
"help": "Number of read failed due to only ones returned",
"type": "gauge", "value": ""}
}
###############################################################################
@app.template_filter('datetime')
def _jinja2_filter_datetime(date):
return time.ctime(date)
###############################################################################
@app.route("/")
def root():
"Returns a 302 redirect to /metrics"
return redirect("/metrics", code=302)
###############################################################################
@app.route("/metrics")
def metrics():
"Returns prometheus data (as text/plain)"
global lastdata, lasterror, logger, device
res = ""
if lastdata is not None:
for k in lastdata:
res += "# HELP {k} {v}\n".format(k=k, v=lastdata[k]["help"])
res += "# TYPE {k} {v}\n".format(k=k, v=lastdata[k]["type"])
res += "{k} {v}\n".format(k=k, v=lastdata[k]["value"])
return Response(res, mimetype="text/plain")
###############################################################################
@app.route("/status")
def status():
"Returns status.html rendered template"
global lastdata, lasterror
return render_template('status.html.j2', data=lastdata, error=lasterror)
###############################################################################
def single_fetch(ws):
"""
Make a single synchronous read
single_fetch(ws)
ws : Weather Station object
"""
global template
ws._init = False
ws._initialize()
newdata = template
t1 = time.time()
newdata["ws3500_external_temp"]["value"] = ws.temp_ext()
newdata["ws3500_external_humidity"]["value"] = ws.humidity_ext()
newdata["ws3500_internal_temp"]["value"] = ws.temp_int()
newdata["ws3500_internal_humidity"]["value"] = ws.humidity_int()
newdata["ws3500_dewpoint"]["value"] = ws.dewpoint()
newdata["ws3500_pressure"]["value"] = ws.rel_pressure()
newdata["ws3500_retries_count"]["value"] = ws.count_retries
newdata["ws3500_fails_differ_count"]["value"] = ws.count_failed_differs
newdata["ws3500_fails_zeroes_count"]["value"] = ws.count_failed_zeroes
newdata["ws3500_fails_ones_count"]["value"] = ws.count_failed_ones
t2 = time.time()
ellapsed = t2-t1
newdata["ws3500_fetch_duration"]["value"] = ellapsed
newdata["ws3500_fetch_time"]["value"] = time.time()
return newdata
###############################################################################
###############################################################################
###############################################################################
class ws3500_fetcher(threading.Thread):
def __init__(self, name, device, logger):
threading.Thread.__init__(self)
self.name = name
self.device = device
self.logger = logger
self.ser = None
self.ws = None
def run(self):
global lastdata, lasterror
self.logger.info("[fetcher] starting thread")
while run:
try:
lasterror = None
if not self.ws:
self.logger.info("[fetcher] opening device")
self.ser = serial.Serial(
baudrate=300, port=self.device,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=None,
writeTimeout=None,
interCharTimeout=None,
rtscts=0,
dsrdtr=None,
xonxoff=0
)
# self.ws = WS3500(self.ser, logger=self.logger)
self.ws = WS3500(self.ser)
lastdata = single_fetch(self.ws)
time.sleep(5)
except Exception as e:
self.logger.warning(
"[fetcher] exception catched, cleaning data")
self.logger.warning(pf(e))
self.ser = None
self.ws = None
lasterror = pf(e)
lastdata = None
print("-"*60)
traceback.print_exc(file=sys.stdout)
print("-"*60)
time.sleep(5)
self.logger.info("[fetcher] exiting thread")
###############################################################################
###############################################################################
###############################################################################
if __name__ == "__main__":
parser = OptionParser()
parser.add_option(
"-d", "--device", dest="DEVICE", default="/dev/ttyUSB0",
help="Device to access serial port")
parser.add_option(
"-P", "--port", dest="PORT", default="5000",
help="Listen port (listen is 5000)")
parser.add_option(
"-H", "--host", dest="HOST", default="127.0.0.1",
help="Listen host (default is 127.0.0.1)")
# parser.add_option(
# "--async", action="store_true", dest="ASYNC",
# help="Operate in asynchronous mode (data fetch in background)")
# parser.add_option(
# "--sync", action="store_false", dest="ASYNC",
# help="Operate in synchronous mode (data fetch in foreground)")
(options, args) = parser.parse_args()
logger = logging.getLogger('WS3500')
logger.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, CRITICAL
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
f = ws3500_fetcher(
"ws3500-fetcher", device=options.DEVICE, logger=logger)
f.start()
app.run(host=options.HOST, port=options.PORT)