totalLightTime.php 1.4 KB

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