DpsbDeviceAction.class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. <?php
  2. class DpsbDeviceAction extends CommonAction {
  3. const tableName = 'dpsb_device';
  4. const pkName = 'ID';
  5. function sys_addedit_old( ){
  6. $form = new \Jiaruan\LigerIframeForm();
  7. $form->before_add = function(&$data){
  8. //检测地址码是否重复
  9. /*$device_addr = trim($data['DeviceAddr']);
  10. $device_info = M('dpsb_device')->where(array('DeviceAddr'=>$device_addr))->find();
  11. if($device_info){
  12. json_fail('该地址码已存在,请重新输入!');
  13. }*/
  14. };
  15. $form->before_save = function(&$data){
  16. $id = I('get.id');
  17. $device_addr = trim($data['DeviceAddr']);
  18. $device_imei = trim($data['DeviceImei']);
  19. $cond = array('ID'=>$id);
  20. $userinfo = M('dpsb_device')->where($cond)->field('DeviceAddr,DeviceImei')->find();
  21. $device_info = M('dpsb_device')->where(array('DeviceAddr'=>$device_addr))->find();
  22. if($device_addr != $userinfo['DeviceAddr'] && $device_info){
  23. json_fail('该地址码已存在,请重新输入!');
  24. }
  25. };
  26. $form->form_filter = function(&$form){
  27. //显示设备类型
  28. $type_list = \Zndp\Device\Option::getDeviceTypeOption();
  29. $form['fields']['DeviceTypeId']['editor'] = array('options'=>$type_list);
  30. //显示设备组下拉数据
  31. $type = new \Zndp\Device\Option;
  32. $channeltype = $type->getGroupOption();
  33. $form['fields']['DeviceGroupId']['editor'] = array('options'=>$channeltype);
  34. };
  35. $form->display($this);
  36. }
  37. function sys_list( ){
  38. $list = new \Jms\Gui\ClGrid();
  39. $list->sql_sort = 'DeviceAddr desc';
  40. $list->sql_filter = function($search,&$cond){
  41. $farmid = I('get.id');
  42. if($farmid){ // 点击导航树进入
  43. //获取当前部门下所有基地
  44. $farm_ids = \Zndp\User\Util::get_depart_farm($farmid);
  45. //取出基地下所有设备组
  46. $where = array('FarmId'=>array('IN',$farm_ids));
  47. $group_ids = M('dpsb_group')->field('ID')->where($where)->select();
  48. $group_ids = array_column($group_ids,'ID');
  49. $cond['DeviceGroupId'] = array('in',$group_ids);
  50. }else{
  51. if($this->userinfo['OrganizationId']){ //默认选中
  52. //获取当前部门下所有基地
  53. $farm_ids = \Zndp\User\Util::get_depart_farm($this->userinfo['OrganizationId']);
  54. $where = array('FarmId'=>array('IN',$farm_ids));
  55. //取出基地下所有设备组
  56. $group_ids = M('dpsb_group')->field('ID')->where($where)->select();
  57. $group_ids = array_column($group_ids,'ID');
  58. $cond['DeviceGroupId'] = array('in',$group_ids);
  59. }else{ //平台管理员
  60. $cond['DeviceGroupId'] = array('neq',0);
  61. }
  62. }
  63. };
  64. $list->row_filter = function(&$row) use ($list){
  65. $row['DeviceAddr'] = $list->renderXEditableInput('地址码',$row,'DeviceAddr',array('rule'=>array('required'=>true)));
  66. $row['DeviceGroupId'] = $list->renderXEditableInput('设备组ID',$row,'DeviceGroupId',array('rule'=>array('required'=>true)));
  67. $row['WorkUnitName'] = $list->renderXEditableInput('设备名称',$row,'WorkUnitName',array('rule'=>array('required'=>true)));
  68. $row['IccId'] = str_replace(' ','<br/>',$row['IccId']);
  69. $where = array('ID'=>$row['DeviceTypeId']);
  70. $row['DeviceTypeId_text'] = M('dpsb_type')->where($where)->getField('TypeName');
  71. };
  72. $list->display($this);
  73. }
  74. public function sys_del( ){
  75. if (IS_AJAX) {
  76. $device_id = I('get.id');
  77. if ( empty($device_id)) {
  78. json_fail('设备id不能为空');
  79. }
  80. // 如果已经绑定大棚,需要先解绑
  81. $where = array( 'DeviceId' => $device_id, 'FarmlandId' => array('neq',-1) );
  82. $count = M('dpsb_chgroup')->where($where)->count();
  83. if( $count > 0 ){
  84. json_fail('有'.$count.'个田地与设备绑定中,无法删除');
  85. }
  86. if(M('dpsb_device')->where(array( 'ID' => $device_id ))->delete() === false){
  87. json_fail('设备表删除失败','');
  88. }
  89. if(M('dpsb_chgroup')->where(array( 'DeviceId' => $device_id ))->delete() === false){
  90. json_fail('通道组组表删除失败','');
  91. }
  92. if(M('dpsb_channel')->where(array( 'DeviceId' => $device_id ))->delete() === false){
  93. json_fail('通道表删除失败','');
  94. }
  95. if(M('dpsb_policy')->where(array( 'DeviceId' => $device_id ))->delete() === false){
  96. json_fail('决策表删除失败','');
  97. }
  98. if(M('dpsj_device')->where(array( 'DeviceId' => $device_id ))->delete() === false){
  99. json_fail('设备数据删除失败','');
  100. }
  101. if(M('dpsj_channel')->where(array( 'DeviceId' => $device_id ))->delete() === false){
  102. json_fail('通道数据删除失败','');
  103. }
  104. json_success('删除成功','');
  105. }
  106. }
  107. function sys_binding( ){
  108. $form = new \Jiaruan\LigerIframeForm();
  109. $form->before_save = function(&$data){
  110. $id = I('get.id');
  111. $group_id = trim($data['DeviceGroupId']);
  112. $cond = array('ID'=>$group_id);
  113. $groupinfo = M('dpsb_group')->where($cond)->find();
  114. if(!$groupinfo){
  115. json_fail('设备组ID不存在');
  116. }
  117. };
  118. $form->display($this);
  119. }
  120. function sys_add_list( ){
  121. $list = new \Jms\Gui\ClGrid();
  122. $list->sql_sort = 'AddTime desc';
  123. $list->right_filter = function($right,$type){
  124. $page = $_SERVER['PATH_INFO'];
  125. return \Jms\Ucenter\Right::defaultFilter($page,$right,$type);
  126. };
  127. $list->sql_filter = function($search,&$cond){
  128. //$cond['DeviceAddr'] = '';
  129. $cond['DeviceGroupId'] = 0;
  130. //$cond['WorkUnitName'] = '';
  131. //搜索
  132. if($search['DeviceImei']){
  133. $cond['DeviceImei'] = $search['DeviceImei'];
  134. }
  135. if($search['IccId']){
  136. $cond['IccId'] = $search['IccId'];
  137. }
  138. };
  139. $list->row_filter = function(&$row) use ($list){
  140. $type_info = M('dpsb_type')->field('TypeName,DeviceModel')->where($where)->find();
  141. $row['DeviceTypeId_text'] = $type_info['TypeName'] .' - '.$type_info['DeviceModel'];
  142. };
  143. //搜索栏回调
  144. $list->searchbar_filter = function(&$searchbar) {
  145. };
  146. $list->display($this);
  147. }
  148. public function sys_debug( ){
  149. if (IS_AJAX) {
  150. $id = I('get.id');
  151. if(empty($id)){
  152. json_fail('id不存在');
  153. }
  154. $addr = M('dpsb_device')->where(array('ID'=>$id))->getField('DeviceAddr');
  155. $host = '127.0.0.1';
  156. $port = 10240;
  157. $socket = new Jms\Network\TcpClient($host,$port);
  158. $cmd = array(
  159. "method" => "apiUploadDebug",
  160. "Addr"=>$addr,
  161. );
  162. $res = $socket->send(json_encode($cmd));
  163. if($res['success']){
  164. json_success('下发成功');
  165. }else{
  166. json_fail($res['message']);
  167. }
  168. }
  169. }
  170. public function sys_upload( ){
  171. if (IS_AJAX) {
  172. $id = I('get.id');
  173. if(empty($id)){
  174. json_fail('id不存在');
  175. }
  176. $addr = M('dpsb_device')->where(array('ID'=>$id))->getField('DeviceAddr');
  177. $host = '127.0.0.1';
  178. $port = 10240;
  179. $socket = new Jms\Network\TcpClient($host,$port);
  180. $cmd = array(
  181. "method" => "apiUpgrade",
  182. "Addr"=>$addr,
  183. );
  184. $res = $socket->send(json_encode($cmd));
  185. if($res['success']){
  186. json_success('下发成功');
  187. }else{
  188. json_fail($res['message']);
  189. }
  190. }
  191. }
  192. public function sys_reboot( ){
  193. if (IS_AJAX) {
  194. $id = I('get.id');
  195. if(empty($id)){
  196. json_fail('id不存在');
  197. }
  198. $addr = M('dpsb_device')->where(array('ID'=>$id))->getField('DeviceAddr');
  199. $host = '127.0.0.1';
  200. $port = 10240;
  201. $socket = new Jms\Network\TcpClient($host,$port);
  202. $cmd = array(
  203. "method" => "apiReboot",
  204. "Addr"=>$addr,
  205. );
  206. $res = $socket->send(json_encode($cmd));
  207. if($res['success']){
  208. json_success('下发成功');
  209. }else{
  210. json_fail($res['message']);
  211. }
  212. }
  213. }
  214. public function sys_sreboot( ){
  215. if (IS_AJAX) {
  216. $id = I('get.id');
  217. if(empty($id)){
  218. json_fail('id不存在');
  219. }
  220. $addr = M('dpsb_device')->where(array('ID'=>$id))->getField('DeviceAddr');
  221. $host = '127.0.0.1';
  222. $port = 10240;
  223. $socket = new Jms\Network\TcpClient($host,$port);
  224. $cmd = array(
  225. "method" => "apiSysReboot",
  226. "Addr"=>$addr,
  227. );
  228. $res = $socket->send(json_encode($cmd));
  229. if($res['success']){
  230. json_success('下发成功');
  231. }else{
  232. json_fail($res['message']);
  233. }
  234. }
  235. }
  236. function sys_addedit_crops( ){
  237. $form = new \Jiaruan\LigerIframeForm();
  238. $form->before_save = function(&$data){
  239. };
  240. $form->display($this);
  241. }
  242. function sys_msg_set( ){
  243. $form = new \Jms\Gui\ClForm();
  244. $form->before_display = function(&$data){
  245. $data['ExceptContent'] = $data['ExceptContent']?:'温度异常了';
  246. };
  247. $form->before_save = function(&$data){
  248. // 温度不能是空字符串
  249. if($data['HighTemp'] === '' || $data['LowTemp'] === ''){
  250. json_fail('温度不能为空');
  251. }
  252. // 温度必须是数字或数字字符串
  253. if( !is_numeric($data['HighTemp']) || !is_numeric($data['LowTemp']) ){
  254. json_fail('温度必须是数字');
  255. }
  256. };
  257. $form->display($this);
  258. }
  259. public function get_alarm_set( ){
  260. $device_addr = I('get.device_addr');
  261. if(!$device_addr){
  262. json_fail('Missing DeviceAddr param!');
  263. }
  264. $where = array('DeviceAddr'=>$device_addr);
  265. $field = "Mobiles,ExceptContent,SmsInterval,HighTemp,IsUseHT,LowTemp,IsUseLT,SoundLightAlarmInterval,SoundLightDuration,SeveralExceptions";
  266. $result = M('dpsb_device')->where($where)->field($field)->find();
  267. if(!$result){ //设备不存在
  268. json_fail('DeviceAddr not exists!');
  269. }
  270. //返回json格式数据
  271. $json_data = json_encode($result,JSON_UNESCAPED_UNICODE);
  272. echo $json_data;
  273. }
  274. function set_screen_msg( ){
  275. /*
  276. 对外地址(提供给农科院) http://zndp.rltest.cn/index.php?s=dpsb_device/set_screen_msg&token=zndp
  277. 查看地址(这个地址也可以修改) http://zndp.rltest.cn/index.php?s=dpsb_device/set_screen_msg&token=zndp&addr=617261
  278. */
  279. if (!$_GET['token'] || $_GET['token'] != 'zndp') {
  280. echo '非法访问';
  281. die;
  282. }
  283. //根据设备地址获取id信息,不能根据id修改信息
  284. $_GET['id'] = 0;
  285. if (IS_POST || $_GET['addr']) {
  286. if ($_GET['addr']) {
  287. $addr = $_GET['addr'];
  288. } else {
  289. $input = json_decode(file_get_contents("php://input"), true);
  290. $addr = $input['DeviceAddr'];
  291. }
  292. if (!$addr) {
  293. json_fail('设备地址有误');
  294. }
  295. $where = array('DeviceAddr' => $addr);
  296. $rows = M(self::tableName)->field('id')->where($where)->select();
  297. if (!$rows || count($rows) != 1) {
  298. json_fail('设备地址错误');
  299. }
  300. $_GET['id'] = $rows[0]['id'];
  301. }
  302. $form = new \Jiaruan\LigerIframeForm();
  303. $form->before_save = function(&$data){
  304. };
  305. $form->display($this);
  306. echo '
  307. <style>
  308. ::-webkit-input-placeholder { /* WebKit, Blink, Edge */ color:#ccc; }
  309. :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color:#ccc; }
  310. ::-moz-placeholder { /* Mozilla Firefox 19+ */ color:#ccc; }
  311. :-ms-input-placeholder { /* Internet Explorer 10-11 */ color:#ccc; }
  312. </style>';
  313. }
  314. function oper_list( ){
  315. $list = new \Jms\Gui\ClGrid();
  316. $list->sql_sort = 'AddTime desc';
  317. $list->sql_filter = function($search,&$cond){
  318. $userid = \Jms\Ucenter\Cookie::getLoginUserId();
  319. $where = array('ID'=>$userid);
  320. $depart_id = M('uc_user')->where($where)->getField('OrganizationId');
  321. if(!$depart_id){
  322. json_fail('登录用户信息有误,请联系管理员!');
  323. }
  324. //获取登录角色部门下所有基地
  325. $depart_ids = \Zndp\User\Util::get_depart_farm($depart_id);
  326. //获取基地下的设备组
  327. $where = array('FarmId'=>array('IN',$depart_id));
  328. $group_ids = M('dpsb_group')->field('ID')->where($where)->select();
  329. $group_ids = array_column($group_ids,'ID');
  330. //根据设备组id查出设备
  331. $cond['DeviceGroupId'] = array('in',$group_ids);
  332. //搜索
  333. if($search['WorkUnitName']){
  334. $cond['WorkUnitName'] = array('like','%'.$search['WorkUnitName'].'%');
  335. }
  336. if($search['DeviceImei']){
  337. $cond['DeviceImei'] = array('like','%'.$search['DeviceImei'] .'%');
  338. }
  339. };
  340. $list->display($this);
  341. }
  342. function set_screen_msg2( ){
  343. $form = new \Jms\Gui\ClForm();
  344. $shed = array(1=>'FirstCrop','SecondCrop','ThirdCrop','FourthCrop');
  345. $form->before_display = function(&$data) use($shed){
  346. // 展示点阵屏信息
  347. for($i=1; $i<=count($shed); $i++){
  348. $shed_data = json_decode($data[$shed[$i]], true);
  349. $data['num'.$i] = $shed_data['num'.$i];
  350. $data['variety'.$i] = $shed_data['variety'.$i];
  351. $data['farmer'.$i] = $shed_data['farmer'.$i];
  352. $data['output'.$i] = $shed_data['output'.$i];
  353. $data['unit'.$i] = $shed_data['unit'.$i];
  354. }
  355. };
  356. $form->before_save = function(&$data) use($shed){
  357. // 保存点阵屏信息
  358. for($i=1; $i<=count($shed); $i++){
  359. $shed_data = array(
  360. 'num'.$i => $data['num'.$i],
  361. 'variety'.$i =>$data['variety'.$i],
  362. 'farmer'.$i =>$data['farmer'.$i],
  363. 'output'.$i =>$data['output'.$i],
  364. 'unit'.$i =>$data['unit'.$i]
  365. );
  366. $data[$shed[$i]] = json_encode($shed_data, JSON_UNESCAPED_UNICODE);
  367. }
  368. };
  369. $form->display($this);
  370. }
  371. function sys_list_search( ){
  372. }
  373. function set_screen_msg3( ){
  374. $form = new \Jms\Gui\ClForm();
  375. $shed = array(1=>'FirstCrop','SecondCrop','ThirdCrop','FourthCrop');
  376. $form->before_display = function(&$data) use($shed){
  377. // 展示点阵屏信息
  378. for($i=1; $i<=count($shed); $i++){
  379. $shed_data = json_decode($data[$shed[$i]], true);
  380. $data['num'.$i] = $shed_data['num'.$i];
  381. $data['variety'.$i] = $shed_data['variety'.$i];
  382. $data['farmer'.$i] = $shed_data['farmer'.$i];
  383. $data['output'.$i] = $shed_data['output'.$i];
  384. $data['unit'.$i] = $shed_data['unit'.$i];
  385. }
  386. };
  387. $form->before_save = function(&$data) use($shed){
  388. // 保存点阵屏信息
  389. for($i=1; $i<=count($shed); $i++){
  390. $shed_data = array(
  391. 'num'.$i => $data['num'.$i],
  392. 'variety'.$i =>$data['variety'.$i],
  393. 'farmer'.$i =>$data['farmer'.$i],
  394. 'output'.$i =>$data['output'.$i],
  395. 'unit'.$i =>$data['unit'.$i]
  396. );
  397. $data[$shed[$i]] = json_encode($shed_data, JSON_UNESCAPED_UNICODE);
  398. }
  399. };
  400. $form->display($this);
  401. }
  402. function sys_nav_tree( ){
  403. //获取导航树数据
  404. $fields = 'ID as id,ParentID as pId,DepartName as name,Level as level';
  405. $zndp_util = new \Zndp\User\Util();
  406. $depart_id = $this->userinfo['OrganizationId'];
  407. $depart_list = $zndp_util->get_depart_option($depart_id,$fields,'name',false);
  408. if(empty($depart_list)){
  409. //没有数据,显示该
  410. $depart_list[0] = array('id'=>'','pid'=>'','name'=>'没有组织','direct_url'=>'');
  411. }else{
  412. for($i=0; $i<count($depart_list); $i++){// 放入链接
  413. if($depart_list[$i]['level'] <= \Zndp\User\DepartEnum::WORK_STATION){
  414. $depart_list[$i]['open']= true;
  415. }
  416. $depart_list[$i]['direct_url'] = '?s=dpsb_device/sys_list';
  417. }
  418. }
  419. //平台,超级管理员,插入所有用户列表
  420. $admin_arr = array(\Zndp\User\RoleEnum::GENERAL_ADMINISTRATOR,\Zndp\User\RoleEnum::SUPER_ADMINISTRATOR);
  421. if( in_array($this->userinfo['RoleId'], $admin_arr) ){
  422. $all_list = array('id'=>'0','pId'=>'-1','name'=>'所有设备','direct_url'=>'?s=dpsb_device/sys_list','open'=>true);
  423. array_unshift($depart_list,$all_list);
  424. }
  425. //未分配的设备
  426. $undistr_devic = array('id'=>'-2','pId'=>'-1','name'=>'未分配设备','direct_url'=>'?s=dpsb_device/sys_add_list');
  427. array_unshift($depart_list,$undistr_devic);
  428. //设备组
  429. $device_group = array('id'=>'-3','pId'=>'-1','name'=>'设备组列表','direct_url'=>'?s=dpsb_group/sys_list');
  430. array_unshift($depart_list,$device_group);
  431. $this->assign('title','用户管理');
  432. $this->assign('tree_data',$depart_list);
  433. $this->display();
  434. }
  435. function sys_list2( ){
  436. $list = new \Jms\Gui\ClGrid();
  437. $list->sql_sort = 'DeviceAddr desc';
  438. $list->before_xeditable_modify = function($name,$data) use ($list){
  439. if( $name == 'WorkUnitName' ){ //修改设备名称
  440. $dpsb_device = M('dpsb_device');
  441. // 同一设备组内唯一
  442. $where = array( $data['pkfield'] => $data['pk'] );
  443. $group_id = $dpsb_device->where($where)->getField('DeviceGroupId');
  444. $where = array(
  445. $data['pkfield'] => array('neq',$data['pk']),
  446. 'DeviceGroupId' => $group_id,
  447. 'WorkUnitName' => $data['value'],
  448. );
  449. $count = $dpsb_device->where($where)->count();
  450. if( $count > 0 ){
  451. json_fail('设备名称已存在');
  452. }
  453. }
  454. };
  455. $list->right_filter = function($right,$type){
  456. $page = $_SERVER['PATH_INFO'];
  457. return \Jms\Ucenter\Right::defaultFilter($page,$right,$type);
  458. };
  459. $list->sql_filter = function($search,&$cond){
  460. $farmid = I('get.id');
  461. if($farmid){ // 点击导航树进入
  462. //获取当前部门下所有基地
  463. $farm_ids = \Zndp\User\Util::get_depart_farm($farmid);
  464. //取出基地下所有设备组
  465. $where = array('FarmId'=>array('IN',$farm_ids));
  466. $group_ids = M('dpsb_group')->field('ID')->where($where)->select();
  467. $group_ids = array_column($group_ids,'ID');
  468. $cond['DeviceGroupId'] = array('in',$group_ids);
  469. }else{
  470. if($this->userinfo['OrganizationId']){ //默认选中
  471. //获取当前部门下所有基地
  472. $farm_ids = \Zndp\User\Util::get_depart_farm($this->userinfo['OrganizationId']);
  473. $where = array('FarmId'=>array('IN',$farm_ids));
  474. //取出基地下所有设备组
  475. $group_ids = M('dpsb_group')->field('ID')->where($where)->select();
  476. $group_ids = array_column($group_ids,'ID');
  477. $cond['DeviceGroupId'] = array('in',$group_ids);
  478. }else{ //平台管理员
  479. $cond['DeviceGroupId'] = array('neq',0);
  480. }
  481. }
  482. //搜索
  483. $keyword = $search['keyword'];
  484. if($keyword){
  485. $field = $search['select_list'];
  486. $cond[$field] = array('LIKE','%'.$keyword.'%');
  487. }
  488. };
  489. $list->row_filter = function(&$row) use ($list){
  490. // 获取设备组名称
  491. $where = array('ID'=>$row['DeviceGroupId']);
  492. $row['DeviceGroupId_text'] = M('dpsb_group')->where($where)->getField('OrgName');
  493. $row['WorkUnitName_text'] = $list->renderXEditableInput('设备名称',$row,'WorkUnitName',array('rule'=>array('required'=>true)));
  494. // 获取设备类型名称
  495. $where = array('ID'=>$row['DeviceTypeId']);
  496. $type_info = M('dpsb_type')->field('TypeName,DeviceModel')->where($where)->find();
  497. $row['DeviceTypeId_text'] = $type_info['TypeName'] .' - '.$type_info['DeviceModel'];
  498. $row['IccId'] = str_replace(' ','<br/>',$row['IccId']);
  499. // 是否在线
  500. if( time() - strtotime($row['OnlineTime']) < 180 ){//超过3分钟算离线
  501. $row['OnlineTime_text'] = "<font color='green'>在线</font>";
  502. }else{
  503. $row['OnlineTime_text'] = "<font color='red'>离线</font>";
  504. }
  505. // 是否安装
  506. $where = array('DeviceId'=>$row['ID'],'FarmlandId'=>array('gt',-1));
  507. $count = M('dpsb_chgroup')->where($where)->count();
  508. if($count > 0){
  509. $row['InstallState'] = "<font color='green'>已安装</font>";
  510. }else{
  511. $row['InstallState'] = "<font color='red'>未安装</font>";
  512. }
  513. //设备已关联田地
  514. $where = array('DeviceId'=>$row['ID'],'FarmlandId'=>array('NEQ',-1));
  515. $chgroup_list = M('dpsb_chgroup')->field('ChGroup,FarmlandId')->where($where)->select();
  516. $assoc_string = '';//关联田地显示字符串
  517. $dpyh_farmland = M('dpyh_farmland');
  518. foreach($chgroup_list as $chgroup){
  519. $where = array('ID'=>$chgroup['FarmlandId']);
  520. $farmland_name = $dpyh_farmland->where($where)->getField('FarmlandName');
  521. $assoc_string .= '通道组'.$chgroup['ChGroup'].' - '.$farmland_name.'<br/>';
  522. }
  523. $row['AssocFarmland'] = $assoc_string;
  524. };
  525. $list->display($this);
  526. }
  527. function sys_new_search( ){
  528. }
  529. function sys_list3( ){
  530. $list = new \Jms\Gui\ClGrid();
  531. $list->sql_sort = 'DeviceAddr desc';
  532. $list->right_filter = function($right,$type){
  533. $page = $_SERVER['PATH_INFO'];
  534. return \Jms\Ucenter\Right::defaultFilter($page,$right,$type);
  535. };
  536. $list->before_xeditable_modify = function($name,$data) use ($list){
  537. if( $name == 'WorkUnitName' ){ //修改设备名称
  538. $dpsb_device = M('dpsb_device');
  539. // 同一设备组内唯一
  540. $where = array( $data['pkfield'] => $data['pk'] );
  541. $group_id = $dpsb_device->where($where)->getField('DeviceGroupId');
  542. $where = array(
  543. $data['pkfield'] => array('neq',$data['pk']),
  544. 'DeviceGroupId' => $group_id,
  545. 'WorkUnitName' => $data['value'],
  546. );
  547. $count = $dpsb_device->where($where)->count();
  548. if( $count > 0 ){
  549. json_fail('设备名称已存在');
  550. }
  551. }
  552. };
  553. $list->sql_filter = function($search,&$cond){
  554. $group_id = I('get.id');
  555. $cond['DeviceGroupId'] = $group_id;
  556. //搜索
  557. $keyword = $search['keyword'];
  558. if($keyword){
  559. $field = $search['select_list'];
  560. $cond[$field] = array('LIKE','%'.$keyword.'%');
  561. }
  562. };
  563. $list->row_filter = function(&$row) use ($list){
  564. $row['WorkUnitName_text'] = $list->renderXEditableInput('设备名称',$row,'WorkUnitName',array('rule'=>array('required'=>true)));
  565. // 获取设备类型名称
  566. $where = array('ID'=>$row['DeviceTypeId']);
  567. $type_info = M('dpsb_type')->field('TypeName,DeviceModel')->where($where)->find();
  568. $row['DeviceTypeId_text'] = $type_info['TypeName'] .' - '.$type_info['DeviceModel'];
  569. $row['IccId'] = str_replace(' ','<br/>',$row['IccId']);
  570. if( time() - strtotime($row['OnlineTime']) < 180 ){//超过3分钟算离线
  571. $row['OnlineTime_text'] = "<font color='green'>在线</font>";
  572. }else{
  573. $row['OnlineTime_text'] = "<font color='red'>离线</font>";
  574. }
  575. $where = array('DeviceId'=>$row['ID'],'FarmlandId'=>array('gt',-1));
  576. $count = M('dpsb_chgroup')->where($where)->count();
  577. if($count > 0){
  578. $row['InstallState'] = "<font color='green'>已安装</font>";
  579. }else{
  580. $row['InstallState'] = "<font color='red'>未安装</font>";
  581. }
  582. //设备已关联田地
  583. $where = array('DeviceId'=>$row['ID'],'FarmlandId'=>array('NEQ',-1));
  584. $chgroup_list = M('dpsb_chgroup')->field('ChGroup,FarmlandId')->where($where)->select();
  585. $assoc_string = '';//关联田地显示字符串
  586. $dpyh_farmland = M('dpyh_farmland');
  587. foreach($chgroup_list as $chgroup){
  588. $where = array('ID'=>$chgroup['FarmlandId']);
  589. $farmland_name = $dpyh_farmland->where($where)->getField('FarmlandName');
  590. $assoc_string .= '通道组'.$chgroup['ChGroup'].' - '.$farmland_name.'<br/>';
  591. }
  592. $row['AssocFarmland'] = $assoc_string;
  593. };
  594. $list->display($this);
  595. }
  596. function sys_addedit( ){
  597. $form = new \Jms\Gui\ClForm();
  598. $form->before_save = function(&$data){
  599. $device_id = I('get.id');
  600. $where = array(
  601. 'ID' => array('neq',$device_id),
  602. 'DeviceGroupId' => $data['DeviceGroupId'],
  603. 'WorkUnitName' => $data['WorkUnitName']
  604. );
  605. $count = M('dpsb_device')->where($where)->count();
  606. if( $count > 0 ){
  607. json_fail('设备名称已存在');
  608. }
  609. };
  610. $form->form_filter = function(&$form){
  611. //显示设备组下拉数据
  612. $type = new \Zndp\Device\Option;
  613. $channeltype = $type->getGroupOption();
  614. $form['fields']['DeviceGroupId']['editor'] = array('options'=>$channeltype);
  615. };
  616. $form->display($this);
  617. }
  618. }