Lampcontrol.php 128 KB

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