webrelay-modbus/FaultMonitor.py
2020-02-10 20:18:39 -08:00

160 lines
4.3 KiB
Python

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# generated by wxGlade 0.9.5 on Fri Feb 7 20:10:41 2020
#
import wx
import wx.grid
import gettext
gettext.install(None)
from pubsub import pub
print('pubsub API version', pub.VERSION_API)
from pubsub.utils.notification import useNotifyByWriteFile
import sys
useNotifyByWriteFile(sys.stdout)
import pprint
from stabiliti import StabilitiController as SC
from faults import faultInfo
from FaultView import FaultFrame
class Controller:
def __init__(self, view):
self.sc = None
self.view = view
self.timer = wx.Timer()
self.timer.Bind(wx.EVT_TIMER, self.OnTimer)
pub.subscribe(self.getFaultInfo, 'fault_selected')
pub.subscribe(self.startTimer, 'monitoring_started')
pub.subscribe(self.stopTimer, 'monitoring_stopped')
pub.subscribe(self.applyNetworkConfigs, 'apply_network_configs')
pub.subscribe(self.resetNetworkConfigs, 'reset_network_configs')
def startTimer(self):
self.timer.Start(2000)
def stopTimer(self):
self.timer.Stop()
def applyNetworkConfigs(self, ncDict):
if self.sc:
self.sc.checkClose()
self.sc = SC(**ncDict)
def resetNetworkConfigs(self):
if self.sc:
self.sc.checkClose()
self.sc = None
def getFaultInfo(self, pos):
y, x = pos
fid = x + y*8
if 0 <= fid <= 63:
try:
info_text = faultInfo[fid]
except IndexError:
info_text = "Fault Number: {} (Reserved)".format(fid)
self.view.text_fault_doc.SetValue(info_text)
status_text = "Not Connected"
if self.sc:
fDict = self.sc.readFaultDetail(fid)
status_text = pprint.pformat(fDict)
self.view.text_fault_status.SetValue(status_text)
def OnTimer(self, event):
factivity = self.sc.readFaultActivity()
foccurence = self.sc.readFaultOccurence()
self.view.SetFaultGrid(factivity, foccurence)
event.Skip()
class MyFrame(FaultFrame):
def __init__(self, *args, **kwds):
FaultFrame.__init__(self, *args, **kwds)
def OnFaultSelect(self, event): # wxGlade: MyFrame.<event_handler>
pub.sendMessage("fault_selected", pos=(event.GetRow(), event.GetCol()))
event.Skip()
def SetFaultGrid(self, fActivity, fOccurence):
if len(fActivity) == len(fOccurence) == 64:
for i in range(8):
for j in range(8):
if fActivity[i*8+j] == '1':
self.fault_grid.SetCellBackgroundColour(i, j, wx.LIGHT_GREY)
if fOccurence[i*8+j] == '1':
self.fault_grid.SetCellValue(i, j, "O")
def ToggleMonitoring(self, event): # wxGlade: MyFrame.<event_handler>
btnLabel = self.button_1.GetLabel()
if self.button_1.Value:
pub.sendMessage("monitoring_started")
self.button_1.SetLabel("Stop")
else:
pub.sendMessage("monitoring_stopped")
self.button_1.SetLabel("Start")
if event:
event.Skip()
def OnApply(self, event):
if self.button_1.Enabled:
return
self.input_uid.Disable()
self.input_port.Disable()
self.input_ip.Disable()
self.input_refresh.Disable()
ncDict = {
"ipaddr": self.input_ip.GetValue(),
"port": int(self.input_port.GetValue()),
"uid": int(self.input_uid.GetValue()),
}
pub.sendMessage("apply_network_configs", ncDict=ncDict)
self.button_1.Enable()
def OnReset(self, event):
if not self.button_1.Enabled:
return
pub.sendMessage("reset_network_configs")
self.button_1.SetValue(False)
self.ToggleMonitoring(None)
self.button_1.Disable()
self.input_uid.Enable()
self.input_port.Enable()
self.input_ip.Enable()
self.input_refresh.Enable()
# end of class MyFrame
class MyApp(wx.App):
def OnInit(self):
self.frame = MyFrame(None, wx.ID_ANY, "")
self.c = Controller(self.frame)
self.SetTopWindow(self.frame)
self.frame.Show()
return True
# end of class MyApp
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()
app.c.sc.checkClose()