loraWan.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. // 智慧灯杆wifi设备收发指令脚本
  3. require_once __DIR__ . '/Autoloader.php';
  4. use Workerman\Worker;
  5. use Workerman\Connection\TcpConnection;
  6. use \Workerman\Lib\Timer;
  7. require_once './Lib/mysql/src/Connection.php';
  8. require_once './config.php';
  9. $tcp_worker = new Worker("tcp://0.0.0.0:8090");
  10. $tcp_worker->onWorkerStart = function($ws_worker)
  11. {
  12. global $cmdClientList;
  13. $cmdClientList = [];
  14. $url = 'https://device.api.ct10649.com:8743/iocm/app/sec/v1.1.0/login';
  15. $data = array(
  16. 'appId' => 'L_JzQ9KoR0py5X6fEGjO9p2PsWYa',
  17. 'secret' => 'pO9P7mecnU2KeucxkxuxiWbGBDsa',
  18. );
  19. $jsonData = json_encode($data);
  20. var_dump(curl($url,$data));
  21. // 指令返回结果处理
  22. // Timer::add(1, function()use($ws_worker){
  23. // global $globalData;
  24. // global $cmdClientList;
  25. // do
  26. // {
  27. // $old_value = $globalData->resList;
  28. // $new_value = [];
  29. // }
  30. // while(!$globalData->cas('resList', $old_value, $new_value));
  31. // $resList = $old_value;
  32. // if (!empty($resList)) {
  33. // foreach ($resList as $resData) {
  34. // $resData = json_decode($resData,true);
  35. // if (isset($cmdClientList[$resData['devId']])) {
  36. // $cmdClientList[$resData['devId']]->send(json_encode($resData));
  37. // unset($cmdClientList[$resData['devId']]);
  38. // }
  39. // }
  40. // }
  41. // });
  42. };
  43. // 下发指令
  44. $tcp_worker->onMessage = function($connection, $data)
  45. {
  46. // global $globalData;
  47. // global $cmdClientList;
  48. // $res = json_decode($data,true);
  49. // if (isset($res['devId'])) {
  50. // $cmdClientList[$res['devId']] = $connection;
  51. // add_cmd($res);
  52. // }else{
  53. // }
  54. };
  55. function curl($url, $params = false, $ispost = 0, $https = 0)
  56. {
  57. $httpInfo = array();
  58. $ch = curl_init();
  59. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  60. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  61. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  62. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
  63. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  64. // curl_setopt($ch, CURLOPT_CAINFO, 'C:\Users\wzh\Desktop\cert\client.crt');
  65. curl_setopt($ch, CURLOPT_SSLCERT, './cert/outgoing.CertwithKey.pem');
  66. curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'IoM@1234');
  67. curl_setopt($ch, CURLOPT_POST, true);
  68. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  69. curl_setopt($ch, CURLOPT_URL, $url);
  70. $response = curl_exec($ch);
  71. if ($response === FALSE) {
  72. echo "cURL Error: " . curl_error($ch);
  73. return false;
  74. }
  75. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  76. $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
  77. curl_close($ch);
  78. return $response;
  79. }
  80. Worker::runAll();