summaryrefslogtreecommitdiffstats
path: root/host/moistcontrol-gui
blob: b59d0738e8f5bb9416e5ba6d01e0ad16200dc8ac (plain)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!/usr/bin/env python3
#
# Moisture control - Graphical user interface
#
# Copyright (c) 2013 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

import sys

if sys.version_info[0] < 3:
	print("The Python interpreter is too old.")
	print("PLEASE INSTALL Python 3.x")
	raw_input("Press enter to exit.")
	sys.exit(1)

import math
from pymoistcontrol import *


# Serial communication parameters
SERIAL_BAUDRATE		= 19200
SERIAL_PAYLOAD_LEN	= 12


class MainWidget(QWidget):
	"""The central widget inside of the main window."""

	serialConnected = Signal()
	serialDisconnected = Signal()

	fetchCycle = [
		"log",
		"rtc",
		"pot_state",
	]

	def __init__(self, parent):
		"""Class constructor."""
		QWidget.__init__(self, parent)
		self.setLayout(QGridLayout(self))

		self.globConfWidget = GlobalConfigWidget(self)
		self.layout().addWidget(self.globConfWidget, 0, 0)

		self.tabWidget = QTabWidget(self)
		self.layout().addWidget(self.tabWidget, 0, 1)

		self.potWidgets = []
		for i in range(MAX_NR_FLOWERPOTS):
			potWidget = PotWidget(i, self)
			self.potWidgets.append(potWidget)
			self.tabWidget.addTab(potWidget,
					      "Pot %d" % (i + 1))
		self.setUiEnabled(False)

		self.logWidget = LogWidget(self)
		self.layout().addWidget(self.logWidget, 1, 0, 1, 2)

		self.connected = False
		self.pollTimer = QTimer(self)
		self.pollTimer.setSingleShot(True)

		self.globConfWidget.configChanged.connect(self.__handleGlobConfigChange)
		self.globConfWidget.rtcEdited.connect(self.__handleRtcEdit)
		for pot in self.potWidgets:
			pot.configChanged.connect(self.__handlePotConfigChange)
			pot.manModeChanged.connect(self.__handleManModeChange)
		self.pollTimer.timeout.connect(self.__pollTimerEvent)

	def __handleCommError(self, exception):
		QMessageBox.critical(self,
				     "Serial communication failed",
				     "Serial communication failed:\n"
				     "%s" % str(exception))
		self.disconnectDev()

	def __makeMsg_GlobalConfig(self):
		msg = MsgContrConf(flags = 0,
				   sensor_lowest_value = self.globConfWidget.lowestRawSensorVal(),
				   sensor_highest_value = self.globConfWidget.highestRawSensorVal())
		if self.globConfWidget.globalEnableActive():
			msg.flags |= msg.CONTR_FLG_ENABLE
		return msg

	def __makeMsg_RTC(self):
		dateTime = self.globConfWidget.getRtcDateTime()
		msg = MsgRtc(second = dateTime.time().second(),
			     minute = dateTime.time().minute(),
			     hour = dateTime.time().hour(),
			     day = dateTime.date().day() - 1,
			     month = dateTime.date().month() - 1,
			     year = clamp(dateTime.date().year(), 2000, 2063) - 2000,
			     day_of_week = dateTime.date().dayOfWeek() - 1)
		return msg

	def __makeMsg_PotConfig(self, potNumber):
		pot = self.potWidgets[potNumber]
		self.globConfWidget.handlePotEnableChange(potNumber,
							  pot.isEnabled())
		msg = MsgContrPotConf(pot_number = potNumber,
				      flags = 0,
				      min_threshold = pot.getMinThreshold(),
				      max_threshold = pot.getMaxThreshold(),
				      start_time = pot.getStartTime(),
				      end_time = pot.getEndTime(),
				      dow_on_mask = pot.getDowEnableMask())
		if pot.isEnabled():
			msg.flags |= msg.POT_FLG_ENABLED
		if pot.loggingEnabled():
			msg.flags |= msg.POT_FLG_LOG
		if pot.verboseLoggingEnabled():
			msg.flags |= msg.POT_FLG_LOGVERBOSE
		return msg

	def __makeMsg_ManMode(self):
		msg = MsgManMode(force_stop_watering_mask = 0,
				 valve_manual_mask = 0,
				 valve_manual_state = 0)
		for i, pot in enumerate(self.potWidgets):
			if pot.forceStopWateringActive():
				msg.force_stop_watering_mask |= 1 << i
			if pot.forceOpenValveActive():
				msg.valve_manual_mask |= 1 << i
				msg.valve_manual_state |= 1 << i
		return msg

	def __handleGlobConfigChange(self):
		try:
			self.serial.send(self.__makeMsg_GlobalConfig())
		except SerialError as e:
			self.__handleCommError(e)
			return

	def __handleRtcEdit(self):
		try:
			self.serial.send(self.__makeMsg_RTC())
		except SerialError as e:
			self.__handleCommError(e)
			return

	def __handlePotConfigChange(self, potNumber):
		try:
			self.serial.send(self.__makeMsg_PotConfig(potNumber))
		except SerialError as e:
			self.__handleCommError(e)
			return

	def __handleManModeChange(self):
		try:
			self.serial.send(self.__makeMsg_ManMode())
		except SerialError as e:
			self.__handleCommError(e)
			return

	def setUiEnabled(self, enabled = True):
		self.globConfWidget.setEnabled(enabled)
		for i in range(self.tabWidget.count()):
			self.tabWidget.widget(i).setEnabled(enabled)

	def __fetchCycleNext(self):
		action = self.fetchCycle[self.fetchCycleNumber]
		try:
			if action == "log":
				msg = MsgLogFetch()
			elif action == "rtc":
				msg = MsgRtcFetch()
			elif action == "pot_state":
				msg = MsgContrPotStateFetch(self.potCycleNumber)
			else:
				assert(0)
			self.serial.send(msg)
		except SerialError as e:
			self.__handleCommError(e)
			return
		self.pollTimer.start(math.ceil(msg.calcFrameDuration() * 1000) + 10)

	def __checkRxMsg(self, msg, expectedType, ignoreErrorCode=False):
		ok = True
		if not ignoreErrorCode:
			if msg.getErrorCode() != Message.COMM_ERR_OK:
				QMessageBox.critical(self,
					"Received message: Error",
					"Received an error: %d" % \
					msg.getErrorCode())
				ok = False
		if ok and\
		   msg.getType() is not None and\
		   msg.getType() != expectedType:
			QMessageBox.critical(self,
				"Received message: Unexpected type",
				"Received a message with an unexpected "
				"type. (got %d, expected %d)" % \
				(msg.getType(), expectedType))
			ok = False
		if not ok:
			self.disconnectDev()
		return ok

	def __convertRxMsg(self, msg, fatalOnNoMsg=False):
		msg = Message.fromRawMessage(msg)
		if not msg:
			if fatalOnNoMsg:
				QMessageBox.critical(self, "Communication failed",
					"Serial communication timeout")
				self.disconnectDev()
			return None
		error = msg.getErrorCode()
		if error not in (Message.COMM_ERR_OK, Message.COMM_ERR_FAIL):
			QMessageBox.critical(self, "Communication failed",
				"Serial communication error: %d" % error)
			self.disconnectDev()
			return None
		return msg

	def __pollTimerEvent(self):
		try:
			msg = self.__convertRxMsg(self.serial.poll())
			if not msg:
				self.__pollRetries += 1
				if self.__pollRetries >= 200:
					QMessageBox.critical(self,
						"Communication failed",
						"Communication failed. "
						"Retry timeout.")
					self.disconnectDev()
					return
				self.pollTimer.start(5) # Retry
				return
			self.__pollRetries = 0
		except SerialError as e:
			self.__handleCommError(e)
			return
		error = msg.getErrorCode()

		advanceFetchCycle = True
		action = self.fetchCycle[self.fetchCycleNumber]
		if action == "log":
			if self.__checkRxMsg(msg, Message.MSG_LOG,
					     ignoreErrorCode = True):
				if error == Message.COMM_ERR_OK:
					self.logWidget.handleLogMessage(msg)
					self.logCount += 1
					if self.logCount < 8:
						advanceFetchCycle = False
			if advanceFetchCycle:
				self.logCount = 0
		elif action == "rtc":
			if self.__checkRxMsg(msg, Message.MSG_RTC):
				self.globConfWidget.handleRtcMessage(msg)
		elif action == "pot_state":
			if self.__checkRxMsg(msg, Message.MSG_CONTR_POT_STATE):
				if msg.pot_number == self.potCycleNumber:
					self.globConfWidget.handlePotStateMessage(msg)
					self.potWidgets[self.potCycleNumber].handlePotStateMessage(msg)
				else:
					QMessageBox.critical(self,
						"Pot state message mismatch",
						"Received pot state message for the"
						"wrong pot (was %d, expected %d)." % \
						(msg.pot_number, self.potCycleNumber))
			self.potCycleNumber += 1
			if self.potCycleNumber < MAX_NR_FLOWERPOTS:
				advanceFetchCycle = False
			else:
				self.potCycleNumber = 0
		else:
			assert(0)

		if advanceFetchCycle:
			self.fetchCycleNumber += 1
			if self.fetchCycleNumber >= len(self.fetchCycle):
				self.fetchCycleNumber = 0
			self.__fetchCycleNext()
		else:
			self.__fetchCycleNext()

	def __initializeDev(self):
		self.fetchCycleNumber = 0
		self.potCycleNumber = 0
		self.logCount = 0
		self.__pollRetries = 0
		try:
			# Get the global configuration from the device
			msg = self.__convertRxMsg(self.serial.sendSync(MsgContrConfFetch()),
						  fatalOnNoMsg = True)
			if not self.__checkRxMsg(msg, Message.MSG_CONTR_CONF):
				return
			self.globConfWidget.handleGlobalConfMessage(msg)
			# Get the pot configurations from the device
			for i in range(MAX_NR_FLOWERPOTS):
				msg = self.__convertRxMsg(self.serial.sendSync(MsgContrPotConfFetch(i)),
							  fatalOnNoMsg = True)
				if not self.__checkRxMsg(msg, Message.MSG_CONTR_POT_CONF):
					return
				self.potWidgets[i].handlePotConfMessage(msg)
				self.globConfWidget.handlePotConfMessage(msg)
			# Reset manual mode
			msg = MsgManMode(force_stop_watering_mask = 0,
					 valve_manual_mask = 0,
					 valve_manual_state = 0)
			self.serial.send(msg)
			# Start cyclic data fetching
			self.__fetchCycleNext()
		except SerialError as e:
			self.__handleCommError(e)
			return False
		return True

	def isConnected(self):
		return self.connected

	def connectDev(self, port=None):
		if self.connected:
			return
		if not port:
			dlg = SerialOpenDialog(self)
			if dlg.exec_() != QDialog.Accepted:
				return
			port = dlg.getSelectedPort()
		try:
			self.serial = SerialComm(port, baudrate = SERIAL_BAUDRATE,
						 payloadLen = SERIAL_PAYLOAD_LEN,
						 debug = False)
		except SerialError as e:
			QMessageBox.critical(self, "Cannot connect serial port",
				"Cannot connect serial port:\n" + str(e))
			return
		self.logWidget.clear()
		self.setUiEnabled(True)
		if not self.__initializeDev():
			return
		self.connected = True
		self.serialConnected.emit()

	def disconnectDev(self):
		self.setUiEnabled(False)
		self.pollTimer.stop()
		if self.serial:
			self.serial.close()
			self.serial = None
		if self.connected:
			self.connected = False
			self.serialDisconnected.emit()

	def getSettingsText(self):
		settings = [
			"[MOISTCONTROL_SETTINGS]\n" \
			"file_version=0\n" \
			"date=%s\n" % \
			(QDateTime.currentDateTime().toUTC().toString("yyyy.MM.dd_hh:mm:ss.zzz_UTC"))
		]
		# Write global config
		msg = self.__makeMsg_GlobalConfig()
		settings.append(msg.toText())
		# Write pot configs
		for i in range(MAX_NR_FLOWERPOTS):
			msg = self.__makeMsg_PotConfig(i)
			settings.append(msg.toText())
		return "\n".join(settings)

	def setSettingsText(self, settings):
		# Drain pending RX-messages
		self.pollTimer.stop()
		time.sleep(0.1)
		while self.serial.poll():
			pass
		# Parse and upload the new config
		try:
			p = configparser.ConfigParser()
			p.read_string(settings)
			ver = p.getint("MOISTCONTROL_SETTINGS", "file_version")
			if ver != 0:
				raise Error("Unsupported file version. "
					    "Expected v0, but got v%d." % ver)
			# Read global config
			msg = MsgContrConf()
			msg.fromText(settings)
			self.serial.send(msg) # send to device
			# Read pot configs
			for i in range(MAX_NR_FLOWERPOTS):
				msg = MsgContrPotConf(i)
				msg.fromText(settings)
				self.serial.send(msg) # send to device
		except configparser.Error as e:
			raise Error(str(e))
		except SerialError as e:
			raise Error("Failed to send config to device:\n" % str(e))
		finally:
			# Restart the communication
			self.__initializeDev()

	def doLoadSettings(self, filename):
		try:
			fd = open(filename, "rb")
			settings = fd.read().decode("UTF-8")
			fd.close()
			self.setSettingsText(settings)
		except (IOError, UnicodeError, Error) as e:
			QMessageBox.critical(self,
				"Failed to read file",
				"Failed to read the settings file:\n"
				"%s" % str(e))

	def loadSettings(self):
		fn, filt = QFileDialog.getOpenFileName(self,
				"Load settings from file",
				"",
				"Settings file (*.moi);;"
				"All files (*)")
		if not fn:
			return
		self.doLoadSettings(fn)

	def doSaveSettingsAs(self, filename):
		try:
			fd = open(filename, "wb")
			fd.write(self.getSettingsText().encode("UTF-8"))
			fd.close()
		except (IOError, UnicodeError, Error) as e:
			QMessageBox.critical(self,
				"Failed to write file",
				"Failed to write the settings file:\n"
				"%s" % str(e))

	def saveSettingsAs(self):
		fn, filt = QFileDialog.getSaveFileName(self,
				"Save settings to file",
				"",
				"Settings file (*.moi);;"
				"All files (*)")
		if not fn:
			return
		if "(*.moi)" in filt:
			if not fn.endswith(".moi"):
				fn += ".moi"
		self.doSaveSettingsAs(fn)

class MainWindow(QMainWindow):
	"""The main program window."""

	def __init__(self):
		"""Class constructor."""
		QMainWindow.__init__(self)
		self.setWindowTitle("Flowerpot moisture control")

		mainWidget = MainWidget(self)
		self.setCentralWidget(mainWidget)

		self.setMenuBar(QMenuBar(self))

		menu = QMenu("&File", self)
		self.loadButton = menu.addAction("&Load settings...", self.loadSettings)
		self.saveButton = menu.addAction("&Save settings as...", self.saveSettingsAs)
		menu.addSeparator()
		menu.addAction("&Exit", self.close)
		self.menuBar().addMenu(menu)

		menu = QMenu("&Device", self)
		self.connMenuButton = menu.addAction("&Connect", self.connectDev)
		self.disconnMenuButton = menu.addAction("&Disconnect", self.disconnectDev)
		self.menuBar().addMenu(menu)

		toolBar = QToolBar(self)
		self.connToolButton = toolBar.addAction("Connect", self.connectDev)
		self.disconnToolButton = toolBar.addAction("Disconnect", self.disconnectDev)
		self.addToolBar(toolBar)

		self.updateConnButtons()

		mainWidget.serialConnected.connect(self.updateConnButtons)
		mainWidget.serialDisconnected.connect(self.updateConnButtons)

	def updateConnButtons(self):
		connected = self.centralWidget().isConnected()
		self.connMenuButton.setEnabled(not connected)
		self.connToolButton.setEnabled(not connected)
		self.disconnMenuButton.setEnabled(connected)
		self.disconnToolButton.setEnabled(connected)
		self.loadButton.setEnabled(connected)
		self.saveButton.setEnabled(connected)

	def loadSettings(self):
		self.centralWidget().loadSettings()

	def saveSettingsAs(self):
		self.centralWidget().saveSettingsAs()

	def connectDev(self, port=None):
		self.centralWidget().connectDev(port)

	def disconnectDev(self):
		self.centralWidget().disconnectDev()

# Program entry point
def main():
	# Create the main QT application object
	qApp = QApplication(sys.argv)
	# Create and show the main window
	mainWnd = MainWindow()
	mainWnd.show()
	if len(sys.argv) >= 2:
		mainWnd.connectDev(sys.argv[1])
	# Enter the QT event loop
	return qApp.exec_()

if __name__ == "__main__":
	sys.exit(main())
bues.ch cgit interface