<?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. slf4j + log4j 是如何初始化的

    SLF4J的全称是 Simple Logging Facade for Java(简单java日志门面) SLF4J自己不提供具体的日志功能实现,只是提供了一个统一的日志门面,在这个统一的门面之下,用 ...

  2. Cracking the Coding Interview(String and array)

    1.1实现一个算法判断一个字符串是否存在重复字符.如果不能利用另外的数据结构又该如何实现? My solution: /** *利用类似一个hash table的计数 *然后检查这个hash tabl ...

  3. redmine生产环境搭建

    记录信息: 搭建基础应用及mysql 配置svn 配置自动重启 配置API开放 配置邮箱发送 开启数据库ip访问权限 确认用户组管理:配置用户同步 配置部门同步

  4. 【JSP】JSP的介绍和基本原理

    JSP简介 JSP的核心实质是Servlet技术.JSP是后来添加的基于Servlet的一种扩展技术.但二者在使用上有不同的方向. 由于Servlet实质是一个Java类,因此非常适合用来处理业务逻辑 ...

  5. vim上下左右键输出A B

    (转)vim上下左右键不能用 把下面这段话存到~/.vimrc就可以了. " An example for a vimrc file. " " Maintainer: B ...

  6. vue之计算属性和侦听器

    一.计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div> {{ message.split('').rev ...

  7. vue之用法

    一.安装 对于新手来说,强烈建议大家使用<script>引入 二. 引入vue.js文件 我们能发现,引入vue.js文件之后,Vue被注册为一个全局的变量,它是一个构造函数. 三.使用V ...

  8. 洛谷P2216 理想的正方形

    题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入输出格式 输入格式: 第一行为3个整数,分别表示a,b,n的值 第二行至 ...

  9. 机器学习TensorFlow安装经过摘要

    第一步:我在Github上面下载了TensorFlow项目源码 第二步:在tensorflow-master/tensorflow/docs_src/install里面找到了install_mac.m ...

  10. pdb学习笔记

    参考资料:https://segmentfault.com/a/1190000006628456 下一行(不进入函数内部):n(ext) 单步(进入函数内部):s(tep) 打印:p 动态添加断点:1 ...