DpsbChgroupAction.class.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. class DpsbChgroupAction extends CommonAction {
  3. const tableName = 'dpsb_chgroup';
  4. const pkName = 'ID';
  5. function sys_device_list( ){
  6. $list = new \Jms\Gui\ClGrid();
  7. $list->sql_filter = function($search,&$cond){
  8. $farmland_id = I('get.id');
  9. $cond['FarmlandId'] = $farmland_id;
  10. };
  11. $list->row_filter = function(&$row){
  12. $where = array('ID'=>$row['DeviceId']);
  13. $fields = 'DeviceTypeId,DeviceAddr,WorkUnitName,DeviceType,DeviceImei,VersionName';
  14. $device_info = M('dpsb_device')->field($fields)->where($where)->find();
  15. $row = array_merge($row,$device_info);
  16. $where = array('ID'=>$row['DeviceTypeId']);
  17. $row['DeviceTypeId_text'] = M('dpsb_type')->where($where)->getField('TypeName');
  18. };
  19. $list->toolbar_filter = function(&$button){
  20. if($button['icon'] == 'add'){
  21. $farmland_id = I('get.id');
  22. $farm_id = I('get.farm_id');
  23. $button['url'] .= '&farm_id='.$farm_id.'&farmland_id=' .$farmland_id;
  24. }
  25. };
  26. $list->display($this);
  27. }
  28. function sys_device_addedit( ){
  29. $form = new \Jms\Gui\ClForm();
  30. $form->onchange_filter = function($field,$data){
  31. $farm_id = I('get.farm_id');
  32. $result = array();
  33. if($field == 'DeviceTypeId'){
  34. //获取当前基地该类型设备
  35. $result['DeviceId'] = $this->get_device_by_type($data[$field],$farm_id);
  36. if($result['DeviceId'][0]['id']){ //如果有设备
  37. $result['ChGroup'] = $this->get_device_chgroup($result['DeviceId'][0]['id']);
  38. }else{
  39. $result['ChGroup'] = array();
  40. }
  41. }
  42. if($field == 'DeviceId'){
  43. $result['ChGroup'] = $this->get_device_chgroup($data[$field]);
  44. }
  45. return $result;
  46. };
  47. $form->before_add = function(&$data){
  48. $chgroup = M('dpsb_chgroup');
  49. //无法绑定已经绑定的通道组
  50. $where = array('ChGroup'=>$data['ChGroup'],'DeviceId'=>$data['DeviceId']);
  51. $farmland_id = $chgroup->where($where)->getField('FarmlandId');
  52. if( $farmland_id != -1 && $farmland_id != ''){
  53. $farmland_name = M('dpyh_farmland')->where(array('ID'=>$farmland_id))->getField('FarmlandName');
  54. json_fail('该通道组已绑定大棚:'.$farmland_name);
  55. }
  56. //添加
  57. $farmland_id = I('get.farmland_id');
  58. $result = $chgroup->where($where)->setField('FarmlandId',$farmland_id);
  59. if(!$result){
  60. json_fail('绑定失败,请稍后重试');
  61. }
  62. // 下发大棚编号给控制台
  63. $where = array('ID'=>$farmland_id);
  64. $farmland_num = M('dpyh_farmland')->where($where)->getField('FarmlandNum');
  65. $where = array('ID'=>$data['DeviceId']);
  66. $device_addr = M('dpsb_device')->where($where)->getField('DeviceAddr');
  67. $host = '127.0.0.1';
  68. $port = 10240;
  69. $socket = new Jms\Network\TcpClient($host,$port);
  70. $cmd = array(
  71. "method" => "farmlandNum",
  72. "addr" => $device_addr, // 设备addr
  73. "channelGroup" => $data['ChGroup'], // 通道组号
  74. "number" => $farmland_num, // 大棚编号
  75. );
  76. $send_result = $socket->send(json_encode($cmd)."\n");
  77. // 添加大棚id到redis(做一个同步大棚id的函数,把已经绑定的添加到Redis)
  78. $redis = Redis("dpsb_farmland_list","hash");
  79. $key = 'DeviceId-'.$data['DeviceId'].'-ChGroup-'.$data['ChGroup'];
  80. $val = $farmland_id;
  81. $hash = array($key => $val );
  82. $result = $redis->add($hash);
  83. if(!$result){
  84. json_fail('Redis缓存设置失败');
  85. }
  86. if(!$send_result['success']){
  87. json_fail('设置控制柜显示大棚编号失败');
  88. }
  89. json_success('绑定成功');
  90. };
  91. $form->form_filter = function(&$form){
  92. //初始化设备类型下拉选项
  93. $type_list = \Zndp\Device\Option::getDeviceTypeOption();
  94. $form['fields']['DeviceTypeId']['editor'] = array('options'=>$type_list);
  95. //初始化设备下拉选项
  96. $farm_id = I('get.farm_id');
  97. $device_list = $this->get_device_by_type($type_list[0]['id'],$farm_id);
  98. $form['fields']['DeviceId']['editor'] = array('options'=>$device_list);
  99. //取第一个设备的通道组
  100. $chgroup_list = $this->get_device_chgroup($device_list[0]['id']);
  101. $form['fields']['ChGroup']['editor'] = array('options'=>$chgroup_list);
  102. };
  103. $form->display($this);
  104. }
  105. public function get_device_by_type( $type_id, $farm_id = '' ){
  106. if(!$type_id){
  107. json_fail('设备类型不能为空');
  108. }
  109. // 初始化查询条件
  110. $where = array('DeviceTypeId'=>$type_id);
  111. if($farm_id){ // 只查找当前基地的设备
  112. $cond = array('FarmId'=>$farm_id);
  113. $device_groups = M('dpsb_group')->field('ID')->where($cond)->select();
  114. $device_groups = array_column($device_groups,'ID');
  115. $where['DeviceGroupId'] = array('IN',$device_groups);
  116. }
  117. $device_list = M('dpsb_device')->field('ID as id,WorkUnitName as text')->where($where)->order('ID DESC')->select();
  118. if(empty($device_list)){
  119. $device_list = array();
  120. $first_option = array('id' => '','text' => '无设备');
  121. array_unshift($device_list,$first_option);
  122. }else{
  123. $first_option = array('id' => '','text' => '--请选择设备--');
  124. array_unshift($device_list,$first_option);
  125. }
  126. return $device_list;
  127. }
  128. public function get_device_chgroup( $device_id ){
  129. $where = array('DeviceId'=>$device_id,'FarmlandId'=>-1);//显示该设备未绑定的通道
  130. $chgroup_list = M('dpsb_chgroup')->field('ChGroup as id,ChGroup as text')->where($where)->select();
  131. if(empty($chgroup_list)){
  132. $chgroup_list = array();
  133. $first_option = array('id' => '','text' => '无可用通道组');
  134. array_unshift($chgroup_list,$first_option);
  135. }else{
  136. $first_option = array('id' => '','text' => '--请选择通道--');
  137. array_unshift($chgroup_list,$first_option);
  138. }
  139. return $chgroup_list;
  140. }
  141. function sys_unbind( ){
  142. $id = I('get.id');
  143. $where = array('ID'=>$id);
  144. $device_id = M('dpsb_chgroup')->where($where)->getField('DeviceId');
  145. $form = new \Jms\Gui\ClForm();
  146. $form->before_display = function(&$data) use($device_id) {
  147. $where = array('ID'=>$device_id);
  148. $data['WorkUnitName'] = M('dpsb_device')->where($where)->getField('WorkUnitName');
  149. };
  150. $form->before_save = function(&$data) use($device_id) {
  151. //解绑设备
  152. $id = I('get.id');
  153. $where = array('ID'=>$id);
  154. $result = M('dpsb_chgroup')->where($where)->setField('FarmlandId',-1);
  155. if(!$result){
  156. json_fail('解绑失败');
  157. }
  158. // 删除Redis中的记录
  159. $redis = Redis("dpsb_farmland_list","hash");
  160. $key = 'DeviceId-'.$device_id .'-ChGroup-'.$data['ChGroup'];
  161. $result = $redis->where($key)->delete();
  162. if(!$result){
  163. json_fail('解绑成功,但Redis缓存删除失败');
  164. }
  165. json_success('解绑成功');
  166. };
  167. $form->display($this);
  168. }
  169. }