Lampcontrol.php 128 KB

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