forked from hnrkp/pyWeatherLink
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWxDaemon.py
More file actions
139 lines (118 loc) · 5.06 KB
/
Copy pathWxDaemon.py
File metadata and controls
139 lines (118 loc) · 5.06 KB
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
"""
WxDaemon - A sample SocketServer, serving weather data as an example on how to use pyWeatherLink.
This file is part of the pyWeatherLink package,
Copyright 2008 by Henrik Persson <root@fulhack.info>.
pyWeatherLink is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
pyWeatherLink is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pyWeatherLink. If not, see <http://www.gnu.org/licenses/>.
"""
import SocketServer
import time
import random
import sys
import threading
from communication import Link
from datatypes import ArchiveImage, SensorImage
class DummyLink:
def getArchiveImage(self):
a = ArchiveImage()
a.AverageWindSpeed = random.randint(1,100)
a.Gust = random.randint(1,100)
return a
def getSensorImage(self):
s = SensorImage()
s.IndoorTemperature = random.randint(1,100)
s.OutdoorTemperature = random.randint(1,100)
s.WindDirection = random.randint(1,100)
s.WindSpeed = random.randint(1,100)
return s
class Poller(threading.Thread):
def __init__(self):
#self.link = DummyLink()
self.link = Link()
#self.link.setArchiveTime(1)
#self.link.setSampleTime(5)
#self.aimg = self.link.getArchiveImage()
self.simg = self.link.getSensorImage()
threading.Thread.__init__ ( self )
def run(self):
gust = 0
#counter = 0
while 1:
time.sleep(2)
try:
self.simg = self.link.getSensorImage()
except Exception as e:
self.simg = None
print "Exception while getting sensor image!", e
import traceback
traceback.print_exc()
continue
#if self.simg.WindSpeed > gust:
# gust = self.simg.WindSpeed
print "\rWS " + str(self.simg.WindSpeed) + " WDIR " + \
str(self.simg.WindDirection) + " OTEMP " + \
str(self.simg.OutdoorTemperature) + " QNH " + str(self.simg.QFE) + \
" TIMESTAMP " + str(self.simg.Timestamp) + " ",
sys.stdout.flush()
#counter += 1
#if counter >= 3:
# counter = 0
# try:
# self.aimg = self.link.getArchiveImage()
# except:
# print "Exception while getting archive image!"
# self.aimg = None
# continue
#
# # Ugly hack to protect ourselves from strange readings when gust
# # in the beginning of the archive image is lower than average and
# # current wind speeds
# if self.aimg.AverageWindSpeed > self.aimg.Gust:
# self.aimg.Gust = self.aimg.AverageWindSpeed
#
# if self.aimg.Gust < gust:
# self.aimg.Gust = gust
#gust = 0
poller = Poller()
poller.daemon = True
poller.start()
class WxRequestHandler(SocketServer.BaseRequestHandler):
def setup(self):
#print self.client_address, 'connected!'
#a = poller.aimg
s = poller.simg
if s is None is None:
self.request.send("ERROR"+"\n")
return
self.request.send("WS " + str(s.WindSpeed) + "\n")
self.request.send("WSAVG "+str(s.AverageWindSpeed)+"\n")
self.request.send("WDIR " + str(s.WindDirection) + "\n")
#self.request.send("WDIRAVG "+str(a.DominantWindDirection)+"\n")
#self.request.send("GUST "+str(a.Gust)+"\n")
self.request.send("ITEMP " + str(s.IndoorTemperature) + "\n")
self.request.send("OTEMP " + str(s.OutdoorTemperature) + "\n")
self.request.send("ODEW " + str(round(s.OutdoorDewpoint, 1)) + "\n")
self.request.send("IRELH " + str(s.IndoorRelativeHumidity) + "\n")
self.request.send("ORELH " + str(s.OutdoorRelativeHumidity) + "\n")
self.request.send("QFE " + str(round(s.QFE, 1)) + "\n")
self.request.send("QFETREND " + str(s.QFETrend) + "\n")
self.request.send("RRATE " + str(round(s.RainRate, 2)) + "\n")
self.request.send("RDAY "+ str(round(s.RainDay, 2)) + "\n")
self.request.send("FORECAST " + str(s.Forecast) + "\n")
self.request.send("TIMESTAMP " + str(s.Timestamp) + "\n")
def handle(self):
return
def finish(self):
#print self.client_address, 'disconnected!'
return
SocketServer.ThreadingTCPServer.allow_reuse_address = True
server = SocketServer.ThreadingTCPServer(('127.0.0.1', 6666), WxRequestHandler)
server.serve_forever()