2022-10-14 16:11:01 +00:00
|
|
|
# Worker for the pyPLC
|
|
|
|
#
|
2022-11-30 19:28:59 +00:00
|
|
|
# Tested on
|
|
|
|
# - Windows10 with python 3.9 and
|
|
|
|
# - Raspbian with python 3.9
|
2022-10-14 16:11:01 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
#------------------------------------------------------------
|
|
|
|
import pyPlcHomeplug
|
2022-11-07 08:21:25 +00:00
|
|
|
import fsmEvse
|
|
|
|
import fsmPev
|
|
|
|
from pyPlcModes import *
|
2022-11-11 10:21:28 +00:00
|
|
|
import addressManager
|
2022-11-25 11:17:53 +00:00
|
|
|
import time
|
2022-11-30 19:28:59 +00:00
|
|
|
import subprocess
|
2022-12-06 17:35:17 +00:00
|
|
|
import hardwareInterface
|
2022-11-11 10:21:28 +00:00
|
|
|
|
2022-10-14 16:11:01 +00:00
|
|
|
|
|
|
|
class pyPlcWorker():
|
2022-11-16 18:33:37 +00:00
|
|
|
def __init__(self, callbackAddToTrace=None, callbackShowStatus=None, mode=C_EVSE_MODE, isSimulationMode=0):
|
2022-10-14 16:11:01 +00:00
|
|
|
print("initializing pyPlcWorker")
|
|
|
|
self.nMainFunctionCalls=0
|
2022-11-07 08:21:25 +00:00
|
|
|
self.mode = mode
|
2022-10-14 16:11:01 +00:00
|
|
|
self.strUserAction = ""
|
2022-11-11 10:21:28 +00:00
|
|
|
self.addressManager = addressManager.addressManager()
|
2022-10-14 16:11:01 +00:00
|
|
|
self.callbackAddToTrace = callbackAddToTrace
|
|
|
|
self.callbackShowStatus = callbackShowStatus
|
2022-11-15 18:36:20 +00:00
|
|
|
self.oldAvlnStatus = 0
|
2022-11-16 18:33:37 +00:00
|
|
|
self.isSimulationMode = isSimulationMode
|
2023-02-13 20:34:13 +00:00
|
|
|
self.hp = pyPlcHomeplug.pyPlcHomeplug(self.workerAddToTrace, self.showStatus, self.mode, self.addressManager, self.callbackReadyForTcp, self.isSimulationMode)
|
|
|
|
self.hardwareInterface = hardwareInterface.hardwareInterface(self.workerAddToTrace, self.showStatus)
|
2022-11-22 20:34:27 +00:00
|
|
|
self.hp.printToUdp("pyPlcWorker init")
|
2022-11-30 19:28:59 +00:00
|
|
|
# Find out the version number, using git.
|
|
|
|
# see https://stackoverflow.com/questions/14989858/get-the-current-git-hash-in-a-python-script
|
|
|
|
try:
|
|
|
|
strLabel = str(subprocess.check_output(["git", "describe", "--tags"], text=True).strip())
|
|
|
|
except:
|
|
|
|
strLabel = "(unknown version. 'git describe --tags' failed.)"
|
|
|
|
self.workerAddToTrace("[pyPlcWorker] Software version " + strLabel)
|
2022-11-07 08:21:25 +00:00
|
|
|
if (self.mode == C_EVSE_MODE):
|
2023-02-13 20:34:13 +00:00
|
|
|
self.evse = fsmEvse.fsmEvse(self.addressManager, self.workerAddToTrace, self.hardwareInterface, self.showStatus)
|
2022-11-07 08:21:25 +00:00
|
|
|
if (self.mode == C_PEV_MODE):
|
2023-02-13 20:34:13 +00:00
|
|
|
self.pev = fsmPev.fsmPev(self.addressManager, self.workerAddToTrace, self.hardwareInterface, self.showStatus)
|
2022-12-09 11:06:10 +00:00
|
|
|
def __del__(self):
|
|
|
|
if (self.mode == C_PEV_MODE):
|
2023-03-22 07:44:07 +00:00
|
|
|
try:
|
|
|
|
del(self.pev)
|
|
|
|
except:
|
|
|
|
pass
|
2022-12-09 11:06:10 +00:00
|
|
|
|
2022-11-22 20:34:27 +00:00
|
|
|
def workerAddToTrace(self, s):
|
|
|
|
# The central logging function. All logging messages from the different parts of the project
|
|
|
|
# shall come here.
|
|
|
|
#print("workerAddToTrace " + s)
|
|
|
|
self.callbackAddToTrace(s) # give the message to the upper level, eg for console log.
|
|
|
|
self.hp.printToUdp(s) # give the message to the udp for remote logging.
|
2022-10-14 16:11:01 +00:00
|
|
|
|
2023-02-27 09:53:42 +00:00
|
|
|
def showStatus(self, s, selection = "", strAuxInfo1="", strAuxInfo2=""):
|
2023-02-13 20:34:13 +00:00
|
|
|
self.callbackShowStatus(s, selection)
|
|
|
|
if (selection == "pevState"):
|
2023-02-27 09:53:42 +00:00
|
|
|
self.hardwareInterface.showOnDisplay(s, strAuxInfo1, strAuxInfo2)
|
2022-11-15 18:36:20 +00:00
|
|
|
|
2022-11-17 12:08:36 +00:00
|
|
|
def callbackReadyForTcp(self, status):
|
2022-11-15 18:36:20 +00:00
|
|
|
if (status==1):
|
2022-11-22 20:34:27 +00:00
|
|
|
self.workerAddToTrace("[PLCWORKER] Network is established, ready for TCP.")
|
2022-11-30 18:58:26 +00:00
|
|
|
#self.workerAddToTrace("[PLCWORKER] Waiting....")
|
|
|
|
#time.sleep(5)
|
|
|
|
#self.workerAddToTrace("[PLCWORKER] now...")
|
2022-11-15 18:36:20 +00:00
|
|
|
if (self.oldAvlnStatus==0):
|
|
|
|
self.oldAvlnStatus = 1
|
|
|
|
if (self.mode == C_PEV_MODE):
|
|
|
|
self.pev.reInit()
|
|
|
|
|
|
|
|
else:
|
2022-11-22 20:34:27 +00:00
|
|
|
self.workerAddToTrace("[PLCWORKER] no network")
|
2022-11-15 18:36:20 +00:00
|
|
|
self.oldAvlnStatus = 0
|
2022-10-14 16:11:01 +00:00
|
|
|
|
|
|
|
def mainfunction(self):
|
|
|
|
self.nMainFunctionCalls+=1
|
2022-10-14 21:36:03 +00:00
|
|
|
#self.showStatus("pyPlcWorker loop " + str(self.nMainFunctionCalls))
|
2022-12-06 17:35:17 +00:00
|
|
|
self.hp.mainfunction() # call the lower-level workers
|
|
|
|
self.hardwareInterface.mainfunction()
|
2022-11-07 08:21:25 +00:00
|
|
|
if (self.mode == C_EVSE_MODE):
|
|
|
|
self.evse.mainfunction() # call the evse state machine
|
|
|
|
if (self.mode == C_PEV_MODE):
|
|
|
|
self.pev.mainfunction() # call the pev state machine
|
2022-10-14 16:11:01 +00:00
|
|
|
|
|
|
|
def handleUserAction(self, strAction):
|
|
|
|
self.strUserAction = strAction
|
2022-12-19 17:09:39 +00:00
|
|
|
print("user action " + strAction)
|
2022-10-19 16:52:43 +00:00
|
|
|
if (strAction == "P"):
|
2022-11-07 08:21:25 +00:00
|
|
|
print("switching to PEV mode")
|
|
|
|
self.mode = C_PEV_MODE
|
|
|
|
if (hasattr(self, 'evse')):
|
|
|
|
print("deleting evse")
|
|
|
|
del self.evse
|
2022-10-19 16:52:43 +00:00
|
|
|
self.hp.enterPevMode()
|
2022-11-07 08:21:25 +00:00
|
|
|
if (not hasattr(self, 'pev')):
|
|
|
|
print("creating pev")
|
2022-12-19 17:09:39 +00:00
|
|
|
self.pev = fsmPev.fsmPev(self.addressManager, self.workerAddToTrace, self.hardwareInterface, self.callbackShowStatus)
|
2022-11-07 08:21:25 +00:00
|
|
|
self.pev.reInit()
|
2022-10-19 16:52:43 +00:00
|
|
|
if (strAction == "E"):
|
2022-11-07 08:21:25 +00:00
|
|
|
print("switching to EVSE mode")
|
|
|
|
self.mode = C_EVSE_MODE
|
|
|
|
if (hasattr(self, 'pev')):
|
|
|
|
print("deleting pev")
|
|
|
|
del self.pev
|
2022-10-19 16:52:43 +00:00
|
|
|
self.hp.enterEvseMode()
|
2022-11-07 08:21:25 +00:00
|
|
|
if (not hasattr(self, 'evse')):
|
|
|
|
print("creating fsmEvse")
|
2022-11-22 20:34:27 +00:00
|
|
|
self.evse = fsmEvse.fsmEvse(self.workerAddToTrace)
|
2022-11-07 08:21:25 +00:00
|
|
|
self.evse.reInit()
|
2022-10-19 16:52:43 +00:00
|
|
|
if (strAction == "L"):
|
2022-11-07 08:21:25 +00:00
|
|
|
print("switching to LISTEN mode")
|
|
|
|
self.mode = C_LISTEN_MODE
|
2022-10-24 21:19:45 +00:00
|
|
|
self.hp.enterListenMode()
|
2022-11-07 08:21:25 +00:00
|
|
|
if (hasattr(self, 'evse')):
|
|
|
|
print("deleting evse")
|
|
|
|
del self.evse
|
|
|
|
if (hasattr(self, 'pev')):
|
|
|
|
print("deleting pev")
|
|
|
|
del self.pev
|
2022-12-19 17:09:39 +00:00
|
|
|
if (strAction == "space"):
|
|
|
|
print("stopping the charge process")
|
|
|
|
if (hasattr(self, 'pev')):
|
|
|
|
self.pev.stopCharging()
|
2022-10-24 21:19:45 +00:00
|
|
|
# self.addToTrace("UserAction " + strAction)
|
2022-10-16 00:57:57 +00:00
|
|
|
self.hp.sendTestFrame(strAction)
|
2022-10-14 16:11:01 +00:00
|
|
|
|