Lampcontrol.php 155 KB

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