-
Notifications
You must be signed in to change notification settings - Fork 7
/
HeavyWeatherService.py
executable file
·216 lines (179 loc) · 6.67 KB
/
HeavyWeatherService.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
#!/usr/bin/python
## This driver is based is based on reverse engineering of HeavyWeather 2800 v 1.54
## All copyright goes to La Crosse Technology (c) 2008
## Python port by Eddi De Pieri <[email protected]>
## Use this software as your own risk.
## Me and La Crosse Technology is not responsable for any damage using this software
## Really thanks tfa-dostmann.de to allow me to go forward with development
#from datetime import datetime
#from datetime import timedelta
import os
import logging
#import time
import threading
import traceback
#import shelve
#import mmap #http://docs.python.org/library/mmap.html
#import USBHardware
#import CWeatherTraits
#import CHistoryDataSet
#import CDataStore
import CCommunicationService
#import CCurrentWeatherData
import EConstants
#import sHID
from CWeatherStationConfig import CWeatherStationConfig
def handleError(self, record):
traceback.print_stack()
logging.Handler.handleError = handleError
#sHID = sHID.sHID()
#USBHardware = USBHardware.USBHardware()
#CCurrentWeatherData = CCurrentWeatherData.CCurrentWeatherData()
#CWeatherStationConfig = CWeatherStationConfig.CWeatherStationConfig()
#CCommunicationService = CCommunicationService.CCommunicationService()
#CWeatherTraits = CWeatherTraits.CWeatherTraits()
ERequestState=EConstants.ERequestState()
#def equal(a, b):
#return abs(a - b) < 1e-6
#
#if equal(f1, f2):
class ws28xxError(IOError):
"Used to signal an error condition"
#filehandler = open("WV5DataStore", 'w')
#pickle.dump(CDataStore.TransceiverSettings, filehandler)
if __name__ == "__main__":
import logging
import sys
#http://rosettacode.org/wiki/Keyboard_input/Keypress_check
import thread
import time
try:
from msvcrt import getch
except ImportError:
def getch():
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
char = None
def keypress():
global char
char = getch()
thread.start_new_thread(keypress, ())
#CRITICAL 50
#ERROR 40
#WARNING 30
#INFO 20
#DEBUG 10
#NOTSET 0
# logging.basicConfig(format='%(asctime)s %(name)s %(message)s',filename="HeavyWeatherService.log",level=logging.DEBUG)
# logging.basicConfig(format='%(asctime)s %(name)s.%(funcName)s %(message)s',filename="HeavyWeatherService.log",level=logging.DEBUG)
logging.basicConfig(format='%(asctime)s %(name)s.%(funcName)s %(message)s',filename="HeavyWeatherService.log",level=logging.INFO)
os.system( [ 'clear', 'cls' ][ os.name == 'nt' ] )
print "Initializing ws28xx...\r"
myCCommunicationService = CCommunicationService.CCommunicationService()
myCCommunicationService.DataStore.setCommModeInterval(3) #move me to setfrontendalive
Freq = 915
if myCCommunicationService.DataStore.getTransmissionFrequency() == 1:
myCCommunicationService.DataStore.TransceiverSettings.Frequency = 868300000
Freq = 868
def infoscreen(TransceiverSerNo,Freq):
os.system( [ 'clear', 'cls' ][ os.name == 'nt' ] )
print "ws28xx python driver\r"
print "====================\r"
print "Transceiver With SerNo %s at %d Mhz\r" % (TransceiverSerNo,Freq)
print "\r"
print "0: Current\r"
print "1: History\r"
print "2: GetConfig\r"
print "3: SetConfig\r"
print "4: SetTime\r"
print "5: Syncronize - press [v] key on Display then choose this option\r"
print "\r"
print "f: switch operating frequency (and exit)\r"
print "o: switch to old rainsensor (and exit)\r"
print "\r"
print "x: exit\r"
print "esc: exit\r"
print "\r"
sys.stdout.write("waiting until transceiver initialized")
while True:
sys.stdout.write(".")
time.sleep(0.5)
if myCCommunicationService.DataStore.getFlag_FLAG_TRANSCEIVER_PRESENT():
print "\r"
break
myCCommunicationService.DataStore.setDeviceRegistered(True); #temp hack
infoscreen(myCCommunicationService.DataStore.getTransceiverSerNo(),Freq)
try:
while True:
if char is not None:
print "Key pressed is %s\r" % char
#print "GetRequestType %d GetRequestState %d\r" % (myCCommunicationService.DataStore.getRequestType(), myCCommunicationService.DataStore.getRequestState())
if char == "0":
char = None
Weather = [0]
Weather[0]=[0]
if myCCommunicationService.DataStore.getRequestState() == ERequestState.rsFinished \
or myCCommunicationService.DataStore.getRequestState() == ERequestState.rsINVALID:
print "getRequestState == rsFinished or rsInvalid... ask for current weather again :-)\r"
TimeOut = myCCommunicationService.DataStore.getPreambleDuration() + myCCommunicationService.DataStore.getRegisterWaitTime()
myCCommunicationService.DataStore.GetCurrentWeather(Weather,TimeOut)
elif char == "1":
char = None
elif char == "2":
char = None
elif char == "3":
char = None
elif char == "4":
char = None
elif char == "5":
char = None
#if myCCommunicationService.DataStore.getDeviceId() == -1:
if True: #hack ident
print "TransceiverSerNo will be overwritten", myCCommunicationService.DataStore.getTransceiverSerNo()
print "DeviceID will be overwritten", myCCommunicationService.DataStore.getDeviceId()
TimeOut = myCCommunicationService.DataStore.getPreambleDuration() + myCCommunicationService.DataStore.getRegisterWaitTime()
print "FirstTimeConfig Timeout=%d" % TimeOut
ID=[0]
ID[0]=0
myCCommunicationService.DataStore.FirstTimeConfig(ID,TimeOut)
print ID[0]
elif char == "F" or char == "f":
char = None
FreqID = myCCommunicationService.DataStore.getTransmissionFrequency()
if FreqID == 1:
FreqID = 0
else:
FreqID = 1
FreqID = myCCommunicationService.DataStore.setTransmissionFrequency(FreqID)
myCCommunicationService.kill_received = True
break
elif char == "O" or char == "o":
char = None
myCCommunicationService.kill_received = True
break
elif char==chr(27) or char == "x":
char = None
myCCommunicationService.kill_received = True
#os._exit(12)
break
else:
char = None
Weather = [0]
Weather[0]=[0]
if myCCommunicationService.DataStore.getRequestState() == ERequestState.rsFinished \
or myCCommunicationService.DataStore.getRequestState() == ERequestState.rsINVALID:
print "getRequestState == rsFinished or rsInvalid... ask for current weather again :-)\r"
TimeOut = myCCommunicationService.DataStore.getPreambleDuration() + myCCommunicationService.DataStore.getRegisterWaitTime()
myCCommunicationService.DataStore.GetCurrentWeather(Weather,TimeOut)
time.sleep(1)
except KeyboardInterrupt:
print "Ctrl-c received! Sending kill to threads..."
myCCommunicationService.kill_received = True
raise