| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | """ |
|---|
| 4 | StormSiren |
|---|
| 5 | Copyright (C) 2008 Chris Freeze <cfreeze/cfreeze_com\> |
|---|
| 6 | |
|---|
| 7 | Redistribution and use in source and binary forms, with or without |
|---|
| 8 | modification, are permitted provided that the following conditions |
|---|
| 9 | are met: |
|---|
| 10 | |
|---|
| 11 | 1. Redistributions of source code must retain the above copyright |
|---|
| 12 | notice, this list of conditions and the following disclaimer. |
|---|
| 13 | 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 14 | notice, this list of conditions and the following disclaimer in the |
|---|
| 15 | documentation and/or other materials provided with the distribution. |
|---|
| 16 | |
|---|
| 17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|---|
| 18 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|---|
| 19 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|---|
| 20 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
|---|
| 21 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|---|
| 22 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|---|
| 26 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 27 | """ |
|---|
| 28 | |
|---|
| 29 | """ |
|---|
| 30 | This is StormSiren a program inspired by the StormSiren application |
|---|
| 31 | written by Roy McManus <slorf/users_sourceforge_net>. Much like the |
|---|
| 32 | original StormSiren written by Roy McManus <slorf/users_sourceforge_net>, |
|---|
| 33 | this program is a simple script capable of scanning and providing |
|---|
| 34 | notifications of weather bulletins issued by the National |
|---|
| 35 | Weather Service. StormSiren supports a wide range of paging devices |
|---|
| 36 | and filtering of alert types per alert device. While inspired by |
|---|
| 37 | StormSiren, StormSiren is a complete rewrite that is capable of using |
|---|
| 38 | the new CAP/XML feeds provided at http://www.weather.gov/alerts/. |
|---|
| 39 | |
|---|
| 40 | For more information there is see the README.TXT file located in the root |
|---|
| 41 | of this directory. |
|---|
| 42 | """ |
|---|
| 43 | |
|---|
| 44 | import logging |
|---|
| 45 | import sys |
|---|
| 46 | import profile |
|---|
| 47 | from optparse import OptionParser |
|---|
| 48 | |
|---|
| 49 | from StormSiren.TestAlert import TestAlert |
|---|
| 50 | from StormSiren.WeatherAlert import WeatherAlert |
|---|
| 51 | from StormSiren.StormWeather import StormWeather |
|---|
| 52 | from StormSiren.StormConfig import StormConfig |
|---|
| 53 | from StormSiren.History import History, DEFAULT_MAX_HISTORICAL_EVENTS |
|---|
| 54 | |
|---|
| 55 | __RELEASE_VERSION = "2.0 RC4" |
|---|
| 56 | __RELEASE_MONTH = "12" |
|---|
| 57 | __RELEASE_DAY = "01" |
|---|
| 58 | __RELEASE_YEAR = "2008" |
|---|
| 59 | |
|---|
| 60 | logging.basicConfig(level=logging.INFO, |
|---|
| 61 | format='%(asctime)s - %(name)s:%(lineno)d - %(levelname)s - %(message)s') |
|---|
| 62 | |
|---|
| 63 | def main(): |
|---|
| 64 | parser = OptionParser() |
|---|
| 65 | log = logging.getLogger() |
|---|
| 66 | |
|---|
| 67 | log.setLevel(logging.DEBUG) |
|---|
| 68 | |
|---|
| 69 | parser.add_option("-c", "--conf", dest="conf", |
|---|
| 70 | help="Override configuration file", metavar="file_name", default=None) |
|---|
| 71 | |
|---|
| 72 | parser.add_option("-H", "--history", dest="history", |
|---|
| 73 | help="Override file used to save history", metavar="file_name", default=None) |
|---|
| 74 | |
|---|
| 75 | parser.add_option("-m", "--maxhistory", dest="maxhistory", |
|---|
| 76 | help="Override maximum history entries to maintain", metavar="integer", default=DEFAULT_MAX_HISTORICAL_EVENTS) |
|---|
| 77 | |
|---|
| 78 | parser.add_option("-s", "--simalert", |
|---|
| 79 | action="store_true", dest="simalert", default=False, |
|---|
| 80 | help="Simulate run and do not use alert devices") |
|---|
| 81 | |
|---|
| 82 | parser.add_option("-S", "--simfetch", |
|---|
| 83 | action="store_true", dest="simfetch", default=False, |
|---|
| 84 | help="Simulate the fetch using files in the examples directory") |
|---|
| 85 | |
|---|
| 86 | parser.add_option("-q", "--quiet", |
|---|
| 87 | action="store_true", dest="quiet", default=False, |
|---|
| 88 | help="Print only important messages to the console") |
|---|
| 89 | |
|---|
| 90 | parser.add_option("-t", "--testpage", |
|---|
| 91 | action="store_true", dest="testpage", default=False, |
|---|
| 92 | help="Send a test alert to all configured devices") |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | (options, args) = parser.parse_args() |
|---|
| 96 | |
|---|
| 97 | if(options.quiet): |
|---|
| 98 | logging.disable(logging.debug) |
|---|
| 99 | logging.disable(logging.info) |
|---|
| 100 | |
|---|
| 101 | conf = StormConfig(options.conf) |
|---|
| 102 | |
|---|
| 103 | log.info("StormSiren: v%s (%s/%s/%s)" % (__RELEASE_VERSION, |
|---|
| 104 | __RELEASE_MONTH, |
|---|
| 105 | __RELEASE_DAY, |
|---|
| 106 | __RELEASE_YEAR)) |
|---|
| 107 | log.info("Copyright (C) %s Chris Freeze <cfreeze\/cfreeze_com\>" % __RELEASE_YEAR) |
|---|
| 108 | log.info("This is free software and comes with no warranty; see the included LICENSE file for details.") |
|---|
| 109 | |
|---|
| 110 | history_file = options.history or conf.history_file |
|---|
| 111 | max_history = int(options.maxhistory) or conf.max_history |
|---|
| 112 | history = History(history_file, max_history) |
|---|
| 113 | |
|---|
| 114 | #conf.display() |
|---|
| 115 | #history.display() |
|---|
| 116 | |
|---|
| 117 | if(len(conf.devices) == 0): |
|---|
| 118 | log.error("No valid devices found, exiting..") |
|---|
| 119 | sys.exit(1) |
|---|
| 120 | |
|---|
| 121 | if(not options.testpage): |
|---|
| 122 | for state in conf.states: |
|---|
| 123 | log.info("State: " + state) |
|---|
| 124 | xw = StormWeather(state,history,options.simfetch,options.simalert,conf.proxy) |
|---|
| 125 | #xw.display() |
|---|
| 126 | for dev in conf.devices: |
|---|
| 127 | if(dev.isStateWanted(state)): |
|---|
| 128 | xw.registerDevice(dev) |
|---|
| 129 | xw.fetch() |
|---|
| 130 | |
|---|
| 131 | history.save() |
|---|
| 132 | else: |
|---|
| 133 | log.info("Sending test page to all devices") |
|---|
| 134 | ta = TestAlert("examples/test-alert.cap") |
|---|
| 135 | ta.fetch() |
|---|
| 136 | for dev in conf.devices: |
|---|
| 137 | log.info("\tSending test alert to:\n%s" % dev.__str__()) |
|---|
| 138 | dev.send(ta.weather_alert) |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | if __name__ == "__main__": |
|---|
| 142 | #profile.run("main()","siren.stats") |
|---|
| 143 | main() |
|---|