python-awips/awips/NotificationMessage.py

184 lines
6.4 KiB
Python
Raw Normal View History

2015-06-12 11:57:06 -06:00
##
# This software was developed and / or modified by Raytheon Company,
2016-04-16 17:00:50 -06:00
# pursuant to Contract DG133W-05-CQ-1067 with the US Government.
2017-01-06 12:00:14 -07:00
#
2016-04-16 17:00:50 -06:00
# U.S. EXPORT CONTROLLED TECHNICAL DATA
2015-06-12 11:57:06 -06:00
# This software product contains export-restricted data whose
# export/transfer/disclosure is restricted by U.S. law. Dissemination
# to non-U.S. persons whether in the United States or abroad requires
# an export license or other authorization.
2016-03-16 16:32:17 -05:00
#
2016-04-16 17:00:50 -06:00
# Contractor Name: Raytheon Company
# Contractor Address: 6825 Pine Street, Suite 340
# Mail Stop B8
# Omaha, NE 68106
# 402.291.0100
2017-01-06 12:00:14 -07:00
#
2015-06-12 11:57:06 -06:00
# See the AWIPS II Master Rights File ("Master Rights File.pdf") for
# further licensing information.
##
from string import Template
import ctypes
2016-04-16 17:00:50 -06:00
from . import stomp
2015-06-12 11:57:06 -06:00
import socket
import sys
import time
import threading
2016-03-16 16:32:17 -05:00
import xml.etree.ElementTree as ET
2015-06-12 11:57:06 -06:00
2016-04-16 17:00:50 -06:00
from . import ThriftClient
2015-06-12 11:57:06 -06:00
from dynamicserialize.dstypes.com.raytheon.uf.common.alertviz import AlertVizRequest
from dynamicserialize import DynamicSerializationManager
#
2016-03-16 16:32:17 -05:00
# Provides a capability of constructing notification messages and sending
2015-06-12 11:57:06 -06:00
# them to a STOMP data source.
2016-03-16 16:32:17 -05:00
#
#
2015-06-12 11:57:06 -06:00
# SOFTWARE HISTORY
2016-03-16 16:32:17 -05:00
#
2015-06-12 11:57:06 -06:00
# Date Ticket# Engineer Description
# ------------ ---------- ----------- --------------------------
# 09/30/08 chammack Initial Creation.
2016-03-11 15:05:01 -07:00
# 11/03/10 5849 cjeanbap Moved to awips package from
2015-06-12 11:57:06 -06:00
# com.raytheon.uf.tools.cli
# 01/07/11 5645 cjeanbap Added audio file to Status Message.
# 05/27/11 3050 cjeanbap Added if-statement to check Priority
# value
2017-01-06 12:00:14 -07:00
# 07/27/15 4654 skorolev Added filters
# 11/11/15 5120 rferrel Cannot serialize empty filters.
2015-06-12 11:57:06 -06:00
#
class NotificationMessage:
2016-03-16 16:32:17 -05:00
priorityMap = {
2015-06-12 11:57:06 -06:00
0: 'CRITICAL',
1: 'SIGNIFICANT',
2: 'PROBLEM',
3: 'EVENTA',
4: 'EVENTB',
5: 'VERBOSE'}
2017-01-06 12:00:14 -07:00
def __init__(self, host='localhost', port=61999, message='', priority='PROBLEM', category="LOCAL", source="ANNOUNCER", audioFile="NONE", filters=None):
2015-06-12 11:57:06 -06:00
self.host = host
self.port = port
self.message = message
self.audioFile = audioFile
self.source = source
2016-03-16 16:32:17 -05:00
self.category = category
2017-01-06 12:00:14 -07:00
self.filters = filters
2015-06-12 11:57:06 -06:00
priorityInt = None
try:
priorityInt = int(priority)
except:
pass
if priorityInt is None:
#UFStatus.java contains mapping of Priority to Logging level mapping
if priority == 'CRITICAL' or priority == 'FATAL':
priorityInt = int(0)
elif priority == 'SIGNIFICANT' or priority == 'ERROR':
priorityInt = int(1)
elif priority == 'PROBLEM' or priority == 'WARN':
priorityInt = int(2)
elif priority == 'EVENTA' or priority == 'INFO':
priorityInt = int(3)
elif priority == 'EVENTB':
priorityInt = int(4)
elif priority == 'VERBOSE' or priority == 'DEBUG':
2016-03-16 16:32:17 -05:00
priorityInt = int(5)
if (priorityInt < 0 or priorityInt > 5):
2016-04-16 17:00:50 -06:00
print("Error occurred, supplied an invalid Priority value: " + str(priorityInt))
print("Priority values are 0, 1, 2, 3, 4 and 5.")
2016-03-16 16:32:17 -05:00
sys.exit(1)
2015-06-12 11:57:06 -06:00
if priorityInt is not None:
self.priority = self.priorityMap[priorityInt]
else:
self.priority = priority
def connection_timeout(self, connection):
if (connection is not None and not connection.is_connected()):
2016-04-16 17:00:50 -06:00
print("Connection Retry Timeout")
for tid, tobj in list(threading._active.items()):
2015-06-12 11:57:06 -06:00
if tobj.name is "MainThread":
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(SystemExit))
if res != 0 and res != 1:
# problem, reset state
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0)
def send(self):
# depending on the value of the port number indicates the distribution
2016-03-16 16:32:17 -05:00
# of the message to AlertViz
2015-06-12 11:57:06 -06:00
# 9581 is global distribution thru ThriftClient to Edex
# 61999 is local distribution
2017-01-06 12:00:14 -07:00
if (int(self.port) == 61999):
2015-06-12 11:57:06 -06:00
# use stomp.py
2017-01-06 12:00:14 -07:00
conn = stomp.Connection(host_and_ports=[(self.host, 61999)])
2015-06-12 11:57:06 -06:00
timeout = threading.Timer(5.0, self.connection_timeout, [conn])
try:
timeout.start();
conn.start()
finally:
timeout.cancel()
conn.connect()
sm = ET.Element("statusMessage")
sm.set("machine", socket.gethostname())
sm.set("priority", self.priority)
sm.set("category", self.category)
sm.set("sourceKey", self.source)
sm.set("audioFile", self.audioFile)
2017-01-06 12:00:14 -07:00
if self.filters is not None and len(self.filters) > 0:
sm.set("filters", self.filters)
2015-06-12 11:57:06 -06:00
msg = ET.SubElement(sm, "message")
msg.text = self.message
details = ET.SubElement(sm, "details")
msg = ET.tostring(sm, "UTF-8")
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
try :
conn.send(msg, destination='/queue/messages')
time.sleep(2)
finally:
conn.stop()
else:
# use ThriftClient
2017-01-06 12:00:14 -07:00
alertVizRequest = createRequest(self.message, self.priority, self.source, self.category, self.audioFile, self.filters)
2015-06-12 11:57:06 -06:00
thriftClient = ThriftClient.ThriftClient(self.host, self.port, "/services")
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
serverResponse = None
try:
serverResponse = thriftClient.sendRequest(alertVizRequest)
2016-04-16 17:00:50 -06:00
except Exception as ex:
print("Caught exception submitting AlertVizRequest: ", str(ex))
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
if (serverResponse != "None"):
2016-04-16 17:00:50 -06:00
print("Error occurred submitting Notification Message to AlertViz receiver: ", serverResponse)
2015-06-12 11:57:06 -06:00
sys.exit(1)
else:
2016-04-16 17:00:50 -06:00
print("Response: " + str(serverResponse))
2016-03-16 16:32:17 -05:00
2017-01-06 12:00:14 -07:00
def createRequest(message, priority, source, category, audioFile, filters):
2015-06-12 11:57:06 -06:00
obj = AlertVizRequest()
2016-03-16 16:32:17 -05:00
obj.setMachine(socket.gethostname())
2015-06-12 11:57:06 -06:00
obj.setPriority(priority)
obj.setCategory(category)
2016-03-16 16:32:17 -05:00
obj.setSourceKey(source)
2015-06-12 11:57:06 -06:00
obj.setMessage(message)
if (audioFile is not None):
obj.setAudioFile(audioFile)
else:
obj.setAudioFile('\0')
2017-01-06 12:00:14 -07:00
obj.setFilters(filters)
2015-06-12 11:57:06 -06:00
return obj
if __name__ == '__main__':
2016-03-16 16:32:17 -05:00
main()