闸门小程序接口

This commit is contained in:
2025-07-07 02:12:33 +08:00
commit cdc59b4598
67 changed files with 1860 additions and 0 deletions
+327
View File
@@ -0,0 +1,327 @@
<?php
session_start();
if( !defined('IN') ) die('bad request!');
include_once( AROOT . 'controller' . DS . 'app.class.php');
header("Content-Type: text/html; charset=utf-8");
class aeratorController extends appController
{
public function __construct(){
parent::__construct();
}
//发送错误结果集
public function send_error($number,$msg)
{
$obj = array();
$obj['code'] = intval($number);
$obj['result'] = $msg;
if( g('API_EMBED_MODE') == 1 ){
return json_encode($obj);
}else{
header("Content-Type: application/json");
die(json_encode($obj));
}
}
//发送结果集
public function send_result($data)
{
$obj = array();
$obj['code'] = 0;
$obj['result'] = 'success';
$obj['data'] = $data;
if( g('API_EMBED_MODE') == 1 ){
return json_encode($obj);
}else{
header("Content-Type: application/json");
die(json_encode($obj));
}
}
// 获取项目中所有的曝气机
public function getAllAeratorList()
{
$token = $_GET['token'];
// 获取项目ID
$basic_info = $this -> getLoginUserInfoByToken($token);
$project_id = $basic_info['project_id'];
$sql = prepare("select aerator.id,terminal.name as terminal_name,terminal.address,aerator.device_name,aerator.name,if(aerator.status=1,'开启','关闭') as status,aerator.count,aerator.created_at from aerator left join terminal on terminal.id = aerator.terminal_id where aerator.project_id = ?i",array($project_id));
$data= get_data($sql);
for ($i=0; $i < count($data); $i++) {
$data[$i]['title']['text'] = $data[$i]['name'];
$data[$i]['title']['color'] = '#000';
$data[$i]['title']['size'] = 30;
$data[$i]['tag']['text'] = $data[$i]['status']."(".$data[$i]['count'].")";
$data[$i]['tag']['color'] = "#ed3f14";
$data[$i]['tag']['size'] = 26;
}
return self::send_result($data);
}
// 获取选中曝气机基本信息
public function getSelectAeratorTerminalInfo()
{
$aerator_id = $_GET['aerator_id'];
$data = $this -> getAeratorBasicInfoById($aerator_id);
return self::send_result($data);
}
// 获取选中曝气机的基本信息
public function getSingleAeratorInfo()
{
$aerator_id = $_GET['aerator_id'];
$aerator_array = $this -> getAeratorBasicInfoById($aerator_id);
return self::send_result($aerator_array);
}
// 获取token值
public function getAeratorToken($aerator_id)
{
$data = $this -> getAeratorBasicInfoById($aerator_id);
// 判断如果token为空 或者是 时间超过了1小时 重新生成token
$duration = floor((time() - strtotime($data['created_at']))/10);
$current_time = date("Y-m-d H:i:s",time());
if(!isset($data['token']) or $data['token'] == '' or $duration > 10){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $data['link'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'username='.$data['account'].'&pwd='.$data['password'],
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'Cookie: PHPSESSID=f53kjsucrafml6n92tolgum1o2'
),
));
$response = curl_exec($curl);
curl_close($curl);
// $token = json_decode($response)->token;
$token = substr($response,21,32);
$sql_update = prepare("update aerator set token=?s,created_at=?s where id=?i",array($token,$current_time,$data['id']));
run_sql($sql_update);
return $token;
}else{
return $data['token'];
}
}
// 获取曝气机属性
public function getAeratorAttribute($aerator_id)
{
// 先更新token
$this -> getAeratorToken($aerator_id);
$data = $this -> getAeratorBasicInfoById($aerator_id);
$current_time = date("Y-m-d H:i:s",time());
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'hk.tchjjc.com/api/thing/properties',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'token='.$data['token'].'&pk='.$data['pk'].'&deviceName='.$data['device_name'],
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'Cookie: PHPSESSID=f53kjsucrafml6n92tolgum1o2'
),
));
$response = curl_exec($curl);
curl_close($curl);
$data = json_decode($response)->data;
$csq_value = $data[0]->value;
$wkmd_balue = $data[1]->value;
$imei_balue = $data[2]->value;
$imsi_balue = $data[5]->value;
$lcen_balue = $data[6]->value;
$rly1_balue = $data[7]->value;
$pwrs_balue = $data[8]->value;
$hold_balue = $data[9]->value;
$iccid_balue = $data[10]->value;
$sql_update = prepare("update aerator set imei=?s,imsi=?s,iccid=?s,csq=?s,status=?i where id = ?i",array($imei_balue,$imsi_balue,$iccid_balue,$csq_value,$rly1_balue,$aerator_id));
run_sql($sql_update);
}
// 获取设备的属性面板
public function getAeratorAttributePanel()
{
// 获取token值
$token = $this->getAeratorToken();
// 默认曝气机参数
$pk = 'a1uZDeulr2x';
$deviceName = 'H4GSAL0132202256465';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'hk.tchjjc.com/api/thing/tsl',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'token=FC33DA76E6179976298FDF91491295DC&pk=a1uZDeulr2x&deviceName=H4GSAL0132202256465',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'Cookie: PHPSESSID=f53kjsucrafml6n92tolgum1o2'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
// 控制开关
public function getAeratorRlyOn(){
$aerator_id = $_GET['aerator_id'];
// 推送最新得设备属性
$this->getAeratorAttribute($aerator_id);
$data = $this->getAeratorBasicInfoById($aerator_id);
if($data['status'] == 0){
$value = 1;
}else{
$value = 0;
}
$identifier = 'rly1';
$new_count = $data['count'] + 1;
$current_time = date("Y-m-d H:i:s",time());
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'hk.tchjjc.com/api/thing/properties/set',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'token='.$data['token'].'&pk='.$data['pk'].'&deviceName='.$data['device_name'].'&identifier='.$identifier.'&value='.$value,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'Cookie: PHPSESSID=f53kjsucrafml6n92tolgum1o2'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
// 更新当前设备状态
$sql_update = prepare("update aerator set status=?i,count=?i,created_at=?s where id = ?i",array($value,$new_count,$current_time,$aerator_id));
run_sql($sql_update);
}
// 获取当前设备状态
public function getAeratorDeviceStatus()
{
$token = $this->getAeratorToken();
// 默认曝气机参数
$pk = 'a1uZDeulr2x';
$deviceName = 'H4GSAL0132202256465';
$device_id = 1;
$current_time = date("Y-m-d H:i:s",time());
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'hk.tchjjc.com/api/thing/status',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'token='.$token.'&pk='.$pk.'&deviceName='.$deviceName,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'Cookie: PHPSESSID=f53kjsucrafml6n92tolgum1o2'
),
));
$response = curl_exec($curl);
curl_close($curl);
$status = json_decode($response)->data->status;
$sql_update = prepare("update device set status = ?i,created_at = ?s where id = ?i",array($status,$current_time,$device_id));
run_sql($sql_update);
}
// 通过session中token获取登录人员基本信息
function getLoginUserInfoByToken($token)
{
$sql = prepare("select project_id,name,mobile from user where token = ?s limit 1",array($token));
$data = get_line($sql);
return $data;
}
// 通过aerator_id获取曝气机基本信息
function getAeratorBasicInfoById($aerator_id)
{
$sql = prepare("select aerator.id,project.name as project_name,terminal.name as terminal_name,terminal.address,aerator.link,aerator.account,aerator.password,aerator.token,aerator.pk,aerator.device_name,aerator.name,aerator.imei,aerator.imsi,aerator.iccid,aerator.csq,aerator.status,aerator.count,aerator.created_at from aerator left join project on project.id = aerator.project_id left join terminal on terminal.id = aerator.terminal_id where aerator.id = ?i limit 1",array($aerator_id));
$data= get_line($sql);
return $data;
}
}
?>
+14
View File
@@ -0,0 +1,14 @@
<?php
if( !defined('IN') ) die('bad request');
include_once( CROOT . 'controller' . DS . 'core.class.php' );
class appController extends coreController
{
function __construct()
{
// 载入默认的
parent::__construct();
}
// login check or something
}
+107
View File
@@ -0,0 +1,107 @@
<?php
session_start();
if( !defined('IN') ) die('bad request!');
include_once( AROOT . 'controller' . DS . 'app.class.php');
header("Content-Type: text/html; charset=utf-8");
class indexController extends appController
{
public function __construct(){
parent::__construct();
}
//发送错误结果集
public function send_error($number,$msg)
{
$obj = array();
$obj['code'] = intval($number);
$obj['result'] = $msg;
if( g('API_EMBED_MODE') == 1 ){
return json_encode($obj);
}else{
header("Content-Type: application/json");
die(json_encode($obj));
}
}
//发送结果集
public function send_result($data)
{
$obj = array();
$obj['code'] = 0;
$obj['result'] = 'success';
$obj['data'] = $data;
if( g('API_EMBED_MODE') == 1 ){
return json_encode($obj);
}else{
header("Content-Type: application/json");
die(json_encode($obj));
}
}
/*
* 返回登录界面
*
*/
public function index()
{
render(null, 'web', 'login');
}
/*
* 返回首页
*
*/
public function returnHomePage()
{
render(null, 'web', 'index');
}
/*
* 页面浏览日志
*
*/
public function recordOperationLog()
{
$token = $_SESSION['token'];
// 通过token获取user_id
$sql = prepare("select id from opm_ds_user where token = ?s limit 1",array($token));
$user_id = get_var($sql);
return $user_id;
}
public function test()
{
$result['time'] = ['09-13','09-14','09-15','09-16','09-17','09-18','09-19','09-20','09-21','09-22'];
$result['res'][0] = array("name"=>'辐射异常报警',"data"=>[1,4,7,3,8,2,1,8,4,3]);
$result['res'][1] = array("name"=>'设备离线报警',"data"=>[4,7,9,5,7,9,0,5,7,8]);
return self::send_result($result);
}
public function test1()
{
$result = [31,21,45];
return self::send_result($result);
}
public function test2()
{
$result = [31,21,45];
return self::send_result($result);
}
}
?>
+77
View File
@@ -0,0 +1,77 @@
<?php
if( !defined('IN') ) die('bad request');
include_once( AROOT . 'controller'.DS.'app.class.php' );
class defaultController extends appController
{
function __construct()
{
parent::__construct();
}
function index()
{
$data['title'] = $data['top_title'] = '首页';
render( $data );
}
function ajax_test()
{
return ajax_echo('1234');
}
function rest()
{
$data = array( );
if( intval(v('o')) == 1 )
{
$data['code'] = 123;
$data['message'] = 'RPWT';
}
else
{
$data['code'] = 0 ;
$data['data'] = array( '2' , '4' , '6' , '8' );
}
render( $data , 'rest' );
}
function mobile()
{
$data['title'] = $data['top_title'] = 'JQMobi';
render( $data , 'mobile' );
}
function ajax_load()
{
return ajax_echo('Hello ' . date("Y-m-d H:i:s"));
}
function about()
{
return info_page( "ftqq.com 荣誉出品" , "About" );
}
function contact()
{
return info_page( "Sina Weibo - <a href='http://weibo.com/easy' target='_blank'>@Easy</a> | Twitter - @Easychen" , "Follow Me" );
}
function test()
{
$data['title'] = $data['top_title'] = '自动测试页';
$data['info'] = '根据访问来源自动切换Layout';
return render( $data );
}
function sql()
{
db();
echo $sql = prepare( "SELECT * FROM `user` WHERE `name` = ?s AND `uid` = ?i AND `level` = ?s LIMIT 1" , array( "Easy'" , '-1', '9.56' ) );
}
}
+80
View File
@@ -0,0 +1,80 @@
<?php
session_start();
if( !defined('IN') ) die('bad request!');
include_once( AROOT . 'controller' . DS . 'app.class.php');
header("Content-Type: text/html; charset=utf-8");
class loginController extends appController
{
public function __construct(){
parent::__construct();
}
//发送错误结果集
public function send_error($number,$msg)
{
$obj = array();
$obj['code'] = intval($number);
$obj['result'] = $msg;
if( g('API_EMBED_MODE') == 1 ){
return json_encode($obj);
}else{
header("Content-Type: application/json");
die(json_encode($obj));
}
}
//发送结果集
public function send_result($data)
{
$obj = array();
$obj['code'] = 0;
$obj['result'] = 'success';
$obj['data'] = $data;
if( g('API_EMBED_MODE') == 1 ){
return json_encode($obj);
}else{
header("Content-Type: application/json");
die(json_encode($obj));
}
}
/*
* 验证用户登录
*
*/
public function postCheckLogin()
{
$account = $_POST['account'];
$password = $_POST['password'];
$current_time = date("Y-m-d H:i:s",time());
$sql = prepare("select id,project_id,account,password,token,name,mobile,role_id from user where account = ?s and password = ?s limit 1",array($account,md5($password)));
$data = get_line($sql);
return self::send_result($data);
}
/*
* token的生成
* MD5
*/
public function createdToken($account,$password)
{
$current_time = date('Y-m-d H:i:s',time());
$token = md5($account.$password.$current_time);
return $token;
}
public function test()
{
$sql = "select * from user";
$data= get_data($sql);
var_dump($data);
}
}
?>
+569
View File
@@ -0,0 +1,569 @@
<?php
session_start();
if( !defined('IN') ) die('bad request!');
include_once( AROOT . 'controller' . DS . 'app.class.php');
header("Content-Type: text/html; charset=utf-8");
class overviewController extends appController
{
public function __construct(){
parent::__construct();
}
//发送错误结果集
public function send_error($number,$msg)
{
$obj = array();
$obj['code'] = intval($number);
$obj['result'] = $msg;
if( g('API_EMBED_MODE') == 1 ){
return json_encode($obj);
}else{
header("Content-Type: application/json");
die(json_encode($obj));
}
}
//发送结果集
public function send_result($data)
{
$obj = array();
$obj['code'] = 0;
$obj['result'] = 'success';
$obj['data'] = $data;
if( g('API_EMBED_MODE') == 1 ){
return json_encode($obj);
}else{
header("Content-Type: application/json");
die(json_encode($obj));
}
}
// 获取项目场站总数、闸门调控次数以及水泵调控次数
public function getProjectBasicInfo()
{
$token = $_GET['token'];
// 获取登录人员基本信息
$user_basic = $this -> getLoginUserInfoByToken($token);
// 获取当前项目中所有控制柜
$sql = prepare("select id from device where project_id = ?i and status = 1",array($user_basic['project_id']));
$data_device_array = get_data($sql);
// for ($i=0; $i < count($data_device_array); $i++) {
// $device_id = $data_device_array[$i]['id'];
// $sql_1 = prepare("select (main_sewage_pump_count+standby_sewage_pump_count+main_rain_pump_count) as pump_count,(gate_up_count+gate_down_count) as gate_count from data where device_id = ?i order by id desc limit 1",array($device_id));
// $total_pump_count += get_line($sql_1)['pump_count'];
// $total_gate_count += get_line($sql_1)['gate_count'];
// }
$sql_pump_count = prepare("select count(*) from event where event like ?s and device_id in (select id from device where project_id = ?i and status = 1)",array('%泵%',$user_basic['project_id']));
$total_pump_count = get_var($sql_pump_count) == null ? 0 : get_var($sql_pump_count);
$sql_gate_count = prepare("select count(*) from event where event like ?s and device_id in (select id from device where project_id = ?i and status = 1)",array('%闸门%',$user_basic['project_id']));
$total_gate_count = get_var($sql_gate_count) == null ? 0 : get_var($sql_gate_count);
// 获取场站数量
$sql_1 = prepare("select count(*) from device where project_id = ?i and status = 1",array($user_basic['project_id']));
$terminal_count = get_var($sql_1);
$result = array('total_pump_count'=>$total_pump_count,'total_gate_count'=>$total_gate_count,'terminal_count'=>$terminal_count);
return self::send_result($result);
}
// 获取总览内容中闸门总次数以及本月闸门次数
public function getOverviewGateActionInfo()
{
$token = $_GET['token'];
// 获取登录人员基本信息
$user_basic = $this -> getLoginUserInfoByToken($token);
// 获取当前项目中所有控制柜
$sql = prepare("select id from device where project_id = ?i",array($user_basic['project_id']));
$data_device_array = get_data($sql);
// $month_start_table = "data_".date("Ym",time())."01";
// for ($i=0; $i < count($data_device_array); $i++) {
// $device_id = $data_device_array[$i]['id'];
// // 获取总的闸门次数
// $sql_1 = prepare("select (gate_up_count+gate_down_count) as gate_count from data where device_id = ?i order by id desc limit 1",array($device_id));
// $total_gate_count += get_var($sql_1);
// // 获取本月之前的闸门次数
// $sql_2 = prepare("select (gate_up_count+gate_down_count) as gate_count from ".$month_start_table." where device_id = ?i order by id desc limit 1",array($device_id));
// $last_gate_count += get_var($sql_2);
// }
$start_time = date("Y-m",time())."-01 00:00:00";
$end_time = date("Y-m-d H:i:s",time());
// 获取总的闸门次数
$sql_gate_count = prepare("select count(*) from event where event like ?s and device_id in (select id from device where project_id = ?i and status = 1)",array('%闸门%',$user_basic['project_id']));
$total_gate_count = get_var($sql_gate_count) == null ? 0 : get_var($sql_gate_count);
// 获取本月之前的闸门次数
$sql_current_gate_count = prepare("select count(*) from event where event like ?s and device_id in (select id from device where project_id = ?i and status = 1) and event_time >= ?s and event_time <= ?s",array('%闸门%',$user_basic['project_id'],$start_time,$end_time));
$current_gate_count = get_var($sql_current_gate_count) == null ? 0 : get_var($sql_current_gate_count);
$result = array("total_gate_count"=>$total_gate_count,'current_gate_count'=>$current_gate_count);
return self::send_result($result);
}
// 获取总览内容中水泵总次数以及本月水泵次数
public function getOverviewPumpActionInfo()
{
$token = $_GET['token'];
// 获取登录人员基本信息
$user_basic = $this -> getLoginUserInfoByToken($token);
// 获取当前项目中所有控制柜
$sql = prepare("select id from device where project_id = ?i",array($user_basic['project_id']));
$data_device_array = get_data($sql);
// $month_start_table = "data_".date("Ym",time())."01";
// for ($i=0; $i < count($data_device_array); $i++) {
// $device_id = $data_device_array[$i]['id'];
// // 获取总的水泵次数
// $sql_1 = prepare("select (main_sewage_pump_count+standby_sewage_pump_count+main_rain_pump_count) as pump_count from data where device_id = ?i order by id desc limit 1",array($device_id));
// $total_pump_count += get_var($sql_1);
// // 获取本月之前的水泵次数
// $sql_2 = prepare("select (main_sewage_pump_count+standby_sewage_pump_count+main_rain_pump_count) as pump_count from ".$month_start_table." where device_id = ?i order by id desc limit 1",array($device_id));
// $last_pump_count += get_var($sql_2);
// }
// // 本月闸门次数
// $current_pump_count = $total_pump_count - $last_pump_count;
$start_time = date("Y-m",time())."-01 00:00:00";
$end_time = date("Y-m-d H:i:s",time());
// 获取总的闸门次数
$sql_pump_count = prepare("select count(*) from event where event like ?s and device_id in (select id from device where project_id = ?i and status = 1)",array('%泵%',$user_basic['project_id']));
$total_pump_count = get_var($sql_pump_count) == null ? 0 : get_var($sql_pump_count);
// 获取本月之前的闸门次数
$sql_current_pump_count = prepare("select count(*) from event where event like ?s and device_id in (select id from device where project_id = ?i and status = 1) and event_time >= ?s and event_time <= ?s",array('%泵%',$user_basic['project_id'],$start_time,$end_time));
$current_pump_count = get_var($sql_current_pump_count) == null ? 0 : get_var($sql_current_pump_count);
$result = array("total_pump_count"=>$total_pump_count,'current_pump_count'=>$current_pump_count);
return self::send_result($result);
}
// 闸门调控统计
public function getOverviewGateLine()
{
$token = $_GET['token'];
$type = $_GET['type'] == '' ? $type = 'month' : $type = $_GET['type'];
// 获取登录人员基本信息
$user_basic = $this -> getLoginUserInfoByToken($token);
// 获取当前项目中所有控制柜
$sql = prepare("select id from device where project_id = ?i",array($user_basic['project_id']));
$data_device_array = get_data($sql);
$current_time = date("Y-m-d",time());
switch ($type) {
case 'month':
// 时间处理
$satrt_query_time = date("Y-m-d",time()-14*24*60*60).' 00:00:00';
$end_query_time = date("Y-m-d H:i:s",time());
$month_time_array = $this -> getDateFromtime($satrt_query_time,$end_query_time);
for ($i=0; $i < count($month_time_array); $i++) {
// 每天开始时间 和 结束时间
// echo $month_time_array[$i];
// if($month_time_array[$i] == $current_time)
// {
// $every_day_start_time_table = "data_".date("Ymd",strtotime($month_time_array[$i])-24*60*60);
// $every_day_end_time_table = "data";
// }
// else
// {
// $every_day_start_time_table = "data_".date("Ymd",strtotime($month_time_array[$i])-24*60*60);
// $every_day_end_time_table = "data_".date("Ymd",strtotime($month_time_array[$i]));
// }
$start_time = $month_time_array[$i]." 00:00:00";
$end_time = $month_time_array[$i]." 23:59:59";
$gate_count_1 = $gate_count_2 = 0;
// 计算每天闸门调控的次数
for ($j=0; $j < count($data_device_array); $j++) {
$device_id = $data_device_array[$j]['id'];
$sql_current_gate_count = prepare("select count(*) from event where event like ?s and device_id =?i and event_time >= ?s and event_time <= ?s",array('%闸门%',$device_id,$start_time,$end_time));
$gate_count_1 += get_var($sql_current_gate_count) == null ? 0 : get_var($sql_current_gate_count);
// $sql_1 = prepare("select (gate_up_count+gate_down_count) as gate_count from ".$every_day_start_time_table." where device_id = ?i order by id desc limit 1",array($device_id));
// $gate_count_1 += get_var($sql_1) == null ? 0 : get_var($sql_1);
// // 获取本月之前的闸门次数
// $sql_2 = prepare("select (gate_up_count+gate_down_count) as gate_count from ".$every_day_end_time_table." where device_id = ?i order by id desc limit 1",array($device_id));
}
$result['count'][$i] = $gate_count_1;
$result['time'][$i] = date("d",strtotime($month_time_array[$i]));
}
break;
case 'year':
// 时间处理
$year_time_array = $this -> getYearMonthTime();
for ($i=0; $i < count($year_time_array); $i++) {
// 每月开始时间 和 结束时间
$every_month_start_time = $year_time_array[$i].'-01 00:00:00';
$every_month_end_time = $year_time_array[$i].'-31 23:59:59';
// echo $every_month_start_time.'-------'.$every_month_end_time."======================\r\n";
$gate_count_1 = $gate_count_2 = 0;
// 计算每天闸门调控的次数
for ($j=0; $j < count($data_device_array); $j++) {
$device_id = $data_device_array[$j]['id'];
$sql_current_gate_count = prepare("select count(*) from event where event like ?s and device_id =?i and event_time >= ?s and event_time <= ?s",array('%闸门%',$device_id,$every_month_start_time,$every_month_end_time));
$gate_count_1 += get_var($sql_current_gate_count) == null ? 0 : get_var($sql_current_gate_count);
// $sql_1 = prepare("select (gate_up_count+gate_down_count) as gate_count from data where device_id = ?i and `current_time`<?s order by id desc limit 1",array($device_id,$every_month_start_time));
// $gate_count_1 += get_var($sql_1) == null ? 0 : get_var($sql_1);
// // 获取本月之前的闸门次数
// $sql_2 = prepare("select (gate_up_count+gate_down_count) as gate_count from data where device_id = ?i and `current_time`<?s order by id desc limit 1",array($device_id,$every_month_end_time));
// $gate_count_2 += get_var($sql_2) == null ? 0 : get_var($sql_2);
}
$result['count'][$i] = $gate_count_1;
$result['time'][$i] = date("m-d",strtotime($year_time_array[$i]));
}
break;
}
self::send_result($result);
}
// 水泵调控统计
public function getOverviewPumpLine()
{
$token = $_GET['token'];
$type = $_GET['type'] == '' ? $type = 'month' : $type = $_GET['type'];
// 获取登录人员基本信息
$user_basic = $this -> getLoginUserInfoByToken($token);
// 获取当前项目中所有控制柜
$sql = prepare("select id from device where project_id = ?i",array($user_basic['project_id']));
$data_device_array = get_data($sql);
$current_time = date("Y-m-d",time());
switch ($type) {
case 'month':
// 时间处理
$satrt_query_time = date("Y-m-d",time()-14*24*60*60).' 00:00:00';
$end_query_time = date("Y-m-d H:i:s",time());
$month_time_array = $this -> getDateFromtime($satrt_query_time,$end_query_time);
for ($i=0; $i < count($month_time_array); $i++) {
// 每天开始时间 和 结束时间
// echo $month_time_array[$i];
// if($month_time_array[$i] == $current_time)
// {
// $every_day_start_time_table = "data_".date("Ymd",strtotime($month_time_array[$i])-24*60*60);
// $every_day_end_time_table = "data";
// }
// else
// {
// $every_day_start_time_table = "data_".date("Ymd",strtotime($month_time_array[$i])-24*60*60);
// $every_day_end_time_table = "data_".date("Ymd",strtotime($month_time_array[$i]));
// }
$start_time = $month_time_array[$i]." 00:00:00";
$end_time = $month_time_array[$i]." 23:59:59";
$pump_count_1 = $pump_count_2 = 0;
// 计算每天闸门调控的次数
for ($j=0; $j < count($data_device_array); $j++) {
$device_id = $data_device_array[$j]['id'];
$sql_current_pump_count = prepare("select count(*) from event where event like ?s and device_id =?i and event_time >= ?s and event_time <= ?s",array('%泵%',$device_id,$start_time,$end_time));
$pump_count_1 += get_var($sql_current_pump_count) == null ? 0 : get_var($sql_current_pump_count);
// $sql_1 = prepare("select (main_sewage_pump_count+standby_sewage_pump_count+main_rain_pump_count) as pump_count from ".$every_day_start_time_table." where device_id = ?i order by id desc limit 1",array($device_id));
// $pump_count_1 += get_var($sql_1) == null ? 0 : get_var($sql_1);
// // 获取本月之前的闸门次数
// $sql_2 = prepare("select (main_sewage_pump_count+standby_sewage_pump_count+main_rain_pump_count) as pump_count from ".$every_day_end_time_table." where device_id = ?i order by id desc limit 1",array($device_id));
// $pump_count_2 += get_var($sql_2) == null ? 0 : get_var($sql_2);
}
$result['count'][$i] = $pump_count_1;
$result['time'][$i] = date("d",strtotime($month_time_array[$i]));
}
break;
case 'year':
// 时间处理
$year_time_array = $this -> getYearMonthTime();
for ($i=0; $i < count($year_time_array); $i++) {
// 每月开始时间 和 结束时间
$every_month_start_time = $year_time_array[$i].'-01 00:00:00';
$every_month_end_time = $year_time_array[$i].'-31 23:59:59';
// echo $every_month_start_time.'-------'.$every_month_end_time."======================\r\n";
$pump_count_1 = $pump_count_2 = 0;
// 计算每天闸门调控的次数
for ($j=0; $j < count($data_device_array); $j++) {
$device_id = $data_device_array[$j]['id'];
$sql_current_pump_count = prepare("select count(*) from event where event like ?s and device_id =?i and event_time >= ?s and event_time <= ?s",array('%泵%',$device_id,$every_month_start_time,$every_month_end_time));
$pump_count_1 += get_var($sql_current_pump_count) == null ? 0 : get_var($sql_current_pump_count);
// $sql_1 = prepare("select (main_sewage_pump_count+standby_sewage_pump_count+main_rain_pump_count) as pump_count from data where device_id = ?i and `current_time`<?s order by id desc limit 1",array($device_id,$every_month_start_time));
// $pump_count_1 += get_var($sql_1) == null ? 0 : get_var($sql_1);
// // 获取本月之前的闸门次数
// $sql_2 = prepare("select (main_sewage_pump_count+standby_sewage_pump_count+main_rain_pump_count) as pump_count from data where device_id = ?i and `current_time`<?s order by id desc limit 1",array($device_id,$every_month_end_time));
// $pump_count_2 += get_var($sql_2) == null ? 0 : get_var($sql_2);
}
$result['count'][$i] = $pump_count_1;
$result['time'][$i] = date("m-d",strtotime($year_time_array[$i]));
}
break;
}
self::send_result($result);
}
// 通过session中token获取登录人员基本信息
function getLoginUserInfoByToken($token)
{
$sql = prepare("select project_id,name,mobile from user where token = ?s limit 1",array($token));
$data = get_line($sql);
return $data;
}
// 通过token获取场站和设备信息
public function getTerminalDeviceInfoByToken()
{
$token = $_GET['token'];
// 获取project_id 根据type 判断是否显示全部设备
$sql = prepare("select project_id,type,device_str from user where token = ?s limit 1",array($token));
$project_id = get_line($sql)['project_id'];
$type = get_line($sql)['type'];
$device_str = get_line($sql)['device_str'];
if($type == 2)
{
// 项目内全部设备
$sql_1 = prepare("select device.id,device.name,terminal.address as addr,device.name as device_name,device.device_no,device.register_address,device.status from device left join terminal on terminal.id = device.terminal_id where device.project_id = ?i",array($project_id));
}
else
{
// 负责的设备
if(!empty($device_str))
{
// 判断是否包含 -
if(strpos($device_str,'-') !== false)
{
$device_id_array = explode('-', $device_str);
for ($i=0; $i < count($device_id_array); $i++)
{
$sql_str .= "device.id = ".$device_id_array[$i]." or ";
}
// 去掉最后的or
$new_sql_str = substr($sql_str, 0, strlen($sql_2)-3);
$final_sql_str = "(".$new_sql_str.")";
$sql_1 = prepare("select device.id,device.name,terminal.address as addr,device.name as device_name,device.device_no,device.register_address,device.status from device left join terminal on terminal.id = device.terminal_id where device.project_id = ?i and ".$final_sql_str,array($project_id));
}
else
{
$sql_1 = prepare("select device.id,device.name,terminal.address as addr,device.name as device_name,device.device_no,device.register_address,device.status from device left join terminal on terminal.id = device.terminal_id where device.project_id = ?i and device.id = ?i",array($project_id,$device_str));
}
}
}
$data = get_data($sql_1);
if(!empty($data))
{
for ($i=0; $i < count($data); $i++)
{
$result[$i]['id'] = $data[$i]['id'];
$result[$i]['name'] = $data[$i]['name'];
$result[$i]['addr'] = $data[$i]['addr'];
$result[$i]['status'] = $data[$i]['status'] == 1 ? '在线' : '安装中';
if($data[$i]['status'] == 1)
{
$sql_2 = prepare("select created_at from data where device_id = ?i order by created_at desc limit 1",array($data[$i]['id']));
$result[$i]['last_time'] = date("m-d H:i",strtotime(get_var($sql_2)));
}
else
{
$result[$i]['last_time'] = '01-01 08:00';
}
}
}
else
{
$result = 'false';
}
return self::send_result($result);
}
// 通过device_id获取场站和设备信息
public function getTerminalDeviceInfoBydeviceId()
{
$device_id = $_GET['device_id'];
// 通过project_id获取device信息
$sql_1 = prepare("select device.id,terminal.name,terminal.address as addr,device.name as device_name,device.device_no,device.register_address,device.status from device left join terminal on terminal.id = device.terminal_id where device.id = ?i limit 1",array($device_id));
$data = get_line($sql_1);
if(!empty($data))
{
$result['id'] = $data['id'];
$result['name'] = $data['name'];
$result['addr'] = $data['addr'];
$result['status'] = $data['status'] == 1 ? '在线' : '安装中';
// 查询参数
$sql_2 = prepare("select ph_data,elec_conductivity_tds_data,level_data,ammonia_data,rainsnow,rainfall_data,gate_up,gate_down,main_sewage_pump,standby_sewage_pump,main_rain_pump,created_at from data where device_id = ?i order by created_at desc limit 1",array($data['id']));
$data_2= get_line($sql_2);
$result['ph_data'] = number_format($data_2['ph_data'],2);
$result['elec_conductivity_tds_data'] = number_format($data_2['elec_conductivity_tds_data'],2);
$result['level_data'] = number_format($data_2['level_data'],2);
$result['ammonia_data'] = number_format($data_2['ammonia_data'],2);
$result['rainsnow'] = number_format($data_2['rainsnow'],2);
$result['rainfall_data'] = number_format($data_2['rainfall_data'],2);
$result['created_at'] = $data_2['created_at'];
if($data_2['main_sewage_pump'] == 1){
$result['main_sewage_pump'] = '启动';
}else if($data_2['main_sewage_pump'] == 2){
$result['main_sewage_pump'] = '停止';
}else{
$result['main_sewage_pump'] = '报警';
}
if($data_2['standby_sewage_pump'] == 1){
$result['standby_sewage_pump'] = '启动';
}else if($data_2['standby_sewage_pump'] == 2){
$result['standby_sewage_pump'] = '停止';
}else{
$result['standby_sewage_pump'] = '报警';
}
if($data_2['main_rain_pump'] == 1){
$result['main_rain_pump'] = '启动';
}else if($data_2['main_rain_pump'] == 2){
$result['main_rain_pump'] = '停止';
}else{
$result['main_rain_pump'] = '报警';
}
if($data_2['gate_up'] == 1)
{
$result['gate_status'] = '上升';
}
else
{
if($data_2['gate_down'] == 1)
{
$result['gate_status'] = '下降';
}
else
{
$result['gate_status'] = '停止';
}
}
}
return self::send_result($result);
}
public function getDeviceFdid()
{
$device_id = $_GET['device_id'];
// 通过project_id获取device信息
$sql = prepare("select f_did from device where id = ?i limit 1",array($device_id));
$data = get_var($sql);
return self::send_result($data);
}
public function getDeviceName()
{
$device_id = $_GET['device_id'];
// 通过project_id获取device信息
$sql = prepare("select name from device where id = ?i limit 1",array($device_id));
$data = get_var($sql);
return self::send_result($data);
}
// 获取两个指定日期之间的每一天
function getDateFromtime($starttime,$endtime)
{
$dt_start = strtotime($starttime);
$dt_end = strtotime($endtime);
while ($dt_start <= $dt_end){
$array[] = date('Y-m-d',$dt_start);
$dt_start = strtotime('+1 day',$dt_start);
}
return $array;
}
function getYearMonthTime()
{
// 获取本月是几月
$m = date("m",time());
for ($i=1; $i < ($m+1); $i++) {
$month = date("Y",time())."-".$i;
$array[] = date("Y-m",strtotime($month));
}
return $array;
}
}
?>