Skip to content

Commit

Permalink
Added the PLC connection sate and time display in the realworld emula…
Browse files Browse the repository at this point in the history
…tor.
  • Loading branch information
LiuYuancheng committed Jul 25, 2023
1 parent 503d0b5 commit 9d027a1
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 9 deletions.
Binary file modified doc/interface.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions src/metroEmuUI/configFiles/metroConfig_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# Test mode:
# True: use the real word internal logic to simulator the control logic.
# False: connect to PLC let plc control the signals
TEST_MD:True
TEST_MD:False

# Init the dataManager port for PCL to fetch and set data.
UDP_PORT:3001
Expand All @@ -16,11 +16,16 @@ UDP_PORT:3001
UI_INTERVAL:0.8

# Init Control paramters
COLL_AVOID:False
COLL_AVOID:True

# Set train dock time: ==0 train will stay in station in a random Interval, > 0 num of time
# circle train will stay in
DOCK_TIME:5

# Init Plc connection time out, if plc is not connect in num of seconds, the system will
# treate it as offline
PLC_TIMEOUT: 3

# Train config json file.
TR_CFG_FOLDER:TrainLocation

Expand Down
19 changes: 16 additions & 3 deletions src/metroEmuUI/dataMgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ def __init__(self, parent) -> None:
'ccline': None,
'mtline': None
}
self.sensorPlcUpdateT = time.time()
self.sensorPlcUpdateT = 0
# init the local station data record dictionary
self.stationsDict = {
'weline': None,
'nsline': None,
'ccline': None,
'mtline': None
}
self.stationPlcUpdateT = time.time()
self.stationPlcUpdateT = 0
# init the local train speed state dictionary
self.trainsDict = {
'weline': None,
'nsline': None,
'ccline': None,
'mtline': None
}
self.trainPlcUpdateT= time.time()
self.trainPlcUpdateT= 0
gv.gDebugPrint("datamanager init finished.", logType=gv.LOG_INFO)

#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -122,6 +122,19 @@ def fetchTrainInfo(self, reqJsonStr):
gv.gDebugPrint("fetchTrainInfo() Error: %s" %str(err), logType=gv.LOG_EXCEPT)
return respStr

#-----------------------------------------------------------------------------
def getLastPlcsConnectionState(self):
#print time.strftime("%b %d %Y %H:%M:%S", time.localtime(time.time))
crtTime = time.time()
sensorPlcOnline = crtTime - self.sensorPlcUpdateT < gv.gPlcTimeout
stationPlcOnline = crtTime - self.stationPlcUpdateT < gv.gPlcTimeout
trainPlcOnline = crtTime - self.trainPlcUpdateT < gv.gPlcTimeout
return {
'sensors': (time.strftime("%H:%M:%S", time.localtime(self.sensorPlcUpdateT)), sensorPlcOnline),
'stations': (time.strftime("%H:%M:%S", time.localtime(self.stationPlcUpdateT)), stationPlcOnline),
'trains': (time.strftime("%H:%M:%S", time.localtime(self.trainPlcUpdateT)), trainPlcOnline),
}

#-----------------------------------------------------------------------------
def msgHandler(self, msg):
""" Function to handle the data-fetch/control request from the monitor-hub.
Expand Down
Binary file added src/metroEmuUI/img/plcIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/metroEmuUI/img/time.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions src/metroEmuUI/metroEmuGobal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
# Author: Yuancheng Liu
#
# Created: 2010/08/26
# Copyright:
# License:
# Version: v_0.1.2
# Copyright: Copyright (c) 2023 LiuYuancheng
# License: MIT License
#-----------------------------------------------------------------------------
"""
For good coding practice, follow the following naming convention:
Expand Down Expand Up @@ -95,13 +96,15 @@
# False: The simultor will connect to the PLC, PLC will implement the control logic.
gTranspPct = 70 # Windows transparent percentage.
# main frame update rate 0.5 sec.
gUpdateRate = float(CONFIG_DICT['TEST_MD']) if float(CONFIG_DICT['TEST_MD']) > 0 else 0.5
gUpdateRate = float(CONFIG_DICT['UI_INTERVAL']) if float(CONFIG_DICT['UI_INTERVAL']) > 0 else 0.5
gTrainDefSpeed = 10 # Train default speed 10 pixels per refersh frame.
gSensorCount = 0 # number of sensors.
gMinTrainDist = 80 # min distance between each trains by refresh rate
gCollAvoid = CONFIG_DICT['COLL_AVOID'] # Auto avoid collision.
gDockTime = int(CONFIG_DICT['DOCK_TIME'])

gPlcTimeout = int(CONFIG_DICT['PLC_TIMEOUT'])

gTrackConfig = OrderedDict()
#gCollsionTestFlg = CONFIG_DICT['TEST_JC_COLLISION'] # flag used to enable test the train collision at the junction.
#gTrainDistTestFlag = CONFIG_DICT['TEST_TR_DISTANCE'] # flag used to see if the minimum distance between trains are observed
Expand Down
6 changes: 5 additions & 1 deletion src/metroEmuUI/railwayMgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,11 @@ def _initEnv(self):
{'id':'JurongEast-Jem', 'img':'city_0.png', 'pos':(360, 520), 'size':(80, 80)},
{'id':'CityHall-01', 'img':'city_2.png', 'pos':(750, 520), 'size':(90, 80)},
{'id':'CityHall-02', 'img':'city_1.png', 'pos':(850, 600), 'size':(80, 60)},
{'id':'Legend', 'img':'legend.png', 'pos':(1450, 820), 'size':(200, 150)}
{'id':'Legend', 'img':'legend.png', 'pos':(1450, 820), 'size':(200, 150)},
{'id':'Sensor-Signal-PLCs[PLC-00, PLC-01, PLC-02]', 'img': 'plcIcon.png', 'pos':(50, 750), 'size':(50,30)},
{'id':'Station-PLCs[PLC-03, PLC-04, PLC-05]', 'img': 'plcIcon.png', 'pos':(50, 810), 'size':(50,30)},
{'id':'Train-PLCs[PLC-06, PLC-07]', 'img': 'plcIcon.png', 'pos':(50, 870), 'size':(50,30)},
{'id':'Date & Time', 'img': 'time.png', 'pos':(1270, 50), 'size':(30,30)}
]
for info in envCfg:
imgPath = os.path.join(gv.IMG_FD, info['img'])
Expand Down
35 changes: 35 additions & 0 deletions src/metroEmuUI/railwayPanelMap.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#-----------------------------------------------------------------------------

import os
import time

import wx
import metroEmuGobal as gv

Expand Down Expand Up @@ -72,6 +74,39 @@ def _drawEnvItems(self, dc):
dc.SetBrush(wx.Brush(color))
dc.DrawRectangle(pos[0]-size[0]//2, pos[1]-size[1]//2, size[0], size[1])
dc.DrawText(str(id), pos[0]-size[0]//2+6, pos[1]-size[1]//2+6)
# Draw the current date and time
dc.SetFont(wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.BOLD))
dc.SetTextForeground(wx.Colour('GREEN'))
dc.DrawText(time.strftime("%b %d %Y %H:%M:%S", time.localtime(time.time())), 1300, 40)

# Draw the PLC state:
if gv.iDataMgr:
dc.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD))
plcStateDict = gv.iDataMgr.getLastPlcsConnectionState()
# draw sensor plc state
timeStr, state = plcStateDict['sensors']
textColor = wx.Colour('GREEN') if state else wx.Colour('RED')
dc.SetTextForeground(textColor)
connState = 'online' if state else 'offline'
dc.DrawText('Last update Time: '+str(timeStr), 80, 740)
dc.DrawText('Connection State: '+str(connState), 80, 755)

# draw sensor plc state
timeStr, state = plcStateDict['stations']
textColor = wx.Colour('GREEN') if state else wx.Colour('RED')
dc.SetTextForeground(textColor)
connState = 'online' if state else 'offline'
dc.DrawText('Last update Time: '+str(timeStr), 80, 800)
dc.DrawText('Connection State: '+str(connState), 80, 815)

# draw trains plc state
timeStr, state = plcStateDict['trains']
textColor = wx.Colour('GREEN') if state else wx.Colour('RED')
dc.SetTextForeground(textColor)
connState = 'online' if state else 'offline'
dc.DrawText('Last update Time: '+str(timeStr), 80, 860)
dc.DrawText('Connection State: '+str(connState), 80, 875)


#-----------------------------------------------------------------------------
def _drawJunction(self, dc):
Expand Down

0 comments on commit 9d027a1

Please sign in to comment.