From ee4bbb1b3c9fe04d285e1d3cd5746b7a26043a5d Mon Sep 17 00:00:00 2001
From: James Meyer <james.meyer@operamail.com>
Date: Tue, 19 Aug 2014 13:55:16 -0500
Subject: supplemental-web: Add menu item for frontend status. The page takes a
 bit to load because it queries the frontends in real time, every time. We
 need to add a please wait screen or some other dialog box in the future.

Also removed remyth from the menu because it's a dead product.

refs #973
---
 abs/core/supplemental-web/PKGBUILD                 |   2 +-
 abs/core/supplemental-web/contents/fe_status.py    | 132 +++++++++++++++++++++
 abs/core/supplemental-web/contents/fe_status.shtml |  12 ++
 abs/core/supplemental-web/contents/header.html     |   3 +-
 4 files changed, 147 insertions(+), 2 deletions(-)
 create mode 100644 abs/core/supplemental-web/contents/fe_status.py
 create mode 100644 abs/core/supplemental-web/contents/fe_status.shtml

diff --git a/abs/core/supplemental-web/PKGBUILD b/abs/core/supplemental-web/PKGBUILD
index 42bbf6a..3f9e8ef 100644
--- a/abs/core/supplemental-web/PKGBUILD
+++ b/abs/core/supplemental-web/PKGBUILD
@@ -1,7 +1,7 @@
 pkgbase=supplemental-web
 pkgname=('supplemental-web' 'supplemental-web-slave')
 pkgver=8.0
-pkgrel=17
+pkgrel=18
 arch=('i686' 'x86_64')
 license=('GPL')
 backup=('data/srv/httpd/cgi/extra.cfg.txt')
diff --git a/abs/core/supplemental-web/contents/fe_status.py b/abs/core/supplemental-web/contents/fe_status.py
new file mode 100644
index 0000000..49bbf95
--- /dev/null
+++ b/abs/core/supplemental-web/contents/fe_status.py
@@ -0,0 +1,132 @@
+#!/usr/bin/python2
+
+import urllib2
+import xml.etree.ElementTree as ET
+import socket
+from  MythTV import Frontend
+import sys
+#socket.setdefaulttimeout(1)
+#socket.setdefaulttimeout(.00001)
+
+def msg(msg):
+    #if cmdargs.silent is False:
+    print "%s" %msg
+
+
+def parse_xml(frontend):
+    temp_dict = {'state':" --- ",
+                 'title':" --- ",
+                 'location':" --- "}
+    url = "http://%s:6547/Frontend/GetStatus" %frontend
+    try:
+        request = urllib2.Request(url)
+        xml = urllib2.urlopen(request,timeout=1)
+        tree = ET.parse(xml)
+        root = tree.getroot()
+    except:
+        msg("   Couldn't connect to %s" %frontend)
+        return temp_dict
+
+    lst = root.find("State")
+    for item in lst:
+            try:
+                keyitem = (item.find('Key').text).strip()
+                valueitem  = (item.find('Value').text).strip()
+            except:
+                continue
+            if keyitem == "state":
+                temp_dict['state'] = valueitem
+
+            elif keyitem == "title":
+                temp_dict['title'] = valueitem
+
+            elif keyitem == "currentlocation":
+                temp_dict['location'] = valueitem
+
+    return temp_dict
+
+
+def mythfe_status(cursor,mythDB):
+    frontends=list(Frontend.fromUPNP())
+    status_dict={}
+    #try:
+        #frontends = mythDB.getFrontends() #use cursor instead so it doesn't test connection
+        #cursor.execute("select hostname from settings where value = 'FrontendIdleTimeout'")
+        #frontends=cursor.fetchall()
+    #except:
+        #msg("Excptions")
+        #return
+
+    for i in frontends:
+        try:
+            fe_hostname = socket.gethostbyaddr(i.host)[0]
+        except:
+            print "DNS lookup failed for %s" %i.host
+            fe_hostname = i.host
+        status_dict[fe_hostname] = parse_xml(i.host)
+
+    return status_dict
+
+
+def print_html(status_dict):
+    print '<div> <p></p>'
+    print '</br> </br> '
+    print '<table class="calllog">'
+
+    #print "Current state of all online frontends"
+    #print '</br> </br> '
+
+    #print status_dict
+    row = '''
+<tr>
+    <td> %s  </td>
+    <td> %s  </td>
+    <td> %s  </td>
+    <td> %s </td>
+</tr>
+'''
+    print row %("  Frontend  ","  State  ","  Title  ","  MythTV Location")
+    for fe in status_dict.keys():
+        temp_dict = status_dict[fe]
+        print row %(fe,
+                    temp_dict['state'],
+                    temp_dict['title'],
+                    temp_dict['location'])
+
+    print "</table>"
+    print '</div>'
+
+    print '''
+    <div id="footer">
+       </br>
+       <p>Offline systems will not be listed</p>
+    </div>
+'''
+
+
+def main():
+    try:
+        from MythTV import MythDB
+        mythDB = MythDB()
+        cursor = mythDB.cursor()
+        db_conn=True
+    except:
+        msg("Couldn't connect to MythTV database.")
+        db_conn=False
+
+    try:
+        from MythTV import MythBE
+        mythBE = MythBE()
+        be_conn=True
+    except:
+        msg("Couldn't connect to MythTV backend.")
+        be_conn=False
+
+    if (db_conn):
+        status_dict = mythfe_status(cursor,mythDB)
+        print_html(status_dict)
+
+
+
+if __name__ == "__main__":
+    main()
diff --git a/abs/core/supplemental-web/contents/fe_status.shtml b/abs/core/supplemental-web/contents/fe_status.shtml
new file mode 100644
index 0000000..d821600
--- /dev/null
+++ b/abs/core/supplemental-web/contents/fe_status.shtml
@@ -0,0 +1,12 @@
+<html>
+    <head>
+        <style type="text/css">@import "/frame.css";</style>
+    </head>
+    <body>
+        <div id="header">
+                    <h1>LinHES Frontend Status </h1>
+
+        </div>
+        <!--#exec cmd="/root/fe_status.py" -->
+    </body>
+</html>
diff --git a/abs/core/supplemental-web/contents/header.html b/abs/core/supplemental-web/contents/header.html
index 032acd1..772cb52 100644
--- a/abs/core/supplemental-web/contents/header.html
+++ b/abs/core/supplemental-web/contents/header.html
@@ -59,9 +59,10 @@ function beStatus()
     <ul>
 <!--        <li><a onClick=r() >MythBackend Status</a></li> -->
         <li><a href="calllog.shtml">Call Log</a></li>
+        <li><a href="fe_status.shtml">Frontend status</a></li>
         <li><a href="mythexport/setup.cgi">MythExport</a></li>
         <li><a href="mythexpress.html">MythExpress</a></li>
-        <li><a href="remyth.html" >ReMyth</a></li>
+<!--    <li><a href="remyth.html" >ReMyth</a></li> -->
         <li><a href="zm">ZoneMinder</a></li>
     </ul>
 </li>
-- 
cgit v0.12

