get( $this->apiUrl('/api/open-api/open/soilMoisture/getCurrentDeviceData'), [ 'deviceId' => $deviceId, ] ); if (data_get($result, 'code') === 200) { return $result['data']; } throw new RuntimeException( sprintf("%s:%s", data_get($result, 'code', 500), data_get($result, 'msg', '出错啦!')) ); } /** * 获取最新的气象数据 */ public function getLatestMeteorologicalReport(string $deviceId) { $result = $this->get( $this->apiUrl('/api/open-api/open/weather/getCurrentDeviceData'), [ 'deviceId' => $deviceId, ] ); if (data_get($result, 'code') === 200) { return $result['data']; } throw new RuntimeException( sprintf("%s:%s", data_get($result, 'code', 500), data_get($result, 'msg', '出错啦!')) ); } public function get(string $url, array $query = []): array { return $this->request('GET', $url, [ 'query' => $query, ]); } /** * @param string $url * @param array $data * @return array */ public function post(string $url, array $data = []): array { return $this->request('POST', $url, [ 'json' => $data, ]); } /** * @param string $method * @param string $url * @param array $options * @return array * * @throws \Illuminate\Http\Client\RequestException * @throws \RuntimeException */ public function request(string $method, string $url, array $options = []): array { switch (strtoupper($method)) { case 'GET': $options['query'] = array_merge($options['query'], [ 'username' => $this->username, 'password' => $this->password, ]); break; case 'POST': $options['json'] = array_merge($options['json'], [ 'username' => $this->username, 'password' => $this->password, ]); break; } /** @var \Illuminate\Http\Client\Response */ $response = Http::withHeaders([ 'Content-Type' => 'application/json', ])->send($method, $url, $options); return $response->throw()->json(); } protected function apiUrl(string $path): string { return 'http://yun.bigdata5s.com'.Str::start($path, '/'); } protected function apiUrl2(string $path): string { return 'http://yun-api.bigdata5s.com'.Str::start($path, '/'); } }