TemperatureDevice.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace tools;
  3. class TemperatureDevice {
  4. protected $produceUrl = 'http://yun.eefield.com/coop/api/';
  5. protected $userName = '18610012796';
  6. protected $pwd = '123456';
  7. protected $login = false;
  8. /**
  9. *获取单个设备数据
  10. *
  11. * @author wst
  12. * @date 2021/6/8 18:03
  13. * @deviceId 设备id 多个用英文逗号隔开
  14. */
  15. public function getDevice($deviceId){
  16. $token = $this->getToken();
  17. try{
  18. if (empty($token)) {
  19. throw new \Exception('token获取失败');
  20. }else{
  21. $data = curl_post($this->produceUrl . 'device/intimeData.htm', ['accessToken' => $token
  22. ,"snaddrs"=>$deviceId]);
  23. $data = json_decode($data, true);
  24. if ($data['code'] == 0) {
  25. $r = [
  26. 'success' => true,
  27. 'msg' => '操作成功',
  28. 'list' => $data['data']
  29. ];
  30. }else {
  31. throw new \Exception($data['msg']);
  32. }
  33. }
  34. return $r;
  35. }catch (\Exception $e){
  36. $r = [
  37. 'success'=>false,
  38. 'msg'=>$e->getMessage(),
  39. ];
  40. return $r;
  41. }
  42. }
  43. /**
  44. * 获取所有设备的基本信息
  45. *
  46. * @author wst
  47. * @date 2021/6/8 17:50
  48. */
  49. public function getDeviceList() {
  50. $token = $this->getToken();
  51. try{
  52. if (empty($token)) {
  53. throw new \Exception('token获取失败');
  54. }
  55. else {
  56. $data = curl_post($this->produceUrl . 'device/list.htm', ['accessToken' => $token]);
  57. $data = json_decode($data, true);
  58. if ($data['code'] == 0) {
  59. $r = [
  60. 'success' => true,
  61. 'msg' => '操作成功',
  62. 'list' => $data['data']
  63. ];
  64. }
  65. else {
  66. throw new \Exception($data['msg']);
  67. }
  68. }
  69. return $r;
  70. }catch (\Exception $e){
  71. $r = [
  72. 'success'=>false,
  73. 'msg'=>$e->getMessage(),
  74. ];
  75. return $r;
  76. }
  77. }
  78. public function getToken() {
  79. $cookie = cache('temperature_token');
  80. //\think\facade\Cache::delete('aqg_cookie');
  81. if (empty($cookie) || $cookie == 'false') {
  82. $url = $this->produceUrl . 'token.htm';
  83. $data = curl_post($url, ['user' => $this->userName, 'psw' => md5($this->pwd)]);
  84. $data = json_decode($data, true);
  85. if ($data['code'] == 0) {
  86. $cookie = $data['accessToken'];
  87. cache('temperature_token', $data['accessToken'], 604800);
  88. }
  89. else {
  90. $cookie = false;
  91. }
  92. }
  93. return $cookie;
  94. }
  95. }