-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCloudLogPyCAT.py
236 lines (208 loc) · 8.01 KB
/
CloudLogPyCAT.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/python3
"""
CloudLogPyCAT
Small app that monitors you radio via CAT and updates Cloudlog to assist logging.
Who's responsible: Michael Bridak K6GTE
Where to yell at : michael.bridak@gmail.com
"""
# pylint: disable=no-name-in-module
# pylint: disable=c-extension-no-member
# pylint: disable=invalid-name
import logging
import datetime
import os
import sys
from json import dumps, loads
import requests
from PyQt5 import QtCore, QtWidgets
from PyQt5 import uic
from PyQt5.QtCore import QDir
from PyQt5.QtGui import QFontDatabase
from cat_interface import CAT
logging.basicConfig(level=logging.WARNING)
def relpath(filename):
"""Checks to see if program has been packaged with pyinstaller.
If so base dir is in a temp folder.
"""
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
base_path = getattr(sys, "_MEIPASS")
else:
base_path = os.path.abspath(".")
return os.path.join(base_path, filename)
def load_fonts_from_dir(directory):
"""loads available font families"""
_families = set()
for filename in QDir(directory).entryInfoList(["*.ttf", "*.woff", "*.woff2"]):
_id = QFontDatabase.addApplicationFont(filename.absoluteFilePath())
_families |= set(QFontDatabase.applicationFontFamilies(_id))
return _families
class MainWindow(QtWidgets.QMainWindow):
"""Yep, it's the main window class."""
oldfreq = "0"
oldmode = "none"
oldpower = 0
newfreq = "0"
newmode = "none"
s = False
settings_dict = {
"key": "yourAPIkey",
"cloudurl": "http://www.youraddress.com/index.php/api/radio",
"radio_name": "IC-7300",
"CAT_type": "rigctld",
"host": "localhost",
"port": 4532,
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
uic.loadUi(self.relpath("main.ui"), self)
self.settingsbutton.clicked.connect(self.settingspressed)
self.cat_interface = None
def relpath(self, filename):
"""Checks to see if program has been packaged with pyinstaller.
If so base dir is in a temp folder.
"""
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
base_path = getattr(sys, "_MEIPASS")
else:
base_path = os.path.abspath(".")
return os.path.join(base_path, filename)
def loadsaved(self):
"""
load saved defaults if they exist.
otherwise write some sane defaults as a json text file in the users home directory.
"""
home = os.path.expanduser("~")
if os.path.exists(home + "/.cloudlogpycat.json"):
with open(
home + "/.cloudlogpycat.json", "rt", encoding="utf-8"
) as file_handle:
self.settings_dict = loads(file_handle.read())
else:
with open(
home + "/.cloudlogpycat.json", "wt", encoding="utf-8"
) as file_handle:
file_handle.write(dumps(self.settings_dict))
self.cat_interface = CAT(
self.settings_dict["CAT_type"],
self.settings_dict["host"],
self.settings_dict["port"],
)
def savestuff(self):
"""
save state as a json file in the home directory
"""
home = os.path.expanduser("~")
with open(home + "/.cloudlogpycat.json", "wt", encoding="utf-8") as file_handle:
file_handle.write(dumps(self.settings_dict))
def settingspressed(self):
"""Creates/Calls the settings window."""
settingsdialog = Settings(self)
settingsdialog.exec()
self.loadsaved()
def rigconnect(self):
"""get the radio state."""
try:
self.newfreq = self.cat_interface.get_vfo()
self.newmode = self.cat_interface.get_mode()
self.newpower = self.cat_interface.get_power()
except Exception:
pass
def mainloop(self):
"""Where the magik happens"""
self.rigconnect()
if (
self.newfreq != self.oldfreq
or self.newmode != self.oldmode
or self.newpower != self.oldpower
):
self.freq_label.setText(self.newfreq)
self.mode_label.setText(self.newmode)
time_stamp = datetime.datetime.utcnow().strftime("%Y/%m/%d %H:%M")
payload = {
"key": self.settings_dict["key"],
"radio": self.settings_dict["radio_name"],
"frequency": self.newfreq,
"mode": self.newmode,
"timestamp": time_stamp,
"power": self.newpower,
}
try:
response = requests.post(
self.settings_dict["cloudurl"], json=payload, timeout=5
)
self.response_label.setText(str(response.status_code))
self.oldfreq = self.newfreq
self.oldmode = self.newmode
self.oldpower = self.newpower
except (
requests.exceptions.ConnectionError,
requests.exceptions.InvalidSchema,
):
...
class Settings(QtWidgets.QDialog):
"""Class to handle... Yep, the settings."""
settings_dict = {}
def __init__(self, parent=None):
super().__init__(parent)
uic.loadUi(self.relpath("settings.ui"), self)
self.buttonBox.accepted.connect(self.save_changes)
self.loadsettings()
def relpath(self, filename):
"""Checks to see if program has been packaged with pyinstaller.
If so base dir is in a temp folder.
"""
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
base_path = getattr(sys, "_MEIPASS")
else:
base_path = os.path.abspath(".")
return os.path.join(base_path, filename)
def loadsettings(self):
"""Loads settings from JSON file."""
home = os.path.expanduser("~")
if os.path.exists(home + "/.cloudlogpycat.json"):
with open(
home + "/.cloudlogpycat.json", "rt", encoding="utf-8"
) as file_handle:
self.settings_dict = loads(file_handle.read())
self.radioname_field.setText(self.settings_dict["radio_name"])
self.cloudlogapi_field.setText(self.settings_dict["key"])
self.cloudlogurl_field.setText(self.settings_dict["cloudurl"])
if (
"CAT_type" in self.settings_dict
and self.settings_dict["CAT_type"] == "rigctld"
):
self.rigctld_radioButton.setChecked(True)
if (
"CAT_type" in self.settings_dict
and self.settings_dict["CAT_type"] == "flrig"
):
self.flrig_radioButton.setChecked(True)
self.rigcontrolip_field.setText(self.settings_dict["host"])
self.rigcontrolport_field.setText(str(self.settings_dict["port"]))
def save_changes(self):
"""Saves changes to a JSON file."""
self.settings_dict["radio_name"] = self.radioname_field.text()
self.settings_dict["key"] = self.cloudlogapi_field.text()
self.settings_dict["cloudurl"] = self.cloudlogurl_field.text()
self.settings_dict["host"] = self.rigcontrolip_field.text()
self.settings_dict["port"] = int(self.rigcontrolport_field.text())
if self.rigctld_radioButton.isChecked():
self.settings_dict["CAT_type"] = "rigctld"
if self.flrig_radioButton.isChecked():
self.settings_dict["CAT_type"] = "flrig"
home = os.path.expanduser("~")
with open(home + "/.cloudlogpycat.json", "wt", encoding="utf-8") as file_handle:
file_handle.write(dumps(self.settings_dict))
app = QtWidgets.QApplication(sys.argv)
app.setStyle("Fusion")
font_dir = relpath("font")
families = load_fonts_from_dir(os.fspath(font_dir))
logging.info(families)
window = MainWindow()
window.show()
window.loadsaved()
# window.rigconnect()
timer = QtCore.QTimer()
timer.timeout.connect(window.mainloop)
timer.start(1000)
app.exec()