policyCmd.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. function send_cmd($cmd,$type = 0, $timeout = 30, $ms_timeout = 200) {
  12. $ip = '123.57.20.89';
  13. $port = '6800';
  14. $client = @stream_socket_client("tcp://{$ip}:{$port}", $errno, $errstr, 30);
  15. if ($client) {
  16. fwrite($client, $cmd);
  17. stream_set_timeout($client, $timeout, $ms_timeout);
  18. $result = fread($client, 8196);
  19. fclose($client);
  20. return $result;
  21. } else {
  22. sleep(1);
  23. send_cmd($cmd,$type, $timeout, $ms_timeout);
  24. }
  25. }
  26. $begin = date('H:i',time());
  27. while (1) {
  28. $end = date('H:i',time());
  29. if ($end == $begin) {
  30. sleep(1);
  31. continue;
  32. }
  33. $begin = $end;
  34. $db = new Db($config);
  35. $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';
  36. $list = $db->query($sql);
  37. foreach ($list as $value) {
  38. $nowTime = time() - 8 * 3600 + $value['value'] * 3600;
  39. $nowDate = date('Y-m-d',$nowTime);
  40. $nowH = date('H:i',$nowTime);
  41. $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';
  42. $policyList = $db->query($sql);
  43. foreach ($policyList as $policy) {
  44. $index = 1;
  45. while ($index <= 10) {
  46. if (empty($policy['time'.$index])) {
  47. break;
  48. }
  49. if ($policy['time'.$index] == $nowH) {
  50. $data = array();
  51. $data['cmdtype'] = 0;
  52. $data['relateid'] = $policy['lampid'];
  53. $data['statuscmd'] = $policy['value'.$index];
  54. $data['updatetime'] = date("Y-m-d H:i:s");
  55. $sql = 'SELECT id from batch_switch_cmd where relateid = '.$policy['lampid'].' AND cmdtype = 0';
  56. $cmdList = $db->query($sql);
  57. if (empty($cmdList) || empty($cmdList[0]['id'])) {
  58. $cmdid = $db->table('batch_switch_cmd')->insert($data);
  59. }else{
  60. $db->table('batch_switch_cmd')->where('id='.$cmdList[0]['id'])->update($data);
  61. $cmdid = $cmdList[0]['id'];
  62. }
  63. $cmd = '{"cmd_type":"batch_switch_cmd","cmd_id":'.$cmdid.',"broadcast":0}';
  64. send_cmd($cmd,1,0);
  65. break;
  66. }
  67. $index ++;
  68. }
  69. }
  70. }
  71. $db->close();
  72. }