Skip to content

Commit

Permalink
added settings for httpport, implemented basic http server
Browse files Browse the repository at this point in the history
  • Loading branch information
saschaludwig committed Feb 20, 2019
1 parent 054cd18 commit c909398
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
10 changes: 10 additions & 0 deletions settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,16 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_12">
<property name="text">
<string>HTTP Port</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="httpport"/>
</item>
</layout>
</item>
<item row="1" column="0">
Expand Down
4 changes: 3 additions & 1 deletion settings_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from collections import defaultdict
import json

versionString = "0.9beta2"
versionString = "0.9beta4"


# class OASSettings for use from OAC
Expand Down Expand Up @@ -261,6 +261,7 @@ def restoreSettingsFromConfig(self):

settings.beginGroup("Network")
self.udpport.setText(settings.value('udpport', '3310'))
self.httpport.setText(settings.value('httpport', '8010'))
settings.endGroup()

settings.beginGroup("Formatting")
Expand Down Expand Up @@ -340,6 +341,7 @@ def getSettingsFromDialog(self):

settings.beginGroup("Network")
settings.setValue('udpport', self.udpport.displayText())
settings.setValue('httpport', self.httpport.displayText())
settings.endGroup()

settings.beginGroup("Formatting")
Expand Down
58 changes: 56 additions & 2 deletions start.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
import ntplib
import signal
import socket
from settings_functions import Settings
from settings_functions import Settings, versionString

from http.server import BaseHTTPRequestHandler, HTTPServer

HOST, PORT = '127.0.0.1', 12345


class MainScreen(QWidget, Ui_MainScreen):
Expand Down Expand Up @@ -174,6 +178,10 @@ def __init__(self):
self.udpsock.bind(port, QUdpSocket.ShareAddress)
self.udpsock.readyRead.connect(self.cmdHandler)

# Setup HTTP Server
self.httpd = HttpDaemon(self)
self.httpd.start()

# display all host addresses
self.displayAllHostaddresses()

Expand Down Expand Up @@ -1045,6 +1053,9 @@ def shutdown_host(self):
cmd = "shutdown -f -t 0"
pass

def closeEvent(self, event):
self.httpd.stop()


class checkNTPOffsetThread(QThread):

Expand Down Expand Up @@ -1086,7 +1097,50 @@ def run(self):
self.oas.ntpHadWarning = True


###################################
class HttpDaemon(QThread):
def run(self):
settings = QSettings(QSettings.UserScope, "astrastudio", "OnAirScreen")
settings.beginGroup("Network")
port = int(settings.value('httpport', 8010))
settings.endGroup()

Handler = OASHTTPRequestHandler
self._server = HTTPServer((HOST, port), Handler)
self._server.serve_forever()

def stop(self):
self._server.shutdown()
self._server.socket.close()
self.wait()


class OASHTTPRequestHandler(BaseHTTPRequestHandler):
server_version = "OnAirScreen/%s" % versionString
# handle HEAD request
def do_HEAD(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()

# handle GET command
def do_GET(self):
print(self.path)
try:
if self.path.startswith('/CMD'):
self.send_response(200)

# send header first
self.send_header('Content-type', 'text-html')
self.end_headers()

# send file content to client
self.wfile.write(b"YEAH")
return

except IOError:
self.send_error(404, 'file not found')

###################################
# App SIGINT handler
###################################
def sigint_handler(*args):
Expand Down

0 comments on commit c909398

Please sign in to comment.