Files

328 lines
15 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 deviceController 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($total,$data)
{
$obj = array();
$obj['rows'] = $data;
// $obj['result'] = 'success';
$obj['total'] = $total;
if( g('API_EMBED_MODE') == 1 ){
return json_encode($obj);
}else{
header("Content-Type: application/json");
die(json_encode($obj));
}
}
// 发送结果集
public function send_resu($data)
{
$obj = array();
$obj['code'] = 0;
$obj['message'] = '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 total(){
$module_hanyupinyin = v("id");
$terminal = v("terminal");
$project_id = $_SESSION['project_id'];
$sql_terminal = prepare("select id from terminal where name=?s",array($terminal));
$terminal_id = get_var($sql_terminal);
if($project_id == '0'){
if(empty($module_hanyupinyin)){
if(empty($terminal)){
//设备总数
$sql_total = "select count(id) from device";
$total = get_var($sql_total);
//控制柜数量 type =1
$sql_1 = "select count(id) from device where type=1";
$total_1 = get_var($sql_1);
//曝气机数量 type=2
$sql_2 = "select count(id) from device where type=2";
$total_2 = get_var($sql_2);
//管网设备数量 type=3
$sql_3 = "select count(id) from device where type=3";
$total_3 = get_var($sql_3);
}else{
//设备总数
$sql_total = prepare("select count(id) from device where terminal_id=?i",array($terminal_id));
$total = get_var($sql_total);
//控制柜数量 type =1
$sql_1 = prepare("select count(id) from device where type=1 and terminal_id=?i",array($terminal_id));
$total_1 = get_var($sql_1);
//曝气机数量 type=2
$sql_2 = prepare("select count(id) from device where type=2 and terminal_id=?i",array($terminal_id));
$total_2 = get_var($sql_2);
//管网设备数量 type=3
$sql_3 = prepare("select count(id) from device where type=3 and terminal_id=?i",array($terminal_id));
$total_3 = get_var($sql_3);
}
}else{
if(empty($terminal)){
//设备总数
$sql_total = prepare("select count(id) from device where project_id=?i",array($module_hanyupinyin));
$total = get_var($sql_total);
//控制柜数量 type =1
$sql_1 = prepare("select count(id) from device where type=1 and project_id=?i",array($module_hanyupinyin));
$total_1 = get_var($sql_1);
//曝气机数量 type=2
$sql_2 = prepare("select count(id) from device where type=2 and project_id=?i",array($module_hanyupinyin));
$total_2 = get_var($sql_2);
//管网设备数量 type=3
$sql_3 = prepare("select count(id) from device where type=3 and project_id=?i",array($module_hanyupinyin));
$total_3 = get_var($sql_3);
}else{
//设备总数
$sql_total = prepare("select count(id) from device where project_id=?i and terminal_id=?i",array($module_hanyupinyin,$terminal_id));
$total = get_var($sql_total);
//控制柜数量 type =1
$sql_1 = prepare("select count(id) from device where type=1 and project_id=?i and terminal_id=?i",array($module_hanyupinyin,$terminal_id));
$total_1 = get_var($sql_1);
//曝气机数量 type=2
$sql_2 = prepare("select count(id) from device where type=2 and project_id=?i and terminal_id=?i",array($module_hanyupinyin,$terminal_id));
$total_2 = get_var($sql_2);
//管网设备数量 type=3
$sql_3 = prepare("select count(id) from device where type=3 and project_id=?i and terminal_id=?i",array($module_hanyupinyin,$terminal_id));
$total_3 = get_var($sql_3);
}
}
}else{
if(!empty($module_hanyupinyin)){
if(empty($terminal)){
//设备总数
$sql_total = prepare("select count(id) from device where project_id=?i",array($module_hanyupinyin));
$total = get_var($sql_total);
//控制柜数量 type =1
$sql_1 = prepare("select count(id) from device where type=1 and project_id=?i",array($module_hanyupinyin));
$total_1 = get_var($sql_1);
//曝气机数量 type=2
$sql_2 = prepare("select count(id) from device where type=2 and project_id=?i",array($module_hanyupinyin));
$total_2 = get_var($sql_2);
//管网设备数量 type=3
$sql_3 = prepare("select count(id) from device where type=3 and project_id=?i",array($module_hanyupinyin));
$total_3 = get_var($sql_3);
}else{
//设备总数
$sql_total = prepare("select count(id) from device where project_id=?i and terminal_id=?i",array($module_hanyupinyin,$terminal_id));
$total = get_var($sql_total);
//控制柜数量 type =1
$sql_1 = prepare("select count(id) from device where type=1 and project_id=?i and terminal_id=?i",array($module_hanyupinyin,$terminal_id));
$total_1 = get_var($sql_1);
//曝气机数量 type=2
$sql_2 = prepare("select count(id) from device where type=2 and project_id=?i and terminal_id=?i",array($module_hanyupinyin,$terminal_id));
$total_2 = get_var($sql_2);
//管网设备数量 type=3
$sql_3 = prepare("select count(id) from device where type=3 and project_id=?i and terminal_id=?i",array($module_hanyupinyin,$terminal_id));
$total_3 = get_var($sql_3);
}
}else{
if(empty($terminal)){
//设备总数
$sql_total = prepare("select count(id) from device where project_id=?i ",array($project_id));
$total = get_var($sql_total);
//控制柜数量 type =1
$sql_1 = prepare("select count(id) from device where type=1 and project_id=?i ",array($project_id));
$total_1 = get_var($sql_1);
//曝气机数量 type=2
$sql_2 = prepare("select count(id) from device where type=2 and project_id=?i ",array($project_id));
$total_2 = get_var($sql_2);
//管网设备数量 type=3
$sql_3 = prepare("select count(id) from device where type=3 and project_id=?i ",array($project_id));
$total_3 = get_var($sql_3);
}else{
//设备总数
$sql_total = prepare("select count(id) from device where project_id=?i and terminal_id=?i",array($project_id,$terminal_id));
$total = get_var($sql_total);
//控制柜数量 type =1
$sql_1 = prepare("select count(id) from device where type=1 and project_id=?i and terminal_id=?i",array($project_id,$terminal_id));
$total_1 = get_var($sql_1);
//曝气机数量 type=2
$sql_2 = prepare("select count(id) from device where type=2 and project_id=?i and terminal_id=?i",array($project_id,$terminal_id));
$total_2 = get_var($sql_2);
//管网设备数量 type=3
$sql_3 = prepare("select count(id) from device where type=3 and project_id=?i and terminal_id=?i",array($project_id,$terminal_id));
$total_3 = get_var($sql_3);
}
}
}
$data = array('total'=>$total,'total_1'=>$total_1,'total_2'=>$total_2,'total_3'=>$total_3);
return self::send_resu($data);
}
public function deviceDataTable(){
// $projectid = v('project_id');
$module_hanyupinyin = v("id");
$terminal = v('terminal');
$project_id = $_SESSION['project_id'];
if($project_id == '0'){
if(empty($module_hanyupinyin)){
if(empty($terminal)){
$sql = 'select device.id,project.name as project,terminal.name as terminal,device.name,device.sn,device.type,device.register_address,device.status,device.note,device.longitude,device.latitude from project,terminal,device where project.id=device.project_id and terminal.id=device.terminal_id';
}else{
$sql = prepare('select device.id,project.name as project,terminal.name as terminal,device.name,device.sn,device.type,device.register_address,device.status,device.note,device.longitude,device.latitude from project,terminal,device where project.id=device.project_id and terminal.id=device.terminal_id and terminal.name=?s',array($terminal));
}
}else{
if(empty($terminal)){
$sql = prepare('select device.id,project.name as project,terminal.name as terminal,device.name,device.sn,device.type,device.register_address,device.status,device.note,device.longitude,device.latitude from project,terminal,device where project.id=device.project_id and terminal.id=device.terminal_id and device.project_id=?i',array($module_hanyupinyin));
}else{
$sql = prepare('select device.id,project.name as project,terminal.name as terminal,device.name,device.sn,device.type,device.register_address,device.status,device.note,device.longitude,device.latitude from project,terminal,device where project.id=device.project_id and terminal.id=device.terminal_id and device.project_id=?i and terminal.name=?s',array($module_hanyupinyin,$terminal));
}
}
}else{
if(empty($module_hanyupinyin)){
if(empty($terminal)){
$sql = prepare("select device.id,project.name as project,terminal.name as terminal,device.name,device.sn,device.type,device.register_address,device.status,device.note,device.longitude,device.latitude from project,terminal,device where project.id=device.project_id and terminal.id=device.terminal_id and device.project_id=?i ",array($project_id));
}else{
$sql = prepare("select device.id,project.name as project,terminal.name as terminal,device.name,device.sn,device.type,device.register_address,device.status,device.note,device.longitude,device.latitude from project,terminal,device where project.id=device.project_id and terminal.id=device.terminal_id and device.project_id=?i and terminal.name=?s",array($project_id,$terminal));
}
}else{
if(empty($terminal)){
$sql = prepare("select device.id,project.name as project,terminal.name as terminal,device.name,device.sn,device.type,device.register_address,device.status,device.note,device.longitude,device.latitude from project,terminal,device where project.id=device.project_id and terminal.id=device.terminal_id and device.project_id=?i ",array($project_id));
}else{
$sql = prepare("select device.id,project.name as project,terminal.name as terminal,device.name,device.sn,device.type,device.register_address,device.status,device.note,device.longitude,device.latitude from project,terminal,device where project.id=device.project_id and terminal.id=device.terminal_id and device.project_id=?i and terminal.name=?s",array($project_id,$terminal));
}
}
}
$data = get_data($sql);
$total = count($data);
return self::send_result($total,$data);
}
/**
* 返回设备详情
*/
public function device_details()
{
$device_id = v("id");
// 累计调控次数
$sql_count = prepare("select (main_sewage_pump_count+standby_sewage_pump_count+main_rain_pump_count+gate_up_count+gate_down_count) as control_count from data where device_id = ?i order by id desc limit 1",array($device_id));
$data['control_count'] = get_var($sql_count);
$sql_warn = prepare("select count(warn.id) from warn where device_id = ?i ",array($device_id));
$data['warn_count'] = get_var($sql_warn);
//基本信息 场站名称 地址 负责人 联系方式
$sql = prepare("select project.name as project_name,terminal.name as terminal_name,device.name,device.register_address,if(device.status=1,'使用中','实施中') as status,device.created_at from device left join project on project.id = device.project_id left join terminal on terminal.id = device.terminal_id where device.id=?i",array($device_id));
$data['info'] = get_line($sql);
//设备情况 控制状态 控制模式 闸门状态 1主污泵状态 2备污泵状态 3雨水泵
$sql_strategy = prepare("select strategy.name from strategy,device where strategy.id=device.strategy_id and device.id=?i",array($device_id));
$data['strategy'] = get_var($sql_strategy);
$sql_controlmodel = prepare("select CASE WHEN control_model = 1 THEN '手动模式' else '自动模式' END from device where id=?i limit 1",array($device_id));
$data['controlmodel'] = get_var($sql_controlmodel);
$sql_device_status = prepare("select if(main_sewage_pump=1,'开启','关闭') as main_sewage_pump,if(standby_sewage_pump=1,'开启','关闭') as standby_sewage_pump,if(main_rain_pump=1,'开启','关闭') as main_rain_pump,if(gate_up=1,'闸门上升','闸门上升停止') as gate_up,if(gate_down=1,'闸门下降','闸门下降停止') as gate_down from data where device_id = ?i order by id desc limit 1",array($device_id));
$data['gate_up'] = get_line($sql_device_status)['gate_up'];
$data['gate_down'] = get_line($sql_device_status)['gate_down'];
$data['main_sewage_pump'] = get_line($sql_device_status)['main_sewage_pump'];
$data['standby_sewage_pump'] = get_line($sql_device_status)['standby_sewage_pump'];
$data['main_rain_pump'] = get_line($sql_device_status)['main_rain_pump'];
if (!empty($_SESSION)) {
render($data, 'web', 'device_details');
} else {
header("Location:?c=index&a=index");
}
}
// 设备详情列表
public function deviceDetailsDataTable(){
$id = v("id");
$sql = prepare("select elec_conductivity_data,elec_resistivity_data,elec_conductivity_tds_data,rainfall_data,rainsnow,level_data,ph_data,ammonia_data,created_at from data where device_id = ?i order by created_at desc limit 200",array($id));
$data = get_data($sql);
$total = count($data);
return self::send_result($total,$data);
}
public function terminal(){
$module_hanyupinyin = 1;
if(empty($module_hanyupinyin)){
$sql_terminal = prepare("select terminal.id,terminal.name from terminal,project where project.id=terminal.project_id",array($module_hanyupinyin));
$terminal = get_data($sql_terminal);
}else{
$sql_terminal = prepare("select terminal.id,terminal.name from terminal,project where project.id=terminal.project_id and terminal.project_id=?i",array($module_hanyupinyin));
$terminal = get_data($sql_terminal);
}
return self::send_resu($terminal);
}
// 设备修改
public function device_edit()
{
$device_id = v('id');
$sql = prepare("select device.id,project.name as project_name,terminal.name as terminal_name,device.name,device.register_address as address,device.sn,device.longitude,device.latitude,device.created_at from device left join project on project.id = device.project_id left join terminal on terminal.id = device.terminal_id where device.id=?i",array($device_id));
$data= get_line($sql);
render($data, 'web', 'device_edit');
}
// 设备修改
public function device_update()
{
$id = $_POST['id'];
$project_name = $_POST['project_name'];
$terminal_name = $_POST['terminal_name'];
$name = $_POST['name'];
$address = $_POST['address'];
$longitude = $_POST['longitude'];
$latitude = $_POST['latitude'];
$sn = $_POST['sn'];
$sql = prepare("update device set name=?s,register_address=?s,longitude=?s,latitude=?s,sn=?s where id=?i",array($name,$address,$longitude,$latitude,$sn,$id));
$impact = run_sql($sql);
if($impact)
{
$result = "success";
}else
{
$result = "false";
}
return self::send_resu($result);
}
}