12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace app\temperature\controller;
- use think\Exception;
- use think\Db;
- require_once $_SERVER['DOCUMENT_ROOT'].'/../vendor/wenkong/TemperatureDevice.php';
- class Push extends Base
- {
- public function initialize()
- {
- parent::initialize();
- }
- public function getDeviceData(){
- $device = Db::name('temperature_device')
- ->where([
- "del"=>0,
- "enable"=>1,
- ])->column('snaddr');
- $api = new \TemperatureDevice();
- try{
- if(empty($device)) return json(['msg'=>'操作成功']);
- $data = $api->getDevice(implode(',',$device));
- if(!$data['success']) throw new Exception($data['msg']);
- $list = $data['list']['dataList'];
- $insertData = [];
- foreach ($list as $k=>$v){
- $a = [
- 'snaddr'=>$v['snaddr'],
- 'humi'=>$v['humi'],
- 'temp'=>$v['temp'],
- 'humiStatus'=>$v['humiStatus'],
- 'tempStatus'=>$v['tempStatus'],
- 'abnormal'=>$v['abnormal'],
- 'create_time'=>date('Y-m-d H:i:s'),
- 'create_ymd'=>date('Ymd'),
- 'create_ymdh'=>date('YmdH'),
- 'create_ymdhi'=>date('YmdHi'),
- ];
- Db::name('temperature_device')
- ->where('snaddr',$v['snaddr'])
- ->update([
- "temp"=>$v['temp'],
- "humi"=>$v['humi'],
- "abnormal"=>$v['abnormal'],
- "caiji_time"=>time(),
- "update_time"=>date('Y-m-d H:i:s'),
- ]);
- $insertData[] = $a;
- }
- if(!empty($insertData)){
- $res = Db::name('temperature_device_data')
- ->insertAll($insertData);
- if(!$res) throw new Exception(Db::name('temperature_device_data')->getLastSql());
- }
- return json(['msg'=>'操作成功']);
- }catch (Exception $e){
- return json(['msg'=>$e->getMessage()]);
- }
- }
- /**
- * 清空超过三十天温度数据
- *
- * @author wst
- * @date 2021/6/15 13:37
- */
- public function clearData(){
- $time = time();
- $date = $time-(30*86400);
- $date = date('Y-m-d',$date).' 00:00:00';
- $res = Db::name('temperature_device_data')
- ->where('create_time','<',$date)
- ->delete();
- echo $res;
- }
- }
|