-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
127 lines (103 loc) · 4.02 KB
/
main.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
126
127
import os
import queue
import sys
import threading
from pathlib import Path
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import QMainWindow
from fabric import Connection
from config_reader import ConfigReader
from credentials import Credentials
from docker_container import DockerContainer
from modbus import Modbus
from settings_dialog import SettingsDialog
from slave_mapping import SlaveMapping
from ui.main import Ui_MainWindow
from utils import get_config_local, save_config_local
CONFIG_FILE = Path('config.yaml')
class CustomSignalWindow(QMainWindow):
signal = QtCore.pyqtSignal(dict)
def __init__(self):
super().__init__()
self.signal.connect(self.signaling)
@staticmethod
def signaling(work: dict):
if 'arg' in work:
work['func'](work['arg'])
else:
work['func']()
class MainWindow(Ui_MainWindow):
def __init__(self):
self.app = QtWidgets.QApplication(sys.argv)
self.main_window = CustomSignalWindow()
self.setupUi(self.main_window)
self.actionconfig.triggered.connect(self.show_settings_dialog)
self.queue = queue.Queue()
self.config: dict = get_config_local(CONFIG_FILE)
if 'error' in self.config:
self.config = {
'host': '127.0.0.1',
'user': 'pi',
'password': '123'
}
save_config_local(CONFIG_FILE, self.config)
self.reader_config: dict = {
'autosize_window': self.autosize_window,
'c': self.get_connection(),
'centralwidget': self.centralwidget,
'gridLayout': self.gridLayout,
'tableWidget': self.tableWidget,
'signal': self.main_window.signal,
'statusBar': self.main_window.statusBar(),
'queue': self.queue,
'widget_counter': 0
}
self.reader_list: dict[str, ConfigReader] = {
'credentials': Credentials(self.reader_config),
'docker': DockerContainer(self.reader_config),
'slave_mapping': SlaveMapping(self.reader_config),
'modbus': Modbus(self.reader_config)
}
self.init_queue()
threading.Thread(target=self.worker, daemon=True).start()
def show(self):
self.main_window.show()
self.app.exec_()
def get_connection(self):
return Connection(host=self.config['host'], user=self.config['user'],
connect_kwargs={'password': self.config['password']})
def init_queue(self):
for key in self.reader_list:
reader = self.reader_list[key]
reader.button.setEnabled(False)
reader.init_queue()
self.queue.put({'func': self.main_window.statusBar().clearMessage, 'type': 'signal'})
def show_settings_dialog(self):
if SettingsDialog(self.config).result == 1:
save_config_local(CONFIG_FILE, self.config)
self.reader_config['c'] = self.get_connection()
for key in self.reader_list:
self.reader_list[key].set_connection()
self.init_queue()
def worker(self):
while True:
work = self.queue.get()
if work['type'] == 'ssh':
work['func']()
elif work['type'] == 'signal':
self.main_window.signal.emit(work)
self.queue.task_done()
def autosize_window(self):
def resize_width():
size = self.main_window.size()
size_hint = self.main_window.minimumSizeHint()
table_size = self.tableWidget.size()
table_size_hint = self.tableWidget.viewportSizeHint()
size.setWidth(size.width() + (table_size_hint.width() - table_size.width()) + 25)
self.main_window.resize(size)
self.main_window.setMinimumSize(size_hint)
QtCore.QTimer.singleShot(0, resize_width)
if __name__ == '__main__':
script_dir = os.path.dirname(os.path.realpath(__file__))
main_window = MainWindow()
main_window.show()