<?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. node爬虫(转)

    我们先来看看今天的目标: mmjpg.com的美腿频道下的图片 一.实现步骤 使用superagent库来获取页面分析页面结构,使用cheerio 获取有效信息保存图片到本地开撸不断优化 这儿我们用到 ...

  2. Thinkphp框架下(同服务器下)不同二级域名之间session互通共享设置

    在Thinkphp框架下根目录打开index.php 在头部加入如下代码即可: //入口文件 define('DOMAIN','abc.com');//abc.com换成自己的跟域名 //以下两行是为 ...

  3. Razon模板

    转:http://www.cnblogs.com/huangxincheng/p/3644313.html 随笔- 119 文章- 0 评论- 2039 抛弃NVelocity,来玩玩Razor   ...

  4. Django---应用如何创建

    创建好的项目之后,需要创建各个应用模块: 创建方法: 就可以看到:index 应用

  5. TFS二次开发02——连接TFS

    在上一篇<TFS二次开发01——TeamProjectsPicher>介绍了  TeamProjectsPicher 对象,使用该对象可以很简单的实现连接TFS. 但是如果我们要实现自定义 ...

  6. ubuntu16.04安装 lrzsz

    编译安装 root 账号登陆后,依次执行以下命令: tar zxvf lrzsz-.tar.gz cd lrzsz- ./configure make make install 上面安装过程默认把ls ...

  7. HDU 1222 - Wolf and Rabbit & HDU 1108 - [最大公约数&最小公倍数]

    水题,只是想借此记一下gcd函数的模板 #include<cstdio> int gcd(int m,int n){return n?gcd(n,m%n):m;} int main() { ...

  8. spring boot+mybatis 系列

    https://blog.csdn.net/linxingliang/article/details/52324937 https://www.cnblogs.com/a8457013/p/90749 ...

  9. python数据结构之堆栈

    本篇我以堆栈的数据类型和操作方法两个方面总结学习笔记 堆栈(Stack) 一种后进先出(LIFO)的线性数据结构,对堆栈的插入和删除操作都只能在栈顶(top)进行. 堆栈可以通过数组和链表轻松实现 一 ...

  10. iOS 项目架构tabbarController 嵌套 navbarController

    简单思路: 进入APP,首先加载 splashVC,加载完成之后,在viewDidAppear里跳转到loginVC,(这里一定要在viewDidLoad方法里新建loginVC跳转). 登陆成功之后 ...