0
0

TemperatureDevice.php 2.9 KB

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