Lampcontrol.php 248 KB

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