123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace App\Servers;
- use App\Models\DeviceTrailModel;
- class HeartBeatServer extends BaseServer
- {
- use DeviceTrailModel;
- public function main($body)
- {
- $body = $this->decodeBody($body);
- self::log($body, 'HeartBeatServer');
- //记录数据
- $isUse = 1;
- if ($body['water_quality'] < 10) {
- $isUse = 0;
- }
- if ($body['water_level_warning']) {
- $isUse = 1;
- }
- if ($body['temperature'] > 65) {
- $isUse = 0;
- }
- self::updateData($_SESSION['box_no'], [
- 'temperature' => $body['temperature'],
- 'water_level_warning' => $body['water_level_warning'],
- 'water_quality' => $body['water_quality'],
- 'is_use' => $isUse,
- 'box_status' => $body['box_status']
- ], ['last_update_time' => date("Y-m-d H::i::s")], 'heart');
- $re = self::isDataChange($_SESSION['box_no'], [
- 'box_status' => $body['box_status']
- ], 'box_status');
- if ($re) {
- self::log('状态改变了');
- $status = $body['box_status'];
- $box_no = $_SESSION['box_no'];
- $result = $this->url_get("http://api.qingji.site.ximengnaikang.com/api/status-change?box_no={$box_no}&status={$status}");
- self::log($result, 'status-change-api');
- }
- return false;
- }
- private function url_get($url)
- {
- $ch = curl_init();
- $timeout = 5;
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- $file_contents = curl_exec($ch);
- curl_close($ch);
- return json_decode($file_contents, true);
- }
- private function decodeBody($body)
- {
- $i = 0;
- $imei = self::decodeBoxNo(self::stitching($body, $i, 15, false));
- $i += 15;
- $temperature = self::stitching($body, $i, 1);
- $i += 1;
- $water_level_warning = (int)self::stitching($body, $i, 1);
- $i += 1;
- $water_quality = self::stitching($body, $i, 1);
- $i += 1;
- $box_status = self::stitching($body, $i, 1);
- switch ($box_status) {
- case "00":
- $box_status = 0;
- break;
- case '01':
- $box_status = 1;
- break;
- case '02':
- $box_status = 2;
- break;
- case '03':
- $box_status = 3;
- break;
- case '04':
- $box_status = 4;
- break;
- case '05':
- $box_status = 5;
- break;
- case '06':
- $box_status = 6;
- break;
- case '07':
- $box_status = 7;
- break;
- case '08':
- $box_status = 8;
- break;
- case "aa":
- $box_status = 99;
- break;
- }
- $i += 1;
- return [
- 'imei' => $imei,
- 'temperature' => (int)$temperature,
- 'water_level_warning' => (int)$water_level_warning,
- 'water_quality' => (int)$water_quality,
- 'box_status' => $box_status,
- ];
- }
- }
|