<?php
namespace app\controllers;
use Yii;
use app\models\Device;use app\models\DeviceSearch;use app\models\DeviceData;use app\models\DeviceStatus;use app\models\DeviceStatusSearch;use yii\data\ActiveDataProvider;use yii\web\Controller;
use yii\web\NotFoundHttpException;use yii\filters\VerbFilter;/**
* DeviceController implements the CRUD actions for Device model. */class DeviceController extends Controller{ /** * @inheritdoc */ private $locationArray=['北京市'=>'北京市','天津市'=>'天津市','上海市'=>'上海市','重庆市'=>'重庆市','黑龙江省'=>'黑龙江省','吉林省'=>'吉林省','辽宁省'=>'辽宁省','河北省'=>'河北省','河南省'=>'河南省','山东省'=>'山东省','江苏省'=>'江苏省','山西省'=>'山西省','陕西省'=>'陕西省','甘肃省'=>'甘肃省','四川省'=>'四川省','青海省'=>'青海省','湖南省'=>'湖南省','湖北省'=>'湖北省','江西省'=>'江西省','安徽省'=>'安徽省','浙江省'=>'浙江省','福建省'=>'福建省','广东省'=>'广东省','广西省'=>'广西省','贵州省'=>'贵州省','云南省'=>'云南省','海南省'=>'海南省','内蒙古自治区'=>'内蒙古自治区','新疆维吾尔自治区'=>'新疆维吾尔自治区','宁夏回族自治区'=>'宁夏回族自治区','西藏自治区'=>'西藏自治区','广西壮族自治区'=>'广西壮族自治区',]; public function behaviors() { return [ 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['POST'], ], ], ]; }/**
* Lists all Device models. * @return mixed */ public function actionIndex() { $searchModel = new DeviceSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams);return $this->render('index', [
'searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'locationArray' => $this->locationArray, ]); }public function actionStatus(){
$searchModel = new DeviceStatusSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams);return $this->render('status', [
'searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'locationArray' => $this->locationArray, ]); }public function actionUpdatebattery($id){
$model= $this->findModel($id); if($model){ $model->updateBattery(); } }public function actionSendsms($id){
$model= $this->findModel($id); if($model){ $channelId ='4216275338380855882'; // '3499875588344819295'; $message = $model->phone;// 设置消息类型为 透传消息
$opts = array ( 'msg_type' => 0 , ); //Yii::$app->push->pushMsgToSingleDevice($channelId,$message,$opts); //$push= new \daidai118\baidupusher\PushSDK(\Yii::$app->params['default_apiKey'], \Yii::$app->params['default_secretkey']); //$push->pushMsgToSingleDevice($channelId,$message,$opts);require_once(dirname(__FILE__).'/../library/baidupush/sdk.php');
$sdk = new \PushSDK(); $rs = $sdk -> pushMsgToSingleDevice($channelId, $message, $opts); if($rs === false){ echo json_encode(['message'=>'错误:'. $sdk->getLastErrorCode(). $sdk->getLastErrorMsg()]); }else{ echo json_encode(['message'=>'已发送短信到'.$model->phone. "\n调试信息:". print_r($rs, true)]); } } }/**
* Displays a single Device model. * @param integer $id * @return mixed */ public function actionView() { if(isset($_REQUEST['id']) ){ $model= $this->findModel($_REQUEST['id']); }else{ $device_id= $_REQUEST['device_id']; $model= Device::find()->where(['device_id'=>$device_id])->one(); if( ! $model){ //create a new device $model= new Device(); $model->device_id= $device_id; $model->phone= '0'; $model->location= 'N'; $model->create_time= date('Y-m-d H:i:s'); $model->station_name= "N"; $model->update_time= time(); $model->save(); } } $dataProvider = new ActiveDataProvider([ 'query' => \app\models\DeviceLog::find()->where(['device_id'=>$model->device_id])->orderBy(['create_time'=>SORT_DESC]), 'pagination' => [ 'pageSize' => 20, ], ]);
//$device_data= DeviceData::find()->where(['device_id'=>$model->device_id])->orderBy(['id'=>SORT_DESC])->one();
$device_status= DeviceStatus::find()->where(['device_id'=>$model->device_id])->orderBy(['create_time'=>SORT_ASC])->one();if( isset($_REQUEST['action']) ){
if( $_REQUEST['action'] == 'action_export'){ header("Content-type:text/csv; charset=utf-8"); $filename="zijiu-{$model->device_id}-".date('Y-m-d').".csv"; header("Content-Disposition:attachment;filename=$filename"); header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); header('Expires:0'); header('Pragma:public');$query= $dataProvider->query; $labels=false; foreach ($query->each() as $p) { if( ! $labels ){ //output header $labels= $p->attributeLabels(); foreach($labels as $v){ echo $v.','; } echo "\n"; }
foreach($labels as $k=>$v){
echo $p[$k].','; } echo "\n"; } } }else{ return $this->render('view', [ 'model' => $model, 'dataProvider' => $dataProvider, //'device_data'=>$device_data, 'device_status'=>$device_status, 'locationArray' => $this->locationArray, // 'battery_bad'=>$battery_bad, // 'battery_bad_count'=>$battery_bad_count, ]); } }/**
* Creates a new Device model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Device();if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', [ 'model' => $model, 'locationArray' => $this->locationArray, ]); } }/**
* Updates an existing Device model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id);if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', [ 'model' => $model, 'locationArray' => $this->locationArray, ]); } }
/**
* Finds the Device model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Device the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Device::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }}