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