123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?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);
- }
-
- }
|