博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
suiyi
阅读量:5249 次
发布时间:2019-06-14

本文共 5888 字,大约阅读时间需要 19 分钟。

<?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.');
}
}
}

转载于:https://www.cnblogs.com/xiong63/p/6477149.html

你可能感兴趣的文章
JSP有哪些动作?作用分别是什么?
查看>>
Linux 进程资源用量监控和按用户设置进程限制
查看>>
IE浏览器整页截屏程序(二)
查看>>
IOS小技巧积累
查看>>
web页面设计稿的完美还原
查看>>
D3.js 之 d3-shap 简介(转)
查看>>
制作满天星空
查看>>
MyBatis日记(三):戏说MyBatis配置文件
查看>>
$_POST和$GLOBALS['HTTP_RAW_POST_DATA'] 的区别
查看>>
类和结构
查看>>
遍历文件夹下所有dll的类
查看>>
Centos 7.6 部署 Jumpserver 1.5.0
查看>>
CSS3选择器(二)之属性选择器
查看>>
[转]Doing more with Outlook filter and SQL DASL syntax
查看>>
[转]事务(ADO.NET)
查看>>
[转]JS跨域总结
查看>>
c# 后台生成微信小程序带参数二维码
查看>>
python所有的魔术方法
查看>>
操作系统基础详解
查看>>
知识体系整理查漏补缺
查看>>