-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from andrius-k/add-support-for-sound-alarm-man…
…ager Added visDQMSoundAlarmManager for enabling/disabling DQM P5 sound alarms
- Loading branch information
Showing
2 changed files
with
254 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
#!/usr/bin/env python | ||
|
||
import sys | ||
import json | ||
import urllib2 | ||
import SocketServer | ||
import SimpleHTTPServer | ||
from urllib import quote | ||
|
||
# Configurable parameters | ||
GUI_URL = 'http://localhost:8070/dqm/online-dev/' | ||
PORT = 8031 | ||
|
||
if len(sys.argv) > 1: | ||
GUI_URL = sys.argv[1] | ||
|
||
if len(sys.argv) > 2: | ||
PORT = int(sys.argv[2]) | ||
|
||
# Constants | ||
ERRORS_FOLDER = 'data/json/live/1/Global/Online/ALL/00 Shift/Errors' | ||
STATUSES = { 30: 'OTHER', 50: 'DISABLED', 60: 'INVALID', 70: 'INSUF_STAT', 90: 'DID_NOT_RUN', | ||
100: 'STATUS_OK', 200: 'WARNING', 300: 'ERROR' | ||
} | ||
|
||
# Dict with keys of disabled ME names | ||
DISABLED_MES = {} | ||
|
||
def start(): | ||
SocketServer.TCPServer.allow_reuse_address = True | ||
server = SocketServer.TCPServer(('0.0.0.0', PORT), CustomRequestHandler) | ||
try: | ||
server.serve_forever() | ||
except: | ||
pass | ||
server.shutdown() | ||
|
||
class CustomRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): | ||
def do_GET(self): | ||
|
||
# Return the main table | ||
if self.path == '/': | ||
state = self.get_gui_data() | ||
content = '' | ||
|
||
for index, item in enumerate(state['contents']): | ||
if 'obj' in item: | ||
me = '%s/%s' % (item['dir'], item['obj']) | ||
content += '<tr>' | ||
content += ' <td>%s</td>' % me | ||
content += ' <td>%s</td>' % ', '.join(str(x['name']) for x in item['qresults']) | ||
content += ' <td>%s</td>' % ', '.join(str(x['algorithm']) for x in item['qresults']) | ||
content += ' <td>%s</td>' % ', '.join(str(x['message']) for x in item['qresults']) | ||
content += ' <td>%s</td>' % ', '.join(str(STATUSES.get(x['status'], x['status'])) for x in item['qresults']) | ||
content += ' <td>%s</td>' % ', '.join(str(x['result']) for x in item['qresults']) | ||
|
||
if me in DISABLED_MES: | ||
content += ' <td class="danger">No</td>' | ||
content += ' <td><button onclick="enable(\'%s\')">Enable</button></td>' % me | ||
else: | ||
content += ' <td class="success">Yes</td>' | ||
content += ' <td><button onclick="disable(\'%s\')">Disable</button></td>' % me | ||
|
||
content += '</tr>' | ||
|
||
self.send_response(200) | ||
self.end_headers() | ||
self.wfile.write(TEMPLATE.replace('$CONTENT$', content)) | ||
|
||
# Return all enabled MEs | ||
elif self.path == '/disabled': | ||
result = [] | ||
for me in DISABLED_MES: | ||
if DISABLED_MES[me]: | ||
result.append(me) | ||
|
||
self.send_response(200) | ||
self.end_headers() | ||
self.wfile.write(json.dumps(result)) | ||
|
||
else: | ||
self.send_response(404) | ||
self.end_headers() | ||
self.wfile.write('<h1>404 Not Found</h1>') | ||
|
||
def do_POST(self): | ||
# Enable given ME | ||
if self.path.startswith('/enable?me='): | ||
me = self.path.split('/enable?me=', 1)[1] | ||
DISABLED_MES.pop(me, None) | ||
self.send_response(200) | ||
self.end_headers() | ||
self.wfile.write(me) | ||
|
||
# Disable given ME | ||
elif self.path.startswith('/disable?me='): | ||
me = self.path.split('/disable?me=', 1)[1] | ||
DISABLED_MES[me] = True | ||
self.send_response(200) | ||
self.end_headers() | ||
self.wfile.write(me) | ||
|
||
else: | ||
self.send_response(404) | ||
self.end_headers() | ||
self.wfile.write('<h1>404 Not Found</h1>') | ||
|
||
def do_HEAD(self): | ||
self.send_response(501) | ||
self.end_headers() | ||
self.wfile.write('<h1>501 Unsupported method</h1>') | ||
|
||
|
||
def get_gui_data(self): | ||
url = '%s%s' % (GUI_URL, quote(ERRORS_FOLDER)) | ||
|
||
try: | ||
datareq = urllib2.Request(url) | ||
result = urllib2.build_opener(urllib2.ProxyHandler({})).open(datareq) | ||
state = result.read() | ||
except: | ||
return json.loads('{"contents": [{ "streamerinfo": "" }]}') | ||
|
||
state = json.loads(state) | ||
|
||
return state | ||
|
||
|
||
TEMPLATE = ''' | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>DQM Sound Alarm Manager</title> | ||
<style> | ||
body { | ||
font-family: arial, sans-serif; | ||
} | ||
h4, h3, h2, p { | ||
padding-left: 6px; | ||
} | ||
table { | ||
border-collapse: collapse; | ||
} | ||
td, | ||
th { | ||
text-align: left; | ||
padding: 6px; | ||
} | ||
tr:nth-child(even) { | ||
background-color: #f1f1f1; | ||
} | ||
.danger { | ||
color: #ff4242; | ||
} | ||
.success { | ||
color: #00b30f; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h3>Enable/disable DQM sound alarms at P5</h3> | ||
<p>You can use this tool to enable/disable P5 sound alarms for certain DQM plot in the Errors folder of the DQM GUI. | ||
If the alarm for a plot is <i>enabled</i>, it <i>will play a sound alarm</i> in the control room.</p> | ||
<p>All alarms will switch to enabled state after the restart of the DQM GUI.</p> | ||
</br> | ||
<table> | ||
<tr> | ||
<th>Monitor element</th> | ||
<th>QResult name</th> | ||
<th>QResult algorithm</th> | ||
<th>QResult Message</th> | ||
<th>QResult Status</th> | ||
<th>QResult Result</th> | ||
<th>Enabled</th> | ||
<th>Action</th> | ||
</tr> | ||
$CONTENT$ | ||
</table> | ||
<script> | ||
function disable(me) { | ||
fetch('/disable?me=' + me, { | ||
method: 'post', | ||
}).then(function(response) { | ||
console.log(response) | ||
location.reload() | ||
}) | ||
} | ||
function enable(me) { | ||
fetch('/enable?me=' + me, { | ||
method: 'post', | ||
}).then(function(response) { | ||
console.log(response) | ||
location.reload() | ||
}) | ||
} | ||
</script> | ||
</body> | ||
</html> | ||
''' | ||
|
||
|
||
if __name__ == '__main__': | ||
start() |