#!/usr/bin/env python

"""
StormSiren
Copyright (C) 2008  Chris Freeze <cfreeze/cfreeze_com\>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

"""
This is StormSiren a program inspired by the StormSiren application
written by Roy McManus <slorf/users_sourceforge_net>.  Much like the
original StormSiren written by Roy McManus <slorf/users_sourceforge_net>,
this program is a simple script capable of scanning and providing
notifications of weather bulletins issued by the National
Weather Service.  StormSiren supports a wide range of paging devices
and filtering of alert types per alert device.  While inspired by
StormSiren, StormSiren is a complete rewrite that is capable of using
the new CAP/XML feeds provided at http://www.weather.gov/alerts/.

For more information there is see the README.TXT file located in the root
of this directory.
"""

import logging
import time

from WeatherTypes import *
from WeatherAlert import *
from XmlWeather import *

class StormWeather(XmlWeather):
	def __init__(self, state, history, simfetch = False, simalert = False, proxy = None):
		super(StormWeather,self).__init__(state,simfetch,proxy)
		self.log = logging.getLogger("StormWeather")
		self.__alert_devs = []
		self.__simalert = simalert
		self.__history = history

		self.log.setLevel(logging.INFO)
	
	def __str__(self):
		str  = "StormWeather: " + "\n"
		if(self.__simalert):
			str  += "Sim Alert: yes\n"
		else:
			str  += "Sim Alert: no\n"

		str += super(StormWeather,self).__str__()
		return str

	def registerDevice(self, dev):
		self.log.debug("Registering Device")
		self.__alert_devs.append(dev)

	def logAtomShort(self, cap_atom):
		self.log.info("---------CAP ATOM---------")
		for l in (cap_atom.__str__().split('\n')):
			self.log.info(l)
		self.log.info("---------CAP ATOM---------")

	def logAtomFull(self, cap_atom):
		self.log.info("---------CAP ATOM---------")
		for l in (cap_atom.__str__().split('\n')):
			self.log.info(l)
		self.log.info("========DESCRIPTION=======")
		if(cap_atom.description):
			for l in (cap_atom.description.split('\n')):
				self.log.info(l)
		self.log.info("========DESCRIPTION=======")
		self.log.info("---------CAP ATOM---------")

	def handleAtom(self, cap_atom):
		self.logAtomShort(cap_atom)
		if(not self.__history.exists(cap_atom.id)):
			for dev in self.__alert_devs:
				if(dev.isAlertWanted(cap_atom)):
					if(not self.__simalert):
						cap_atom.expand(self.simfetch,self.proxy)
						self.logAtomFull(cap_atom)
						dev.send(cap_atom)
						self.__history.add(cap_atom.id)
					else:
						self.log.info("Simulated Alert on: %s" % cap_atom.id)
		else:
			self.log.debug("Ignoring %s: Already alerted on" % cap_atom.id)

