Lampcontrol.php 153 KB

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