-
Notifications
You must be signed in to change notification settings - Fork 5
/
sr0wx.py
329 lines (261 loc) · 10.1 KB
/
sr0wx.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/python -tt
# -*- coding: utf-8 -*-
COLOR_HEADER = '\033[95m'
COLOR_OKBLUE = '\033[94m'
COLOR_OKGREEN = '\033[92m'
COLOR_WARNING = '\033[93m'
COLOR_FAIL = '\033[91m'
COLOR_BOLD = '\033[1m'
COLOR_UNDERLINE = '\033[4m'
COLOR_ENDC = '\033[0m'
LICENSE = COLOR_OKBLUE + """
Copyright 2009-2014 Michal Sadowski (sq6jnx at hamradio dot pl)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-----------------------------------------------------------
You can find full list of contributors on github.com/sq6jnx/sr0wx.py
""" + COLOR_ENDC
#
#
# ********
# sr0wx.py
# ********
#
# This is main program file for automatic weather station project;
# codename SR0WX.
#
# (At the moment) SR0WX can read METAR [#]_ weather informations and
# is able to read them aloud in Polish. SR0WX is fully extensible, so
# it's easy to make it read any other data in any language (I hope).
#
# .. [#] http://en.wikipedia.org/wiki/METAR
#
# =====
# About
# =====
#
# Every automatic station's callsign in Poland (SP) is prefixed by "SR".
# This software is intended to read aloud weather informations (mainly).
# That's why we (or I) called it SR0WX.
#
# Extensions (mentioned above) are called ``modules`` (or ``languages``).
# Main part of SR0WX is called ``core``.
#
# SR0WX consists quite a lot of independent files so I (SQ6JNX) suggest
# reading other manuals (mainly configuration- and internationalization
# manual) in the same time as reading this one. Really.
#
# ============
# Requirements
# ============
#
# SR0WX (core) requires the following packages:
import getopt
import os
import pygame
import sys
import logging, logging.handlers
import numpy
import urllib2
# ``os``, ``sys`` and ``time`` doesn't need further explanation, these are
# syandard Python packages.
#
# ``pygame`` [#]_ is a library helpful for game development, this project
# uses it for reading (playing) sound samples. ``config`` is just a
# SR0WX configuration file and it is described separately.
#
# ..[#] www.pygame.org
#
# =========
# Let's go!
# =========
#
# For infrmational purposes script says hello and gives local time/date,
# so it will be possible to find out how long script was running.
# Logging configuration
def setup_logging(config):
# create formatter and add it to the handlers
formatter = logging.Formatter(config.log_line_format)
# Creating logger with the lowest log level in config handlers
min_log_level = min([h['log_level'] for h in config.log_handlers])
logger = logging.getLogger()
logger.setLevel(min_log_level)
# create logging handlers according to its definitions
for handler_definition in config.log_handlers:
handler = handler_definition['class'](**handler_definition['config'])
handler.setLevel(handler_definition['log_level'])
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
def my_import(name):
mod = __import__(name)
components = name.split('.')
for comp in components[1:]:
mod = getattr(mod, comp)
return mod
#
# All datas returned by SR0WX modules will be stored in ``data`` variable.
message = " "
# Information about which modules are to be executed is written in SR0WX
# config file. Program starts every single of them and appends it's return
# value in ``data`` variable. As you can see every module is started with
# language variable, which is also defined in configuration.
# Refer configuration and internationalization manuals for further
# informations.
#
# Modules may be also given in commandline, separated by a comma.
config = None
try:
opts, args = getopt.getopt(sys.argv[1:], "c:", ["config="])
except getopt.GetoptError:
pass
for opt, arg in opts:
if opt in ("-c", "--config"):
if arg[-3:] == '.py':
arg = arg[:-3]
config = __import__(arg)
if config is None:
import config
logger = setup_logging(config)
logger.info(COLOR_WARNING + "sr0wx.py started" + COLOR_ENDC)
logger.info(LICENSE)
if len(args) > 0:
modules = args[0].split(",")
else:
modules = config.modules
try:
dane = urllib2.urlopen('http://google.pl', None, 30);
except urllib2.URLError, e:
modules = []
message += " ".join(config.data_sources_error_msg)
logger.info(COLOR_FAIL + "Brak połączenia z internetem" + COLOR_ENDC + "\n")
except socket.timeout:
modules = []
message += " ".join(config.data_sources_error_msg)
logger.info(COLOR_FAIL + "Brak połączenia z internetem" + COLOR_ENDC + "\n")
lang = my_import('.'.join((config.lang, config.lang)))
sources = [lang.source, ]
for module in modules:
try:
logger.info(COLOR_OKGREEN + "starting %s..." + COLOR_ENDC, module)
module_data = module.get_data()
module_message = module_data.get("message", "")
module_source = module_data.get("source", "")
message = " ".join((message, module_message))
if module_message != "" and module_source != "":
sources.append(module_data['source'])
except:
logger.exception(COLOR_FAIL + "Exception when running %s"+ COLOR_ENDC, module)
# When all the modules finished its' work it's time to ``.split()`` returned
# data. Every element of returned list is actually a filename of a sample.
message = config.hello_msg + message.split()
if hasattr(config, 'read_sources_msg'):
if config.read_sources_msg:
if len(sources) > 1:
message += sources
else:
message += sources
message += config.goodbye_msg
# It's time to init ``pygame``'s mixer (and ``pygame``). Possibly defined
# sound quality is far-too-good (44kHz 16bit, stereo), so you can change it.
pygame.mixer.init(16000, -16, 2, 1024)
# Next (as a tiny timesaver & memory eater ;) program loads all necessary
# samples into memory. I think that this is better approach than reading
# every single sample from disk in the same moment when it's time to play it.
# Just for debug: our playlist (whole message as a list of filenames)
playlist = []
for el in message:
if "upper" in dir(el):
playlist.append(el)
else:
playlist.append("[sndarray]")
if hasattr(config, 'ctcss_tone'):
volume = 25000
arr = numpy.array([volume * numpy.sin(2.0 * numpy.pi * round(config.ctcss_tone) * x / 16000) for x in range(0, 16000)]).astype(numpy.int16)
arr2 = numpy.c_[arr,arr]
ctcss = pygame.sndarray.make_sound(arr2)
logger.info(COLOR_WARNING + "CTCSS tone %sHz" + COLOR_ENDC + "\n", "%.1f" % config.ctcss_tone)
ctcss.play(-1)
logger.info("playlist elements: %s", " ".join(playlist)+"\n")
logger.info("loading sound samples...")
logger.info("playing sound samples\n")
sound_samples = {}
for el in message:
if "upper" in dir(el):
if el[0:7] == 'file://':
sound_samples[el] = pygame.mixer.Sound(el[7:])
if el is not "_" and el not in sound_samples:
if not os.path.isfile(config.lang + "/" + el + ".ogg"):
logger.warn(COLOR_FAIL + "Couldn't find %s" % (config.lang + "/" + el + ".ogg" + COLOR_ENDC))
sound_samples[el] = pygame.mixer.Sound(config.lang + "/beep.ogg")
else:
sound_samples[el] = pygame.mixer.Sound(config.lang + "/" + el + ".ogg")
# Program should be able to "press PTT" via RSS232. See ``config`` for
# details.
if config.serial_port is not None:
import serial
try:
ser = serial.Serial(config.serial_port, config.serial_baud_rate)
if config.serial_signal == 'DTR':
logger.info(COLOR_OKGREEN + "DTR/PTT set to ON\n" + COLOR_ENDC)
ser.setDTR(1)
ser.setRTS(0)
else:
logger.info(COLOR_OKGREEN + "RTS/PTT set to ON\n" + COLOR_ENDC)
ser.setDTR(0)
ser.setRTS(1)
except:
log = COLOR_FAIL + "Failed to open serial port %s@%i\n" + COLOR_ENDC
logger.error(log, config.serial_port, config.serial_baud_rate)
pygame.time.delay(1000)
# OK, data prepared, samples loaded, let the party begin!
#
# Take a look at ``while`` condition -- program doesn't check if the
# sound had finished played all the time, but only 25 times/sec (default).
# It is because I don't want 100% CPU usage. If you don't have as fast CPU
# as mine (I think you have, though) you can always lower this value.
# Unfortunately, there may be some pauses between samples so "reading
# aloud" will be less natural.
for el in message:
#print el # wyświetlanie nazw próbek
if el == "_":
pygame.time.wait(500)
else:
if "upper" in dir(el):
try:
voice_channel = sound_samples[el].play()
except:
a=1
elif "upper" not in dir(el):
sound = pygame.sndarray.make_sound(el)
if config.pygame_bug == 1:
sound = pygame.sndarray.make_sound(pygame.sndarray.array(sound)[:len(pygame.sndarray.array(sound))/2])
voice_channel = sound.play()
while voice_channel.get_busy():
pygame.time.Clock().tick(25)
# Possibly the argument of ``pygame.time.Clock().tick()`` should be in
# config file...
#
# The following four lines give us a one second break (for CTCSS, PTT and
# other stuff) before closing the ``pygame`` mixer and display some debug
# informations.
logger.info(COLOR_WARNING + "finishing...\n" + COLOR_ENDC)
pygame.time.delay(1000)
# If we've opened serial it's now time to close it.
try:
if config.serial_port is not None:
ser.close()
logger.info(COLOR_OKGREEN + "RTS/PTT set to OFF\n" + COLOR_ENDC)
except NameError:
# sudo gpasswd --add ${USER} dialout
logger.exception(COLOR_FAIL + "Couldn't close serial port" + COLOR_ENDC)
logger.info(COLOR_WARNING + "goodbye" + COLOR_ENDC)
# Documentation is a good thing when you need to double or triple your
# Lines-Of-Code index ;)