HeartBeatServer.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Servers;
  3. use App\Models\DeviceTrailModel;
  4. class HeartBeatServer extends BaseServer
  5. {
  6. use DeviceTrailModel;
  7. public function main($body)
  8. {
  9. $body = $this->decodeBody($body);
  10. self::log($body, 'HeartBeatServer');
  11. //记录数据
  12. $isUse = 1;
  13. if ($body['water_quality'] < 10) {
  14. $isUse = 0;
  15. }
  16. if ($body['water_level_warning']) {
  17. $isUse = 1;
  18. }
  19. if ($body['temperature'] > 65) {
  20. $isUse = 0;
  21. }
  22. self::updateData($_SESSION['box_no'], [
  23. 'temperature' => $body['temperature'],
  24. 'water_level_warning' => $body['water_level_warning'],
  25. 'water_quality' => $body['water_quality'],
  26. 'is_use' => $isUse,
  27. 'box_status' => $body['box_status']
  28. ], ['last_update_time' => date("Y-m-d H::i::s")], 'heart');
  29. $re = self::isDataChange($_SESSION['box_no'], [
  30. 'box_status' => $body['box_status']
  31. ], 'box_status');
  32. if ($re) {
  33. self::log('状态改变了');
  34. $status = $body['box_status'];
  35. $box_no = $_SESSION['box_no'];
  36. $result = $this->url_get("http://api.qingji.site.ximengnaikang.com/api/status-change?box_no={$box_no}&status={$status}");
  37. self::log($result, 'status-change-api');
  38. }
  39. return false;
  40. }
  41. private function url_get($url)
  42. {
  43. $ch = curl_init();
  44. $timeout = 5;
  45. curl_setopt($ch, CURLOPT_URL, $url);
  46. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  47. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  48. $file_contents = curl_exec($ch);
  49. curl_close($ch);
  50. return json_decode($file_contents, true);
  51. }
  52. private function decodeBody($body)
  53. {
  54. $i = 0;
  55. $imei = self::decodeBoxNo(self::stitching($body, $i, 15, false));
  56. $i += 15;
  57. $temperature = self::stitching($body, $i, 1);
  58. $i += 1;
  59. $water_level_warning = (int)self::stitching($body, $i, 1);
  60. $i += 1;
  61. $water_quality = self::stitching($body, $i, 1);
  62. $i += 1;
  63. $box_status = self::stitching($body, $i, 1);
  64. switch ($box_status) {
  65. case "00":
  66. $box_status = 0;
  67. break;
  68. case '01':
  69. $box_status = 1;
  70. break;
  71. case '02':
  72. $box_status = 2;
  73. break;
  74. case '03':
  75. $box_status = 3;
  76. break;
  77. case '04':
  78. $box_status = 4;
  79. break;
  80. case '05':
  81. $box_status = 5;
  82. break;
  83. case '06':
  84. $box_status = 6;
  85. break;
  86. case '07':
  87. $box_status = 7;
  88. break;
  89. case '08':
  90. $box_status = 8;
  91. break;
  92. case "aa":
  93. $box_status = 99;
  94. break;
  95. }
  96. $i += 1;
  97. return [
  98. 'imei' => $imei,
  99. 'temperature' => (int)$temperature,
  100. 'water_level_warning' => (int)$water_level_warning,
  101. 'water_quality' => (int)$water_quality,
  102. 'box_status' => $box_status,
  103. ];
  104. }
  105. }