Lampcontrol.php 127 KB

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