totalLightTime.php 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. // 计算灯控累计亮灯时间
  3. require_once './DB.php';
  4. date_default_timezone_set('Asia/Shanghai');
  5. $config = json_decode(file_get_contents('./db_config.php'),true);
  6. // $db = new Db($config);
  7. while (1) {
  8. $db = new Db($config);
  9. $lampLogList = $db->query('select id,lampid,daychargemincurrent,updatetime from battery_info_log where is_sum_light_time = 0 order by updatetime asc limit 10000');
  10. foreach ($lampLogList as $lampLog) {
  11. $oldLampLog = $db->query('select lampid,daychargemincurrent from battery_info_log where lampid = '.$lampLog['lampid'].' AND updatetime < "'.$lampLog['updatetime'].'" order by updatetime DESC limit 1');
  12. if (empty($oldLampLog) || empty($oldLampLog[0]['lampid'])) {
  13. $lightTime = $lampLog['daychargemincurrent'];
  14. }else{
  15. if ($oldLampLog[0]['daychargemincurrent'] < $lampLog['daychargemincurrent']) {
  16. $lightTime = $lampLog['daychargemincurrent'] - $oldLampLog[0]['daychargemincurrent'];
  17. }else{
  18. $lightTime = $lampLog['daychargemincurrent'];
  19. }
  20. }
  21. $db->execute('update lampinfo set totalLightTime = totalLightTime + '.$lightTime.' where id = '.$lampLog['lampid']);
  22. $db->execute('update battery_info_log set is_sum_light_time = 1 where id = '.$lampLog['id']);
  23. }
  24. $db->close();
  25. // var_dump('ok');die;
  26. sleep(30);
  27. }
  28. ?>