Docker is now working

This commit is contained in:
YuruC3 2025-05-10 13:44:21 +02:00
parent 964236b6b5
commit fa4153d68d
8 changed files with 235 additions and 18 deletions

View File

@ -0,0 +1,3 @@
baud_rate=
serial_adapter=
wait_time=

View File

@ -0,0 +1,28 @@
FROM alpine:latest
# https://docs.docker.com/reference/dockerfile/#environment-replacement
ENV MD1200BAUD=38400
ENV SERIALADAPTER=/dev/ttyUSB0
ENV EPPYSLEEPY=300
# VOLUME [""]
RUN apk update && \
apk add python3 py3-pip
RUN mkdir /etc/MD1200FAN/
WORKDIR /etc/MD1200FAN/
COPY ./mainDocker.py /etc/MD1200FAN/
# COPY ./requirements.txt /etc/MD1200FAN/
RUN python3 -m venv venv && \
venv/bin/python3 -m pip install --upgrade pip && \
venv/bin/pip3 install PySerial
# venv/bin/pip3 install -r requirements.txt
# VOLUME ["/etc/MD1200FAN/"]
CMD ["venv/bin/python3", "mainDocker.py"]
# CMD ["/etc/MD1200FAN/venv/bin/python3", "/etc/MD1200FAN/mainDocker.py"]

View File

@ -0,0 +1,15 @@
---
services:
mdfanchanger:
container_name: MD_Fan_Changer
build: ./
image: dm1200fanchanger:V1
environment:
- baud_rate=${baud_rate:-38400}
- serial_adapter=${serial_adapter:-"/dev/ttyUSB0"}
- wait_time=${wait_time:-300}
devices:
- /dev/ttyUSB0:/dev/ttyUSB0
restart: unless-stopped
privileged: false

View File

@ -0,0 +1,141 @@
import serial, time, os
# CONST
if os.environ["MD1200BAUD"]:
MD1200BAUD = int(os.environ["MD1200BAUD"])
else:
MD1200BAUD = 38400
if os.environ["SERIALADAPTER"]:
SERIALADAPTER = os.environ["SERIALADAPTER"]
else:
SERIALADAPTER = "/dev/ttyUSB0"
GETTEMP = "_temp_rd"
SETFANPRCNT = "set_speed"
if os.environ["EPPYSLEEPY"]:
EPPYSLEEPY = int(os.environ["EPPYSLEEPY"])
else:
EPPYSLEEPY = 300 # 5 minutes
# init
MDserial = serial.Serial(
port=SERIALADAPTER,\
baudrate=MD1200BAUD,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=1)
def getTemp(inpMDreturning):
MDict = {}
# Sanitise output
MDsanit = inpMDreturning.splitlines()
#if there is smth do smth
if inpMDreturning:
for line in MDsanit:
if ">" in line or "b'" in line:
continue
matchstm = line[2:6]
match matchstm:
case "BP_1":
MDict["bp1"] = int(line[12:14])
case "BP_2":
MDict["bp2"] = int(line[12:14])
case "SIM0":
MDict["sim0"] = int(line[12:14])
case "SIM1":
MDict["sim1"] = int(line[12:14])
case "EXP0":
MDict["exp0"] = int(line[12:14])
case "EXP1":
MDict["exp1"] = int(line[12:14])
case "AVG":
MDict["avg"] = int(line[12:14])
case _:
continue
return MDict
def setSpeed(inSpeeDict: dict):
bpavrg = 0
# Some safe fan speedvalue
defoutprntg = 27
# default
outfanprcntg = 0
# Decide on fan speeds
LOW_FAN_TRSHD = 21
HIGH_FAN_TRSHD = 40
TEMP_FACTOR = 19
# get backplanbe average
if inSpeeDict["bp1"] and inSpeeDict["bp2"]:
bpavrg = (inSpeeDict["bp1"] + inSpeeDict["bp2"]) /2
outfanprcntg = int((bpavrg / (HIGH_FAN_TRSHD - LOW_FAN_TRSHD)) * TEMP_FACTOR)
# Set fan speed
if outfanprcntg >= 20:
MDserial.write(("set_speed " + str(outfanprcntg) + " \n\r").encode())
print(f"setting {outfanprcntg}%")
return 0
else:
# Set default value
MDserial.write(("set_speed " + str(defoutprntg) + " \n\r").encode())
return 1
# If something goes super wrong
return -1
# Check if UART is used
# Not neede because when defining MDserial it gets automatically opened
# Will leave it here anyway
# try:
# MDserial.open()
# except serial.serialutil.SerialException:
# # MDserial.close()
# # MDserial.open()
# print("Port allready opened.\nTry closing it first")
while True:
# https://stackoverflow.com/questions/52578122/not-able-to-send-the-enter-command-on-pyserial
MDserial.write("_temp_rd\n\r".encode())
time.sleep(1)
MDreturning = MDserial.read_until(" >").decode()
MDtempDict = getTemp(MDreturning)
setSpeedrcode = setSpeed(MDtempDict)
# good
if setSpeedrcode == 0:
# print("Were mint")
time.sleep(EPPYSLEEPY)
# not good
elif setSpeedrcode == 1:
print("Ambigous temperature readings.\nFalling back to safe values.")
time.sleep(EPPYSLEEPY)
# very not good
elif setSpeedrcode == -1:
print("o nyo")
exit()
# very very very not good
else:
print("idk")
exit()
print("closing port")
MDserial.close()

View File

@ -5,8 +5,8 @@ MD1200BAUD = 38400
SERIALADAPTER = "/dev/ttyUSB0" SERIALADAPTER = "/dev/ttyUSB0"
GETTEMP = "_temp_rd" GETTEMP = "_temp_rd"
SETFANPRCNT = "set_speed" SETFANPRCNT = "set_speed"
EPYSLEEPY = 300 # 5 minutes EPPYSLEEPY = 300 # 5 minutes
#EPYSLEEPY = 150 # 2,5 minutes #EPPYSLEEPY = 150 # 2,5 minutes
# init # init
MDserial = serial.Serial( MDserial = serial.Serial(
@ -112,11 +112,11 @@ while True:
# good # good
if setSpeedrcode == 0: if setSpeedrcode == 0:
# print("Were mint") # print("Were mint")
time.sleep(EPYSLEEPY) time.sleep(EPPYSLEEPY)
# not good # not good
elif setSpeedrcode == 1: elif setSpeedrcode == 1:
print("Ambigous temperature readings.\nFalling back to safe values.") print("Ambigous temperature readings.\nFalling back to safe values.")
time.sleep(EPYSLEEPY) time.sleep(EPPYSLEEPY)
# very not good # very not good
elif setSpeedrcode == -1: elif setSpeedrcode == -1:
print("o nyo") print("o nyo")

View File

@ -1,6 +1,6 @@
[Unit] [Unit]
Description=Netflow to InfluxDB script Description=Adjust MD1200/MD1220 fan speeds
After=multi-user.target network.target network-online.target After=multi-user.target
# Place in /etc/systemd/system/ # Place in /etc/systemd/system/
[Service] [Service]
@ -10,14 +10,13 @@ Type=simple
Restart=on-failure Restart=on-failure
# EnvironmentFile=/etc/NetFlux/netflow.env # EnvironmentFile=/etc/NetFlux/netflow.env
# User=myuser # User=myuser
WorkingDirectory=/etc/NetFlux/HQ/ WorkingDirectory=/etc/MD1200FAN/
ExecStart=/etc/NetFlux/HQ/venv/bin/python3 /etc/NetFlux/HQ/HQnetflow.py --serve-in-foreground ExecStart=/etc/MD1200FAN/venv/bin/python3 /etc/MD1200FAN/main.py --serve-in-foreground
#StandardInput=tty-force #StandardInput=tty-force
# Log file will be create if it doesn't exist # Log file will be create if it doesn't exist
StandardOutput=append:/var/log/HQNetFlowInflux.log StandardOutput=append:/var/log/MD1200FAN.py.log
StandardError=append:/var/log/HQNetFlowInflux.errlog StandardError=append:/var/log/MD1200FAN.py.errlog
# StandardOutput=syslog # StandardOutput=syslog
# StandardError=syslog # StandardError=syslog

View File

@ -1,6 +1,6 @@
[Unit] [Unit]
Description=Netflow to InfluxDB script Description=Adjust MD1200/MD1220 fan speeds
After=multi-user.target network.target network-online.target After=multi-user.target
# Place in /etc/systemd/system/ # Place in /etc/systemd/system/
[Service] [Service]
@ -11,12 +11,12 @@ Restart=on-failure
# EnvironmentFile=/etc/NetFlux/netflow.env # EnvironmentFile=/etc/NetFlux/netflow.env
# User=myuser # User=myuser
WorkingDirectory=/dir/to/script/ WorkingDirectory=/dir/to/script/
ExecStart=/dir/to/script'sVENV/venv/bin/python3 /dir/to/script/NetFlowCollect.py --serve-in-foreground ExecStart=/dir/to/script'sVENV/venv/bin/python3 /dir/to/script/main.py --serve-in-foreground
#StandardInput=tty-force #StandardInput=tty-force
# Log file will be create if it doesn't exist # Log file will be create if it doesn't exist
StandardOutput=append:/var/log/NetFlowCollect.log StandardOutput=append:/var/log/MD1200FAN.py.log
StandardError=append:/var/log/NetFlowCollect.errlog StandardError=append:/var/log/MD1200FAN.py.errlog
# StandardOutput=syslog # StandardOutput=syslog
# StandardError=syslog # StandardError=syslog

View File

@ -4,9 +4,40 @@ A set of scripts that automagically set fan speed on a MD1200 (probably MD1220 a
## PC ## PC
Working working working. ### Docker
So far seems to work. In .env file change
```serial_adapter``` which is a serial port you're using.
On linux it is /dev/ttyUSBx and on windows it is COMx
```wait_time``` is the interval in which script is checking temperature. By default it is 300 seconds, which is 5 minutes.
### Systemd
First create virtual enviroment
```
python3 -m venv venv
```
Then install required modules
```
venv/bin/pip3 install PySerial
```
After that you just need to change a few things
```SERIALADAPTER``` to a port you're using.
On linux it is /dev/ttyUSBx and on windows it is COMx
```EPPYSLEEPY``` is the interval in which script is checking temperature. By default it is 300 seconds, which is 5 minutes.
### Proxmox LXC
You can also run it in LXC container on your Proxmox host. Just follow the [systemd](###Systemd) instructions.
Here you will also need to add ```/dev/ttyUSBx``` to your LXC container. You do it under Resources -> Add -> Device Passthrough -> ```/dev/ttyUSBx``` as Device Path.
## STM32F103C6T6 ## STM32F103C6T6