crontab调用PHP功能的频率 - 寻找替代选项

发布于 2025-02-09 11:19:33 字数 2039 浏览 1 评论 0原文

我将自己的“智能家庭”作为学习项目编程。 我的代码运行正常。我正在寻求帮助以提高效率和代码和/或Crontab + PHP代码的设置。

我正在用WiFi能量计监视洗衣机的能源消耗。目标是通知我一旦洗衣机完成,所以我不会忘记清除它。

在我的pi上,我有一个 crontab so:

*/20 7-22 * * * /usr/bin/php '/home/holger/html/plugs/washer.php' 

它以 php代码 (我简化为更好的可读性):

[...]/我称之为函数,当然可以, but this function does the main task

function loop($maschine, $watt_init, $trashhold){
    
      $max = 75;//max loops to avoid endless runs
      $i = 1;//start counter
      $tackt = 3;//tact time to check energy consumption
      //$trashhold = 4;//ab x Watt kein standby
      if ($watt_init < 1 ) {//Machine is switched off if energy consumption < 1 Watt
        die;//quit
      }
      elseif ($watt_init < 2 ) {//Machine is switched off or in standby if energy consumption < 1 Watt
        die;//quit
      }
      else {//Any thing else: Machine is running   
        while ($i < $max) {//loop as long as max loops are not reached
          $watt_current = json_combine(IPplug5);//getting current energy consumption from WIFI energy meter via JSON
          sleep(60*$tackt);//sleep and continue every 60s x tact time 
          $i++;//increase counter +1
          //compare actual consumption with defined trashhold
          if ($watt_current[0] >= $trashhold) {//continue while energy consumption bigger then trashhold
            continue;//repeat loop
          }
          elseif ($watt_current[0] < $trashhold) {//stop if energy consumption lower then trashhold
            break;//stop loop
          }
        }
        echo "Program done. please clear. Runtime: " . $i*$tackt. "Min."
           //[...] message me to my telegram bot
    }
  }

The code is running fine and I'm getting the output I need.

我的问题是:有没有更好的方法来做到这一点?

目前,我害怕用太多的开放式PHP会话超载PI,因此,我每20分钟只启动代码循环睡觉3分钟。但是,为了提高准确性,我喜欢更频繁地运行Cronjob,也让同时循环仅入睡30秒。 我的要求是坚持我的PI和PHP代码,并且不使用任何可用的软件,例如Home Assisant.io,因为它与我的学习方法相矛盾。 任何想法或见解欢迎。

I'm programming my own "smart home" as a learning project.
My code is running fine. I'm looking for help to improve the efficiency and of the code and/or the setup of crontab + php code.

I'm monitoring the energy consumption of my washing machine with a WIFI energy meter. Target is to notify me once the washing machine is completed so I don't forget to clear it.

on my Pi I have a crontab like so:

*/20 7-22 * * * /usr/bin/php '/home/holger/html/plugs/washer.php' 

which runs following php code (I simplified for better readability):

[...]/I call the function, of course, but this function does the main task

function loop($maschine, $watt_init, $trashhold){
    
      $max = 75;//max loops to avoid endless runs
      $i = 1;//start counter
      $tackt = 3;//tact time to check energy consumption
      //$trashhold = 4;//ab x Watt kein standby
      if ($watt_init < 1 ) {//Machine is switched off if energy consumption < 1 Watt
        die;//quit
      }
      elseif ($watt_init < 2 ) {//Machine is switched off or in standby if energy consumption < 1 Watt
        die;//quit
      }
      else {//Any thing else: Machine is running   
        while ($i < $max) {//loop as long as max loops are not reached
          $watt_current = json_combine(IPplug5);//getting current energy consumption from WIFI energy meter via JSON
          sleep(60*$tackt);//sleep and continue every 60s x tact time 
          $i++;//increase counter +1
          //compare actual consumption with defined trashhold
          if ($watt_current[0] >= $trashhold) {//continue while energy consumption bigger then trashhold
            continue;//repeat loop
          }
          elseif ($watt_current[0] < $trashhold) {//stop if energy consumption lower then trashhold
            break;//stop loop
          }
        }
        echo "Program done. please clear. Runtime: " . $i*$tackt. "Min."
           //[...] message me to my telegram bot
    }
  }

The code is running fine and I'm getting the output I need.

My question is: Is there a better way to do that?

Currently I'm afraid to overload my Pi with too many open php sessions, therefore I'm starting the code only every 20min and also let the while loop sleep for 3 Min. But for improved accuracy I like to run the cronjob more often and also let the while loop sleep only for 30s.
My requirements are to stick to my PI and php code and not to use any available software like Home Assisant.io as it contradicts with my learning approach.
Any ideas or insights welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热情消退 2025-02-16 11:19:33

理想情况下,这不是处理和测量功耗的最佳方法。此外,您可以创建日志并将其存储在数据库中。

虽然,对于您当前的问题,这是一种替代解决方案。

  1. 设置每秒运行的cron。

     函数get_powerConsumption($机器,$ watt_init,$ threshold)
      {
        如果($ watt_init&lt; 2){
         出口();
      }
     $ time = date(“ ymd h:i”);
     $ filename = $机器。 '_ power_consumption.log'; //在这里期待一些机器标识名称。否则忽略前缀
     $ watt_current = json_combine(ipplug5);
     if($ watt_current [0]&gt; = $ threshold){
         $ data =数组(
             $时间,
             $ watt_current [0]
         );
         file_put_contents($ filename,json_encode($ data)。“ \ n”,file_append);
     } elseif($ watt_current [0]&lt; $ threshold){
         $ data =数组(
             $时间,
             “停止”
         );
         file_put_contents($ filename,json_encode($ data)。“ \ n”,file_append);
     }
     

    }

  2. 创建另一个cron,以查找文件记录的停止事件。如果找到过程,则基于记录的数据(例如时间和消耗)计算。您可以根据自己的需求(例如每秒或一分钟或间隔之后)设置此cron进行运行。

另外,一旦找到了旧日志,请处理代码以删除旧日志。

Ideally it's a not the best approach to handling and measuring power consumption. It would be best if you created an API that accepts events like on/off or threshold hold limit extends from your IP devices.  Further you can create logs and store them in databases.

Although, for your current problem here is one alternate solution.

  1. Set your cron that runs every second.

     function get_powerConsumption($machine, $watt_init, $threshold)
      {
        if ($watt_init < 2) {
         exit();
      }
     $time = date("Y-m-d H:i");
     $filename = $machine . '_power_consumption.log'; // expecting some machine identification name here. otherwise ignore prefix
     $watt_current = json_combine(IPplug5);
     if ($watt_current[0] >= $threshold) {
         $data = array(
             $time,
             $watt_current[0]
         );
         file_put_contents($filename, json_encode($data) . "\n", FILE_APPEND);
     } elseif ($watt_current[0] < $threshold) {
         $data = array(
             $time,
             'stopped'
         );
         file_put_contents($filename, json_encode($data) . "\n", FILE_APPEND);
     }
    

    }

  2. Create another cron to look up for stopped events logged in the file. if found process the calculation based on logged data like time and consumption. You can set this cron to run based on your need like every second or minute or after some interval.

Also, handle code to delete old logs, once stopped events found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文