-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
125 lines (98 loc) · 3.57 KB
/
gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import (QAction, QActionGroup, QApplication, QFrame,
QLabel, QMainWindow, QMenu, QMessageBox, QSizePolicy, QVBoxLayout,
QWidget)
import octoprint
import json
import requests
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# Load Settings from config.json
self.settings = self.loadSettings()
# Open Connection to api
if 'apikey' in self.settings.keys():
self.octopi = octoprint.Api('octopi.local',self.settings['apikey'])
else:
self.octopi = octoprint.Api('octopi.local')
# Timer for refreshing
self.timer = QTimer(self);
self.timer.setInterval(1000)
self.timer.timeout.connect(self.setTimeValue)
self.timer.start()
# Display Stuff
widget = QWidget()
self.setCentralWidget(widget)
topFiller = QWidget()
topFiller.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.infoLabel = QLabel(
"<h1>Time Left</h1> ",
alignment=Qt.AlignCenter)
self.infoLabel.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken)
bottomFiller = QWidget()
bottomFiller.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
vbox = QVBoxLayout()
vbox.setContentsMargins(5, 5, 5, 5)
vbox.addWidget(topFiller)
vbox.addWidget(self.infoLabel)
vbox.addWidget(bottomFiller)
widget.setLayout(vbox)
self.setTimeValue()
def loadSettings(self):
""" Load Configuration from settings.json """
try:
settings = json.load(open("settings.json"))
except:
settings = {}
if type(settings) == list:
# If Settings is list - its assumed that several configs are stored in configfile
for setting in settings:
if checkHost(setting["url"]):
return setting # Return the Setting, if Host is reachable
return settings
def setTimeValue(self):
connected,progress = self.octopi.progress()
print(progress)
if connected:
self.infoLabel.setText(u'<h1>Time Left:</h1> %s' %(timestring(progress['printTimeLeft'])))
else:
self.infoLabel.setText(u'<h1>Connection Error</h1>')
def checkHost(hostname):
""" Check if host is reachable """
try:
r = requests.get("http://%s" %(hostname))
except requests.exceptions.ConnectionError:
return False
if r.status_code == 200:
return True
else:
return False
def timestring(sec_elapsed):
""" Convert duration in Secs to human readable format. If values are large shorter units are not shown """
h = int(sec_elapsed / (60 * 60))
m = int((sec_elapsed % (60 * 60)) / 60)
s = sec_elapsed % 60.
if h > 3:
return "{}h".format(h)
else:
if h > 0:
return "{}h {:>02}m".format(h,m)
else:
# Less than one hour
if m > 10:
return"{}m".format(m)
else:
if m > 0:
return "{}m {}s".format(h,s)
else:
return "{}s".format(s)
return "{}:{:>02}:{:>02}".format(h, m, s)
# End hms_string
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())