Lampcontrol.php 248 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217
  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. $network_id = $this->input->post('network_id', true);
  36. $section = $this->input->post('section', true);
  37. $type = intval($this->input->post('type', true));
  38. $company = intval($this->input->post('company', true));
  39. if (!empty($type) && !empty($company)) {
  40. if ($type == 2) {
  41. $filter['L.manu'] = $company;
  42. }elseif ($type == 3) {
  43. $filter['L.supplier'] = $company;
  44. }elseif ($type == 4) {
  45. $filter['L.po'] = $company;
  46. }else {
  47. $filter['L.upazilla'] = $company;
  48. }
  49. }
  50. if (!empty($network_id)) $filter['L.networkid'] = $network_id;
  51. if (!empty($section)) $filter['L.section'] = $section;
  52. // $offline = $this->input->post('offline', true);
  53. // if (!empty($offline)) $filter['N.status'] = 0;
  54. // $online = $this->input->post('online', true);
  55. // if (!empty($online)) $filter['N.status'] = 1;
  56. // 分页数据
  57. $page = intval($this->input->post('page',true));
  58. $page = empty($page) ? 1 : $page;
  59. $count = intval($this->input->post('count',true));
  60. $limit = empty($count) ? 10 : $count;
  61. $offset = ($page - 1)*$count;
  62. $keyword = $this->input->post('keyword', true);
  63. if ($keyword !== '' && $keyword !== null) $filter['L.number|L.section|L.address'] = $keyword;
  64. $status = $this->input->post('status', true);
  65. if (!empty($status)) {
  66. $filter['L.isfaulted'] = $status;
  67. $filter['AI.stralarmtype !='] = '';
  68. $filter['AI.status'] = 0;
  69. }
  70. $alarmtype = $this->input->post('alarmtype', true);
  71. if (!empty($alarmtype)) $filter['AI.alarmtype'] = $alarmtype;
  72. // 记录用户选择的字段
  73. if (!empty($field1)) {
  74. $fieldData = array('userid'=>$userid,'lampfield'=>$field1);
  75. $this->User_model->update_user_field($fieldData);
  76. }
  77. $lite = $this->input->post('lite', true);
  78. $version = $this->session->userdata('version');
  79. if ($lite == 1) {
  80. if(empty($fields)){
  81. $fields = 'L.id, L.number';
  82. }else{
  83. $fields .= ",RI.updatetime";
  84. }
  85. $join = array();
  86. $data['lamps'] = $this->Lamp_model->get_list_by_join($filter, $fields,$limit, $offset, $join, "L.number ASC", NUll, 'L');
  87. } else {
  88. if(empty($fields) || empty($field1)){
  89. $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';
  90. }else{
  91. $fields .= ",L.id,L.projectid,L.section,L.loadtype,P.timezone as timezoneid";
  92. }
  93. $download = intval($this->input->post('download',true));
  94. if (!empty($download)) {
  95. $limit = null;
  96. $offset = null;
  97. }
  98. $join = array();
  99. $join[] = ['table'=>'project as P', 'cond'=>'L.projectid = P.id', 'type'=>'left'];
  100. $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'];
  101. $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'];
  102. $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'];
  103. $data['lamps'] = $this->Lamp_model->get_list_by_join($filter, $fields,$limit, $offset, $join, "L.number ASC", NUll, 'L');
  104. // $data['lamps'] = $this->Lamp_model->get_list_in('L.projectid', $pid_arr, $fields, $filter);
  105. // var_dump(microtime());
  106. $indexArr = [2=>'16',3=>'32',4=>'48',5=>'64'];
  107. if (empty($version)) {
  108. $chargeStageMap = array(0=>'没有充电',1=>'启动充电',6=>'限流',7=>'充满',16=>'MPPT充电',32=>'均衡充电',48=>'提升充电',64=>'浮充',''=>'未知状态');
  109. $battstatusArr = array(0=>'空闲',1=>'放电',2=>'充电',3=>'一边充电一边放电',''=>'未知状态');
  110. $unknown = '未知状态';
  111. }else{
  112. $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');
  113. $battstatusArr = array(0=>'Free',1=>'Discharge',2=>'Charge',3=>'Discharge while charging',''=>'Unknown state');
  114. $unknown = 'Unknown state';
  115. }
  116. if (!empty($download)) {
  117. $fieldArr = explode(',', $field1);
  118. $fieldArr[] = 'iccid';
  119. $temp = [];
  120. $i = 1;
  121. foreach ($data['lamps'] as $key => &$value) {
  122. $value['section'] = empty($value['section']) ? '' : $value['section'];
  123. if (isset($value['updatetime']) && $value['updatetime'] == '0000-00-00 00:00:00') {
  124. $value['updatetime'] = null;
  125. }
  126. if (array_key_exists('lamppower',$value)) {
  127. $value['lamppower'] = !empty($value['lamppower']) ? $value['lamppower'] : "0";
  128. }
  129. // 故障信息处理
  130. if (array_key_exists('isfaulted',$value)) {
  131. if ($value['isfaulted'] == 0) {
  132. if (empty($version)) {
  133. $value['isfaulted'] = '无';
  134. }else{
  135. $value['isfaulted'] = 'Nothing';
  136. }
  137. }else{
  138. $res = $this->Alarm_model->get_one_by_filter(array('lampid'=>$value['id'],'status'=>0),'stralarmtype');
  139. if (empty($version)) {
  140. $stralarmtype = trim($res['stralarmtype']);
  141. if (!empty($stralarmtype)) {
  142. $alarmArr = explode(',', $stralarmtype);
  143. $temp2 = array();
  144. foreach ($alarmArr as $a) {
  145. if ($a == '电池故障') {
  146. $a = '电池电压异常';
  147. }
  148. $temp2[] = $a;
  149. }
  150. $value['isfaulted'] = implode(',', $temp2);
  151. }else{
  152. $value['isfaulted'] = '无';
  153. }
  154. }else{
  155. $stralarmtype = trim($res['stralarmtype']);
  156. if (!empty($stralarmtype)) {
  157. $alarmArr = explode(',', $stralarmtype);
  158. $temp2 = array();
  159. foreach ($alarmArr as $a) {
  160. if ($a == '电池故障') {
  161. $a = '电池电压异常';
  162. }
  163. $temp[] = alarm_translate($a);
  164. }
  165. $value['isfaulted'] = implode(',', $temp2);
  166. }else{
  167. $value['isfaulted'] = 'Nothing';
  168. }
  169. }
  170. }
  171. }
  172. if (array_key_exists('lampstatus',$value)) {
  173. $value['lampstatus'] = !empty($value['lampstatus']) ? $value['lampstatus'] : "0";
  174. }
  175. if (array_key_exists('electricSOC',$value)) {
  176. $value['electricSOC'] = !empty($value['electricSOC']) ? $value['electricSOC'] : "0";
  177. }
  178. if (array_key_exists('battvoltage',$value)) {
  179. $value['battvoltage'] = !empty($value['battvoltage']) ? $value['battvoltage'] : "0";
  180. }
  181. if (array_key_exists('chargestage',$value)) {
  182. $value['chargestage'] = isset($value['chargestage']) ? $chargeStageMap[$value['chargestage']] : $unknown;
  183. }
  184. if (array_key_exists('battstatus',$value)) {
  185. $value['battstatus'] = isset($value['battstatus']) ? $battstatusArr[$value['battstatus']] : $unknown;
  186. }
  187. if (array_key_exists('overtimes',$value)) {
  188. $value['overtimes'] = !empty($value['overtimes']) ? $value['overtimes'] : 0;
  189. }
  190. if (array_key_exists('solarpower',$value)) {
  191. $value['solarpower'] = !empty($value['solarpower']) ? $value['solarpower'] : 0;
  192. }
  193. if (array_key_exists('iccid',$value)) {
  194. $value['iccid'] = !empty($value['iccid']) ? $value['iccid'] : '';
  195. }
  196. if (array_key_exists('updatetime',$value)) {
  197. $value['updatetime'] = !empty($value['updatetime']) ? $value['updatetime'] : '';
  198. // 通过时区修改时间
  199. if (!empty($value['updatetime'])) {
  200. $timezone = $this->Project_model->get_timezone_by_projectid($value['projectid']);
  201. if ($timezone['value'] != 8) {
  202. $value['updatetime'] = date_change($value['updatetime'],8,$timezone['value']);
  203. }
  204. }
  205. }
  206. // 当前策略
  207. if (array_key_exists('policyid',$value)) {
  208. if (empty($value['policyid'])) {
  209. $value['policyid'] = '';
  210. }else{
  211. $policyData = $this->db->query('select name from policy_info where id = '.$value['policyid'])->row_array();
  212. if (empty($policyData) || empty($policyData['name'])) {
  213. $value['policyid'] = '';
  214. }else{
  215. $value['policyid'] = $policyData['name'];
  216. }
  217. }
  218. }
  219. $temp[$key][] = $i;
  220. foreach ($fieldArr as $f) {
  221. if ($f == 'lampstatus') {
  222. if (empty($version)) {
  223. $temp[$key][] = empty($value[$f]) ? '关灯' : '开灯';
  224. }else{
  225. $temp[$key][] = empty($value[$f]) ? 'off' : 'on';
  226. }
  227. continue;
  228. }
  229. if ($f == 'status') {
  230. if (empty($version)) {
  231. $temp[$key][] = empty($value[$f]) ? '不在线' : '在线';
  232. }else{
  233. $temp[$key][] = empty($value[$f]) ? 'off-line' : 'online';
  234. }
  235. continue;
  236. }
  237. if ($f == 'daychargemincurrent' || $f == 'daydischargemincurrent') {
  238. $t = floor($value[$f]/3600) < 10 ? '0'.floor($value[$f]/3600) : floor($value[$f]/3600);
  239. $m = ceil($value[$f]%3600/60) < 10 ? '0'.ceil($value[$f]%3600/60) : ceil($value[$f]%3600/60);
  240. $temp[$key][] = $t.':'.$m;
  241. continue;
  242. }
  243. $temp[$key][] = $value[$f];
  244. }
  245. $i ++;
  246. }
  247. $title1 = get_fields_by_type($field1,$version);
  248. if (empty($version)) {
  249. $title2 = array_merge(array('编号'),$title1);
  250. }else{
  251. $title2 = array_merge(array('Number'),$title1);
  252. }
  253. $title = [];
  254. foreach ($title2 as $t) {
  255. $title[0][] = $t;
  256. }
  257. $title[0][] = 'iccid';
  258. $data = array_merge($title,$temp);
  259. $path = push_excel($data,'lampinfoExcel_'.date('Ymd'));
  260. exit(json_result('0000', $this->response['0000'], array('path'=>$path)));
  261. }
  262. $timezone = null;
  263. foreach ($data['lamps'] as $key => &$value) {
  264. $value['section'] = empty($value['section']) ? '' : $value['section'];
  265. if (isset($value['updatetime']) && $value['updatetime'] == '0000-00-00 00:00:00') {
  266. $value['updatetime'] = null;
  267. }
  268. if (array_key_exists('lamppower',$value)) {
  269. $value['lamppower'] = !empty($value['lamppower']) ? $value['lamppower'] : "0";
  270. }
  271. // 故障信息处理
  272. if (array_key_exists('isfaulted',$value)) {
  273. if ($value['isfaulted'] == 0) {
  274. if (empty($version)) {
  275. $value['isfaulted'] = '无';
  276. }else{
  277. $value['isfaulted'] = 'Nothing';
  278. }
  279. }else{
  280. $res = $this->Alarm_model->get_one_by_filter(array('lampid'=>$value['id'],'status'=>0),'stralarmtype');
  281. if (empty($version)) {
  282. // $value['isfaulted'] = empty($res['stralarmtype']) ? '无' : $res['stralarmtype'];
  283. $stralarmtype = trim($res['stralarmtype']);
  284. if (!empty($stralarmtype)) {
  285. $alarmArr = explode(',', $stralarmtype);
  286. $temp = array();
  287. foreach ($alarmArr as $a) {
  288. if ($a == '电池故障') {
  289. $a = '电池电压异常';
  290. }
  291. $temp[] = $a;
  292. }
  293. $value['isfaulted'] = implode(',', $temp);
  294. }else{
  295. $value['isfaulted'] = '无';
  296. }
  297. }else{
  298. $stralarmtype = trim($res['stralarmtype']);
  299. if (!empty($stralarmtype)) {
  300. $alarmArr = explode(',', $stralarmtype);
  301. $temp = array();
  302. foreach ($alarmArr as $a) {
  303. if ($a == '电池故障') {
  304. $a = '电池电压异常';
  305. }
  306. $temp[] = alarm_translate($a);
  307. }
  308. $value['isfaulted'] = implode(',', $temp);
  309. }else{
  310. $value['isfaulted'] = 'Nothing';
  311. }
  312. }
  313. }
  314. }
  315. if (array_key_exists('lampstatus',$value)) {
  316. $value['lampstatus'] = !empty($value['lampstatus']) ? $value['lampstatus'] : "0";
  317. }
  318. if (array_key_exists('electricSOC',$value)) {
  319. $value['electricSOC'] = !empty($value['electricSOC']) ? $value['electricSOC'] : "0";
  320. }
  321. if (array_key_exists('battvoltage',$value)) {
  322. $value['battvoltage'] = !empty($value['battvoltage']) ? $value['battvoltage'] : "0";
  323. }
  324. if (array_key_exists('chargestage',$value)) {
  325. $value['chargestage'] = isset($value['chargestage']) && isset($chargeStageMap[$value['chargestage']]) ? $chargeStageMap[$value['chargestage']] : $unknown;
  326. }
  327. if (array_key_exists('battstatus',$value)) {
  328. $value['battstatus'] = isset($value['battstatus']) ? $battstatusArr[$value['battstatus']] : $unknown;
  329. }
  330. if (array_key_exists('overtimes',$value)) {
  331. $value['overtimes'] = !empty($value['overtimes']) ? $value['overtimes'] : 0;
  332. }
  333. if (array_key_exists('solarpower',$value)) {
  334. $value['solarpower'] = !empty($value['solarpower']) ? $value['solarpower'] : 0;
  335. }
  336. // 当前策略
  337. if (array_key_exists('policyid',$value)) {
  338. if (empty($value['policyid'])) {
  339. $value['policyid'] = '';
  340. }else{
  341. $policyData = $this->db->query('select name from policy_info where id = '.$value['policyid'])->row_array();
  342. if (empty($policyData) || empty($policyData['name'])) {
  343. $value['policyid'] = '';
  344. }else{
  345. $value['policyid'] = $policyData['name'];
  346. }
  347. }
  348. }
  349. if (array_key_exists('updatetime',$value)) {
  350. $value['updatetime'] = !empty($value['updatetime']) ? $value['updatetime'] : '';
  351. // 通过时区修改时间
  352. if (!empty($value['updatetime'])) {
  353. // $value['timezoneid'];
  354. if($timezone === null) $timezone = $this->db->query('select value from timezone where id = '.$value['timezoneid'])->row_array();
  355. if ($timezone['value'] != 8) {
  356. $value['updatetime'] = date_change($value['updatetime'],0,$timezone['value']);
  357. }
  358. }
  359. }
  360. }
  361. if (!empty($project_id)) {
  362. }
  363. }
  364. unset($filter['page']);
  365. unset($filter['count']);
  366. $data['total_data']['install_num'] = $this->Lamp_model->get_count(array('projectid'=>$project_id));
  367. $data['total_data']['light_num'] = $this->Lamp_model->get_count(array('projectid'=>$project_id,'netstatus'=>1,'status'=>1));
  368. $data['total_data']['online_num'] = $this->Lamp_model->get_count(array('projectid'=>$project_id,'netstatus'=>1));
  369. $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);
  370. $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;
  371. $data['total_data']['light_rate'] .= '%';
  372. $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;
  373. $data['total_data']['online_rate'] .= '%';
  374. $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;
  375. $data['total_data']['fault_rate'] .= '%';
  376. // $data['total_data']['network_num'] = $this->Network_model->get_count(['projectid'=>$project_id,'type'=>1]);
  377. $res = $this->Lamp_model->get_list_by_multi_join($filter, 'count(*) total',$limit, $offset, $join, "L.number ASC", NUll, 'L',true);
  378. $result = !empty($pid_arr) ? $this->Lamp_model->get_total('L.projectid', $pid_arr, 'L.id') : array();
  379. $result = $this->Lamp_model->get_count(['projectid'=>$pid_arr]);
  380. $data['total'] = $res['total'];
  381. $data['count'] = $result;
  382. $data['fault'] = $res['total'];
  383. exit(json_result('0000', $this->response['0000'], $data));
  384. }
  385. // 更新灯控信息
  386. public function update() {
  387. $role = $this->get_user_info('role');
  388. $username = $this->input->post('username',true);
  389. $client_key = $this->input->post('client_key',true);
  390. $token = $this->input->post('token',true);
  391. $type = intval($this->input->post('type',true));
  392. $lamp_id = $this->input->post('lamp_id',true);
  393. $clientid = $this->input->post('clientid',true);
  394. $version = $this->session->userdata('version');
  395. $lamp_id = $this->input->post('lamp_id', true);
  396. if (empty($lamp_id)) {
  397. exit(json_result('0411', $this->response['0411'], array()));
  398. }
  399. $ids = explode(',', $lamp_id);
  400. $status = 1;
  401. foreach ($ids as $v) {
  402. $record['cmdtype'] = 0;
  403. $record['relateid'] = $v;
  404. $record['statuscmd'] = 0;
  405. $record['updatetime'] = date("Y-m-d H:i:s");
  406. $recordid = $this->Batmanage_model->get_record_id($record, 'batch_update_cmd');
  407. if(!$recordid){
  408. exit(json_result('0009', $this->response['0009'], array()));
  409. }
  410. if ($recordid != -1) {
  411. $status = 0;
  412. $cmd = '{"cmd_type":"batch_update_cmd","cmd_id":'.$recordid.'}';
  413. $lampData = $this->Lamp_model->getOne($v,'N.status,N.protocoltype,L.protocoltype as lampprotocoltype');
  414. if ($lampData['protocoltype'] == 4 || $lampData['protocoltype'] == 6) {
  415. $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/update_asy';
  416. doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'lamp_id'=>$v]);
  417. continue;
  418. }
  419. if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
  420. $cmdret = send_cmd($cmd,1,0,$lampData['protocoltype']);
  421. if ($cmdret === false) {
  422. sleep(1);
  423. $cmdret = send_cmd($cmd,1,0,$lampData['protocoltype']);
  424. }
  425. }else{
  426. $cmdret = send_cmd($cmd,0,0,$lampData['protocoltype']);
  427. if ($cmdret === false) {
  428. sleep(1);
  429. $cmdret = send_cmd($cmd,0,0,$lampData['protocoltype']);
  430. }
  431. }
  432. if($cmdret === false){
  433. exit(json_result('0008', $this->response['0008'], array()));
  434. }
  435. }
  436. }
  437. sleep(2);
  438. if ($status) {
  439. exit(json_result('0406', $this->response['0406'], array()));
  440. }else{
  441. exit(json_result('0407', $this->response['0407'], array()));
  442. }
  443. }
  444. // 异步更新数据
  445. public function update_asy(){
  446. $lamp_id = $this->input->post('lamp_id',true);
  447. $lampData = $this->Lamp_model->getOne($lamp_id,'L.number,N.protocoltype,L.protocoltype as lampprotocoltype,L.address,L.networkid,N.status,N.deviceid');
  448. if ($lampData['lampprotocoltype'] == 0) {
  449. $address = '';
  450. if (mb_strlen($lampData['address']) >= 8) {
  451. $address = mb_substr($lampData['address'], -8);
  452. }else{
  453. $address = '00000000';
  454. }
  455. $sendData = $address.'0300004646';
  456. if ($lampData['protocoltype'] == 4) {
  457. $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*','0001'.$sendData));
  458. }else{
  459. // $cmd .= crc16(pack('H*',$sendData));
  460. device_cmd($lampData['deviceid'],pack('H*',$sendData));
  461. $cmd = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
  462. $info = send_lorawan_915($cmd);
  463. $res = array();
  464. if (!empty($info)) {
  465. $info = json_decode($info,true);
  466. if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
  467. }
  468. $file = fopen('./file/lorawan.txt', 'a+');
  469. fwrite($file, $res.'----'.date('Y-m-d H:i:s',time()).'
  470. ');
  471. fclose($file);
  472. // die;
  473. }
  474. if (!empty($res)) {
  475. $msg1 = $res['msg'];
  476. // 数据校验
  477. $msg = mb_substr($msg1, 10);
  478. $t = 0;
  479. for ($i=0; $i < mb_strlen($msg) / 2 - 1; $i++) {
  480. $s = mb_substr($msg, $i*2,2);
  481. $t += base_convert($s, 16, 10);
  482. }
  483. $t = base_convert($t, 10, 16);
  484. if (mb_strlen($t) > 2) {
  485. $t = mb_substr($t, -2);
  486. }elseif($t <= 1){
  487. $t = '0'.$t;
  488. }
  489. if ($t == mb_substr($msg, -2)) {
  490. // 校验通过,解析数据
  491. $updatetime = date('Y-m-d H:i:s',time());
  492. // 系统信息
  493. $systemInfoStr = mb_substr($msg1, 16,6);
  494. $systemInfo = array(
  495. 'sysvoltage' => base_convert(mb_substr($systemInfoStr, 0,2), 16, 10),
  496. 'syscurrent' => base_convert(mb_substr($systemInfoStr, 2,2), 16, 10),
  497. 'temper' => bitDecode(mb_substr($systemInfoStr, 4,2)),
  498. 'updatetime' => $updatetime,
  499. 'lampid' => $lamp_id,
  500. );
  501. $this->db->insert('system_info_log', $systemInfo);
  502. // 路灯信息
  503. $lampInfoStr = mb_substr($msg1, 22,12);
  504. $lampInfo = array(
  505. 'lampvoltage' => round(base_convert(mb_substr($lampInfoStr, 2,4), 16, 10)/10,1),
  506. 'lampcurrent' => round(base_convert(mb_substr($lampInfoStr, 6,4), 16, 10)/100,2),
  507. 'temper' => bitDecode(mb_substr($lampInfoStr, 10,2)),
  508. 'updatetime' => $updatetime,
  509. 'lampid' => $lamp_id,
  510. );
  511. $lampInfo['lamppower'] = round($lampInfo['lampvoltage'] * $lampInfo['lampcurrent'],2);
  512. $light = base_convert(mb_substr($lampInfoStr, 0,2), 16, 2);
  513. if (mb_strlen($light) == 8) {
  514. $lampInfo['lighteness'] = base_convert(mb_substr($light, -7), 2, 10);
  515. $lampInfo['lampstatus'] = 1;
  516. }else{
  517. $lampInfo['lighteness'] = 0;
  518. $lampInfo['lampstatus'] = 0;
  519. }
  520. $this->db->insert('lamp_info_log', $lampInfo);
  521. // 太阳能信息
  522. $solarInfoStr = mb_substr($msg1, 34,8);
  523. $solarInfo = array(
  524. 'solarvoltage' => round(base_convert(mb_substr($solarInfoStr, 0,4), 16, 10)/10,1),
  525. 'solarcurrent' => round(base_convert(mb_substr($solarInfoStr, 4,4), 16, 10)/100,2),
  526. 'updatetime' => $updatetime,
  527. 'lampid' => $lamp_id,
  528. );
  529. $solarInfo['solarpower'] = round($solarInfo['solarvoltage'] * $solarInfo['solarcurrent'],2);
  530. $this->db->insert('solar_info_log', $solarInfo);
  531. // 蓄电池信息
  532. $batterInfoStr = mb_substr($msg1, 42,70);
  533. $batteryInfo = array(
  534. 'battvoltage' => round(base_convert(mb_substr($batterInfoStr, 2,4), 16, 10)/10,1),
  535. 'chargecurrent' => round(base_convert(mb_substr($batterInfoStr, 6,4), 16, 10)/100,2),
  536. 'discharcurrent' => round(base_convert(mb_substr($batterInfoStr, 10,4), 16, 10)/100,2),
  537. 'batttemper' => bitDecode(mb_substr($batterInfoStr, 14,2)),
  538. 'electrictotal' => base_convert(mb_substr($batterInfoStr, 16,4),16,10),
  539. 'electricleft' => base_convert(mb_substr($batterInfoStr, 20,4),16,10),
  540. 'electricSOC' => base_convert(mb_substr($batterInfoStr, 24,2),16,10),
  541. 'voltagedaymin' => round(base_convert(mb_substr($batterInfoStr, 26,4),16,10)/10,1),
  542. 'voltagedaymax' => round(base_convert(mb_substr($batterInfoStr, 30,4),16,10)/10,1),
  543. 'daychargeah' => round(base_convert(mb_substr($batterInfoStr, 34,4),16,10)/10,1),
  544. 'daydischarah' => round(base_convert(mb_substr($batterInfoStr, 38,4),16,10)/10,1),
  545. 'daychargemaxpow' => round(base_convert(mb_substr($batterInfoStr, 42,4),16,10)/10,1),
  546. 'daydischarmaxpow' => round(base_convert(mb_substr($batterInfoStr, 46,4),16,10)/10,1),
  547. 'daychargemincurrent' => base_convert(mb_substr($batterInfoStr, 50,4),16,10),
  548. 'daycharmaxcurrent' => round(base_convert(mb_substr($batterInfoStr, 54,4),16,10)/100,2),
  549. 'daydischargemincurrent' => base_convert(mb_substr($batterInfoStr, 58,4),16,10),
  550. 'daydischarmaxcurrent' => round(base_convert(mb_substr($batterInfoStr, 62,4),16,10)/100,2),
  551. 'daybattmintemper' => bitDecode(mb_substr($batterInfoStr, 66,2)),
  552. 'daybattmaxtemper' => bitDecode(mb_substr($batterInfoStr, 68,2)),
  553. 'updatetime' => $updatetime,
  554. 'lampid' => $lamp_id,
  555. );
  556. $batteryInfo['chargepower'] = round($batteryInfo['chargecurrent'] * $batteryInfo['battvoltage'],2);
  557. $batteryInfo['dischargepower'] = round($batteryInfo['discharcurrent'] * $batteryInfo['battvoltage'],2);
  558. $chargestage = base_convert(mb_substr($solarInfoStr, 0,2), 16, 2);
  559. $chargestage = mb_strlen($chargestage) < 8 ? mb_substr('00000000'.$chargestage, -8) : $chargestage;
  560. $batteryInfo['chargestage'] = base_convert(mb_substr($chargestage, 0,4).'0000', 2, 10);
  561. $batteryInfo['battstatus'] = base_convert(mb_substr($chargestage, -4), 2, 10);
  562. $this->db->insert('battery_info_log', $batteryInfo);
  563. // 发用电量信息
  564. $eleInfoStr = mb_substr($msg1, 112,24);
  565. $eleInfo = array(
  566. 'daygeneration' => round(base_convert(mb_substr($eleInfoStr, 0,4),16,10)/10,1),
  567. 'dayconsumption' => round(base_convert(mb_substr($eleInfoStr, 4,4),16,10)/10,1),
  568. 'totalgeneration' => round(base_convert(mb_substr($eleInfoStr, 8,8),16,10)/10,1),
  569. 'totalconsumption' => round(base_convert(mb_substr($eleInfoStr, 16,8),16,10)/10,1),
  570. 'updatetime' => $updatetime,
  571. 'lampid' => $lamp_id,
  572. );
  573. $this->db->insert('electric_info_log', $eleInfo);
  574. // 历史数据信息
  575. $historyInfoStr = mb_substr($msg1, 136,12);
  576. $historyInfo = array(
  577. 'rundays' => base_convert(mb_substr($historyInfoStr, 0,4),16,10),
  578. 'overtimes' => base_convert(mb_substr($historyInfoStr, 4,4),16,10),
  579. 'fulltimes' => base_convert(mb_substr($historyInfoStr, 8,4),16,10),
  580. 'updatetime' => $updatetime,
  581. 'lampid' => $lamp_id,
  582. );
  583. $historyInfo['totalchargeah'] = $batteryInfo['daychargeah'];
  584. $historyInfo['totaldischarah'] = $batteryInfo['daydischarah'];
  585. $this->db->insert('history_info_log', $historyInfo);
  586. // 故障信息
  587. $alarmInfoStr = mb_substr($msg1, 148,8);
  588. $alarmInfo = array(
  589. 'alarmtype' => 0,
  590. 'stralarmtype' => '',
  591. 'updatetime' => $updatetime,
  592. 'lampid' => $lamp_id,
  593. );
  594. $alarmArr = array('0'=>'过放','1'=>'超压','2'=>'负载短路','3'=>'电池故障','4'=>'内部超温','5'=>'外部超温','7'=>'负载开路','8'=>'输出电容超压保护','10'=>'电池板短路','11'=>'电池板超压','13'=>'充电逆流','15'=>'锂电池低温关闭充电');
  595. $isfaulted = 0;
  596. if ($alarmInfoStr != '00000000') {
  597. $res = base_convert($alarmInfoStr, 16, 2);
  598. var_dump($res);
  599. $res = strrev($res);
  600. $t = array();
  601. for ($i=0; $i < mb_strlen($res); $i++) {
  602. if (mb_substr($res, $i,1) == 1 && isset($alarmArr[$i])) {
  603. $t[] = $alarmArr[$i];
  604. }
  605. }
  606. if (!empty($t)) {
  607. $alarmInfo['alarmtype'] = base_convert($alarmInfoStr, 16, 10);
  608. $alarmInfo['stralarmtype'] = implode(',', $t);
  609. }
  610. }
  611. if (!empty($alarmInfo['stralarmtype'])) {
  612. $this->db->insert('alarm_info_log', $alarmInfo);
  613. $isfaulted = 1;
  614. }
  615. $this->Lamp_model->update(array('id'=>$lamp_id),array('status'=>$lampInfo['lampstatus'],'lighteness'=>$lampInfo['lighteness'],'isfaulted'=>$isfaulted));
  616. if ($lampData['status'] == 0) $this->Network_model->update(array('id'=>$lampData['networkid']),array('status'=>1));
  617. }
  618. }
  619. }elseif ($lampData['lampprotocoltype'] == 1) {
  620. $sendData = '0103000a0002e409';
  621. $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*','0001'.$sendData));
  622. if (!empty($res) && strtolower(crc16(pack('H*',mb_substr($res['msg'], 0,-4)))) == strtolower(mb_substr($res['msg'], -4))) {
  623. $sysInfoStr = $res['msg'];
  624. $sendData = '010300fd003115ee';
  625. $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*','0001'.$sendData));
  626. if (!empty($res) && strtolower(crc16(pack('H*',mb_substr($res['msg'], 0,-4)))) == strtolower(mb_substr($res['msg'], -4))) {
  627. $data = array();
  628. $msg = $res['msg'];
  629. // 校验通过,解析数据
  630. $updatetime = date('Y-m-d H:i:s',time());
  631. // 系统信息
  632. $systemInfo = array(
  633. 'sysvoltage' => base_convert(mb_substr($sysInfoStr, 6,2), 16, 10),
  634. 'syscurrent' => base_convert(mb_substr($sysInfoStr, 8,2), 16, 10),
  635. 'temper' => bitDecode(mb_substr($msg, 30,2)),
  636. 'updatetime' => $updatetime,
  637. 'lampid' => $lamp_id,
  638. );
  639. $this->db->insert('system_info_log', $systemInfo);
  640. // 路灯信息
  641. $lampInfoStr = mb_substr($msg, 34,12);
  642. $lampInfo = array(
  643. 'lampvoltage' => round(base_convert(mb_substr($lampInfoStr, 0,4), 16, 10)/10,1),
  644. 'lampcurrent' => round(base_convert(mb_substr($lampInfoStr, 4,4), 16, 10)/100,2),
  645. 'updatetime' => $updatetime,
  646. 'lampid' => $lamp_id,
  647. 'lamppower' => base_convert(mb_substr($lampInfoStr, 8,4), 16, 10),
  648. );
  649. $light = base_convert(mb_substr($msg, 6,2), 16, 2);
  650. if (base_convert(mb_substr('00000000'.$light, -7), 2, 10) > 0) {
  651. $lampInfo['lighteness'] = base_convert(mb_substr($light, -7), 2, 10);
  652. $lampInfo['lampstatus'] = 1;
  653. }else{
  654. $lampInfo['lighteness'] = 0;
  655. $lampInfo['lampstatus'] = 0;
  656. }
  657. $this->db->insert('lamp_info_log', $lampInfo);
  658. // 太阳能信息
  659. $solarInfoStr = mb_substr($msg, 46,12);
  660. $solarInfo = array(
  661. 'solarvoltage' => round(base_convert(mb_substr($solarInfoStr, 0,4), 16, 10)/10,1),
  662. 'solarcurrent' => round(base_convert(mb_substr($solarInfoStr, 4,4), 16, 10)/100,2),
  663. 'updatetime' => $updatetime,
  664. 'lampid' => $lamp_id,
  665. );
  666. $solarInfo['solarpower'] = round($solarInfo['solarvoltage'] * $solarInfo['solarcurrent'],2);
  667. $this->db->insert('solar_info_log', $solarInfo);
  668. // 蓄电池信息
  669. // $batterInfoStr = mb_substr($msg, 42,70);
  670. $batteryInfo = array(
  671. 'battvoltage' => round(base_convert(mb_substr($msg, 22,4), 16, 10)/10,1),
  672. 'chargecurrent' => round(base_convert(mb_substr($msg, 26,4), 16, 10)/100,2),
  673. // 'discharcurrent' => round(base_convert(mb_substr($msg, 10,4), 16, 10)/100,2),
  674. 'batttemper' => bitDecode(mb_substr($msg, 32,2)),
  675. // 'electrictotal' => base_convert(mb_substr($msg, 16,4),16,10),
  676. // 'electricleft' => base_convert(mb_substr($msg, 20,4),16,10),
  677. 'electricSOC' => base_convert(mb_substr($msg, 18,4),16,10),
  678. 'voltagedaymin' => round(base_convert(mb_substr($msg, 62,4),16,10)/10,1),
  679. 'voltagedaymax' => round(base_convert(mb_substr($msg, 66,4),16,10)/10,1),
  680. 'daychargeah' => base_convert(mb_substr($msg, 86,4),16,10),
  681. 'daydischarah' => base_convert(mb_substr($msg, 90,4),16,10),
  682. 'daychargemaxpow' => base_convert(mb_substr($msg, 78,4),16,10),
  683. 'daydischarmaxpow' => base_convert(mb_substr($msg, 82,4),16,10),
  684. 'daychargemincurrent' => base_convert(mb_substr($msg, 174,4),16,10),
  685. 'daycharmaxcurrent' => round(base_convert(mb_substr($msg, 70,4),16,10)/100,2),
  686. 'daydischargemincurrent' => base_convert(mb_substr($msg, 194,4),16,10),
  687. 'daydischarmaxcurrent' => round(base_convert(mb_substr($msg, 74,4),16,10)/100,2),
  688. 'daybattmintemper' => doubBitDecode(mb_substr($msg, 162,4)),
  689. 'daybattmaxtemper' => doubBitDecode(mb_substr($msg, 158,4)),
  690. 'updatetime' => $updatetime,
  691. 'lampid' => $lamp_id,
  692. );
  693. $batteryInfo['chargepower'] = round($batteryInfo['chargecurrent'] * $batteryInfo['battvoltage'],2);
  694. // $batteryInfo['dischargepower'] = round($batteryInfo['discharcurrent'] * $batteryInfo['battvoltage'],2);
  695. $status = base_convert(mb_substr($msg, 8,2),16,10);
  696. if (mb_strlen($light) == 8 && mb_substr($light, 0,1) == 1) {
  697. $batteryInfo['battstatus'] = 1;
  698. }else{
  699. if ($status != 0){
  700. $batteryInfo['battstatus'] = 2;
  701. }else{
  702. $batteryInfo['battstatus'] = 0;
  703. }
  704. }
  705. $batteryInfo['chargestage'] = $status;
  706. $this->db->insert('battery_info_log', $batteryInfo);
  707. // 发用电量信息
  708. // $eleInfoStr = mb_substr($msg1, 112,24);
  709. $eleInfo = array(
  710. 'daygeneration' => round(base_convert(mb_substr($msg, 94,4),16,10)/1000,3),
  711. 'dayconsumption' => round(base_convert(mb_substr($msg, 98,4),16,10)/1000,3),
  712. 'totalgeneration' => round(base_convert(mb_substr($msg, 134,8),16,10)/1000,3),
  713. 'totalconsumption' => round(base_convert(mb_substr($msg, 142,8),16,10)/1000,3),
  714. 'updatetime' => $updatetime,
  715. 'lampid' => $lamp_id,
  716. );
  717. $this->db->insert('electric_info_log', $eleInfo);
  718. // 历史数据信息
  719. $historyInfoStr = mb_substr($msg, 102,28);
  720. $historyInfo = array(
  721. 'rundays' => base_convert(mb_substr($historyInfoStr, 0,4),16,10),
  722. 'overtimes' => base_convert(mb_substr($historyInfoStr, 4,4),16,10),
  723. 'fulltimes' => base_convert(mb_substr($historyInfoStr, 8,4),16,10),
  724. 'totalchargeah' => base_convert(mb_substr($historyInfoStr, 12,8),16,10),
  725. 'totaldischarah' => base_convert(mb_substr($historyInfoStr, 20,8),16,10),
  726. 'updatetime' => $updatetime,
  727. 'lampid' => $lamp_id,
  728. );
  729. $this->db->insert('history_info_log', $historyInfo);
  730. // 故障信息
  731. $alarmInfoStr = mb_substr($msg, 10,8);
  732. $alarmInfo = array(
  733. 'alarmtype' => 0,
  734. 'stralarmtype' => '',
  735. 'updatetime' => $updatetime,
  736. 'lampid' => $lamp_id,
  737. );
  738. $alarmArr = array('10'=>'BMS过充保护','11'=>'电池低温保护,停止充电','12'=>'电池反接','13'=>'电容超压','14'=>'感应探头损坏','15'=>'负载开路','16'=>'蓄电池过放','17'=>'蓄电池超压','18'=>'欠压警告','19'=>'负载短路','20'=>'负载功率过大或负载过流','21'=>'控制器温度过高','22'=>'外部环境温度过高','23'=>'光伏输入功率过大','24'=>'光伏输入端短路','25'=>'光伏输入端超压','26'=>'太阳板逆流','27'=>'太阳板工作点超压','28'=>'太阳板反接');
  739. $isfaulted = 0;
  740. if ($alarmInfoStr != '00000000') {
  741. $res = base_convert($alarmInfoStr, 16, 2);
  742. var_dump($res);
  743. $res = strrev($res);
  744. $t = array();
  745. for ($i=0; $i < mb_strlen($res); $i++) {
  746. if (mb_substr($res, $i,1) == 1 && isset($alarmArr[$i])) {
  747. $t[] = $alarmArr[$i];
  748. }
  749. }
  750. if (!empty($t)) {
  751. $alarmInfo['alarmtype'] = base_convert($alarmInfoStr, 16, 10);
  752. $alarmInfo['stralarmtype'] = implode(',', $t);
  753. }
  754. }
  755. if (!empty($alarmInfo['stralarmtype'])) {
  756. $this->db->insert('alarm_info_log', $alarmInfo);
  757. $isfaulted = 1;
  758. }
  759. $this->Lamp_model->update(array('id'=>$lamp_id),array('status'=>$lampInfo['lampstatus'],'lighteness'=>$lampInfo['lighteness'],'isfaulted'=>$isfaulted));
  760. if ($lampData['status'] == 0) $this->Network_model->update(array('id'=>$lampData['networkid']),array('status'=>1));
  761. }
  762. }
  763. }
  764. }
  765. // 灯控的开启/关闭(支持批量处理)
  766. public function turnonoff(){
  767. $username = $this->input->post('username',true);
  768. $client_key = $this->input->post('client_key',true);
  769. $token = $this->input->post('token',true);
  770. $type = intval($this->input->post('type',true));
  771. $lamp_id = $this->input->post('lamp_id',true);
  772. $clientid = $this->input->post('clientid',true);
  773. $version = $this->session->userdata('version');
  774. $lampArr = explode(',', $lamp_id);
  775. $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/dimming_asy';
  776. $lightness = empty($type) ? 0 : 100;
  777. foreach ($lampArr as $l) {
  778. doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'type'=>$type,'lamp_id'=>$l,'lightness'=>$lightness]);
  779. }
  780. exit();
  781. }
  782. // 灯控调光
  783. public function dimming(){
  784. $username = $this->input->post('username',true);
  785. $client_key = $this->input->post('client_key',true);
  786. $token = $this->input->post('token',true);
  787. $lamp_id = $this->input->post('lamp_id',true);
  788. $lightness = $this->input->post('lightness',true);
  789. $clientid = $this->input->post('clientid',true);
  790. $version = $this->session->userdata('version');
  791. $lampArr = explode(',', $lamp_id);
  792. // $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
  793. $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/dimming_asy';
  794. foreach ($lampArr as $l) {
  795. doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'lamp_id'=>$l,'lightness'=>$lightness]);
  796. }
  797. exit();
  798. }
  799. public function dimming_asy(){
  800. set_time_limit(0);
  801. $lamp_id = intval($this->input->post('lamp_id',true));
  802. $lightness = intval($this->input->post('lightness',true));
  803. $clientid = $this->input->post('clientid',true);
  804. $version = $this->input->post('version',true);
  805. if (empty($lamp_id)) {
  806. exit(json_result('0400', $this->response['0400'], array()));
  807. }
  808. $lampData = $this->Lamp_model->get_one(['id'=>$lamp_id],'number,address');
  809. $seq = get_seq();
  810. $seq = '01';
  811. $sendData = '12'.PROTOCOL_VERSION.$seq.'0000000000';
  812. $sendData .= $lightness > 0 ? '01' : '00';
  813. $lightness2 = empty($lightness) ? '00' : '00'.base_convert($lightness, 10, 16);
  814. $sendData .= strlen($lightness2) == 2 ? $lightness2 : substr($lightness2, -2);
  815. $sendData .= '00';
  816. $sendData .= crc16(pack('H*',$sendData));
  817. var_dump($sendData);
  818. $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*',$sendData),30);
  819. var_dump('/IDCOL/CmdInput/'.$lampData['address']);
  820. if (empty($res)) {
  821. $data = ['code'=>'0016','status'=>2,'msg'=>$this->response['0016'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
  822. exit(send_websocket($clientid,$data));
  823. }else{
  824. $res['msg'] = unpack('H*', $res['msg'])[1];
  825. var_dump($res['msg']);
  826. if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'], -4)) && strtolower(substr($res['msg'], 18,2)) == '01') {
  827. if ($lightness != 0) {
  828. $this->Lamp_model->update(array('id'=>$lampData['id']),array('status'=>1,'lighteness'=>$lightness));
  829. }else{
  830. $this->Lamp_model->update(array('id'=>$lampData['id']),array('status'=>0,'lighteness'=>0));
  831. }
  832. $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$lamp_id),'P.projectname,N.networkname,L.number,L.address');
  833. $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯调光\"{$lightness}%\"",0);
  834. $this->add_operation_log('cmd',"Light dimming:\"{$lampData['number']}\".Wireless module address:\"{$lampData['address']}\".Project name:\"{$lampData['projectname']}\".Network name:\"{$lampData['networkname']}\".Lighteness:\"{$lightness}%\"",0,1);
  835. exit(json_result('0000',$this->response['0000'],['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]));
  836. }else{
  837. exit(json_result('0010',$this->response['0010'],['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]));
  838. }
  839. }
  840. }
  841. // 删除灯控(支持批量处理)
  842. public function del(){
  843. $role = $this->get_user_info('role');
  844. $lamp_ids = $this->input->post('lamp_ids',true);
  845. if (empty($lamp_ids)) {
  846. exit(json_result('0400', $this->response['0400'], array()));
  847. }
  848. $ids = explode(",", $lamp_ids);
  849. // 获取灯控信息
  850. $dataArr = $this->Lamp_model->getBatch($ids);
  851. // 修改网络跟项目下灯控数量
  852. foreach ($dataArr as $data) {
  853. $data['faultcount'] = $data['isfaulted'] > 0 ? 1 : 0;
  854. $this->Network_model->minus_lamp_count($data);
  855. $this->Project_model->minus_lamp_count($data);
  856. $netData = $this->Network_model->getOne($data['networkid'],'gatewaytype,protocoltype');
  857. if ($netData['protocoltype'] == 6) $this->remove_device('00000000'.$data['address']);
  858. if (isset($netData) && $netData['gatewaytype'] == 'direct' && !empty($data['networkid'])) {// 直连设备
  859. // 删除网络
  860. // $this->Zone_model->delOne($data['networkid']);
  861. $this->Network_model->delData(array('id'=>$data['networkid']));
  862. $cmdstr = '{"cmd_type":"delete_network_cmd","cmd_id":'.$data['networkid'].'}';
  863. // send_cmd($cmdstr,0);
  864. if (isset($netData['protocoltype']) && $netData['protocoltype'] != 0) {
  865. $cmdret = send_cmd($cmdstr,1,0,$netData['protocoltype']);
  866. if ($cmdret === false) {
  867. sleep(1);
  868. $cmdret = send_cmd($cmdstr,1,0,$netData['protocoltype']);
  869. }
  870. }else{
  871. $cmdret = send_cmd($cmdstr,0,0,$netData['protocoltype']);
  872. if ($cmdret === false) {
  873. sleep(1);
  874. $cmdret = send_cmd($cmdstr,0,0,$netData['protocoltype']);
  875. }
  876. }
  877. }
  878. }
  879. // 删除灯控告警信息
  880. $this->Alarm_model->delBatch($ids, 'lampid');
  881. foreach ($ids as $v) {
  882. $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$v),'P.projectname,N.networkname,L.number,L.address');
  883. $this->add_operation_log('delete',"删除\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯",0);
  884. $this->add_operation_log('delete',"Delete lamp:\"{$lampData['number']}\".Wireless module address:\"{$lampData['address']}\".Project name:\"{$lampData['projectname']}\".Network name:\"{$lampData['networkname']}\"",0,1);
  885. }
  886. $this->Lamp_model->delBatch($ids);
  887. exit(json_result('0000', $this->response['0000'], array()));
  888. }
  889. // 默认巡检设置
  890. private function patrol_set($lampid,$time = null){
  891. if (isset($time) && !empty($time)) {
  892. $where['id'] = $lampid;
  893. $update['patroltype'] = 1;
  894. $update['patrolinterval'] = $time;
  895. $update['updatetime'] = date("Y-m-d H:i:s");
  896. $update['nextquerytime'] = date("Y-m-d H:i:s");
  897. $ret = $this->Patrol_model->get_one($where);
  898. if(empty($ret)){
  899. $update['id'] = $where['id'];
  900. $this->Patrol_model->add($update);
  901. } else {
  902. $this->Patrol_model->update($update,$where);
  903. }
  904. $res = $this->Lamp_model->getOne($lampid,'N.protocoltype,L.address,L.protocoltype AS lampprotocoltype');
  905. if ($res['protocoltype'] == 4) {
  906. $sendData = '000101A501';;
  907. $sendData .= bitSubStr(base_convert(intval($time/5), 10, 16));
  908. lampMqttCmd('/lamp/WriteIn/'.$res['address'],'/lamp/WriteOut/'.$res['address'],pack('H*', $sendData),0);
  909. return;
  910. }
  911. if (isset($res['protocoltype']) && $res['protocoltype'] != 0) {
  912. $cmd = '{"cmd_type":"set_patrol_cmd","cmd_id":'.$where['id'].',"broadcast":0}';
  913. $cmdret = send_cmd($cmd,1, 0,$res['protocoltype']);
  914. // if ($cmdret === false) {
  915. // sleep(1);
  916. // $cmdret = send_cmd($cmd,1, 0,$res['protocoltype']);
  917. // }
  918. }
  919. }
  920. //$cmd = '{"cmd_type":"patrol_cmd","cmd_id":'.$where['id'].',"broadcast":0}';
  921. //$cmdret = send_cmd($cmd, 1);
  922. }
  923. // 更新负载设置
  924. public function set_load(){
  925. $username = $this->input->post('username',true);
  926. $client_key = $this->input->post('client_key',true);
  927. $token = $this->input->post('token',true);
  928. $lamp_id = $this->input->post('lamp_id',true);
  929. $clientid = $this->input->post('clientid',true);
  930. $version = $this->session->userdata('version');
  931. $workmode = $this->input->post('workmode',true);
  932. $worktimefirst = $this->input->post('worktimefirst',true);
  933. $workpowerfirst = $this->input->post('workpowerfirst',true);
  934. $worktimesencond = $this->input->post('worktimesencond',true);
  935. $workpowersencond = $this->input->post('workpowersencond',true);
  936. $worktimethird = $this->input->post('worktimethird',true);
  937. $workpowerthird = $this->input->post('workpowerthird',true);
  938. $worktimeforth = $this->input->post('worktimeforth',true);
  939. $workpowerforth = $this->input->post('workpowerforth',true);
  940. $voptically = $this->input->post('voptically',true);
  941. $delaytime = $this->input->post('delaytime',true);
  942. $ledloadcurrent = $this->input->post('ledloadcurrent',true);
  943. $powercmd = $this->input->post('powercmd',true);
  944. $switchfeature = $this->input->post('switchfeature',true);
  945. $lampArr = explode(',', $lamp_id);
  946. $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
  947. $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/set_load_asy';
  948. foreach ($lampList as $l) {
  949. if ($l['protocoltype'] == 0) {
  950. 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],30);
  951. }else{
  952. 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]);
  953. }
  954. }
  955. exit();
  956. }
  957. //更新负载设置
  958. public function set_load_asy(){
  959. $res = $this->load_update();
  960. $clientid = $this->input->post('clientid',true);
  961. $lampid = $this->input->post('lamp_id',true);
  962. $data = $this->Lamp_model->getData(array('id'=>$lampid),'projectid,networkid,address,number');
  963. if ($res['msg'] == '0000') {
  964. $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
  965. $networkData = $this->Network_model->getData(array('id'=>$data['networkid']),'networkname');
  966. $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯负载参数");
  967. $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);
  968. }
  969. if (empty($res['result'])) {
  970. // exit(json_result($res['msg'],$res['msg'],array('number'=>$data['number'])));
  971. $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
  972. exit(send_websocket($clientid,$data));
  973. }else{
  974. // exit(json_result($res['msg'],$this->response[$res['msg']],array('number'=>$data['number'])));
  975. $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$this->response[$res['msg']],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
  976. exit(send_websocket($clientid,$data));
  977. }
  978. }
  979. //更新负载设置
  980. private function load_update(){
  981. $where['relateid'] = $this->input->post('lamp_id',true);
  982. $update['relateid'] = $this->input->post('lamp_id',true);
  983. $update['workmode'] = $this->input->post('workmode',true);
  984. $update['worktimefirst'] = $this->input->post('worktimefirst',true);
  985. $update['workpowerfirst'] = $this->input->post('workpowerfirst',true);
  986. $update['worktimesencond'] = $this->input->post('worktimesencond',true);
  987. $update['workpowersencond'] = $this->input->post('workpowersencond',true);
  988. $update['worktimethird'] = $this->input->post('worktimethird',true);
  989. $update['workpowerthird'] = $this->input->post('workpowerthird',true);
  990. $update['worktimeforth'] = $this->input->post('worktimeforth',true);
  991. $update['workpowerforth'] = $this->input->post('workpowerforth',true);
  992. $update['voptically'] = $this->input->post('voptically',true);
  993. $update['delaytime'] = $this->input->post('delaytime',true);
  994. $update['ledloadcurrent'] = $this->input->post('ledloadcurrent',true);
  995. $update['powercmd'] = $this->input->post('powercmd',true);
  996. $update['switchfeature'] = $this->input->post('switchfeature',true);
  997. $update['updatetime'] = date("Y-m-d H:i:s");
  998. return $this->load_set($where, $update, 0);
  999. }
  1000. private function load_set($where,$update,$broadcast){
  1001. $ret = $this->Load_model->get_data_by_filter(['relateid'=>$update['relateid'],'cmdtype'=>0]);
  1002. if(empty($ret)){
  1003. // $update['id'] = $where['id'];
  1004. $id = $this->Load_model->insert($update);
  1005. } else {
  1006. $this->Load_model->update(['id'=>$ret['id']],$update);
  1007. $id = $ret['id'];
  1008. }
  1009. $cmd = '{"cmd_type":"load_param_cmd","cmd_id":'.$id.',"broadcast":'.$broadcast.'}';
  1010. $lampProData = $this->Lamp_model->getOne($where['relateid'],'N.protocoltype,L.address,L.protocoltype AS lampprotocoltype,N.deviceid');
  1011. if (($lampProData['protocoltype'] == 4 || $lampProData['protocoltype'] == 6) && $lampProData['lampprotocoltype'] == 0) {
  1012. $address = '';
  1013. if (mb_strlen($lampProData['address']) >= 8) {
  1014. $address = mb_substr($lampProData['address'], -8);
  1015. }else{
  1016. $address = '00000000';
  1017. }
  1018. $sendData = '10040010';
  1019. $sendData .= bitSubStr(base_convert($update['workmode'], 10, 16));
  1020. $sendData .= bitSubStr(base_convert($update['worktimefirst'], 10, 16));
  1021. $sendData .= bitSubStr(base_convert($update['workpowerfirst'], 10, 16));
  1022. $sendData .= bitSubStr(base_convert($update['worktimesencond'], 10, 16));
  1023. $sendData .= bitSubStr(base_convert($update['workpowersencond'], 10, 16));
  1024. $sendData .= bitSubStr(base_convert($update['worktimethird'], 10, 16));
  1025. $sendData .= bitSubStr(base_convert($update['workpowerthird'], 10, 16));
  1026. $sendData .= bitSubStr(base_convert($update['worktimeforth'], 10, 16));
  1027. $sendData .= bitSubStr(base_convert($update['workpowerforth'], 10, 16));
  1028. $sendData .= bitSubStr(base_convert($update['delaytime'], 10, 16));
  1029. $sendData .= doubBitSubStr(base_convert(intval($update['voptically']*10), 10, 16));
  1030. $sendData .= doubBitSubStr(base_convert(intval($update['ledloadcurrent']), 10, 16));
  1031. $sendData .= bitSubStr(base_convert($update['powercmd'], 10, 16));
  1032. $sendData .= bitSubStr(base_convert($update['switchfeature'], 10, 16));
  1033. $sum = 0;
  1034. for ($i=0; $i < mb_strlen($sendData)/2; $i++) {
  1035. if ($i == 0) continue;
  1036. $t = mb_substr($sendData, $i*2,2);
  1037. $sum += base_convert($t, 16, 10);
  1038. }
  1039. $sum = base_convert($sum, 10, 16);
  1040. $sendData .= bitSubStr($sum);
  1041. $sendData = $address.$sendData;
  1042. if ($lampProData['protocoltype'] == 4) {
  1043. $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampProData['address'],'/IDCOL/CmdOutput/'.$lampProData['address'],pack('H*','0001'.$sendData));
  1044. }else{
  1045. device_cmd($lampProData['deviceid'],pack('H*',$sendData));
  1046. $cmd = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
  1047. $info = send_lorawan_915($cmd);
  1048. $res = array();
  1049. if (!empty($info)) {
  1050. $info = json_decode($info,true);
  1051. if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
  1052. }
  1053. }
  1054. if (empty($res)) {
  1055. return array('result'=>1,'msg'=>'0016');
  1056. }else{
  1057. $msg1 = $res['msg'];
  1058. // 数据校验
  1059. $msg = mb_substr($msg1, 10);
  1060. $t = 0;
  1061. for ($i=0; $i < mb_strlen($msg) / 2 - 1; $i++) {
  1062. $s = mb_substr($msg, $i*2,2);
  1063. $t += base_convert($s, 16, 10);
  1064. }
  1065. $t = base_convert($t, 10, 16);
  1066. if (mb_strlen($t) > 2) {
  1067. $t = mb_substr($t, -2);
  1068. }elseif($t <= 1){
  1069. $t = '0'.$t;
  1070. }
  1071. if ($t == mb_substr($msg, -2)) {
  1072. if (mb_substr($msg1, -10,-2) == '10040010') {
  1073. return array('result'=>1,'msg'=>'0000');
  1074. }else{
  1075. return array('result'=>1,'msg'=>'0010');
  1076. }
  1077. }else{
  1078. return array('result'=>1,'msg'=>'0010');
  1079. }
  1080. }
  1081. }
  1082. if (isset($lampProData['protocoltype']) && $lampProData['protocoltype'] != 0) {
  1083. $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
  1084. if ($cmdret === false) {
  1085. sleep(1);
  1086. $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
  1087. }
  1088. }else{
  1089. $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
  1090. if ($cmdret === false) {
  1091. sleep(1);
  1092. $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
  1093. }
  1094. }
  1095. $res = array();
  1096. if (empty($cmdret)){
  1097. $res['result'] = 1;
  1098. $res['msg'] = '0016';
  1099. return $res;
  1100. }
  1101. if($cmdret === false){
  1102. $res['result'] = 1;
  1103. $res['msg'] = '0008';
  1104. } else {
  1105. $res = json_decode($cmdret, true);
  1106. if ($res['result'] == false) {
  1107. $res['result'] = 0;
  1108. $version = $this->input->post('version',true);
  1109. if (empty($version)) {
  1110. $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
  1111. }else{
  1112. $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
  1113. }
  1114. } else {
  1115. $res['result'] = 1;
  1116. $res['msg'] = '0000';
  1117. }
  1118. }
  1119. return $res;
  1120. }
  1121. // 新控制器更新负载设置
  1122. public function modbus_set_load(){
  1123. $data['username'] = $this->input->post('username',true);
  1124. $data['client_key'] = $this->input->post('client_key',true);
  1125. $data['token'] = $this->input->post('token',true);
  1126. $data['lamp_id'] = $this->input->post('lamp_id',true);
  1127. $data['clientid'] = $this->input->post('clientid',true);
  1128. $data['version'] = $this->session->userdata('version');
  1129. // $data['light_voltage'] = $this->input->post('light_voltage',true);
  1130. $data['intell_power'] = $this->input->post('intell_power',true);
  1131. $data['load_current'] = $this->input->post('load_current',true);
  1132. $data['light_delay'] = $this->input->post('light_delay',true);
  1133. $data['induction_delay'] = $this->input->post('induction_delay',true);
  1134. $data['first_light_time'] = $this->input->post('first_light_time',true);
  1135. $data['first_light_human_power'] = $this->input->post('first_light_human_power',true);
  1136. $data['first_light_unman_power'] = $this->input->post('first_light_unman_power',true);
  1137. $data['second_light_time'] = $this->input->post('second_light_time',true);
  1138. $data['second_light_human_power'] = $this->input->post('second_light_human_power',true);
  1139. $data['second_light_unman_power'] = $this->input->post('second_light_unman_power',true);
  1140. $data['third_light_time'] = $this->input->post('third_light_time',true);
  1141. $data['third_light_human_power'] = $this->input->post('third_light_human_power',true);
  1142. $data['third_light_unman_power'] = $this->input->post('third_light_unman_power',true);
  1143. $data['fourth_light_time'] = $this->input->post('fourth_light_time',true);
  1144. $data['fourth_light_human_power'] = $this->input->post('fourth_light_human_power',true);
  1145. $data['fourth_light_unman_power'] = $this->input->post('fourth_light_unman_power',true);
  1146. $data['fifth_light_time'] = $this->input->post('fifth_light_time',true);
  1147. $data['fifth_light_human_power'] = $this->input->post('fifth_light_human_power',true);
  1148. $data['fifth_light_unman_power'] = $this->input->post('fifth_light_unman_power',true);
  1149. $data['sixth_light_time'] = $this->input->post('sixth_light_time',true);
  1150. $data['sixth_light_human_power'] = $this->input->post('sixth_light_human_power',true);
  1151. $data['sixth_light_unman_power'] = $this->input->post('sixth_light_unman_power',true);
  1152. $data['seventh_light_time'] = $this->input->post('seventh_light_time',true);
  1153. $data['seventh_light_human_power'] = $this->input->post('seventh_light_human_power',true);
  1154. $data['seventh_light_unman_power'] = $this->input->post('seventh_light_unman_power',true);
  1155. $data['eighth_light_time'] = $this->input->post('eighth_light_time',true);
  1156. $data['eighth_light_human_power'] = $this->input->post('eighth_light_human_power',true);
  1157. $data['eighth_light_unman_power'] = $this->input->post('eighth_light_unman_power',true);
  1158. $data['ninth_light_time'] = $this->input->post('ninth_light_time',true);
  1159. $data['ninth_light_human_power'] = $this->input->post('ninth_light_human_power',true);
  1160. $data['ninth_light_unman_power'] = $this->input->post('ninth_light_unman_power',true);
  1161. $data['tenth_light_time'] = $this->input->post('tenth_light_time',true);
  1162. $data['tenth_light_human_power'] = $this->input->post('tenth_light_human_power',true);
  1163. $data['tenth_light_unman_power'] = $this->input->post('tenth_light_unman_power',true);
  1164. $data['begin_voltage'] = $this->input->post('begin_voltage',true);
  1165. $data['end_voltage'] = $this->input->post('end_voltage',true);
  1166. $data['end_current'] = $this->input->post('end_current',true);
  1167. foreach ($data as $key => $value) {
  1168. $data[$key] = urldecode($value);
  1169. }
  1170. $file = fopen('../upload/test.txt', 'a+');
  1171. fwrite($file, json_encode($data).'------'.date('Y-m-d H:i:s').'
  1172. ');
  1173. fclose($file);
  1174. $lampArr = explode(',', $data['lamp_id']);
  1175. $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
  1176. $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/modbus_set_load_asy';
  1177. foreach ($lampList as $l) {
  1178. $data['lamp_id'] = $l['id'];
  1179. if ($l['protocoltype'] == 0) {
  1180. doAsyncRequest($url,$data,30);
  1181. }else{
  1182. doAsyncRequest($url,$data);
  1183. }
  1184. }
  1185. exit();
  1186. }
  1187. // 新控制器更新负载设置
  1188. public function modbus_set_load_asy(){
  1189. $res = $this->modbus_load_update();
  1190. $clientid = $this->input->post('clientid',true);
  1191. $lampid = $this->input->post('lamp_id',true);
  1192. $data = $this->Lamp_model->getData(array('id'=>$lampid),'projectid,networkid,address,number');
  1193. $file = fopen('../upload/test.txt', 'a+');
  1194. fwrite($file, '66666------'.date('Y-m-d H:i:s').'
  1195. ');
  1196. fclose($file);
  1197. if ($res['msg'] == '0000') {
  1198. $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
  1199. $networkData = $this->Network_model->getData(array('id'=>$data['networkid']),'networkname');
  1200. $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯负载参数");
  1201. $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);
  1202. }
  1203. if (empty($res['result'])) {
  1204. // exit(json_result($res['msg'],$res['msg'],array('number'=>$data['number'])));
  1205. $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
  1206. $file = fopen('../upload/test.txt', 'a+');
  1207. fwrite($file, json_encode($data).'---'.$clientid.'---'.date('Y-m-d H:i:s').'
  1208. ');
  1209. fclose($file);
  1210. exit(send_websocket($clientid,$data));
  1211. }else{
  1212. // exit(json_result($res['msg'],$this->response[$res['msg']],array('number'=>$data['number'])));
  1213. $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$this->response[$res['msg']],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
  1214. $file = fopen('../upload/test.txt', 'a+');
  1215. fwrite($file, json_encode($data).'---'.$clientid.'---'.date('Y-m-d H:i:s').'
  1216. ');
  1217. fclose($file);
  1218. exit(send_websocket($clientid,$data));
  1219. }
  1220. }
  1221. private function format_date($date){
  1222. return intval(explode(':', $date)[0]) * 3600 + intval(explode(':', $date)[1]) * 60;
  1223. }
  1224. // 新控制器更新负载设置
  1225. private function modbus_load_update(){
  1226. $where['relateid'] = $this->input->post('lamp_id',true);
  1227. $data['relateid'] = $this->input->post('lamp_id',true);
  1228. // $data['light_voltage'] = $this->input->post('light_voltage',true);
  1229. $data['intell_power'] = $this->input->post('intell_power',true);
  1230. $data['load_current'] = $this->input->post('load_current',true);
  1231. $data['light_delay'] = $this->input->post('light_delay',true);
  1232. $data['induction_delay'] = $this->input->post('induction_delay',true);
  1233. $data['first_light_time'] = $this->input->post('first_light_time',true);
  1234. // $data['first_light_time'] = $data['first_light_time'] * 3600;
  1235. $data['first_light_time'] = $this->format_date($data['first_light_time']);
  1236. $data['first_light_human_power'] = $this->input->post('first_light_human_power',true);
  1237. $data['first_light_unman_power'] = $this->input->post('first_light_unman_power',true);
  1238. $data['second_light_time'] = $this->input->post('second_light_time',true);
  1239. // $data['second_light_time'] = $data['second_light_time'] * 3600;
  1240. $data['second_light_time'] = $this->format_date($data['second_light_time']);
  1241. $data['second_light_human_power'] = $this->input->post('second_light_human_power',true);
  1242. $data['second_light_unman_power'] = $this->input->post('second_light_unman_power',true);
  1243. $data['third_light_time'] = $this->input->post('third_light_time',true);
  1244. // $data['third_light_time'] = $data['third_light_time'] * 3600;
  1245. $data['third_light_time'] = $this->format_date($data['third_light_time']);
  1246. $data['third_light_human_power'] = $this->input->post('third_light_human_power',true);
  1247. $data['third_light_unman_power'] = $this->input->post('third_light_unman_power',true);
  1248. $data['fourth_light_time'] = $this->input->post('fourth_light_time',true);
  1249. // $data['fourth_light_time'] = $data['fourth_light_time'] * 3600;
  1250. $data['fourth_light_time'] = $this->format_date($data['fourth_light_time']);
  1251. $data['fourth_light_human_power'] = $this->input->post('fourth_light_human_power',true);
  1252. $data['fourth_light_unman_power'] = $this->input->post('fourth_light_unman_power',true);
  1253. $data['fifth_light_time'] = $this->input->post('fifth_light_time',true);
  1254. // $data['fifth_light_time'] = $data['fifth_light_time'] * 3600;
  1255. $data['fifth_light_time'] = $this->format_date($data['fifth_light_time']);
  1256. $data['fifth_light_human_power'] = $this->input->post('fifth_light_human_power',true);
  1257. $data['fifth_light_unman_power'] = $this->input->post('fifth_light_unman_power',true);
  1258. $data['sixth_light_time'] = $this->input->post('sixth_light_time',true);
  1259. // $data['sixth_light_time'] = $data['sixth_light_time'] * 3600;
  1260. $data['sixth_light_time'] = $this->format_date($data['sixth_light_time']);
  1261. $data['sixth_light_human_power'] = $this->input->post('sixth_light_human_power',true);
  1262. $data['sixth_light_unman_power'] = $this->input->post('sixth_light_unman_power',true);
  1263. $data['seventh_light_time'] = $this->input->post('seventh_light_time',true);
  1264. // $data['seventh_light_time'] = $data['seventh_light_time'] * 3600;
  1265. $data['seventh_light_time'] = $this->format_date($data['seventh_light_time']);
  1266. $data['seventh_light_human_power'] = $this->input->post('seventh_light_human_power',true);
  1267. $data['seventh_light_unman_power'] = $this->input->post('seventh_light_unman_power',true);
  1268. $data['eighth_light_time'] = $this->input->post('eighth_light_time',true);
  1269. // $data['eighth_light_time'] = $data['eighth_light_time'] * 3600;
  1270. $data['eighth_light_time'] = $this->format_date($data['eighth_light_time']);
  1271. $data['eighth_light_human_power'] = $this->input->post('eighth_light_human_power',true);
  1272. $data['eighth_light_unman_power'] = $this->input->post('eighth_light_unman_power',true);
  1273. $data['ninth_light_time'] = $this->input->post('ninth_light_time',true);
  1274. // $data['ninth_light_time'] = $data['ninth_light_time'] * 3600;
  1275. $data['ninth_light_time'] = $this->format_date($data['ninth_light_time']);
  1276. $data['ninth_light_human_power'] = $this->input->post('ninth_light_human_power',true);
  1277. $data['ninth_light_unman_power'] = $this->input->post('ninth_light_unman_power',true);
  1278. $data['tenth_light_time'] = $this->input->post('tenth_light_time',true);
  1279. // $data['tenth_light_time'] = $data['tenth_light_time'] * 3600;
  1280. $data['tenth_light_time'] = $this->format_date($data['tenth_light_time']);
  1281. $data['tenth_light_human_power'] = $this->input->post('tenth_light_human_power',true);
  1282. $data['tenth_light_unman_power'] = $this->input->post('tenth_light_unman_power',true);
  1283. $data['begin_voltage'] = $this->input->post('begin_voltage',true);
  1284. $data['end_voltage'] = $this->input->post('end_voltage',true);
  1285. $data['end_current'] = $this->input->post('end_current',true);
  1286. $data['updatetime'] = date("Y-m-d H:i:s");
  1287. $file = fopen('../upload/test.txt', 'a+');
  1288. fwrite($file, json_encode($data).'------'.date('Y-m-d H:i:s').'
  1289. ');
  1290. fclose($file);
  1291. return $this->modbus_load_set($where, $data, 0);
  1292. }
  1293. private function modbus_load_set($where,$update,$broadcast){
  1294. $file = fopen('../upload/test.txt', 'a+');
  1295. fwrite($file, '11111------'.date('Y-m-d H:i:s').'
  1296. ');
  1297. fclose($file);
  1298. $ret = $this->Modbus_load_model->get_data_by_filter(['relateid'=>$update['relateid'],'cmdtype'=>0]);
  1299. if(empty($ret)){
  1300. // $update['id'] = $where['id'];
  1301. $id = $this->Modbus_load_model->insert($update);
  1302. } else {
  1303. $this->Modbus_load_model->update(['id'=>$ret['id']],$update);
  1304. $id = $ret['id'];
  1305. }
  1306. $file = fopen('../upload/test.txt', 'a+');
  1307. fwrite($file, '22222------'.date('Y-m-d H:i:s').'
  1308. ');
  1309. fclose($file);
  1310. $cmd = '{"cmd_type":"modbus_load_param_cmd","cmd_id":'.$id.',"broadcast":'.$broadcast.'}';
  1311. $lampProData = $this->Lamp_model->getOne($where['relateid'],'N.protocoltype,N.deviceid,L.protocoltype as lampprotocoltype,L.address');
  1312. if ($lampProData['protocoltype'] == 4 && $lampProData['lampprotocoltype'] == 1) {
  1313. $sendData = '0103e08d0027a23b';
  1314. $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampProData['address'],'/IDCOL/CmdOutput/'.$lampProData['address'],pack('H*','0001'.$sendData));
  1315. if (empty($res)) {
  1316. return array('result'=>1,'msg'=>'0016');
  1317. }else{
  1318. if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
  1319. $old = $res['msg'];
  1320. $data = array();
  1321. $sendData = '';
  1322. $sendData .= doubBitSubStr(base_convert(intval($update['load_current']*100), 10, 16)).''; // 负载电流
  1323. $sendData .= doubBitSubStr(base_convert(intval($update['intell_power']), 10, 16)).''; // 智能功率
  1324. $sendData .= doubBitSubStr(base_convert(intval($update['light_delay']), 10,16)).''; // 光控延时
  1325. $sendData .= doubBitSubStr(base_convert(intval($update['induction_delay']), 10,16)).''; // 感应延时
  1326. $sendData .= substr($old, 22,4);
  1327. $sendData .= doubBitSubStr(base_convert(intval($update['first_light_time']), 10,16)).''; // 第一段亮灯时间
  1328. $sendData .= doubBitSubStr(base_convert(intval($update['first_light_human_power']), 10,16)).''; // 第一段有人功率
  1329. $sendData .= doubBitSubStr(base_convert(intval($update['first_light_unman_power']), 10,16)).''; // 第一段无人功率
  1330. $sendData .= doubBitSubStr(base_convert(intval($update['second_light_time']), 10,16)).''; // 第二段亮灯时间
  1331. $sendData .= doubBitSubStr(base_convert(intval($update['second_light_human_power']), 10,16)).''; // 第二段有人功率
  1332. $sendData .= doubBitSubStr(base_convert(intval($update['second_light_unman_power']), 10,16)).''; // 第二段无人功率
  1333. $sendData .= doubBitSubStr(base_convert(intval($update['third_light_time']), 10,16)).''; // 第三段亮灯时间
  1334. $sendData .= doubBitSubStr(base_convert(intval($update['third_light_human_power']), 10,16)).''; // 第三段有人功率
  1335. $sendData .= doubBitSubStr(base_convert(intval($update['third_light_unman_power']), 10,16)).''; // 第三段无人功率
  1336. $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_time']), 10,16)).''; // 第四段亮灯时间
  1337. $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_human_power']), 10,16)).''; // 第四段有人功率
  1338. $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_unman_power']), 10,16)).''; // 第四段无人功率
  1339. $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_time']), 10,16)).''; // 第五段亮灯时间
  1340. $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_human_power']), 10,16)).''; // 第五段有人功率
  1341. $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_unman_power']), 10,16)).''; // 第五段无人功率
  1342. $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_time']), 10,16)).''; // 第六段亮灯时间
  1343. $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_human_power']), 10,16)).''; // 第六段有人功率
  1344. $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_unman_power']), 10,16)).''; // 第六段无人功率
  1345. $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_time']), 10,16)).''; // 第七段亮灯时间
  1346. $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_human_power']), 10,16)).''; // 第七段有人功率
  1347. $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_unman_power']), 10,16)).''; // 第七段无人功率
  1348. $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_time']), 10,16)).''; // 第八段亮灯时间
  1349. $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_human_power']), 10,16)).''; // 第八段有人功率
  1350. $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_unman_power']), 10,16)).''; // 第八段无人功率
  1351. $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_time']), 10,16)).''; // 第九段亮灯时间
  1352. $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_human_power']), 10,16)).''; // 第九段有人功率
  1353. $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_unman_power']), 10,16)).''; // 第九段无人功率
  1354. $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_time']), 10,16)).''; // 晨亮时间
  1355. $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_human_power']), 10,16)).''; // 晨亮有人功率
  1356. $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_unman_power']), 10,16)).''; // 晨亮无人功率
  1357. $sendData .= doubBitSubStr(base_convert(intval($update['begin_voltage']*10), 10, 16)).''; // 智能功率USER1起始电压
  1358. $sendData .= doubBitSubStr(base_convert(intval($update['end_voltage']*10), 10, 16)).''; // 智能功率USER1终止电压
  1359. $sendData .= substr($old, 154,4);
  1360. $sendData .= doubBitSubStr(base_convert(intval($update['end_current']*100), 10, 16)).''; // 智能功率USER1终止电流
  1361. $len = doubBitSubStr(base_convert(intval(mb_strlen($sendData)/4), 10, 16)).bitSubStr(base_convert(intval(mb_strlen($sendData)/2), 10, 16));
  1362. $sendData = '0110e08d'.$len.$sendData;
  1363. $sendData .= crc16(pack('H*',$sendData));
  1364. $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampProData['address'],'/IDCOL/CmdOutput/'.$lampProData['address'],pack('H*','0001'.$sendData));
  1365. if (empty($res)) {
  1366. return array('result'=>1,'msg'=>'0016');
  1367. }else{
  1368. if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
  1369. return array('result'=>1,'msg'=>'0000');
  1370. }else{
  1371. return array('result'=>1,'msg'=>'0010');
  1372. }
  1373. }
  1374. }else{
  1375. return array('result'=>1,'msg'=>'0010');
  1376. }
  1377. }
  1378. }elseif ($lampProData['protocoltype'] == 6 && $lampProData['lampprotocoltype'] == 1) {
  1379. $cmdLine='0103e08d0027a23b';
  1380. //$cmdLine .= crc16(pack('H*',$cmdLine));
  1381. device_cmd($lampProData['deviceid'],pack('H*',$cmdLine));
  1382. $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
  1383. $res=send_lorawan_915($cmdLine);
  1384. if (empty($res)) {
  1385. return array('result'=>1,'msg'=>'0016');
  1386. }else {
  1387. $res=json_decode($res,true);
  1388. $cmdRes=$res['resInfo'];
  1389. if(strtolower(substr($cmdRes, 0,6)) == '01034e'){
  1390. $old = $cmdRes;
  1391. $data = array();
  1392. $sendData = '';
  1393. $sendData .= doubBitSubStr(base_convert(intval($update['load_current']*100), 10, 16)).''; // 负载电流
  1394. $sendData .= doubBitSubStr(base_convert(intval($update['intell_power']), 10, 16)).''; // 智能功率
  1395. $sendData .= doubBitSubStr(base_convert(intval($update['light_delay']), 10,16)).''; // 光控延时
  1396. $sendData .= doubBitSubStr(base_convert(intval($update['induction_delay']), 10,16)).''; // 感应延时
  1397. $sendData .= substr($old, 22,4);
  1398. $sendData .= doubBitSubStr(base_convert(intval($update['first_light_time']), 10,16)).''; // 第一段亮灯时间
  1399. $sendData .= doubBitSubStr(base_convert(intval($update['first_light_human_power']), 10,16)).''; // 第一段有人功率
  1400. $sendData .= doubBitSubStr(base_convert(intval($update['first_light_unman_power']), 10,16)).''; // 第一段无人功率
  1401. $sendData .= doubBitSubStr(base_convert(intval($update['second_light_time']), 10,16)).''; // 第二段亮灯时间
  1402. $sendData .= doubBitSubStr(base_convert(intval($update['second_light_human_power']), 10,16)).''; // 第二段有人功率
  1403. $sendData .= doubBitSubStr(base_convert(intval($update['second_light_unman_power']), 10,16)).''; // 第二段无人功率
  1404. $sendData .= doubBitSubStr(base_convert(intval($update['third_light_time']), 10,16)).''; // 第三段亮灯时间
  1405. $sendData .= doubBitSubStr(base_convert(intval($update['third_light_human_power']), 10,16)).''; // 第三段有人功率
  1406. $sendData .= doubBitSubStr(base_convert(intval($update['third_light_unman_power']), 10,16)).''; // 第三段无人功率
  1407. $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_time']), 10,16)).''; // 第四段亮灯时间
  1408. $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_human_power']), 10,16)).''; // 第四段有人功率
  1409. $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_unman_power']), 10,16)).''; // 第四段无人功率
  1410. $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_time']), 10,16)).''; // 第五段亮灯时间
  1411. $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_human_power']), 10,16)).''; // 第五段有人功率
  1412. $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_unman_power']), 10,16)).''; // 第五段无人功率
  1413. $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_time']), 10,16)).''; // 第六段亮灯时间
  1414. $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_human_power']), 10,16)).''; // 第六段有人功率
  1415. $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_unman_power']), 10,16)).''; // 第六段无人功率
  1416. $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_time']), 10,16)).''; // 第七段亮灯时间
  1417. $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_human_power']), 10,16)).''; // 第七段有人功率
  1418. $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_unman_power']), 10,16)).''; // 第七段无人功率
  1419. $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_time']), 10,16)).''; // 第八段亮灯时间
  1420. $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_human_power']), 10,16)).''; // 第八段有人功率
  1421. $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_unman_power']), 10,16)).''; // 第八段无人功率
  1422. $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_time']), 10,16)).''; // 第九段亮灯时间
  1423. $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_human_power']), 10,16)).''; // 第九段有人功率
  1424. $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_unman_power']), 10,16)).''; // 第九段无人功率
  1425. $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_time']), 10,16)).''; // 晨亮时间
  1426. $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_human_power']), 10,16)).''; // 晨亮有人功率
  1427. $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_unman_power']), 10,16)).''; // 晨亮无人功率
  1428. $sendData .= doubBitSubStr(base_convert(intval($update['begin_voltage']*10), 10, 16)).''; // 智能功率USER1起始电压
  1429. $sendData .= doubBitSubStr(base_convert(intval($update['end_voltage']*10), 10, 16)).''; // 智能功率USER1终止电压
  1430. $sendData .= substr($old, 154,4);
  1431. $sendData .= doubBitSubStr(base_convert(intval($update['end_current']*100), 10, 16)).''; // 智能功率USER1终止电流
  1432. $len = strlen($sendData)/4;
  1433. $con1 = substr($sendData,0,20*4);
  1434. $con2 = substr($sendData,20*4);
  1435. $address1='e08d';
  1436. $address2=substr('0000'.base_convert(base_convert($address1,16,10)+20,10,16),-4);
  1437. $l1=substr('0000'.base_convert(20,10,16),-4);
  1438. $l2=substr('00'.base_convert(40,10,16),-2);
  1439. $cmd1='0110'.$address1.$l1.$l2.$con1;
  1440. $cmd1 .=crc16(pack('H*',$cmd1));
  1441. device_cmd($lampProData['deviceid'],pack('H*',$cmd1));
  1442. $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
  1443. $res=send_lorawan_915($cmdLine);
  1444. $file = fopen('./file/loraWan_loader.txt', 'a+');
  1445. fwrite($file, date('Y-m-d H:i:s').'----'.$res.'
  1446. ');
  1447. fwrite($file, date('Y-m-d H:i:s').'----'.$len.'
  1448. ');
  1449. fwrite($file, date('Y-m-d H:i:s').'----'.$sendData.'
  1450. ');
  1451. fwrite($file, date('Y-m-d H:i:s').'----'.$cmd1.'
  1452. ');
  1453. fclose($file);
  1454. if (empty($res)) {
  1455. return array('result'=>1,'msg'=>'0016');
  1456. }else{
  1457. $res = json_decode($res,true);
  1458. if (!isset($res['resInfo']) || strtolower($res['resInfo']) != '0110e08d001467ed') return array('result'=>1,'msg'=>'0016');
  1459. $l1=substr('0000'.base_convert(19,10,16),-4);
  1460. $l2=substr('00'.base_convert(38,10,16),-2);
  1461. $cmd2='0110'.$address2.$l1.$l2.$con2;
  1462. $cmd2 .=crc16(pack('H*',$cmd2));
  1463. device_cmd($lampProData['deviceid'],pack('H*',$cmd2));
  1464. $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
  1465. $res=send_lorawan_915($cmdLine);
  1466. $file = fopen('./file/loraWan_loader.txt', 'a+');
  1467. fwrite($file, date('Y-m-d H:i:s').'----'.$res.'
  1468. ');
  1469. fclose($file);
  1470. if (empty($res)) {
  1471. return array('result'=>1,'msg'=>'0016');
  1472. }else{
  1473. $res = json_decode($res,true);
  1474. if (!isset($res['resInfo']) || strtolower($res['resInfo']) != '0110e0a10013e7e6') return array('result'=>1,'msg'=>'0016');
  1475. return array('result'=>1,'msg'=>'0000');
  1476. }
  1477. }
  1478. // $cmdLine='0110e08d00274e'.$sendData;
  1479. // $cmdLine .= crc16(pack('H*',$cmdLine));
  1480. // device_cmd($lampProData['deviceid'],pack('H*',$cmdLine));
  1481. // $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
  1482. // $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
  1483. // $res=send_lorawan_915($cmdLine);
  1484. // $file = fopen('./file/loraWan_loader.txt', 'a+');
  1485. // fwrite($file, date('Y-m-d H:i:s').'----'.$res.' ');
  1486. // fclose($file);
  1487. // if (empty($res)) {
  1488. // return array('result'=>1,'msg'=>'0016');
  1489. // }else {
  1490. // $file = fopen('./file/loraWan_loader.txt', 'a+');
  1491. // fwrite($file, date('Y-m-d H:i:s').'----'.$res.' ');
  1492. // fclose($file);
  1493. // $res=json_decode($res,true);
  1494. // $cmdRes2=$res['resInfo'];
  1495. // if(strtolower(substr($cmdRes2,0,6))=='01034e') {
  1496. // return array('result'=>1,'msg'=>'0000');
  1497. // }else{
  1498. // return array('result'=>1,'msg'=>'0010');
  1499. // }
  1500. // }
  1501. }else{
  1502. return array('result'=>1,'msg'=>'0010');
  1503. }
  1504. }
  1505. }
  1506. $file = fopen('../upload/test.txt', 'a+');
  1507. fwrite($file, '333333------'.date('Y-m-d H:i:s').'
  1508. ');
  1509. fclose($file);
  1510. if (isset($lampProData['protocoltype']) && $lampProData['protocoltype'] != 0) {
  1511. $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
  1512. if ($cmdret === false) {
  1513. sleep(1);
  1514. $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
  1515. }
  1516. }else{
  1517. $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
  1518. if ($cmdret === false) {
  1519. sleep(1);
  1520. $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
  1521. }
  1522. }
  1523. $file = fopen('../upload/test.txt', 'a+');
  1524. fwrite($file, '44444------'.date('Y-m-d H:i:s').'
  1525. ');
  1526. fclose($file);
  1527. $res = array();
  1528. if (empty($cmdret)){
  1529. $res['result'] = 1;
  1530. $res['msg'] = '0016';
  1531. return $res;
  1532. }
  1533. if($cmdret === false){
  1534. $res['result'] = 1;
  1535. $res['msg'] = '0008';
  1536. } else {
  1537. $res = json_decode($cmdret, true);
  1538. if ($res['result'] == false) {
  1539. $res['result'] = 0;
  1540. $version = $this->input->post('version',true);
  1541. if (empty($version)) {
  1542. $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
  1543. }else{
  1544. $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
  1545. }
  1546. } else {
  1547. $res['result'] = 1;
  1548. $res['msg'] = '0000';
  1549. }
  1550. }
  1551. $file = fopen('../upload/test.txt', 'a+');
  1552. fwrite($file, '55555------'.date('Y-m-d H:i:s').'
  1553. ');
  1554. fclose($file);
  1555. return $res;
  1556. }
  1557. // 更新蓄电池设置
  1558. public function set_battery(){
  1559. $username = $this->input->post('username',true);
  1560. $client_key = $this->input->post('client_key',true);
  1561. $token = $this->input->post('token',true);
  1562. $lamp_id = $this->input->post('lamp_id',true);
  1563. $clientid = $this->input->post('clientid',true);
  1564. $version = $this->session->userdata('version');
  1565. $batterytype = $this->input->post('batterytype',true);//蓄电池类型
  1566. $capacity = $this->input->post('capacity',true);//蓄电池容量
  1567. $vpromotecharge = $this->input->post('vpromotecharge',true);//提升充电电压/过充电压(锂电池)
  1568. $voverdischargerecover = $this->input->post('voverdischargerecover',true);//过放恢复电压
  1569. $vpromoterecover = $this->input->post('vpromoterecover',true);//提升充电恢复电压/过充返回电压(锂电池)
  1570. $voverdischarge = $this->input->post('voverdischarge',true);//过放电压
  1571. if ($batterytype == 1) {
  1572. $vsystem = $this->input->post('vsystem',true);//系统电压
  1573. $minusnocharge = $this->input->post('minusnocharge',true);//零下禁充
  1574. $chargemode = $this->input->post('chargemode',true);//充电模式
  1575. } else {
  1576. $vovervoltage = $this->input->post('vovervoltage',true);
  1577. $vlimitedcharge = $this->input->post('vlimitedcharge',true);
  1578. $vbalancecharge = $this->input->post('vbalancecharge',true);
  1579. $vfloatingcharge = $this->input->post('vfloatingcharge',true);
  1580. $vundervoltagewarn = $this->input->post('vundervoltagewarn',true);
  1581. $balancechargetime = $this->input->post('balancechargetime',true);
  1582. $promotechargetime = $this->input->post('promotechargetime',true);
  1583. $balanceinterval = $this->input->post('balanceinterval',true);
  1584. $tempcompensation = $this->input->post('tempcompensation',true);
  1585. $tempcompmax = $this->input->post('tempcompmax',true);
  1586. $tempcompmin = $this->input->post('tempcompmin',true);
  1587. $turnfloatingcurrent = $this->input->post('turnfloatingcurrent',true);
  1588. }
  1589. $lampArr = explode(',', $lamp_id);
  1590. $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
  1591. $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/set_battery_asy';
  1592. foreach ($lampList as $l) {
  1593. if ($l['protocoltype'] == 0) {
  1594. if ($batterytype == 1) {
  1595. doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'lamp_id'=>$l['id'],'batterytype'=>$batterytype,'capacity'=>$capacity,'vpromotecharge'=>$vpromotecharge,'voverdischargerecover'=>$voverdischargerecover,'vpromoterecover'=>$vpromoterecover,'voverdischarge'=>$voverdischarge,'vsystem'=>$vsystem,'minusnocharge'=>$minusnocharge,'chargemode'=>$chargemode],30);
  1596. }else{
  1597. doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'lamp_id'=>$l['id'],'batterytype'=>$batterytype,'capacity'=>$capacity,'vpromotecharge'=>$vpromotecharge,'voverdischargerecover'=>$voverdischargerecover,'vpromoterecover'=>$vpromoterecover,'voverdischarge'=>$voverdischarge,'vovervoltage'=>$vovervoltage,'vlimitedcharge'=>$vlimitedcharge,'vbalancecharge'=>$vbalancecharge,'vfloatingcharge'=>$vfloatingcharge,'vundervoltagewarn'=>$vundervoltagewarn,'balancechargetime'=>$balancechargetime,'promotechargetime'=>$promotechargetime,'balanceinterval'=>$balanceinterval,'tempcompensation'=>$tempcompensation,'tempcompmax'=>$tempcompmax,'tempcompmin'=>$tempcompmin,'turnfloatingcurrent'=>$turnfloatingcurrent],30);
  1598. }
  1599. }else{
  1600. if ($batterytype == 1) {
  1601. doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'lamp_id'=>$l['id'],'batterytype'=>$batterytype,'capacity'=>$capacity,'vpromotecharge'=>$vpromotecharge,'voverdischargerecover'=>$voverdischargerecover,'vpromoterecover'=>$vpromoterecover,'voverdischarge'=>$voverdischarge,'vsystem'=>$vsystem,'minusnocharge'=>$minusnocharge,'chargemode'=>$chargemode]);
  1602. }else{
  1603. doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'lamp_id'=>$l['id'],'batterytype'=>$batterytype,'capacity'=>$capacity,'vpromotecharge'=>$vpromotecharge,'voverdischargerecover'=>$voverdischargerecover,'vpromoterecover'=>$vpromoterecover,'voverdischarge'=>$voverdischarge,'vovervoltage'=>$vovervoltage,'vlimitedcharge'=>$vlimitedcharge,'vbalancecharge'=>$vbalancecharge,'vfloatingcharge'=>$vfloatingcharge,'vundervoltagewarn'=>$vundervoltagewarn,'balancechargetime'=>$balancechargetime,'promotechargetime'=>$promotechargetime,'balanceinterval'=>$balanceinterval,'tempcompensation'=>$tempcompensation,'tempcompmax'=>$tempcompmax,'tempcompmin'=>$tempcompmin,'turnfloatingcurrent'=>$turnfloatingcurrent]);
  1604. }
  1605. }
  1606. }
  1607. exit();
  1608. }
  1609. public function set_battery_asy(){
  1610. $res = $this->battery_update();
  1611. $clientid = $this->input->post('clientid',true);
  1612. $lampid = $this->input->post('lamp_id',true);
  1613. $data = $this->Lamp_model->getData(array('id'=>$lampid),'projectid,networkid,address,number');
  1614. if ($res['msg'] == '0000') {
  1615. $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
  1616. $networkData = $this->Network_model->getData(array('id'=>$data['networkid']),'networkname');
  1617. $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯蓄电池参数");
  1618. $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);
  1619. }
  1620. if (empty($res['result'])) {
  1621. // exit(json_result($res['msg'],$res['msg'],array('number'=>$data['number'])));
  1622. $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
  1623. exit(send_websocket($clientid,$data));
  1624. }else{
  1625. // exit(json_result($res['msg'],$this->response[$res['msg']],array('number'=>$data['number'])));
  1626. $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$this->response[$res['msg']],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
  1627. exit(send_websocket($clientid,$data));
  1628. }
  1629. }
  1630. //更新蓄电池设置
  1631. private function battery_update(){
  1632. $where['id'] = $this->input->post('lamp_id',true);//路灯id
  1633. $update['batterytype'] = $this->input->post('batterytype',true);//蓄电池类型
  1634. $update['capacity'] = $this->input->post('capacity',true);//蓄电池容量
  1635. $update['vpromotecharge'] = $this->input->post('vpromotecharge',true);//提升充电电压/过充电压(锂电池)
  1636. $update['voverdischargerecover'] = $this->input->post('voverdischargerecover',true);//过放恢复电压
  1637. $update['vpromoterecover'] = $this->input->post('vpromoterecover',true);//提升充电恢复电压/过充返回电压(锂电池)
  1638. $update['voverdischarge'] = $this->input->post('voverdischarge',true);//过放电压
  1639. $update['updatetime'] = date("Y-m-d H:i:s");//更新时间
  1640. if ($update['batterytype'] == 1) {
  1641. $update['vovervoltage'] = $update['vpromotecharge']+2;//超压电压
  1642. $update['vlimitedcharge'] = $update['vpromotecharge']+1;//充电限制电压
  1643. $update['vbalancecharge'] = $update['vpromotecharge'];//均衡充电电压
  1644. $update['vfloatingcharge'] = $update['vpromoterecover'];//浮充充电电压
  1645. $update['vundervoltagewarn'] = 12;//欠压警告电压
  1646. $update['balancechargetime'] = 2;//均衡充电时间
  1647. $update['promotechargetime'] = 2;//提升充电时间
  1648. $update['balanceinterval'] = 30;//均衡充电间隔
  1649. $update['tempcompensation'] = 4;//温度补偿系数
  1650. $update['tempcompmax'] = 60;//温度补偿最高温度
  1651. $update['tempcompmin'] = 0;//温度补偿最低温度
  1652. $update['turnfloatingcurrent'] = 1;//转浮充的电流
  1653. $update['vsystem'] = $this->input->post('vsystem',true);//系统电压
  1654. $update['minusnocharge'] = $this->input->post('minusnocharge',true);//零下禁充
  1655. $update['chargemode'] = $this->input->post('chargemode',true);//充电模式
  1656. } else {
  1657. $update['vovervoltage'] = $this->input->post('vovervoltage',true);
  1658. $update['vlimitedcharge'] = $this->input->post('vlimitedcharge',true);
  1659. $update['vbalancecharge'] = $this->input->post('vbalancecharge',true);
  1660. $update['vfloatingcharge'] = $this->input->post('vfloatingcharge',true);
  1661. $update['vundervoltagewarn'] = $this->input->post('vundervoltagewarn',true);
  1662. $update['balancechargetime'] = $this->input->post('balancechargetime',true);
  1663. $update['promotechargetime'] = $this->input->post('promotechargetime',true);
  1664. $update['balanceinterval'] = $this->input->post('balanceinterval',true);
  1665. $update['tempcompensation'] = $this->input->post('tempcompensation',true);
  1666. $update['tempcompmax'] = $this->input->post('tempcompmax',true);
  1667. $update['tempcompmin'] = $this->input->post('tempcompmin',true);
  1668. $update['turnfloatingcurrent'] = $this->input->post('turnfloatingcurrent',true);
  1669. $update['vsystem'] = 12;
  1670. $update['minusnocharge'] = 0;
  1671. $update['chargemode'] = 0;
  1672. }
  1673. foreach ($update as $v) {
  1674. if ($v === null || $v === '') {
  1675. $lampData = $this->Lamp_model->getOne($where['id'],'number');
  1676. $data = ['code'=>'0007','status'=>2,'msg'=>$this->response['0007'],'data'=>['number'=>$lampData['number'],'type'=>'set_load','id'=>$where['id']]];
  1677. exit(send_websocket($clientid,$data));
  1678. }
  1679. }
  1680. return $this->battery_set($where, $update, 0);
  1681. }
  1682. private function battery_set($where,$update,$broadcast){
  1683. $ret = $this->Battery_model->getOne($where['id']);
  1684. if(empty($ret)){
  1685. $update['id'] = $where['id'];
  1686. $this->Battery_model->insert($update);
  1687. } else {
  1688. $this->Battery_model->update($where,$update);
  1689. }
  1690. $cmd = '{"cmd_type":"battery_cmd","cmd_id":'.$where['id'].',"broadcast":'.$broadcast.'}';
  1691. $lampProData = $this->Lamp_model->getOne($where['id'],'N.protocoltype,L.address,L.protocoltype AS lampprotocoltype,N.deviceid');
  1692. if (($lampProData['protocoltype'] == 4 || $lampProData['protocoltype'] == 6) && $lampProData['lampprotocoltype'] == 0) {
  1693. $address = '';
  1694. if (mb_strlen($lampProData['address']) >= 8) {
  1695. $address = mb_substr($lampProData['address'], -8);
  1696. }else{
  1697. $address = '00000000';
  1698. }
  1699. $sendData = '10032820';
  1700. $sendData .= doubBitSubStr(base_convert($update['capacity'], 10, 16));
  1701. $sendData .= doubBitSubStr(base_convert(intval($update['vovervoltage']*10), 10, 16));
  1702. $sendData .= doubBitSubStr(base_convert(intval($update['vlimitedcharge']*10), 10, 16));
  1703. $sendData .= doubBitSubStr(base_convert(intval($update['vbalancecharge']*10), 10, 16));
  1704. $sendData .= doubBitSubStr(base_convert(intval($update['vpromotecharge']*10), 10, 16));
  1705. $sendData .= doubBitSubStr(base_convert(intval($update['vfloatingcharge']*10), 10, 16));
  1706. $sendData .= doubBitSubStr(base_convert(intval($update['vpromoterecover']*10), 10, 16));
  1707. $sendData .= doubBitSubStr(base_convert(intval($update['voverdischargerecover']*10), 10, 16));
  1708. $sendData .= doubBitSubStr(base_convert(intval($update['vundervoltagewarn']*10), 10, 16));
  1709. $sendData .= doubBitSubStr(base_convert(intval($update['voverdischarge']*10), 10, 16));
  1710. $sendData .= bitSubStr(base_convert($update['balancechargetime'], 10, 16));
  1711. $sendData .= bitSubStr(base_convert($update['promotechargetime'], 10, 16));
  1712. $sendData .= bitSubStr(base_convert($update['balanceinterval'], 10, 16));
  1713. $sendData .= bitSubStr(base_convert($update['tempcompensation'], 10, 16));
  1714. $sendData .= bitEncode($update['tempcompmax']);
  1715. $sendData .= bitEncode($update['tempcompmin']);
  1716. $sendData .= doubBitSubStr(base_convert(intval($update['turnfloatingcurrent']*100), 10, 16));
  1717. $sendData .= bitSubStr(base_convert($update['batterytype'], 10, 16));
  1718. $sendData .= bitSubStr(base_convert($update['vsystem'], 10, 16));
  1719. $sendData .= bitSubStr(base_convert($update['minusnocharge'], 10, 16));
  1720. $sendData .= bitSubStr(base_convert($update['chargemode'], 10, 16));
  1721. $sum = 0;
  1722. for ($i=0; $i < mb_strlen($sendData)/2; $i++) {
  1723. if ($i == 0) continue;
  1724. $t = mb_substr($sendData, $i*2,2);
  1725. $sum += base_convert($t, 16, 10);
  1726. }
  1727. $sum = base_convert($sum, 10, 16);
  1728. $sendData .= bitSubStr($sum);
  1729. $sendData = $address.$sendData;
  1730. if ($lampProData['protocoltype'] == 4) {
  1731. $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampProData['address'],'/IDCOL/CmdOutput/'.$lampProData['address'],pack('H*','0001'.$sendData));
  1732. }else{
  1733. device_cmd($lampProData['deviceid'],pack('H*',$sendData));
  1734. $cmd = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
  1735. $info = send_lorawan_915($cmd);
  1736. $res = array();
  1737. if (!empty($info)) {
  1738. $info = json_decode($info,true);
  1739. if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
  1740. }
  1741. }
  1742. if (empty($res)) {
  1743. return array('result'=>1,'msg'=>'0016');
  1744. }else{
  1745. $msg1 = $res['msg'];
  1746. // 数据校验
  1747. $msg = mb_substr($msg1, 10);
  1748. $t = 0;
  1749. for ($i=0; $i < mb_strlen($msg) / 2 - 1; $i++) {
  1750. $s = mb_substr($msg, $i*2,2);
  1751. $t += base_convert($s, 16, 10);
  1752. }
  1753. $t = base_convert($t, 10, 16);
  1754. if (mb_strlen($t) > 2) {
  1755. $t = mb_substr($t, -2);
  1756. }elseif($t <= 1){
  1757. $t = '0'.$t;
  1758. }
  1759. if ($t == mb_substr($msg, -2)) {
  1760. if (mb_substr($msg1, -10,-2) == '10032820') {
  1761. return array('result'=>1,'msg'=>'0000');
  1762. }else{
  1763. return array('result'=>1,'msg'=>'0010');
  1764. }
  1765. }else{
  1766. return array('result'=>1,'msg'=>'0010');
  1767. }
  1768. }
  1769. }
  1770. if (isset($lampProData['protocoltype']) && $lampProData['protocoltype'] != 0) {
  1771. $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
  1772. if ($cmdret === false) {
  1773. sleep(1);
  1774. $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
  1775. }
  1776. }else{
  1777. $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
  1778. if ($cmdret === false) {
  1779. sleep(1);
  1780. $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
  1781. }
  1782. }
  1783. $res = array();
  1784. if (empty($cmdret)){
  1785. $res['result'] = 1;
  1786. $res['msg'] = '0016';
  1787. return $res;
  1788. }
  1789. if($cmdret === false){
  1790. $res['result'] = 1;
  1791. $res['msg'] = '0008';
  1792. } else {
  1793. $res = json_decode($cmdret, true);
  1794. if ($res['result'] == false) {
  1795. $res['result'] = 0;
  1796. $version = $this->input->post('version',true);
  1797. if (empty($version)) {
  1798. $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
  1799. }else{
  1800. $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
  1801. }
  1802. } else {
  1803. $res['result'] = 1;
  1804. $res['msg'] = '0000';
  1805. }
  1806. }
  1807. return $res;
  1808. }
  1809. // 新控制器更新蓄电池设置
  1810. public function modbus_set_battery(){
  1811. $username = $this->input->post('username',true);
  1812. $client_key = $this->input->post('client_key',true);
  1813. $token = $this->input->post('token',true);
  1814. $lamp_id = $this->input->post('lamp_id',true);
  1815. $clientid = $this->input->post('clientid',true);
  1816. $version = $this->session->userdata('version');
  1817. $light_voltage = $this->input->post('light_voltage',true);
  1818. $battery_type = $this->input->post('battery_type',true);
  1819. $li_battery_type = $this->input->post('li_battery_type',true);
  1820. $system_voltage = $this->input->post('system_voltage',true);
  1821. $increa_char_voltage = $this->input->post('increa_char_voltage',true);
  1822. $float_char_voltage = $this->input->post('float_char_voltage',true);
  1823. $over_dischar_return_voltage = $this->input->post('over_dischar_return_voltage',true);
  1824. $over_dischar_voltage = $this->input->post('over_dischar_voltage',true);
  1825. $char_upper_limit_temper = $this->input->post('char_upper_limit_temper',true);
  1826. $char_lower_limit_temper = $this->input->post('char_lower_limit_temper',true);
  1827. $dischar_upper_limit_temper = $this->input->post('dischar_upper_limit_temper',true);
  1828. $dischar_lower_limit_temper = $this->input->post('dischar_lower_limit_temper',true);
  1829. if ($battery_type == 0) {
  1830. $overvoltage = $this->input->post('overvoltage',true);
  1831. $char_limit_voltage = $this->input->post('char_limit_voltage',true);
  1832. $equ_char_voltage = $this->input->post('equ_char_voltage',true);
  1833. $increa_char_return_voltage = $this->input->post('increa_char_return_voltage',true);
  1834. $equ_char_time = $this->input->post('equ_char_time',true) * 60;
  1835. $increa_char_time = $this->input->post('increa_char_time',true) * 60;
  1836. $balan_char_interval = $this->input->post('balan_char_interval',true);
  1837. $temper_compen_coeff = $this->input->post('temper_compen_coeff',true);
  1838. }
  1839. $lampArr = explode(',', $lamp_id);
  1840. $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
  1841. $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/modbus_set_battery_asy';
  1842. foreach ($lampList as $l) {
  1843. if ($l['protocoltype'] == 0) {
  1844. if ($battery_type == 1) {
  1845. doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'lamp_id'=>$l['id'],'battery_type'=>$battery_type,'system_voltage'=>$system_voltage,'increa_char_voltage'=>$increa_char_voltage,'float_char_voltage'=>$float_char_voltage,'over_dischar_return_voltage'=>$over_dischar_return_voltage,'over_dischar_voltage'=>$over_dischar_voltage,'char_upper_limit_temper'=>$char_upper_limit_temper,'char_lower_limit_temper'=>$char_lower_limit_temper,'dischar_upper_limit_temper'=>$dischar_upper_limit_temper,'dischar_lower_limit_temper'=>$dischar_lower_limit_temper,'light_voltage'=>$light_voltage,'li_battery_type'=>$li_battery_type],30);
  1846. }else{
  1847. doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'lamp_id'=>$l['id'],'battery_type'=>$battery_type,'system_voltage'=>$system_voltage,'increa_char_voltage'=>$increa_char_voltage,'float_char_voltage'=>$float_char_voltage,'over_dischar_return_voltage'=>$over_dischar_return_voltage,'over_dischar_voltage'=>$over_dischar_voltage,'char_upper_limit_temper'=>$char_upper_limit_temper,'char_lower_limit_temper'=>$char_lower_limit_temper,'dischar_upper_limit_temper'=>$dischar_upper_limit_temper,'dischar_lower_limit_temper'=>$dischar_lower_limit_temper,'overvoltage'=>$overvoltage,'char_limit_voltage'=>$char_limit_voltage,'equ_char_voltage'=>$equ_char_voltage,'increa_char_return_voltage'=>$increa_char_return_voltage,'equ_char_time'=>$equ_char_time,'increa_char_time'=>$increa_char_time,'balan_char_interval'=>$balan_char_interval,'temper_compen_coeff'=>$temper_compen_coeff,'light_voltage'=>$light_voltage,'li_battery_type'=>$li_battery_type],30);
  1848. }
  1849. }else{
  1850. if ($battery_type == 1) {
  1851. doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'lamp_id'=>$l['id'],'battery_type'=>$battery_type,'system_voltage'=>$system_voltage,'increa_char_voltage'=>$increa_char_voltage,'float_char_voltage'=>$float_char_voltage,'over_dischar_return_voltage'=>$over_dischar_return_voltage,'over_dischar_voltage'=>$over_dischar_voltage,'char_upper_limit_temper'=>$char_upper_limit_temper,'char_lower_limit_temper'=>$char_lower_limit_temper,'dischar_upper_limit_temper'=>$dischar_upper_limit_temper,'dischar_lower_limit_temper'=>$dischar_lower_limit_temper,'light_voltage'=>$light_voltage,'li_battery_type'=>$li_battery_type]);
  1852. }else{
  1853. doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'lamp_id'=>$l['id'],'battery_type'=>$battery_type,'system_voltage'=>$system_voltage,'increa_char_voltage'=>$increa_char_voltage,'float_char_voltage'=>$float_char_voltage,'over_dischar_return_voltage'=>$over_dischar_return_voltage,'over_dischar_voltage'=>$over_dischar_voltage,'char_upper_limit_temper'=>$char_upper_limit_temper,'char_lower_limit_temper'=>$char_lower_limit_temper,'dischar_upper_limit_temper'=>$dischar_upper_limit_temper,'dischar_lower_limit_temper'=>$dischar_lower_limit_temper,'overvoltage'=>$overvoltage,'char_limit_voltage'=>$char_limit_voltage,'equ_char_voltage'=>$equ_char_voltage,'increa_char_return_voltage'=>$increa_char_return_voltage,'equ_char_time'=>$equ_char_time,'increa_char_time'=>$increa_char_time,'balan_char_interval'=>$balan_char_interval,'temper_compen_coeff'=>$temper_compen_coeff,'light_voltage'=>$light_voltage,'li_battery_type'=>$li_battery_type]);
  1854. }
  1855. }
  1856. }
  1857. exit();
  1858. }
  1859. public function modbus_set_battery_asy(){
  1860. $res = $this->modbus_battery_update();
  1861. $clientid = $this->input->post('clientid',true);
  1862. $lampid = $this->input->post('lamp_id',true);
  1863. $data = $this->Lamp_model->getData(array('id'=>$lampid),'projectid,networkid,address,number');
  1864. if ($res['msg'] == '0000') {
  1865. $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
  1866. $networkData = $this->Network_model->getData(array('id'=>$data['networkid']),'networkname');
  1867. $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯蓄电池参数");
  1868. $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);
  1869. }
  1870. if (empty($res['result'])) {
  1871. // exit(json_result($res['msg'],$res['msg'],array('number'=>$data['number'])));
  1872. $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
  1873. exit(send_websocket($clientid,$data));
  1874. }else{
  1875. // exit(json_result($res['msg'],$this->response[$res['msg']],array('number'=>$data['number'])));
  1876. $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$this->response[$res['msg']],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
  1877. exit(send_websocket($clientid,$data));
  1878. }
  1879. }
  1880. //新控制器更新蓄电池设置
  1881. private function modbus_battery_update(){
  1882. $where['id'] = $this->input->post('lamp_id',true);//路灯id
  1883. $update['light_voltage'] = $this->input->post('light_voltage',true);
  1884. $update['battery_type'] = $this->input->post('battery_type',true);
  1885. $update['li_battery_type'] = intval($this->input->post('li_battery_type',true));
  1886. $update['system_voltage'] = $this->input->post('system_voltage',true);
  1887. $update['increa_char_voltage'] = $this->input->post('increa_char_voltage',true);
  1888. $update['float_char_voltage'] = $this->input->post('float_char_voltage',true);
  1889. $update['over_dischar_return_voltage'] = $this->input->post('over_dischar_return_voltage',true);
  1890. $update['over_dischar_voltage'] = $this->input->post('over_dischar_voltage',true);
  1891. $update['char_upper_limit_temper'] = $this->input->post('char_upper_limit_temper',true);
  1892. $update['char_lower_limit_temper'] = $this->input->post('char_lower_limit_temper',true);
  1893. $update['dischar_upper_limit_temper'] = $this->input->post('dischar_upper_limit_temper',true);
  1894. $update['dischar_lower_limit_temper'] = $this->input->post('dischar_lower_limit_temper',true);
  1895. if ($update['battery_type'] == 0) {
  1896. $update['overvoltage'] = $this->input->post('overvoltage',true);
  1897. $update['char_limit_voltage'] = $this->input->post('char_limit_voltage',true);
  1898. $update['equ_char_voltage'] = $this->input->post('equ_char_voltage',true);
  1899. $update['increa_char_return_voltage'] = $this->input->post('increa_char_return_voltage',true);
  1900. $update['equ_char_time'] = $this->input->post('equ_char_time',true);
  1901. $update['increa_char_time'] = $this->input->post('increa_char_time',true);
  1902. $update['balan_char_interval'] = $this->input->post('balan_char_interval',true);
  1903. $update['temper_compen_coeff'] = $this->input->post('temper_compen_coeff',true);
  1904. }
  1905. foreach ($update as $v) {
  1906. if ($v === null || $v === '') {
  1907. $lampData = $this->Lamp_model->getOne($where['id'],'number');
  1908. $data = ['code'=>'0007','status'=>2,'msg'=>$this->response['0007'],'data'=>['number'=>$lampData['number'],'type'=>'set_load','id'=>$where['id']]];
  1909. exit(send_websocket($clientid,$data));
  1910. }
  1911. }
  1912. return $this->modbus_battery_set($where, $update, 0);
  1913. }
  1914. private function modbus_battery_set($where,$update,$broadcast){
  1915. $ret = $this->Modbus_battery_model->getOne($where['id']);
  1916. if(empty($ret)){
  1917. $update['id'] = $where['id'];
  1918. $this->Modbus_battery_model->insert($update);
  1919. } else {
  1920. $this->Modbus_battery_model->update($where,$update);
  1921. }
  1922. $cmd = '{"cmd_type":"modbus_battery_cmd","cmd_id":'.$where['id'].',"broadcast":'.$broadcast.'}';
  1923. $lampProData = $this->Lamp_model->getOne($where['id'],'N.protocoltype,N.deviceid,L.address,L.protocoltype as lampprotocoltype');
  1924. if ($lampProData['protocoltype'] == 4 && $lampProData['lampprotocoltype'] == 1) {
  1925. $sendData = '0103e0020020d212';
  1926. $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampProData['address'],'/IDCOL/CmdOutput/'.$lampProData['address'],pack('H*','0001'.$sendData));
  1927. if (empty($res)) {
  1928. return array('result'=>1,'msg'=>'0016');
  1929. }else{
  1930. if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
  1931. $old = $res['msg'];
  1932. $data = array();
  1933. $sendData = '';
  1934. $sendData .= substr($old, 6,4);
  1935. if ($update['battery_type'] == 0) { // 铅酸电池
  1936. $sendData .= doubBitSubStr(base_convert(intval($update['system_voltage']), 10, 16)).''; // 系统电压
  1937. $sendData .= doubBitSubStr(base_convert(10, 10, 16)).''; // 电池类型
  1938. $sendData .= doubBitSubStr(base_convert(intval($update['overvoltage']*10), 10, 16)).''; // 超压电压
  1939. $sendData .= doubBitSubStr(base_convert(intval($update['char_limit_voltage']*10), 10, 16)).''; // 充电限制电压
  1940. $sendData .= doubBitSubStr(base_convert(intval($update['equ_char_voltage']*10), 10, 16)).''; // 均衡充电电压
  1941. $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10), 10, 16)).''; // 提升充电电压
  1942. $sendData .= doubBitSubStr(base_convert(intval($update['float_char_voltage']*10), 10, 16)).''; // 浮充充电电压
  1943. $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_return_voltage']*10), 10, 16)).''; // 提升充电返回电压
  1944. $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_return_voltage']*10), 10, 16)).''; // 过放返回电压
  1945. $sendData .= substr($old, 46,4);
  1946. $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10), 10, 16)).''; // 过放电压
  1947. $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10 - 5), 10, 16)).''; // 放电限制电压
  1948. $sendData .= substr($old, 58,8);
  1949. $sendData .= doubBitSubStr(base_convert(intval($update['equ_char_time']), 10, 16)).''; // 均衡充电时间
  1950. $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_time']), 10, 16)).''; // 提升充电时间
  1951. $sendData .= doubBitSubStr(base_convert(intval($update['balan_char_interval']), 10, 16)).''; // 均衡充电间隔
  1952. $sendData .= doubBitSubStr(base_convert(intval($update['temper_compen_coeff']), 10, 16)).''; // 温度补偿系数
  1953. $sendData .= substr($old, 82,2);
  1954. $sendData .= bitEncode(intval($update['char_upper_limit_temper'])).''; // 充电上限温度
  1955. $sendData .= substr($old, 86,2);
  1956. $sendData .= bitEncode(intval($update['char_lower_limit_temper'])).''; // 充电下限温度
  1957. $sendData .= substr($old, 90,2);
  1958. $sendData .= bitEncode(intval($update['dischar_upper_limit_temper'])).''; // 放电上限温度
  1959. $sendData .= substr($old, 94,2);
  1960. $sendData .= bitEncode(intval($update['dischar_lower_limit_temper'])).''; // 放电下限温度
  1961. $sendData .= substr($old, 98,24);
  1962. $sendData .= doubBitSubStr(base_convert(intval($update['light_voltage']), 10, 16)).''; // 光控电压
  1963. $sendData .= substr($old, 126,6);
  1964. // 特殊功能系统电压设置
  1965. $str = substr('00000000'.base_convert(substr($old, 132,6), 16, 2), -8);
  1966. if ($update['system_voltage'] == 12) {
  1967. $str = substr($str, 0,3).'00'.substr($str, -3);
  1968. }else{
  1969. $str = substr($str, 0,3).'01'.substr($str, -3);
  1970. }
  1971. // 特殊功能系统电池类型设置
  1972. $str = '0'.substr($str, -7);
  1973. $sendData .= bitSubStr(base_convert($str, 2, 16));
  1974. }else{ // 锂电池
  1975. $sendData .= doubBitSubStr(base_convert(intval($update['system_voltage']), 10, 16)).''; // 系统电压
  1976. $sendData .= doubBitSubStr(base_convert(11, 10, 16)).''; // 电池类型
  1977. $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10 + 2), 10, 16)).''; // 超压电压
  1978. $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10 + 1), 10, 16)).''; // 充电限制电压
  1979. $sendData .= substr($old, 26,4);
  1980. $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10), 10, 16)).''; // 充电电压
  1981. $sendData .= doubBitSubStr(base_convert(intval($update['float_char_voltage']*10), 10, 16)).''; // 充电返回电压
  1982. $sendData .= substr($old, 38,4);
  1983. $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_return_voltage']*10), 10, 16)).''; // 过放返回电压
  1984. $sendData .= substr($old, 46,4);
  1985. $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10), 10, 16)).''; // 过放电压
  1986. $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10 - 5), 10, 16)).''; // 放电限制电压
  1987. $sendData .= substr($old, 58,24);
  1988. $sendData .= substr($old, 82,2);
  1989. $sendData .= bitEncode(intval($update['char_upper_limit_temper'])).''; // 充电上限温度
  1990. $sendData .= substr($old, 86,2);
  1991. $sendData .= bitEncode(intval($update['char_lower_limit_temper'])).''; // 充电下限温度
  1992. $sendData .= substr($old, 90,2);
  1993. $sendData .= bitEncode(intval($update['dischar_upper_limit_temper'])).''; // 放电上限温度
  1994. $sendData .= substr($old, 94,2);
  1995. $sendData .= bitEncode(intval($update['dischar_lower_limit_temper'])).''; // 放电下限温度
  1996. $sendData .= substr($old, 98,24);
  1997. $sendData .= doubBitSubStr(base_convert(intval($update['light_voltage']), 10, 16)).''; // 光控电压
  1998. $sendData .= substr($old, 126,6);
  1999. // 特殊功能系统电压设置
  2000. $str = substr('00000000'.base_convert(substr($old, 132,6), 16, 2), -8);
  2001. if ($update['system_voltage'] == 12) {
  2002. $str = substr($str, 0,3).'00'.substr($str, -3);
  2003. }elseif ($update['system_voltage'] == 24) {
  2004. $str = substr($str, 0,3).'01'.substr($str, -3);
  2005. }elseif ($update['system_voltage'] == 3) {
  2006. $str = substr($str, 0,3).'10'.substr($str, -3);
  2007. }else{
  2008. $str = substr($str, 0,3).'11'.substr($str, -3);
  2009. }
  2010. // 特殊功能系统电池类型设置
  2011. $str = '1'.substr($str, -7);
  2012. $sendData .= bitSubStr(base_convert($str, 2, 16));
  2013. }
  2014. $len = doubBitSubStr(base_convert(intval(mb_strlen($sendData)/4), 10, 16)).bitSubStr(base_convert(intval(mb_strlen($sendData)/2), 10, 16));
  2015. $sendData = '0110e002'.$len.$sendData;
  2016. $sendData .= crc16(pack('H*',$sendData));
  2017. $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampProData['address'],'/IDCOL/CmdOutput/'.$lampProData['address'],pack('H*','0001'.$sendData));
  2018. if (empty($res)) {
  2019. return array('result'=>1,'msg'=>'0016');
  2020. }else{
  2021. if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
  2022. return array('result'=>1,'msg'=>'0000');
  2023. }else{
  2024. return array('result'=>1,'msg'=>'0010');
  2025. }
  2026. }
  2027. }else{
  2028. return array('result'=>1,'msg'=>'0010');
  2029. }
  2030. }
  2031. }
  2032. elseif ($lampProData['protocoltype']==6 && $lampProData['lampprotocoltype'] == 1){
  2033. $cmdLine='0103e0020020d212';
  2034. device_cmd($lampProData['deviceid'],pack('H*',$cmdLine));
  2035. $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
  2036. $res=send_lorawan_915($cmdLine);
  2037. if (empty($res)) {
  2038. return array('result'=>1,'msg'=>'0016');
  2039. }else{
  2040. $res=json_decode($res,true);
  2041. $cmdRes=$res['resInfo'];
  2042. if(strtolower(substr($cmdRes,0,6))=='010340'){
  2043. $old = $cmdRes;
  2044. $data = array();
  2045. $sendData = '';
  2046. $sendData .= substr($old, 6,4);
  2047. if ($update['battery_type'] == 0) { // 铅酸电池
  2048. $sendData .= doubBitSubStr(base_convert(intval($update['system_voltage']), 10, 16)).''; // 系统电压
  2049. $sendData .= doubBitSubStr(base_convert(10, 10, 16)).''; // 电池类型
  2050. $sendData .= doubBitSubStr(base_convert(intval($update['overvoltage']*10), 10, 16)).''; // 超压电压
  2051. $sendData .= doubBitSubStr(base_convert(intval($update['char_limit_voltage']*10), 10, 16)).''; // 充电限制电压
  2052. $sendData .= doubBitSubStr(base_convert(intval($update['equ_char_voltage']*10), 10, 16)).''; // 均衡充电电压
  2053. $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10), 10, 16)).''; // 提升充电电压
  2054. $sendData .= doubBitSubStr(base_convert(intval($update['float_char_voltage']*10), 10, 16)).''; // 浮充充电电压
  2055. $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_return_voltage']*10), 10, 16)).''; // 提升充电返回电压
  2056. $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_return_voltage']*10), 10, 16)).''; // 过放返回电压
  2057. $sendData .= substr($old, 46,4);
  2058. $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10), 10, 16)).''; // 过放电压
  2059. $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10 - 5), 10, 16)).''; // 放电限制电压
  2060. $sendData .= substr($old, 58,8);
  2061. $sendData .= doubBitSubStr(base_convert(intval($update['equ_char_time']), 10, 16)).''; // 均衡充电时间
  2062. $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_time']), 10, 16)).''; // 提升充电时间
  2063. $sendData .= doubBitSubStr(base_convert(intval($update['balan_char_interval']), 10, 16)).''; // 均衡充电间隔
  2064. $sendData .= doubBitSubStr(base_convert(intval($update['temper_compen_coeff']), 10, 16)).''; // 温度补偿系数
  2065. $sendData .= substr($old, 82,2);
  2066. $sendData .= bitEncode(intval($update['char_upper_limit_temper'])).''; // 充电上限温度
  2067. $sendData .= substr($old, 86,2);
  2068. $sendData .= bitEncode(intval($update['char_lower_limit_temper'])).''; // 充电下限温度
  2069. $sendData .= substr($old, 90,2);
  2070. $sendData .= bitEncode(intval($update['dischar_upper_limit_temper'])).''; // 放电上限温度
  2071. $sendData .= substr($old, 94,2);
  2072. $sendData .= bitEncode(intval($update['dischar_lower_limit_temper'])).''; // 放电下限温度
  2073. $sendData .= substr($old, 98,24);
  2074. $sendData .= doubBitSubStr(base_convert(intval($update['light_voltage']), 10, 16)).''; // 光控电压
  2075. $sendData .= substr($old, 126,6);
  2076. // 特殊功能系统电压设置
  2077. $str = substr('00000000'.base_convert(substr($old, 132,6), 16, 2), -8);
  2078. if ($update['system_voltage'] == 12) {
  2079. $str = substr($str, 0,3).'00'.substr($str, -3);
  2080. }else{
  2081. $str = substr($str, 0,3).'01'.substr($str, -3);
  2082. }
  2083. // 特殊功能系统电池类型设置
  2084. $str = '0'.substr($str, -7);
  2085. $sendData .= bitSubStr(base_convert($str, 2, 16));
  2086. }else{ // 锂电池
  2087. $sendData .= doubBitSubStr(base_convert(intval($update['system_voltage']), 10, 16)).''; // 系统电压
  2088. $sendData .= doubBitSubStr(base_convert(11, 10, 16)).''; // 电池类型
  2089. $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10 + 2), 10, 16)).''; // 超压电压
  2090. $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10 + 1), 10, 16)).''; // 充电限制电压
  2091. $sendData .= substr($old, 26,4);
  2092. $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10), 10, 16)).''; // 充电电压
  2093. $sendData .= doubBitSubStr(base_convert(intval($update['float_char_voltage']*10), 10, 16)).''; // 充电返回电压
  2094. $sendData .= substr($old, 38,4);
  2095. $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_return_voltage']*10), 10, 16)).''; // 过放返回电压
  2096. $sendData .= substr($old, 46,4);
  2097. $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10), 10, 16)).''; // 过放电压
  2098. $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10 - 5), 10, 16)).''; // 放电限制电压
  2099. $sendData .= substr($old, 58,24);
  2100. $sendData .= substr($old, 82,2);
  2101. $sendData .= bitEncode(intval($update['char_upper_limit_temper'])).''; // 充电上限温度
  2102. $sendData .= substr($old, 86,2);
  2103. $sendData .= bitEncode(intval($update['char_lower_limit_temper'])).''; // 充电下限温度
  2104. $sendData .= substr($old, 90,2);
  2105. $sendData .= bitEncode(intval($update['dischar_upper_limit_temper'])).''; // 放电上限温度
  2106. $sendData .= substr($old, 94,2);
  2107. $sendData .= bitEncode(intval($update['dischar_lower_limit_temper'])).''; // 放电下限温度
  2108. $sendData .= substr($old, 98,24);
  2109. $sendData .= doubBitSubStr(base_convert(intval($update['light_voltage']), 10, 16)).''; // 光控电压
  2110. $sendData .= substr($old, 126,6);
  2111. // 特殊功能系统电压设置
  2112. $str = substr('00000000'.base_convert(substr($old, 132,6), 16, 2), -8);
  2113. if ($update['system_voltage'] == 12) {
  2114. $str = substr($str, 0,3).'00'.substr($str, -3);
  2115. }elseif ($update['system_voltage'] == 24) {
  2116. $str = substr($str, 0,3).'01'.substr($str, -3);
  2117. }elseif ($update['system_voltage'] == 3) {
  2118. $str = substr($str, 0,3).'10'.substr($str, -3);
  2119. }else{
  2120. $str = substr($str, 0,3).'11'.substr($str, -3);
  2121. }
  2122. // 特殊功能系统电池类型设置
  2123. $str = '1'.substr($str, -7);
  2124. $sendData .= bitSubStr(base_convert($str, 2, 16));
  2125. }
  2126. // $file = fopen('./file/loraWan_battery.txt', 'a+');
  2127. // fwrite($file, date('Y-m-d H:i:s').'----'.$cmdLine.' ');
  2128. $len = strlen($sendData)/4;
  2129. $con1 = substr($sendData, 0, $len/2*4);
  2130. $con2 = substr($sendData, $len/2*4);
  2131. $address1 = 'e002';
  2132. $address2 = substr('0000'.base_convert(base_convert($address1, 16, 10) + $len/2, 10, 16), -4);
  2133. $l1 = substr('0000'.base_convert($len/2, 10, 16), -4);
  2134. $l2 = substr('00'.base_convert($len/2, 10, 16), -2);
  2135. $cmd1 = '0110'.$address1.$l1.$l2.$con1;
  2136. $cmd1 .= crc16(pack('H*',$cmd1));
  2137. device_cmd($lampProData['deviceid'],pack('H*',$cmd1));
  2138. $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
  2139. $res=send_lorawan_915($cmdLine);
  2140. // $cmd2 = '0110'.$address2.$l1.$l2.$con2;
  2141. // $cmd2 .= crc16(pack('H*',$cmd2));
  2142. // $res = device_cmd($lampProData['deviceid'],pack('H*',$cmd2));
  2143. // device_cmd($lampProData['deviceid'],pack('H*',$cmdLine));
  2144. // fwrite($file, date('Y-m-d H:i:s').'----battery');
  2145. // fclose($file);
  2146. if (empty($res)) {
  2147. return array('result'=>1,'msg'=>'0016');
  2148. }else{
  2149. $cmd2 = '0110'.$address2.$l1.$l2.$con2;
  2150. $cmd2 .= crc16(pack('H*',$cmd2));
  2151. device_cmd($lampProData['deviceid'],pack('H*',$cmd2));
  2152. $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
  2153. $res=send_lorawan_915($cmdLine);
  2154. if (empty($res)) {
  2155. return array('result'=>1,'msg'=>'0016');
  2156. }else{
  2157. return array('result'=>1,'msg'=>'0000');
  2158. }
  2159. // $file = fopen('./file/loraWan_battery.txt', 'a+');
  2160. // fwrite($file, date('Y-m-d H:i:s').'----'.$res.' ');
  2161. // fclose($file);
  2162. // $res=json_decode($res,true);
  2163. // $cmdRes2=$res['resInfo'];
  2164. // if(strtolower(substr($cmdRes2,0,6))=='010340') {
  2165. // return array('result'=>1,'msg'=>'0000');
  2166. // }else{
  2167. // return array('result'=>1,'msg'=>'0010');
  2168. // }
  2169. }
  2170. }else{
  2171. return array('result' => 1, 'msg' => '0010');
  2172. }
  2173. }
  2174. }
  2175. if (isset($lampProData['protocoltype']) && $lampProData['protocoltype'] != 0) {
  2176. $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
  2177. if ($cmdret === false) {
  2178. sleep(1);
  2179. $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
  2180. }
  2181. }else{
  2182. $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
  2183. if ($cmdret === false) {
  2184. sleep(1);
  2185. $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
  2186. }
  2187. }
  2188. $res = array();
  2189. if (empty($cmdret)){
  2190. $res['result'] = 1;
  2191. $res['msg'] = '0016';
  2192. return $res;
  2193. }
  2194. if($cmdret === false){
  2195. $res['result'] = 1;
  2196. $res['msg'] = '0008';
  2197. } else {
  2198. $res = json_decode($cmdret, true);
  2199. if ($res['result'] == false) {
  2200. $res['result'] = 0;
  2201. $version = $this->input->post('version',true);
  2202. if (empty($version)) {
  2203. $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
  2204. }else{
  2205. $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
  2206. }
  2207. } else {
  2208. $res['result'] = 1;
  2209. $res['msg'] = '0000';
  2210. }
  2211. }
  2212. return $res;
  2213. }
  2214. // 添加编辑灯控
  2215. public function save() {
  2216. $role = $this->get_user_info('role');
  2217. $where['id'] = $this->input->post('lamp_id',true);
  2218. $data['projectid'] = $this->input->post('project_id',true);
  2219. $data['number'] = $this->input->post('lamp_no',true);
  2220. $data['section'] = $this->input->post('section',true);
  2221. $data['address'] = $this->input->post('address',true);
  2222. $data['longitude'] = doubleval($this->input->post('longitude',true));
  2223. $data['latitude'] = doubleval($this->input->post('latitude',true));
  2224. if ($data['longitude'] < -180 || $data['longitude'] > 180) exit(json_result('0421',$this->response['0421']));
  2225. if ($data['latitude'] < -90 || $data['latitude'] > 90) exit(json_result('0422',$this->response['0422']));
  2226. $poleheight = $this->input->post('poleheight',true);
  2227. if ($poleheight !== null) {
  2228. $data['poleheight'] = $poleheight;
  2229. }
  2230. $data["polediameter"] = $this->input->post('polediameter',true);
  2231. $data["polematerial"] = $this->input->post('polematerial',true);
  2232. $data["lighttype"] = $this->input->post('lighttype',true);
  2233. $data["lamptype"] = $this->input->post('lamptype',true);
  2234. $data["boardtype"] = $this->input->post('boardtype',true);
  2235. $data["batterytype"] = $this->input->post('batterytype',true);
  2236. $data["boardpower"] = $this->input->post('boardpower',true);
  2237. $data["batteryah"] = $this->input->post('batteryah',true);
  2238. $wattage = $this->input->post('wattage',true);
  2239. $data["wattage"] = !empty($wattage) ? $wattage : 0;
  2240. $supplier = $this->input->post('supplier',true);
  2241. $data["supplier"] = !empty($supplier) ? $supplier : '';
  2242. $upazilla = $this->input->post('upazilla',true);
  2243. $data["upazilla"] = !empty($upazilla) ? $upazilla : '';
  2244. $po = $this->input->post('po',true);
  2245. $data["po"] = !empty($po) ? $po : '';
  2246. $manu = $this->input->post('manu',true);
  2247. $data["manu"] = !empty($manu) ? $manu : '';
  2248. $protocoltype = $this->input->post('protocoltype',true);
  2249. $data['protocoltype'] = $protocoltype;
  2250. $loadtype = $this->input->post('loadtype',true);
  2251. if ($loadtype !== null) {
  2252. $data["loadtype"] = intval($loadtype);
  2253. }
  2254. // 控制器协议类型
  2255. $version = $this->session->userdata('version');
  2256. if (strlen($data['number']) > 6) exit(json_result('0408', $this->response['0408'], array()));
  2257. if (empty($data['projectid'])) exit(json_result('0308', $this->response['0308'], array()));
  2258. if (empty($data['number'])) exit(json_result('0409', $this->response['0409'], array()));
  2259. if (!is_numeric($data['number']) || $data['number'] < 1 || $data['number'] > 999999) exit(json_result('0410', $this->response['0410'], array()));
  2260. if (empty($data['address'])) exit(json_result('0805', $this->response['0805'], array()));
  2261. if (is_float($data['address'])) {
  2262. $data['address'] = strval(intval(ceil($data['address'])));
  2263. }
  2264. $is_true = preg_match('/^[A-Fa-f0-9]+$/', $data['address']);
  2265. if (empty($is_true)) exit(json_result('0414', $this->response['0414'], array()));
  2266. $data = remove_null_params($data);
  2267. if (empty($where['id'])) {
  2268. if ($this->Lamp_model->getDataCount(array('number'=>$data['number'], 'projectid'=>$data['projectid'])) > 0) {
  2269. exit(json_result('0401', $this->response['0401'], array()));
  2270. }
  2271. $lamp = $this->Lamp_model->get_one(array('address'=>$data['address']),'projectid,number');
  2272. if (!empty($lamp)) {
  2273. $projectid = $lamp['projectid'];
  2274. $number = $lamp['number'];
  2275. $projectname = $this->Project_model->getNameById($projectid);
  2276. if (empty($version)) {
  2277. exit(json_result('0404', "该无线模块地址已被【{$projectname}】项目,【{$number}】编号的灯控使用", array()));
  2278. }else{
  2279. exit(json_result('other', "The wireless module address has been used by \"{$projectname}\" project, and \"{$number}\" serial number", array()));
  2280. }
  2281. }
  2282. $projectData = $this->Project_model->get_one(['id'=>$data['projectid']],'patrolinterval,cityid,projectname');
  2283. if (empty($data['longitude']) && empty($data['latitude']) && !empty($projectData['cityid'])) {
  2284. $globalData = $this->Global_location_model->getOne($projectData['cityid'],'longitude,latitude');
  2285. $data['longitude'] = $globalData['longitude'];
  2286. $data['latitude'] = $globalData['latitude'];
  2287. }
  2288. $data['createtime'] = date('Y-m-d H:i:s',time());
  2289. $lampid = $this->Lamp_model->add($data);
  2290. $this->Project_model->add_lamp_count(array('lampcount'=>1,'faultcount'=>0,'projectid'=>$data['projectid']));
  2291. $lampId = intval($lampid);
  2292. $this->add_operation_log('insert',"在\"{$projectData['projectname']}\"项目里添加编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯",0);
  2293. $this->add_operation_log('insert',"Add lamp:\"{$data['number']}\".Wireless module address:\"{$data['address']}\".Project name:\"{$projectData['projectname']}\"",0,1);
  2294. } else {
  2295. if ($this->Lamp_model->getDataCount(array('number'=>$data['number'], 'projectid'=>$data['projectid']), $where['id']) > 0) {
  2296. exit(json_result('0401', $this->response['0401'], array()));
  2297. }
  2298. $lamp = $this->Lamp_model->get_one(array('address'=>$data['address'], 'id !='=>$where['id']),'projectid,number');
  2299. if (!empty($lamp)) {
  2300. $projectid = $lamp['projectid'];
  2301. $number = $lamp['number'];
  2302. $projectname = $this->Project_model->getNameById($projectid);
  2303. if (empty($version)) {
  2304. exit(json_result('0404', "该无线模块地址已被【{$projectname}】项目,【{$number}】编号的灯控使用", array()));
  2305. }else{
  2306. exit(json_result('other', "The wireless module address has been used by \"{$projectname}\" project, and \"{$number}\" serial number", array()));
  2307. }
  2308. }
  2309. $old = $this->Lamp_model->get_one($where,'isfaulted,projectid,address,protocoltype');
  2310. if ($old['projectid'] != $data['projectid']) {
  2311. $new['projectid'] = $data['projectid'];
  2312. $new['faultcount'] = $old['isfaulted'] > 0 ? 1 : 0;
  2313. $this->Project_model->add_lamp_count($new);
  2314. $old['faultcount'] = $new['faultcount'];
  2315. $this->Project_model->minus_lamp_count($old);
  2316. }
  2317. // $oldNet = $this->Network_model->getData(array('id'=>$old['networkid']));
  2318. if ($old['address'] != $data['address'] || $protocoltype != $old['protocoltype']) {
  2319. }
  2320. $this->Lamp_model->update($data,$where);
  2321. $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
  2322. $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯信息");
  2323. $this->add_operation_log('update',"Update lamp:\"{$data['number']}\".Wireless module address:\"{$data['address']}\".Project name:\"{$projectData['projectname']}\".",0,1);
  2324. //更新负载设置和蓄电池设置
  2325. if (isset($_POST['workmode'])) {
  2326. $res = $this->load_update();
  2327. if ($res['result'] == 1) {
  2328. $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯负载参数");
  2329. $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);
  2330. }else{
  2331. exit(json_result($res['msg'],$res['msg'],array()));
  2332. }
  2333. }
  2334. if (isset($_POST['capacity'])) {
  2335. $res = $this->battery_update();
  2336. if ($res['result'] == 1) {
  2337. $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯蓄电池参数");
  2338. $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);
  2339. }else{
  2340. exit(json_result($res['msg'],$res['msg'],array()));
  2341. }
  2342. }
  2343. $lampId = intval($where['id']);
  2344. }
  2345. exit(json_result('0000', $this->response['0000'], array("id" => $lampId)));
  2346. }
  2347. // 获取负载参数设置
  2348. public function load_setting() {
  2349. $lamp_id = $this->input->post('lamp_id', true);
  2350. if (empty($lamp_id)) {
  2351. exit(json_result('0400', $this->response['0400'], array()));
  2352. }
  2353. $lampData = $this->Lamp_model->getOne($lamp_id,'N.status,N.protocoltype,L.protocoltype as lampprotocoltype,L.address,L.number,N.deviceid');
  2354. $cmd = '{"cmd_type":"get_load_setting_cmd","cmd_id":'.$lamp_id.',"broadcast":0}';
  2355. if (($lampData['protocoltype'] == 4 || $lampData['protocoltype'] == 6) && $lampData['lampprotocoltype'] == 0) {
  2356. $address = '';
  2357. if (mb_strlen($lampData['address']) >= 8) {
  2358. $address = mb_substr($lampData['address'], -8);
  2359. }else{
  2360. $address = '00000000';
  2361. }
  2362. $sendData = $address.'0304001014';
  2363. if ($lampData['protocoltype'] == 4) {
  2364. $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*','0001'.$sendData));
  2365. }else{
  2366. device_cmd($lampData['deviceid'],pack('H*',$sendData));
  2367. $cmd = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
  2368. $info = send_lorawan_915($cmd);
  2369. $res = array();
  2370. if (!empty($info)) {
  2371. $info = json_decode($info,true);
  2372. if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
  2373. }
  2374. }
  2375. if (empty($res)) {
  2376. exit(json_result('0016', $this->response['0016']));
  2377. }else{
  2378. $msg1 = $res['msg'];
  2379. // 数据校验
  2380. $msg = mb_substr($msg1, 10);
  2381. $t = 0;
  2382. for ($i=0; $i < mb_strlen($msg) / 2 - 1; $i++) {
  2383. $s = mb_substr($msg, $i*2,2);
  2384. $t += base_convert($s, 16, 10);
  2385. }
  2386. $t = base_convert($t, 10, 16);
  2387. if (mb_strlen($t) > 2) {
  2388. $t = mb_substr($t, -2);
  2389. }elseif($t <= 1){
  2390. $t = '0'.$t;
  2391. }
  2392. if ($t == mb_substr($msg, -2)) {
  2393. $data = array();
  2394. $dataStr = mb_substr($msg1, 16);
  2395. $data['workmode'] = base_convert(mb_substr($dataStr, 0,2), 16, 10);
  2396. $data['worktimefirst'] = base_convert(mb_substr($dataStr, 2,2), 16, 10);
  2397. $data['workpowerfirst'] = base_convert(mb_substr($dataStr, 4,2), 16, 10);
  2398. $data['worktimesencond'] = base_convert(mb_substr($dataStr, 6,2), 16, 10);
  2399. $data['workpowersencond'] = base_convert(mb_substr($dataStr, 8,2), 16, 10);
  2400. $data['worktimethird'] = base_convert(mb_substr($dataStr, 10,2), 16, 10);
  2401. $data['workpowerthird'] = base_convert(mb_substr($dataStr, 12,2), 16, 10);
  2402. $data['worktimeforth'] = base_convert(mb_substr($dataStr, 14,2), 16, 10);
  2403. $data['workpowerforth'] = base_convert(mb_substr($dataStr, 16,2), 16, 10);
  2404. $data['delaytime'] = base_convert(mb_substr($dataStr, 18,2), 16, 10);
  2405. $data['voptically'] = round(base_convert(mb_substr($dataStr, 20,4), 16, 10)/10,1);
  2406. $data['ledloadcurrent'] = round(base_convert(mb_substr($dataStr, 24,4), 16, 10),2);
  2407. $data['powercmd'] = base_convert(mb_substr($dataStr, 28,2), 16, 10);
  2408. $data['switchfeature'] = base_convert(mb_substr($dataStr, 30,2), 16, 10);
  2409. exit(json_result('0000', $this->response['0000'], $data));
  2410. }else{
  2411. exit(json_result('0010', $this->response['0010']));
  2412. }
  2413. }
  2414. }
  2415. if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
  2416. $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
  2417. if ($cmdret === false) {
  2418. sleep(1);
  2419. $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
  2420. }
  2421. }else{
  2422. $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
  2423. if ($cmdret === false) {
  2424. sleep(1);
  2425. $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
  2426. }
  2427. }
  2428. if (empty($cmdret)) exit(json_result('0016',$this->response['0016']));
  2429. $r = json_decode($cmdret, true);
  2430. if ($r['result'] == false) {
  2431. $version = $this->session->userdata('version');
  2432. if (empty($version)) {
  2433. $msg = empty($r['msg']) ? '未知错误' : transfer_error_tips($r['msg']);
  2434. }else{
  2435. $msg = empty($r['msg']) ? 'Unknown error' : $r['msg'];
  2436. }
  2437. exit(json_result('other', $msg));
  2438. }
  2439. $res = $this->Load_model->get_data_by_filter(['relateid'=>$lamp_id,'cmdtype'=>0]);
  2440. if (empty($res)) {
  2441. $update['workmode'] = '0';
  2442. $update['worktimefirst'] = '4';
  2443. $update['workpowerfirst'] = '80';
  2444. $update['worktimesencond'] = '1';
  2445. $update['workpowersencond'] = '40';
  2446. $update['worktimethird'] = '8';
  2447. $update['workpowerthird'] = '30';
  2448. $update['worktimeforth'] = '0';
  2449. $update['workpowerforth'] = '30';
  2450. $update['voptically'] = '5';
  2451. $update['delaytime'] = '1';
  2452. $update['ledloadcurrent'] = '330';
  2453. $update['powercmd'] = '1';
  2454. $update['switchfeature'] = '1';
  2455. $res = $update;
  2456. }
  2457. unset($res['id']);
  2458. unset($res['updatetime']);
  2459. $data = $res;
  2460. exit(json_result('0000', $this->response['0000'], $data));
  2461. }
  2462. // 新控制器获取参数设置
  2463. public function modbus_get_load() {
  2464. $lamp_id = $this->input->post('lamp_id', true);
  2465. if (empty($lamp_id)) {
  2466. exit(json_result('0400', $this->response['0400'], array()));
  2467. }
  2468. //
  2469. $lampData = $this->Lamp_model->getOne($lamp_id,'N.status,N.deviceid,N.protocoltype,L.address,L.protocoltype as lampprotocoltype,L.number');
  2470. $cmd = '{"cmd_type":"get_load_setting_cmd","cmd_id":'.$lamp_id.',"broadcast":0}';
  2471. if ($lampData['protocoltype'] == 4 && $lampData['lampprotocoltype'] == 1) {
  2472. $sendData = '0103e08d0027a23b';
  2473. $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*','0001'.$sendData));
  2474. if (empty($res)) {
  2475. exit(json_result('0016', $this->response['0016']));
  2476. }else{
  2477. if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
  2478. $data = array();
  2479. $data['load_current'] = number_format(base_convert(substr($res['msg'], 6,4), 16, 10)/100,2).''; // 负载电流
  2480. $data['intell_power'] = base_convert(substr($res['msg'], 10,4), 16, 10).''; // 智能功率
  2481. $data['light_delay'] = base_convert(substr($res['msg'], 14,4), 16, 10).''; // 光控延时
  2482. $data['induction_delay'] = base_convert(substr($res['msg'], 18,4), 16, 10).''; // 感应延时
  2483. $data['first_light_time'] = base_convert(substr($res['msg'], 26,4), 16, 10).''; // 第一段亮灯时间
  2484. $data['first_light_human_power'] = base_convert(substr($res['msg'], 30,4), 16, 10).''; // 第一段有人功率
  2485. $data['first_light_unman_power'] = base_convert(substr($res['msg'], 34,4), 16, 10).''; // 第一段无人功率
  2486. $data['second_light_time'] = base_convert(substr($res['msg'], 38,4), 16, 10).''; // 第二段亮灯时间
  2487. $data['second_light_human_power'] = base_convert(substr($res['msg'], 42,4), 16, 10).''; // 第二段有人功率
  2488. $data['second_light_unman_power'] = base_convert(substr($res['msg'], 46,4), 16, 10).''; // 第二段无人功率
  2489. $data['third_light_time'] = base_convert(substr($res['msg'], 50,4), 16, 10).''; // 第三段亮灯时间
  2490. $data['third_light_human_power'] = base_convert(substr($res['msg'], 54,4), 16, 10).''; // 第三段有人功率
  2491. $data['third_light_unman_power'] = base_convert(substr($res['msg'], 58,4), 16, 10).''; // 第三段无人功率
  2492. $data['fourth_light_time'] = base_convert(substr($res['msg'], 62,4), 16, 10).''; // 第四段亮灯时间
  2493. $data['fourth_light_human_power'] = base_convert(substr($res['msg'], 66,4), 16, 10).''; // 第四段有人功率
  2494. $data['fourth_light_unman_power'] = base_convert(substr($res['msg'], 70,4), 16, 10).''; // 第四段无人功率
  2495. $data['fifth_light_time'] = base_convert(substr($res['msg'], 74,4), 16, 10).''; // 第五段亮灯时间
  2496. $data['fifth_light_human_power'] = base_convert(substr($res['msg'], 78,4), 16, 10).''; // 第五段有人功率
  2497. $data['fifth_light_unman_power'] = base_convert(substr($res['msg'], 82,4), 16, 10).''; // 第五段无人功率
  2498. $data['sixth_light_time'] = base_convert(substr($res['msg'], 86,4), 16, 10).''; // 第六段亮灯时间
  2499. $data['sixth_light_human_power'] = base_convert(substr($res['msg'], 90,4), 16, 10).''; // 第六段有人功率
  2500. $data['sixth_light_unman_power'] = base_convert(substr($res['msg'], 94,4), 16, 10).''; // 第六段无人功率
  2501. $data['seventh_light_time'] = base_convert(substr($res['msg'], 98,4), 16, 10).''; // 第七段亮灯时间
  2502. $data['seventh_light_human_power'] = base_convert(substr($res['msg'], 102,4), 16, 10).''; // 第七段有人功率
  2503. $data['seventh_light_unman_power'] = base_convert(substr($res['msg'], 106,4), 16, 10).''; // 第七段无人功率
  2504. $data['eighth_light_time'] = base_convert(substr($res['msg'], 110,4), 16, 10).''; // 第八段亮灯时间
  2505. $data['eighth_light_human_power'] = base_convert(substr($res['msg'], 114,4), 16, 10).''; // 第八段有人功率
  2506. $data['eighth_light_unman_power'] = base_convert(substr($res['msg'], 118,4), 16, 10).''; // 第八段无人功率
  2507. $data['ninth_light_time'] = base_convert(substr($res['msg'], 122,4), 16, 10).''; // 第九段亮灯时间
  2508. $data['ninth_light_human_power'] = base_convert(substr($res['msg'], 126,4), 16, 10).''; // 第九段有人功率
  2509. $data['ninth_light_unman_power'] = base_convert(substr($res['msg'], 130,4), 16, 10).''; // 第九段无人功率
  2510. $data['tenth_light_time'] = base_convert(substr($res['msg'], 134,4), 16, 10).''; // 晨亮时间
  2511. $data['tenth_light_human_power'] = base_convert(substr($res['msg'], 138,4), 16, 10).''; // 晨亮有人功率
  2512. $data['tenth_light_unman_power'] = base_convert(substr($res['msg'], 142,4), 16, 10).''; // 晨亮无人功率
  2513. $data['begin_voltage'] = round(base_convert(substr($res['msg'], 146,4), 16, 10)/10,1).''; // 智能功率USER1起始电压
  2514. $data['end_voltage'] = round(base_convert(substr($res['msg'], 150,4), 16, 10)/10,1).''; // 智能功率USER1终止电压
  2515. $data['end_current'] = round(base_convert(substr($res['msg'], 158,4), 16, 10)/100,2).''; // 智能功率USER1终止电流
  2516. $res = $data;
  2517. }else{
  2518. exit(json_result('0010', $this->response['0010']));
  2519. }
  2520. }
  2521. }
  2522. elseif($lampData['protocoltype'] == 6 && $lampData['lampprotocoltype'] == 1){
  2523. //$sendData = '0103e08d0027a23ba5';
  2524. $cmdLine='0103e08d0027a23b';
  2525. //$cmdLine .= crc16(pack('H*',$cmdLine));
  2526. device_cmd($lampData['deviceid'],pack('H*',$cmdLine));
  2527. $cmdLine = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
  2528. $res=send_lorawan_915($cmdLine);
  2529. if($res){
  2530. $res=json_decode($res,true);
  2531. if($res['info']=='success'){
  2532. $res = $this->Modbus_load_model->get_data_by_filter(['relateid'=>$lamp_id,'cmdtype'=>0]);
  2533. }else{
  2534. exit(json_result('0010', $this->response['0010']));
  2535. }
  2536. }else{
  2537. exit(json_result('0010', $this->response['0010']));
  2538. }
  2539. }
  2540. else{
  2541. $cmdret = '';
  2542. if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
  2543. $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
  2544. if ($cmdret === false) {
  2545. sleep(1);
  2546. $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
  2547. }
  2548. }else{
  2549. $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
  2550. if ($cmdret === false) {
  2551. sleep(1);
  2552. $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
  2553. }
  2554. }
  2555. if (empty($cmdret)) exit(json_result('0016',$this->response['0016']));
  2556. $r = json_decode($cmdret, true);
  2557. if ($r['result'] == false) {
  2558. $version = $this->session->userdata('version');
  2559. if (empty($version)) {
  2560. $msg = empty($r['msg']) ? '未知错误' : transfer_error_tips($r['msg']);
  2561. }else{
  2562. $msg = empty($r['msg']) ? 'Unknown error' : $r['msg'];
  2563. }
  2564. exit(json_result('other', $msg));
  2565. }
  2566. $res = $this->Modbus_load_model->get_data_by_filter(['relateid'=>$lamp_id,'cmdtype'=>0]);
  2567. if (empty($res)) {
  2568. exit(json_result('0417', $this->response['0417']));
  2569. }
  2570. }
  2571. $res['load_current'] = number_format($res['load_current'],2);
  2572. $res['end_current'] = number_format($res['end_current'],2);
  2573. $res['end_voltage'] = number_format($res['end_voltage'],1);
  2574. $res['begin_voltage'] = number_format($res['begin_voltage'],1);
  2575. $res['first_light_time'] = $this->format_date_en($res['first_light_time']);
  2576. $res['second_light_time'] = $this->format_date_en($res['second_light_time']);
  2577. $res['third_light_time'] = $this->format_date_en($res['third_light_time']);
  2578. $res['fourth_light_time'] = $this->format_date_en($res['fourth_light_time']);
  2579. $res['fifth_light_time'] = $this->format_date_en($res['fifth_light_time']);
  2580. $res['sixth_light_time'] = $this->format_date_en($res['sixth_light_time']);
  2581. $res['seventh_light_time'] = $this->format_date_en($res['seventh_light_time']);
  2582. $res['eighth_light_time'] = $this->format_date_en($res['eighth_light_time']);
  2583. $res['ninth_light_time'] = $this->format_date_en($res['ninth_light_time']);
  2584. $res['tenth_light_time'] = $this->format_date_en($res['tenth_light_time']);
  2585. unset($res['id']);
  2586. unset($res['updatetime']);
  2587. $data = $res;
  2588. exit(json_result('0000', $this->response['0000'], $data));
  2589. }
  2590. private function format_date_en($date){
  2591. $h = floor($date/3600);
  2592. $h = $h > 10 ? $h : '0'.$h;
  2593. $t = floor($date%3600/60);
  2594. $t = $t > 10 ? $t : '0'.$t;
  2595. return $h.':'.$t;
  2596. }
  2597. // 获取默认参数值
  2598. public function get_data(){
  2599. $type = $this->input->post('type',true);
  2600. $type = empty($type) ? 1 : $type;
  2601. $update = array();
  2602. if ($type == 1) { // 负载参数
  2603. $update['workmode'] = '0';
  2604. $update['worktimefirst'] = '4';
  2605. $update['workpowerfirst'] = '80';
  2606. $update['worktimesencond'] = '1';
  2607. $update['workpowersencond'] = '40';
  2608. $update['worktimethird'] = '8';
  2609. $update['workpowerthird'] = '30';
  2610. $update['worktimeforth'] = '0';
  2611. $update['workpowerforth'] = '30';
  2612. $update['voptically'] = '5';
  2613. $update['delaytime'] = '1';
  2614. $update['ledloadcurrent'] = '330';
  2615. $update['powercmd'] = '1';
  2616. $update['switchfeature'] = '1';
  2617. }else{ // 蓄电池参数
  2618. $update['batterytype'] = '0';//蓄电池类型
  2619. $update['capacity0'] = '200';//蓄电池容量
  2620. $update['capacity1'] = '';//蓄电池容量
  2621. $update['vovervoltage'] = '16';//超压电压
  2622. $update['vlimitedcharge'] = '15.5';//充电限制电压
  2623. $update['vbalancecharge'] = '14.6';//均衡充电电压
  2624. $update['vpromotecharge0'] = '14.4';//提升充电电压/过充电压(锂电池)
  2625. $update['vpromotecharge1'] = '12.6';//提升充电电压/过充电压(锂电池)
  2626. $update['vfloatingcharge'] = '13.8';//浮充充电电压
  2627. $update['vpromoterecover0'] = '13.2';//提升充电恢复电压/过充返回电压(锂电池)
  2628. $update['vpromoterecover1'] = '12';//提升充电恢复电压/过充返回电压(锂电池)
  2629. $update['voverdischargerecover0'] = '12.6';//过放恢复电压
  2630. $update['voverdischargerecover1'] = '11';//过放恢复电压
  2631. $update['vundervoltagewarn'] = '12';//欠压警告电压
  2632. $update['voverdischarge0'] = '11';//过放电压
  2633. $update['voverdischarge1'] = '9';//过放电压
  2634. $update['balancechargetime'] = '1';//均衡充电时间
  2635. $update['promotechargetime'] = '4';//提升充电时间
  2636. $update['balanceinterval'] = '30';//均衡充电间隔
  2637. $update['tempcompensation'] = '-4';//温度补偿系数
  2638. $update['tempcompmax'] = '99';//温度补偿最高温度
  2639. $update['tempcompmin'] = '-40';//温度补偿最低温度
  2640. $update['turnfloatingcurrent'] = '20';//转浮充的电流
  2641. $update['vsystem'] = '12';//系统电压
  2642. $update['minusnocharge'] = '0';//零下禁充
  2643. $update['chargemode'] = '0';//充电模式
  2644. }
  2645. exit(json_result('0000',$this->response['0000'],$update));
  2646. }
  2647. // 新控制器获取默认参数值
  2648. public function modbus_get_data(){
  2649. $type = $this->input->post('type',true);
  2650. $type = empty($type) ? 1 : $type;
  2651. $update = array();
  2652. if ($type == 1) { // 负载参数
  2653. $update['intell_power'] = '2';
  2654. $update['load_current'] = '0.9';
  2655. $update['light_delay'] = '20';
  2656. $update['induction_delay'] = '20';
  2657. $update['first_light_time'] = '00:30';
  2658. $update['first_light_human_power'] = '50';
  2659. $update['first_light_unman_power'] = '20';
  2660. $update['second_light_time'] = '00:30';
  2661. $update['second_light_human_power'] = '70';
  2662. $update['second_light_unman_power'] = '30';
  2663. $update['third_light_time'] = '02:00';
  2664. $update['third_light_human_power'] = '100';
  2665. $update['third_light_unman_power'] = '50';
  2666. $update['fourth_light_time'] = '00:30';
  2667. $update['fourth_light_human_power'] = '70';
  2668. $update['fourth_light_unman_power'] = '30';
  2669. $update['fifth_light_time'] = '00:30';
  2670. $update['fifth_light_human_power'] = '70';
  2671. $update['fifth_light_unman_power'] = '30';
  2672. $update['sixth_light_time'] = '04:00';
  2673. $update['sixth_light_human_power'] = '30';
  2674. $update['sixth_light_unman_power'] = '10';
  2675. $update['seventh_light_time'] = '00:00';
  2676. $update['seventh_light_human_power'] = '100';
  2677. $update['seventh_light_unman_power'] = '50';
  2678. $update['eighth_light_time'] = '00:00';
  2679. $update['eighth_light_human_power'] = '100';
  2680. $update['eighth_light_unman_power'] = '50';
  2681. $update['ninth_light_time'] = '00:00';
  2682. $update['ninth_light_human_power'] = '100';
  2683. $update['ninth_light_unman_power'] = '50';
  2684. $update['tenth_light_time'] = '00:00';
  2685. $update['tenth_light_human_power'] = '50';
  2686. $update['tenth_light_unman_power'] = '20';
  2687. $update['begin_voltage'] = '11.5';
  2688. $update['end_voltage'] = '10.0';
  2689. $update['end_current'] = '0.15';
  2690. }else{ // 蓄电池参数
  2691. $update['light_voltage'] = '5';
  2692. $update['system_voltage'] = '12';
  2693. $update['battery_type'] = '0';
  2694. $update['li_battery_type'] = '0';
  2695. $update['overvoltage'] = '15.5';
  2696. $update['char_limit_voltage'] = '15.0';
  2697. $update['equ_char_voltage'] = '14.6';
  2698. $update['increa_char_voltage'] = '14.4';
  2699. $update['float_char_voltage'] = '13.8';
  2700. $update['increa_char_return_voltage'] = '13.2';
  2701. $update['over_dischar_return_voltage'] = '12.6';
  2702. $update['over_dischar_voltage'] = '11.1';
  2703. $update['equ_char_time'] = '120';
  2704. $update['increa_char_time'] = '120';
  2705. $update['balan_char_interval'] = '30';
  2706. $update['temper_compen_coeff'] = '5';
  2707. $update['char_upper_limit_temper'] = '60';
  2708. $update['char_lower_limit_temper'] = '-30';
  2709. $update['dischar_upper_limit_temper'] = '60';
  2710. $update['dischar_lower_limit_temper'] = '-30';
  2711. }
  2712. exit(json_result('0000',$this->response['0000'],$update));
  2713. }
  2714. // 获取蓄电池设置
  2715. public function battery_setting() {
  2716. $lamp_id = $this->input->post('lamp_id', true);
  2717. if (empty($lamp_id)) {
  2718. exit(json_result('0400', $this->response['0400'], array()));
  2719. }
  2720. $lampData = $this->Lamp_model->getOne($lamp_id,'N.status,N.protocoltype,L.address,L.protocoltype as lampprotocoltype,L.number,N.deviceid');
  2721. // if ($lampData['status'] == 0) {
  2722. // exit(json_result('0412',$this->response['0412'],array()));
  2723. // }
  2724. $cmd = '{"cmd_type":"get_battery_setting_cmd","cmd_id":'.$lamp_id.',"broadcast":0}';
  2725. if (($lampData['protocoltype'] == 4 || $lampData['protocoltype'] == 6) && $lampData['lampprotocoltype'] == 0) {
  2726. $address = '';
  2727. if (mb_strlen($lampData['address']) >= 8) {
  2728. $address = mb_substr($lampData['address'], -8);
  2729. }else{
  2730. $address = '00000000';
  2731. }
  2732. $sendData = $address.'030328204b';
  2733. if ($lampData['protocoltype'] == 4) {
  2734. $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*','0001'.$sendData));
  2735. }else{
  2736. device_cmd($lampData['deviceid'],pack('H*',$sendData));
  2737. $cmd = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
  2738. $info = send_lorawan_915($cmd);
  2739. $res = array();
  2740. if (!empty($info)) {
  2741. $info = json_decode($info,true);
  2742. if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
  2743. }
  2744. }
  2745. if (empty($res)) {
  2746. exit(json_result('0016', $this->response['0016']));
  2747. }else{
  2748. $msg1 = $res['msg'];
  2749. // 数据校验
  2750. $msg = mb_substr($msg1, 10);
  2751. $t = 0;
  2752. for ($i=0; $i < mb_strlen($msg) / 2 - 1; $i++) {
  2753. $s = mb_substr($msg, $i*2,2);
  2754. $t += base_convert($s, 16, 10);
  2755. }
  2756. $t = base_convert($t, 10, 16);
  2757. if (mb_strlen($t) > 2) {
  2758. $t = mb_substr($t, -2);
  2759. }elseif($t <= 1){
  2760. $t = '0'.$t;
  2761. }
  2762. if ($t == mb_substr($msg, -2)) {
  2763. $data = array();
  2764. $dataStr = mb_substr($msg1, 16);
  2765. $data['batterytype'] = base_convert(mb_substr($dataStr, 56,2), 16, 10);//蓄电池类型
  2766. $data['capacity'] = base_convert(mb_substr($dataStr, 0,4), 16, 10);//蓄电池容量
  2767. $data['capacity1'] = base_convert(mb_substr($dataStr, 0,4), 16, 10);//蓄电池容量
  2768. $data['vovervoltage'] = round(base_convert(mb_substr($dataStr, 4,4), 16, 10)/10,1);//超压电压
  2769. $data['vlimitedcharge'] = round(base_convert(mb_substr($dataStr, 8,4), 16, 10)/10,1);//充电限制电压
  2770. $data['vbalancecharge'] = round(base_convert(mb_substr($dataStr, 12,4), 16, 10)/10,1);//均衡充电电压
  2771. $data['vpromotecharge0'] = round(base_convert(mb_substr($dataStr, 16,4), 16, 10)/10,1);//提升充电电压/过充电压(锂电池)
  2772. $data['vpromotecharge1'] = round(base_convert(mb_substr($dataStr, 16,4), 16, 10)/10,1);//提升充电电压/过充电压(锂电池)
  2773. $data['vfloatingcharge'] = round(base_convert(mb_substr($dataStr, 20,4), 16, 10)/10,1);//浮充充电电压
  2774. $data['vpromoterecover0'] = round(base_convert(mb_substr($dataStr, 24,4), 16, 10)/10,1);//提升充电恢复电压/过充返回电压(锂电池)
  2775. $data['vpromoterecover1'] = round(base_convert(mb_substr($dataStr, 24,4), 16, 10)/10,1);//提升充电恢复电压/过充返回电压(锂电池)
  2776. $data['voverdischargerecover0'] = round(base_convert(mb_substr($dataStr, 28,4), 16, 10)/10,1);//过放恢复电压
  2777. $data['voverdischargerecover1'] = round(base_convert(mb_substr($dataStr, 28,4), 16, 10)/10,1);//过放恢复电压
  2778. $data['vundervoltagewarn'] = round(base_convert(mb_substr($dataStr, 32,4), 16, 10)/10,1);//欠压警告电压
  2779. $data['voverdischarge0'] = round(base_convert(mb_substr($dataStr, 36,4), 16, 10)/10,1);//过放电压
  2780. $data['voverdischarge1'] = round(base_convert(mb_substr($dataStr, 36,4), 16, 10)/10,1);//过放电压
  2781. $data['balancechargetime'] = base_convert(mb_substr($dataStr, 40,2), 16, 10);//均衡充电时间
  2782. $data['promotechargetime'] = base_convert(mb_substr($dataStr, 42,2), 16, 10);//提升充电时间
  2783. $data['balanceinterval'] = base_convert(mb_substr($dataStr, 44,2), 16, 10);//均衡充电间隔
  2784. $data['tempcompensation'] = base_convert(mb_substr($dataStr, 46,2), 16, 10);//温度补偿系数
  2785. $data['tempcompmax'] = bitDecode(mb_substr($dataStr, 48,2));//温度补偿最高温度
  2786. $data['tempcompmin'] = bitDecode(mb_substr($dataStr, 50,2));//温度补偿最低温度
  2787. $data['turnfloatingcurrent'] = round(base_convert(mb_substr($dataStr, 52,4), 16, 10)/100,2);//转浮充的电流
  2788. $data['vsystem'] = base_convert(mb_substr($dataStr, 58,2), 16, 10);//系统电压
  2789. $data['minusnocharge'] = base_convert(mb_substr($dataStr, 60,2), 16, 10);//零下禁充
  2790. $data['chargemode'] = base_convert(mb_substr($dataStr, 62,2), 16, 10);//充电模式
  2791. exit(json_result('0000', $this->response['0000'], $data));
  2792. }else{
  2793. exit(json_result('0010', $this->response['0010']));
  2794. }
  2795. }
  2796. }
  2797. if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
  2798. $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
  2799. if ($cmdret === false) {
  2800. sleep(1);
  2801. $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
  2802. }
  2803. }else{
  2804. $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
  2805. if ($cmdret === false) {
  2806. sleep(1);
  2807. $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
  2808. }
  2809. }
  2810. if (empty($cmdret)) exit(json_result('0016',$this->response['0016']));
  2811. $r = json_decode($cmdret, true);
  2812. if ($r['result'] == false) {
  2813. $version = $this->session->userdata('version');
  2814. if (empty($version)) {
  2815. $msg = empty($r['msg']) ? '未知错误' : transfer_error_tips($r['msg']);
  2816. }else{
  2817. $msg = empty($r['msg']) ? 'Unknown error' : $r['msg'];
  2818. }
  2819. exit(json_result('other', $msg));
  2820. }
  2821. $res = $this->Battery_model->getOne($lamp_id);
  2822. if (empty($res)) {
  2823. $update['batterytype'] = '0';//蓄电池类型
  2824. $update['capacity0'] = '200';//蓄电池容量
  2825. $update['capacity1'] = '';//蓄电池容量
  2826. $update['vovervoltage'] = '16';//超压电压
  2827. $update['vlimitedcharge'] = '15.5';//充电限制电压
  2828. $update['vbalancecharge'] = '14.6';//均衡充电电压
  2829. $update['vpromotecharge0'] = '14.4';//提升充电电压/过充电压(锂电池)
  2830. $update['vpromotecharge1'] = '12.6';//提升充电电压/过充电压(锂电池)
  2831. $update['vfloatingcharge'] = '13.8';//浮充充电电压
  2832. $update['vpromoterecover0'] = '13.2';//提升充电恢复电压/过充返回电压(锂电池)
  2833. $update['vpromoterecover1'] = '12';//提升充电恢复电压/过充返回电压(锂电池)
  2834. $update['voverdischargerecover0'] = '12.6';//过放恢复电压
  2835. $update['voverdischargerecover1'] = '11';//过放恢复电压
  2836. $update['vundervoltagewarn'] = '12';//欠压警告电压
  2837. $update['voverdischarge0'] = '11';//过放电压
  2838. $update['voverdischarge1'] = '9';//过放电压
  2839. $update['balancechargetime'] = '1';//均衡充电时间
  2840. $update['promotechargetime'] = '4';//提升充电时间
  2841. $update['balanceinterval'] = '30';//均衡充电间隔
  2842. $update['tempcompensation'] = '-4';//温度补偿系数
  2843. $update['tempcompmax'] = '99';//温度补偿最高温度
  2844. $update['tempcompmin'] = '-40';//温度补偿最低温度
  2845. $update['turnfloatingcurrent'] = '20';//转浮充的电流
  2846. $update['vsystem'] = '12';//系统电压
  2847. $update['minusnocharge'] = '0';//零下禁充
  2848. $update['chargemode'] = '0';//充电模式
  2849. $res = $update;
  2850. }
  2851. $data = $res;
  2852. unset($data['id']);
  2853. unset($data['updatetime']);
  2854. exit(json_result('0000', $this->response['0000'], $data));
  2855. }
  2856. // 新控制器获取蓄电池设置
  2857. public function modbus_get_battery() {
  2858. $lamp_id = $this->input->post('lamp_id', true);
  2859. if (empty($lamp_id)) {
  2860. exit(json_result('0400', $this->response['0400'], array()));
  2861. }
  2862. $lampData = $this->Lamp_model->getOne($lamp_id,'N.status,N.protocoltype,N.deviceid,L.address,L.protocoltype as lampprotocoltype,L.number');
  2863. // if ($lampData['status'] == 0) {
  2864. // exit(json_result('0412',$this->response['0412'],array()));
  2865. // }
  2866. $cmd = '{"cmd_type":"get_battery_setting_cmd","cmd_id":'.$lamp_id.',"broadcast":0}';
  2867. if ($lampData['protocoltype'] == 4 && $lampData['lampprotocoltype'] == 1) {
  2868. $sendData = '0103e0020020d212';
  2869. $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*','0001'.$sendData));
  2870. if (empty($res)) {
  2871. exit(json_result('0016', $this->response['0016']));
  2872. }else{
  2873. if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
  2874. $data = array();
  2875. $data['system_voltage'] = base_convert(substr($res['msg'], 10,4), 16, 10).''; // 系统电压
  2876. if (substr('00000000'.base_convert(substr($res['msg'], 132,2), 16, 2), -8,1) == 1) { // 电池类型
  2877. $data['battery_type'] = '1';
  2878. }else{
  2879. $data['battery_type'] = '0';
  2880. }
  2881. $data['overvoltage'] = round(base_convert(substr($res['msg'], 18,4), 16, 10)/10,1).''; // 超压电压
  2882. $data['char_limit_voltage'] = round(base_convert(substr($res['msg'], 22,4), 16, 10)/10,1).''; // 充电限制电压
  2883. $data['equ_char_voltage'] = round(base_convert(substr($res['msg'], 26,4), 16, 10)/10,1).''; // 均衡充电电压
  2884. $data['increa_char_voltage'] = round(base_convert(substr($res['msg'], 30,4), 16, 10)/10,1).''; // 提升充电电压
  2885. $data['float_char_voltage'] = round(base_convert(substr($res['msg'], 34,4), 16, 10)/10,1).''; // 浮充充电电压
  2886. $data['increa_char_return_voltage'] = round(base_convert(substr($res['msg'], 38,4), 16, 10)/10,1).''; // 提升充电返回电压
  2887. $data['over_dischar_return_voltage'] = round(base_convert(substr($res['msg'], 42,4), 16, 10)/10,1).''; // 过放返回电压
  2888. $data['over_dischar_voltage'] = round(base_convert(substr($res['msg'], 50,4), 16, 10)/10,1).''; // 过放电压
  2889. $data['equ_char_time'] = base_convert(substr($res['msg'], 66,4), 16, 10).''; // 均衡充电时间
  2890. $data['increa_char_time'] = base_convert(substr($res['msg'], 70,4), 16, 10).''; // 提升充电时间
  2891. $data['balan_char_interval'] = base_convert(substr($res['msg'], 74,4), 16, 10).''; // 均衡充电间隔
  2892. $data['temper_compen_coeff'] = base_convert(substr($res['msg'], 78,4), 16, 10).''; // 温度补偿系数
  2893. $data['char_upper_limit_temper'] = bitDecode(substr($res['msg'], 84,2)).''; // 充电上限温度
  2894. $data['char_lower_limit_temper'] = bitDecode(substr($res['msg'], 88,2)).''; // 充电下限温度
  2895. $data['dischar_upper_limit_temper'] = bitDecode(substr($res['msg'], 92,2)).''; // 放电上限温度
  2896. $data['dischar_lower_limit_temper'] = bitDecode(substr($res['msg'], 96,2)).''; // 放电下限温度
  2897. $data['light_voltage'] = base_convert(substr($res['msg'], 122,4),16,10).''; // 光控电压
  2898. $info = $this->Modbus_battery_model->getOne($lamp_id);
  2899. if (empty($info)) {
  2900. $data['li_battery_type'] = '0';
  2901. }else{
  2902. $data['li_battery_type'] = $info['li_battery_type'];
  2903. }
  2904. $res = $data;
  2905. }else{
  2906. exit(json_result('0010', $this->response['0010']));
  2907. }
  2908. }
  2909. }
  2910. elseif ($lampData['protocoltype'] == 6 && $lampData['lampprotocoltype'] == 1){
  2911. $sendData = '0103e0020020d212';
  2912. $cmdLine='0103e0020020d212';
  2913. //$cmdLine .= crc16(pack('H*',$cmdLine));
  2914. device_cmd($lampData['deviceid'],pack('H*',$cmdLine));
  2915. $cmdLine = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
  2916. $res=send_lorawan_915($cmdLine);
  2917. if($res){
  2918. $res=json_decode($res,true);
  2919. //var_dump($res);exit();
  2920. if($res['info']=='success'){
  2921. $res =$this->Modbus_battery_model->getOne($lamp_id);
  2922. if (empty($res)) {
  2923. $res['li_battery_type'] = '0';
  2924. }
  2925. }else{
  2926. exit(json_result('0010', $this->response['0010']));
  2927. }
  2928. }else{
  2929. exit(json_result('0010', $this->response['0010']));
  2930. }
  2931. }
  2932. else{
  2933. if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
  2934. $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
  2935. if ($cmdret === false) {
  2936. sleep(1);
  2937. $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
  2938. }
  2939. }else{
  2940. $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
  2941. if ($cmdret === false) {
  2942. sleep(1);
  2943. $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
  2944. }
  2945. }
  2946. if (empty($cmdret)) exit(json_result('0016',$this->response['0016']));
  2947. $r = json_decode($cmdret, true);
  2948. if ($r['result'] == false) {
  2949. $version = $this->session->userdata('version');
  2950. if (empty($version)) {
  2951. $msg = empty($r['msg']) ? '未知错误' : transfer_error_tips($r['msg']);
  2952. }else{
  2953. $msg = empty($r['msg']) ? 'Unknown error' : $r['msg'];
  2954. }
  2955. exit(json_result('other', $msg));
  2956. }
  2957. $res = $this->Modbus_battery_model->getOne($lamp_id);
  2958. if (empty($res)) {
  2959. exit(json_result('0417', $this->response['0417']));
  2960. }
  2961. }
  2962. $data = $res;
  2963. $data['equ_char_time'] = intval($data['equ_char_time']/60).'';
  2964. $data['increa_char_time'] = intval($data['increa_char_time']/60).'';
  2965. unset($data['id']);
  2966. unset($data['updatetime']);
  2967. exit(json_result('0000', $this->response['0000'], $data));
  2968. }
  2969. // 灯控相关数据
  2970. public function viewloglist() {
  2971. $lamp_id = $this->input->post('lamp_id',true);
  2972. $datatype = $this->input->post('datatype',true);
  2973. if (empty($lamp_id)) {
  2974. exit(json_result('0400', $this->response['0400'], array()));
  2975. }
  2976. if ($datatype == 'lampinfo') {
  2977. $fields = "number,projectid,address,longitude,latitude,section,boardpower,lamptype,boardtype,batteryah,loadpower,devstatus,loadtype,supplier,upazilla,po,manu,protocoltype,updatetime,status";
  2978. $res = $this->Lamp_model->get_one(['id'=>$lamp_id],$fields);
  2979. if (empty($res)) {
  2980. exit(json_result('0000', $this->response['0006'], array()));
  2981. }
  2982. $version = $this->session->userdata('version');
  2983. if (!empty($version)) {
  2984. $polemaType = ['冷镀锌'=>'Cold galvanizing','热镀锌'=>'Hot galvanizing','不锈钢'=>'Stainless steel'];
  2985. $lightType = ['LED灯'=>'LED lamp','钠灯'=>'Sodium lamp','节能灯'=>'Energy saving light'];
  2986. $lampType = ['太阳能路灯'=>'Solar street lamp','景观灯'=>'Landscape lamp','庭院灯'=>'Garden light'];
  2987. $boardType = ['单晶板'=>'Single crystal plate','多晶板'=>'Polycrystalline plate','非晶板'=>'Amorphous plate'];
  2988. $batteryType = ['铅酸电池'=>'Lead acid battery','锂电池'=>'Lithium battery'];
  2989. if (!preg_match('/^[A-Za-z\s*]+$/', $res['lamptype'])) {
  2990. $res['lamptype'] = empty($lampType[$res['lamptype']]) ? '' : $lampType[$res['lamptype']];
  2991. }
  2992. if (!preg_match('/^[A-Za-z\s*]+$/', $res['boardtype'])) {
  2993. $res['boardtype'] = empty($boardType[$res['boardtype']]) ? '' : $boardType[$res['boardtype']];
  2994. }
  2995. }else{
  2996. $polemaType = ['Cold galvanizing'=>'冷镀锌','Hot galvanizing'=>'热镀锌','Stainless steel'=>'不锈钢'];
  2997. $lightType = ['LED lamp'=>'LED灯','Sodium lamp'=>'钠灯','Energy saving light'=>'节能灯'];
  2998. $lampType = ['Solar street lamp'=>'太阳能路灯','Landscape lamp'=>'景观灯','Garden light'=>'庭院灯'];
  2999. $boardType = ['Single crystal plate'=>'单晶板','Polycrystalline plate'=>'多晶板','Amorphous plate'=>'非晶板'];
  3000. $batteryType = ['Lead acid battery'=>'铅酸电池','Lithium battery'=>'锂电池'];
  3001. if (preg_match('/^[A-Za-z\s*]+$/', $res['lamptype'])) {
  3002. $res['lamptype'] = empty($lampType[$res['lamptype']]) ? '' : $lampType[$res['lamptype']];
  3003. }
  3004. if (preg_match('/^[A-Za-z\s*]+$/', $res['boardtype'])) {
  3005. $res['boardtype'] = empty($boardType[$res['boardtype']]) ? '' : $boardType[$res['boardtype']];
  3006. }
  3007. }
  3008. // if ($res["isfaulted"] == 0) {
  3009. // $res["isfaulted"] = '正常';
  3010. // }else{
  3011. // }
  3012. $alarmData = $this->Alarm_model->get_one_by_filter(array('lampid'=>$lamp_id,'status'=>0),'stralarmtype');
  3013. $version = $this->session->userdata('version');
  3014. if (empty($version)) {
  3015. $res['isfaulted'] = empty($alarmData['stralarmtype']) ? '正常' : $alarmData['stralarmtype'];
  3016. }else{
  3017. $res['isfaulted'] = empty($alarmData['stralarmtype']) ? 'Normal' : alarm_translate($alarmData['stralarmtype']);
  3018. }
  3019. $timezone = $this->Project_model->get_timezone_by_projectid($res['projectid']);
  3020. $res["updatetime"] = date_change($res["updatetime"],8,$timezone['value']);
  3021. $data = array(
  3022. "lamp_no" => $res["number"],
  3023. "project_name" => $this->Project_model->getNameById($res["projectid"]),
  3024. "address" => $res["address"],
  3025. "longitude" => $res["longitude"],
  3026. "latitude" => $res["latitude"],
  3027. "isfaulted" => $res["devstatus"],
  3028. "section" => $res["section"],
  3029. "boardpower" => floatval($res["boardpower"]),
  3030. "lamptype" => $res['lamptype'],
  3031. "boardtype" => $res['boardtype'],
  3032. "batteryah" => $res["batteryah"],
  3033. "loadpower" => $res["loadpower"],
  3034. 'status' => $res['status'],
  3035. // 'batterytype' => $res['batterytype'],
  3036. 'protocoltype'=>intval($res['protocoltype']),
  3037. 'supplier'=>intval($res['supplier']),
  3038. 'upazilla'=>intval($res['upazilla']),
  3039. 'po'=>intval($res['po']),
  3040. 'manu'=>intval($res['manu']),
  3041. "updatetime" => $res["updatetime"],
  3042. 'loadtype' => $res['loadtype']
  3043. );
  3044. // $msisdn = $this->db->query('select iccid from msisdn where imsi = "'.$res['simid'].'"')->row_array();
  3045. // if (empty($msisdn) || empty($msisdn['iccid'])) {
  3046. // $data['iccid'] = '';
  3047. // }else{
  3048. // $data['iccid'] = $msisdn['iccid'];
  3049. // }
  3050. // if ($res['protocoltype'] != 1 && $res['protocoltype'] != 2) {
  3051. // unset($data['packageSurplus']);
  3052. // unset($data['simid']);
  3053. // }
  3054. // $username = $this->get_user_info('username');
  3055. // if ($username == 'zns') {
  3056. // $data['monthly_can_usage'] = intval($res['monthly_can_usage']);
  3057. // $data['monthly_already_usage'] = round($res['monthly_already_usage'],2);
  3058. // }
  3059. exit(json_result('0000', $this->response['0000'], $data));
  3060. }
  3061. $table_map = array('system_info_log','lamp_info_log','solar_info_log','battery_info_log','electric_info_log','history_info_log');
  3062. $table = in_array($datatype, $table_map) ? $datatype : 'system_info_log';
  3063. $res = $this->Lamp_model->getForInfoLog($lamp_id, $table);
  3064. $lampData = $this->Lamp_model->get_one(['id'=>$lamp_id],'projectid');
  3065. $timezone = $this->Project_model->get_timezone_by_projectid($lampData['projectid']);
  3066. if(!$res){
  3067. exit(json_result('0000', $this->response['0006'], array()));
  3068. }
  3069. if ($table == 'battery_info_log') {
  3070. if (isset($res['daydischargemincurrent'])) {
  3071. $h = floor($res['daydischargemincurrent']/3600);
  3072. $h = $h < 10 ? '0'.$h : $h;
  3073. $i = intval($res['daydischargemincurrent']/60)%60;
  3074. $i = $i < 10 ? '0'.$i : $i;
  3075. $res['daydischargemincurrent'] = $h.':'.$i;
  3076. }
  3077. if (isset($res['daychargemincurrent'])) {
  3078. $h = floor($res['daychargemincurrent']/3600);
  3079. $h = $h < 10 ? '0'.$h : $h;
  3080. $i = intval($res['daychargemincurrent']/60)%60;
  3081. $i = $i < 10 ? '0'.$i : $i;
  3082. $res['daychargemincurrent'] = $h.':'.$i;
  3083. }
  3084. }
  3085. $res["updatetime"] = date_change($res["updatetime"],8,$timezone['value']);
  3086. exit(json_result('0000', $this->response['0000'], $res));
  3087. }
  3088. // 灯控历史数据
  3089. public function historylist() {
  3090. $lamp_id = $this->input->post('lamp_id',true);
  3091. $datatype = $this->input->post('datatype',true);
  3092. $page = intval($this->input->post('page',true));
  3093. $count = intval($this->input->post('count',true));
  3094. $count = empty($count) ? 16 : $count;
  3095. if (empty($lamp_id)) {
  3096. exit(json_result('0400', $this->response['0400'], array()));
  3097. }
  3098. $date = $this->input->post('date',true);
  3099. $beginDate = '';
  3100. $endDate = '';
  3101. // 按时区修改时间
  3102. $lampData = $this->Lamp_model->get_one(['id'=>$lamp_id],'projectid,protocoltype');
  3103. $timezone = $this->Project_model->get_timezone_by_projectid($lampData['projectid']);
  3104. if (!empty($date)) {
  3105. $dateArr = explode('/', $date);
  3106. $beginDate = date('Y-m-d H:i:s',strtotime($dateArr[0]) - 8*3600 + $timezone['value']*3600);
  3107. $endDate = date('Y-m-d H:i:s',strtotime($dateArr[1]) - 8*3600 + $timezone['value']*3600);
  3108. // $beginDate = $dateArr[0];
  3109. // $endDate = $dateArr[1];
  3110. if (strtotime($endDate) - strtotime($beginDate) > 31*24*3600) {
  3111. exit(json_result('0418',$this->response['0418']));
  3112. }
  3113. }else{
  3114. $beginDate = date('Y-m-d H:i:s',strtotime(date('Y-m-d 00:00:00',time())) - 8*3600 + $timezone['value']*3600);
  3115. $endDate = date('Y-m-d H:i:s',strtotime(date('Y-m-d 23:59:59',time())) - 8*3600 + $timezone['value']*3600);
  3116. }
  3117. if($page < 1){
  3118. $page = 1;
  3119. }
  3120. $download = intval($this->input->post('download'));
  3121. if (!empty($download)) {
  3122. if (empty($date)) exit(json_result('0007',$this->response['0007']));
  3123. $dataList = array();
  3124. // 路灯
  3125. $lampInfoList = $this->Lamp_model->getListForInfoLog($lamp_id, 'lamp_info_log', null, null,$beginDate,$endDate);
  3126. $temp = array();
  3127. $i = 1;
  3128. if (empty($this->version)) {
  3129. $lampStatusArr = array(0=>'关灯',1=>'开灯');
  3130. $title = array('序号','亮度%','状态','电压(V)','电流(A)','功率(W)','当天放电最大功率(W)','当天亮灯时长(HH:MM)','更新时间');
  3131. $name = '路灯';
  3132. }else{
  3133. $lampStatusArr = array(0=>'off',1=>'on');
  3134. $title = array('Number','Brightness%','Status','Voltage(V)','Current(A)','Power(W)','Maximum discharge power of the day(W)','dayLighting duration(HH:MM)','Update time');
  3135. $name = 'Lamp';
  3136. }
  3137. foreach ($lampInfoList as $k=>$l) {
  3138. $temp[$k][] = $i;
  3139. $temp[$k][] = $l['lighteness'];
  3140. $temp[$k][] = $lampStatusArr[$l['lampstatus']];
  3141. $temp[$k][] = $l['lampvoltage'];
  3142. $temp[$k][] = $l['lampcurrent'];
  3143. $temp[$k][] = $l['lamppower'];
  3144. $temp[$k][] = $l['daydischarmaxpow'];
  3145. $h = floor($l['daychargemincurrent'] / 3600);
  3146. $h = $h < 10 ? '0'.$h : $h;
  3147. $m = ceil($l['daychargemincurrent'] % 3600 / 60);
  3148. $m = $m < 10 ? '0'.$m : $m;
  3149. $temp[$k][] = $h.':'.$m;
  3150. $temp[$k][] = date_change($l["updatetime"],8,$timezone['value']);
  3151. $i ++;
  3152. }
  3153. $title1 = [];
  3154. foreach ($title as $t) {
  3155. $title1[0][] = $t;
  3156. }
  3157. $data = array_merge($title1,$temp);
  3158. $dataList[] = array('name'=>$name,'dataList'=>$data);
  3159. // 蓄电池
  3160. $lampInfoList = $this->Lamp_model->getListForInfoLog($lamp_id, 'battery_info_log', null, null,$beginDate,$endDate);
  3161. $temp = array();
  3162. $i = 1;
  3163. if (empty($this->version)) {
  3164. $name = '蓄电池';
  3165. if ($lampData['protocoltype'] == 2) {
  3166. $chargeStageMap = array(0=>'没有充电',1=>'等待',2=>'初始',3=>'MPPT',4=>'恒压',5=>'恒流',6=>'浮充',7=>'均衡',8=>'提升',9=>'涓流',10=>'完成',''=>'未知状态');
  3167. }else{
  3168. $chargeStageMap = array(0=>'没有充电',1=>'启动充电',6=>'限流',7=>'充满',16=>'MPPT充电',32=>'均衡充电',48=>'提升充电',64=>'浮充',''=>'未知状态');
  3169. }
  3170. $battstatusArr = array(0=>'空闲',1=>'放电',2=>'充电',3=>'一边充电一边放电',''=>'未知状态');
  3171. $unknown = '未知状态';
  3172. $title = array('序号','系统电压(V)','系统电流(A)','MOS温度(℃)','电池状态','充电阶段','电池电压(V)','当天最低电压(V)','当天最高电压(V)','电池温度(℃)','当天电池最低温度(℃)','当天电池最高温度(℃)','更新时间');
  3173. }else{
  3174. $name = 'Battery';
  3175. if ($lampData['protocoltype'] == 2) {
  3176. $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');
  3177. }else{
  3178. $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');
  3179. }
  3180. $battstatusArr = array(0=>'Free',1=>'Discharge',2=>'Charge',3=>'Discharge while charging',''=>'Unknown state');
  3181. $unknown = 'Unknown state';
  3182. $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');
  3183. }
  3184. $indexArr = [2=>'16',3=>'32',4=>'48',5=>'64'];
  3185. foreach ($lampInfoList as $k=>$l) {
  3186. $temp[$k][] = $i;
  3187. $temp[$k][] = $l['sysvoltage'];
  3188. $temp[$k][] = $l['syscurrent'];
  3189. $temp[$k][] = $l['temper'];
  3190. $temp[$k][] = $battstatusArr[$l['battstatus']];
  3191. if ($lampData['protocoltype'] == 1) {
  3192. $l['chargestage'] = isset($indexArr[intval($l['chargestage'])]) ? $indexArr[intval($l['chargestage'])] : $l['chargestage'];
  3193. }
  3194. $temp[$k][] = $chargeStageMap[$l['chargestage']];
  3195. $temp[$k][] = $l['battvoltage'];
  3196. $temp[$k][] = $l['voltagedaymin'];
  3197. $temp[$k][] = $l['voltagedaymax'];
  3198. $temp[$k][] = $l['batttemper'];
  3199. $temp[$k][] = $l['daybattmintemper'];
  3200. $temp[$k][] = $l['daybattmaxtemper'];
  3201. $temp[$k][] = date_change($l["updatetime"],8,$timezone['value']);
  3202. $i ++;
  3203. }
  3204. $title1 = [];
  3205. foreach ($title as $t) {
  3206. $title1[0][] = $t;
  3207. }
  3208. $data = array_merge($title1,$temp);
  3209. $dataList[] = array('name'=>$name,'dataList'=>$data);
  3210. // 太阳能
  3211. $lampInfoList = $this->Lamp_model->getListForInfoLog($lamp_id, 'solar_info_log', null, null,$beginDate,$endDate);
  3212. $temp = array();
  3213. $i = 1;
  3214. if (empty($this->version)) {
  3215. $name = '太阳能';
  3216. $title = array('序号','电压(V)','电流(A)','功率(W)','当天充电最大功率(WH)','当天充电时长(HH:MM)','更新时间');
  3217. }else{
  3218. $name = 'Solar';
  3219. $title = array('Number','Voltage(V)','Current(A)','Power(W)','Maximum Power Charged on the Day(W)','day Charging time(HH:MM)','Update time');
  3220. }
  3221. foreach ($lampInfoList as $k=>$l) {
  3222. $temp[$k][] = $i;
  3223. $temp[$k][] = $l['solarvoltage'];
  3224. $temp[$k][] = $l['solarcurrent'];
  3225. $temp[$k][] = $l['solarpower'];
  3226. $temp[$k][] = $l['daychargemaxpow'];
  3227. $h = floor($l['daydischargemincurrent'] / 3600);
  3228. $h = $h < 10 ? '0'.$h : $h;
  3229. $m = ceil($l['daydischargemincurrent'] % 3600 / 60);
  3230. $m = $m < 10 ? '0'.$m : $m;
  3231. $temp[$k][] = $h.':'.$m;
  3232. $temp[$k][] = date_change($l["updatetime"],8,$timezone['value']);
  3233. $i ++;
  3234. }
  3235. $title1 = [];
  3236. foreach ($title as $t) {
  3237. $title1[0][] = $t;
  3238. }
  3239. $data = array_merge($title1,$temp);
  3240. $dataList[] = array('name'=>$name,'dataList'=>$data);
  3241. // 系统
  3242. $lampInfoList = $this->Lamp_model->getListForInfoLog($lamp_id, 'system_info_log', null, null,$beginDate,$endDate);
  3243. $temp = array();
  3244. $i = 1;
  3245. if (empty($this->version)) {
  3246. $name = '系统';
  3247. $title = array('序号','系统电压(V)','系统电流(A)','控制器温度(℃)','更新时间');
  3248. }else{
  3249. $name = 'System';
  3250. $title = array('Serial number','System voltage(V)','System current(A)','Control temperature(℃)','Update time');
  3251. }
  3252. foreach ($lampInfoList as $k=>$l) {
  3253. $temp[$k][] = $i;
  3254. $temp[$k][] = $l['sysvoltage'];
  3255. $temp[$k][] = $l['syscurrent'];
  3256. $temp[$k][] = $l['temper'];
  3257. $temp[$k][] = date_change($l["updatetime"],8,$timezone['value']);
  3258. $i ++;
  3259. }
  3260. $title1 = [];
  3261. foreach ($title as $t) {
  3262. $title1[0][] = $t;
  3263. }
  3264. $data = array_merge($title1,$temp);
  3265. $dataList[] = array('name'=>$name,'dataList'=>$data);
  3266. $path = push_excel($dataList,'historyInfoExcel_'.date('Ymd'),1);
  3267. exit(json_result('0000', $this->response['0000'], array('path'=>$path)));
  3268. }
  3269. // $table_map = array('system_info_log','lamp_info_log','solar_info_log','battery_info_log','electric_info_log','history_info_log');
  3270. $table = !empty($datatype) ? $datatype : 'system_info_log';
  3271. if ($table == 'lamp_info_log') {
  3272. $table = 'realtime_info_log';
  3273. $total = $this->RealtimeInfoLog_model->get_count(['lampid'=>$lamp_id,'updatetime >='=>$beginDate,'updatetime <='=>$endDate]);
  3274. $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);
  3275. // $res = $this->Lamp_model->getListForInfoLog($lamp_id, $table, ($page-1)*$count, $count,$beginDate,$endDate);
  3276. }elseif ($table == 'system_info_log') {
  3277. $total = $this->Lamp_model->getCountForInfoLog($lamp_id, $table,$beginDate,$endDate);
  3278. $res = $this->Lamp_model->getListForInfoLog($lamp_id, $table, ($page-1)*$count, $count,$beginDate,$endDate);
  3279. }elseif ($table == 'battery_info_log') {
  3280. $join = [
  3281. // ['table'=>'history_info_log as HI','cond'=>'HI.lampid = RI.lampid and HI.updatetime = RI.updatetime','type'=>'inner'],
  3282. ];
  3283. $where = ['lampid'=>$lamp_id,'updatetime >='=>$beginDate,'updatetime <='=>$endDate];
  3284. $total = $this->RealtimeInfoLog_model->get_count($where,'count(*) as total');
  3285. $fields = 'batvoltage as battvoltage,updatetime,DayOverDisChgTimes as overtimes';
  3286. $res = $this->RealtimeInfoLog_model->get_list($where, $fields,$count, ($page-1)*$count, 'updatetime desc,id desc');
  3287. }else{
  3288. $total = $this->Lamp_model->getCountForInfoLog($lamp_id, $table,$beginDate,$endDate);
  3289. $res = $this->Lamp_model->getListForInfoLog($lamp_id, $table, ($page-1)*$count, $count,$beginDate,$endDate);
  3290. }
  3291. if(!$res){
  3292. exit(json_result('0000', $this->response['0006'], array()));
  3293. }else{
  3294. $arr = [2=>'16',3=>'32',4=>'48',5=>'64'];
  3295. foreach ($res as &$v) {
  3296. $v['updatetime'] = date_change($v["updatetime"],8,$timezone['value']);
  3297. if ($lampData['protocoltype'] == 1 && $table == 'battery_info_log') {
  3298. $v['chargestage'] = isset($arr[intval($v['chargestage'])]) ? $arr[intval($v['chargestage'])] : $v['chargestage'];
  3299. }
  3300. if ($table == 'system_info_log') {
  3301. $v['lampid'] = $v['number'];
  3302. }
  3303. }
  3304. }
  3305. $data = array(
  3306. 'total' => ceil($total/$count),
  3307. 'list' => $res
  3308. );
  3309. exit(json_result('0000', $this->response['0000'], $data));
  3310. }
  3311. // 修改路灯位置
  3312. public function change_location(){
  3313. $id = intval($this->input->post('id',true));
  3314. $type = intval($this->input->post('type',true));
  3315. $data['longitude'] = $this->input->post('longitude',true);
  3316. $data['latitude'] = $this->input->post('latitude',true);
  3317. $data['updatetime'] = date('Y-m-d H:i:s',time());
  3318. if (empty($id) || empty($type) || !isset($data['longitude']) || !isset($data['latitude'])) {
  3319. exit(json_result('0007',$this->response['0007'],array()));
  3320. }
  3321. if ($type == 1) {
  3322. $this->Lamp_model->update($data,array('id'=>$id));
  3323. }else{
  3324. $this->Videomonitor_model->update($data,array('id'=>$id));
  3325. }
  3326. exit(json_result('0000',$this->response['0000'],array()));
  3327. }
  3328. // 同步巡检
  3329. public function set_patrol_cmd(){
  3330. $id = intval($this->input->post('id',true));
  3331. if (empty($id)) exit(json_result('0007',$this->response['0007'],array('id'=>$id)));
  3332. $lampData = $this->Lamp_model->getOne($id,'N.status,N.protocoltype,L.address,L.protocoltype as lampprotocoltype,L.number');
  3333. $cmd = '{"cmd_type":"set_sync_patrol_cmd","cmd_id":'.$id.'}';
  3334. $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
  3335. if ($cmdret === false) {
  3336. sleep(1);
  3337. $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
  3338. }
  3339. exit(json_result('0000',$this->response['0000'],array('id'=>$id)));
  3340. }
  3341. // 批量添加
  3342. public function batchInput(){
  3343. set_time_limit(0);
  3344. $userid = $this->get_user_info('id');
  3345. // $idArr = $this->get_section_by_user();
  3346. $role = $this->get_user_info('role');
  3347. $companyid = $this->get_user_info('companyid');
  3348. $type = intval($this->input->post('type',true));
  3349. if ($type == 1) { // 验证
  3350. $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'];
  3351. $data = $this->batch_import($indexArr);
  3352. }else{ // 创建
  3353. $data = $this->input->post('lampData',true);
  3354. if (empty($data)) exit(json_result('0007',$this->response['0007']));
  3355. $data = json_decode($data,true);
  3356. }
  3357. foreach ($data as &$v) {
  3358. if ($type == 1 && is_float($v['address'])) $v['address'] = strval(intval(ceil($v['address'])));
  3359. }
  3360. $res = 0;
  3361. $typeArr = ['tcp'=>0,'nb'=>1,'gprs'=>2,'lorawan'=>3,'MQTT'=>4,'WeLoraWan'=>6,'onenet'=>7];
  3362. $this->db->trans_begin();
  3363. $msg = [];
  3364. $patrol_arr = array();
  3365. foreach ($data as $key => $value) {
  3366. $num = $key+2;
  3367. $value['number'] = trim($value['number']);
  3368. if (!isset($value['number']) || $value['number'] == '') {
  3369. $res = 1;
  3370. if (empty($this->version)) {
  3371. $msg[] = '表格第'.$num.'行,路灯编号不能为空';
  3372. }else{
  3373. $msg[] = 'Table '.$num.' row.Lamp number can not be empty';
  3374. }
  3375. }else{
  3376. if (!is_numeric($value['number']) && ($value['number'] <= 0 || $value['number'] > 999999)) {
  3377. $res = 1;
  3378. if (empty($this->version)) {
  3379. $msg[] = '表格第'.$num.'行,路灯编号为1-999999的数字';
  3380. }else{
  3381. $msg[] = 'Table '.$num.' row.The number of street lamps numbered 1-999999';
  3382. }
  3383. }
  3384. }
  3385. // if ($type == 1 && is_float($value['address'])) {
  3386. // $value['address'] = strval(intval(ceil($value['address'])));
  3387. // }
  3388. $value['address'] = trim($value['address']);
  3389. if (empty($value['address'])) {
  3390. $res = 1;
  3391. if (empty($this->version)) {
  3392. $msg[] = '表格第'.$num.'行,无线模块地址不能为空';
  3393. }else{
  3394. $msg[] = 'Table '.$num.' row.Address of the wireless module can not be empty';
  3395. }
  3396. }else{
  3397. $status = preg_match('/^[A-Fa-f0-9]+$/', $value['address']);
  3398. if (empty($status)) {
  3399. $res = 1;
  3400. if (empty($this->version)) {
  3401. $msg[] = '表格第'.$num.'行,无线模块地址为16进制数';
  3402. }else{
  3403. $msg[] = 'Table '.$num.' row.Wireless module address is 16 hexadecimal';
  3404. }
  3405. }else{
  3406. if ($this->Lamp_model->getDataCount(['address'=>$value['address']])) {
  3407. $res = 1;
  3408. if (empty($this->version)) {
  3409. $msg[] = '表格第'.$num.'行,无线模块地址已经存在';
  3410. }else{
  3411. $msg[] = 'Table '.$num.' row.Wireless module address already exists';
  3412. }
  3413. }
  3414. }
  3415. }
  3416. $value['company_name'] = trim($value['company_name']);
  3417. if (empty($value['company_name'])) {
  3418. $res = 1;
  3419. if (empty($this->version)) {
  3420. $msg[] = '表格第'.$num.'行,公司名称不能为空';
  3421. }else{
  3422. $msg[] = 'Table '.$num.' row.Company name cannot be empty';
  3423. }
  3424. }
  3425. $value['projectid'] = trim($value['projectid']);
  3426. if (empty($value['projectid'])) {
  3427. $res = 1;
  3428. if (empty($this->version)) {
  3429. $msg[] = '表格第'.$num.'行,项目名称不能为空';
  3430. }else{
  3431. $msg[] = 'Table '.$num.' row.Project name cannot be empty';
  3432. }
  3433. }else{
  3434. $projectid = $this->Project_model->project_exist_by_name($role,$userid,$companyid,$value['projectid'],$value['company_name']);
  3435. if (empty($projectid)) {
  3436. $res = 1;
  3437. if (empty($this->version)) {
  3438. $msg[] = '表格第'.$num.'行,项目不存在,请先创建项目';
  3439. }else{
  3440. $msg[] = 'Table '.$num.' row.The project does not exist. Please create a project first.';
  3441. }
  3442. $value['projectid'] = 0;
  3443. }else{
  3444. $value['projectid'] = $projectid;
  3445. $this->Project_model->add_lamp_count(array('faultcount'=>0,'projectid'=>$projectid));
  3446. if ($this->Lamp_model->getDataCount(['projectid'=>$value['projectid'],'number'=>$value['number']])) {
  3447. $res = 1;
  3448. if (empty($this->version)) {
  3449. $msg[] = '表格第'.$num.'行,路灯编号已经存在';
  3450. }else{
  3451. $msg[] = 'Table '.$num.' row lamp number already exists';
  3452. }
  3453. }
  3454. }
  3455. }
  3456. if($type != 1 && isset($typeArr[$value['protocoltype']]) && $typeArr[$value['protocoltype']] == 6){
  3457. $this->add_device($value['address']);
  3458. }
  3459. $value['devicetype'] = intval(trim($value['devicetype']));
  3460. if ($value['devicetype'] == 0) { // 非直连设备
  3461. unset($value['protocoltype']);
  3462. $value['networkid'] = trim($value['networkid']);
  3463. if (empty($value['networkid'])) {
  3464. $res = 1;
  3465. if (empty($this->version)) {
  3466. $msg[] = '表格第'.$num.'行,非直连设备网络编号不能为空';
  3467. }else{
  3468. $msg[] = 'Table '.$num.' row.Indirect device network number cannot be empty';
  3469. }
  3470. }else{
  3471. $networkid = $this->Network_model->project_exist_by_name($value['projectid'],$value['networkid']);
  3472. // var_dump($this->db->last_query());die;
  3473. if (empty($networkid)) {
  3474. $res = 1;
  3475. if (empty($this->version)) {
  3476. $msg[] = '表格第'.$num.'行,网络不存在,请先添加网络';
  3477. }else{
  3478. $msg[] = 'Table '.$num.' row.The network does not exist. Please add the network first';
  3479. }
  3480. }else{
  3481. $this->Network_model->add_lamp_count(array('faultcount'=>0,'networkid'=>$networkid));
  3482. $value['networkid'] = $networkid;
  3483. }
  3484. }
  3485. }elseif ($value['devicetype'] == 1) { // 直连设备
  3486. $value['protocoltype'] = trim($value['protocoltype']);
  3487. if (empty($value['protocoltype'])) {
  3488. $res = 1;
  3489. if (empty($this->version)) {
  3490. $msg[] = '表格第'.$num.'行,协议类型不能为空';
  3491. }else{
  3492. $msg[] = 'Table '.$num.' row.Protocol type cannot be empty';
  3493. }
  3494. }else{
  3495. if (!isset($typeArr[$value['protocoltype']])) {
  3496. $res = 1;
  3497. if (empty($this->version)) {
  3498. $msg[] = '表格第'.$num.'行,协议类型不存在';
  3499. }else{
  3500. $msg[] = 'Table '.$num.' row.Protocol type not exists';
  3501. }
  3502. }
  3503. }
  3504. // 创建直连网络
  3505. $number = mb_strlen($value['address']) == 13 ? base_convert($value['address'], 16, 10) : $value['address'];
  3506. if ($this->Network_model->getDataCount(array('devicesn'=>$number))) {
  3507. $res = 1;
  3508. if (empty($this->version)) {
  3509. $msg[] = '表格第'.$num.'行,无线模块地址已被使用';
  3510. }else{
  3511. $msg[] = 'Table '.$num.' row.Wireless module address has been used';
  3512. }
  3513. }
  3514. // 创建网络
  3515. // $networkData['id'] = $this->Zone_model->insert(array(
  3516. // 'name' => $number,
  3517. // 'parent' => $value['projectid'] ,
  3518. // 'level' => 4
  3519. // ));
  3520. // 控制器协议类型
  3521. $value['lampprotocoltype'] = trim($value['lampprotocoltype']);
  3522. if (empty($value['lampprotocoltype'])) {
  3523. $networkData['devicetype'] = 0;
  3524. }else{
  3525. if ($value['lampprotocoltype'] == 'ModBus') {
  3526. $networkData['devicetype'] = 3;
  3527. }elseif ($value['lampprotocoltype'] == 'LightBus') {
  3528. $networkData['devicetype'] = 0;
  3529. }else{
  3530. $networkData['devicetype'] = 0;
  3531. }
  3532. }
  3533. $networkData['networkid'] = $number;
  3534. $networkData['gatewaytype'] = 'direct';
  3535. $networkData['protocoltype'] = empty($typeArr[$value['protocoltype']]) ? 'tcp' : $typeArr[$value['protocoltype']];
  3536. $networkData['projectid'] = $value['projectid'];
  3537. $networkData['devicesn'] = $number;
  3538. $networkData['networkname'] = $number;
  3539. $networkData['type'] = 1;
  3540. $networkData['lampcount'] = 1;
  3541. $networkData['createtime'] = date('Y-m-d H:i:s',time());
  3542. if ($type == 1) {
  3543. $this->db->insert('network',$networkData);
  3544. $value['networkid'] = $this->db->insert_id();
  3545. }else{
  3546. $value['networkid'] = $this->Network_model->insert($networkData);
  3547. }
  3548. }else{
  3549. $res = 1;
  3550. if (empty($this->version)) {
  3551. $msg[] = '表格第'.$num.'行,设备类型选择错误';
  3552. }else{
  3553. $msg[] = 'Table '.$num.' row.Device type selection error';
  3554. }
  3555. }
  3556. unset($value['protocoltype']);
  3557. // 控制器协议类型
  3558. $value['lampprotocoltype'] = trim($value['lampprotocoltype']);
  3559. if (empty($value['lampprotocoltype'])) {
  3560. $res = 1;
  3561. if (empty($this->version)) {
  3562. $msg[] = '表格第'.$num.'行,协议类型不能为空';
  3563. }else{
  3564. $msg[] = 'Table '.$num.' row.Protocol type can not be empty';
  3565. }
  3566. }else{
  3567. if ($value['lampprotocoltype'] == 'ModBus') {
  3568. $value['protocoltype'] = 1;
  3569. }elseif ($value['lampprotocoltype'] == 'LightBus') {
  3570. $value['protocoltype'] = 0;
  3571. }else{
  3572. $res = 1;
  3573. if (empty($this->version)) {
  3574. $msg[] = '表格第'.$num.'行,协议类型选择错误';
  3575. }else{
  3576. $msg[] = 'Table '.$num.' row.Protocol type selection error';
  3577. }
  3578. }
  3579. }
  3580. unset($value['lampprotocoltype']);
  3581. $value['longitude'] = trim($value['longitude']);
  3582. if (empty($value['longitude'])) {
  3583. $value['longitude'] = 0;
  3584. }else{
  3585. if (!is_numeric($value['longitude']) || $value['longitude'] > 180 || $value['longitude'] < -180) {
  3586. $res = 1;
  3587. if (empty($this->version)) {
  3588. $msg[] = '表格第'.$num.'行,经度输入错误';
  3589. }else{
  3590. $msg[] = 'Table '.$num.' row longitude input error';
  3591. }
  3592. }
  3593. }
  3594. $value['latitude'] = trim($value['latitude']);
  3595. if (empty($value['latitude'])) {
  3596. $value['latitude'] = 0;
  3597. }else{
  3598. if (!is_numeric($value['latitude']) || $value['latitude'] > 90 || $value['latitude'] < -90) {
  3599. $res = 1;
  3600. if (empty($this->version)) {
  3601. $msg[] = '表格第'.$num.'行,纬度输入错误';
  3602. }else{
  3603. $msg[] = 'Table '.$num.' row latitude input error';
  3604. }
  3605. }
  3606. }
  3607. if (empty($value['latitude']) && empty($value['longitude']) && !empty($value['projectid'])) {
  3608. $projectData = $this->Project_model->getOne($value['projectid'],'cityid');
  3609. if (!empty($projectData['cityid'])){
  3610. $globalData = $this->Global_location_model->getOne($projectData['cityid'],'longitude,latitude');
  3611. $value['longitude'] = $globalData['longitude'];
  3612. $value['latitude'] = $globalData['latitude'];
  3613. }
  3614. }
  3615. $value['loadtype'] = trim($value['loadtype']);
  3616. if (empty($value['loadtype']) || $value['loadtype'] == '0' || $value['loadtype'] == '1') {
  3617. if ($value['loadtype'] == '1') {
  3618. $value['loadtype'] = 1;
  3619. }else{
  3620. $value['loadtype'] = 0;
  3621. }
  3622. }else{
  3623. $res = 1;
  3624. if (empty($this->version)) {
  3625. $msg[] = '表格第'.$num.'行,负载类型错误';
  3626. }else{
  3627. $msg[] = 'Table '.$num.' row load type error';
  3628. }
  3629. }
  3630. if (trim($value['poleheight']) == '') {
  3631. unset($value['poleheight']);
  3632. }else{
  3633. $value['poleheight'] = trim($value['poleheight']);
  3634. }
  3635. if (trim($value['polediameter']) == '') {
  3636. unset($value['polediameter']);
  3637. }else{
  3638. $value['polediameter'] = trim($value['polediameter']);
  3639. }
  3640. if (trim($value['section']) == '') {
  3641. $value['section'] = '';
  3642. $res = 1;
  3643. if (empty($this->version)) {
  3644. $msg[] = '表格第'.$num.'行,路段不能为空';
  3645. }else{
  3646. $msg[] = 'Table '.$num.' row. Section cannot be empty';
  3647. }
  3648. }else{
  3649. $value['section'] = trim($value['section']);
  3650. }
  3651. if (trim($value['polematerial']) == '') {
  3652. unset($value['polematerial']);
  3653. }else{
  3654. $value['polematerial'] = trim($value['polematerial']);
  3655. }
  3656. if (trim($value['lighttype']) == '') {
  3657. unset($value['lighttype']);
  3658. }else{
  3659. $value['lighttype'] = trim($value['lighttype']);
  3660. }
  3661. if (trim($value['lamptype']) == '') {
  3662. unset($value['lamptype']);
  3663. }else{
  3664. $value['lamptype'] = trim($value['lamptype']);
  3665. }
  3666. if (trim($value['boardtype']) == '') {
  3667. unset($value['boardtype']);
  3668. }else{
  3669. $value['boardtype'] = trim($value['boardtype']);
  3670. }
  3671. if (trim($value['batterytype']) == '') {
  3672. unset($value['batterytype']);
  3673. }else{
  3674. $value['batterytype'] = trim($value['batterytype']);
  3675. }
  3676. if (trim($value['boardpower']) == '') {
  3677. unset($value['boardpower']);
  3678. }else{
  3679. $value['boardpower'] = trim($value['boardpower']);
  3680. }
  3681. if (trim($value['batteryah']) == '') {
  3682. unset($value['batteryah']);
  3683. }else{
  3684. $value['batteryah'] = trim($value['batteryah']);
  3685. }
  3686. if (trim($value['loadpower']) == '') {
  3687. unset($value['loadpower']);
  3688. }else{
  3689. $value['loadpower'] = trim($value['loadpower']);
  3690. }
  3691. $value['createtime'] = date('Y-m-d H:i:s',time());
  3692. $value['updatetime'] = date('Y-m-d H:i:s',time());
  3693. unset($value['company_name']);
  3694. if (is_null($value['networkid'])) {
  3695. unset($value['networkid']);
  3696. }
  3697. $lamp_id = $this->Lamp_model->insert($value);
  3698. $patrol_arr[] = array('lampid'=>$lamp_id,'projectid'=>$value['projectid']);
  3699. }
  3700. if ($res || $type == 1) {
  3701. $this->db->trans_rollback();
  3702. exit(json_result('0000',$this->response['0000'],['lampData'=>$data,'msg'=>$msg]));
  3703. }else{
  3704. $this->db->trans_commit();
  3705. foreach ($patrol_arr as $patrol) {
  3706. $projectData = $this->Project_model->getOne($patrol['projectid'],'patrolinterval');
  3707. $this->patrol_set($patrol['lampid'],$projectData['patrolinterval']);
  3708. }
  3709. exit(json_result('0000',$this->response['0000']));
  3710. }
  3711. }
  3712. // 路灯历史数据曲线图
  3713. public function lamp_history_data(){
  3714. $type = intval($this->input->post('type',true));
  3715. $lampId = intval($this->input->post('lampid',true));
  3716. if (empty($lampId)) exit(json_result('0007',$this->response['0007']));
  3717. $dateType = intval($this->input->post('dateType',true));
  3718. $date = $this->input->post('date',true);
  3719. if ($dateType == 0) { // 一天
  3720. $beginDate = date('Y-m-d H:i:s',time()-24*3600);
  3721. $endDate = date('Y-m-d H:i:s',time());
  3722. }elseif ($dateType == 1) { // 三天
  3723. $beginDate = date('Y-m-d H:i:s',time()-24*3600*3);
  3724. $endDate = date('Y-m-d H:i:s',time());
  3725. }elseif ($dateType == 2) { // 七天
  3726. $beginDate = date('Y-m-d H:i:s',time()-24*3600*7);
  3727. $endDate = date('Y-m-d H:i:s',time());
  3728. }elseif ($dateType == 3) { // 十四天
  3729. $beginDate = date('Y-m-d H:i:s',time()-24*3600*14);
  3730. $endDate = date('Y-m-d H:i:s',time());
  3731. }else{ // 选择时间
  3732. if (empty($date)) exit(json_result('0007',$this->response['0007']));
  3733. $res = explode('/', $date);
  3734. if (strtotime($res[1]) - strtotime($res[0]) > 31 * 24 * 3600) {
  3735. exit(json_result('0418',$this->response['0418']));
  3736. }
  3737. $beginDate = $res[0];
  3738. $endDate = $res[1];
  3739. }
  3740. $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";
  3741. $lampData = $this->db->query($sql)->row_array();
  3742. $timezoneData = $this->Project_model->get_timezone_by_projectid($lampData['id']);
  3743. $timezone = $timezoneData['value'];
  3744. $data = ['patrolinterval'=>$lampData['patrolinterval']];
  3745. if ($type == 0) { // 路灯
  3746. $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.'"';
  3747. $list = $this->db->query($sql)->result_array();
  3748. $temp = [];
  3749. foreach ($list as $value) {
  3750. $temp['lampvoltage'][] = $value['lampvoltage'];
  3751. $temp['lampcurrent'][] = $value['lampcurrent'];
  3752. $temp['lamppower'][] = $value['lamppower'];
  3753. // $temp['lighteness'][] = $value['lighteness'];
  3754. $temp['updatetime'][] = date_change($value['updatetime'],8,$timezone);
  3755. if (!isset($data['maxcurrent'])) {
  3756. $data['maxcurrent'] = $value['lampcurrent'];
  3757. }else{
  3758. $data['maxcurrent'] = $value['lampcurrent'] > $data['maxcurrent'] ? $value['lampcurrent'] : $data['maxcurrent'];
  3759. }
  3760. // if (!isset($data['maxdaychargemincurrent'])) {
  3761. // $data['maxdaychargemincurrent'] = $value['daychargemincurrent'];
  3762. // }else{
  3763. // $data['maxdaychargemincurrent'] = $value['daychargemincurrent'] > $data['maxdaychargemincurrent'] ? $value['daychargemincurrent'] : $data['maxdaychargemincurrent'];
  3764. // }
  3765. if (!isset($data['mincurrent'])) {
  3766. $data['mincurrent'] = $value['lampcurrent'];
  3767. }else{
  3768. $data['mincurrent'] = $value['lampcurrent'] < $data['mincurrent'] ? $value['lampcurrent'] : $data['mincurrent'];
  3769. }
  3770. if (!isset($data['maxvoltage'])) {
  3771. $data['maxvoltage'] = $value['lampvoltage'];
  3772. }else{
  3773. $data['maxvoltage'] = $value['lampvoltage'] > $data['maxvoltage'] ? $value['lampvoltage'] : $data['maxvoltage'];
  3774. }
  3775. if (!isset($data['minvoltage'])) {
  3776. $data['minvoltage'] = $value['lampvoltage'];
  3777. }else{
  3778. $data['minvoltage'] = $value['lampvoltage'] < $data['minvoltage'] ? $value['lampvoltage'] : $data['minvoltage'];
  3779. }
  3780. if (!isset($data['maxpower'])) {
  3781. $data['maxpower'] = $value['lamppower'];
  3782. }else{
  3783. $data['maxpower'] = $value['lamppower'] > $data['maxpower'] ? $value['lamppower'] : $data['maxpower'];
  3784. }
  3785. if (!isset($data['minpower'])) {
  3786. $data['minpower'] = $value['lamppower'];
  3787. }else{
  3788. $data['minpower'] = $value['lamppower'] < $data['minpower'] ? $value['lamppower'] : $data['minpower'];
  3789. }
  3790. // if (!isset($data['maxlighteness'])) {
  3791. // $data['maxlighteness'] = $value['lighteness'];
  3792. // }else{
  3793. // $data['maxlighteness'] = $value['lighteness'] > $data['maxlighteness'] ? $value['lighteness'] : $data['maxlighteness'];
  3794. // }
  3795. // if (!isset($data['minlighteness'])) {
  3796. // $data['minlighteness'] = $value['lighteness'];
  3797. // }else{
  3798. // $data['minlighteness'] = $value['lighteness'] < $data['minlighteness'] ? $value['lighteness'] : $data['minlighteness'];
  3799. // }
  3800. // if (isset($data['maxdaychargemincurrent'])) {
  3801. // $h = floor($data['maxdaychargemincurrent'] / 3600);
  3802. // $h = $h < 10 ? '0'.$h : $h;
  3803. // $m = floor($data['maxdaychargemincurrent'] / 3600 % 60);
  3804. // $m = $m < 10 ? '0'.$m : $m;
  3805. // $data['maxdaychargemincurrent'] = $h.':'.$m;
  3806. // }
  3807. }
  3808. $list = $temp;
  3809. }elseif ($type == 1) { // 蓄电池
  3810. $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.'"';
  3811. $list = $this->db->query($sql)->result_array();
  3812. $temp = [];
  3813. foreach ($list as $value) {
  3814. $temp['battvoltage'][] = $value['battvoltage'];
  3815. $temp['updatetime'][] = date_change($value['updatetime'],8,$timezone);
  3816. $batterycurrent = $value['chargecurrent'];
  3817. $temp['batterycurrent'][] = $batterycurrent;
  3818. $batterypower = $value['chargepower'];
  3819. $temp['batterypower'][] = $batterypower;
  3820. if (!isset($data['maxcurrent'])) {
  3821. $data['maxcurrent'] = $batterycurrent;
  3822. }else{
  3823. $data['maxcurrent'] = $batterycurrent > $data['maxcurrent'] ? $batterycurrent : $data['maxcurrent'];
  3824. }
  3825. if (!isset($data['mincurrent'])) {
  3826. $data['mincurrent'] = $batterycurrent;
  3827. }else{
  3828. $data['mincurrent'] = $batterycurrent < $data['mincurrent'] ? $batterycurrent : $data['mincurrent'];
  3829. }
  3830. if (!isset($data['maxvoltage'])) {
  3831. $data['maxvoltage'] = $value['battvoltage'];
  3832. }else{
  3833. $data['maxvoltage'] = $value['battvoltage'] > $data['maxvoltage'] ? $value['battvoltage'] : $data['maxvoltage'];
  3834. }
  3835. if (!isset($data['minvoltage'])) {
  3836. $data['minvoltage'] = $value['battvoltage'];
  3837. }else{
  3838. $data['minvoltage'] = $value['battvoltage'] < $data['minvoltage'] ? $value['battvoltage'] : $data['minvoltage'];
  3839. }
  3840. if (!isset($data['maxbatttemper'])) {
  3841. $data['maxbatttemper'] = $value['batttemper'];
  3842. }else{
  3843. $data['maxbatttemper'] = $value['batttemper'] > $data['maxbatttemper'] ? $value['batttemper'] : $data['maxbatttemper'];
  3844. }
  3845. if (!isset($data['minbatttemper'])) {
  3846. $data['minbatttemper'] = $value['batttemper'];
  3847. }else{
  3848. $data['minbatttemper'] = $value['batttemper'] < $data['minbatttemper'] ? $value['batttemper'] : $data['minbatttemper'];
  3849. }
  3850. if (!isset($data['maxpower'])) {
  3851. $data['maxpower'] = round($batterypower,2);
  3852. }else{
  3853. $data['maxpower'] = $batterypower > $data['maxpower'] ? round($batterypower,2) : round($data['maxpower'],2);
  3854. }
  3855. if (!isset($data['minpower'])) {
  3856. $data['minpower'] = round($batterypower,2);
  3857. }else{
  3858. $data['minpower'] = $batterypower < $data['minpower'] ? round($batterypower,2) : round($data['minpower'],2);
  3859. }
  3860. }
  3861. $list = $temp;
  3862. }elseif ($type == 2) { // 太阳能板
  3863. $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.'"';
  3864. $list = $this->db->query($sql)->result_array();
  3865. $temp = [];
  3866. foreach ($list as $value) {
  3867. $temp['solarvoltage'][] = $value['solarvoltage'];
  3868. $temp['solarcurrent'][] = $value['solarcurrent'];
  3869. $temp['solarpower'][] = $value['solarpower'];
  3870. $temp['updatetime'][] = date_change($value['updatetime'],8,$timezone);
  3871. if (!isset($data['maxcurrent'])) {
  3872. $data['maxcurrent'] = $value['solarcurrent'];
  3873. }else{
  3874. $data['maxcurrent'] = $value['solarcurrent'] > $data['maxcurrent'] ? $value['solarcurrent'] : $data['maxcurrent'];
  3875. }
  3876. if (!isset($data['mincurrent'])) {
  3877. $data['mincurrent'] = $value['solarcurrent'];
  3878. }else{
  3879. $data['mincurrent'] = $value['solarcurrent'] < $data['mincurrent'] ? $value['solarcurrent'] : $data['mincurrent'];
  3880. }
  3881. if (!isset($data['maxvoltage'])) {
  3882. $data['maxvoltage'] = $value['solarvoltage'];
  3883. }else{
  3884. $data['maxvoltage'] = $value['solarvoltage'] > $data['maxvoltage'] ? $value['solarvoltage'] : $data['maxvoltage'];
  3885. }
  3886. // if (!isset($data['maxdaydischargemincurrent'])) {
  3887. // $data['maxdaydischargemincurrent'] = $value['daydischargemincurrent'];
  3888. // }else{
  3889. // $data['maxdaydischargemincurrent'] = $value['daydischargemincurrent'] > $data['maxdaydischargemincurrent'] ? $value['daydischargemincurrent'] : $data['maxdaydischargemincurrent'];
  3890. // }
  3891. if (!isset($data['minvoltage'])) {
  3892. $data['minvoltage'] = $value['solarvoltage'];
  3893. }else{
  3894. $data['minvoltage'] = $value['solarvoltage'] < $data['minvoltage'] ? $value['solarvoltage'] : $data['minvoltage'];
  3895. }
  3896. if (!isset($data['maxpower'])) {
  3897. $data['maxpower'] = $value['solarpower'];
  3898. }else{
  3899. $data['maxpower'] = $value['solarpower'] > $data['maxpower'] ? $value['solarpower'] : $data['maxpower'];
  3900. }
  3901. if (!isset($data['minpower'])) {
  3902. $data['minpower'] = $value['solarpower'];
  3903. }else{
  3904. $data['minpower'] = $value['solarpower'] < $data['minpower'] ? $value['solarpower'] : $data['minpower'];
  3905. }
  3906. if (isset($data['maxdaydischargemincurrent'])) {
  3907. $h = floor($data['maxdaydischargemincurrent'] / 3600);
  3908. $h = $h < 10 ? '0'.$h : $h;
  3909. $m = floor($data['maxdaydischargemincurrent'] / 3600 % 60);
  3910. $m = $m < 10 ? '0'.$m : $m;
  3911. $data['maxdaydischargemincurrent'] = $h.':'.$m;
  3912. }
  3913. }
  3914. $list = $temp;
  3915. }else{ // 电量
  3916. $sql = 'select daychargeah,daydischarah,updatetime from battery_info_log where lampid = '.$lampId.' and updatetime >= "'.$beginDate.'" and updatetime <= "'.$endDate.'"';
  3917. $list = $this->db->query($sql)->result_array();
  3918. $temp = [];
  3919. foreach ($list as $value) {
  3920. $temp['daychargeah'][] = $value['daychargeah'];
  3921. $temp['daydischarah'][] = $value['daydischarah'];
  3922. $temp['updatetime'][] = date_change($value['updatetime'],8,$timezone);
  3923. if (!isset($data['maxdaychargeah'])) {
  3924. $data['maxdaychargeah'] = $value['daychargeah'];
  3925. }else{
  3926. $data['maxdaychargeah'] = $value['daychargeah'] > $data['maxdaychargeah'] ? $value['daychargeah'] : $data['maxdaychargeah'];
  3927. }
  3928. if (!isset($data['mindaychargeah'])) {
  3929. $data['mindaychargeah'] = $value['daychargeah'];
  3930. }else{
  3931. $data['mindaychargeah'] = $value['daychargeah'] < $data['mindaychargeah'] ? $value['daychargeah'] : $data['mindaychargeah'];
  3932. }
  3933. if (!isset($data['maxdaydischarah'])) {
  3934. $data['maxdaydischarah'] = $value['daydischarah'];
  3935. }else{
  3936. $data['maxdaydischarah'] = $value['daydischarah'] > $data['maxdaydischarah'] ? $value['daydischarah'] : $data['maxdaydischarah'];
  3937. }
  3938. if (!isset($data['mindaydischarah'])) {
  3939. $data['mindaydischarah'] = $value['daydischarah'];
  3940. }else{
  3941. $data['mindaydischarah'] = $value['daydischarah'] < $data['mindaydischarah'] ? $value['daydischarah'] : $data['mindaydischarah'];
  3942. }
  3943. }
  3944. $list = $temp;
  3945. }
  3946. if (empty($list)) {
  3947. $data['list'] = new StdClass();
  3948. }else{
  3949. $data['list'] = $list;
  3950. }
  3951. exit(json_result('0000',$this->response['0000'],$data));
  3952. }
  3953. }