IdeasXWSCView.py 12 KB

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