Lampcontrol.php 125 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. include_once(FCPATH . 'application/controllers/Base_Controller.php');
  3. class Lampcontrol extends Base_Controller {
  4. private $chargeStageMap = array(0=>'没有充电',16=>'MPPT充电',32=>'均衡充电',48=>'提升充电',64=>'浮充',''=>'未知状态');
  5. public function __construct() {
  6. parent::__construct();
  7. $this->load->model('Company_model');
  8. $this->load->model('Project_model');
  9. $this->load->model('Network_model');
  10. $this->load->model('Lamp_model');
  11. $this->load->model('Batmanage_model');
  12. $this->load->model('Patrol_model');
  13. $this->load->model('Load_model');
  14. $this->load->model('Battery_model');
  15. $this->load->model('Alarm_model');
  16. // $this->load->model('Zone_model');
  17. $this->load->model('Videomonitor_model');
  18. $this->load->model('Modbus_load_model');
  19. $this->load->model('Modbus_battery_model');
  20. $this->load->model('Global_location_model');
  21. $this->load->model('RealtimeInfoLog_model');
  22. }
  23. // 灯控列表
  24. public function get() {
  25. $userid = $this->get_user_info('id');
  26. $role = $this->get_user_info('role');
  27. $company = $this->input->post('company',true);
  28. $project_id = intval($this->input->post('project_id', true));
  29. if (empty($project_id)) exit(json_result('0007',$this->response['0007']));
  30. $fields = $this->input->post('fields', true);
  31. $field1 = $this->input->post('field1', true);
  32. $pid_arr = $project_id;
  33. $timezone = $this->Project_model->get_timezone_by_projectid($project_id);
  34. $filter = 'L.projectid = '.$project_id;
  35. $countWhere = array('projectid = '.$project_id);
  36. $network_id = $this->input->post('network_id', true);
  37. $section = $this->input->post('section', true);
  38. $type = intval($this->input->post('type', true));
  39. $company = intval($this->input->post('company', true));
  40. if (!empty($type) && !empty($company)) {
  41. if ($type == 2) {
  42. $filter .= ' AND L.manu = '.$company;
  43. $countWhere[] = 'manu = '.$company;
  44. }elseif ($type == 3) {
  45. $filter .= ' AND L.supplier = '.$company;
  46. $countWhere[] = 'supplier = '.$company;
  47. }elseif ($type == 4) {
  48. $filter .= ' AND L.po = '.$company;
  49. $countWhere[] = 'po = '.$company;
  50. }else {
  51. $filter .= ' AND P.cityid = '.$company;
  52. }
  53. }
  54. if (!empty($section)) {
  55. $filter .= ' AND L.section = "'.$section.'"';
  56. $countWhere[] = 'section = "'.$section.'"';
  57. }
  58. $offline = $this->input->post('offline', true);
  59. if (!empty($offline)) {
  60. $filter .= ' AND L.netstatus = 0';
  61. $countWhere[] = 'netstatus = 0';
  62. }
  63. $online = $this->input->post('online', true);
  64. if (!empty($online)) {
  65. $filter .= ' AND L.netstatus= 1';
  66. $countWhere[] = 'netstatus = 1';
  67. }
  68. // 分页数据
  69. $page = intval($this->input->post('page',true));
  70. $page = empty($page) ? 1 : $page;
  71. $count = intval($this->input->post('count',true));
  72. $limit = empty($count) ? 10 : $count;
  73. $offset = ($page - 1)*$count;
  74. $keyword = $this->input->post('keyword', true);
  75. if ($keyword !== '' && $keyword !== null) {
  76. $filter .= ' AND (L.number like "%'.$keyword.'%" OR L.section like "%'.$keyword.'%" OR L.address like "%'.$keyword.'%")';
  77. $countWhere[] = '(number like "%'.$keyword.'%" OR section like "%'.$keyword.'%" OR address like "%'.$keyword.'%")';
  78. }
  79. $status = $this->input->post('status', true);
  80. if (!empty($status)) {
  81. // $filter['AI.isfaulted'] = $status;
  82. $filter .= ' AND (AI.batstatus != 0 OR AI.panelstatus != 0 OR AI.lampstatus != 0 OR AI.tempstatus != 0)';
  83. }
  84. // $alarmtype = $this->input->post('alarmtype', true);
  85. // if (!empty($alarmtype)) $filter['AI.alarmtype'] = $alarmtype;
  86. // 记录用户选择的字段
  87. if (!empty($field1)) {
  88. $fieldData = array('userid'=>$userid,'lampfield'=>$field1);
  89. $this->User_model->update_user_field($fieldData);
  90. }
  91. $lite = $this->input->post('lite', true);
  92. $version = $this->session->userdata('version');
  93. if ($lite == 1) {
  94. if(empty($fields)){
  95. $fields = 'L.id, L.number';
  96. }else{
  97. $fields .= ",RI.updatetime";
  98. }
  99. $join = array();
  100. $data['lamps'] = $this->Lamp_model->get_list_by_join($filter, $fields,$limit, $offset, $join, "L.number ASC", NUll, 'L');
  101. } else {
  102. if(empty($fields) || empty($field1)){
  103. $fields = 'L.id, L.number, IFNULL(L.status,0) as status, L.isfaulted, L.address, L.lamppower, L.electricSOC, L.battvoltage, L.chargestage, RI.updatetime, RI.updatetime as modifytime,L.overtimes,L.boardpower,L.lighteness as light,L.projectid,L.devstatus as online,P.projectname as project_name,L.section,L.loadtype,P.timezone as timezoneid';
  104. }else{
  105. $fields .= ",L.id,L.projectid,L.section,L.loadtype,P.timezone as timezoneid";
  106. }
  107. $download = intval($this->input->post('download',true));
  108. if (!empty($download)) {
  109. $limit = null;
  110. $offset = null;
  111. }
  112. $join = array();
  113. $join[] = ['table'=>'project as P', 'cond'=>'L.projectid = P.id', 'type'=>'left'];
  114. // $join[] = ['table'=>'(select t1.* from alarm_info_log t1, (select lampid, max(updatetime) as maxtime from alarm_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as AI', 'cond'=>'L.id = AI.lampid', 'type'=>'left'];
  115. $join[] = ['table'=>'(select t1.* from warning_info_log t1, (select lampid, max(updatetime) as maxtime from warning_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as AI', 'cond'=>'L.id = AI.lampid', 'type'=>'left'];
  116. $join[] = ['table'=>'(select t1.* from history_info_log t1, (select lampid, max(updatetime) as maxtime from history_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as HI', 'cond'=>'L.id = HI.lampid', 'type'=>'left'];
  117. $join[] = ['table'=>'(select t1.* from realtime_info_log t1, (select lampid, max(updatetime) as maxtime from realtime_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as RI', 'cond'=>'L.id = RI.lampid', 'type'=>'left'];
  118. $data['lamps'] = $this->Lamp_model->get_list_by_join($filter, $fields,$limit, $offset, $join, "L.number ASC", NUll, 'L');
  119. // $data['lamps'] = $this->Lamp_model->get_list_in('L.projectid', $pid_arr, $fields, $filter);
  120. // var_dump(microtime());
  121. $indexArr = [2=>'16',3=>'32',4=>'48',5=>'64'];
  122. if (empty($version)) {
  123. $chargeStageMap = array(0=>'没有充电',1=>'启动充电',6=>'限流',7=>'充满',16=>'MPPT充电',32=>'均衡充电',48=>'提升充电',64=>'浮充',''=>'未知状态');
  124. $battstatusArr = array(0=>'空闲',1=>'放电',2=>'充电',3=>'一边充电一边放电',''=>'未知状态');
  125. $unknown = '未知状态';
  126. }else{
  127. $chargeStageMap = array(0=>'There is no charge',1=>'Start charging',6=>'Current limiting',7=>'Full',16=>'MPPT charging',32=>'Equalizing charge',48=>'Improve charging',64=>'Floating',''=>'Unknown state');
  128. $battstatusArr = array(0=>'Free',1=>'Discharge',2=>'Charge',3=>'Discharge while charging',''=>'Unknown state');
  129. $unknown = 'Unknown state';
  130. }
  131. if (!empty($download)) {
  132. $fieldArr = explode(',', $field1);
  133. $fieldArr[] = 'iccid';
  134. $temp = [];
  135. $i = 1;
  136. foreach ($data['lamps'] as $key => &$value) {
  137. $value['section'] = empty($value['section']) ? '' : $value['section'];
  138. if (isset($value['updatetime']) && $value['updatetime'] == '0000-00-00 00:00:00') {
  139. $value['updatetime'] = null;
  140. }
  141. if (array_key_exists('lamppower',$value)) {
  142. $value['lamppower'] = !empty($value['lamppower']) ? $value['lamppower'] : "0";
  143. }
  144. // 故障信息处理
  145. if (array_key_exists('alarmStatus',$value)) {
  146. if (empty($value['batstatus']) && $value['panelstatus'] && $value['lampstatus'] && $value['tempstatus']) {
  147. if (empty($version)) {
  148. $value['isfaulted'] = '无';
  149. }else{
  150. $value['isfaulted'] = 'Nothing';
  151. }
  152. }else{
  153. $batstatus = $this->config->item('batstatus');
  154. $panelstatus = $this->config->item('panelstatus');
  155. $lampstatus = $this->config->item('lampstatus');
  156. $tempstatus = $this->config->item('tempstatus');
  157. $temp2 = array();
  158. if (isset($batstatus[$value['batstatus']])) $temp2[] = $batstatus[$value['batstatus']];
  159. if (isset($panelstatus[$value['panelstatus']])) $temp2[] = $panelstatus[$value['panelstatus']];
  160. if (isset($lampstatus[$value['lampstatus']])) $temp2[] = $lampstatus[$value['lampstatus']];
  161. if (isset($tempstatus[$value['tempstatus']])) $temp2[] = $tempstatus[$value['tempstatus']];
  162. if (!empty($temp2)) {
  163. $value['isfaulted'] = implode(',', $temp2);
  164. }else{
  165. $value['isfaulted'] = 'Nothing';
  166. }
  167. }
  168. }
  169. if (array_key_exists('lampstatus',$value)) {
  170. $value['lampstatus'] = !empty($value['lampstatus']) ? $value['lampstatus'] : "0";
  171. }
  172. if (array_key_exists('electricSOC',$value)) {
  173. $value['electricSOC'] = !empty($value['electricSOC']) ? $value['electricSOC'] : "0";
  174. }
  175. if (array_key_exists('battvoltage',$value)) {
  176. $value['battvoltage'] = !empty($value['battvoltage']) ? $value['battvoltage'] : "0";
  177. }
  178. if (array_key_exists('chargestage',$value)) {
  179. $value['chargestage'] = isset($value['chargestage']) ? $chargeStageMap[$value['chargestage']] : $unknown;
  180. }
  181. if (array_key_exists('battstatus',$value)) {
  182. $value['battstatus'] = isset($value['battstatus']) ? $battstatusArr[$value['battstatus']] : $unknown;
  183. }
  184. if (array_key_exists('overtimes',$value)) {
  185. $value['overtimes'] = !empty($value['overtimes']) ? $value['overtimes'] : 0;
  186. }
  187. if (array_key_exists('solarpower',$value)) {
  188. $value['solarpower'] = !empty($value['solarpower']) ? $value['solarpower'] : 0;
  189. }
  190. if (array_key_exists('iccid',$value)) {
  191. $value['iccid'] = !empty($value['iccid']) ? $value['iccid'] : '';
  192. }
  193. if (array_key_exists('updatetime',$value)) {
  194. $value['updatetime'] = !empty($value['updatetime']) ? $value['updatetime'] : '';
  195. // 通过时区修改时间
  196. if (!empty($value['updatetime'])) {
  197. $timezone = $this->Project_model->get_timezone_by_projectid($value['projectid']);
  198. if ($timezone['value'] != 8) {
  199. $value['updatetime'] = date_change($value['updatetime'],8,$timezone['value']);
  200. }
  201. }
  202. }
  203. // 当前策略
  204. if (array_key_exists('policyid',$value)) {
  205. if (empty($value['policyid'])) {
  206. $value['policyid'] = '';
  207. }else{
  208. $policyData = $this->db->query('select name from policy_info where id = '.$value['policyid'])->row_array();
  209. if (empty($policyData) || empty($policyData['name'])) {
  210. $value['policyid'] = '';
  211. }else{
  212. $value['policyid'] = $policyData['name'];
  213. }
  214. }
  215. }
  216. $temp[$key][] = $i;
  217. foreach ($fieldArr as $f) {
  218. if ($f == 'lampstatus') {
  219. if (empty($version)) {
  220. $temp[$key][] = empty($value[$f]) ? '关灯' : '开灯';
  221. }else{
  222. $temp[$key][] = empty($value[$f]) ? 'off' : 'on';
  223. }
  224. continue;
  225. }
  226. if ($f == 'status') {
  227. if (empty($version)) {
  228. $temp[$key][] = empty($value[$f]) ? '不在线' : '在线';
  229. }else{
  230. $temp[$key][] = empty($value[$f]) ? 'off-line' : 'online';
  231. }
  232. continue;
  233. }
  234. if ($f == 'daychargemincurrent' || $f == 'daydischargemincurrent') {
  235. $t = floor($value[$f]/3600) < 10 ? '0'.floor($value[$f]/3600) : floor($value[$f]/3600);
  236. $m = ceil($value[$f]%3600/60) < 10 ? '0'.ceil($value[$f]%3600/60) : ceil($value[$f]%3600/60);
  237. $temp[$key][] = $t.':'.$m;
  238. continue;
  239. }
  240. $temp[$key][] = $value[$f];
  241. }
  242. $i ++;
  243. }
  244. $title1 = get_fields_by_type($field1,$version);
  245. if (empty($version)) {
  246. $title2 = array_merge(array('编号'),$title1);
  247. }else{
  248. $title2 = array_merge(array('Number'),$title1);
  249. }
  250. $title = [];
  251. foreach ($title2 as $t) {
  252. $title[0][] = $t;
  253. }
  254. $title[0][] = 'iccid';
  255. $data = array_merge($title,$temp);
  256. $path = push_excel($data,'lampinfoExcel_'.date('Ymd'));
  257. exit(json_result('0000', $this->response['0000'], array('path'=>$path)));
  258. }
  259. foreach ($data['lamps'] as $key => &$value) {
  260. $value['section'] = empty($value['section']) ? '' : $value['section'];
  261. if (isset($value['updatetime']) && $value['updatetime'] == '0000-00-00 00:00:00') {
  262. $value['updatetime'] = null;
  263. }
  264. if (array_key_exists('lamppower',$value)) {
  265. $value['lamppower'] = !empty($value['lamppower']) ? $value['lamppower'] : "0";
  266. }
  267. // 故障信息处理
  268. if (array_key_exists('alarmStatus',$value)) {
  269. if (empty($value['batstatus']) && $value['panelstatus'] && $value['lampstatus'] && $value['tempstatus']) {
  270. if (empty($version)) {
  271. $value['isfaulted'] = '无';
  272. }else{
  273. $value['isfaulted'] = 'Nothing';
  274. }
  275. }else{
  276. $batstatus = $this->config->item('batstatus');
  277. $panelstatus = $this->config->item('panelstatus');
  278. $lampstatus = $this->config->item('lampstatus');
  279. $tempstatus = $this->config->item('tempstatus');
  280. $temp2 = array();
  281. if (isset($batstatus[$value['batstatus']])) $temp2[] = $batstatus[$value['batstatus']];
  282. if (isset($panelstatus[$value['panelstatus']])) $temp2[] = $panelstatus[$value['panelstatus']];
  283. if (isset($lampstatus[$value['lampstatus']])) $temp2[] = $lampstatus[$value['lampstatus']];
  284. if (isset($tempstatus[$value['tempstatus']])) $temp2[] = $tempstatus[$value['tempstatus']];
  285. if (!empty($temp2)) {
  286. $value['isfaulted'] = implode(',', $temp2);
  287. }else{
  288. $value['isfaulted'] = 'Nothing';
  289. }
  290. }
  291. }
  292. if (array_key_exists('lampstatus',$value)) {
  293. $value['lampstatus'] = !empty($value['lampstatus']) ? $value['lampstatus'] : "0";
  294. }
  295. if (array_key_exists('electricSOC',$value)) {
  296. $value['electricSOC'] = !empty($value['electricSOC']) ? $value['electricSOC'] : "0";
  297. }
  298. if (array_key_exists('battvoltage',$value)) {
  299. $value['battvoltage'] = !empty($value['battvoltage']) ? $value['battvoltage'] : "0";
  300. }
  301. if (array_key_exists('chargestage',$value)) {
  302. $value['chargestage'] = isset($value['chargestage']) && isset($chargeStageMap[$value['chargestage']]) ? $chargeStageMap[$value['chargestage']] : $unknown;
  303. }
  304. if (array_key_exists('battstatus',$value)) {
  305. $value['battstatus'] = isset($value['battstatus']) ? $battstatusArr[$value['battstatus']] : $unknown;
  306. }
  307. if (array_key_exists('overtimes',$value)) {
  308. $value['overtimes'] = !empty($value['overtimes']) ? $value['overtimes'] : 0;
  309. }
  310. if (array_key_exists('solarpower',$value)) {
  311. $value['solarpower'] = !empty($value['solarpower']) ? $value['solarpower'] : 0;
  312. }
  313. // 当前策略
  314. if (array_key_exists('policyid',$value)) {
  315. if (empty($value['policyid'])) {
  316. $value['policyid'] = '';
  317. }else{
  318. $policyData = $this->db->query('select name from policy_info where id = '.$value['policyid'])->row_array();
  319. if (empty($policyData) || empty($policyData['name'])) {
  320. $value['policyid'] = '';
  321. }else{
  322. $value['policyid'] = $policyData['name'];
  323. }
  324. }
  325. }
  326. if (array_key_exists('updatetime',$value)) {
  327. $value['updatetime'] = !empty($value['updatetime']) ? $value['updatetime'] : '';
  328. // 通过时区修改时间
  329. if (!empty($value['updatetime'])) {
  330. // $value['timezoneid'];
  331. if($timezone === null) $timezone = $this->db->query('select value from timezone where id = '.$value['timezoneid'])->row_array();
  332. // var_dump($timezone);die;
  333. $value['updatetime'] = set_timezone($value['updatetime'],$timezone['value']);
  334. // if ($timezone['value'] != 8) {
  335. // $value['updatetime'] = date_change($value['updatetime'],0,$timezone['value']);
  336. // }
  337. }
  338. }
  339. }
  340. if (!empty($project_id)) {
  341. }
  342. }
  343. // unset($filter['page']);
  344. // unset($filter['count']);
  345. $countWheret1 = implode(' AND ', $countWhere);
  346. $data['total_data']['install_num'] = $this->Lamp_model->get_count($countWheret1);
  347. $countWheret = $countWhere;
  348. $countWheret[] = 'status = 1';
  349. $countWheret[] = 'netstatus = 1';
  350. $countWheret1 = implode(' AND ', $countWheret);
  351. $data['total_data']['light_num'] = $this->Lamp_model->get_count($countWheret1);
  352. $countWheret = $countWhere;
  353. $countWheret[] = 'netstatus = 1';
  354. $countWheret1 = implode(' AND ', $countWhere);
  355. $data['total_data']['online_num'] = $this->Lamp_model->get_count($countWheret1);
  356. $filter2 = $filter.' AND (AI.batstatus != 0 OR AI.panelstatus != 0 OR AI.lampstatus != 0 OR AI.tempstatus != 0)';
  357. $join2 = array();
  358. $join2[] = ['table'=>'project as P', 'cond'=>'L.projectid = P.id', 'type'=>'left'];
  359. $join2[] = ['table'=>'(select lampid,max(updatetime) as maxTime from warning_info_log group by lampid) as WI','cond'=>'WI.lampid = L.id','type'=>'inner'];
  360. $join2[] = ['table'=>'warning_info_log as AI','cond'=>'WI.lampid = AI.lampid AND WI.maxTime = AI.updatetime','type'=>'inner'];
  361. $res = $this->Lamp_model->get_list_by_multi_join($filter2, 'count(*) total',null, null, $join2, "L.number ASC", NUll, 'L',true);
  362. $data['total_data']['fault_count'] = $res['total'];
  363. $data['total_data']['light_rate'] = empty($data['total_data']['install_num']) ? '0' : round($data['total_data']['light_num']/$data['total_data']['install_num'],4)*100;
  364. $data['total_data']['light_rate'] .= '%';
  365. $data['total_data']['online_rate'] = empty($data['total_data']['install_num']) ? '0' : round($data['total_data']['online_num']/$data['total_data']['install_num'],4)*100;
  366. $data['total_data']['online_rate'] .= '%';
  367. $data['total_data']['fault_rate'] = empty($data['total_data']['install_num']) ? '0' : round($data['total_data']['fault_count']/$data['total_data']['install_num'],4)*100;
  368. $data['total_data']['fault_rate'] .= '%';
  369. // $data['total_data']['network_num'] = $this->Network_model->get_count(['projectid'=>$project_id,'type'=>1]);
  370. $res = $this->Lamp_model->get_list_by_multi_join($filter, 'count(*) total',$limit, $offset, $join, "L.number ASC", NUll, 'L',true);
  371. $result = !empty($pid_arr) ? $this->Lamp_model->get_count(['projectid'=>$pid_arr]) : array();
  372. $result = $this->Lamp_model->get_count(['projectid'=>$pid_arr]);
  373. $data['total'] = $res['total'];
  374. $data['count'] = $result;
  375. $data['fault'] = $res['total'];
  376. exit(json_result('0000', $this->response['0000'], $data));
  377. }
  378. // 更新灯控信息
  379. public function update() {
  380. $role = $this->get_user_info('role');
  381. $username = $this->input->post('username',true);
  382. $client_key = $this->input->post('client_key',true);
  383. $token = $this->input->post('token',true);
  384. $type = intval($this->input->post('type',true));
  385. $lamp_id = $this->input->post('lamp_id',true);
  386. $version = $this->session->userdata('version');
  387. $lamp_id = $this->input->post('lamp_id', true);
  388. if (empty($lamp_id)) {
  389. exit(json_result('0411', $this->response['0411'], array()));
  390. }
  391. $ids = explode(',', $lamp_id);
  392. $status = 1;
  393. $list = $this->Lamp_model->get_list(['id'=>$ids],'address');
  394. foreach ($list as $v) {
  395. $this->send_cmd('18',$v['address'],'','0000000000','0100',0);
  396. }
  397. sleep(2);
  398. if ($status) {
  399. exit(json_result('0406', $this->response['0406'], array()));
  400. }else{
  401. exit(json_result('0407', $this->response['0407'], array()));
  402. }
  403. }
  404. // 灯控的开启/关闭(支持批量处理)
  405. public function turnonoff(){
  406. $username = $this->input->post('username',true);
  407. $role = $this->input->post('role',true);
  408. $client_key = $this->input->post('client_key',true);
  409. $token = $this->input->post('token',true);
  410. $type = intval($this->input->post('type',true));
  411. $lamp_id = $this->input->post('lamp_id',true);
  412. $clientid = $this->input->post('clientid',true);
  413. $version = $this->session->userdata('version');
  414. $lampArr = explode(',', $lamp_id);
  415. $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/lampcontrol/dimming_asy';
  416. $lightness = empty($type) ? 0 : 100;
  417. foreach ($lampArr as $l) {
  418. doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'type'=>$type,'lamp_id'=>$l,'lightness'=>$lightness,'role'=>$role]);
  419. }
  420. exit();
  421. }
  422. // 灯控调光
  423. public function dimming(){
  424. $username = $this->input->post('username',true);
  425. $role = $this->input->post('role',true);
  426. $client_key = $this->input->post('client_key',true);
  427. $token = $this->input->post('token',true);
  428. $lamp_id = $this->input->post('lamp_id',true);
  429. $lightness = $this->input->post('lightness',true);
  430. $clientid = $this->input->post('clientid',true);
  431. $version = $this->session->userdata('version');
  432. $lampArr = explode(',', $lamp_id);
  433. // $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
  434. $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/lampcontrol/dimming_asy';
  435. foreach ($lampArr as $l) {
  436. doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'lamp_id'=>$l,'lightness'=>$lightness,'role'=>$role]);
  437. }
  438. exit();
  439. }
  440. public function dimming_asy(){
  441. set_time_limit(0);
  442. $lamp_id = intval($this->input->post('lamp_id',true));
  443. $lightness = intval($this->input->post('lightness',true));
  444. $clientid = $this->input->post('clientid',true);
  445. $version = $this->input->post('version',true);
  446. if (empty($lamp_id)) {
  447. exit(json_result('0400', $this->response['0400'], array()));
  448. }
  449. $lampData = $this->Lamp_model->get_one(['id'=>$lamp_id],'number,address,id');
  450. $seq = get_seq();
  451. $seq = '01';
  452. $sendData = '12'.PROTOCOL_VERSION.$seq.'0000000000';
  453. $sendData .= $lightness > 0 ? '01' : '00';
  454. $lightness2 = empty($lightness) ? '00' : '00'.base_convert($lightness, 10, 16);
  455. $sendData .= strlen($lightness2) == 2 ? $lightness2 : substr($lightness2, -2);
  456. $sendData .= '00';
  457. $sendData .= crc16(pack('H*',$sendData));
  458. $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*',$sendData),30);
  459. if (empty($res)) {
  460. $data = ['code'=>'0016','status'=>2,'msg'=>$this->response['0016'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
  461. exit(send_websocket($clientid,$data));
  462. }else{
  463. $res['msg'] = unpack('H*', $res['msg'])[1];
  464. if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'], -4)) && strtolower(substr($res['msg'], 18,2)) == '01') {
  465. if ($lightness != 0) {
  466. $this->Lamp_model->update(array('status'=>1,'lighteness'=>$lightness),array('id'=>$lampData['id']));
  467. }else{
  468. $this->Lamp_model->update(array('status'=>0,'lighteness'=>0),array('id'=>$lampData['id']));
  469. }
  470. $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$lamp_id),'P.projectname,L.number,L.address');
  471. $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯调光\"{$lightness}%\"",0);
  472. $this->add_operation_log('cmd',"Light dimming:\"{$lampData['number']}\".Wireless module address:\"{$lampData['address']}\".Project name:\"{$lampData['projectname']}\".Lighteness:\"{$lightness}%\"",0,1);
  473. $data = ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
  474. exit(send_websocket($clientid,$data));
  475. }else{
  476. $data = ['code'=>'0010','status'=>2,'msg'=>$this->response['0010'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
  477. exit(send_websocket($clientid,$data));
  478. }
  479. }
  480. }
  481. // 删除灯控(支持批量处理)
  482. public function del(){
  483. $role = $this->get_user_info('role');
  484. $lamp_ids = $this->input->post('lamp_ids',true);
  485. if (empty($lamp_ids)) {
  486. exit(json_result('0400', $this->response['0400'], array()));
  487. }
  488. $ids = explode(",", $lamp_ids);
  489. // 获取灯控信息
  490. $dataArr = $this->Lamp_model->get_list(['id'=>$ids]);
  491. // 修改网络跟项目下灯控数量
  492. foreach ($dataArr as $data) {
  493. $temp = array();
  494. $temp['faultcount'] = $data['isfaulted'] > 0 ? 1 : 0;
  495. $temp['lampcount'] = 1;
  496. $temp['projectid'] = $data['projectid'];
  497. // $this->Network_model->minus_lamp_count($data);
  498. $this->Project_model->minus_lamp_count($temp);
  499. // $netData = $this->Network_model->getOne($data['networkid'],'gatewaytype,protocoltype');
  500. // if ($netData['protocoltype'] == 6) $this->remove_device('00000000'.$data['address']);
  501. // if (isset($netData) && $netData['gatewaytype'] == 'direct' && !empty($data['networkid'])) {// 直连设备
  502. // // 删除网络
  503. // // $this->Zone_model->delOne($data['networkid']);
  504. // $this->Network_model->delData(array('id'=>$data['networkid']));
  505. // $cmdstr = '{"cmd_type":"delete_network_cmd","cmd_id":'.$data['networkid'].'}';
  506. // // send_cmd($cmdstr,0);
  507. // if (isset($netData['protocoltype']) && $netData['protocoltype'] != 0) {
  508. // $cmdret = send_cmd($cmdstr,1,0,$netData['protocoltype']);
  509. // if ($cmdret === false) {
  510. // sleep(1);
  511. // $cmdret = send_cmd($cmdstr,1,0,$netData['protocoltype']);
  512. // }
  513. // }else{
  514. // $cmdret = send_cmd($cmdstr,0,0,$netData['protocoltype']);
  515. // if ($cmdret === false) {
  516. // sleep(1);
  517. // $cmdret = send_cmd($cmdstr,0,0,$netData['protocoltype']);
  518. // }
  519. // }
  520. // }
  521. }
  522. // 删除灯控告警信息
  523. $this->Alarm_model->delete(['lampid'=>$ids]);
  524. foreach ($ids as $v) {
  525. $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$v),'P.projectname,L.number,L.address');
  526. $this->add_operation_log('delete',"删除\"{$lampData['projectname']}\"项目下的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯",0);
  527. $this->add_operation_log('delete',"Delete lamp:\"{$lampData['number']}\".Wireless module address:\"{$lampData['address']}\".Project name:\"{$lampData['projectname']}\"",0,1);
  528. }
  529. $this->Lamp_model->delete(['id'=>$ids]);
  530. $this->RealtimeInfoLog_model->delete(['lampid'=>$ids]);
  531. exit(json_result('0000', $this->response['0000'], array()));
  532. }
  533. // 更新负载设置
  534. public function modbus_set_load(){
  535. $data['username'] = $this->input->post('username',true);
  536. $data['client_key'] = $this->input->post('client_key',true);
  537. $data['token'] = $this->input->post('token',true);
  538. $data['lamp_id'] = $this->input->post('lamp_id',true);
  539. $data['clientid'] = $this->input->post('clientid',true);
  540. $data['version'] = $this->session->userdata('version');
  541. // $data['light_voltage'] = $this->input->post('light_voltage',true);
  542. $data['intell_power'] = $this->input->post('intell_power',true);
  543. $data['light_type'] = $this->input->post('light_type',true);
  544. $data['first_light_time'] = $this->input->post('first_light_time',true);
  545. $data['first_light_human_power'] = $this->input->post('first_light_human_power',true);
  546. $data['first_light_unman_power'] = $this->input->post('first_light_unman_power',true);
  547. $data['second_light_time'] = $this->input->post('second_light_time',true);
  548. $data['second_light_human_power'] = $this->input->post('second_light_human_power',true);
  549. $data['second_light_unman_power'] = $this->input->post('second_light_unman_power',true);
  550. $data['third_light_time'] = $this->input->post('third_light_time',true);
  551. $data['third_light_human_power'] = $this->input->post('third_light_human_power',true);
  552. $data['third_light_unman_power'] = $this->input->post('third_light_unman_power',true);
  553. $data['fourth_light_time'] = $this->input->post('fourth_light_time',true);
  554. $data['fourth_light_human_power'] = $this->input->post('fourth_light_human_power',true);
  555. $data['fourth_light_unman_power'] = $this->input->post('fourth_light_unman_power',true);
  556. $data['fifth_light_time'] = $this->input->post('fifth_light_time',true);
  557. $data['fifth_light_human_power'] = $this->input->post('fifth_light_human_power',true);
  558. $data['fifth_light_unman_power'] = $this->input->post('fifth_light_unman_power',true);
  559. $data['sixth_light_time'] = $this->input->post('sixth_light_time',true);
  560. $data['sixth_light_human_power'] = $this->input->post('sixth_light_human_power',true);
  561. $data['sixth_light_unman_power'] = $this->input->post('sixth_light_unman_power',true);
  562. $data['seventh_light_time'] = $this->input->post('seventh_light_time',true);
  563. $data['seventh_light_human_power'] = $this->input->post('seventh_light_human_power',true);
  564. $data['seventh_light_unman_power'] = $this->input->post('seventh_light_unman_power',true);
  565. $data['tenth_light_time'] = $this->input->post('tenth_light_time',true);
  566. $data['tenth_light_human_power'] = $this->input->post('tenth_light_human_power',true);
  567. $data['tenth_light_unman_power'] = $this->input->post('tenth_light_unman_power',true);
  568. // foreach ($data as $key => $value) {
  569. // $data[$key] = urldecode($value);
  570. // }
  571. $lampArr = explode(',', $data['lamp_id']);
  572. // $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
  573. $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/lampcontrol/set_load_asy';
  574. foreach ($lampArr as $l) {
  575. $data['lamp_id'] = $l;
  576. doAsyncRequest($url,$data);
  577. }
  578. }
  579. public function set_load_asy(){
  580. $clientid = $this->input->post('clientid',true);
  581. $lampid = $this->input->post('lamp_id',true);
  582. $data['intell_power'] = $this->input->post('intell_power',true);
  583. $data['light_type'] = $this->input->post('light_type',true);
  584. $data['first_light_time'] = $this->input->post('first_light_time',true);
  585. $data['first_light_human_power'] = $this->input->post('first_light_human_power',true);
  586. $data['first_light_unman_power'] = $this->input->post('first_light_unman_power',true);
  587. $data['second_light_time'] = $this->input->post('second_light_time',true);
  588. $data['second_light_human_power'] = $this->input->post('second_light_human_power',true);
  589. $data['second_light_unman_power'] = $this->input->post('second_light_unman_power',true);
  590. $data['third_light_time'] = $this->input->post('third_light_time',true);
  591. $data['third_light_human_power'] = $this->input->post('third_light_human_power',true);
  592. $data['third_light_unman_power'] = $this->input->post('third_light_unman_power',true);
  593. $data['fourth_light_time'] = $this->input->post('fourth_light_time',true);
  594. $data['fourth_light_human_power'] = $this->input->post('fourth_light_human_power',true);
  595. $data['fourth_light_unman_power'] = $this->input->post('fourth_light_unman_power',true);
  596. $data['fifth_light_time'] = $this->input->post('fifth_light_time',true);
  597. $data['fifth_light_human_power'] = $this->input->post('fifth_light_human_power',true);
  598. $data['fifth_light_unman_power'] = $this->input->post('fifth_light_unman_power',true);
  599. $data['sixth_light_time'] = $this->input->post('sixth_light_time',true);
  600. $data['sixth_light_human_power'] = $this->input->post('sixth_light_human_power',true);
  601. $data['sixth_light_unman_power'] = $this->input->post('sixth_light_unman_power',true);
  602. $data['seventh_light_time'] = $this->input->post('seventh_light_time',true);
  603. $data['seventh_light_human_power'] = $this->input->post('seventh_light_human_power',true);
  604. $data['seventh_light_unman_power'] = $this->input->post('seventh_light_unman_power',true);
  605. $data['tenth_light_time'] = $this->input->post('tenth_light_time',true);
  606. $data['tenth_light_human_power'] = $this->input->post('tenth_light_human_power',true);
  607. $data['tenth_light_unman_power'] = $this->input->post('tenth_light_unman_power',true);
  608. $sendData = '';
  609. foreach ($data as $key => $value) {
  610. $word = '';
  611. if ($key == 'intell_power' || $key == 'light_type') {
  612. $word = substr('0'.base_convert($value, 10, 16), -1);
  613. }elseif (strpos('light_time', $key)) {
  614. $t = explode(':', $value);
  615. $m = $t[0]*60+$t[1];
  616. $word = substr('00'.base_convert($m, 10, 16), -2);
  617. }else{
  618. $word = substr('00'.base_convert($value, 10, 16), -2);
  619. }
  620. $sendData .= $word;
  621. }
  622. $sendData .= '0000';
  623. $lampData = $this->Lamp_model->getData(array('id'=>$lampid),'projectid,address,number');
  624. $res = $this->send_cmd('12',$lampData['address'],$sendData);
  625. if (!empty($res)) {
  626. $msg = unpack('H*', $res['msg'])[1];
  627. if (substr($msg, 18,2) == '02') {
  628. $data = ['code'=>'0010','status'=>2,'msg'=>$this->response['0010'],'data'=>['number'=>$lampData['number'],'type'=>'set_load','id'=>$lampid]];
  629. exit(send_websocket($clientid,$data));
  630. }
  631. $projectData = $this->Project_model->getData(array('id'=>$lampData['projectid']),'projectname');
  632. $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯负载参数");
  633. $this->add_operation_log('update',"Update the load setting of the street lamp:\"{$lampData['number']}\".Wireless module address:\"{$lampData['address']}\".Project name:\"{$projectData['projectname']}\".",0,1);
  634. $data = ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'set_load','id'=>$lampid]];
  635. exit(send_websocket($clientid,$data));
  636. }else{
  637. $data = ['code'=>'0016','status'=>2,'msg'=>$this->response['0016'],'data'=>['number'=>$lampData['number'],'type'=>'set_load','id'=>$lampid]];
  638. exit(send_websocket($clientid,$data));
  639. }
  640. }
  641. private function format_date($date){
  642. return intval(explode(':', $date)[0]) * 3600 + intval(explode(':', $date)[1]) * 60;
  643. }
  644. // 更新蓄电池设置
  645. public function set_battery(){
  646. $data['username'] = $this->input->post('username',true);
  647. $data['client_key'] = $this->input->post('client_key',true);
  648. $data['token'] = $this->input->post('token',true);
  649. $lamp_id = $this->input->post('lamp_id',true);
  650. $data['clientid'] = $this->input->post('clientid',true);
  651. $data['role'] = $this->input->post('role',true);
  652. $data['version'] = $this->session->userdata('version');
  653. $data['battery_count'] = $this->input->post('battery_count',true); // 电池串数
  654. $data['system_voltage'] = $this->input->post('system_voltage',true); // 电池电压
  655. $data['battery_type'] = $this->input->post('battery_type',true); //蓄电池类型
  656. $data['overvoltage'] = $this->input->post('overvoltage',true) * 1000; // 超压电压
  657. $data['char_limit_voltage'] = $this->input->post('char_limit_voltage',true) * 1000; // 充电限制电压
  658. $data['equ_char_voltage'] = $this->input->post('equ_char_voltage',true) * 1000; // 均衡充电电压
  659. $data['increa_char_voltage'] = $this->input->post('increa_char_voltage',true) * 1000; // 提升充电电压
  660. $data['float_char_voltage'] = $this->input->post('float_char_voltage',true) * 1000; // 浮充充电电压
  661. $data['increa_char_return_voltage'] = $this->input->post('increa_char_return_voltage',true) * 1000; // 提升充电返回电压
  662. $data['over_dischar_return_voltage'] = $this->input->post('over_dischar_return_voltage',true) * 1000; // 过放返回电压
  663. $data['battery_under_voltage'] = $this->input->post('battery_under_voltage',true) * 1000; // 欠压警告电压 新增
  664. $data['over_dischar_voltage'] = $this->input->post('over_dischar_voltage',true) * 1000; // 过放电压
  665. $data['discharge_limit_voltage'] = $this->input->post('discharge_limit_voltage',true) * 1000; // 放电限制电压 新增
  666. $data['equ_char_time'] = $this->input->post('equ_char_time',true) * 60; // 均衡充电时间
  667. $data['increa_char_time'] = $this->input->post('increa_char_time',true) * 60; // 提升充电时间
  668. $data['balan_char_interval'] = $this->input->post('balan_char_interval',true); // 均衡充电间隔
  669. $data['temper_compen_coeff'] = $this->input->post('temper_compen_coeff',true); // 温度补偿系数
  670. $data['char_upper_limit_temper'] = $this->input->post('char_upper_limit_temper',true); // 上限温度
  671. $data['char_lower_limit_temper'] = $this->input->post('char_lower_limit_temper',true); // 下限温度
  672. $data['reduced_start_voltage'] = $this->input->post('reduced_start_voltage',true) * 1000; // 智能功率起始电压
  673. $data['reduced_stop_voltage'] = $this->input->post('reduced_stop_voltage',true) * 1000; // 智能功率结束电压
  674. $data['light_voltage'] = $this->input->post('light_voltage',true); // 光控电压
  675. $data['light_delay_time'] = $this->input->post('light_delay_time',true); // 光控延时时间
  676. $lampArr = explode(',', $lamp_id);
  677. // $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
  678. $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/lampcontrol/set_battery_asy';
  679. foreach ($lampArr as $l) {
  680. $data['lampid'] = $l;
  681. doAsyncRequest($url,$data);
  682. }
  683. exit();
  684. }
  685. //更新蓄电池设置
  686. public function set_battery_asy(){
  687. $clientid = $this->input->post('clientid',true);
  688. $where['id'] = $this->input->post('lamp_id',true);//路灯id
  689. $data['system_voltage'] = $this->input->post('system_voltage',true); // 电池电压
  690. $data['battery_count'] = $this->input->post('battery_count',true); // 电池串数
  691. $data['battery_type'] = $this->input->post('battery_type',true); //蓄电池类型
  692. $data['overvoltage'] = $this->input->post('overvoltage',true); // 超压电压
  693. $data['char_limit_voltage'] = $this->input->post('char_limit_voltage',true); // 充电限制电压
  694. $data['equ_char_voltage'] = $this->input->post('equ_char_voltage',true); // 均衡充电电压
  695. $data['increa_char_voltage'] = $this->input->post('increa_char_voltage',true); // 提升充电电压
  696. $data['float_char_voltage'] = $this->input->post('float_char_voltage',true); // 浮充充电电压
  697. $data['increa_char_return_voltage'] = $this->input->post('increa_char_return_voltage',true); // 提升充电返回电压
  698. $data['over_dischar_return_voltage'] = $this->input->post('over_dischar_return_voltage',true); // 过放返回电压
  699. $data['battery_under_voltage'] = $this->input->post('battery_under_voltage',true); // 欠压警告电压 新增
  700. $data['over_dischar_voltage'] = $this->input->post('over_dischar_voltage',true); // 过放电压
  701. $data['discharge_limit_voltage'] = $this->input->post('discharge_limit_voltage',true) * 60; // 放电限制电压 新增
  702. $data['equ_char_time'] = $this->input->post('equ_char_time',true) * 60; // 均衡充电时间
  703. $data['increa_char_time'] = $this->input->post('increa_char_time',true) * 60; // 提升充电时间
  704. $data['balan_char_interval'] = $this->input->post('balan_char_interval',true); // 均衡充电间隔
  705. $data['temper_compen_coeff'] = $this->input->post('temper_compen_coeff',true); // 温度补偿系数
  706. $data['char_upper_limit_temper'] = $this->input->post('char_upper_limit_temper',true); // 上限温度
  707. $data['char_lower_limit_temper'] = $this->input->post('char_lower_limit_temper',true); // 下限温度
  708. $data['reduced_start_voltage'] = $this->input->post('reduced_start_voltage',true); // 智能功率起始电压
  709. $data['reduced_stop_voltage'] = $this->input->post('reduced_stop_voltage',true); // 智能功率结束电压
  710. $data['light_voltage'] = $this->input->post('light_voltage',true); // 光控电压
  711. $data['light_delay_time'] = $this->input->post('light_delay_time',true); // 光控延时时间
  712. $lampData = $this->Lamp_model->get_one($where,'projectid,address,number');
  713. $sendData = '';
  714. foreach ($data as $k=>$v) {
  715. if ($k == 'system_voltage' || $k == 'light_voltage' || $k == 'light_delay_time' || $k == 'balan_char_interval' || $k == 'temper_compen_coeff') {
  716. $word = substr('00'.base_convert($v, 10, 16), -2);
  717. }elseif ($k == 'char_upper_limit_temper' || $k == 'char_lower_limit_temper') {
  718. if ($v < 0){
  719. $word = '1'.substr('0000000000000000'.base_convert(substr('0000'.base_convert(abs($v), 10, 16), -4), 16, 2), -15);
  720. $word = substr('0000'.base_convert($word, 2, 16), -4);
  721. }else{
  722. $word = substr('0000'.base_convert($v, 10, 16), -4);
  723. }
  724. }elseif ($k == 'battery_type' || $k == 'battery_count') {
  725. $word = substr('0'.base_convert($v, 10, 16), -1);
  726. }else{
  727. $word = substr('0000'.base_convert($v, 10, 16), -4);
  728. }
  729. $sendData .= $word;
  730. }
  731. $res = $this->send_cmd('10',$lampData['address'],$sendData);
  732. if (!empty($res)) {
  733. $projectData = $this->Project_model->get_one(array('id'=>$lampData['projectid']),'projectname');
  734. $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯蓄电池参数");
  735. $this->add_operation_log('update',"Update the lamp battery setting:\"{$lampData['number']}\".Wireless module address:\"{$lampData['address']}\".Project name:\"{$projectData['projectname']}\".",0,1);
  736. $data = ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'set_battery','id'=>$lamp_id]];
  737. exit(send_websocket($clientid,$data));
  738. }else{
  739. $data = ['code'=>'0016','status'=>2,'msg'=>$this->response['0016'],'data'=>['number'=>$lampData['number'],'type'=>'set_battery','id'=>$lamp_id]];
  740. exit(send_websocket($clientid,$data));
  741. }
  742. }
  743. // 添加编辑灯控
  744. public function save() {
  745. $role = $this->get_user_info('role');
  746. $where['id'] = $this->input->post('lamp_id',true);
  747. $data['projectid'] = $this->input->post('project_id',true);
  748. $data['number'] = $this->input->post('lamp_no',true);
  749. $data['section'] = $this->input->post('section',true);
  750. $data['address'] = $this->input->post('address',true);
  751. $data['longitude'] = doubleval($this->input->post('longitude',true));
  752. $data['latitude'] = doubleval($this->input->post('latitude',true));
  753. if ($data['longitude'] < -180 || $data['longitude'] > 180) exit(json_result('0421',$this->response['0421']));
  754. if ($data['latitude'] < -90 || $data['latitude'] > 90) exit(json_result('0422',$this->response['0422']));
  755. $poleheight = $this->input->post('poleheight',true);
  756. if ($poleheight !== null) {
  757. $data['poleheight'] = $poleheight;
  758. }
  759. $data["polediameter"] = $this->input->post('polediameter',true);
  760. $data["polematerial"] = $this->input->post('polematerial',true);
  761. $data["lighttype"] = $this->input->post('lighttype',true);
  762. $data["lamptype"] = $this->input->post('lamptype',true);
  763. $data["boardtype"] = $this->input->post('boardtype',true);
  764. $data["batterytype"] = $this->input->post('batterytype',true);
  765. $data["boardpower"] = $this->input->post('boardpower',true);
  766. $data["batteryah"] = $this->input->post('batteryah',true);
  767. $wattage = $this->input->post('wattage',true);
  768. $data["wattage"] = !empty($wattage) ? $wattage : 0;
  769. $supplier = $this->input->post('supplier',true);
  770. $data["supplier"] = !empty($supplier) ? $supplier : 0;
  771. $po = $this->input->post('po',true);
  772. $data["po"] = !empty($po) ? $po : 0;
  773. $manu = $this->input->post('manu',true);
  774. $data["manu"] = !empty($manu) ? $manu : 0;
  775. $protocoltype = $this->input->post('protocoltype',true);
  776. $data['protocoltype'] = $protocoltype;
  777. $loadtype = $this->input->post('loadtype',true);
  778. if ($loadtype !== null) {
  779. $data["loadtype"] = intval($loadtype);
  780. }
  781. $role = $this->get_user_info('role');
  782. $company = $this->get_user_info('company');
  783. if ($role != SYSTEM_ADMIN) {
  784. if ($role == 2) {
  785. $data["manu"] = $company;
  786. }elseif ($role == 4) {
  787. $data["po"] = $company;
  788. }else{
  789. exit(json_result('0013',$this->response['0013']));
  790. }
  791. }
  792. // 控制器协议类型
  793. $version = $this->session->userdata('version');
  794. if (strlen($data['number']) > 6) exit(json_result('0408', $this->response['0408'], array()));
  795. if (empty($data['projectid'])) exit(json_result('0308', $this->response['0308'], array()));
  796. if (empty($data['number'])) exit(json_result('0409', $this->response['0409'], array()));
  797. if (!is_numeric($data['number']) || $data['number'] < 1 || $data['number'] > 999999) exit(json_result('0410', $this->response['0410'], array()));
  798. if (empty($data['address'])) exit(json_result('0805', $this->response['0805'], array()));
  799. if (is_float($data['address'])) {
  800. $data['address'] = strval(intval(ceil($data['address'])));
  801. }
  802. $is_true = preg_match('/^[A-Fa-f0-9]+$/', $data['address']);
  803. if (empty($is_true)) exit(json_result('0414', $this->response['0414'], array()));
  804. $data = remove_null_params($data);
  805. if (empty($where['id'])) {
  806. if ($this->Lamp_model->getDataCount(array('number'=>$data['number'], 'projectid'=>$data['projectid'])) > 0) {
  807. exit(json_result('0401', $this->response['0401'], array()));
  808. }
  809. $lamp = $this->Lamp_model->get_one(array('address'=>$data['address']),'projectid,number');
  810. if (!empty($lamp)) {
  811. $projectid = $lamp['projectid'];
  812. $number = $lamp['number'];
  813. $projectname = $this->Project_model->getNameById($projectid);
  814. if (empty($version)) {
  815. exit(json_result('0404', "该无线模块地址已被【{$projectname}】项目,【{$number}】编号的灯控使用", array()));
  816. }else{
  817. exit(json_result('other', "The wireless module address has been used by \"{$projectname}\" project, and \"{$number}\" serial number", array()));
  818. }
  819. }
  820. $projectData = $this->Project_model->get_one(['id'=>$data['projectid']],'patrolinterval,cityid,projectname');
  821. if (empty($data['longitude']) && empty($data['latitude']) && !empty($projectData['cityid'])) {
  822. $globalData = $this->Global_location_model->getOne($projectData['cityid'],'longitude,latitude');
  823. $data['longitude'] = $globalData['longitude'];
  824. $data['latitude'] = $globalData['latitude'];
  825. }
  826. $data['createtime'] = date('Y-m-d H:i:s',time());
  827. $lampid = $this->Lamp_model->add($data);
  828. $this->Project_model->add_lamp_count(array('lampcount'=>1,'faultcount'=>0,'projectid'=>$data['projectid']));
  829. $lampId = intval($lampid);
  830. $this->add_operation_log('insert',"在\"{$projectData['projectname']}\"项目里添加编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯",0);
  831. $this->add_operation_log('insert',"Add lamp:\"{$data['number']}\".Wireless module address:\"{$data['address']}\".Project name:\"{$projectData['projectname']}\"",0,1);
  832. } else {
  833. if ($this->Lamp_model->getDataCount(array('number'=>$data['number'], 'projectid'=>$data['projectid']), $where['id']) > 0) {
  834. exit(json_result('0401', $this->response['0401'], array()));
  835. }
  836. $lamp = $this->Lamp_model->get_one(array('address'=>$data['address'], 'id !='=>$where['id']),'projectid,number');
  837. if (!empty($lamp)) {
  838. $projectid = $lamp['projectid'];
  839. $number = $lamp['number'];
  840. $projectname = $this->Project_model->getNameById($projectid);
  841. if (empty($version)) {
  842. exit(json_result('0404', "该无线模块地址已被【{$projectname}】项目,【{$number}】编号的灯控使用", array()));
  843. }else{
  844. exit(json_result('other', "The wireless module address has been used by \"{$projectname}\" project, and \"{$number}\" serial number", array()));
  845. }
  846. }
  847. $old = $this->Lamp_model->get_one($where,'isfaulted,projectid,address,protocoltype');
  848. if ($old['projectid'] != $data['projectid']) {
  849. $new['projectid'] = $data['projectid'];
  850. $new['faultcount'] = $old['isfaulted'] > 0 ? 1 : 0;
  851. $this->Project_model->add_lamp_count($new);
  852. $old['faultcount'] = $new['faultcount'];
  853. $this->Project_model->minus_lamp_count($old);
  854. }
  855. // $oldNet = $this->Network_model->getData(array('id'=>$old['networkid']));
  856. if ($old['address'] != $data['address'] || $protocoltype != $old['protocoltype']) {
  857. }
  858. $this->Lamp_model->update($data,$where);
  859. $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
  860. $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯信息");
  861. $this->add_operation_log('update',"Update lamp:\"{$data['number']}\".Wireless module address:\"{$data['address']}\".Project name:\"{$projectData['projectname']}\".",0,1);
  862. //更新负载设置和蓄电池设置
  863. if (isset($_POST['workmode'])) {
  864. $res = $this->load_update();
  865. if ($res['result'] == 1) {
  866. $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯负载参数");
  867. $this->add_operation_log('update',"Update the load setting of the street lamp:\"{$data['number']}\".Wireless module address:\"{$data['address']}\".Project name:\"{$projectData['projectname']}\".Network name:\"{$networkData['networkname']}\".",1,1);
  868. }else{
  869. exit(json_result($res['msg'],$res['msg'],array()));
  870. }
  871. }
  872. if (isset($_POST['capacity'])) {
  873. $res = $this->battery_update();
  874. if ($res['result'] == 1) {
  875. $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯蓄电池参数");
  876. $this->add_operation_log('update',"Update the lamp battery setting:\"{$data['number']}\".Wireless module address:\"{$data['address']}\".Project name:\"{$projectData['projectname']}\".Network name:\"{$networkData['networkname']}\".",1,1);
  877. }else{
  878. exit(json_result($res['msg'],$res['msg'],array()));
  879. }
  880. }
  881. $lampId = intval($where['id']);
  882. }
  883. exit(json_result('0000', $this->response['0000'], array("id" => $lampId)));
  884. }
  885. private function format_date_en($date){
  886. $h = floor($date/3600);
  887. $h = $h > 10 ? $h : '0'.$h;
  888. $t = floor($date%3600/60);
  889. $t = $t > 10 ? $t : '0'.$t;
  890. return $h.':'.$t;
  891. }
  892. // 新控制器获取默认参数值
  893. public function get_data(){
  894. $type = $this->input->post('type',true);
  895. $type = empty($type) ? 1 : $type;
  896. $update = array();
  897. if ($type == 1) { // 负载参数
  898. $update['intell_power'] = '2';
  899. $update['light_type'] = '1';
  900. $update['first_light_time'] = '00:30';
  901. $update['first_light_human_power'] = '50';
  902. $update['first_light_unman_power'] = '20';
  903. $update['second_light_time'] = '00:30';
  904. $update['second_light_human_power'] = '70';
  905. $update['second_light_unman_power'] = '30';
  906. $update['third_light_time'] = '02:00';
  907. $update['third_light_human_power'] = '100';
  908. $update['third_light_unman_power'] = '50';
  909. $update['fourth_light_time'] = '00:30';
  910. $update['fourth_light_human_power'] = '70';
  911. $update['fourth_light_unman_power'] = '30';
  912. $update['fifth_light_time'] = '00:30';
  913. $update['fifth_light_human_power'] = '70';
  914. $update['fifth_light_unman_power'] = '30';
  915. $update['sixth_light_time'] = '04:00';
  916. $update['sixth_light_human_power'] = '30';
  917. $update['sixth_light_unman_power'] = '10';
  918. $update['seventh_light_time'] = '00:00';
  919. $update['seventh_light_human_power'] = '100';
  920. $update['seventh_light_unman_power'] = '50';
  921. $update['tenth_light_time'] = '00:00';
  922. $update['tenth_light_human_power'] = '50';
  923. $update['tenth_light_unman_power'] = '20';
  924. }else{ // 蓄电池参数
  925. $update['system_voltage'] = '12'; // 电池电压
  926. $update['battery_type'] = '1'; //蓄电池类型
  927. $update['overvoltage'] = '15.5'; // 超压电压
  928. $update['char_limit_voltage'] = '15.0'; // 充电限制电压
  929. $update['equ_char_voltage'] = '14.6'; // 均衡充电电压
  930. $update['increa_char_voltage'] = '14.4'; // 提升充电电压
  931. $update['float_char_voltage'] = '13.8'; // 浮充充电电压
  932. $update['increa_char_return_voltage'] = '13.2'; // 提升充电返回电压
  933. $update['over_dischar_return_voltage'] = '12.6'; // 过放返回电压
  934. $update['battery_under_voltage'] = '12.0'; // 欠压警告电压 新增
  935. $update['over_dischar_voltage'] = '11.1'; // 过放电压
  936. $update['discharge_limit_voltage'] = '10.6'; // 放电限制电压 新增
  937. $update['equ_char_time'] = '120'; // 均衡充电时间
  938. $update['increa_char_time'] = '120'; // 提升充电时间
  939. $update['balan_char_interval'] = '30'; // 均衡充电间隔
  940. $update['temper_compen_coeff'] = '5'; // 温度补偿系数
  941. $update['char_upper_limit_temper'] = '60'; // 上限温度
  942. $update['char_lower_limit_temper'] = '-30'; // 下限温度
  943. $update['reduced_start_voltage'] = '11.5'; // 智能功率起始电压
  944. $update['reduced_stop_voltage'] = '10.0'; // 智能功率结束电压
  945. $update['light_voltage'] = '5'; // 光控电压
  946. $update['light_delay_time'] = '20'; // 光控延时时间
  947. }
  948. exit(json_result('0000',$this->response['0000'],$update));
  949. }
  950. // 新控制器获取参数设置
  951. public function get_load() {
  952. $lamp_id = $this->input->post('lamp_id', true);
  953. if (empty($lamp_id)) {
  954. exit(json_result('0400', $this->response['0400'], array()));
  955. }
  956. $lampData = $this->Lamp_model->get_one(['id'=>$lamp_id],'address,number');
  957. $res = $this->send_cmd('13',$lampData['address'],'');
  958. if (empty($res)) {
  959. exit(json_result('0016', $this->response['0016']));
  960. }else{
  961. // if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
  962. $res['msg'] = unpack('H*', $res['msg'])[1];
  963. $data = array();
  964. $data['intell_power'] = base_convert(substr($res['msg'], 18,1), 16, 10).''; // 智能功率
  965. $data['light_type'] = base_convert(substr($res['msg'], 19,1), 16, 10).''; // 灯控模式
  966. $data['first_light_time'] = base_convert(substr($res['msg'], 20,2), 16, 10).''; // 第一段亮灯时间
  967. $data['first_light_human_power'] = base_convert(substr($res['msg'], 22,2), 16, 10).''; // 第一段有人功率
  968. $data['first_light_unman_power'] = base_convert(substr($res['msg'], 24,2), 16, 10).''; // 第一段无人功率
  969. $data['second_light_time'] = base_convert(substr($res['msg'], 26,2), 16, 10).''; // 第二段亮灯时间
  970. $data['second_light_human_power'] = base_convert(substr($res['msg'], 28,2), 16, 10).''; // 第二段有人功率
  971. $data['second_light_unman_power'] = base_convert(substr($res['msg'], 30,2), 16, 10).''; // 第二段无人功率
  972. $data['third_light_time'] = base_convert(substr($res['msg'], 32,2), 16, 10).''; // 第三段亮灯时间
  973. $data['third_light_human_power'] = base_convert(substr($res['msg'], 34,2), 16, 10).''; // 第三段有人功率
  974. $data['third_light_unman_power'] = base_convert(substr($res['msg'], 36,2), 16, 10).''; // 第三段无人功率
  975. $data['fourth_light_time'] = base_convert(substr($res['msg'], 38,2), 16, 10).''; // 第四段亮灯时间
  976. $data['fourth_light_human_power'] = base_convert(substr($res['msg'], 40,2), 16, 10).''; // 第四段有人功率
  977. $data['fourth_light_unman_power'] = base_convert(substr($res['msg'], 42,2), 16, 10).''; // 第四段无人功率
  978. $data['fifth_light_time'] = base_convert(substr($res['msg'], 44,2), 16, 10).''; // 第五段亮灯时间
  979. $data['fifth_light_human_power'] = base_convert(substr($res['msg'], 46,2), 16, 10).''; // 第五段有人功率
  980. $data['fifth_light_unman_power'] = base_convert(substr($res['msg'], 48,2), 16, 10).''; // 第五段无人功率
  981. $data['sixth_light_time'] = base_convert(substr($res['msg'], 50,2), 16, 10).''; // 第六段亮灯时间
  982. $data['sixth_light_human_power'] = base_convert(substr($res['msg'], 52,2), 16, 10).''; // 第六段有人功率
  983. $data['sixth_light_unman_power'] = base_convert(substr($res['msg'], 54,2), 16, 10).''; // 第六段无人功率
  984. $data['seventh_light_time'] = base_convert(substr($res['msg'], 56,2), 16, 10).''; // 第七段亮灯时间
  985. $data['seventh_light_human_power'] = base_convert(substr($res['msg'], 58,2), 16, 10).''; // 第七段有人功率
  986. $data['seventh_light_unman_power'] = base_convert(substr($res['msg'], 60,2), 16, 10).''; // 第七段无人功率
  987. $data['tenth_light_time'] = base_convert(substr($res['msg'], 62,2), 16, 10).''; // 晨亮时间
  988. $data['tenth_light_human_power'] = base_convert(substr($res['msg'], 64,2), 16, 10).''; // 晨亮有人功率
  989. $data['tenth_light_unman_power'] = base_convert(substr($res['msg'], 68,2), 16, 10).''; // 晨亮无人功率
  990. // $data['begin_voltage'] = round(base_convert(substr($res['msg'], 146,4), 16, 10)/10,1).''; // 智能功率USER1起始电压
  991. // $data['end_voltage'] = round(base_convert(substr($res['msg'], 150,4), 16, 10)/10,1).''; // 智能功率USER1终止电压
  992. // $data['end_current'] = round(base_convert(substr($res['msg'], 158,4), 16, 10)/100,2).''; // 智能功率USER1终止电流
  993. $res = $data;
  994. // }else{
  995. // exit(json_result('0010', $this->response['0010']));
  996. // }
  997. }
  998. // $res['load_current'] = number_format($res['load_current'],2);
  999. // $res['end_current'] = number_format($res['end_current'],2);
  1000. // $res['end_voltage'] = number_format($res['end_voltage'],1);
  1001. // $res['begin_voltage'] = number_format($res['begin_voltage'],1);
  1002. $res['first_light_time'] = $this->format_date_en($res['first_light_time']);
  1003. $res['second_light_time'] = $this->format_date_en($res['second_light_time']);
  1004. $res['third_light_time'] = $this->format_date_en($res['third_light_time']);
  1005. $res['fourth_light_time'] = $this->format_date_en($res['fourth_light_time']);
  1006. $res['fifth_light_time'] = $this->format_date_en($res['fifth_light_time']);
  1007. $res['sixth_light_time'] = $this->format_date_en($res['sixth_light_time']);
  1008. $res['seventh_light_time'] = $this->format_date_en($res['seventh_light_time']);
  1009. $res['tenth_light_time'] = $this->format_date_en($res['tenth_light_time']);
  1010. // unset($res['id']);
  1011. // unset($res['updatetime']);
  1012. $data = $res;
  1013. exit(json_result('0000', $this->response['0000'], $data));
  1014. }
  1015. // 获取蓄电池设置
  1016. public function battery_setting() {
  1017. $lamp_id = $this->input->post('lamp_id', true);
  1018. if (empty($lamp_id)) {
  1019. exit(json_result('0400', $this->response['0400'], array()));
  1020. }
  1021. $lampData = $this->Lamp_model->get_one(['id'=>$lamp_id],'address,number,projectid');
  1022. $res = $this->send_cmd('11',$lampData['address'],'');
  1023. // $lampData = $this->Lamp_model->getData(array('id'=>$lamp_id),'projectid,address,number');
  1024. // var_dump($res);
  1025. if (!empty($res)) {
  1026. $info = unpack('H*', $res['msg'])[1];
  1027. $data['system_voltage'] = base_convert(substr($info, 18,2), 16, 10); // 电池电压
  1028. $data['battery_count'] = base_convert(substr($info, 20,1), 16, 10); // 电池串数
  1029. $data['battery_type'] = base_convert(substr($info, 21,1), 16, 10); //蓄电池类型
  1030. $data['overvoltage'] = round(base_convert(substr($info, 22,4), 16, 10)/1000,1); // 超压电压
  1031. $data['char_limit_voltage'] = round(base_convert(substr($info, 26,4), 16, 10)/1000,1); // 充电限制电压
  1032. $data['equ_char_voltage'] = round(base_convert(substr($info, 30,4), 16, 10)/1000,1); // 均衡充电电压
  1033. $data['increa_char_voltage'] = round(base_convert(substr($info, 34,4), 16, 10)/1000,1); // 提升充电电压
  1034. $data['float_char_voltage'] = round(base_convert(substr($info, 38,4), 16, 10)/1000,1); // 浮充充电电压
  1035. $data['increa_char_return_voltage'] = round(base_convert(substr($info, 42,4), 16, 10)/1000,1); // 提升充电返回电压
  1036. $data['over_dischar_return_voltage'] = round(base_convert(substr($info, 46,4), 16, 10)/1000,1); // 过放返回电压
  1037. $data['battery_under_voltage'] = round(base_convert(substr($info, 50,4), 16, 10)/1000,1); // 欠压警告电压 新增
  1038. $data['over_dischar_voltage'] = round(base_convert(substr($info, 54,4), 16, 10)/1000,1); // 过放电压
  1039. $data['discharge_limit_voltage'] = round(base_convert(substr($info, 58,4), 16, 10)/1000,1); // 放电限制电压 新增
  1040. $data['equ_char_time'] = base_convert(substr($info, 62,4), 16, 10); // 均衡充电时间
  1041. $data['increa_char_time'] = base_convert(substr($info, 66,4), 16, 10); // 提升充电时间
  1042. $data['balan_char_interval'] = base_convert(substr($info, 70,2), 16, 10); // 均衡充电间隔
  1043. $data['temper_compen_coeff'] = base_convert(substr($info, 72,2), 16, 10); // 温度补偿系数
  1044. $char_upper_limit_temper = base_convert(substr($info, 74,2), 16, 2); // 上限温度
  1045. if (substr(substr('00000000'.$char_upper_limit_temper, -8), 0,1) == 1) {
  1046. $data['char_upper_limit_temper'] = 0 - base_convert(substr('0000000'.$char_upper_limit_temper, -7), 2, 10);
  1047. }else{
  1048. $data['char_upper_limit_temper'] = base_convert($char_upper_limit_temper, 2, 10);
  1049. }
  1050. $char_lower_limit_temper = base_convert(substr($info, 76,2), 16, 2); // 下限温度
  1051. if (substr(substr('00000000'.$char_lower_limit_temper, -8), 0,1) == 1) {
  1052. $data['char_lower_limit_temper'] = 0 - base_convert(substr('0000000'.$char_lower_limit_temper, -7), 2, 10);
  1053. }else{
  1054. $data['char_lower_limit_temper'] = base_convert($char_lower_limit_temper, 2, 10);
  1055. }
  1056. $data['reduced_start_voltage'] = round(base_convert(substr($info, 78,4), 16, 10)/1000,1); // 智能功率起始电压
  1057. $data['reduced_stop_voltage'] = round(base_convert(substr($info, 82,4), 16, 10)/1000,1); // 智能功率结束电压
  1058. $data['light_voltage'] = base_convert(substr($info, 86,2), 16, 10); // 光控电压
  1059. $data['light_delay_time'] = base_convert(substr($info, 88,2), 16, 10); // 光控延时时间
  1060. exit(json_result('0000',$this->response['0000'],$data));
  1061. }else{
  1062. exit(json_result('0016',$this->response['0016']));
  1063. }
  1064. }
  1065. // 灯控相关数据
  1066. public function viewloglist() {
  1067. $lamp_id = $this->input->post('lamp_id',true);
  1068. $datatype = $this->input->post('datatype',true);
  1069. if (empty($lamp_id)) {
  1070. exit(json_result('0400', $this->response['0400'], array()));
  1071. }
  1072. if ($datatype == 'lampinfo') {
  1073. $fields = "number,projectid,address,longitude,latitude,section,boardpower,lamptype,boardtype,batteryah,loadpower,devstatus,loadtype,supplier,upazilla,po,manu,protocoltype,updatetime,status";
  1074. $res = $this->Lamp_model->get_one(['id'=>$lamp_id],$fields);
  1075. if (empty($res)) {
  1076. exit(json_result('0000', $this->response['0006'], array()));
  1077. }
  1078. $version = $this->session->userdata('version');
  1079. if (!empty($version)) {
  1080. $polemaType = ['冷镀锌'=>'Cold galvanizing','热镀锌'=>'Hot galvanizing','不锈钢'=>'Stainless steel'];
  1081. $lightType = ['LED灯'=>'LED lamp','钠灯'=>'Sodium lamp','节能灯'=>'Energy saving light'];
  1082. $lampType = ['太阳能路灯'=>'Solar street lamp','景观灯'=>'Landscape lamp','庭院灯'=>'Garden light'];
  1083. $boardType = ['单晶板'=>'Single crystal plate','多晶板'=>'Polycrystalline plate','非晶板'=>'Amorphous plate'];
  1084. $batteryType = ['铅酸电池'=>'Lead acid battery','锂电池'=>'Lithium battery'];
  1085. if (!preg_match('/^[A-Za-z\s*]+$/', $res['lamptype'])) {
  1086. $res['lamptype'] = empty($lampType[$res['lamptype']]) ? '' : $lampType[$res['lamptype']];
  1087. }
  1088. if (!preg_match('/^[A-Za-z\s*]+$/', $res['boardtype'])) {
  1089. $res['boardtype'] = empty($boardType[$res['boardtype']]) ? '' : $boardType[$res['boardtype']];
  1090. }
  1091. }else{
  1092. $polemaType = ['Cold galvanizing'=>'冷镀锌','Hot galvanizing'=>'热镀锌','Stainless steel'=>'不锈钢'];
  1093. $lightType = ['LED lamp'=>'LED灯','Sodium lamp'=>'钠灯','Energy saving light'=>'节能灯'];
  1094. $lampType = ['Solar street lamp'=>'太阳能路灯','Landscape lamp'=>'景观灯','Garden light'=>'庭院灯'];
  1095. $boardType = ['Single crystal plate'=>'单晶板','Polycrystalline plate'=>'多晶板','Amorphous plate'=>'非晶板'];
  1096. $batteryType = ['Lead acid battery'=>'铅酸电池','Lithium battery'=>'锂电池'];
  1097. if (preg_match('/^[A-Za-z\s*]+$/', $res['lamptype'])) {
  1098. $res['lamptype'] = empty($lampType[$res['lamptype']]) ? '' : $lampType[$res['lamptype']];
  1099. }
  1100. if (preg_match('/^[A-Za-z\s*]+$/', $res['boardtype'])) {
  1101. $res['boardtype'] = empty($boardType[$res['boardtype']]) ? '' : $boardType[$res['boardtype']];
  1102. }
  1103. }
  1104. // if ($res["isfaulted"] == 0) {
  1105. // $res["isfaulted"] = '正常';
  1106. // }else{
  1107. // }
  1108. // $alarmData = $this->Alarm_model->get_one_by_filter(array('lampid'=>$lamp_id,'status'=>0),'stralarmtype');
  1109. // $version = $this->session->userdata('version');
  1110. // if (empty($version)) {
  1111. // $res['isfaulted'] = empty($alarmData['stralarmtype']) ? '正常' : $alarmData['stralarmtype'];
  1112. // }else{
  1113. // $res['isfaulted'] = empty($alarmData['stralarmtype']) ? 'Normal' : alarm_translate($alarmData['stralarmtype']);
  1114. // }
  1115. $timezone = $this->Project_model->get_timezone_by_projectid($res['projectid']);
  1116. $res["updatetime"] = date_change($res["updatetime"],8,$timezone['value']);
  1117. $data = array(
  1118. "lamp_no" => $res["number"],
  1119. "project_name" => $this->Project_model->getNameById($res["projectid"]),
  1120. "address" => $res["address"],
  1121. "longitude" => $res["longitude"],
  1122. "latitude" => $res["latitude"],
  1123. "isfaulted" => $res["devstatus"],
  1124. "section" => $res["section"],
  1125. "boardpower" => floatval($res["boardpower"]),
  1126. "lamptype" => $res['lamptype'],
  1127. "boardtype" => $res['boardtype'],
  1128. "batteryah" => $res["batteryah"],
  1129. "loadpower" => $res["loadpower"],
  1130. 'status' => $res['status'],
  1131. // 'batterytype' => $res['batterytype'],
  1132. 'protocoltype'=>intval($res['protocoltype']),
  1133. 'supplier'=>intval($res['supplier']),
  1134. 'upazilla'=>intval($res['upazilla']),
  1135. 'po'=>intval($res['po']),
  1136. 'manu'=>intval($res['manu']),
  1137. "updatetime" => $res["updatetime"],
  1138. 'loadtype' => $res['loadtype']
  1139. );
  1140. // $msisdn = $this->db->query('select iccid from msisdn where imsi = "'.$res['simid'].'"')->row_array();
  1141. // if (empty($msisdn) || empty($msisdn['iccid'])) {
  1142. // $data['iccid'] = '';
  1143. // }else{
  1144. // $data['iccid'] = $msisdn['iccid'];
  1145. // }
  1146. // if ($res['protocoltype'] != 1 && $res['protocoltype'] != 2) {
  1147. // unset($data['packageSurplus']);
  1148. // unset($data['simid']);
  1149. // }
  1150. // $username = $this->get_user_info('username');
  1151. // if ($username == 'zns') {
  1152. // $data['monthly_can_usage'] = intval($res['monthly_can_usage']);
  1153. // $data['monthly_already_usage'] = round($res['monthly_already_usage'],2);
  1154. // }
  1155. exit(json_result('0000', $this->response['0000'], $data));
  1156. }
  1157. $table_map = array('system_info_log','lamp_info_log','solar_info_log','battery_info_log','electric_info_log','history_info_log');
  1158. $table = in_array($datatype, $table_map) ? $datatype : 'system_info_log';
  1159. $res = $this->Lamp_model->getForInfoLog($lamp_id, $table);
  1160. $lampData = $this->Lamp_model->get_one(['id'=>$lamp_id],'projectid');
  1161. $timezone = $this->Project_model->get_timezone_by_projectid($lampData['projectid']);
  1162. if(!$res){
  1163. exit(json_result('0000', $this->response['0006'], array()));
  1164. }
  1165. if ($table == 'battery_info_log') {
  1166. if (isset($res['daydischargemincurrent'])) {
  1167. $h = floor($res['daydischargemincurrent']/3600);
  1168. $h = $h < 10 ? '0'.$h : $h;
  1169. $i = intval($res['daydischargemincurrent']/60)%60;
  1170. $i = $i < 10 ? '0'.$i : $i;
  1171. $res['daydischargemincurrent'] = $h.':'.$i;
  1172. }
  1173. if (isset($res['daychargemincurrent'])) {
  1174. $h = floor($res['daychargemincurrent']/3600);
  1175. $h = $h < 10 ? '0'.$h : $h;
  1176. $i = intval($res['daychargemincurrent']/60)%60;
  1177. $i = $i < 10 ? '0'.$i : $i;
  1178. $res['daychargemincurrent'] = $h.':'.$i;
  1179. }
  1180. }
  1181. $res["updatetime"] = date_change($res["updatetime"],8,$timezone['value']);
  1182. exit(json_result('0000', $this->response['0000'], $res));
  1183. }
  1184. // 灯控历史数据
  1185. public function historylist() {
  1186. $lamp_id = $this->input->post('lamp_id',true);
  1187. $datatype = $this->input->post('datatype',true);
  1188. $page = intval($this->input->post('page',true));
  1189. $count = intval($this->input->post('count',true));
  1190. $count = empty($count) ? 16 : $count;
  1191. if (empty($lamp_id)) {
  1192. exit(json_result('0400', $this->response['0400'], array()));
  1193. }
  1194. $date = $this->input->post('date',true);
  1195. $beginDate = '';
  1196. $endDate = '';
  1197. // 按时区修改时间
  1198. $lampData = $this->Lamp_model->get_one(['id'=>$lamp_id],'projectid,protocoltype');
  1199. $timezone = $this->Project_model->get_timezone_by_projectid($lampData['projectid']);
  1200. if (!empty($date)) {
  1201. $dateArr = explode('/', $date);
  1202. $beginDate = date('Y-m-d H:i:s',strtotime($dateArr[0]) - 8*3600 + $timezone['value']*3600);
  1203. $endDate = date('Y-m-d H:i:s',strtotime($dateArr[1]) - 8*3600 + $timezone['value']*3600);
  1204. // $beginDate = $dateArr[0];
  1205. // $endDate = $dateArr[1];
  1206. if (strtotime($endDate) - strtotime($beginDate) > 31*24*3600) {
  1207. exit(json_result('0418',$this->response['0418']));
  1208. }
  1209. }else{
  1210. $beginDate = date('Y-m-d H:i:s',strtotime(date('Y-m-d 00:00:00',time())) - 8*3600 + $timezone['value']*3600);
  1211. $endDate = date('Y-m-d H:i:s',strtotime(date('Y-m-d 23:59:59',time())) - 8*3600 + $timezone['value']*3600);
  1212. }
  1213. if($page < 1){
  1214. $page = 1;
  1215. }
  1216. $download = intval($this->input->post('download'));
  1217. if (!empty($download)) {
  1218. if (empty($date)) exit(json_result('0007',$this->response['0007']));
  1219. $dataList = array();
  1220. // 路灯
  1221. $lampInfoList = $this->Lamp_model->getListForInfoLog($lamp_id, 'lamp_info_log', null, null,$beginDate,$endDate);
  1222. $temp = array();
  1223. $i = 1;
  1224. if (empty($this->version)) {
  1225. $lampStatusArr = array(0=>'关灯',1=>'开灯');
  1226. $title = array('序号','亮度%','状态','电压(V)','电流(A)','功率(W)','当天放电最大功率(W)','当天亮灯时长(HH:MM)','更新时间');
  1227. $name = '路灯';
  1228. }else{
  1229. $lampStatusArr = array(0=>'off',1=>'on');
  1230. $title = array('Number','Brightness%','Status','Voltage(V)','Current(A)','Power(W)','Maximum discharge power of the day(W)','dayLighting duration(HH:MM)','Update time');
  1231. $name = 'Lamp';
  1232. }
  1233. foreach ($lampInfoList as $k=>$l) {
  1234. $temp[$k][] = $i;
  1235. $temp[$k][] = $l['lighteness'];
  1236. $temp[$k][] = $lampStatusArr[$l['lampstatus']];
  1237. $temp[$k][] = $l['lampvoltage'];
  1238. $temp[$k][] = $l['lampcurrent'];
  1239. $temp[$k][] = $l['lamppower'];
  1240. $temp[$k][] = $l['daydischarmaxpow'];
  1241. $h = floor($l['daychargemincurrent'] / 3600);
  1242. $h = $h < 10 ? '0'.$h : $h;
  1243. $m = ceil($l['daychargemincurrent'] % 3600 / 60);
  1244. $m = $m < 10 ? '0'.$m : $m;
  1245. $temp[$k][] = $h.':'.$m;
  1246. $temp[$k][] = date_change($l["updatetime"],8,$timezone['value']);
  1247. $i ++;
  1248. }
  1249. $title1 = [];
  1250. foreach ($title as $t) {
  1251. $title1[0][] = $t;
  1252. }
  1253. $data = array_merge($title1,$temp);
  1254. $dataList[] = array('name'=>$name,'dataList'=>$data);
  1255. // 蓄电池
  1256. $lampInfoList = $this->Lamp_model->getListForInfoLog($lamp_id, 'battery_info_log', null, null,$beginDate,$endDate);
  1257. $temp = array();
  1258. $i = 1;
  1259. if (empty($this->version)) {
  1260. $name = '蓄电池';
  1261. if ($lampData['protocoltype'] == 2) {
  1262. $chargeStageMap = array(0=>'没有充电',1=>'等待',2=>'初始',3=>'MPPT',4=>'恒压',5=>'恒流',6=>'浮充',7=>'均衡',8=>'提升',9=>'涓流',10=>'完成',''=>'未知状态');
  1263. }else{
  1264. $chargeStageMap = array(0=>'没有充电',1=>'启动充电',6=>'限流',7=>'充满',16=>'MPPT充电',32=>'均衡充电',48=>'提升充电',64=>'浮充',''=>'未知状态');
  1265. }
  1266. $battstatusArr = array(0=>'空闲',1=>'放电',2=>'充电',3=>'一边充电一边放电',''=>'未知状态');
  1267. $unknown = '未知状态';
  1268. $title = array('序号','系统电压(V)','系统电流(A)','MOS温度(℃)','电池状态','充电阶段','电池电压(V)','当天最低电压(V)','当天最高电压(V)','电池温度(℃)','当天电池最低温度(℃)','当天电池最高温度(℃)','更新时间');
  1269. }else{
  1270. $name = 'Battery';
  1271. if ($lampData['protocoltype'] == 2) {
  1272. $chargeStageMap = array(0=>'There is no charge',1=>'wait for',2=>'initial',3=>'MPPT',4=>'Constant voltage',5=>'constant current',6=>'floating charge',7=>'equilibrium',8=>'Promote',9=>'Trickle',10=>'complete',''=>'Unknown state');
  1273. }else{
  1274. $chargeStageMap = array(0=>'There is no charge',1=>'Start charging',6=>'Current limiting',7=>'Full',16=>'MPPT charging',32=>'Equalizing charge',48=>'Improve charging',64=>'Floating',''=>'Unknown state');
  1275. }
  1276. $battstatusArr = array(0=>'Free',1=>'Discharge',2=>'Charge',3=>'Discharge while charging',''=>'Unknown state');
  1277. $unknown = 'Unknown state';
  1278. $title = array('Serial number','System voltage(V)','System current(A)','MOS temperature(℃)','Battery status','Charging stage','Battery voltage(V)','The lowest voltage of the day(V)','Maximum Voltage of the Day(V)','Battery temperature(℃)','Battery minimum temperature for the day(℃)','The highest temperature of the battery on that day(℃)','Update time');
  1279. }
  1280. $indexArr = [2=>'16',3=>'32',4=>'48',5=>'64'];
  1281. foreach ($lampInfoList as $k=>$l) {
  1282. $temp[$k][] = $i;
  1283. $temp[$k][] = $l['sysvoltage'];
  1284. $temp[$k][] = $l['syscurrent'];
  1285. $temp[$k][] = $l['temper'];
  1286. $temp[$k][] = $battstatusArr[$l['battstatus']];
  1287. if ($lampData['protocoltype'] == 1) {
  1288. $l['chargestage'] = isset($indexArr[intval($l['chargestage'])]) ? $indexArr[intval($l['chargestage'])] : $l['chargestage'];
  1289. }
  1290. $temp[$k][] = $chargeStageMap[$l['chargestage']];
  1291. $temp[$k][] = $l['battvoltage'];
  1292. $temp[$k][] = $l['voltagedaymin'];
  1293. $temp[$k][] = $l['voltagedaymax'];
  1294. $temp[$k][] = $l['batttemper'];
  1295. $temp[$k][] = $l['daybattmintemper'];
  1296. $temp[$k][] = $l['daybattmaxtemper'];
  1297. $temp[$k][] = date_change($l["updatetime"],8,$timezone['value']);
  1298. $i ++;
  1299. }
  1300. $title1 = [];
  1301. foreach ($title as $t) {
  1302. $title1[0][] = $t;
  1303. }
  1304. $data = array_merge($title1,$temp);
  1305. $dataList[] = array('name'=>$name,'dataList'=>$data);
  1306. // 太阳能
  1307. $lampInfoList = $this->Lamp_model->getListForInfoLog($lamp_id, 'solar_info_log', null, null,$beginDate,$endDate);
  1308. $temp = array();
  1309. $i = 1;
  1310. if (empty($this->version)) {
  1311. $name = '太阳能';
  1312. $title = array('序号','电压(V)','电流(A)','功率(W)','当天充电最大功率(WH)','当天充电时长(HH:MM)','更新时间');
  1313. }else{
  1314. $name = 'Solar';
  1315. $title = array('Number','Voltage(V)','Current(A)','Power(W)','Maximum Power Charged on the Day(W)','day Charging time(HH:MM)','Update time');
  1316. }
  1317. foreach ($lampInfoList as $k=>$l) {
  1318. $temp[$k][] = $i;
  1319. $temp[$k][] = $l['solarvoltage'];
  1320. $temp[$k][] = $l['solarcurrent'];
  1321. $temp[$k][] = $l['solarpower'];
  1322. $temp[$k][] = $l['daychargemaxpow'];
  1323. $h = floor($l['daydischargemincurrent'] / 3600);
  1324. $h = $h < 10 ? '0'.$h : $h;
  1325. $m = ceil($l['daydischargemincurrent'] % 3600 / 60);
  1326. $m = $m < 10 ? '0'.$m : $m;
  1327. $temp[$k][] = $h.':'.$m;
  1328. $temp[$k][] = date_change($l["updatetime"],8,$timezone['value']);
  1329. $i ++;
  1330. }
  1331. $title1 = [];
  1332. foreach ($title as $t) {
  1333. $title1[0][] = $t;
  1334. }
  1335. $data = array_merge($title1,$temp);
  1336. $dataList[] = array('name'=>$name,'dataList'=>$data);
  1337. // 系统
  1338. $lampInfoList = $this->Lamp_model->getListForInfoLog($lamp_id, 'system_info_log', null, null,$beginDate,$endDate);
  1339. $temp = array();
  1340. $i = 1;
  1341. if (empty($this->version)) {
  1342. $name = '系统';
  1343. $title = array('序号','系统电压(V)','系统电流(A)','控制器温度(℃)','更新时间');
  1344. }else{
  1345. $name = 'System';
  1346. $title = array('Serial number','System voltage(V)','System current(A)','Control temperature(℃)','Update time');
  1347. }
  1348. foreach ($lampInfoList as $k=>$l) {
  1349. $temp[$k][] = $i;
  1350. $temp[$k][] = $l['sysvoltage'];
  1351. $temp[$k][] = $l['syscurrent'];
  1352. $temp[$k][] = $l['temper'];
  1353. $temp[$k][] = date_change($l["updatetime"],8,$timezone['value']);
  1354. $i ++;
  1355. }
  1356. $title1 = [];
  1357. foreach ($title as $t) {
  1358. $title1[0][] = $t;
  1359. }
  1360. $data = array_merge($title1,$temp);
  1361. $dataList[] = array('name'=>$name,'dataList'=>$data);
  1362. $path = push_excel($dataList,'historyInfoExcel_'.date('Ymd'),1);
  1363. exit(json_result('0000', $this->response['0000'], array('path'=>$path)));
  1364. }
  1365. // $table_map = array('system_info_log','lamp_info_log','solar_info_log','battery_info_log','electric_info_log','history_info_log');
  1366. $table = !empty($datatype) ? $datatype : 'system_info_log';
  1367. if ($table == 'lamp_info_log') {
  1368. $table = 'realtime_info_log';
  1369. $total = $this->RealtimeInfoLog_model->get_count(['lampid'=>$lamp_id,'updatetime >='=>$beginDate,'updatetime <='=>$endDate]);
  1370. $res = $this->RealtimeInfoLog_model->get_list(['lampid'=>$lamp_id,'updatetime >='=>$beginDate,'updatetime <='=>$endDate], 'loadvoltage as lampvoltage,lighteness,loadcurrent as lampcurrent,loadpower as lamppower,updatetime,loadWorkTimeDay as daychargemincurrent',$count, ($page-1)*$count, 'updatetime desc,id desc', NUll);
  1371. // $res = $this->Lamp_model->getListForInfoLog($lamp_id, $table, ($page-1)*$count, $count,$beginDate,$endDate);
  1372. }elseif ($table == 'system_info_log') {
  1373. $total = $this->Lamp_model->getCountForInfoLog($lamp_id, $table,$beginDate,$endDate);
  1374. $res = $this->Lamp_model->getListForInfoLog($lamp_id, $table, ($page-1)*$count, $count,$beginDate,$endDate);
  1375. }elseif ($table == 'battery_info_log') {
  1376. $join = [
  1377. // ['table'=>'history_info_log as HI','cond'=>'HI.lampid = RI.lampid and HI.updatetime = RI.updatetime','type'=>'inner'],
  1378. ];
  1379. $where = ['lampid'=>$lamp_id,'updatetime >='=>$beginDate,'updatetime <='=>$endDate];
  1380. $total = $this->RealtimeInfoLog_model->get_count($where,'count(*) as total');
  1381. $fields = 'batvoltage as battvoltage,updatetime,DayOverDisChgTimes as overtimes';
  1382. $res = $this->RealtimeInfoLog_model->get_list($where, $fields,$count, ($page-1)*$count, 'updatetime desc,id desc');
  1383. }else{
  1384. $join = [
  1385. // ['table'=>'history_info_log as HI','cond'=>'HI.lampid = RI.lampid and HI.updatetime = RI.updatetime','type'=>'inner'],
  1386. ];
  1387. $where = ['lampid'=>$lamp_id,'updatetime >='=>$beginDate,'updatetime <='=>$endDate];
  1388. $total = $this->RealtimeInfoLog_model->get_count($where,'count(*) as total');
  1389. $fields = 'panelvoltage as solarvoltage,updatetime,panelcurrent as solarcurrent,panelpower as solarpower';
  1390. $res = $this->RealtimeInfoLog_model->get_list($where, $fields,$count, ($page-1)*$count, 'updatetime desc,id desc');
  1391. // $total = $this->Lamp_model->getCountForInfoLog($lamp_id, $table,$beginDate,$endDate);
  1392. // $res = $this->Lamp_model->getListForInfoLog($lamp_id, $table, ($page-1)*$count, $count,$beginDate,$endDate);
  1393. }
  1394. if(!$res){
  1395. exit(json_result('0000', $this->response['0006'], array()));
  1396. }else{
  1397. $arr = [2=>'16',3=>'32',4=>'48',5=>'64'];
  1398. foreach ($res as &$v) {
  1399. if (isset($v['lighteness'])) {
  1400. $v['lampstatus'] = empty($v['lighteness']) ? '0' : '1';
  1401. }
  1402. if (isset($v['daychargemincurrent'])) $v['daychargemincurrent'] = intval($v['daychargemincurrent']);
  1403. // $v['updatetime'] = date_change($v["updatetime"],8,$timezone['value']);
  1404. $v['updatetime'] = set_timezone($v["updatetime"],$timezone['value']);
  1405. if ($lampData['protocoltype'] == 1 && $table == 'battery_info_log') {
  1406. $v['chargestage'] = isset($arr[intval($v['chargestage'])]) ? $arr[intval($v['chargestage'])] : $v['chargestage'];
  1407. }
  1408. if ($table == 'system_info_log') {
  1409. $v['lampid'] = $v['number'];
  1410. }
  1411. }
  1412. }
  1413. $data = array(
  1414. 'total' => ceil($total/$count),
  1415. 'list' => $res
  1416. );
  1417. exit(json_result('0000', $this->response['0000'], $data));
  1418. }
  1419. // 修改路灯位置
  1420. public function change_location(){
  1421. $id = intval($this->input->post('id',true));
  1422. $type = intval($this->input->post('type',true));
  1423. $data['longitude'] = $this->input->post('longitude',true);
  1424. $data['latitude'] = $this->input->post('latitude',true);
  1425. $data['updatetime'] = date('Y-m-d H:i:s',time());
  1426. if (empty($id) || empty($type) || !isset($data['longitude']) || !isset($data['latitude'])) {
  1427. exit(json_result('0007',$this->response['0007'],array()));
  1428. }
  1429. if ($type == 1) {
  1430. $this->Lamp_model->update($data,array('id'=>$id));
  1431. }else{
  1432. $this->Videomonitor_model->update($data,array('id'=>$id));
  1433. }
  1434. exit(json_result('0000',$this->response['0000'],array()));
  1435. }
  1436. // 同步巡检
  1437. public function set_patrol_cmd(){
  1438. $id = intval($this->input->post('id',true));
  1439. if (empty($id)) exit(json_result('0007',$this->response['0007'],array('id'=>$id)));
  1440. $lampData = $this->Lamp_model->getOne($id,'N.status,N.protocoltype,L.address,L.protocoltype as lampprotocoltype,L.number');
  1441. $cmd = '{"cmd_type":"set_sync_patrol_cmd","cmd_id":'.$id.'}';
  1442. $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
  1443. if ($cmdret === false) {
  1444. sleep(1);
  1445. $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
  1446. }
  1447. exit(json_result('0000',$this->response['0000'],array('id'=>$id)));
  1448. }
  1449. // 批量添加
  1450. public function batchInput(){
  1451. set_time_limit(0);
  1452. $userid = $this->get_user_info('id');
  1453. // $idArr = $this->get_section_by_user();
  1454. $role = $this->get_user_info('role');
  1455. $companyid = $this->get_user_info('companyid');
  1456. $type = intval($this->input->post('type',true));
  1457. if ($type == 1) { // 验证
  1458. $indexArr = ['A'=>'number','B'=>'address','C'=>'company_name','D'=>'projectid','E'=>'devicetype','F'=>'networkid','G'=>'protocoltype','H'=>'lampprotocoltype','I'=>'section','J'=>'longitude','K'=>'latitude','L'=>'poleheight','M'=>'polediameter','N'=>'polematerial','O'=>'lighttype','P'=>'lamptype','Q'=>'boardtype','R'=>'batterytype','S'=>'boardpower','T'=>'batteryah','U'=>'loadpower','V'=>'loadtype'];
  1459. $data = $this->batch_import($indexArr);
  1460. }else{ // 创建
  1461. $data = $this->input->post('lampData',true);
  1462. if (empty($data)) exit(json_result('0007',$this->response['0007']));
  1463. $data = json_decode($data,true);
  1464. }
  1465. foreach ($data as &$v) {
  1466. if ($type == 1 && is_float($v['address'])) $v['address'] = strval(intval(ceil($v['address'])));
  1467. }
  1468. $res = 0;
  1469. $typeArr = ['tcp'=>0,'nb'=>1,'gprs'=>2,'lorawan'=>3,'MQTT'=>4,'WeLoraWan'=>6,'onenet'=>7];
  1470. $this->db->trans_begin();
  1471. $msg = [];
  1472. $patrol_arr = array();
  1473. foreach ($data as $key => $value) {
  1474. $num = $key+2;
  1475. $value['number'] = trim($value['number']);
  1476. if (!isset($value['number']) || $value['number'] == '') {
  1477. $res = 1;
  1478. if (empty($this->version)) {
  1479. $msg[] = '表格第'.$num.'行,路灯编号不能为空';
  1480. }else{
  1481. $msg[] = 'Table '.$num.' row.Lamp number can not be empty';
  1482. }
  1483. }else{
  1484. if (!is_numeric($value['number']) && ($value['number'] <= 0 || $value['number'] > 999999)) {
  1485. $res = 1;
  1486. if (empty($this->version)) {
  1487. $msg[] = '表格第'.$num.'行,路灯编号为1-999999的数字';
  1488. }else{
  1489. $msg[] = 'Table '.$num.' row.The number of street lamps numbered 1-999999';
  1490. }
  1491. }
  1492. }
  1493. // if ($type == 1 && is_float($value['address'])) {
  1494. // $value['address'] = strval(intval(ceil($value['address'])));
  1495. // }
  1496. $value['address'] = trim($value['address']);
  1497. if (empty($value['address'])) {
  1498. $res = 1;
  1499. if (empty($this->version)) {
  1500. $msg[] = '表格第'.$num.'行,无线模块地址不能为空';
  1501. }else{
  1502. $msg[] = 'Table '.$num.' row.Address of the wireless module can not be empty';
  1503. }
  1504. }else{
  1505. $status = preg_match('/^[A-Fa-f0-9]+$/', $value['address']);
  1506. if (empty($status)) {
  1507. $res = 1;
  1508. if (empty($this->version)) {
  1509. $msg[] = '表格第'.$num.'行,无线模块地址为16进制数';
  1510. }else{
  1511. $msg[] = 'Table '.$num.' row.Wireless module address is 16 hexadecimal';
  1512. }
  1513. }else{
  1514. if ($this->Lamp_model->getDataCount(['address'=>$value['address']])) {
  1515. $res = 1;
  1516. if (empty($this->version)) {
  1517. $msg[] = '表格第'.$num.'行,无线模块地址已经存在';
  1518. }else{
  1519. $msg[] = 'Table '.$num.' row.Wireless module address already exists';
  1520. }
  1521. }
  1522. }
  1523. }
  1524. $value['company_name'] = trim($value['company_name']);
  1525. if (empty($value['company_name'])) {
  1526. $res = 1;
  1527. if (empty($this->version)) {
  1528. $msg[] = '表格第'.$num.'行,公司名称不能为空';
  1529. }else{
  1530. $msg[] = 'Table '.$num.' row.Company name cannot be empty';
  1531. }
  1532. }
  1533. $value['projectid'] = trim($value['projectid']);
  1534. if (empty($value['projectid'])) {
  1535. $res = 1;
  1536. if (empty($this->version)) {
  1537. $msg[] = '表格第'.$num.'行,项目名称不能为空';
  1538. }else{
  1539. $msg[] = 'Table '.$num.' row.Project name cannot be empty';
  1540. }
  1541. }else{
  1542. $projectid = $this->Project_model->project_exist_by_name($role,$userid,$companyid,$value['projectid'],$value['company_name']);
  1543. if (empty($projectid)) {
  1544. $res = 1;
  1545. if (empty($this->version)) {
  1546. $msg[] = '表格第'.$num.'行,项目不存在,请先创建项目';
  1547. }else{
  1548. $msg[] = 'Table '.$num.' row.The project does not exist. Please create a project first.';
  1549. }
  1550. $value['projectid'] = 0;
  1551. }else{
  1552. $value['projectid'] = $projectid;
  1553. $this->Project_model->add_lamp_count(array('faultcount'=>0,'projectid'=>$projectid));
  1554. if ($this->Lamp_model->getDataCount(['projectid'=>$value['projectid'],'number'=>$value['number']])) {
  1555. $res = 1;
  1556. if (empty($this->version)) {
  1557. $msg[] = '表格第'.$num.'行,路灯编号已经存在';
  1558. }else{
  1559. $msg[] = 'Table '.$num.' row lamp number already exists';
  1560. }
  1561. }
  1562. }
  1563. }
  1564. if($type != 1 && isset($typeArr[$value['protocoltype']]) && $typeArr[$value['protocoltype']] == 6){
  1565. $this->add_device($value['address']);
  1566. }
  1567. $value['devicetype'] = intval(trim($value['devicetype']));
  1568. if ($value['devicetype'] == 0) { // 非直连设备
  1569. unset($value['protocoltype']);
  1570. $value['networkid'] = trim($value['networkid']);
  1571. if (empty($value['networkid'])) {
  1572. $res = 1;
  1573. if (empty($this->version)) {
  1574. $msg[] = '表格第'.$num.'行,非直连设备网络编号不能为空';
  1575. }else{
  1576. $msg[] = 'Table '.$num.' row.Indirect device network number cannot be empty';
  1577. }
  1578. }else{
  1579. $networkid = $this->Network_model->project_exist_by_name($value['projectid'],$value['networkid']);
  1580. // var_dump($this->db->last_query());die;
  1581. if (empty($networkid)) {
  1582. $res = 1;
  1583. if (empty($this->version)) {
  1584. $msg[] = '表格第'.$num.'行,网络不存在,请先添加网络';
  1585. }else{
  1586. $msg[] = 'Table '.$num.' row.The network does not exist. Please add the network first';
  1587. }
  1588. }else{
  1589. $this->Network_model->add_lamp_count(array('faultcount'=>0,'networkid'=>$networkid));
  1590. $value['networkid'] = $networkid;
  1591. }
  1592. }
  1593. }elseif ($value['devicetype'] == 1) { // 直连设备
  1594. $value['protocoltype'] = trim($value['protocoltype']);
  1595. if (empty($value['protocoltype'])) {
  1596. $res = 1;
  1597. if (empty($this->version)) {
  1598. $msg[] = '表格第'.$num.'行,协议类型不能为空';
  1599. }else{
  1600. $msg[] = 'Table '.$num.' row.Protocol type cannot be empty';
  1601. }
  1602. }else{
  1603. if (!isset($typeArr[$value['protocoltype']])) {
  1604. $res = 1;
  1605. if (empty($this->version)) {
  1606. $msg[] = '表格第'.$num.'行,协议类型不存在';
  1607. }else{
  1608. $msg[] = 'Table '.$num.' row.Protocol type not exists';
  1609. }
  1610. }
  1611. }
  1612. // 创建直连网络
  1613. $number = mb_strlen($value['address']) == 13 ? base_convert($value['address'], 16, 10) : $value['address'];
  1614. if ($this->Network_model->getDataCount(array('devicesn'=>$number))) {
  1615. $res = 1;
  1616. if (empty($this->version)) {
  1617. $msg[] = '表格第'.$num.'行,无线模块地址已被使用';
  1618. }else{
  1619. $msg[] = 'Table '.$num.' row.Wireless module address has been used';
  1620. }
  1621. }
  1622. // 创建网络
  1623. // $networkData['id'] = $this->Zone_model->insert(array(
  1624. // 'name' => $number,
  1625. // 'parent' => $value['projectid'] ,
  1626. // 'level' => 4
  1627. // ));
  1628. // 控制器协议类型
  1629. $value['lampprotocoltype'] = trim($value['lampprotocoltype']);
  1630. if (empty($value['lampprotocoltype'])) {
  1631. $networkData['devicetype'] = 0;
  1632. }else{
  1633. if ($value['lampprotocoltype'] == 'ModBus') {
  1634. $networkData['devicetype'] = 3;
  1635. }elseif ($value['lampprotocoltype'] == 'LightBus') {
  1636. $networkData['devicetype'] = 0;
  1637. }else{
  1638. $networkData['devicetype'] = 0;
  1639. }
  1640. }
  1641. $networkData['networkid'] = $number;
  1642. $networkData['gatewaytype'] = 'direct';
  1643. $networkData['protocoltype'] = empty($typeArr[$value['protocoltype']]) ? 'tcp' : $typeArr[$value['protocoltype']];
  1644. $networkData['projectid'] = $value['projectid'];
  1645. $networkData['devicesn'] = $number;
  1646. $networkData['networkname'] = $number;
  1647. $networkData['type'] = 1;
  1648. $networkData['lampcount'] = 1;
  1649. $networkData['createtime'] = date('Y-m-d H:i:s',time());
  1650. if ($type == 1) {
  1651. $this->db->insert('network',$networkData);
  1652. $value['networkid'] = $this->db->insert_id();
  1653. }else{
  1654. $value['networkid'] = $this->Network_model->insert($networkData);
  1655. }
  1656. }else{
  1657. $res = 1;
  1658. if (empty($this->version)) {
  1659. $msg[] = '表格第'.$num.'行,设备类型选择错误';
  1660. }else{
  1661. $msg[] = 'Table '.$num.' row.Device type selection error';
  1662. }
  1663. }
  1664. unset($value['protocoltype']);
  1665. // 控制器协议类型
  1666. $value['lampprotocoltype'] = trim($value['lampprotocoltype']);
  1667. if (empty($value['lampprotocoltype'])) {
  1668. $res = 1;
  1669. if (empty($this->version)) {
  1670. $msg[] = '表格第'.$num.'行,协议类型不能为空';
  1671. }else{
  1672. $msg[] = 'Table '.$num.' row.Protocol type can not be empty';
  1673. }
  1674. }else{
  1675. if ($value['lampprotocoltype'] == 'ModBus') {
  1676. $value['protocoltype'] = 1;
  1677. }elseif ($value['lampprotocoltype'] == 'LightBus') {
  1678. $value['protocoltype'] = 0;
  1679. }else{
  1680. $res = 1;
  1681. if (empty($this->version)) {
  1682. $msg[] = '表格第'.$num.'行,协议类型选择错误';
  1683. }else{
  1684. $msg[] = 'Table '.$num.' row.Protocol type selection error';
  1685. }
  1686. }
  1687. }
  1688. unset($value['lampprotocoltype']);
  1689. $value['longitude'] = trim($value['longitude']);
  1690. if (empty($value['longitude'])) {
  1691. $value['longitude'] = 0;
  1692. }else{
  1693. if (!is_numeric($value['longitude']) || $value['longitude'] > 180 || $value['longitude'] < -180) {
  1694. $res = 1;
  1695. if (empty($this->version)) {
  1696. $msg[] = '表格第'.$num.'行,经度输入错误';
  1697. }else{
  1698. $msg[] = 'Table '.$num.' row longitude input error';
  1699. }
  1700. }
  1701. }
  1702. $value['latitude'] = trim($value['latitude']);
  1703. if (empty($value['latitude'])) {
  1704. $value['latitude'] = 0;
  1705. }else{
  1706. if (!is_numeric($value['latitude']) || $value['latitude'] > 90 || $value['latitude'] < -90) {
  1707. $res = 1;
  1708. if (empty($this->version)) {
  1709. $msg[] = '表格第'.$num.'行,纬度输入错误';
  1710. }else{
  1711. $msg[] = 'Table '.$num.' row latitude input error';
  1712. }
  1713. }
  1714. }
  1715. if (empty($value['latitude']) && empty($value['longitude']) && !empty($value['projectid'])) {
  1716. $projectData = $this->Project_model->getOne($value['projectid'],'cityid');
  1717. if (!empty($projectData['cityid'])){
  1718. $globalData = $this->Global_location_model->getOne($projectData['cityid'],'longitude,latitude');
  1719. $value['longitude'] = $globalData['longitude'];
  1720. $value['latitude'] = $globalData['latitude'];
  1721. }
  1722. }
  1723. $value['loadtype'] = trim($value['loadtype']);
  1724. if (empty($value['loadtype']) || $value['loadtype'] == '0' || $value['loadtype'] == '1') {
  1725. if ($value['loadtype'] == '1') {
  1726. $value['loadtype'] = 1;
  1727. }else{
  1728. $value['loadtype'] = 0;
  1729. }
  1730. }else{
  1731. $res = 1;
  1732. if (empty($this->version)) {
  1733. $msg[] = '表格第'.$num.'行,负载类型错误';
  1734. }else{
  1735. $msg[] = 'Table '.$num.' row load type error';
  1736. }
  1737. }
  1738. if (trim($value['poleheight']) == '') {
  1739. unset($value['poleheight']);
  1740. }else{
  1741. $value['poleheight'] = trim($value['poleheight']);
  1742. }
  1743. if (trim($value['polediameter']) == '') {
  1744. unset($value['polediameter']);
  1745. }else{
  1746. $value['polediameter'] = trim($value['polediameter']);
  1747. }
  1748. if (trim($value['section']) == '') {
  1749. $value['section'] = '';
  1750. $res = 1;
  1751. if (empty($this->version)) {
  1752. $msg[] = '表格第'.$num.'行,路段不能为空';
  1753. }else{
  1754. $msg[] = 'Table '.$num.' row. Section cannot be empty';
  1755. }
  1756. }else{
  1757. $value['section'] = trim($value['section']);
  1758. }
  1759. if (trim($value['polematerial']) == '') {
  1760. unset($value['polematerial']);
  1761. }else{
  1762. $value['polematerial'] = trim($value['polematerial']);
  1763. }
  1764. if (trim($value['lighttype']) == '') {
  1765. unset($value['lighttype']);
  1766. }else{
  1767. $value['lighttype'] = trim($value['lighttype']);
  1768. }
  1769. if (trim($value['lamptype']) == '') {
  1770. unset($value['lamptype']);
  1771. }else{
  1772. $value['lamptype'] = trim($value['lamptype']);
  1773. }
  1774. if (trim($value['boardtype']) == '') {
  1775. unset($value['boardtype']);
  1776. }else{
  1777. $value['boardtype'] = trim($value['boardtype']);
  1778. }
  1779. if (trim($value['batterytype']) == '') {
  1780. unset($value['batterytype']);
  1781. }else{
  1782. $value['batterytype'] = trim($value['batterytype']);
  1783. }
  1784. if (trim($value['boardpower']) == '') {
  1785. unset($value['boardpower']);
  1786. }else{
  1787. $value['boardpower'] = trim($value['boardpower']);
  1788. }
  1789. if (trim($value['batteryah']) == '') {
  1790. unset($value['batteryah']);
  1791. }else{
  1792. $value['batteryah'] = trim($value['batteryah']);
  1793. }
  1794. if (trim($value['loadpower']) == '') {
  1795. unset($value['loadpower']);
  1796. }else{
  1797. $value['loadpower'] = trim($value['loadpower']);
  1798. }
  1799. $value['createtime'] = date('Y-m-d H:i:s',time());
  1800. $value['updatetime'] = date('Y-m-d H:i:s',time());
  1801. unset($value['company_name']);
  1802. if (is_null($value['networkid'])) {
  1803. unset($value['networkid']);
  1804. }
  1805. $lamp_id = $this->Lamp_model->insert($value);
  1806. $patrol_arr[] = array('lampid'=>$lamp_id,'projectid'=>$value['projectid']);
  1807. }
  1808. if ($res || $type == 1) {
  1809. $this->db->trans_rollback();
  1810. exit(json_result('0000',$this->response['0000'],['lampData'=>$data,'msg'=>$msg]));
  1811. }else{
  1812. $this->db->trans_commit();
  1813. // foreach ($patrol_arr as $patrol) {
  1814. // $projectData = $this->Project_model->getOne($patrol['projectid'],'patrolinterval');
  1815. // $this->patrol_set($patrol['lampid'],$projectData['patrolinterval']);
  1816. // }
  1817. exit(json_result('0000',$this->response['0000']));
  1818. }
  1819. }
  1820. // 路灯历史数据曲线图
  1821. public function lamp_history_data(){
  1822. $type = intval($this->input->post('type',true));
  1823. $lampId = intval($this->input->post('lampid',true));
  1824. if (empty($lampId)) exit(json_result('0007',$this->response['0007']));
  1825. $dateType = intval($this->input->post('dateType',true));
  1826. $date = $this->input->post('date',true);
  1827. if ($dateType == 0) { // 一天
  1828. $beginDate = date('Y-m-d H:i:s',time()-24*3600);
  1829. $endDate = date('Y-m-d H:i:s',time());
  1830. }elseif ($dateType == 1) { // 三天
  1831. $beginDate = date('Y-m-d H:i:s',time()-24*3600*3);
  1832. $endDate = date('Y-m-d H:i:s',time());
  1833. }elseif ($dateType == 2) { // 七天
  1834. $beginDate = date('Y-m-d H:i:s',time()-24*3600*7);
  1835. $endDate = date('Y-m-d H:i:s',time());
  1836. }elseif ($dateType == 3) { // 十四天
  1837. $beginDate = date('Y-m-d H:i:s',time()-24*3600*14);
  1838. $endDate = date('Y-m-d H:i:s',time());
  1839. }else{ // 选择时间
  1840. if (empty($date)) exit(json_result('0007',$this->response['0007']));
  1841. $res = explode('/', $date);
  1842. if (strtotime($res[1]) - strtotime($res[0]) > 31 * 24 * 3600) {
  1843. exit(json_result('0418',$this->response['0418']));
  1844. }
  1845. $beginDate = $res[0];
  1846. $endDate = $res[1];
  1847. }
  1848. $sql = "SELECT P.id,P.patrolinterval FROM lampinfo AS L LEFT JOIN project as P on P.id = L.projectid WHERE L.id = {$lampId} limit 1";
  1849. $lampData = $this->db->query($sql)->row_array();
  1850. $timezoneData = $this->Project_model->get_timezone_by_projectid($lampData['id']);
  1851. $timezone = $timezoneData['value'];
  1852. $data = ['patrolinterval'=>$lampData['patrolinterval']];
  1853. if ($type == 0) { // 路灯
  1854. $sql = 'select LI.loadvoltage as lampvoltage,LI.loadcurrent as lampcurrent,LI.loadpower as lamppower,LI.updatetime from realtime_info_log as LI where LI.lampid = '.$lampId.' and LI.updatetime >= "'.$beginDate.'" and LI.updatetime <= "'.$endDate.'"';
  1855. $list = $this->db->query($sql)->result_array();
  1856. $temp = [];
  1857. foreach ($list as $value) {
  1858. $temp['lampvoltage'][] = $value['lampvoltage'];
  1859. $temp['lampcurrent'][] = $value['lampcurrent'];
  1860. $temp['lamppower'][] = $value['lamppower'];
  1861. // $temp['lighteness'][] = $value['lighteness'];
  1862. $temp['updatetime'][] = date_change($value['updatetime'],8,$timezone);
  1863. if (!isset($data['maxcurrent'])) {
  1864. $data['maxcurrent'] = $value['lampcurrent'];
  1865. }else{
  1866. $data['maxcurrent'] = $value['lampcurrent'] > $data['maxcurrent'] ? $value['lampcurrent'] : $data['maxcurrent'];
  1867. }
  1868. // if (!isset($data['maxdaychargemincurrent'])) {
  1869. // $data['maxdaychargemincurrent'] = $value['daychargemincurrent'];
  1870. // }else{
  1871. // $data['maxdaychargemincurrent'] = $value['daychargemincurrent'] > $data['maxdaychargemincurrent'] ? $value['daychargemincurrent'] : $data['maxdaychargemincurrent'];
  1872. // }
  1873. if (!isset($data['mincurrent'])) {
  1874. $data['mincurrent'] = $value['lampcurrent'];
  1875. }else{
  1876. $data['mincurrent'] = $value['lampcurrent'] < $data['mincurrent'] ? $value['lampcurrent'] : $data['mincurrent'];
  1877. }
  1878. if (!isset($data['maxvoltage'])) {
  1879. $data['maxvoltage'] = $value['lampvoltage'];
  1880. }else{
  1881. $data['maxvoltage'] = $value['lampvoltage'] > $data['maxvoltage'] ? $value['lampvoltage'] : $data['maxvoltage'];
  1882. }
  1883. if (!isset($data['minvoltage'])) {
  1884. $data['minvoltage'] = $value['lampvoltage'];
  1885. }else{
  1886. $data['minvoltage'] = $value['lampvoltage'] < $data['minvoltage'] ? $value['lampvoltage'] : $data['minvoltage'];
  1887. }
  1888. if (!isset($data['maxpower'])) {
  1889. $data['maxpower'] = $value['lamppower'];
  1890. }else{
  1891. $data['maxpower'] = $value['lamppower'] > $data['maxpower'] ? $value['lamppower'] : $data['maxpower'];
  1892. }
  1893. if (!isset($data['minpower'])) {
  1894. $data['minpower'] = $value['lamppower'];
  1895. }else{
  1896. $data['minpower'] = $value['lamppower'] < $data['minpower'] ? $value['lamppower'] : $data['minpower'];
  1897. }
  1898. // if (!isset($data['maxlighteness'])) {
  1899. // $data['maxlighteness'] = $value['lighteness'];
  1900. // }else{
  1901. // $data['maxlighteness'] = $value['lighteness'] > $data['maxlighteness'] ? $value['lighteness'] : $data['maxlighteness'];
  1902. // }
  1903. // if (!isset($data['minlighteness'])) {
  1904. // $data['minlighteness'] = $value['lighteness'];
  1905. // }else{
  1906. // $data['minlighteness'] = $value['lighteness'] < $data['minlighteness'] ? $value['lighteness'] : $data['minlighteness'];
  1907. // }
  1908. // if (isset($data['maxdaychargemincurrent'])) {
  1909. // $h = floor($data['maxdaychargemincurrent'] / 3600);
  1910. // $h = $h < 10 ? '0'.$h : $h;
  1911. // $m = floor($data['maxdaychargemincurrent'] / 3600 % 60);
  1912. // $m = $m < 10 ? '0'.$m : $m;
  1913. // $data['maxdaychargemincurrent'] = $h.':'.$m;
  1914. // }
  1915. }
  1916. $list = $temp;
  1917. }elseif ($type == 1) { // 蓄电池
  1918. $sql = 'select batvoltage as battvoltage,batcurrent as chargecurrent,chgtemper as batttemper,batpower as chargepower,updatetime from realtime_info_log where lampid = '.$lampId.' and updatetime >= "'.$beginDate.'" and updatetime <= "'.$endDate.'"';
  1919. $list = $this->db->query($sql)->result_array();
  1920. $temp = [];
  1921. foreach ($list as $value) {
  1922. $temp['battvoltage'][] = $value['battvoltage'];
  1923. $temp['updatetime'][] = date_change($value['updatetime'],8,$timezone);
  1924. $batterycurrent = $value['chargecurrent'];
  1925. $temp['batterycurrent'][] = $batterycurrent;
  1926. $batterypower = $value['chargepower'];
  1927. $temp['batterypower'][] = $batterypower;
  1928. if (!isset($data['maxcurrent'])) {
  1929. $data['maxcurrent'] = $batterycurrent;
  1930. }else{
  1931. $data['maxcurrent'] = $batterycurrent > $data['maxcurrent'] ? $batterycurrent : $data['maxcurrent'];
  1932. }
  1933. if (!isset($data['mincurrent'])) {
  1934. $data['mincurrent'] = $batterycurrent;
  1935. }else{
  1936. $data['mincurrent'] = $batterycurrent < $data['mincurrent'] ? $batterycurrent : $data['mincurrent'];
  1937. }
  1938. if (!isset($data['maxvoltage'])) {
  1939. $data['maxvoltage'] = $value['battvoltage'];
  1940. }else{
  1941. $data['maxvoltage'] = $value['battvoltage'] > $data['maxvoltage'] ? $value['battvoltage'] : $data['maxvoltage'];
  1942. }
  1943. if (!isset($data['minvoltage'])) {
  1944. $data['minvoltage'] = $value['battvoltage'];
  1945. }else{
  1946. $data['minvoltage'] = $value['battvoltage'] < $data['minvoltage'] ? $value['battvoltage'] : $data['minvoltage'];
  1947. }
  1948. if (!isset($data['maxbatttemper'])) {
  1949. $data['maxbatttemper'] = $value['batttemper'];
  1950. }else{
  1951. $data['maxbatttemper'] = $value['batttemper'] > $data['maxbatttemper'] ? $value['batttemper'] : $data['maxbatttemper'];
  1952. }
  1953. if (!isset($data['minbatttemper'])) {
  1954. $data['minbatttemper'] = $value['batttemper'];
  1955. }else{
  1956. $data['minbatttemper'] = $value['batttemper'] < $data['minbatttemper'] ? $value['batttemper'] : $data['minbatttemper'];
  1957. }
  1958. if (!isset($data['maxpower'])) {
  1959. $data['maxpower'] = round($batterypower,2);
  1960. }else{
  1961. $data['maxpower'] = $batterypower > $data['maxpower'] ? round($batterypower,2) : round($data['maxpower'],2);
  1962. }
  1963. if (!isset($data['minpower'])) {
  1964. $data['minpower'] = round($batterypower,2);
  1965. }else{
  1966. $data['minpower'] = $batterypower < $data['minpower'] ? round($batterypower,2) : round($data['minpower'],2);
  1967. }
  1968. }
  1969. $list = $temp;
  1970. }elseif ($type == 2) { // 太阳能板
  1971. $sql = 'select SI.panelvoltage as solarvoltage,SI.panelcurrent as solarcurrent,SI.panelpower as solarpower,SI.updatetime from realtime_info_log as SI where SI.lampid = '.$lampId.' and SI.updatetime >= "'.$beginDate.'" and SI.updatetime <= "'.$endDate.'"';
  1972. $list = $this->db->query($sql)->result_array();
  1973. $temp = [];
  1974. foreach ($list as $value) {
  1975. $temp['solarvoltage'][] = $value['solarvoltage'];
  1976. $temp['solarcurrent'][] = $value['solarcurrent'];
  1977. $temp['solarpower'][] = $value['solarpower'];
  1978. $temp['updatetime'][] = date_change($value['updatetime'],8,$timezone);
  1979. if (!isset($data['maxcurrent'])) {
  1980. $data['maxcurrent'] = $value['solarcurrent'];
  1981. }else{
  1982. $data['maxcurrent'] = $value['solarcurrent'] > $data['maxcurrent'] ? $value['solarcurrent'] : $data['maxcurrent'];
  1983. }
  1984. if (!isset($data['mincurrent'])) {
  1985. $data['mincurrent'] = $value['solarcurrent'];
  1986. }else{
  1987. $data['mincurrent'] = $value['solarcurrent'] < $data['mincurrent'] ? $value['solarcurrent'] : $data['mincurrent'];
  1988. }
  1989. if (!isset($data['maxvoltage'])) {
  1990. $data['maxvoltage'] = $value['solarvoltage'];
  1991. }else{
  1992. $data['maxvoltage'] = $value['solarvoltage'] > $data['maxvoltage'] ? $value['solarvoltage'] : $data['maxvoltage'];
  1993. }
  1994. // if (!isset($data['maxdaydischargemincurrent'])) {
  1995. // $data['maxdaydischargemincurrent'] = $value['daydischargemincurrent'];
  1996. // }else{
  1997. // $data['maxdaydischargemincurrent'] = $value['daydischargemincurrent'] > $data['maxdaydischargemincurrent'] ? $value['daydischargemincurrent'] : $data['maxdaydischargemincurrent'];
  1998. // }
  1999. if (!isset($data['minvoltage'])) {
  2000. $data['minvoltage'] = $value['solarvoltage'];
  2001. }else{
  2002. $data['minvoltage'] = $value['solarvoltage'] < $data['minvoltage'] ? $value['solarvoltage'] : $data['minvoltage'];
  2003. }
  2004. if (!isset($data['maxpower'])) {
  2005. $data['maxpower'] = $value['solarpower'];
  2006. }else{
  2007. $data['maxpower'] = $value['solarpower'] > $data['maxpower'] ? $value['solarpower'] : $data['maxpower'];
  2008. }
  2009. if (!isset($data['minpower'])) {
  2010. $data['minpower'] = $value['solarpower'];
  2011. }else{
  2012. $data['minpower'] = $value['solarpower'] < $data['minpower'] ? $value['solarpower'] : $data['minpower'];
  2013. }
  2014. if (isset($data['maxdaydischargemincurrent'])) {
  2015. $h = floor($data['maxdaydischargemincurrent'] / 3600);
  2016. $h = $h < 10 ? '0'.$h : $h;
  2017. $m = floor($data['maxdaydischargemincurrent'] / 3600 % 60);
  2018. $m = $m < 10 ? '0'.$m : $m;
  2019. $data['maxdaydischargemincurrent'] = $h.':'.$m;
  2020. }
  2021. }
  2022. $list = $temp;
  2023. }else{ // 电量
  2024. $sql = 'select daychargeah,daydischarah,updatetime from battery_info_log where lampid = '.$lampId.' and updatetime >= "'.$beginDate.'" and updatetime <= "'.$endDate.'"';
  2025. $list = $this->db->query($sql)->result_array();
  2026. $temp = [];
  2027. foreach ($list as $value) {
  2028. $temp['daychargeah'][] = $value['daychargeah'];
  2029. $temp['daydischarah'][] = $value['daydischarah'];
  2030. $temp['updatetime'][] = date_change($value['updatetime'],8,$timezone);
  2031. if (!isset($data['maxdaychargeah'])) {
  2032. $data['maxdaychargeah'] = $value['daychargeah'];
  2033. }else{
  2034. $data['maxdaychargeah'] = $value['daychargeah'] > $data['maxdaychargeah'] ? $value['daychargeah'] : $data['maxdaychargeah'];
  2035. }
  2036. if (!isset($data['mindaychargeah'])) {
  2037. $data['mindaychargeah'] = $value['daychargeah'];
  2038. }else{
  2039. $data['mindaychargeah'] = $value['daychargeah'] < $data['mindaychargeah'] ? $value['daychargeah'] : $data['mindaychargeah'];
  2040. }
  2041. if (!isset($data['maxdaydischarah'])) {
  2042. $data['maxdaydischarah'] = $value['daydischarah'];
  2043. }else{
  2044. $data['maxdaydischarah'] = $value['daydischarah'] > $data['maxdaydischarah'] ? $value['daydischarah'] : $data['maxdaydischarah'];
  2045. }
  2046. if (!isset($data['mindaydischarah'])) {
  2047. $data['mindaydischarah'] = $value['daydischarah'];
  2048. }else{
  2049. $data['mindaydischarah'] = $value['daydischarah'] < $data['mindaydischarah'] ? $value['daydischarah'] : $data['mindaydischarah'];
  2050. }
  2051. }
  2052. $list = $temp;
  2053. }
  2054. if (empty($list)) {
  2055. $data['list'] = new StdClass();
  2056. }else{
  2057. $data['list'] = $list;
  2058. }
  2059. exit(json_result('0000',$this->response['0000'],$data));
  2060. }
  2061. }