Added thread locking instead of variable lock

This commit is contained in:
YuruC3 2025-07-16 17:47:42 +02:00
parent c5782018b8
commit 1f96e10f76

View File

@ -28,7 +28,9 @@ TEMPREADINTERVAL = float(os.getenv("TEMPREADINTERVAL", 15))
TEMPSETING = bool(os.getenv("TEMPSETING", True)) TEMPSETING = bool(os.getenv("TEMPSETING", True))
PROCESSTEMPWAITTIME = float(os.getenv("PROCESSTEMPWAITTIME"), 0.75) PROCESSTEMPWAITTIME = float(os.getenv("PROCESSTEMPWAITTIME", 0.75))
BACKOFFTIME = float(os.getenv("BACKOFFTIME", 0.1))
# INFLUXDB config # INFLUXDB config
# token = "apg1gysUeCcxdcRTMmosJTenbEppmUNi9rXlANDB2oNadBdWAu2GVTDc_q_dyo0iyYsckKaOvPRm6ba2NK0y_A==" # token = "apg1gysUeCcxdcRTMmosJTenbEppmUNi9rXlANDB2oNadBdWAu2GVTDc_q_dyo0iyYsckKaOvPRm6ba2NK0y_A=="
@ -60,7 +62,7 @@ MDserial = serial.Serial(
lastTempReading = time.time() lastTempReading = time.time()
MDtempDict = {} MDtempDict = {}
MDict = {} MDict = {}
currentSerialUsage = False currentSerialUsage = threading.Lock()
fluxSending = False fluxSending = False
currentTime = 0 currentTime = 0
lastTempReading = 0 lastTempReading = 0
@ -73,15 +75,15 @@ write_api = inflxdb_client.write_api(write_options=SYNCHRONOUS)
def getTemp(): def getTemp():
global MDict, fluxSending, currentSerialUsage global MDict, fluxSending
currentSerialUsage = True
with currentSerialUsage:
MDserial.write(f"{GETTMPCMND}\n\r".encode()) MDserial.write(f"{GETTMPCMND}\n\r".encode())
time.sleep(1) time.sleep(1)
MDreturning = MDserial.read_until(" >").decode() MDreturning = MDserial.read_until(" >").decode()
currentSerialUsage = False
# MDict = {} # MDict = {}
@ -142,7 +144,6 @@ def getTemp():
def setSpeed(inSpeeDict: dict): def setSpeed(inSpeeDict: dict):
global currentSerialUsage
bpavrg = 0 bpavrg = 0
# default # default
@ -154,23 +155,21 @@ def setSpeed(inSpeeDict: dict):
outfanprcntg = int((bpavrg / (HIGH_FAN_TRSHD - LOW_FAN_TRSHD)) * TEMP_FACTOR) outfanprcntg = int((bpavrg / (HIGH_FAN_TRSHD - LOW_FAN_TRSHD)) * TEMP_FACTOR)
# os.system(f"echo setting {outfanprcntg}%") # os.system(f"echo setting {outfanprcntg}%")
currentSerialUsage = True with currentSerialUsage:
# Set fan speed # Set fan speed
if outfanprcntg >= 20: if outfanprcntg >= 20:
MDserial.write((f"{SETFANCMND} {str(outfanprcntg)} \n\r").encode()) MDserial.write((f"{SETFANCMND} {str(outfanprcntg)} \n\r").encode())
print(f"setting {outfanprcntg}%", flush=True) print(f"setting {outfanprcntg}%", flush=True)
currentSerialUsage = False
return 0 return 0
else: else:
# Set default value # Set default value
MDserial.write((f"{SETFANCMND} {str(DEFOUTPRCNTG)} \n\r").encode()) MDserial.write((f"{SETFANCMND} {str(DEFOUTPRCNTG)} \n\r").encode())
currentSerialUsage = False
return 1 return 1
currentSerialUsage = False
# If something goes super wrong # If something goes super wrong
return -1 return -1
@ -233,7 +232,7 @@ lastTempReading = time.time()
def mainCodeHere(): def mainCodeHere():
while True: while True:
global MDict, fluxSending, currentTime, lastTempReading, currentSerialUsage global MDict, fluxSending, currentTime, lastTempReading
# https://stackoverflow.com/questions/52578122/not-able-to-send-the-enter-command-on-pyserial # https://stackoverflow.com/questions/52578122/not-able-to-send-the-enter-command-on-pyserial
# get temperature data, wait for MD1200 to answer and store # get temperature data, wait for MD1200 to answer and store
@ -241,6 +240,7 @@ def mainCodeHere():
currentTime = time.time() currentTime = time.time()
if currentTime - lastTempReading >= TEMPREADINTERVAL: if currentTime - lastTempReading >= TEMPREADINTERVAL:
getTemp() getTemp()
lastTempReading = currentTime lastTempReading = currentTime
@ -270,11 +270,11 @@ def mainCodeHere():
else: else:
print(f"temperature not yet pulled.\nFalling back do default fan speed", flush=True) print(f"temperature not yet pulled.\nFalling back do default fan speed", flush=True)
# os.system(f"echo temperature not yet pulled.\nFalling back do default fan speed") # os.system(f"echo temperature not yet pulled.\nFalling back do default fan speed")
currentSerialUsage = True
with currentSerialUsage:
MDserial.write((f"{SETFANCMND} {str(DEFOUTPRCNTG)} \n\r").encode()) MDserial.write((f"{SETFANCMND} {str(DEFOUTPRCNTG)} \n\r").encode())
currentSerialUsage = False
time.sleep(EPPYSLEEPY) time.sleep(EPPYSLEEPY)