Lampcontrol.php 155 KB

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