Push.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace app\temperature\controller;
  3. use think\Exception;
  4. use think\Db;
  5. use tools\TemperatureDevice;
  6. class Push extends Base
  7. {
  8. public function initialize()
  9. {
  10. parent::initialize();
  11. }
  12. public function getDeviceData(){
  13. $device = Db::name('temperature_device')
  14. ->where([
  15. "del"=>0,
  16. "enable"=>1,
  17. ])->column('snaddr');
  18. $api = new TemperatureDevice();
  19. try{
  20. if(empty($device)) return json(['msg'=>'操作成功']);
  21. $data = $api->getDevice(implode(',',$device));
  22. if(!$data['success']) throw new Exception($data['msg']);
  23. $list = $data['list']['dataList'];
  24. $insertData = [];
  25. foreach ($list as $k=>$v){
  26. $a = [
  27. 'snaddr'=>$v['snaddr'],
  28. 'humi'=>$v['humi'],
  29. 'temp'=>$v['temp'],
  30. 'humiStatus'=>$v['humiStatus'],
  31. 'tempStatus'=>$v['tempStatus'],
  32. 'abnormal'=>$v['abnormal'],
  33. 'create_time'=>date('Y-m-d H:i:s'),
  34. 'create_ymd'=>date('Ymd'),
  35. 'create_ymdh'=>date('YmdH'),
  36. 'create_ymdhi'=>date('YmdHi'),
  37. ];
  38. Db::name('temperature_device')
  39. ->where('snaddr',$v['snaddr'])
  40. ->update([
  41. "temp"=>$v['temp'],
  42. "humi"=>$v['humi'],
  43. "abnormal"=>$v['abnormal'],
  44. "caiji_time"=>time(),
  45. "update_time"=>date('Y-m-d H:i:s'),
  46. ]);
  47. $insertData[] = $a;
  48. }
  49. if(!empty($insertData)){
  50. $res = Db::name('temperature_device_data')
  51. ->insertAll($insertData);
  52. if(!$res) throw new Exception(Db::name('temperature_device_data')->getLastSql());
  53. }
  54. return json(['msg'=>'操作成功']);
  55. }catch (Exception $e){
  56. return json(['msg'=>$e->getMessage()]);
  57. }
  58. }
  59. /**
  60. * 清空超过三十天温度数据
  61. *
  62. * @author wst
  63. * @date 2021/6/15 13:37
  64. */
  65. public function clearData(){
  66. $time = time();
  67. $date = $time-(30*86400);
  68. $date = date('Y-m-d',$date).' 00:00:00';
  69. $res = Db::name('temperature_device_data')
  70. ->where('create_time','<',$date)
  71. ->delete();
  72. echo $res;
  73. }
  74. }