Commit 5c39c2aa authored by Mikhail Karpenko's avatar Mikhail Karpenko

Install tempmon to /usr/bin and update its init script

parent f7dc5d36
#!/bin/sh
DEAMON=/usr/bin/init_tempmon.py
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/init_tempmon
NAME=init_tempmon
DESC="temperature monitor daemon"
SCRIPT_NAME=init_tempmon.py
MNTPOINT=/mnt/mmc
MMCDEV=/dev/mmcblk0p1
SOME_SCRIPT=init_tempmon.py
# PID file name, must match to the name in init_tempmon.py sript
PID_FILE=/var/run/init_tempmon.pid
. /etc/init.d/functions
test -x $DEAMON || exit 0
case "$1" in
start)
echo -n "Starting $DESC: "
if [ -f $MNTPOINT/$SOME_SCRIPT ]; then
echo " Launching $SOME_SCRIPT"
$MNTPOINT/$SOME_SCRIPT
if [ ! -f $PID_FILE ]; then
echo -n "Starting $DESC: "
echo $SCRIPT_NAME
$DEAMON &
else
echo " $SOME_SCRIPT not found. Nothing to launch."
echo "Already running"
fi
;;
stop)
echo -n "Stopping $DESC: "
echo "$NAME."
if [ -f $PID_FILE ]; then
echo -n "Stopping $DESC: "
echo "$NAME"
pid=$(cat $PID_FILE)
/bin/kill $pid
rm $PID_FILE
else
echo "Not running, nothing to stop"
fi
;;
restart)
echo -n "Restarting $DESC: "
echo "$NAME."
if [ -f $PID_FILE ]; then
echo -n "Restarting $DESC: "
echo "$NAME"
pid=$(cat $PID_FILE)
/bin/kill $pid
$DEAMON &
else
echo "Not running, nothing to restart"
fi
;;
status)
echo -n "$NAME status:"
if [ -f /var/run/$NAME ]; then
echo -n "Running"
echo -n "$NAME status: "
if [ -f $PID_FILE ]; then
echo "Running"
else
echo -n "Not running"
echo "Not running"
fi
;;
*)
......
......@@ -26,6 +26,7 @@ import os
import sys
import time
import subprocess
import atexit
from posix import EX_OSFILE, F_OK
DEBUG = 0
......@@ -40,6 +41,8 @@ TEMP_HYSTERESIS = 2.0
SAMPLING_TIME = 1
""" Temperature scaling factor. """
SCALE_CFT = 0.001
""" The name of the file in /var/run which will contain PID """
PID_NAME = "init_tempmon"
ctrl_path_pref = "/sys/devices/soc0/elphel393-pwr@0/"
ctrl_fn = {"poweroff_fn": "power_off",
......@@ -47,11 +50,12 @@ ctrl_fn = {"poweroff_fn": "power_off",
"vp5_dis_fn": "channels_dis",
"gpio_fn": "gpio_10389"}
hwmon_path_pref = "/sys/devices/soc0/amba@0/f8007100.ps7-xadc/iio:device0/"
hwmon_fn = {"offset": "in_temp0_offset",
"raw": "in_temp0_raw",
"scale": "in_temp0_scale"}
hwmon_fn = {"offset": "in_temp0_offset",
"raw": "in_temp0_raw",
"scale": "in_temp0_scale"}
out_path_pref = "/var/volatile/tmp/"
out_fn = {"core_temp_fn": "core_temp"}
out_fn = {"core_temp_fn": "core_temp",
"temp_params_fn": "core_temp_params"}
class temp_monitor():
def __init__(self, ctrl_path_pref, ctrl_file_names, hwmon_path_pref, hwmon_fn,
......@@ -63,17 +67,36 @@ class temp_monitor():
self.hwmon_path_prefix = hwmon_path_pref
self.hwmon_fnames = hwmon_fn
self.samples_window = []
self.pid_fname = "/var/run/" + PID_NAME + ".pid"
""" Number of samples for temperature averaging """
self.samples_num = 5
self.fan_cmd = {"fan_cmd_on": "0x101",
"fan_cmd_off": "0x100"}
self.params = {"temp_minor": TEMP_FAN_ON,
"temp_major": TEMP_SHUTDOWN,
"temp_hyst": TEMP_HYSTERESIS,
"temp_sampling_time": SAMPLING_TIME}
""" Create output files """
try:
self.core_temp_out_f = open(out_path_prefix + out_fnames["core_temp_fn"], "w")
except IOError:
print "Failed to create file: '%s%s'" % (out_path_prefix, out_fnames["core_temp_fn"])
try:
self.core_temp_params_f = open(out_path_prefix + out_fnames["temp_params_fn"], "w")
except IOError:
self.core_temp_out_f.close()
print "Failed to create file: '%s%s'" % (out_path_prefix, out_fnames["temp_params_fn"])
""" Create pid file """
with open(self.pid_fname, "w") as pid_file:
pid_file.write(str(os.getpid()))
atexit.register(self.delpid)
def delpid(self):
if os.access(self.pid_fname, F_OK):
os.remove(self.pid_fname)
def check_files(self):
"""
Check if all files exist in the system.
......@@ -141,22 +164,36 @@ class temp_monitor():
samples.sort()
avg = samples[len(samples) / 2]
if core_temp < (TEMP_FAN_ON - TEMP_HYSTERESIS) and self.is_fan_on:
if core_temp < (self.params["temp_minor"] - self.params["temp_hyst"]) and self.is_fan_on:
self.fan_ctrl("off")
elif core_temp >= TEMP_FAN_ON and core_temp < TEMP_SHUTDOWN and not self.is_fan_on:
elif core_temp >= self.params["temp_minor"] and core_temp < self.params["temp_major"] and not self.is_fan_on:
self.fan_ctrl("on")
elif avg >= TEMP_SHUTDOWN:
elif avg >= self.params["temp_major"]:
self.shutdown()
self.core_temp_out_f.seek(0)
self.core_temp_out_f.write(str(core_temp))
self.core_temp_out_f.flush()
print "Core temperature: '%f', median: '%f'" % (core_temp, avg)
time.sleep(SAMPLING_TIME)
# print "Core temperature: '%f', median: '%f'" % (core_temp, avg)
time.sleep(self.params["temp_sampling_time"])
except (KeyboardInterrupt, SystemExit):
print "Got keyboard interrupt. Exiting."
self.core_temp_out_f.close()
self.core_temp_params_f.close()
os.remove(self.pid_fname)
print "'%s': got keyboard interrupt. Exiting." % os.path.basename(__file__)
sys.exit(0)
def read_temp_params(self):
"""
Read parameters from file and update local values.
"""
for key, val in self.params.items():
self.core_temp_params_f.seek(0)
file_lines = self.core_temp_params_f.readlines()
for line in file_lines:
if line.find(key) == 0:
self.core_temp_params_f[key] = float(line.split(":")[1].strip())
if __name__ == "__main__":
if DEBUG:
......
......@@ -21,7 +21,7 @@ SRC_URI = "file://init_tempmon \
S = "${WORKDIR}/"
INITSCRIPT_NAME = "init_tempmon"
INITSCRIPT_PARAMS = "defaults 96"
INITSCRIPT_PARAMS = "defaults 94"
RDEPENDS_${PN} += "python-core"
......@@ -29,6 +29,7 @@ FILES_${PN} = "\
/etc/* \
/usr/* \
"
PACKAGES = " init-tempmon"
#This needs to get the script into rc?.d/
inherit update-rc.d
......@@ -36,20 +37,6 @@ inherit update-rc.d
do_install_append() {
install -d ${D}${sysconfdir}/init.d
install -m 0755 ${WORKDIR}/init_tempmon ${D}${sysconfdir}/init.d
for RLOC in ${PRODUCTION_ROOT_LOCATION}; do
if [ ! -d ${DEPLOY_DIR_IMAGE}/${RLOC} ]; then
mkdir -p ${DEPLOY_DIR_IMAGE}/${RLOC}
fi
if [ -f ${S}init_tempmon.py ]; then
if [ -f ${DEPLOY_DIR_IMAGE}/${RLOC}/init_tempmon.py ]; then
rm ${DEPLOY_DIR_IMAGE}/${RLOC}/init_tempmon.py
fi
cp ${S}init_tempmon.py ${DEPLOY_DIR_IMAGE}/${RLOC}/init_tempmon.py
else
echo "NOT 3 FOUND!"
fi
done
install -d ${D}${bindir}
install -m 0755 ${S}/init_tempmon.py ${D}${bindir}
}
PACKAGES = " init-tempmon"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment