Lampcontrol.php 126 KB

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