From ddea53931f5e309aa390f36b99d74031cdc6d984 Mon Sep 17 00:00:00 2001
From: Thomas Flori <thflori@gmail.com>
Date: Wed, 8 Feb 2023 11:53:30 +0100
Subject: [PATCH] implement pid check and add install script

---
 app/Command/Daemon.php |   9 ++++
 composer.json          |   5 +-
 example-config.php     | 114 +++++++++++++++++++++++++++++++++++++++++
 install.sh             |  42 +++++++++++++++
 4 files changed, 168 insertions(+), 2 deletions(-)
 create mode 100644 example-config.php
 create mode 100755 install.sh

diff --git a/app/Command/Daemon.php b/app/Command/Daemon.php
index 06fc367..06af7bc 100644
--- a/app/Command/Daemon.php
+++ b/app/Command/Daemon.php
@@ -8,6 +8,8 @@ use Hugga\Console;
 
 class Daemon extends AbstractCommand
 {
+    const PID_PATH = '/run/fan-ctrl.pid';
+
     protected $name = 'daemon';
 
     protected $description = 'Run the monitoring and control the fans';
@@ -23,6 +25,13 @@ class Daemon extends AbstractCommand
             $this->warn('This programm has to run as root');
             return 1;
         }
+
+        if (file_exists(self::PID_PATH) && posix_getpgid((int)file_get_contents(self::PID_PATH))) {
+            $this->warn('Fan Control is already running');
+            return 1;
+        }
+        file_put_contents(self::PID_PATH, posix_getpid());
+
         $fanConfig = $this->app->fanConfig;
         try {
             $fanConfig->readConfig();
diff --git a/composer.json b/composer.json
index d98f179..7ccc20c 100644
--- a/composer.json
+++ b/composer.json
@@ -7,9 +7,10 @@
         "ulrichsg/getopt-php":   "^4.0",
         "filp/whoops": "^2.14",
         "monolog/monolog": "^1.9",
+        "mossadal/math-parser": "^1.3",
         "ext-pcntl": "*",
-        "php": ">=7.4",
-        "mossadal/math-parser": "^1.3"
+        "ext-posix": "*",
+        "php": ">=7.4"
     },
     "autoload":          {
         "psr-4": {
diff --git a/example-config.php b/example-config.php
new file mode 100644
index 0000000..8c521ef
--- /dev/null
+++ b/example-config.php
@@ -0,0 +1,114 @@
+<?php
+
+return [
+    'fans' => [
+        [
+            'name' => 'exhaust fans',
+            'type' => 'App\\Model\\Fan\\HwmonFan',
+            'options' => [
+                'hwmon' => 'nct6798',
+                'fan' => 'fan1',
+                'pwm' => 'pwm1',
+                'start' => 70,
+                'max' => 200,
+            ],
+        ],
+        [
+            'name' => 'cpu fan',
+            'type' => 'App\\Model\\Fan\\HwmonFan',
+            'options' => [
+                'hwmon' => 'nct6798',
+                'fan' => 'fan2',
+                'pwm' => 'pwm2',
+                'start' => 40,
+                'max' => 255,
+            ],
+        ],
+        [
+            'name' => 'radiator fans',
+            'type' => 'App\\Model\\Fan\\HwmonFan',
+            'options' => [
+                'hwmon' => 'nct6798',
+                'fan' => 'fan3',
+                'pwm' => 'pwm3',
+                'start' => 60,
+                'max' => 255,
+            ],
+        ],
+        [
+            'name' => 'bottom fans',
+            'type' => 'App\\Model\\Fan\\HwmonFan',
+            'options' => [
+                'hwmon' => 'nct6798',
+                'fan' => 'fan4',
+                'pwm' => 'pwm4',
+                'start' => 65,
+                'max' => 160,
+            ],
+        ],
+        [
+            'name' => 'water pump',
+            'type' => 'App\\Model\\Fan\\HwmonFan',
+            'options' => [
+                'hwmon' => 'nct6798',
+                'fan' => 'fan6',
+                'pwm' => 'pwm6',
+                'start' => 20,
+                'max' => 128,
+            ],
+        ],
+    ],
+    'sensors' => [
+        [
+            'name' => 'cpu',
+            'type' => 'App\\Model\\Sensor\\HwmonSensor',
+            'options' => [
+                'hwmon' => 'k10temp',
+                'temp' => 'temp1',
+            ],
+        ],
+        [
+            'name' => 'gpu',
+            'type' => 'App\\Model\\Sensor\\CommandSensor',
+            'options' => [
+                'command' => 'nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader',
+                'conversion' => NULL,
+            ],
+        ],
+    ],
+    'rules' => [
+        [
+            'fans' => [
+                'exhaust fans',
+                'cpu fan',
+                'bottom fans',
+            ],
+            'sensor' => 'cpu',
+            'type' => 'App\\Model\\Rule\\CurveRule',
+            'options' => [
+                'alwaysOn' => true,
+                'points' => [
+                    0 => 55,
+                    60 => 80,
+                    100 => 90,
+                ],
+            ],
+        ],
+        [
+            'fans' => [
+                'radiator fans',
+                'water pump',
+            ],
+            'sensor' => 'gpu',
+            'type' => 'App\\Model\\Rule\\CurveRule',
+            'options' => [
+                'alwaysOn' => true,
+                'points' => [
+                    0 => 35,
+                    60 => 55,
+                    100 => 90,
+                ],
+            ],
+        ],
+    ],
+];
diff --git a/install.sh b/install.sh
new file mode 100755
index 0000000..63b5f2b
--- /dev/null
+++ b/install.sh
@@ -0,0 +1,42 @@
+#!/usr/bin/env bash
+set -e
+
+installTarget=/usr/share/fan-ctrl-php
+
+# check for php and composer
+if ! which php > /dev/null 2>&1 || ! which composer > /dev/null 2>&1; then
+  echo 'php and composer have to be installed in order to run fan-ctrl-php'
+  exit 1;
+fi
+
+# copy app, bin, composer.json, composer.lock to /usr/share/fan-ctrl-php
+mkdir -p $installTarget
+cp -r app bin composer.json composer.lock $installTarget
+
+# install composer dependencies
+pushd $installTarget
+composer install -no
+popd
+
+# create symlink to bin/fan-ctrl in /usr/bin/
+ln -s $installTarget/bin/fan-ctrl /usr/bin/fan-ctrl
+
+# install fan-ctrl.service in /etc/systemd/system/
+{
+ echo '[Unit]'
+ echo 'Description=A fan control service written in php'
+ echo ''
+ echo '[Service]'
+ echo 'User=root'
+ echo 'WorkingDirectory='$installTarget
+ echo 'ExecStart=/usr/bin/fan-ctrl daemon'
+ echo 'Restart=always'
+ echo ''
+ echo '[Install]'
+ echo 'WantedBy=multi-user.target'
+} > /etc/systemd/system/fan-ctrl.service
+
+echo Fan Control PHP successfully installed. Enable the service with \$ systemctl enable fan-ctrl.service
+if ! test -f /etc/fan-ctrl.php; then
+  echo To create a configuration file and test the fans use \$ fan-ctrl config
+fi
-- 
GitLab