python-awips/awips/NotificationMessage.py

166 lines
5.8 KiB
Python
Raw Permalink Normal View History

2015-06-12 11:57:06 -06:00
##
2018-06-19 09:18:30 -06:00
# This software was developed and / or modified by Raytheon Company,
# pursuant to Contract DG133W-05-CQ-1067 with the US Government.
#
# U.S. EXPORT CONTROLLED TECHNICAL DATA
# 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.
#
# Contractor Name: Raytheon Company
# Contractor Address: 6825 Pine Street, Suite 340
# Mail Stop B8
# Omaha, NE 68106
# 402.291.0100
#
# See the AWIPS II Master Rights File ("Master Rights File.pdf") for
# further licensing information.
2015-06-12 11:57:06 -06:00
##
2018-06-19 09:18:30 -06:00
from __future__ import print_function
2015-06-12 11:57:06 -06:00
2018-06-19 09:18:30 -06:00
import stomp
2015-06-12 11:57:06 -06:00
import socket
import sys
import time
2016-03-16 16:32:17 -05:00
import xml.etree.ElementTree as ET
2015-06-12 11:57:06 -06:00
2018-06-19 09:18:30 -06:00
import ThriftClient
2015-06-12 11:57:06 -06:00
from dynamicserialize.dstypes.com.raytheon.uf.common.alertviz import AlertVizRequest
#
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.
2018-06-19 09:18:30 -06:00
# 03/05/18 6899 dgilling Update to latest version of stomp.py API.
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):
2018-06-19 09:18:30 -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 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
2018-06-19 09:18:30 -06:00
conn = stomp.Connection(host_and_ports=[(self.host, 61999)],
timeout=5.)
2015-06-12 11:57:06 -06:00
try:
conn.start()
2018-06-19 09:18:30 -06:00
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)
if self.filters:
sm.set("filters", self.filters)
msg = ET.SubElement(sm, "message")
msg.text = self.message
msg = ET.tostring(sm, "UTF-8")
conn.send(destination='/queue/messages', body=msg, content_type='application/xml;charset=utf-8')
2015-06-12 11:57:06 -06:00
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)
except Exception as ex:
2018-06-19 09:18:30 -06:00
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"):
2018-06-19 09:18:30 -06:00
print("Error occurred submitting Notification Message to AlertViz receiver:", serverResponse)
2015-06-12 11:57:06 -06:00
sys.exit(1)
else:
2018-06-19 09:18:30 -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()