Files

567 lines
18 KiB
PHP

<?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()
{
if(isset($_SESSION['token'])){
header("Location:?c=home&a=dashboard");
}else{//什么都没有的跳转到登录表单
render(null, 'web', 'login');
}
}
/*
*
* 获取登录用户的权限
*/
public function getUserRole($token)
{
$sql = prepare("select role_id from user where token = ?s limit 0,1",array($token));
$role_id = get_var($sql);
return $role_id;
}
//
public function createdHomeMapPoint()
{
// 1.控制柜 2.曝气机 3.管网监测
$type = $_GET['type'];
switch ($type) {
case '1':
$token = $_SESSION['token'];
$role_id = $this->getUserRole($token);
if($role_id == 2) // 超级管理员账号
{
$sql = "select device.id,terminal.name as terminal,device.name,device.register_address as address,if(device.status=1,'开启','安装') as status,device.longitude as lng,device.latitude as lat,device.created_at as date,1 as type from device left join terminal on terminal.id = device.terminal_id where device.status = 1";
}
elseif($role_id == 1) // 项目账号
{
$sql = prepare("select device.id,terminal.name as terminal,device.name,device.register_address as address,if(device.status=1,'开启','安装') as status,device.longitude as lng,device.latitude as lat,device.created_at as date,1 as type from device left join terminal on terminal.id = device.terminal_id where device.status = 1 and device.id in (select id from device where project_id = (select project_id from user where token = ?s limit 1))",array($token));
}
$data = get_data($sql);
for ($i=0; $i < count($data); $i++)
{
$sql_1 = prepare("select elec_conductivity_tds_data,rainfall_data,level_data,ph_data,ammonia_data from data where device_id = ?i order by id desc limit 1",array($data[$i]['id']));
$result[$i]['elec_conductivity_tds_data'] = get_line($sql_1)['elec_conductivity_tds_data'] == null ? 0 : get_line($sql_1)['elec_conductivity_tds_data'];
$result[$i]['rainfall_data'] = get_line($sql_1)['rainfall_data'] == null ? 0 : get_line($sql_1)['rainfall_data'];
$result[$i]['level_data'] = get_line($sql_1)['level_data'] == null ? 0 : get_line($sql_1)['level_data'];
$result[$i]['ph_data'] = get_line($sql_1)['ph_data'] == null ? 0 : get_line($sql_1)['ph_data'];
$result[$i]['ammonia_data'] = get_line($sql_1)['ammonia_data'] == null ? 0 : get_line($sql_1)['ammonia_data'];
$result[$i]['terminal'] = $data[$i]['terminal'];
$result[$i]['name'] = $data[$i]['name'];
$result[$i]['address'] = $data[$i]['address'];
$result[$i]['status'] = $data[$i]['status'];
$result[$i]['lng'] = $data[$i]['lng'];
$result[$i]['lat'] = $data[$i]['lat'];
$result[$i]['date'] = $data[$i]['date'];
$result[$i]['type'] = $data[$i]['type'];
}
return self::send_result($result);
break;
case '2':
$result = array(
array("lng"=>'116.600388',"lat"=>'40.077332',"date"=>'2022-11-29',"name"=>'曝气机1',"terminal"=>'湟水湿地公园',"address"=>'青海省西宁市城西区海晏路绿地云香郡北',"type"=>"2","status"=>"开启"),
array("lng"=>'116.322159',"lat"=>'39.894672',"date"=>'2022-11-29',"name"=>'曝气机2',"terminal"=>'湟水湿地公园',"address"=>'青海省西宁市城西区海晏路绿地云香郡北',"type"=>"2","status"=>"开启")
);
break;
case '3':
$result = array(
array("lng"=>'116.378731',"lat"=>'39.865265',"date"=>'2022-11-29',"name"=>'管网监测A',"terminal"=>'管网监测A',"address"=>'青海省西宁市城西区海晏路绿地云香郡北',"type"=>"3","status"=>"开启"),
array("lng"=>'116.427208',"lat"=>'39.90414',"date"=>'2022-11-29',"name"=>'管网监测B',"terminal"=>'管网监测B',"address"=>'青海省西宁市城西区海晏路绿地云香郡北',"type"=>"3","status"=>"开启")
);
break;
}
return self::send_result($result);
}
// 获取地址的经纬度
public function getLgLa()
{
$sql = "select id,address from terminal where id > 2";
$data= get_data($sql);
for ($i=0; $i < count($data); $i++)
{
$id = $data[$i]['id'];
$address = $data[$i]['address']; // 要获取经纬度的地址
// 发送HTTP请求到Google Maps Geocoding API
$response = file_get_contents('http://api.map.baidu.com/geocoding/v3/?address=' . urlencode($address) . '&output=json&ak=WgwItPVbCpYhM8yjahWTMapWHo4tLkxQ');
// 解析API响应
$result = json_decode($response);
// 提取经纬度信息
if ($result->status === 0) {
$latitude = $result->result->location->lat;
$longitude = $result->result->location->lng;
$sql_1 = prepare("update terminal set longitude=?s,latitude=?s where id=?i",array($longitude,$latitude,$id));
echo $sql_1;
run_sql($sql_1);
} else {
echo 'Geocoding failed. Status: ' . $result->status;
}
}
}
// 将经纬度同步到device表
public function tbLgLaDevice()
{
$sql = "select id,terminal_id from device";
$data= get_data($sql);
for ($i=0; $i < count($data); $i++)
{
$id = $data[$i]['id'];
$terminal_id = $data[$i]['terminal_id'];
$sql_1 = prepare("select longitude,latitude from terminal where id = ?i",array($terminal_id));
$longitude = get_line($sql_1)['longitude'];
$latitude = get_line($sql_1)['latitude'];
$sql_2 = prepare("update device set longitude=?s,latitude=?s where id = ?i",array($longitude,$latitude,$id));
echo $sql_2;
echo "<br>";
run_sql($sql_2);
}
}
/**
* 获取两个指定日期之间的每一天
*/
public 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;
}
// 获取登录账号的项目
public function getIndexProjectList()
{
$token = $_SESSION['token'];
$role_id = $this->getUserRole($token);
if($role_id == 2) // 超级管理员账号
{
$sql = "select id,substr(name,1,4) as name from project";
}
elseif($role_id == 1) // 项目账号
{
$sql = prepare("select id,substr(name,1,4) as name from project where id = (select project_id from user where token = ?s limit 1)",array($token));
}
$result = get_data($sql);
return self::send_result($result);
}
// 获取项目总览
public function getIndexProjectInfo()
{
$token = $_SESSION['token'];
$role_id = $this->getUserRole($token);
if($role_id == 2) // 超级管理员账号
{
// 项目数量
$sql_project_count = "select count(*) from project";
// 控制柜数量
$sql_device_1_count = "select count(*) from device where type = 1";
// 曝气机数量
$sql_device_2_count = "select count(*) from device where type = 2";
// 管网数量
$sql_device_3_count = "select count(*) from device where type = 3";
}
elseif($role_id == 1) // 项目账号
{
// 项目数量
$sql_project_count = prepare("select count(*) from project where id = (select project_id from user where token = ?s limit 1)",array($token));
// 控制柜数量
$sql_device_1_count = prepare("select count(*) from device where type = 1 and project_id = (select project_id from user where token = ?s limit 1)",array($token));
// 曝气机数量
$sql_device_2_count = prepare("select count(*) from device where type = 2 and project_id = (select project_id from user where token = ?s limit 1)",array($token));
// 管网数量
$sql_device_3_count = prepare("select count(*) from device where type = 3 and project_id = (select project_id from user where token = ?s limit 1)",array($token));
}
$result = array(
"project_count"=>get_var($sql_project_count),
"device_1_count"=>get_var($sql_device_1_count),
"device_2_count"=>get_var($sql_device_2_count),
"device_3_count"=>get_var($sql_device_3_count),
);
return self::send_result($result);
}
// 获取运行橄榄
public function getIndexRunInfo()
{
$token = $_SESSION['token'];
$role_id = $this->getUserRole($token);
if($role_id == 2) // 超级管理员账号
{
// 获取天数
$sql_day_count = "select created_at from project order by created_at desc limit 1";
$first_time = get_var($sql_day_count);
$sql = "select id from device where type = 1 and status = 1";
}
elseif($role_id == 1) // 项目账号
{
// 获取天数
$sql_day_count = prepare("select created_at from project where id = (select project_id from user where token = ?s limit 1)",array($token));
$first_time = get_var($sql_day_count);
$sql = prepare("select id from device where type = 1 and status = 1 and project_id = (select project_id from user where token = ?s limit 1)",array($token));
}
$data= get_data($sql);
$action_all_total_count = 0;
if(!empty($data))
{
for ($i=0; $i < count($data); $i++)
{
$device_id = $data[$i]['id'];
$sql_action_all_count = prepare("select action_all_count from data where device_id = ?i order by id desc limit 1",array($device_id));
$action_all_total_count += get_var($sql_action_all_count);
}
}
// 平均调控频控/次 计算两个时间的天数差
$current_time = date("Y-m-d H:i:s",time());
$timestamp1 = time();
$timestamp2 = strtotime($first_time);
$diff = abs($timestamp1 - $timestamp2);
$days_count = floor($diff/(60*60*24));
$frequency = floor($action_all_total_count/$days_count);
$result = array("action_all_total_count"=>$action_all_total_count,"frequency"=>$frequency,"days_count"=>$days_count);
return self::send_result($result);
}
// 运行统计折线图
public function getIndexRunLine()
{
$token = $_SESSION['token'];
$role_id = $this->getUserRole($token);
$ten_day_before = date("Y-m-d",time()-9*24*60*60);
$current_time = date("Y-m-d",time());
// 获取时间的数组
$time_array = $this->getDateFromtime($ten_day_before,$current_time);
if($role_id == 2) // 超级管理员账号
{
for ($i=0; $i < count($time_array); $i++)
{
$today_count = 0;
$all_count = 0;
$all_count_before = 0;
if($time_array[$i]==date("Y-m-d",time()))
{
$table_name = 'data';
$table_name_before = "data_".date("Ymd",strtotime($time_array[$i])-24*60*60);
}
else
{
$table_name = "data_".date("Ymd",strtotime($time_array[$i]));
$table_name_before = "data_".date("Ymd",strtotime($time_array[$i])-24*60*60);
}
$sql_device_id_array = "select id from device where status = 1";
$device_id_array = get_data($sql_device_id_array);
for ($j=0; $j < count($device_id_array); $j++)
{
$sql_count = prepare("select action_all_count from ".$table_name." where device_id = ?i order by id desc limit 1",array($device_id_array[$j]['id']));
$all_count += get_var($sql_count);
$sql_count_before = prepare("select action_all_count from ".$table_name_before." where device_id = ?i order by id desc limit 1",array($device_id_array[$j]['id']));
$all_count_before += get_var($sql_count_before);
}
$today_count = $all_count-$all_count_before<0?$all_count_before-$all_count:$all_count-$all_count_before;
$result['time'][$i] = $time_array[$i];
$result['count'][$i] = $today_count;
}
}
elseif($role_id == 1) // 项目账号
{
for ($i=0; $i < count($time_array); $i++)
{
$today_count = 0;
if($time_array[$i]==date("Y-m-d",time()))
{
$table_name = 'data';
$table_name_before = "data_".date("Ymd",strtotime($time_array[$i])-24*60*60);
}
else
{
$table_name = "data_".date("Ymd",strtotime($time_array[$i]));
$table_name_before = "data_".date("Ymd",strtotime($time_array[$i])-24*60*60);
}
$sql_device_id_array = prepare("select id from device where status = 1 and project_id = (select project_id from user where token = ?s limit 1)",array($token));
$device_id_array = get_data($sql_device_id_array);
for ($j=0; $j < count($device_id_array); $j++)
{
$sql_count = prepare("select action_all_count from ".$table_name." where device_id = ?i order by id desc limit 1",array($device_id_array[$j]['id']));
$all_count += get_var($sql_count);
$sql_count_before = prepare("select action_all_count from ".$table_name_before." where device_id = ?i order by id desc limit 1",array($device_id_array[$j]['id']));
$all_count_before += get_var($sql_count_before);
}
$today_count = $all_count-$all_count_before<0?$all_count_before-$all_count:$all_count-$all_count_before;
$result['time'][$i] = $time_array[$i];
$result['count'][$i] = $today_count;
}
}
return self::send_result($result);
}
// 调控频率排行榜
public function getIndexRank()
{
$token = $_SESSION['token'];
$role_id = $this->getUserRole($token);
if($role_id == 2) // 超级管理员账号
{
$sql = "select action_all_count,device.terminal_id from data left join device on device.id = data.device_id where data. device_id !=0 and device.terminal_id is not null group by data.device_id order by data.action_all_count desc limit 5";
$data= get_data($sql);
}
elseif($role_id == 1) // 项目账号
{
$sql = prepare("select action_all_count,device.terminal_id from data left join device on device.id = data.device_id where data. device_id in (select id from device where project_id = (select project_id from user where token = ?s limit 1)) and data.device_id != 0 and device.terminal_id is not null group by data.device_id order by data.action_all_count desc limit 5",array($token));
$data= get_data($sql);
}
for ($i=0; $i < count($data); $i++)
{
$sql_1 = prepare("select name from terminal where id = ?i limit 1",array($data[$i]['terminal_id']));
$result[$i]['name'] = get_var($sql_1);
$result[$i]['count']= $data[$i]['action_all_count'];
}
return self::send_result($result);
}
// 预警统计
public function getIndexWarnInfo()
{
$result = array("warn_all_count"=>0,"warn_deal_count"=>0,"warn_no_deal_count"=>0);
return self::send_result($result);
}
// 事件统计
public function getIndexEventInfo()
{
$token = $_SESSION['token'];
$role_id = $this->getUserRole($token);
if($role_id == 2) // 超级管理员账号
{
$sql = "select count(*) as count,event from event group by event";
}
elseif($role_id == 1) // 项目账号
{
$sql = prepare("select count(*) as count,event from event where device_id in (select id from device where project_id = (select project_id from user where token = ?s limit 1)) and device_id != 0 group by event",array($token));
}
$data = get_data($sql);
for ($i=0; $i < count($data); $i++)
{
$result['count'][$i] = intval($data[$i]['count']);
$result['event'][$i] = $data[$i]['event'];
}
return self::send_result($result);
}
// 事件统计比例
public function getIndexEventRatioInfo()
{
$token = $_SESSION['token'];
$role_id = $this->getUserRole($token);
if($role_id == 2) // 超级管理员账号
{
$sql = "select count(*) as count,event from event group by event";
$sql_1 = "select count(*) from event";
}
elseif($role_id == 1) // 项目账号
{
$sql = prepare("select count(*) as count,event from event where device_id in (select id from device where project_id = (select project_id from user where token = ?s limit 1)) and device_id != 0 group by event",array($token));
$sql_1 = prepare("select count(*) from event where device_id in (select id from device where project_id = (select project_id from user where token = ?s limit 1)) and device_id != 0",array($token));
}
$data = get_data($sql);
$all_count = get_var($sql_1);
for ($i=0; $i < count($data); $i++)
{
$result[$i]['event'] = $data[$i]['event'];
$result[$i]['ratio'] = round(intval($data[$i]['count'])/$all_count*100,0);
}
return self::send_result($result);
}
// 事件列表
public function getIndexEventList()
{
$token = $_SESSION['token'];
$role_id = $this->getUserRole($token);
if($role_id == 2) // 超级管理员账号
{
$sql = "select device_id,event,event_time from event order by id desc limit 5";
}
elseif($role_id == 1) // 项目账号
{
$sql = prepare("select device_id,event,event_time from event where device_id in (select id from device where project_id = (select project_id from user where token = ?s limit 1)) order by id desc limit 5",array($token));
}
$data = get_data($sql);
for ($i=0; $i < count($data); $i++)
{
$sql_1 = prepare("select terminal.name from device left join terminal on terminal.id = device.terminal_id where device.id = ?i limit 1",array($data[$i]['device_id']));
$terminal_name = get_var($sql_1);
$result[$i]['terminal_name'] = $terminal_name;
$result[$i]['event'] = $data[$i]['event'];
$result[$i]['event_time'] = $data[$i]['event_time'];
}
return self::send_result($result);
}
public function test()
{
$str = $_GET['str'];
echo bin2hex($str);
}
}
?>