-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.py
274 lines (237 loc) · 10.3 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# This Python file uses the following encoding: utf-8
import sys
import os
import asyncio
from struct import *
from sys import platform
from bleak import BleakScanner
from bleak import BleakClient
from qasync import QEventLoop, asyncClose
from PySide2.QtWidgets import QApplication, QColorDialog
from PySide2.QtGui import QColor
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtCore import QObject, Slot, Signal, QTimer, QSettings
TARGET_TEMP = "fc540003-236c-4c94-8fa9-944a3e5353fa"
LED_COLOR_UUID = "fc540014-236c-4c94-8fa9-944a3e5353fa"
CURRENT_TEMP = "fc540002-236c-4c94-8fa9-944a3e5353fa"
CURRENT_BAT = "fc540007-236c-4c94-8fa9-944a3e5353fa"
class MainWindow(QObject):
def __init__(self):
QObject.__init__(self)
# Settings
self.globalSettings = QSettings("Pledi", "EmberControl")
# Check for settings
if self.globalSettings.value("coffeeTemp") is None:
self.globalSettings.setValue("coffeeTemp", 5500)
if self.globalSettings.value("teaTemp") is None:
self.globalSettings.setValue("teaTemp", 5900)
# Init
self.keepConnectionAlive = True
self.searchForDevice = True
self.connectedClient = None
# try to connect
asyncio.ensure_future(self.connectToMug(self))
# function to get the current temp from the async loop.
def fetchCurrentTemperature(self):
if self.connectedClient is not None:
asyncio.ensure_future(self.getCurrentTemp(self))
# function to get the current temp from the async loop.
def fetchCurrentBattery(self):
if self.connectedClient is not None:
asyncio.ensure_future(self.getCurrentBattery(self))
@staticmethod
async def getCurrentTemp(self):
try:
if await self.connectedClient.is_connected():
currentTemp = await self.connectedClient.read_gatt_char(CURRENT_TEMP)
CurrentDegree = float(int.from_bytes(currentTemp, byteorder='little', signed=False)) * 0.01
CurrentDegree = round(CurrentDegree, 1)
print(CurrentDegree)
# Send UI Signal
self.getDegree.emit(float(CurrentDegree))
else:
self.connectionChanged.emit(False)
print("not connected")
except Exception as exc:
print('Error: {}'.format(exc))
@staticmethod
async def setLEDColor(self, color):
if await self.connectedClient.is_connected():
await self.connectedClient.write_gatt_char(LED_COLOR_UUID, color, False)
print("Changed Color to {0}".format(color))
else:
self.connectionChanged.emit(False)
print("not connected")
@staticmethod
async def fetchLEDColor(self):
if await self.connectedClient.is_connected():
c = await self.connectedClient.read_gatt_char(LED_COLOR_UUID)
self.ledColor = QColor(c[0],c[1],c[2],c[3])
# Send UI Signal
self.getColor.emit(self.ledColor)
print("Changed Color to {0}".format(self.ledColor))
else:
self.connectionChanged.emit(False)
print("not connected")
@staticmethod
async def getTargetTemp(self):
if await self.connectedClient.is_connected():
currentTemp = await self.connectedClient.read_gatt_char(TARGET_TEMP)
TargetDegree = float(int.from_bytes(currentTemp, byteorder='little', signed=False)) * 0.01
print("Target temp set to {0}".format(TargetDegree))
else:
self.connectionChanged.emit(False)
print("not connected")
@staticmethod
async def getCurrentBattery(self):
if await self.connectedClient.is_connected():
currentBat = await self.connectedClient.read_gatt_char(CURRENT_BAT)
print(float(currentBat[0]))
# Send UI Signal
self.getBattery.emit(float(currentBat[0]))
else:
self.connectionChanged.emit(False)
print("not connected")
@staticmethod
async def setToTemp(self, temp):
if await self.connectedClient.is_connected():
print("try setting the target temperature")
newtarget = temp.to_bytes(2, 'little')
await self.connectedClient.write_gatt_char(TARGET_TEMP, newtarget,False)
# Send UI Signal
self.getDegree.emit(float(temp * 0.01))
else:
self.connectionChanged.emit(False)
print("not connected")
@staticmethod
async def cleanup(self):
tasks = [t for t in asyncio.all_tasks() if t is not
asyncio.current_task()]
[task.cancel() for task in tasks]
await asyncio.gather(*tasks, return_exceptions=True)
loop.stop()
print("stopped")
@staticmethod
async def connectToMug(self):
try:
print("Searching..", end='')
self.connectionChanged.emit(False)
# Search for the mug as long til we find it.
while self.searchForDevice:
print('.', end='')
scanner = BleakScanner()
# scanner.register_detection_callback(detection_callback)
await scanner.start()
await asyncio.sleep(5.0)
await scanner.stop()
devices = await scanner.get_discovered_devices()
for device in devices:
if device.name == "Ember Ceramic Mug":
# We found the ember mug!
print(device.address)
print(device.name)
print(device.details)
# try to connect to the mug
async with BleakClient(device) as client:
self.connectedClient = client
x = await client.is_connected()
print("Connected: {0}".format(x))
if platform != "darwin":
# Avoid this on mac, since CoreBluetooth doesnt support pairing.
y = await client.pair()
print("Paired: {0}".format(y))
# Set connection parameters and use signal to send it to the UI.
self.keepConnectionAlive = True
self.connectionChanged.emit(True)
await self.fetchLEDColor(self)
# Auto update Temp and Battery
self.timer = QTimer()
self.timer.timeout.connect(lambda: self.fetchCurrentTemperature())
self.timer.timeout.connect(lambda: self.fetchCurrentBattery())
self.timer.start(3000)
# we are connected, get the settings we need.
while self.keepConnectionAlive:
# We stay in here to keep the client alive
# once keepConnectionAlive is set to false
# the client will also disconnect automatically
print(".")
await asyncio.sleep(2)
self.keepConnectionAlive = await self.connectedClient.is_connected()
except Exception as exc:
self.connectionChanged.emit(False)
print('Error: {}'.format(exc))
@Slot()
def closeEvent(self):
print("close")
self.searchForDevice = False
self.keepConnectionAlive = False
if hasattr(self, 'timer'):
self.timer.stop()
asyncio.ensure_future(self.cleanup(self))
# UI Signal getDegree
getDegree = Signal(float)
# UI Signal getBattery
getBattery = Signal(float)
# UI Signal getColor
getColor = Signal(QColor)
# UI Signal wroteColor
wroteColor = Signal(QColor)
# UI Signal connectionChanged
connectionChanged = Signal(bool)
# UI SLOTS
@Slot(int)
def setCoffeeTemp(self, temp):
print((temp))
self.globalSettings.setValue("coffeeTemp", int(temp))
@Slot(int)
def setTeaTemp(self, temp):
print((temp))
self.globalSettings.setValue("teaTemp", int(temp))
@Slot(result=float)
def getTeaTemperature(self):
return self.globalSettings.value("teaTemp") * 0.01
@Slot(result=float)
def getCoffeeTemperature(self):
return self.globalSettings.value("coffeeTemp") * 0.01
@Slot(result=QColor)
def getLEDColor(self):
asyncio.ensure_future(self.fetchLEDColor(self))
print("color atm: {0}".format(self.ledColor))
return self.ledColor
@Slot()
def setCoffee(self):
asyncio.ensure_future(self.setToTemp(self, self.globalSettings.value("coffeeTemp")))
asyncio.ensure_future(self.getTargetTemp(self))
@Slot()
def setTea(self):
asyncio.ensure_future(self.setToTemp(self, self.globalSettings.value("teaTemp")))
asyncio.ensure_future(self.getTargetTemp(self))
@Slot()
def openColorPicker(self):
col = QColorDialog.getColor(options=QColorDialog.ShowAlphaChannel)
if col.isValid():
color = bytearray([col.red(), col.green(), col.blue(), col.alpha()])
asyncio.ensure_future(self.setLEDColor(self, color))
self.wroteColor.emit(col)
if __name__ == "__main__":
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
# Event Loop
loop = QEventLoop(app)
asyncio.set_event_loop(loop)
# Get Context
main = MainWindow()
engine.rootContext().setContextProperty("backend", main)
# Load QML File
engine.load(os.path.join(os.path.dirname(__file__), "main.qml"))
try:
loop.run_forever()
except (KeyboardInterrupt, SystemExit) as e:
logging.info(f"{e.__class__.__name__} received")
except Exception as e:
exception_manager.handle_exception(e)
finally:
sys.exit(0)
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())