| 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 subprocess |
|---|
| 45 | import os |
|---|
| 46 | |
|---|
| 47 | from AlertDevice import * |
|---|
| 48 | |
|---|
| 49 | class ExecutionAlertDevice(AlertDevice): |
|---|
| 50 | def __init__(self,exeInfo,alerts,areas): |
|---|
| 51 | super(ExecutionAlertDevice,self).__init__(alerts,areas) |
|---|
| 52 | self.__exeInfo = exeInfo |
|---|
| 53 | self.log = logging.getLogger('ExecutionAlertDevice') |
|---|
| 54 | self.log.setLevel(logging.DEBUG) |
|---|
| 55 | |
|---|
| 56 | def display(self): |
|---|
| 57 | print self.__str__() |
|---|
| 58 | |
|---|
| 59 | def __str__(self): |
|---|
| 60 | str = "ExecutionAlertDevice\n" + \ |
|---|
| 61 | self.__exeInfo.__str__() + \ |
|---|
| 62 | super(ExecutionAlertDevice,self).__str__() |
|---|
| 63 | return str |
|---|
| 64 | |
|---|
| 65 | def send(self,alert): |
|---|
| 66 | if(alert.type & ALERT): |
|---|
| 67 | self._execute(self.__exeInfo.alert,alert) |
|---|
| 68 | if(alert.type & FORECAST): |
|---|
| 69 | self._execute(self.__exeInfo.forecast,alert) |
|---|
| 70 | if(alert.type & STATEMENT): |
|---|
| 71 | self._execute(self.__exeInfo.statement,alert) |
|---|
| 72 | if(alert.type & ADVISORY): |
|---|
| 73 | self._execute(self.__exeInfo.advisory,alert) |
|---|
| 74 | if(alert.type & WATCH): |
|---|
| 75 | self._execute(self.__exeInfo.watch,alert) |
|---|
| 76 | if(alert.type & WARNING): |
|---|
| 77 | self._execute(self.__exeInfo.warning,alert) |
|---|
| 78 | |
|---|
| 79 | def _execute(self,exe,alert): |
|---|
| 80 | try: |
|---|
| 81 | subprocess.Popen([exe,alert.id,"\"" + WeatherTypes.toString(alert.type) + "\"",alert.state,"\"" + alert.getAreas() + "\""]).wait() |
|---|
| 82 | self.log.info("Executing: [%s %s %s %s %s]" % (exe,alert.id,"\"" + WeatherTypes.toString(alert.type) + "\"",alert.state,"\"" + alert.getAreas() + "\"")) |
|---|
| 83 | except os.error: |
|---|
| 84 | self.log.error("Problem Executing: [%s %s %s %s %s]" % (exe,alert.id,"\"" + WeatherTypes.toString(alert.type),alert.state,"\"" + alert.getAreas() + "\"")) |
|---|
| 85 | |
|---|
| 86 | class ExecutionAlertDeviceInfo(object): |
|---|
| 87 | def __init__(self): |
|---|
| 88 | self.__forecast = None |
|---|
| 89 | self.__statement = None |
|---|
| 90 | self.__advisory = None |
|---|
| 91 | self.__watch = None |
|---|
| 92 | self.__warning = None |
|---|
| 93 | self.__alert = None |
|---|
| 94 | |
|---|
| 95 | def setAlert(self,alert): |
|---|
| 96 | self.__alert = alert |
|---|
| 97 | |
|---|
| 98 | def getAlert(self): |
|---|
| 99 | return self.__alert |
|---|
| 100 | |
|---|
| 101 | def setForecast(self,forecast): |
|---|
| 102 | self.__forecast = forecast |
|---|
| 103 | |
|---|
| 104 | def getForecast(self): |
|---|
| 105 | return self.__forecast |
|---|
| 106 | |
|---|
| 107 | def setStatement(self,statement): |
|---|
| 108 | self.__statement = statement |
|---|
| 109 | |
|---|
| 110 | def getStatement(self): |
|---|
| 111 | return self.__statement |
|---|
| 112 | |
|---|
| 113 | def setAdvisory(self,advisory): |
|---|
| 114 | self.__advisory = advisory |
|---|
| 115 | |
|---|
| 116 | def getAdvisory(self): |
|---|
| 117 | return self.__advisory |
|---|
| 118 | |
|---|
| 119 | def setWatch(self,watch): |
|---|
| 120 | self.__watch = watch |
|---|
| 121 | |
|---|
| 122 | def getWatch(self): |
|---|
| 123 | return self.__watch |
|---|
| 124 | |
|---|
| 125 | def setWarning(self,warning): |
|---|
| 126 | self.__warning = warning |
|---|
| 127 | |
|---|
| 128 | def getWarning(self): |
|---|
| 129 | return self.__warning |
|---|
| 130 | |
|---|
| 131 | alert = property(getAlert,setAlert,None) |
|---|
| 132 | forecast = property(getForecast,setForecast,None) |
|---|
| 133 | statement = property(getStatement,setStatement,None) |
|---|
| 134 | advisory = property(getAdvisory,setAdvisory,None) |
|---|
| 135 | watch = property(getWatch,setWatch,None) |
|---|
| 136 | warning = property(getWarning,setWarning,None) |
|---|
| 137 | |
|---|
| 138 | def display(self): |
|---|
| 139 | print self.__str__() |
|---|
| 140 | |
|---|
| 141 | def __str__(self): |
|---|
| 142 | str = "Execution Info\n" |
|---|
| 143 | |
|---|
| 144 | if(self.alert): |
|---|
| 145 | str += "\tAlert: " + self.alert + "\n" |
|---|
| 146 | |
|---|
| 147 | if(self.forecast): |
|---|
| 148 | str += "\tForecast: " + self.forecast + "\n" |
|---|
| 149 | |
|---|
| 150 | if(self.statement): |
|---|
| 151 | str += "\tStatement: " + self.statement + "\n" |
|---|
| 152 | |
|---|
| 153 | if(self.advisory): |
|---|
| 154 | str += "\tAdvisory: " + self.advisory + "\n" |
|---|
| 155 | |
|---|
| 156 | if(self.watch): |
|---|
| 157 | str += "\tWatch: " + self.watch + "\n" |
|---|
| 158 | |
|---|
| 159 | if(self.warning): |
|---|
| 160 | str += "\tWarning: " + self.warning + "\n" |
|---|
| 161 | |
|---|
| 162 | return str |
|---|