Lampcontrol.php 127 KB

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