Lampcontrol.php 266 KB

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