policyCmd.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. // 策略指令下发脚本
  3. require_once './DB.php';
  4. require_once './config.php';
  5. date_default_timezone_set('Asia/Shanghai');
  6. $config = [
  7. 'hostname' => 'rm-wz98r5cn33zq4ou980o.mysql.rds.aliyuncs.com',
  8. 'username' => 'lampmanager',
  9. 'password' => 'lampmanager@2019',
  10. 'dbname' => 'lampmanager',
  11. ];
  12. function send_cmd($cmd,$type = 0, $timeout = 30, $ms_timeout = 200) {
  13. $ip = '123.57.20.89';
  14. $port = '6800';
  15. $client = @stream_socket_client("tcp://{$ip}:{$port}", $errno, $errstr, 30);
  16. if ($client) {
  17. fwrite($client, $cmd);
  18. stream_set_timeout($client, $timeout, $ms_timeout);
  19. $result = fread($client, 8196);
  20. fclose($client);
  21. return $result;
  22. } else {
  23. sleep(1);
  24. send_cmd($cmd,$type, $timeout, $ms_timeout);
  25. }
  26. }
  27. $begin = date('H:i',time());
  28. while (1) {
  29. $end = date('H:i',time());
  30. if ($end == $begin) {
  31. sleep(1);
  32. continue;
  33. }
  34. $begin = $end;
  35. $db = new Db($config);
  36. $sql = 'SELECT P.id,T.value FROM project AS P left join lampinfo as L on L.projectid = P.id left join timezone AS T on P.timezone = T.id where L.policyid != 0';
  37. $list = $db->query($sql);
  38. foreach ($list as $value) {
  39. $nowTime = time() - 8 * 3600 + $value['value'] * 3600;
  40. $nowDate = date('Y-m-d',$nowTime);
  41. $nowH = date('H:i',$nowTime);
  42. $sql = 'SELECT L.id as lampid,PI.* FROM lampinfo AS L join (SELECT * from policy_cmd where starttime <= "'.$nowDate.'") as PI on L.policyid = PI.policyid where PI.policyid != 0';
  43. $policyList = $db->query($sql);
  44. foreach ($policyList as $policy) {
  45. $index = 1;
  46. while ($index <= 10) {
  47. if (empty($policy['time'.$index])) {
  48. break;
  49. }
  50. if ($policy['time'.$index] == $nowH) {
  51. $data = array();
  52. $data['cmdtype'] = 0;
  53. $data['relateid'] = $policy['lampid'];
  54. $data['statuscmd'] = $policy['value'.$index];
  55. $data['updatetime'] = date("Y-m-d H:i:s");
  56. $sql = 'SELECT id from batch_switch_cmd where relateid = '.$policy['lampid'].' AND cmdtype = 0';
  57. $cmdList = $db->query($sql);
  58. if (empty($cmdList) || empty($cmdList[0]['id'])) {
  59. $cmdid = $db->table('batch_switch_cmd')->insert($data);
  60. }else{
  61. $db->table('batch_switch_cmd')->where('id='.$cmdList[0]['id'])->update($data);
  62. $cmdid = $cmdList[0]['id'];
  63. }
  64. $cmd = '{"cmd_type":"batch_switch_cmd","cmd_id":'.$cmdid.',"broadcast":0}';
  65. send_cmd($cmd,1,0);
  66. break;
  67. }
  68. $index ++;
  69. }
  70. }
  71. }
  72. $db->close();
  73. }