瀏覽代碼

first commit

git 6 年之前
父節點
當前提交
240f974041
共有 60 個文件被更改,包括 4509 次插入3 次删除
  1. 1 0
      Home/Common/common.php
  2. 2243 0
      Home/Conf/db_structure.php
  3. 43 0
      Home/Lib/Action/CommonAction.class.php
  4. 237 0
      Home/Lib/Action/DpsbChannelAction.class.php
  5. 190 0
      Home/Lib/Action/DpsbChgroupAction.class.php
  6. 44 0
      Home/Lib/Action/DpsbDevice2Action.class.php
  7. 689 0
      Home/Lib/Action/DpsbDeviceAction.class.php
  8. 126 0
      Home/Lib/Action/DpsbGroupAction.class.php
  9. 52 0
      Home/Lib/Action/DpsbListNavAction.class.php
  10. 596 0
      Home/Lib/Action/DpsbPolicyAction.class.php
  11. 61 0
      Home/Lib/Action/DpsbTypeAction.class.php
  12. 10 0
      Home/Lib/Action/IndexAction.class.php
  13. 2 0
      Home/Tpl/DpsbChannel/send.html
  14. 2 0
      Home/Tpl/DpsbChannel/sys_addedit.html
  15. 2 0
      Home/Tpl/DpsbChannel/sys_list.html
  16. 2 0
      Home/Tpl/DpsbChannel/sys_msg_set.html
  17. 2 0
      Home/Tpl/DpsbChgroup/sys_device_addedit.html
  18. 2 0
      Home/Tpl/DpsbChgroup/sys_device_list.html
  19. 2 0
      Home/Tpl/DpsbChgroup/sys_msg_set.html
  20. 2 0
      Home/Tpl/DpsbChgroup/sys_unbind.html
  21. 2 0
      Home/Tpl/DpsbDevice/oper_list.html
  22. 2 0
      Home/Tpl/DpsbDevice/set_screen_msg.html
  23. 2 0
      Home/Tpl/DpsbDevice/set_screen_msg2.html
  24. 2 0
      Home/Tpl/DpsbDevice/set_screen_msg3.html
  25. 2 0
      Home/Tpl/DpsbDevice/sys_add_list.html
  26. 2 0
      Home/Tpl/DpsbDevice/sys_addedit.html
  27. 2 0
      Home/Tpl/DpsbDevice/sys_addedit_crops.html
  28. 2 0
      Home/Tpl/DpsbDevice/sys_addedit_new.html
  29. 2 0
      Home/Tpl/DpsbDevice/sys_addedit_old.html
  30. 2 0
      Home/Tpl/DpsbDevice/sys_binding.html
  31. 2 0
      Home/Tpl/DpsbDevice/sys_import_excel.html
  32. 2 0
      Home/Tpl/DpsbDevice/sys_list.html
  33. 2 0
      Home/Tpl/DpsbDevice/sys_list2.html
  34. 2 0
      Home/Tpl/DpsbDevice/sys_list3.html
  35. 1 0
      Home/Tpl/DpsbDevice/sys_list_search.html
  36. 2 0
      Home/Tpl/DpsbDevice/sys_msg_set.html
  37. 1 0
      Home/Tpl/DpsbDevice/sys_nav_tree.html
  38. 1 0
      Home/Tpl/DpsbDevice/sys_new_search.html
  39. 2 0
      Home/Tpl/DpsbDevice2/set_screen_msg2.html
  40. 2 0
      Home/Tpl/DpsbGroup/import_device.html
  41. 2 0
      Home/Tpl/DpsbGroup/sys_addedit.html
  42. 2 0
      Home/Tpl/DpsbGroup/sys_list.html
  43. 1 0
      Home/Tpl/DpsbListNav/sys_list_nav.html
  44. 2 0
      Home/Tpl/DpsbPolicy/sys_add.html
  45. 2 0
      Home/Tpl/DpsbPolicy/sys_addedit.html
  46. 2 0
      Home/Tpl/DpsbPolicy/sys_addedit_dp.html
  47. 2 0
      Home/Tpl/DpsbPolicy/sys_device_list.html
  48. 2 0
      Home/Tpl/DpsbPolicy/sys_farmland_list.html
  49. 2 0
      Home/Tpl/DpsbPolicy/sys_list.html
  50. 1 0
      Home/Tpl/DpsbPolicy/test.html
  51. 2 0
      Home/Tpl/DpsbType/sys_addedit.html
  52. 2 0
      Home/Tpl/DpsbType/sys_list.html
  53. 0 3
      README.md
  54. 5 0
      changelog.md
  55. 1 0
      database.json
  56. 40 0
      dp_index.php
  57. 6 0
      index.php
  58. 28 0
      install.php
  59. 53 0
      plugin.json
  60. 9 0
      stub.php

+ 1 - 0
Home/Common/common.php

@@ -0,0 +1 @@
+<?php

File diff suppressed because it is too large
+ 2243 - 0
Home/Conf/db_structure.php


+ 43 - 0
Home/Lib/Action/CommonAction.class.php

@@ -0,0 +1,43 @@
+<?php
+
+
+
+class CommonAction extends Action {
+			
+
+
+ 
+	public  function _initialize(  ){
+		//项目信息
+		$this->project_id = C('CC_PROJECT_ID');
+		if(!$this->project_id){
+			echo 'project_id empty';
+			return;
+		}
+		$this->site_title = C('SITE_TITLE') ? : '用户中心';
+		$this->site_copyright = C('SITE_COPYRIGHT') ? : '版权所有';
+		
+		//跳过检查登录的操作
+		if(in_array(ACTION_NAME,array('login','login2','register','init','set_screen_msg') ))
+			return;	
+		if(strpos(ACTION_NAME,'api_')===0)
+			return;
+		
+		//获取用户信息
+		$this->userid = \Jiaruan\UcCookie::get();
+		if(!$this->userid){
+			header('Location: ?s=uc_manage/login');
+			exit;
+		}
+		$cond = array('ID'=>$this->userid);
+		$this->userinfo = M('uc_user')->where($cond)->find();
+		
+		//跳转到登录页面
+		if(!$this->userinfo){
+			header('Location: ?s=uc_manage/login');
+			exit;
+		}
+	}
+	
+
+}

+ 237 - 0
Home/Lib/Action/DpsbChannelAction.class.php

@@ -0,0 +1,237 @@
+<?php
+
+
+
+class DpsbChannelAction extends CommonAction {
+	const tableName =  'dpsb_channel';	
+	const pkName =  'ID';
+	
+
+
+ 
+	  function sys_list(  ){
+		$list = new \Jms\Gui\ClGrid();
+		$list->sql_sort = 'ChNumber desc';
+		$list->sql_filter = function($search,&$cond){ 
+			// 如果是从大棚点击查看的
+		   $id = I('get.id');
+		   $where = array('FarmlandId' => $id);
+		   $device_ids = M('dpsb_chgroup')->field('DeviceId,Chgroup')->where($where)->select();
+		   if( $device_ids ){
+				$query_sql = '';
+		      for($i=0; $i<count($device_ids); $i++){
+		      	$query_sql .= '(DeviceId=' .$device_ids[$i]['DeviceId'] .' AND ChGroup='.$device_ids[$i]['Chgroup'].'),';
+		      }
+		      $query_sql = str_replace(',','OR',rtrim($query_sql,','));
+				$cond = array($query_sql);
+		   }else{
+				$cond['DeviceId'] = $id;
+		   }
+		    
+		};
+		$list->row_filter = function(&$row) use ($list){
+			$type = new \Zndp\Device\ChannelType;
+		   $typeinfo = $type->getTypeInfo($row['ChType']);
+			$row['ChannelType_text'] = $typeinfo[0].'('.$typeinfo[1].')';
+		   $row['ChannelGroup_text'] = '通道组'.($row['ChGroup']+1);
+		   if($row['IsUse'] == 0){
+		   	 $row['IsUse_text'] = "<span style = 'color:gray'>未使用</span>";
+		   }else{
+		   	 $row['IsUse_text'] = "<span style = 'color:green'>使用</span>";
+		   }
+		    if($row['ChMan'] == 1){
+		     $row['ChMan_text'] = "<span style = 'color:gray'>手动</span>";
+		   }else{
+		   	  $row['ChMan_text'] = "<span style = 'color:green'>自动</span>";
+		   }
+		   $row['DeviceId'] = M('dpsb_device')->where(array('ID'=>$row['DeviceId']))->getField('WorkUnitName');
+		   // 传感器名称可编辑
+		   $row['DisplayName'] = $list->renderXEditableInput('编辑传感器名称',$row,'DisplayName');
+		};
+		$list->toolbar_filter = function(&$button){
+			
+			$deviceid = I('get.id');
+			if($button['icon'] == 'add'){
+		   	  $button['url'] .= '&deviceid=' .$deviceid; //修改按钮网址
+		   }
+		};
+		$list->display($this);
+	}
+	
+ 
+	  function sys_addedit(  ){
+		$form = new \Jiaruan\LigerIframeForm();
+		$form->before_add = function(&$data){
+		  $data['DeviceId'] = I('get.deviceid');
+		  //检测IMEI是否重复
+		  $chnumber = trim($data['ChNumber']);
+		  $channel_info = M('dpsb_channel')->where(array('ChNumber'=>$chnumber))->find();
+		  if($channel_info){
+		    json_fail('该通道已存在,请重新输入!');
+		  }
+		  //判断通道组中存在某一通道类型之后不能再重复添加
+		  $ghannelgroup = trim($data['ChGroup']);
+		  $channel_info = M('dpsb_channel')->where(array('ChGroup'=>$ghannelgroup))->find();
+		  if($data['ChType'] == $channel_info['ChType']){
+		 	 json_fail('该通道类型已存在,请重新输入!');
+		  }
+		};
+		$form->before_save = function(&$data){
+		  $id = I('get.id');
+		  $chnumber = trim($data['ChNumber']);
+		  $cond = array('ID'=>$id);
+		  $userinfo = M('dpsb_channel')->where($cond)->field('ChNumber')->find();
+		  $channel_info = M('dpsb_channel')->where(array('ChNumber'=>$chnumber))->find();
+		  if($chnumber != $userinfo['ChNumber'] && $channel_info){
+		    json_fail('该通道已存在,请重新输入!');
+		  }
+		}; 
+		$form->form_filter = function(&$form){
+		   //显示下拉数据
+		  $type = new \Zndp\Device\ChannelType;
+		  $channeltype = $type->getTypeOptions();
+		  $form['fields']['ChType']['editor'] = array('options'=>$channeltype);
+		};
+		
+		$form->display($this);
+	}
+	
+ 
+	public  function sys_del(  ){
+		if (IS_AJAX) {
+			$channel_id = I('get.id');
+			if ( empty($channel_id)) {
+				json_fail('设备id不能为空');
+			}
+			if(M('dpsb_channel')->where(array( 'ID' => $channel_id ))->delete() === false){
+				json_fail('删除失败','');
+			}
+			json_success('删除成功','');
+		}
+		
+	}
+	
+ 
+	public  function test(  ){
+		var_dump(time());
+		
+	}
+	
+ 
+	  function send(  ){
+		$form = new \Jiaruan\LigerIframeForm();
+		$form->before_save = function(&$data){
+		  //$addr = M('dpsb_device')->where(array('ID'=>$data['DeviceId']))->getField('DeviceAddr');
+		  $host = '127.0.0.1';
+		  $port = 10240;
+		  $socket = new Jms\Network\TcpClient($host,$port);
+		  $cmd = array(
+		    "method" => "apiSendControl", 
+		    "Addr" => $data['DeviceAddr'],     //设备addr
+		    "channelNumber" => $data['ChNumber'],   //通道编号
+		    "status" => $data['Status'],
+		  );
+		  $res = $socket->send(json_encode($cmd)); 
+		 // var_dump($res);
+		  if($res['success']){
+		    json_success('下发成功');
+		  }else{
+		    json_fail($res['message']);
+		  }
+		};
+		$form->before_display = function(&$data){
+			$data['DeviceId_text'] = MM('dpsb_device')->where(array('ID'=>$data['DeviceId']))->getField('WorkUnitName');
+		  	$data['DeviceAddr'] = MM('dpsb_device')->where(array('ID'=>$data['DeviceId']))->getField('DeviceAddr');
+		};
+		
+		$form->display($this);
+	}
+	
+ 
+	public  function sys_start(  ){
+		if(IS_AJAX){
+			$id = I('get.id');
+			if ( empty($id)) {
+				json_fail('通道id不能为空');
+			}
+			$device_id = M('dpsb_channel')->where(array('ID'=>$id))->getField('DeviceId');
+			$ch_number = M('dpsb_channel')->where(array('ID'=>$id))->getField('ChNumber');
+			$addr = M('dpsb_device')->where(array('ID'=>$device_id))->getField('DeviceAddr');
+			$host = '127.0.0.1';
+			$port = 10240;
+			$socket = new Jms\Network\TcpClient($host,$port);
+			$cmd = array(
+				"method" => "apiSendControl", 
+				"Addr" => $addr,     //设备addr
+				"channelNumber" => $ch_number,   //通道编号
+				"status" => 1,
+			);
+			$res = $socket->send(json_encode($cmd)); 
+			if($res['success']){
+				json_success('下发成功');
+			}else{
+				json_fail($res['message']);
+			}
+		}
+	}
+	
+ 
+	public  function sys_close(  ){
+		if(IS_AJAX){
+			$id = I('get.id');
+			if ( empty($id)) {
+				json_fail('通道id不能为空');
+			}
+			$device_id = M('dpsb_channel')->where(array('ID'=>$id))->getField('DeviceId');
+			$ch_number = M('dpsb_channel')->where(array('ID'=>$id))->getField('ChNumber');
+			$addr = M('dpsb_device')->where(array('ID'=>$device_id))->getField('DeviceAddr');
+			$host = '127.0.0.1';
+			$port = 10240;
+			$socket = new Jms\Network\TcpClient($host,$port);
+			$cmd = array(
+				"method" => "apiSendControl", 
+				"Addr" => $addr,     //设备addr
+				"channelNumber" => $ch_number,   //通道编号
+				"status" => 2,
+			);
+			$res = $socket->send(json_encode($cmd)); 
+			if($res['success']){
+				json_success('下发成功');
+			}else{
+				json_fail($res['message']);
+			}
+		}
+		
+	}
+	
+ 
+	public  function sys_stop(  ){
+		if(IS_AJAX){	
+			$id = I('get.id');
+			if ( empty($id)) {
+				json_fail('通道id不能为空');
+			}
+			$device_id = M('dpsb_channel')->where(array('ID'=>$id))->getField('DeviceId');
+			$ch_number = M('dpsb_channel')->where(array('ID'=>$id))->getField('ChNumber');
+			$addr = M('dpsb_device')->where(array('ID'=>$device_id))->getField('DeviceAddr');
+			$host = '127.0.0.1';
+			$port = 10240;
+			$socket = new Jms\Network\TcpClient($host,$port);
+			$cmd = array(
+				"method" => "apiSendControl", 
+				"Addr" => $addr,     //设备addr
+				"channelNumber" => $ch_number,   //通道编号
+				"status" => 0,
+			);
+			$res = $socket->send(json_encode($cmd)); 
+			if($res['success']){
+				json_success('下发成功');
+			}else{
+				json_fail($res['message']);
+			}
+		
+		}
+	}
+	
+
+}

+ 190 - 0
Home/Lib/Action/DpsbChgroupAction.class.php

@@ -0,0 +1,190 @@
+<?php
+
+
+
+class DpsbChgroupAction extends CommonAction {
+	const tableName =  'dpsb_chgroup';	
+	const pkName =  'ID';
+	
+
+
+ 
+	  function sys_device_list(  ){
+		$list = new \Jms\Gui\ClGrid();
+		$list->sql_filter = function($search,&$cond){
+			$farmland_id = I('get.id');
+		   $cond['FarmlandId'] = $farmland_id;
+		};
+		$list->row_filter = function(&$row){
+			$where = array('ID'=>$row['DeviceId']);
+		   $fields = 'DeviceTypeId,DeviceAddr,WorkUnitName,DeviceType,DeviceImei,VersionName';
+			$device_info = M('dpsb_device')->field($fields)->where($where)->find();
+			$row = array_merge($row,$device_info);
+		   $where = array('ID'=>$row['DeviceTypeId']);
+		   $row['DeviceTypeId_text'] = M('dpsb_type')->where($where)->getField('TypeName');
+		};
+		$list->toolbar_filter = function(&$button){
+		  if($button['icon'] == 'add'){
+		    $farmland_id = I('get.id');
+		    $farm_id = I('get.farm_id');
+		    $button['url'] .= '&farm_id='.$farm_id.'&farmland_id=' .$farmland_id;
+		  }
+		};
+		$list->display($this);
+	}
+	
+ 
+	  function sys_device_addedit(  ){
+		$form = new \Jms\Gui\ClForm();
+		$form->onchange_filter = function($field,$data){
+		  $farm_id = I('get.farm_id');
+		  $result = array();
+		  if($field == 'DeviceTypeId'){
+			//获取当前基地该类型设备
+		    $result['DeviceId'] = $this->get_device_by_type($data[$field],$farm_id);
+		    if($result['DeviceId'][0]['id']){ //如果有设备
+		    	$result['ChGroup'] = $this->get_device_chgroup($result['DeviceId'][0]['id']);
+		    }else{
+		    	$result['ChGroup'] = array();
+		    }
+		  }
+		  if($field == 'DeviceId'){
+		  	$result['ChGroup'] = $this->get_device_chgroup($data[$field]);
+		  }
+		  return $result;
+		};
+		$form->before_add = function(&$data){
+		   $chgroup = M('dpsb_chgroup');
+		  	//无法绑定已经绑定的通道组
+			$where = array('ChGroup'=>$data['ChGroup'],'DeviceId'=>$data['DeviceId']);
+		  	$farmland_id = $chgroup->where($where)->getField('FarmlandId');
+		  	if( $farmland_id != -1 && $farmland_id != ''){
+		      $farmland_name = M('dpyh_farmland')->where(array('ID'=>$farmland_id))->getField('FarmlandName');
+		      json_fail('该通道组已绑定大棚:'.$farmland_name);
+		  	}
+		   //添加
+			$farmland_id = I('get.farmland_id');
+		  	$result = $chgroup->where($where)->setField('FarmlandId',$farmland_id);
+		  	if(!$result){
+		  		json_fail('绑定失败,请稍后重试');
+		  	}
+		   
+		  	// 下发大棚编号给控制台
+		  	 $where = array('ID'=>$farmland_id);
+		  	 $farmland_num = M('dpyh_farmland')->where($where)->getField('FarmlandNum');
+		  	 $where = array('ID'=>$data['DeviceId']);
+		  	 $device_addr = M('dpsb_device')->where($where)->getField('DeviceAddr');
+		    $host = '127.0.0.1';
+		    $port = 10240;
+		    $socket = new Jms\Network\TcpClient($host,$port);
+		    $cmd = array(
+		                "method" 		=> "farmlandNum", 
+		                "addr"			=> $device_addr,     // 设备addr
+		                "channelGroup"  => $data['ChGroup'],   // 通道组号
+		                "number" 		=> $farmland_num,		// 大棚编号
+		    );
+		    $send_result = $socket->send(json_encode($cmd)."\n");
+		  	 
+		    // 添加大棚id到redis(做一个同步大棚id的函数,把已经绑定的添加到Redis)
+		    $redis = Redis("dpsb_farmland_list","hash");
+		    $key = 'DeviceId-'.$data['DeviceId'].'-ChGroup-'.$data['ChGroup'];
+		    $val = $farmland_id;
+		    $hash = array($key => $val );
+		    $result = $redis->add($hash);
+		    if(!$result){
+		      json_fail('Redis缓存设置失败');
+		    }
+		  	if(!$send_result['success']){
+		  		json_fail('设置控制柜显示大棚编号失败');
+		  	}
+		  	json_success('绑定成功');
+		};
+		$form->form_filter = function(&$form){
+		  	//初始化设备类型下拉选项
+		  	$type_list = \Zndp\Device\Option::getDeviceTypeOption();
+		  	$form['fields']['DeviceTypeId']['editor'] = array('options'=>$type_list);
+		  	//初始化设备下拉选项
+		  	$farm_id = I('get.farm_id');
+			$device_list = $this->get_device_by_type($type_list[0]['id'],$farm_id);
+			$form['fields']['DeviceId']['editor'] = array('options'=>$device_list);
+		  	//取第一个设备的通道组
+			$chgroup_list = $this->get_device_chgroup($device_list[0]['id']);
+		  	$form['fields']['ChGroup']['editor'] = array('options'=>$chgroup_list);
+		};
+		$form->display($this);
+	}
+	
+ 
+	public  function get_device_by_type( $type_id, $farm_id = '' ){
+		if(!$type_id){ 
+			json_fail('设备类型不能为空');
+		}
+		// 初始化查询条件
+		$where = array('DeviceTypeId'=>$type_id);
+		if($farm_id){ // 只查找当前基地的设备
+			$cond = array('FarmId'=>$farm_id);
+			$device_groups = M('dpsb_group')->field('ID')->where($cond)->select();
+			$device_groups = array_column($device_groups,'ID');
+			$where['DeviceGroupId'] = array('IN',$device_groups);
+		}
+		$device_list = M('dpsb_device')->field('ID as id,WorkUnitName as text')->where($where)->order('ID DESC')->select();
+		if(empty($device_list)){
+			$device_list = array();
+			$first_option = array('id' => '','text' => '无设备');
+			array_unshift($device_list,$first_option);
+		}else{
+			$first_option = array('id' => '','text' => '--请选择设备--');
+			array_unshift($device_list,$first_option);
+		}
+		return $device_list;
+	}
+	
+ 
+	public  function get_device_chgroup( $device_id ){
+		$where = array('DeviceId'=>$device_id,'FarmlandId'=>-1);//显示该设备未绑定的通道
+		$chgroup_list = M('dpsb_chgroup')->field('ChGroup as id,ChGroup as text')->where($where)->select();
+		if(empty($chgroup_list)){
+			$chgroup_list = array();
+			$first_option = array('id' => '','text' => '无可用通道组');
+			array_unshift($chgroup_list,$first_option);
+		}else{
+			$first_option = array('id' => '','text' => '--请选择通道--');
+			array_unshift($chgroup_list,$first_option);
+		}
+		return $chgroup_list;
+	}
+	
+ 
+	  function sys_unbind(  ){
+		$id = I('get.id');
+		$where = array('ID'=>$id);
+		$device_id = M('dpsb_chgroup')->where($where)->getField('DeviceId');
+		
+		$form = new \Jms\Gui\ClForm();
+		$form->before_display = function(&$data) use($device_id) {
+		  	$where = array('ID'=>$device_id);
+			$data['WorkUnitName'] = M('dpsb_device')->where($where)->getField('WorkUnitName');
+		  
+		};
+		$form->before_save = function(&$data) use($device_id) {
+		  //解绑设备
+		  $id = I('get.id');
+		  $where = array('ID'=>$id);
+		  $result = M('dpsb_chgroup')->where($where)->setField('FarmlandId',-1);
+		  if(!$result){
+		    json_fail('解绑失败');
+		  }
+		  // 删除Redis中的记录
+		  $redis = Redis("dpsb_farmland_list","hash");
+		  $key = 'DeviceId-'.$device_id .'-ChGroup-'.$data['ChGroup'];
+		  $result = $redis->where($key)->delete();
+		  if(!$result){
+		  	json_fail('解绑成功,但Redis缓存删除失败');
+		  }
+		  json_success('解绑成功');
+		};
+		$form->display($this);
+	}
+	
+
+}

+ 44 - 0
Home/Lib/Action/DpsbDevice2Action.class.php

@@ -0,0 +1,44 @@
+<?php
+
+
+
+class DpsbDevice2Action extends CommonAction {
+	const tableName =  'dpsb_device2';	
+	const pkName =  'ID';
+	
+
+
+ 
+	  function set_screen_msg2(  ){
+		$_GET['id'] = 0;
+		
+		//根据设备地址获取id信息
+		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);
+	}
+	
+
+}

+ 689 - 0
Home/Lib/Action/DpsbDeviceAction.class.php

@@ -0,0 +1,689 @@
+<?php
+
+
+
+class DpsbDeviceAction extends CommonAction {
+	const tableName =  'dpsb_device';	
+	const pkName =  'ID';
+	
+
+
+ 
+	  function sys_addedit_old(  ){
+		$form = new \Jiaruan\LigerIframeForm();
+		$form->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(' ','<br/>',$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 '
+		<style>
+		::-webkit-input-placeholder { /* WebKit, Blink, Edge */    color:#ccc; }
+		:-moz-placeholder { /* Mozilla Firefox 4 to 18 */   color:#ccc; }
+		::-moz-placeholder { /* Mozilla Firefox 19+ */   color:#ccc; }
+		:-ms-input-placeholder { /* Internet Explorer 10-11 */   color:#ccc; }
+		</style>';
+	}
+	
+ 
+	  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; $i<count($depart_list); $i++){// 放入链接
+				if($depart_list[$i]['level'] <= \Zndp\User\DepartEnum::WORK_STATION){
+					$depart_list[$i]['open']= true;
+				}			
+				$depart_list[$i]['direct_url'] = '?s=dpsb_device/sys_list';
+			}
+		}
+		//平台,超级管理员,插入所有用户列表
+		$admin_arr = array(\Zndp\User\RoleEnum::GENERAL_ADMINISTRATOR,\Zndp\User\RoleEnum::SUPER_ADMINISTRATOR);
+		if( in_array($this->userinfo['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(' ','<br/>',$row['IccId']);
+		   // 是否在线
+		   if( time() - strtotime($row['OnlineTime']) < 180 ){//超过3分钟算离线
+		     $row['OnlineTime_text'] = "<font color='green'>在线</font>";
+		   }else{
+				$row['OnlineTime_text'] = "<font color='red'>离线</font>";
+		   }
+		   // 是否安装
+		   $where = array('DeviceId'=>$row['ID'],'FarmlandId'=>array('gt',-1));
+		   $count = M('dpsb_chgroup')->where($where)->count();
+		   if($count > 0){
+		   		$row['InstallState'] = "<font color='green'>已安装</font>";
+		   }else{
+		   		$row['InstallState'] = "<font color='red'>未安装</font>";
+		   }
+		   //设备已关联田地
+		   $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.'<br/>';
+		   }
+		   $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(' ','<br/>',$row['IccId']);
+		   if( time() - strtotime($row['OnlineTime']) < 180 ){//超过3分钟算离线
+		     $row['OnlineTime_text'] = "<font color='green'>在线</font>";
+		   }else{
+				$row['OnlineTime_text'] = "<font color='red'>离线</font>";
+		   }
+		   $where = array('DeviceId'=>$row['ID'],'FarmlandId'=>array('gt',-1));
+		   $count = M('dpsb_chgroup')->where($where)->count();
+		   if($count > 0){
+		   		$row['InstallState'] = "<font color='green'>已安装</font>";
+		   }else{
+		   		$row['InstallState'] = "<font color='red'>未安装</font>";
+		   }
+		   //设备已关联田地
+		   $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.'<br/>';
+		   }
+		   $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);
+	}
+	
+
+}

+ 126 - 0
Home/Lib/Action/DpsbGroupAction.class.php

@@ -0,0 +1,126 @@
+<?php
+
+
+
+class DpsbGroupAction extends CommonAction {
+	const tableName =  'dpsb_group';	
+	const pkName =  'ID';
+	
+
+
+ 
+	  function sys_list(  ){
+		$list = new \Jms\Gui\ClGrid();
+		$list->sql_sort = 'ID 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){ 
+			//显示登录角色部门下属所有设备组
+		   if($this->userinfo['OrganizationId']){ //非管理员
+				$base_ids = \Zndp\User\Util::get_depart_farm($this->userinfo['OrganizationId']);
+		      $cond['FarmId'] = array('IN',$base_ids);
+		   }
+		};
+		$list->row_filter = function(&$row) use ($list){
+		   $row['FarmId_text'] = MM('dpyh_farm')->where(array('ID'=>$row['FarmId']))->getField('FarmName');
+		   //计算设备组包含的设备数量
+		   $where = array('DeviceGroupId' => $row['ID'] );
+		   $row['DeviceCount'] = M('dpsb_device')->where($where)->count();
+		};
+		
+		$list->display($this);
+	}
+	
+ 
+	  function sys_addedit(  ){
+		$form = new \Jiaruan\LigerIframeForm();
+		$form->before_add = function(&$data){
+		  //检测设备组名是否重复
+		  $org_name = trim($data['OrgName']);
+		  $org_info = M('dpsb_group')->where(array('OrgName'=>$org_name))->find();
+		  if($org_info){
+		    json_fail('该设备组名已存在,请重新输入!');
+		  }
+		};
+		$form->before_modify = function(&$data){
+		  $id = I('get.id');
+		  $org_name = trim($data['OrgName']);
+		  $cond = array('ID'=>$id);
+		  $group_info = M('dpsb_group')->where($cond)->field('OrgName')->find();
+		  $org_info = M('dpsb_group')->where(array('OrgName'=>$org_name))->find();
+		  if($org_name != $group_info['OrgName'] && $org_info){
+		    json_fail('该设备组名已存在,请重新输入!');
+		  }
+		}; 
+		$form->form_filter = function(&$form){
+		   //显示用户部门下属所有基地
+		  if($this->userinfo['OrganizationId']){
+		  	$zndp_util = new \Zndp\User\Util();
+		  	$depart_list = $zndp_util->get_depart_farm_options($this->userinfo['OrganizationId']);
+		  }else{
+		  	$type = new \Zndp\Device\Option;
+		  	$depart_list = $type->getFarmOption();
+		  }
+		  $form['fields']['FarmId']['editor'] = array('options'=>$depart_list);
+		};
+		$form->display($this);
+	}
+	
+ 
+	public  function sys_del(  ){
+		$grid = new \Jiaruan\GridData();
+		$grid->before_delete = function($data){
+			$where = array('DeviceGroupId'=>$data['ID']);
+			$count = M('dpsb_device')->where($where)->count();
+			if($count > 0){
+				json_fail('设备组下还有设备,无法删除');
+			}
+		};
+		$grid->deleteByPk($this);
+		
+	}
+	
+ 
+	  function import_device(  ){
+		if(!I('get.id')){
+			exit('missing params');
+		}
+		$form = new \Jms\Gui\ClForm();
+		$form->before_save = function(&$data){
+		  $device_addr = trim($data['DeviceAddr']);
+		  $where = array( 'DeviceAddr'=>$device_addr);
+		  $dpsb_device = M('dpsb_device');
+		  $device_info = $dpsb_device->field('DeviceGroupId')->where($where)->find();
+		  if( !$device_info ){
+		  	json_fail('设备序列号:'.$device_addr . ' 未上报');
+		  }
+		  if( $device_info['DeviceGroupId'] > 0 ){
+		  	json_fail('该设备已有设备组');
+		  }
+		  $save_data = array(
+		    'DeviceGroupId' => I('get.id'),
+		    'DeviceTypeId' => $data['DeviceTypeId'],
+		  	 'WorkUnitName' => $data['WorkUnitName']
+		  );
+		  $result = $dpsb_device->createSave($where,$save_data);
+		  if($result === false){
+		  	json_fail('导入失败');
+		  }else{
+			json_success('导入成功');  
+		  }
+		  
+		}; 
+		$form->form_filter = function(&$form){
+		  //显示设备类型
+		  $type_list = \Zndp\Device\Option::getDeviceTypeOption();
+		  $form['fields']['DeviceTypeId']['editor'] = array('options'=>$type_list);
+		};
+		
+		
+		$form->display($this);
+	}
+	
+
+}

+ 52 - 0
Home/Lib/Action/DpsbListNavAction.class.php

@@ -0,0 +1,52 @@
+<?php
+class DpsbListNavAction extends Action {
+	/*
+
+	*/
+	 function sys_list_nav(){
+		//菜单组
+		$menus = array(
+			array('id'=>'device','text'=>'设备管理','expand'=>true),
+			array('pid'=>'device','text' => '新添设备','url' => 'dpsb_device/sys_add_list','active'=>true),
+			array('pid'=>'device','text' => '设备列表','url' => 'dpsb_device/sys_list'),
+			array('pid'=>'device','text' => '设备组列表','url' => 'dpsb_group/sys_list'),
+			//array('pid'=>'device','text' => '决策列表','url' => 'dpsb_decision/sys_list'),
+		);
+		
+		//导航树配置数据
+		$data = array(
+			'page' => '设备管理',
+			'title' => '设备管理',
+			'menus' => $menus
+		);
+		//生成导航树
+		$nav = new \Jiaruan\ClNavTree;
+		$nav->setData($data);
+		$nav->display($this);
+	}
+	/*
+
+	*/
+	 function sys_list_nav_clone(){
+		//菜单组
+		$menus = array(
+			array('id'=>'device','text'=>'设备管理','expand'=>true),
+			array('pid'=>'device','text' => '新添设备','url' => 'dpsb_device/sys_add_list','active'=>true),
+			array('pid'=>'device','text' => '设备列表','url' => 'dpsb_device/sys_list'),
+			array('pid'=>'device','text' => '设备组列表','url' => 'dpsb_group/sys_list'),
+			//array('pid'=>'device','text' => '决策列表','url' => 'dpsb_decision/sys_list'),
+		);
+		
+		//导航树配置数据
+		$data = array(
+			'page' => '设备管理',
+			'title' => '设备管理',
+			'menus' => $menus
+		);
+		//生成导航树
+		$nav = new \Jiaruan\ClNavTree;
+		$nav->setData($data);
+		$nav->display($this);
+	}
+
+}

+ 596 - 0
Home/Lib/Action/DpsbPolicyAction.class.php

@@ -0,0 +1,596 @@
+<?php
+
+
+
+class DpsbPolicyAction extends CommonAction {
+	const tableName =  'dpsb_policy';	
+	const pkName =  'ID';
+	
+
+
+ 
+	  function sys_device_list(  ){
+		$list = new \Jms\Gui\ClGrid();
+		$list->sql_sort = 'DeviceId desc';
+		$list->sql_filter = function($search,&$cond){ 
+			$cond['DeviceId'] = I('get.id');
+		};
+		$list->row_filter = function(&$row) use ($list){
+		    for($i=1;$i<=5;$i++){
+		      $ontime = 'OnTime'.$i;
+		      $offtime = 'OffTime'.$i;
+		      $row[$ontime] = $this->timeToStr($row[$ontime]);
+		      $row[$offtime] = $this->timeToStr($row[$offtime]);
+		    }
+		    $device_id = I('get.id');
+		    $where = array(
+		    	'DeviceId' => $device_id,
+		       'ChNumber' => $row['Channel']
+		    );
+		    $cond = array(
+		    	'DeviceId' => $device_id,
+		       'ChNumber' => $row['RefChannel']
+		    );
+		    $row['DeviceId_text'] = M('dpsb_device')->where(array('ID'=>$row['DeviceId']))->getField('WorkUnitName');
+		    $channel_list = M('dpsb_channel')->where($where)->find();
+		    $refchannel_list = M('dpsb_channel')->where($cond)->find();
+		    
+		   	$row['Channel'] = $channel_list['DisplayName'];
+		   	$row['RefChannel'] = $refchannel_list['DisplayName'];
+		
+		    
+		    if($row['Man'] == 0){
+		    	$row['Man'] = "<span style = 'color:green'>自动</span>";
+		    }else{
+		    	$row['Man'] = "<span style = 'color:gray'>手动</span>";
+		    }
+		    if($row['SendResult'] == 0){
+		    	$row['SendResult'] = "<span style = 'color:green'>成功</span>";
+		    }
+		};
+		$list->toolbar_filter = function(&$button){
+			
+			$deviceid = I('get.id');
+			if($button['icon'] == 'add'){
+		   	  $button['url'] .= '&deviceid=' .$deviceid; //修改按钮网址
+		   }
+		};
+		
+		$list->display($this);
+	}
+	
+ 
+	  function sys_addedit(  ){
+		$form = new \Jiaruan\LigerIframeForm();
+		$form->before_add = function(&$data){
+		  $data['DeviceId'] = I('get.deviceid');
+		  for($i=1;$i<=5;$i++){
+		    $ontime = 'OnTime'.$i;
+		    $offtime = 'OffTime'.$i;
+		    $patten = "/^([0-5]\d):([0-5]\d)$/";
+		    //$patten = "(s((([0-1]?[0-9])|([1-2][0-3])):([0-5]?[0-9])))?$"
+		    if($data[$ontime]){
+		      if(!preg_match($patten, $data[$ontime])){
+		        json_fail($ontime .'时间格式不正确');
+		      }else{
+		        $data[$ontime] = $this->strToTime($data[$ontime]);
+		      }
+		    }else{
+		    	$data[$ontime] = -1;
+		    }
+		    if($data[$offtime]){
+		      if(!preg_match($patten, $data[$offtime])){
+		        json_fail($offtime .'时间格式不正确');
+		      }else{
+		        $data[$offtime] = $this->strToTime($data[$offtime]);
+		      }
+		    }else{
+		    	$data[$offtime] = -1;
+		    }
+		  }
+		  $addr = M('dpsb_device')->where(array('ID'=>$data['DeviceId']))->getField('DeviceAddr');
+		  $host = '127.0.0.1';
+		  $port = 10240;
+		  $socket = new Jms\Network\TcpClient($host,$port);
+		  $msg = array(
+		    'method' => 'apiModifyAutoControlInfo',
+		    'Addr' => $addr,
+		    'channelNumber' => $data['Channel'],
+		    'man' => $data['Man'],
+		    'refChannel' => $data['RefChannel'],
+		    'TimeEn' => $data['IsTime'],
+		    'UpperLimit_Value' => $data['UpperLimitValue'],
+		    'UpperLimit_State' => $data['UpperLimitState'],
+		    'LowerLimit_Value' => $data['LowerLimitValue'],
+		    'LowerLimit_State' => $data['LowerLimitState'],
+		    'OnTime1'  => $data['OnTime1'],
+		    'OffTime1' => $data['OffTime1'],
+		    'OnTime2'  => $data['OnTime2'],
+		    'OffTime2' => $data['OffTime2'],
+		    'OnTime3'  => $data['OnTime3'],
+		    'OffTime3' => $data['OffTime3'],
+		    'OnTime4'  => $data['OnTime4'],
+		    'OffTime4' => $data['OffTime4'],
+		    'OnTime5'  => $data['OnTime5'],
+		    'OffTime5' => $data['OffTime5'],
+		    'IssueTime' => $data['IssueTime'],
+		  );
+		  
+		  $res = $socket->send(json_encode($msg)); 
+		  if($res['success']){
+		  	json_success('下发成功');
+		  }else{
+		  	 json_fail($res['message']);
+		  }
+		};
+		$form->before_display = function(&$data){
+		  $deviceid = I('get.deviceid');
+		  $data['IssueTime'] = 5;
+		  $data['DeviceName'] = MM('dpsb_device')->where(array('ID'=>$deviceid))->getField('WorkUnitName');
+		  $data['Man'] = 1;
+		  $data['IsTime'] = 0;
+		  $data['UpperLimitState'] = 0;
+		  $data['LowerLimitState'] = 0;
+		  
+		  for($i=1;$i<=5;$i++){
+		    $ontime = 'OnTime'.$i;
+		    $offtime = 'OffTime'.$i;
+		    $data[$ontime] = $this->timeToStr($data[$ontime]);
+		    $data[$offtime] = $this->timeToStr($data[$offtime]);
+		  }
+		};
+		$form->before_save = function(&$data){
+		  $data['DeviceId'] = I('get.deviceid');
+		  for($i=1;$i<=5;$i++){
+		    $ontime = 'OnTime'.$i;
+		    $offtime = 'OffTime'.$i;
+		    $patten = "/^([0-5]\d):([0-5]\d)$/";
+		    if($data[$ontime]){
+		      if(!preg_match($patten, $data[$ontime])){
+		        json_fail($ontime .'时间格式不正确');
+		      }else{
+		        $data[$ontime] = $this->strToTime($data[$ontime]);
+		      }
+		    }else{
+		    	$data[$ontime] = -1;
+		    }
+		    if($data[$offtime]){
+		      if(!preg_match($patten, $data[$offtime])){
+		        json_fail($offtime .'时间格式不正确');
+		      }else{
+		        $data[$offtime] = $this->strToTime($data[$offtime]);
+		      }
+		    }else{
+		    	$data[$offtime] = -1;
+		    }
+		  }
+		  $addr = M('dpsb_device')->where(array('ID'=>$data['DeviceId']))->getField('DeviceAddr');
+		  $host = '127.0.0.1';
+		  $port = 10240;
+		  $socket = new Jms\Network\TcpClient($host,$port);
+		  $msg = array(
+		    'method' => 'apiModifyAutoControlInfo',
+		    'Addr' => $addr,
+		    'channelNumber' => $data['Channel'],
+		    'man' => $data['Man'],
+		    'refChannel' => $data['RefChannel'],
+		    'TimeEn' => $data['IsTime'],
+		    'UpperLimit_Value' => $data['UpperLimitValue'],
+		    'UpperLimit_State' => $data['UpperLimitState'],
+		    'LowerLimit_Value' => $data['LowerLimitValue'],
+		    'LowerLimit_State' => $data['LowerLimitState'],
+		    'OnTime1'  => $data['OnTime1'],
+		    'OffTime1' => $data['OffTime1'],
+		    'OnTime2'  => $data['OnTime2'],
+		    'OffTime2' => $data['OffTime2'],
+		    'OnTime3'  => $data['OnTime3'],
+		    'OffTime3' => $data['OffTime3'],
+		    'OnTime4'  => $data['OnTime4'],
+		    'OffTime4' => $data['OffTime4'],
+		    'OnTime5'  => $data['OnTime5'],
+		    'OffTime5' => $data['OffTime5'],
+		    'IssueTime' => $data['IssueTime'],
+		  );
+		  $res = $socket->send(json_encode($msg)); 
+		  if($res['success']){
+		  	json_success('下发成功');
+		  }else{
+		  	 json_fail($res['message']);
+		  }
+		}; 
+		$form->form_filter = function(&$form){
+		  //显示下拉数据
+		  $deviceid = I('get.deviceid');
+		  $type = new \Zndp\Device\Option;
+		  $controlchannel = $type->getControlChannelOption($deviceid);
+		  $channel = $type->getATypeChannelOption($deviceid);
+		  $form['fields']['Channel']['editor'] = array('options'=>$controlchannel);
+		  $form['fields']['RefChannel']['editor'] = array('options'=>$channel);
+		};
+		
+		
+		$form->display($this);
+	}
+	
+ 
+	public  function strToTime( $time ){
+		$array = explode(':',$time);
+		$data = $array[0]*60+$array[1];
+		return $data;
+	}
+	
+ 
+	public  function timeToStr( $time ){
+		if($time == -1){
+			$data = '';
+		}else{
+			$minute = $time%60;
+			if($minute<10){
+				$minute = '0'.$minute;
+			}
+			$hour = ($time-$minute)/60;
+			if($hour<10){
+				$hour = '0'.$hour;
+			}
+			$data = $hour.':'.$minute;
+		}
+		return $data;
+	}
+	
+ 
+	  function sys_list(  ){
+		$list = new \Jms\Gui\ClGrid();
+		$list->sql_sort = 'DeviceId desc';
+		$list->sql_filter = function($search,&$cond){ 
+		  //显示登录用户权限下所有策略
+		  if($this->userinfo['OrganizationId']){ //非平台管理员
+		    //基地管理员获取自己的策略
+		    if( $this->userinfo['RoleId'] == \Zndp\User\RoleEnum::BASE_ADMINISTRATOR ){
+		    	$where = array('ManagerId'=>$this->userid);
+		    }else{
+		      //否则获取登录角色下属所有基地管理员的操作记录
+		      $base_ids = \Zndp\User\Util::get_depart_farm($this->userinfo['OrganizationId']);
+		      $where = array('FarmId'=>array('IN',$base_ids));
+		    }
+		    $farmland_ids = MM('dpyh_farmland')->field('ID')->where($where)->select();
+		    $farmland_ids = array_column($farmland_ids,'ID');
+		    $cond['FarmlandId'] = array('IN',$farmland_ids);
+		  }
+		};
+		$list->row_filter = function(&$row) use ($list){
+		    for($i=1;$i<=5;$i++){
+		      $ontime = 'OnTime'.$i;
+		      $offtime = 'OffTime'.$i;
+		      $row[$ontime] = $this->timeToStr($row[$ontime]);
+		      $row[$offtime] = $this->timeToStr($row[$offtime]);
+		    }
+		    $where = array(
+		    	 'DeviceId' => $row['DeviceId'],
+		       'ChNumber' => $row['Channel']
+		    );
+		    $cond = array(
+		     	'DeviceId' => $row['DeviceId'],
+		       'ChNumber' => $row['RefChannel']
+		    );
+		    $row['DeviceId_text'] = M('dpsb_device')->where(array('ID'=>$row['DeviceId']))->getField('WorkUnitName');
+		    $channel_list = M('dpsb_channel')->where($where)->find();
+		    $refchannel_list = M('dpsb_channel')->where($cond)->find();
+		    
+		   	$row['Channel'] = $channel_list['DisplayName'];
+		   	$row['RefChannel'] = $refchannel_list['DisplayName'];
+		
+		    if($row['Man'] == 0){
+		    	$row['Man'] = "<span style = 'color:green'>自动</span>";
+		    }else{
+		    	$row['Man'] = "<span style = 'color:gray'>手动</span>";
+		    }  
+		    if($row['SendResult'] == 0){
+		    	$row['SendResult'] = "<span style = 'color:green'>成功</span>";
+		    }
+		    $row['IsTime_text'] = $row['IsTime'] ? '是' : '否';
+		};
+		$list->toolbar_filter = function(&$button){
+			
+			$deviceid = I('get.id');
+			if($button['icon'] == 'add'){
+		   	  $button['url'] .= '&deviceid=' .$deviceid; //修改按钮网址
+		   }
+		};
+		$list->display($this);
+	}
+	
+ 
+	public  function sys_del(  ){
+		if (IS_AJAX) {
+			$group_id = I('get.id');
+			if ( empty($group_id)) {
+				json_fail('决策id不能为空');
+			}
+			if(M('dpsb_policy')->where(array( 'ID' => $group_id ))->delete() === false){
+				json_fail('删除失败','');
+			}
+			json_success('删除成功','');
+		}
+		
+	}
+	
+ 
+	  function test(  ){
+		
+	}
+	
+ 
+	  function sys_addedit_dp(  ){
+		$form = new \Jms\Gui\ClForm();
+		$form->before_display = function(&$data){
+		  $farmland_id = I('get.farmland_id');
+		  $data['FarmlandName'] = MM('dpyh_farmland')->where(array('ID'=>$farmland_id))->getField('FarmlandName');
+		  $data['IssueTime'] = $data['IssueTime'] ? : 5;  //自动控制时间(秒)
+		  $data['Man'] = isset($data['Man']) ? $data['Man'] : 1; 	// 手动控制
+		  $data['UpperLimitState'] = $data['UpperLimitState'] ? : 0;//不启用上限
+		  $data['LowerLimitState'] = $data['LowerLimitState'] ? : 0;//不启用下限
+		  $data['IsTime'] = $data['IsTime'] ? : 0; 	  // 关闭定时控制
+		  // 定时控制时间
+		  for($i=1;$i<=5;$i++){
+		    $ontime = 'OnTime'.$i;
+		    $offtime = 'OffTime'.$i;
+		    $data[$ontime] = $this->timeToStr($data[$ontime]);
+		    $data[$offtime] = $this->timeToStr($data[$offtime]);
+		  }
+		};
+		  $form->before_add = function(&$data){
+		  $data['DeviceId'] = I('get.deviceid');
+		  $data['FarmlandId'] = I('get.farmland_id');
+		  $data['UpperLimitValue'] = floatval($data['UpperLimitValue']);
+		  $data['LowerLimitValue'] = floatval($data['LowerLimitValue']);
+		  for($i=1;$i<=5;$i++){
+		    $ontime = 'OnTime'.$i;
+		    $offtime = 'OffTime'.$i;
+		    // 00:00 - 23:59
+		    $patten = "/^(([0-1]\d)|([2][0-3])):[0-5]\d$/";
+		    if($data[$ontime]){
+		      if(!preg_match($patten, $data[$ontime])){
+		      	json_fail($ontime .'时间格式不正确');
+		      }else{
+		        $data[$ontime] = $this->strToTime($data[$ontime]);
+		      }
+		    }else{
+		      $data[$ontime] = -1;
+		    }
+		    if($data[$offtime]){
+		      if(!preg_match($patten, $data[$offtime])){
+		        json_fail($offtime .'时间格式不正确');
+		      }else{
+		        $data[$offtime] = $this->strToTime($data[$offtime]);
+		      }
+		    }else{
+		      $data[$offtime] = -1;
+		    }
+		  }
+		  //如果设备id为DeviceId,通道号为channelNumber的策略已存在,则修改
+		  $where = array(
+		        'DeviceId'=> $data['DeviceId'],
+		        'Channel'=> $data['Channel'],
+		       );
+		  $policy_id = M('dpsb_policy')->where($where)->getField('ID');
+		  if( $policy_id ){
+		    $where = array('ID'=>$policy_id);
+		    $result = M('dpsb_policy')->createSave($where,$data);
+		    if( $result === false ){
+		      json_fail('添加失败,请稍后再试');
+		    }
+		  }
+		  // 发送控制策略
+		  $addr = M('dpsb_device')->where(array('ID'=>$data['DeviceId']))->getField('DeviceAddr');
+		  $host = '127.0.0.1';
+		  $port = 10240;
+		  $socket = new Jms\Network\TcpClient($host,$port);
+		  $msg = array(
+		  	 'method' => 'apiModifyAutoControlInfo',
+		    'Addr' => $addr,
+		    'channelNumber' => $data['Channel'],
+		    'man' => $data['Man'],
+		    'refChannel' => $data['RefChannel'],
+		    'TimeEn' => $data['IsTime'],
+		    'UpperLimit_Value' => $data['UpperLimitValue'],
+		    'UpperLimit_State' => $data['UpperLimitState'],
+		    'LowerLimit_Value' => $data['LowerLimitValue'],
+		    'LowerLimit_State' => $data['LowerLimitState'],
+		    'OnTime1'  => $data['OnTime1'],
+		    'OffTime1' => $data['OffTime1'],
+		    'OnTime2'  => $data['OnTime2'],
+		    'OffTime2' => $data['OffTime2'],
+		    'OnTime3'  => $data['OnTime3'],
+		    'OffTime3' => $data['OffTime3'],
+		    'OnTime4'  => $data['OnTime4'],
+		    'OffTime4' => $data['OffTime4'],
+		    'OnTime5'  => $data['OnTime5'],
+		    'OffTime5' => $data['OffTime5'],
+		    'IssueTime' => $data['IssueTime'],
+		    );
+		  $res = $socket->send(json_encode($msg));
+		  $data['SendTime'] = date('Y-m-d H:i:s'); 
+		  //添加到dpsj_policy日志表中
+		  $data['SendResult'] = $res['message'];
+		  $result = M('dpsj_policy')->createAdd($data);
+		  if($res['success']){
+		    if($result !== false){
+		      $msg = '下发成功,策略日志添加成功';
+		    }else{
+		      $msg = '下发成功,策略日志添加失败';
+		    }
+		    json_success($msg);
+		  }else{
+		    if($result !== false){
+		      $msg = '下发失败,策略日志添加成功';
+		    }else{
+		      $msg = '下发失败,策略日志添加失败';
+		      }
+		    json_fail($msg);
+		  }
+		};
+		$form->before_modify = function(&$data){
+		  $data['DeviceId'] = I('get.deviceid');
+		  $data['FarmlandId'] = I('get.farmland_id');
+		  $data['UpperLimitValue'] = floatval($data['UpperLimitValue']);
+		  $data['LowerLimitValue'] = floatval($data['LowerLimitValue']);
+		  for($i=1;$i<=5;$i++){
+		    $ontime = 'OnTime'.$i;
+		    $offtime = 'OffTime'.$i;
+		    $patten = "/^([0-5]\d):([0-5]\d)$/";
+		    if($data[$ontime]){
+		      if(!preg_match($patten, $data[$ontime])){
+		      json_fail($ontime .'时间格式不正确');
+		    }else{
+		      $data[$ontime] = $this->strToTime($data[$ontime]);
+		    }
+		    }else{
+		      $data[$ontime] = -1;
+		    }
+		    if($data[$offtime]){
+		      if(!preg_match($patten, $data[$offtime])){
+		        json_fail($offtime .'时间格式不正确');
+		      }else{
+		        $data[$offtime] = $this->strToTime($data[$offtime]);
+		      }
+		    }else{
+		      $data[$offtime] = -1;
+		    }
+		  }
+		};
+		$form->after_modify = function($data){
+		  // 发送控制策略
+		  $addr = M('dpsb_device')->where(array('ID'=>$data['DeviceId']))->getField('DeviceAddr');
+		  $host = '127.0.0.1';
+		  $port = 10240;
+		  $socket = new Jms\Network\TcpClient($host,$port);
+		  $msg = array(
+		  	 'method' => 'apiModifyAutoControlInfo',
+		    'Addr' => $addr,
+		    'channelNumber' => $data['Channel'],
+		    'man' => $data['Man'],
+		    'refChannel' => $data['RefChannel'],
+		    'TimeEn' => $data['IsTime'],
+		    'UpperLimit_Value' => $data['UpperLimitValue'],
+		    'UpperLimit_State' => $data['UpperLimitState'],
+		    'LowerLimit_Value' => $data['LowerLimitValue'],
+		    'LowerLimit_State' => $data['LowerLimitState'],
+		    'OnTime1'  => $data['OnTime1'],
+		    'OffTime1' => $data['OffTime1'],
+		    'OnTime2'  => $data['OnTime2'],
+		    'OffTime2' => $data['OffTime2'],
+		    'OnTime3'  => $data['OnTime3'],
+		    'OffTime3' => $data['OffTime3'],
+		    'OnTime4'  => $data['OnTime4'],
+		    'OffTime4' => $data['OffTime4'],
+		    'OnTime5'  => $data['OnTime5'],
+		    'OffTime5' => $data['OffTime5'],
+		    'IssueTime' => $data['IssueTime'],
+		    );
+		  $res = $socket->send(json_encode($msg)); 
+		  $data['SendTime'] = date('Y-m-d H:i:s');
+		  //添加到dpsj_policy日志表中
+		  $data['SendResult'] = $res['message'];
+		  unset($data['ID']);
+		  $result = M('dpsj_policy')->createAdd($data);
+		
+		  if($res['success']){
+		    if($result !== false){
+		      $msg = '下发成功,策略日志添加成功';
+		    }else{
+		      $msg = '下发成功,策略日志添加失败';
+		    }
+		    json_success($msg);
+		  }else{
+		    if($result !== false){
+		      $msg = '下发失败,策略日志添加成功';
+		    }else{
+		      $msg = '下发失败,策略日志添加失败';
+		    }
+		    json_fail($msg);
+		  }
+		}; 
+		  $form->form_filter = function(&$form){
+		  $deviceid = I('get.deviceid');
+		  $chgroup = I('get.chgroup');
+		  $type = new \Zndp\Device\Option;
+		  $controlchannel = $type->getControlChannelOption($deviceid,$chgroup);
+		  $form['fields']['Channel']['editor'] = array('options'=>$controlchannel);//初始化控制通道
+		  $channel = $type->getATypeChannelOption($deviceid,$chgroup);
+		  $form['fields']['RefChannel']['editor'] = array('options'=>$channel); //初始化参考通道
+		};
+		  $form->display($this);
+	}
+	
+ 
+	  function sys_farmland_list(  ){
+		$farmland_id = I('get.farmland_id');
+		$where = array('FarmlandId'=>$farmland_id);
+		$chgroup_info = M('dpsb_chgroup')->field('DeviceId,ChGroup,FarmlandId')->where($where)->find();
+		
+		$list = new \Jms\Gui\ClGrid();
+		$list->sql_sort = 'SendTime desc';
+		$list->sql_filter = function($search,&$cond)use($chgroup_info){ 
+		   //显示绑定该大棚的设备组中的通道策略
+		   $where = array('DeviceId'=>$chgroup_info['DeviceId'],'ChGroup'=>$chgroup_info['ChGroup']);
+		   $channel_list = M('dpsb_channel')->field('ChNumber')->where($where)->select();
+		   $channels = array_column($channel_list,'ChNumber');
+		   $cond['DeviceId'] = $chgroup_info['DeviceId'];
+		   $cond['Channel'] = array('IN',$channels);
+		};
+		$list->row_filter = function(&$row) use ($list,$chgroup_info){
+			$row['FarmlandId'] = $chgroup_info['FarmlandId'];//传给添加,编辑策略表单
+		   $row['ChGroup'] = $chgroup_info['ChGroup'];//传给添加,编辑策略表单
+		    for($i=1;$i<=5;$i++){
+		      $ontime = 'OnTime'.$i;
+		      $offtime = 'OffTime'.$i;
+		      $row[$ontime] = $this->timeToStr($row[$ontime]);
+		      $row[$offtime] = $this->timeToStr($row[$offtime]);
+		    }
+		    //设备名称
+		    $row['DeviceId_text'] = M('dpsb_device')->where(array('ID'=>$row['DeviceId']))->getField('WorkUnitName');
+		    //控制通道名称
+		    $where = array(
+		    	'DeviceId' => $row['DeviceId'],
+		       'ChNumber' => $row['Channel']
+		    );
+		    $dpsb_channel = M('dpsb_channel');
+		    $row['Channel_text'] = $dpsb_channel->where($where)->getField('DisplayName');
+		    //参考通道名称
+		    $cond = array(
+		    	'DeviceId' => $row['DeviceId'],
+		       'ChNumber' => $row['RefChannel']
+		    );
+		    $row['RefChannel_text'] = $dpsb_channel->where($cond)->getField('DisplayName');
+		    // 上限参考状态
+		    $row['UpperLimitState_text'] = $row['UpperLimitState'] ? '启用' : '停用';
+		    // 下限参考状态
+		    $row['LowerLimitState_text'] = $row['LowerLimitState'] ? '启用' : '停用';
+			//控制状态
+		    if($row['Man'] == 0){
+		    	$row['Man'] = "<span style = 'color:green'>自动</span>";
+		    }elseif($row['Man'] == 1){
+		    	$row['Man'] = "<span style = 'color:gray'>手动</span>";
+		    }
+		    // 定时控制状态
+		    if($row['IsTime'] == 0){
+		    	$row['IsTime_text'] = '关闭';
+		    }elseif($row['IsTime'] == 1){
+		    	$row['IsTime_text'] = '开启';
+		    }
+		    //发送状态
+		    if($row['SendResult'] == 0){
+		    	$row['SendResult'] = "<span style = 'color:green'>成功</span>";
+		    }
+		};
+		$list->toolbar_filter = function(&$button)use($chgroup_info){
+			if($button['icon'] == 'add'){
+		   	  $button['url'] .= '&deviceid=' .$chgroup_info['DeviceId'].'&farmland_id=' .$chgroup_info['FarmlandId'] .'&chgroup=' .$chgroup_info['ChGroup']; //修改按钮网址
+		   }
+		};
+		
+		$list->display($this);
+	}
+	
+
+}

+ 61 - 0
Home/Lib/Action/DpsbTypeAction.class.php

@@ -0,0 +1,61 @@
+<?php
+
+
+
+class DpsbTypeAction extends CommonAction {
+	const tableName =  'dpsb_type';	
+	const pkName =  'ID';
+	
+
+
+ 
+	  function sys_list(  ){
+		$list = new \Jms\Gui\ClGrid();
+		$list->right_filter = function($right,$type){
+			$page = $_SERVER['PATH_INFO'];
+			return \Jms\Ucenter\Right::defaultFilter($page,$right,$type);
+		};
+		$list->display($this);
+	}
+	
+ 
+	  function sys_addedit(  ){
+		$form = new \Jms\Gui\ClForm();
+		$form->before_add = function(&$data){
+		  $data['TypeName'] = trim($data['TypeName']);
+		  $data['DeviceModel'] = trim($data['DeviceModel']);
+		  //检查设备型号是否重复
+		  $where = array('TypeName'=>$data['TypeName'],'DeviceModel'=>$data['DeviceModel']);
+		  $count = M('dpsb_type')->where($where)->count();
+		  if($count > 0){
+		    json_fail('设备型号已存在');
+		  }
+		};
+		$form->before_modify = function(&$data){
+		  $id = I('get.id');
+		  $data['TypeName'] = trim($data['TypeName']);
+		  $data['DeviceModel'] = trim($data['DeviceModel']);
+		  $where = array('TypeName'=>$data['TypeName'],'DeviceModel'=>$data['DeviceModel'],'ID'=>array('neq',$id));
+		  $count = M('dpsb_type')->where($where)->count();
+		  if($count > 0){
+		    json_fail('设备型号已存在');
+		  }
+		};
+		$form->display($this);
+	}
+	
+ 
+	public  function sys_del(  ){
+		$grid = new \Jiaruan\GridData();
+		$grid->before_delete = function(&$data){
+			$where = array('DeviceTypeId' => $data['ID']);//检查是否有设备存在
+			$count = M('dpsb_device')->where($where)->count();
+			if($count > 0){
+				json_fail('该类型下有设备在使用,不能删除');
+			}
+		};
+		$grid->deleteByPk($this);
+	}
+	
+
+}

+ 10 - 0
Home/Lib/Action/IndexAction.class.php

@@ -0,0 +1,10 @@
+<?php
+class IndexAction extends Action {
+	/*
+
+	*/
+	public function index(){
+		
+	}
+
+}

File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbChannel/send.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbChannel/sys_addedit.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbChannel/sys_list.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbChannel/sys_msg_set.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbChgroup/sys_device_addedit.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbChgroup/sys_device_list.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbChgroup/sys_msg_set.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbChgroup/sys_unbind.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbDevice/oper_list.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbDevice/set_screen_msg.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbDevice/set_screen_msg2.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbDevice/set_screen_msg3.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbDevice/sys_add_list.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbDevice/sys_addedit.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbDevice/sys_addedit_crops.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbDevice/sys_addedit_new.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbDevice/sys_addedit_old.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbDevice/sys_binding.html


+ 2 - 0
Home/Tpl/DpsbDevice/sys_import_excel.html

@@ -0,0 +1,2 @@
+<{extends file='jiaruan:form/xiaoyang/0.0.8'}>
+<{block name=content}>{"form":{"fields":[{"name":"FilePath","type":"fileUploader","label":"上传文件","editor":{"url":"","extensions":"*.xls;*.xlsx;","filesizelimit":"","uploadmode":"cache","uploaddir":""},"newline":1,"type_textfield":"文件上传","width":"","fieldHelpContent":"","styleCss":""}]},"common":{"saveCallbackType":"toView","ViewName":"sys_import_excel","ViewTitle":"导入excel"},"link":[]}<{/block}>

File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbDevice/sys_list.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbDevice/sys_list2.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbDevice/sys_list3.html


File diff suppressed because it is too large
+ 1 - 0
Home/Tpl/DpsbDevice/sys_list_search.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbDevice/sys_msg_set.html


+ 1 - 0
Home/Tpl/DpsbDevice/sys_nav_tree.html

@@ -0,0 +1 @@
+<{extends file='jiaruan:navtree/ztree/0.0.1'}>

File diff suppressed because it is too large
+ 1 - 0
Home/Tpl/DpsbDevice/sys_new_search.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbDevice2/set_screen_msg2.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbGroup/import_device.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbGroup/sys_addedit.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbGroup/sys_list.html


+ 1 - 0
Home/Tpl/DpsbListNav/sys_list_nav.html

@@ -0,0 +1 @@
+<{extends file='jiaruan:cl_nav_tree@0.0.1'}>

File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbPolicy/sys_add.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbPolicy/sys_addedit.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbPolicy/sys_addedit_dp.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbPolicy/sys_device_list.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbPolicy/sys_farmland_list.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbPolicy/sys_list.html


+ 1 - 0
Home/Tpl/DpsbPolicy/test.html

@@ -0,0 +1 @@
+{"form":{"fields":[{"name":"baidumap","type":"baidumap","label":"百度地图","editor":{},"newline":true,"type_textfield":"百度地图"}]},"common":{"saveCallbackType":"toView","ViewName":"test","ViewTitle":"测试地图"},"link":{}}

File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbType/sys_addedit.html


File diff suppressed because it is too large
+ 2 - 0
Home/Tpl/DpsbType/sys_list.html


+ 0 - 3
README.md

@@ -1,3 +0,0 @@
-# dpsb
-
-大棚设备管理

+ 5 - 0
changelog.md

@@ -0,0 +1,5 @@
+#1.0.0
+完成日期:2018-07-13
+设计人员:张晓武
+完成功能:
+	1.刘旻 添加:添加输入点阵屏显示信息表单

File diff suppressed because it is too large
+ 1 - 0
database.json


+ 40 - 0
dp_index.php

@@ -0,0 +1,40 @@
+<?php
+//设置插件别名
+$plugins = array(
+	'uc'   => 'ucenter',
+	'dpcl' => 'dpcl',
+	'dpyh' => 'dpyh',
+	'dpsb' => 'dpsb',
+	'dpsj' => 'dpsj',
+	'dpxf' => 'dpxf',
+	'dpqd' => 'dpqd',
+	'dpjk' => 'dpjk',
+);
+
+//获取并检查插件标识前缀
+if(PHP_SAPI == 'cli'){
+	if(count($argv) >= 2 && in_array($argv[1],array('start','stop','status')) ){
+		if(count($argv)>=3 && $argv[2] == '-d')
+			$_GET['s'] = $argv[3];
+		else
+			$_GET['s'] = $argv[2];
+	} else{
+		$_GET['s'] = $argv[1];
+	}
+}
+else{
+	if(! isset($_GET['s']))
+		$_GET['s'] = 'uc_manage/login';
+}
+$prefix = substr( $_GET['s'] ,0,strpos($_GET['s'] ,'_'));
+if(!isset($plugins[$prefix]))
+	die('unknown plugin prefix - ' . $prefix);
+
+//运行相应插件
+$path = __DIR__ . '/' . $plugins[$prefix] . '/stub.php';
+if(! file_exists($path) )
+	die('invalid plugin path - ' . $path);
+
+define('SCRIPT_FILENAME', $path);
+require $path;
+require __DIR__.'/jrtp/stub.php';

+ 6 - 0
index.php

@@ -0,0 +1,6 @@
+<?php
+define('SCRIPT_FILENAME', __FILE__);
+//插件信息
+require  __DIR__ . '/stub.php';
+//TP入口
+require( dirname(__DIR__) . '/jrtp/stub.php');

+ 28 - 0
install.php

@@ -0,0 +1,28 @@
+<?php
+//error_reporting(E_ALL);
+//检测必须是命令行执行
+if(php_sapi_name() != 'cli'){
+	echo 'intall.php must be executed under cli mode';
+	exit;
+}
+
+//检查命令行参数是否正确
+if($_SERVER['argc'] <= 1){
+	echo 'intall.php must be executed with 1 paramter';
+	exit;	
+}
+
+//获取命令行参数 (构件部署配置文件)
+$assembly_json = $_SERVER['argv'][1];
+if(!file_exists($assembly_json)){
+	echo 'intall.php must be existing json file';
+	exit;
+}
+
+$indexfile = __DIR__ .'/dp_index.php';
+//拷贝index.php
+copy($indexfile, dirname(__DIR__) .'/index.php');
+
+echo 'success';
+
+?>

+ 53 - 0
plugin.json

@@ -0,0 +1,53 @@
+{
+  "id": "b4b2a3beabc3c7df15248419d0544dd7",
+  "name": "dpsb",
+  "title": "大棚设备",
+  "prefix": "dpsb",
+  "version": "1.0.3",
+  "icon": "",
+  "description": "大棚设备",
+  "homepage": "",
+  "license": "",
+  "repository": {
+    "type": "git",
+    "url": ""
+  },
+  "author": {
+    "name": "elviss",
+	"mobile": "", 
+    "email": "elvisszhang@icloud.com",
+	"company": ""
+  },
+  "dependencies": {
+
+  },
+  "devDependencies": {
+    "thinkphp": "3.1.3"
+  },
+    "pointcuts":[
+    {
+        "name": "ui_grid_after_display",
+        "title": "保存打开列表"
+    },
+    {
+        "name": "ui_form_after_display",
+        "title": "保存查看表单"
+    },
+    {
+        "name": "ui_form_after_insert",
+        "title": "保存表单添加"
+    },
+    {
+        "name": "ui_form_after_update",
+        "title": "保存表单修改"
+    }
+  ],
+  "environment":[
+    {
+        "name":"DB_DSN",
+        "title":"MySQL数据库配置",
+        "description":"",
+        "type":"text"
+    }
+  ]
+}

+ 9 - 0
stub.php

@@ -0,0 +1,9 @@
+<?php
+//插件或者工程ID
+define('APP_ID','b4b2a3beabc3c7df15248419d0544dd7');
+//插件前缀
+define('APP_PREFIX','dpsb');
+//插件版本
+define('APP_VERSION','1.0.3');
+//入口文件路径
+define('APP_FILENAME', __FILE__);