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

suiyi的更多相关文章

  1. css.day01.eg

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 自己写的一个tomcat发布脚本

    闲来无事,就自己写一个shell脚本,方便自己以后在服务器上部署tomcat下的项目.我本地用maven打包,然后每次都要人工去切换一堆堆目录,有点繁琐,所以我写了下面的shell脚本. #! /bi ...

  3. 生成html报告并整合自动发动邮件功能

    from HTMLTestRunner import HTMLTestRunnerfrom email.mime.text import MIMETextfrom email.header impor ...

  4. LINUX下 一句话添加用户并设置ROOT权限

    来源:linux一条命令添加用户并设置密码 linux一条命令添加一个root级别账户并设置密码 LINUX提权,除非是拿的EXP反弹CMD,才会有回显,这样添加管理员方便了. 通常是在SHELL,菜 ...

  5. node使用JsonWebToken 生成token,完成用户登录、登录检测

    最近在用node做后台的登录,检测登录功能.在本地使用session可以成功,但是放服务器后发现session失效了,每次请求session都会变化,着了很久原因.原来,自己项目是前后端分离的,前端调 ...

  6. lambda匿名函数,sorted排序,filter()筛选,map()映射

    一丶匿名函数 语法: 函数名 = lambda参数:返回值 # 普通的正常的函数 def func(n): return n * n ret = func(9) print(ret) # 匿名函数 a ...

  7. Bootstrap--常用及实例合集

    栅格系统 1. row必须放到container和container-fluid里面        2. 你的内容应当放置于“列(column)”内,并且,只有“列(column)”可以作为行(row ...

  8. MySQL06-- mysql索引

    目录 一.索引介绍 1.什么是索引 2.索引类型介绍 3.索引管理 5.索引操作 6.前缀索引 7.联合索引 8.创建索引总结: 一.索引介绍 1.什么是索引 1)索引就好比一本书的目录,它能让你更快 ...

随机推荐

  1. VUE单独页面body css设置

    使用created周期用JS来处理BODY的样式 export default { beforeCreate: function () { document.getElementsByTagName( ...

  2. vue生成路由实例

    一.vue路由https://router.vuejs.org/zh-cn/1.bower下载vue-routervue的里的链接 <router-link to="/home&quo ...

  3. Google浏览器清除缓存快捷键

    1.CTRL+SHIFT+DEL:直接进入“清除浏览数据”页面,包括清除浏览历史记录.清空缓存.删除Cookie等. 2.chrome浏览器F12中 ctrl+p 可以定位文件

  4. 新浪的动态策略灰度发布系统:ABTestingGateway

    原文链接:http://www.open-open.com/lib/view/open1439889185239.html ABTesingGateway 是一个可以动态设置分流策略的灰度发布系统,工 ...

  5. Xcode - 打开工程,提示No Scheme解决

    错误提示,如图: 解决思路:

  6. POJ 3280 - Cheapest Palindrome - [区间DP]

    题目链接:http://poj.org/problem?id=3280 Time Limit: 2000MS Memory Limit: 65536K Description Keeping trac ...

  7. CCCC L2 部落 L3社交集群

    https://www.patest.cn/contests/gplt/L2-024 题解:部落是并查集模板题. 社交集群用并查集暴力有23分 坑:写了半天,发现自己并查集没怎么学明白,现在才搞懂: ...

  8. free 释放内存

    http://www.cplusplus.com/reference/cstdlib/free/ free void free (void* ptr); Deallocate memory block ...

  9. initrd和initramfs的区别

      Linux内核在初始化之后会执行init进程,而init进程会挂载我们的根文件系统,但由于init程序也是在根文件系统上的,所以这就有了悖论.Linux采用两步走的方法来解决这个问题.Linux2 ...

  10. iOS-多语言版本开发(二)(转载)

    题记   iOS 多语言版本的开发(一) 中我们完成了让应用跟随系统语言进行切换,而用户自己却不能切换的功能,也基本上算是实现了多语言版本:可是,对于某些应用来说,实现跟随系统语言切换的同时, 也想要 ...