IdeasXWSCView.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import sys
  2. import time
  3. import sip
  4. from PyQt5 import QtCore, QtGui, QtWidgets
  5. from mainwindow import Ui_MainWindow
  6. from ideasxdevice import Ui_IdeasXDevice
  7. from IdeasXWSCBackend import IdeasXWSCNetworkThread
  8. from ParsingTools import ParsingTools
  9. from encoderconfigurationdialog import Ui_SwitchConfigDialog
  10. class IdeasXDeviceManager():
  11. def __init__(self, deviceClass, wsc):
  12. self.__devices = {}
  13. self.__deviceClass = deviceClass
  14. self.__deviceLayout = QtWidgets.QVBoxLayout()
  15. self.__deviceLayout.setAlignment(QtCore.Qt.AlignTop)
  16. self.__deviceLayout.setContentsMargins(9, 0, 9, 0)
  17. self.__deviceLayout.setSpacing(0)
  18. self.__wsc = wsc
  19. def refreshDevices(self, devices):
  20. for deviceMAC in devices.keys():
  21. if deviceMAC in self.__devices.keys():
  22. print("Updating Device")
  23. self.__devices[deviceMAC].updateDevice(devices[deviceMAC])
  24. else:
  25. print("Adding Device")
  26. self.__devices[deviceMAC] = self.__deviceClass(devices[deviceMAC], self.__wsc)
  27. self.__deviceLayout.addWidget(self.__devices[deviceMAC])
  28. for deviceMAC in list(self.__devices.keys()):
  29. if deviceMAC not in devices.keys():
  30. print("Removing Device")
  31. self.removeDevice(deviceMAC)
  32. def removeDevice(self, deviceMAC):
  33. self.__deviceLayout.removeWidget(self.__devices[deviceMAC])
  34. sip.delete(self.__devices[deviceMAC])
  35. self.__devices.pop(deviceMAC)
  36. def returnLayout(self):
  37. return self.__deviceLayout
  38. def filterDevices(self, searchPhase):
  39. print("This currently doesn't work")
  40. def printDevices(self):
  41. print(self.__devices)
  42. class IdeasXEncoder(QtWidgets.QWidget):
  43. sendCommand = QtCore.pyqtSignal(['QString'], name='sendCommand')
  44. def __init__(self, encoder, wsc):
  45. # This should become a static variable for the class
  46. self.__pathToIcon = {'network': './icon/network/',
  47. 'battery': './icon/battery/',
  48. 'battery_charging': './icon/battery/'
  49. }
  50. self.__icon = {'network': ['network-wireless-offline-symbolic.png',
  51. 'network-wireless-signal-weak-symbolic.png',
  52. 'network-wireless-signal-ok-symbolic.png',
  53. 'network-wireless-signal-good-symbolic.png',
  54. 'network-wireless-signal-excellent-symbolic.png'],
  55. 'battery': ['battery-empty-symbolic.png',
  56. 'battery-caution-symbolic.png',
  57. 'battery-low-symbolic.png',
  58. 'battery-good-symbolic.png',
  59. 'battery-full-symbolic.png'],
  60. 'battery_charging': ['battery-empty-charging-symbolic.png',
  61. 'battery-caution-charging-symbolic.png',
  62. 'battery-low-charging-symbolic.png',
  63. 'battery-good-charging-symbolic.png',
  64. 'battery-full-charged-symbolic.png']
  65. }
  66. self.__deviceType = 'encoder'
  67. self.__wsc = wsc
  68. # Setup UI components
  69. super(IdeasXEncoder, self).__init__()
  70. self.__ui = Ui_IdeasXDevice()
  71. self.__ui.setupUi(self)
  72. self._parserTools = ParsingTools()
  73. self.updateDevice(encoder)
  74. self.setupMenu()
  75. self.__ui.buttonActivate.clicked.connect(self.activateEncoder)
  76. self.__ui.buttonSwitchOne.clicked.connect(self.openSwitchDialog)
  77. self.__ui.buttonSwitchTwo.clicked.connect(self.openSwitchDialog)
  78. def openSwitchDialog(self):
  79. dialog = QtWidgets.QDialog()
  80. dialog.ui = Ui_SwitchConfigDialog()
  81. dialog.ui.setupUi(dialog)
  82. dialog.exec_()
  83. def setupMenu(self):
  84. shutdownAction = QtWidgets.QAction('Shutdown Encoder', self)
  85. shutdownAction.triggered.connect(lambda: self.__wsc.shutdownDevice(self.__strModuleID, None))
  86. deviceMenu = QtWidgets.QMenu()
  87. deviceMenu.addSection("General Actions")
  88. deviceMenu.addAction("Pair Encoder with Actuator")
  89. deviceMenu.addAction("Train Adaptive Switch")
  90. deviceMenu.addAction("Configure Module")
  91. deviceMenu.addSection("Encoder Commands")
  92. deviceMenu.addAction(shutdownAction)
  93. deviceMenu.addAction("Restart Encoder")
  94. deviceMenu.addAction("Update Firmware")
  95. self.__ui.buttonMenu.setPopupMode(2)
  96. self.__ui.buttonMenu.setMenu(deviceMenu)
  97. self.__ui.buttonMenu.setStyleSheet("* { padding-right: 3px } QToolButton::menu-indicator { image: none }")
  98. def activateEncoder(self):
  99. if self.__ui.buttonActivate.text() == "Activate":
  100. print("Activating Encoder: " + self.__ui.labelModuleID.text())
  101. self.__ui.buttonActivate.setText("Deactivate")
  102. else:
  103. print("Deactivating Encoder: " + self.__ui.labelModuleID.text())
  104. self.__ui.buttonActivate.setText("Activate")
  105. self.sendCommand.emit(self.__ui.labelModuleID.text())
  106. def updateDevice(self, encoder):
  107. self.__rssi = encoder['rssi']
  108. self.__soc = self._parserTools.calculateSOC(encoder['soc'])
  109. self.__vcell = self._parserTools.calculateVCell(encoder['vcell'])
  110. self.__strModuleID = self._parserTools.macToString(encoder['module_id'])
  111. self.__updateTime = encoder['time']
  112. self.setModuleID(self.__strModuleID)
  113. self.setSOCIcon(self.__soc)
  114. self.setRSSIIcon(self.__rssi)
  115. self.setStatusTime(self.__updateTime)
  116. def setModuleID(self, strModuleID):
  117. self.__ui.labelModuleID.setText(strModuleID)
  118. def setSOCIcon(self, soc):
  119. if soc >= 75:
  120. batteryIcon = 4
  121. elif soc >= 50 and soc < 75:
  122. batteryIcon = 3
  123. elif soc >= 25 and soc < 50:
  124. batteryIcon = 2
  125. elif soc >=10 and soc < 25:
  126. batteryIcon = 1
  127. elif soc < 10:
  128. batteryIcon = 0
  129. batteryIcon = self.__pathToIcon['battery']+self.__icon['battery'][batteryIcon]
  130. self.__ui.labelBattery.setPixmap(QtGui.QPixmap(batteryIcon))
  131. self.__ui.labelBattery.setToolTip(str(soc) + "%")
  132. def setStatusTime(self, updateTime):
  133. # set last update time or date
  134. lastUpdate = time.ctime(updateTime).split(" ")
  135. currentTime = time.ctime().split(" ")
  136. if currentTime[1] != lastUpdate[1] or currentTime[2] != lastUpdate[2] or currentTime[4] != lastUpdate[4]:
  137. lastUpdate = lastUpdate[1] + " " + lastUpdate[2] + " " + lastUpdate[4]
  138. else:
  139. lastUpdate = lastUpdate[3]
  140. self.__ui.labelStatus.setText("Last Update: " + lastUpdate)
  141. def setRSSIIcon(self, rssi):
  142. # set rssi icon
  143. if rssi >= -50:
  144. rssiIcon = 4
  145. elif rssi >= -60 and rssi < -50:
  146. rssiIcon = 3
  147. elif rssi >= -70 and rssi < -60:
  148. rssiIcon = 2
  149. elif rssi < -70:
  150. rssiIcon = 1
  151. rssiIcon = self.__pathToIcon['network'] + self.__icon['network'][rssiIcon]
  152. self.__ui.labelSignal.setPixmap(QtGui.QPixmap(rssiIcon))
  153. self.__ui.labelSignal.setToolTip(str(rssi) + " dBm")
  154. class IdeasXMainWindow(QtWidgets.QMainWindow):
  155. def __init__(self):
  156. super(IdeasXMainWindow, self).__init__()
  157. self.__ui = Ui_MainWindow()
  158. self.__ui.setupUi(self)
  159. p = self.__ui.contentEncoder.palette()
  160. p.setColor(self.backgroundRole(), QtCore.Qt.white)
  161. self.__ui.contentEncoder.setPalette(p)
  162. def setEncoderLayout(self, layout):
  163. self.__ui.contentEncoder.setLayout(layout)
  164. if __name__ == "__main__":
  165. app = QtWidgets.QApplication(sys.argv)
  166. app.setWindowIcon(QtGui.QIcon('icon/logo/ideasx.png'))
  167. mainWindow = IdeasXMainWindow()
  168. wsc = IdeasXWSCNetworkThread()
  169. encoderManager = IdeasXDeviceManager(IdeasXEncoder, wsc)
  170. mainWindow.setEncoderLayout(encoderManager.returnLayout())
  171. wsc.encoderUpdate.connect(encoderManager.refreshDevices)
  172. wsc.guiStartWorkstationClient('ideasx.duckdns.org')
  173. #timer = QtCore.QTimer()
  174. #timer.timeout.connect(mainWindow.hideEncoder)
  175. #timer.start(1000)
  176. #time.sleep(0.5)
  177. mainWindow.show()
  178. sys.exit(app.exec_())