Lampcontrol.php 248 KB

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