mirror of
https://github.com/uhi22/pyPLC.git
synced 2024-11-10 01:05:42 +00:00
feature: added connection manager draft
This commit is contained in:
parent
ce4577da09
commit
3b244ec3ca
1 changed files with 143 additions and 0 deletions
143
connMgr.py
Normal file
143
connMgr.py
Normal file
|
@ -0,0 +1,143 @@
|
|||
|
||||
|
||||
|
||||
# Connection Manager
|
||||
|
||||
# This module is informed by the several state machines in case of good connection.
|
||||
# It calculates an overall ConnectionLevel.
|
||||
# This ConnectionLevel is provided to the state machines, so that each state machine
|
||||
# has the possiblity to decide whether it needs to do something or just stays silent.
|
||||
#
|
||||
# The basic rule is, that a good connection on higher layer (e.g. TCP) implicitely
|
||||
# confirms the good connection on lower layer (e.g. Modem presence). This means,
|
||||
# the lower-layer state machine can stay silent as long as the upper layers are working
|
||||
# fine.
|
||||
|
||||
CONNLEVEL_100_APPL_RUNNING = 100
|
||||
CONNLEVEL_80_TCP_RUNNING = 80
|
||||
CONNLEVEL_50_SDP_DONE = 50
|
||||
CONNLEVEL_20_TWO_MODEMS_FOUND = 20
|
||||
CONNLEVEL_15_SLAC_ONGOING = 15
|
||||
CONNLEVEL_10_ONE_MODEM_FOUND = 10
|
||||
CONNLEVEL_5_ETH_LINK_PRESENT = 5
|
||||
|
||||
CONNMGR_TIMER_MAX = (5*33) # 5 seconds until an OkReport is forgotten.
|
||||
CONNMGR_TIMER_MAX_10s = (10*33) # 10 seconds until an OkReport is forgotten.
|
||||
CONNMGR_TIMER_MAX_15s = (15*33) # 15 seconds until an OkReport is forgotten.
|
||||
|
||||
|
||||
class connMgr():
|
||||
def getConnectionLevel(self):
|
||||
return self.ConnectionLevel
|
||||
|
||||
def __init__(self, callbackAddToTrace, callbackShowStatus):
|
||||
self.timerEthLink = 0
|
||||
self.timerModemLocal = 0
|
||||
self.timerModemRemote = 0
|
||||
self.timerSlac = 0
|
||||
self.timerSDP = 0
|
||||
self.timerTCP = 0
|
||||
self.timerAppl = 0
|
||||
self.ConnectionLevel = 0
|
||||
self.ConnectionLevelOld = 0
|
||||
self.cycles = 0
|
||||
self.addToTrace = callbackAddToTrace
|
||||
|
||||
def mainfunction(self):
|
||||
# count all the timers down
|
||||
if (self.timerEthLink>0):
|
||||
self.timerEthLink-=1
|
||||
if (self.timerModemLocal>0):
|
||||
self.timerModemLocal-=1
|
||||
if (self.timerModemRemote>0):
|
||||
self.timerModemRemote-=1
|
||||
if (self.timerSlac>0):
|
||||
self.timerSlac-=1
|
||||
if (self.timerSDP>0):
|
||||
self.timerSDP-=1
|
||||
if (self.timerTCP>0):
|
||||
self.timerTCP-=1
|
||||
if (self.timerAppl>0):
|
||||
self.timerAppl-=1
|
||||
# Based on the timers, calculate the connectionLevel.
|
||||
if (self.timerAppl>0):
|
||||
self.ConnectionLevel=CONNLEVEL_100_APPL_RUNNING
|
||||
else:
|
||||
if (self.timerTCP>0):
|
||||
self.ConnectionLevel=CONNLEVEL_80_TCP_RUNNING
|
||||
else:
|
||||
if (self.timerSDP>0):
|
||||
self.ConnectionLevel=CONNLEVEL_50_SDP_DONE
|
||||
else:
|
||||
if (self.timerModemRemote>0):
|
||||
self.ConnectionLevel=CONNLEVEL_20_TWO_MODEMS_FOUND
|
||||
else:
|
||||
if (self.timerSlac>0):
|
||||
self.ConnectionLevel=CONNLEVEL_15_SLAC_ONGOING
|
||||
else:
|
||||
if (self.timerModemLocal>0):
|
||||
self.ConnectionLevel=CONNLEVEL_10_ONE_MODEM_FOUND
|
||||
else:
|
||||
if (self.timerEthLink>0):
|
||||
self.ConnectionLevel=CONNLEVEL_5_ETH_LINK_PRESENT
|
||||
else:
|
||||
self.ConnectionLevel=0
|
||||
if (self.ConnectionLevelOld!=self.ConnectionLevel):
|
||||
self.addToTrace("[CONNMGR] ConnectionLevel changed from " + str(self.ConnectionLevelOld) + " to " + str(self.ConnectionLevel))
|
||||
self.ConnectionLevelOld = self.ConnectionLevel
|
||||
self.cycles+=1
|
||||
|
||||
def ModemFinderOk(self, numberOfFoundModems):
|
||||
if (numberOfFoundModems>=1):
|
||||
self.timerModemLocal = CONNMGR_TIMER_MAX
|
||||
if (numberOfFoundModems>=2):
|
||||
self.timerModemRemote = CONNMGR_TIMER_MAX_10s # 10s for the slac sequence, to avoid too fast timeout
|
||||
|
||||
def SlacOk(self):
|
||||
# The SetKey was sent to the local modem. This leads to restart of the
|
||||
# local modem, and potenially also for the remote modem. If both modems are up,
|
||||
# they need additional time to pair. We need to be patient during this process. */
|
||||
self.timerSlac = CONNMGR_TIMER_MAX_15s
|
||||
|
||||
def SdpOk(self):
|
||||
self.timerSDP = CONNMGR_TIMER_MAX
|
||||
|
||||
def TcpOk(self):
|
||||
self.timerTCP = CONNMGR_TIMER_MAX
|
||||
|
||||
def ApplOk(self):
|
||||
self.timerAppl = CONNMGR_TIMER_MAX
|
||||
|
||||
|
||||
def testCallbackAddToTrace(s):
|
||||
print("callbackAddToTrace: " + s)
|
||||
|
||||
def testCallbackShowStatus(s):
|
||||
print("callbackShowStatus: " + s)
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Testing ConnectionManager...")
|
||||
cm = connMgr(testCallbackAddToTrace, testCallbackShowStatus)
|
||||
print("connection level " + str(cm.getConnectionLevel()))
|
||||
cm.mainfunction()
|
||||
print("connection level " + str(cm.getConnectionLevel()))
|
||||
cm.SlacOk()
|
||||
cm.mainfunction()
|
||||
print("connection level " + str(cm.getConnectionLevel()))
|
||||
for i in range(1000):
|
||||
cm.mainfunction()
|
||||
cm.ModemFinderOk(1)
|
||||
for i in range(1000):
|
||||
cm.mainfunction()
|
||||
cm.ModemFinderOk(2)
|
||||
for i in range(1000):
|
||||
cm.mainfunction()
|
||||
cm.SdpOk()
|
||||
for i in range(1000):
|
||||
cm.mainfunction()
|
||||
cm.TcpOk()
|
||||
for i in range(1000):
|
||||
cm.mainfunction()
|
||||
cm.ApplOk()
|
||||
for i in range(1000):
|
||||
cm.mainfunction()
|
Loading…
Reference in a new issue