before_add = function(&$data){
//检测地址码是否重复
/*$device_addr = trim($data['DeviceAddr']);
$device_info = M('dpsb_device')->where(array('DeviceAddr'=>$device_addr))->find();
if($device_info){
json_fail('该地址码已存在,请重新输入!');
}*/
};
$form->before_save = function(&$data){
$id = I('get.id');
$device_addr = trim($data['DeviceAddr']);
$device_imei = trim($data['DeviceImei']);
$cond = array('ID'=>$id);
$userinfo = M('dpsb_device')->where($cond)->field('DeviceAddr,DeviceImei')->find();
$device_info = M('dpsb_device')->where(array('DeviceAddr'=>$device_addr))->find();
if($device_addr != $userinfo['DeviceAddr'] && $device_info){
json_fail('该地址码已存在,请重新输入!');
}
};
$form->form_filter = function(&$form){
//显示设备类型
$type_list = \Zndp\Device\Option::getDeviceTypeOption();
$form['fields']['DeviceTypeId']['editor'] = array('options'=>$type_list);
//显示设备组下拉数据
$type = new \Zndp\Device\Option;
$channeltype = $type->getGroupOption();
$form['fields']['DeviceGroupId']['editor'] = array('options'=>$channeltype);
};
$form->display($this);
}
function sys_list( ){
$list = new \Jms\Gui\ClGrid();
$list->sql_sort = 'DeviceAddr desc';
$list->sql_filter = function($search,&$cond){
$farmid = I('get.id');
if($farmid){ // 点击导航树进入
//获取当前部门下所有基地
$farm_ids = \Zndp\User\Util::get_depart_farm($farmid);
//取出基地下所有设备组
$where = array('FarmId'=>array('IN',$farm_ids));
$group_ids = M('dpsb_group')->field('ID')->where($where)->select();
$group_ids = array_column($group_ids,'ID');
$cond['DeviceGroupId'] = array('in',$group_ids);
}else{
if($this->userinfo['OrganizationId']){ //默认选中
//获取当前部门下所有基地
$farm_ids = \Zndp\User\Util::get_depart_farm($this->userinfo['OrganizationId']);
$where = array('FarmId'=>array('IN',$farm_ids));
//取出基地下所有设备组
$group_ids = M('dpsb_group')->field('ID')->where($where)->select();
$group_ids = array_column($group_ids,'ID');
$cond['DeviceGroupId'] = array('in',$group_ids);
}else{ //平台管理员
$cond['DeviceGroupId'] = array('neq',0);
}
}
};
$list->row_filter = function(&$row) use ($list){
$row['DeviceAddr'] = $list->renderXEditableInput('地址码',$row,'DeviceAddr',array('rule'=>array('required'=>true)));
$row['DeviceGroupId'] = $list->renderXEditableInput('设备组ID',$row,'DeviceGroupId',array('rule'=>array('required'=>true)));
$row['WorkUnitName'] = $list->renderXEditableInput('设备名称',$row,'WorkUnitName',array('rule'=>array('required'=>true)));
$row['IccId'] = str_replace(' ','
',$row['IccId']);
$where = array('ID'=>$row['DeviceTypeId']);
$row['DeviceTypeId_text'] = M('dpsb_type')->where($where)->getField('TypeName');
};
$list->display($this);
}
public function sys_del( ){
if (IS_AJAX) {
$device_id = I('get.id');
if ( empty($device_id)) {
json_fail('设备id不能为空');
}
// 如果已经绑定大棚,需要先解绑
$where = array( 'DeviceId' => $device_id, 'FarmlandId' => array('neq',-1) );
$count = M('dpsb_chgroup')->where($where)->count();
if( $count > 0 ){
json_fail('有'.$count.'个田地与设备绑定中,无法删除');
}
if(M('dpsb_device')->where(array( 'ID' => $device_id ))->delete() === false){
json_fail('设备表删除失败','');
}
if(M('dpsb_chgroup')->where(array( 'DeviceId' => $device_id ))->delete() === false){
json_fail('通道组组表删除失败','');
}
if(M('dpsb_channel')->where(array( 'DeviceId' => $device_id ))->delete() === false){
json_fail('通道表删除失败','');
}
if(M('dpsb_policy')->where(array( 'DeviceId' => $device_id ))->delete() === false){
json_fail('决策表删除失败','');
}
if(M('dpsj_device')->where(array( 'DeviceId' => $device_id ))->delete() === false){
json_fail('设备数据删除失败','');
}
if(M('dpsj_channel')->where(array( 'DeviceId' => $device_id ))->delete() === false){
json_fail('通道数据删除失败','');
}
json_success('删除成功','');
}
}
function sys_binding( ){
$form = new \Jiaruan\LigerIframeForm();
$form->before_save = function(&$data){
$id = I('get.id');
$group_id = trim($data['DeviceGroupId']);
$cond = array('ID'=>$group_id);
$groupinfo = M('dpsb_group')->where($cond)->find();
if(!$groupinfo){
json_fail('设备组ID不存在');
}
};
$form->display($this);
}
function sys_add_list( ){
$list = new \Jms\Gui\ClGrid();
$list->sql_sort = 'AddTime desc';
$list->right_filter = function($right,$type){
$page = $_SERVER['PATH_INFO'];
return \Jms\Ucenter\Right::defaultFilter($page,$right,$type);
};
$list->sql_filter = function($search,&$cond){
//$cond['DeviceAddr'] = '';
$cond['DeviceGroupId'] = 0;
//$cond['WorkUnitName'] = '';
//搜索
if($search['DeviceImei']){
$cond['DeviceImei'] = $search['DeviceImei'];
}
if($search['IccId']){
$cond['IccId'] = $search['IccId'];
}
};
$list->row_filter = function(&$row) use ($list){
$type_info = M('dpsb_type')->field('TypeName,DeviceModel')->where($where)->find();
$row['DeviceTypeId_text'] = $type_info['TypeName'] .' - '.$type_info['DeviceModel'];
};
//搜索栏回调
$list->searchbar_filter = function(&$searchbar) {
};
$list->display($this);
}
public function sys_debug( ){
if (IS_AJAX) {
$id = I('get.id');
if(empty($id)){
json_fail('id不存在');
}
$addr = M('dpsb_device')->where(array('ID'=>$id))->getField('DeviceAddr');
$host = '127.0.0.1';
$port = 10240;
$socket = new Jms\Network\TcpClient($host,$port);
$cmd = array(
"method" => "apiUploadDebug",
"Addr"=>$addr,
);
$res = $socket->send(json_encode($cmd));
if($res['success']){
json_success('下发成功');
}else{
json_fail($res['message']);
}
}
}
public function sys_upload( ){
if (IS_AJAX) {
$id = I('get.id');
if(empty($id)){
json_fail('id不存在');
}
$addr = M('dpsb_device')->where(array('ID'=>$id))->getField('DeviceAddr');
$host = '127.0.0.1';
$port = 10240;
$socket = new Jms\Network\TcpClient($host,$port);
$cmd = array(
"method" => "apiUpgrade",
"Addr"=>$addr,
);
$res = $socket->send(json_encode($cmd));
if($res['success']){
json_success('下发成功');
}else{
json_fail($res['message']);
}
}
}
public function sys_reboot( ){
if (IS_AJAX) {
$id = I('get.id');
if(empty($id)){
json_fail('id不存在');
}
$addr = M('dpsb_device')->where(array('ID'=>$id))->getField('DeviceAddr');
$host = '127.0.0.1';
$port = 10240;
$socket = new Jms\Network\TcpClient($host,$port);
$cmd = array(
"method" => "apiReboot",
"Addr"=>$addr,
);
$res = $socket->send(json_encode($cmd));
if($res['success']){
json_success('下发成功');
}else{
json_fail($res['message']);
}
}
}
public function sys_sreboot( ){
if (IS_AJAX) {
$id = I('get.id');
if(empty($id)){
json_fail('id不存在');
}
$addr = M('dpsb_device')->where(array('ID'=>$id))->getField('DeviceAddr');
$host = '127.0.0.1';
$port = 10240;
$socket = new Jms\Network\TcpClient($host,$port);
$cmd = array(
"method" => "apiSysReboot",
"Addr"=>$addr,
);
$res = $socket->send(json_encode($cmd));
if($res['success']){
json_success('下发成功');
}else{
json_fail($res['message']);
}
}
}
function sys_addedit_crops( ){
$form = new \Jiaruan\LigerIframeForm();
$form->before_save = function(&$data){
};
$form->display($this);
}
function sys_msg_set( ){
$form = new \Jms\Gui\ClForm();
$form->before_display = function(&$data){
$data['ExceptContent'] = $data['ExceptContent']?:'温度异常了';
};
$form->before_save = function(&$data){
// 温度不能是空字符串
if($data['HighTemp'] === '' || $data['LowTemp'] === ''){
json_fail('温度不能为空');
}
// 温度必须是数字或数字字符串
if( !is_numeric($data['HighTemp']) || !is_numeric($data['LowTemp']) ){
json_fail('温度必须是数字');
}
};
$form->display($this);
}
public function get_alarm_set( ){
$device_addr = I('get.device_addr');
if(!$device_addr){
json_fail('Missing DeviceAddr param!');
}
$where = array('DeviceAddr'=>$device_addr);
$field = "Mobiles,ExceptContent,SmsInterval,HighTemp,IsUseHT,LowTemp,IsUseLT,SoundLightAlarmInterval,SoundLightDuration,SeveralExceptions";
$result = M('dpsb_device')->where($where)->field($field)->find();
if(!$result){ //设备不存在
json_fail('DeviceAddr not exists!');
}
//返回json格式数据
$json_data = json_encode($result,JSON_UNESCAPED_UNICODE);
echo $json_data;
}
function set_screen_msg( ){
/*
对外地址(提供给农科院) http://zndp.rltest.cn/index.php?s=dpsb_device/set_screen_msg&token=zndp
查看地址(这个地址也可以修改) http://zndp.rltest.cn/index.php?s=dpsb_device/set_screen_msg&token=zndp&addr=617261
*/
if (!$_GET['token'] || $_GET['token'] != 'zndp') {
echo '非法访问';
die;
}
//根据设备地址获取id信息,不能根据id修改信息
$_GET['id'] = 0;
if (IS_POST || $_GET['addr']) {
if ($_GET['addr']) {
$addr = $_GET['addr'];
} else {
$input = json_decode(file_get_contents("php://input"), true);
$addr = $input['DeviceAddr'];
}
if (!$addr) {
json_fail('设备地址有误');
}
$where = array('DeviceAddr' => $addr);
$rows = M(self::tableName)->field('id')->where($where)->select();
if (!$rows || count($rows) != 1) {
json_fail('设备地址错误');
}
$_GET['id'] = $rows[0]['id'];
}
$form = new \Jiaruan\LigerIframeForm();
$form->before_save = function(&$data){
};
$form->display($this);
echo '
';
}
function oper_list( ){
$list = new \Jms\Gui\ClGrid();
$list->sql_sort = 'AddTime desc';
$list->sql_filter = function($search,&$cond){
$userid = \Jms\Ucenter\Cookie::getLoginUserId();
$where = array('ID'=>$userid);
$depart_id = M('uc_user')->where($where)->getField('OrganizationId');
if(!$depart_id){
json_fail('登录用户信息有误,请联系管理员!');
}
//获取登录角色部门下所有基地
$depart_ids = \Zndp\User\Util::get_depart_farm($depart_id);
//获取基地下的设备组
$where = array('FarmId'=>array('IN',$depart_id));
$group_ids = M('dpsb_group')->field('ID')->where($where)->select();
$group_ids = array_column($group_ids,'ID');
//根据设备组id查出设备
$cond['DeviceGroupId'] = array('in',$group_ids);
//搜索
if($search['WorkUnitName']){
$cond['WorkUnitName'] = array('like','%'.$search['WorkUnitName'].'%');
}
if($search['DeviceImei']){
$cond['DeviceImei'] = array('like','%'.$search['DeviceImei'] .'%');
}
};
$list->display($this);
}
function set_screen_msg2( ){
$form = new \Jms\Gui\ClForm();
$shed = array(1=>'FirstCrop','SecondCrop','ThirdCrop','FourthCrop');
$form->before_display = function(&$data) use($shed){
// 展示点阵屏信息
for($i=1; $i<=count($shed); $i++){
$shed_data = json_decode($data[$shed[$i]], true);
$data['num'.$i] = $shed_data['num'.$i];
$data['variety'.$i] = $shed_data['variety'.$i];
$data['farmer'.$i] = $shed_data['farmer'.$i];
$data['output'.$i] = $shed_data['output'.$i];
$data['unit'.$i] = $shed_data['unit'.$i];
}
};
$form->before_save = function(&$data) use($shed){
// 保存点阵屏信息
for($i=1; $i<=count($shed); $i++){
$shed_data = array(
'num'.$i => $data['num'.$i],
'variety'.$i =>$data['variety'.$i],
'farmer'.$i =>$data['farmer'.$i],
'output'.$i =>$data['output'.$i],
'unit'.$i =>$data['unit'.$i]
);
$data[$shed[$i]] = json_encode($shed_data, JSON_UNESCAPED_UNICODE);
}
};
$form->display($this);
}
function sys_list_search( ){
}
function set_screen_msg3( ){
$form = new \Jms\Gui\ClForm();
$shed = array(1=>'FirstCrop','SecondCrop','ThirdCrop','FourthCrop');
$form->before_display = function(&$data) use($shed){
// 展示点阵屏信息
for($i=1; $i<=count($shed); $i++){
$shed_data = json_decode($data[$shed[$i]], true);
$data['num'.$i] = $shed_data['num'.$i];
$data['variety'.$i] = $shed_data['variety'.$i];
$data['farmer'.$i] = $shed_data['farmer'.$i];
$data['output'.$i] = $shed_data['output'.$i];
$data['unit'.$i] = $shed_data['unit'.$i];
}
};
$form->before_save = function(&$data) use($shed){
// 保存点阵屏信息
for($i=1; $i<=count($shed); $i++){
$shed_data = array(
'num'.$i => $data['num'.$i],
'variety'.$i =>$data['variety'.$i],
'farmer'.$i =>$data['farmer'.$i],
'output'.$i =>$data['output'.$i],
'unit'.$i =>$data['unit'.$i]
);
$data[$shed[$i]] = json_encode($shed_data, JSON_UNESCAPED_UNICODE);
}
};
$form->display($this);
}
function sys_nav_tree( ){
//获取导航树数据
$fields = 'ID as id,ParentID as pId,DepartName as name,Level as level';
$zndp_util = new \Zndp\User\Util();
$depart_id = $this->userinfo['OrganizationId'];
$depart_list = $zndp_util->get_depart_option($depart_id,$fields,'name',false);
if(empty($depart_list)){
//没有数据,显示该
$depart_list[0] = array('id'=>'','pid'=>'','name'=>'没有组织','direct_url'=>'');
}else{
for($i=0; $iuserinfo['RoleId'], $admin_arr) ){
$all_list = array('id'=>'0','pId'=>'-1','name'=>'所有设备','direct_url'=>'?s=dpsb_device/sys_list','open'=>true);
array_unshift($depart_list,$all_list);
}
//未分配的设备
$undistr_devic = array('id'=>'-2','pId'=>'-1','name'=>'未分配设备','direct_url'=>'?s=dpsb_device/sys_add_list');
array_unshift($depart_list,$undistr_devic);
//设备组
$device_group = array('id'=>'-3','pId'=>'-1','name'=>'设备组列表','direct_url'=>'?s=dpsb_group/sys_list');
array_unshift($depart_list,$device_group);
$this->assign('title','用户管理');
$this->assign('tree_data',$depart_list);
$this->display();
}
function sys_list2( ){
$list = new \Jms\Gui\ClGrid();
$list->sql_sort = 'DeviceAddr desc';
$list->before_xeditable_modify = function($name,$data) use ($list){
if( $name == 'WorkUnitName' ){ //修改设备名称
$dpsb_device = M('dpsb_device');
// 同一设备组内唯一
$where = array( $data['pkfield'] => $data['pk'] );
$group_id = $dpsb_device->where($where)->getField('DeviceGroupId');
$where = array(
$data['pkfield'] => array('neq',$data['pk']),
'DeviceGroupId' => $group_id,
'WorkUnitName' => $data['value'],
);
$count = $dpsb_device->where($where)->count();
if( $count > 0 ){
json_fail('设备名称已存在');
}
}
};
$list->right_filter = function($right,$type){
$page = $_SERVER['PATH_INFO'];
return \Jms\Ucenter\Right::defaultFilter($page,$right,$type);
};
$list->sql_filter = function($search,&$cond){
$farmid = I('get.id');
if($farmid){ // 点击导航树进入
//获取当前部门下所有基地
$farm_ids = \Zndp\User\Util::get_depart_farm($farmid);
//取出基地下所有设备组
$where = array('FarmId'=>array('IN',$farm_ids));
$group_ids = M('dpsb_group')->field('ID')->where($where)->select();
$group_ids = array_column($group_ids,'ID');
$cond['DeviceGroupId'] = array('in',$group_ids);
}else{
if($this->userinfo['OrganizationId']){ //默认选中
//获取当前部门下所有基地
$farm_ids = \Zndp\User\Util::get_depart_farm($this->userinfo['OrganizationId']);
$where = array('FarmId'=>array('IN',$farm_ids));
//取出基地下所有设备组
$group_ids = M('dpsb_group')->field('ID')->where($where)->select();
$group_ids = array_column($group_ids,'ID');
$cond['DeviceGroupId'] = array('in',$group_ids);
}else{ //平台管理员
$cond['DeviceGroupId'] = array('neq',0);
}
}
//搜索
$keyword = $search['keyword'];
if($keyword){
$field = $search['select_list'];
$cond[$field] = array('LIKE','%'.$keyword.'%');
}
};
$list->row_filter = function(&$row) use ($list){
// 获取设备组名称
$where = array('ID'=>$row['DeviceGroupId']);
$row['DeviceGroupId_text'] = M('dpsb_group')->where($where)->getField('OrgName');
$row['WorkUnitName_text'] = $list->renderXEditableInput('设备名称',$row,'WorkUnitName',array('rule'=>array('required'=>true)));
// 获取设备类型名称
$where = array('ID'=>$row['DeviceTypeId']);
$type_info = M('dpsb_type')->field('TypeName,DeviceModel')->where($where)->find();
$row['DeviceTypeId_text'] = $type_info['TypeName'] .' - '.$type_info['DeviceModel'];
$row['IccId'] = str_replace(' ','
',$row['IccId']);
// 是否在线
if( time() - strtotime($row['OnlineTime']) < 180 ){//超过3分钟算离线
$row['OnlineTime_text'] = "在线";
}else{
$row['OnlineTime_text'] = "离线";
}
// 是否安装
$where = array('DeviceId'=>$row['ID'],'FarmlandId'=>array('gt',-1));
$count = M('dpsb_chgroup')->where($where)->count();
if($count > 0){
$row['InstallState'] = "已安装";
}else{
$row['InstallState'] = "未安装";
}
//设备已关联田地
$where = array('DeviceId'=>$row['ID'],'FarmlandId'=>array('NEQ',-1));
$chgroup_list = M('dpsb_chgroup')->field('ChGroup,FarmlandId')->where($where)->select();
$assoc_string = '';//关联田地显示字符串
$dpyh_farmland = M('dpyh_farmland');
foreach($chgroup_list as $chgroup){
$where = array('ID'=>$chgroup['FarmlandId']);
$farmland_name = $dpyh_farmland->where($where)->getField('FarmlandName');
$assoc_string .= '通道组'.$chgroup['ChGroup'].' - '.$farmland_name.'
';
}
$row['AssocFarmland'] = $assoc_string;
};
$list->display($this);
}
function sys_new_search( ){
}
function sys_list3( ){
$list = new \Jms\Gui\ClGrid();
$list->sql_sort = 'DeviceAddr desc';
$list->right_filter = function($right,$type){
$page = $_SERVER['PATH_INFO'];
return \Jms\Ucenter\Right::defaultFilter($page,$right,$type);
};
$list->before_xeditable_modify = function($name,$data) use ($list){
if( $name == 'WorkUnitName' ){ //修改设备名称
$dpsb_device = M('dpsb_device');
// 同一设备组内唯一
$where = array( $data['pkfield'] => $data['pk'] );
$group_id = $dpsb_device->where($where)->getField('DeviceGroupId');
$where = array(
$data['pkfield'] => array('neq',$data['pk']),
'DeviceGroupId' => $group_id,
'WorkUnitName' => $data['value'],
);
$count = $dpsb_device->where($where)->count();
if( $count > 0 ){
json_fail('设备名称已存在');
}
}
};
$list->sql_filter = function($search,&$cond){
$group_id = I('get.id');
$cond['DeviceGroupId'] = $group_id;
//搜索
$keyword = $search['keyword'];
if($keyword){
$field = $search['select_list'];
$cond[$field] = array('LIKE','%'.$keyword.'%');
}
};
$list->row_filter = function(&$row) use ($list){
$row['WorkUnitName_text'] = $list->renderXEditableInput('设备名称',$row,'WorkUnitName',array('rule'=>array('required'=>true)));
// 获取设备类型名称
$where = array('ID'=>$row['DeviceTypeId']);
$type_info = M('dpsb_type')->field('TypeName,DeviceModel')->where($where)->find();
$row['DeviceTypeId_text'] = $type_info['TypeName'] .' - '.$type_info['DeviceModel'];
$row['IccId'] = str_replace(' ','
',$row['IccId']);
if( time() - strtotime($row['OnlineTime']) < 180 ){//超过3分钟算离线
$row['OnlineTime_text'] = "在线";
}else{
$row['OnlineTime_text'] = "离线";
}
$where = array('DeviceId'=>$row['ID'],'FarmlandId'=>array('gt',-1));
$count = M('dpsb_chgroup')->where($where)->count();
if($count > 0){
$row['InstallState'] = "已安装";
}else{
$row['InstallState'] = "未安装";
}
//设备已关联田地
$where = array('DeviceId'=>$row['ID'],'FarmlandId'=>array('NEQ',-1));
$chgroup_list = M('dpsb_chgroup')->field('ChGroup,FarmlandId')->where($where)->select();
$assoc_string = '';//关联田地显示字符串
$dpyh_farmland = M('dpyh_farmland');
foreach($chgroup_list as $chgroup){
$where = array('ID'=>$chgroup['FarmlandId']);
$farmland_name = $dpyh_farmland->where($where)->getField('FarmlandName');
$assoc_string .= '通道组'.$chgroup['ChGroup'].' - '.$farmland_name.'
';
}
$row['AssocFarmland'] = $assoc_string;
};
$list->display($this);
}
function sys_addedit( ){
$form = new \Jms\Gui\ClForm();
$form->before_save = function(&$data){
$device_id = I('get.id');
$where = array(
'ID' => array('neq',$device_id),
'DeviceGroupId' => $data['DeviceGroupId'],
'WorkUnitName' => $data['WorkUnitName']
);
$count = M('dpsb_device')->where($where)->count();
if( $count > 0 ){
json_fail('设备名称已存在');
}
};
$form->form_filter = function(&$form){
//显示设备组下拉数据
$type = new \Zndp\Device\Option;
$channeltype = $type->getGroupOption();
$form['fields']['DeviceGroupId']['editor'] = array('options'=>$channeltype);
};
$form->display($this);
}
}