| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000 | 
							- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
- include_once(FCPATH . 'application/controllers/Base_Controller.php');
 
- class Lampcontrol extends Base_Controller {
 
- 	
 
-     private $chargeStageMap = array(0=>'没有充电',16=>'MPPT充电',32=>'均衡充电',48=>'提升充电',64=>'浮充',''=>'未知状态');
 
-     public function __construct() {
 
-         parent::__construct();
 
-         $this->load->model('Company_model');
 
-         $this->load->model('Project_model');
 
-         $this->load->model('Network_model');
 
-         $this->load->model('Lamp_model');
 
-         $this->load->model('Batmanage_model');
 
-         $this->load->model('Patrol_model'); 
 
-         $this->load->model('Load_model');
 
-         $this->load->model('Battery_model');
 
-         $this->load->model('Alarm_model');
 
-         // $this->load->model('Zone_model');
 
-         $this->load->model('Videomonitor_model');
 
-         $this->load->model('Modbus_load_model');
 
-         $this->load->model('Modbus_battery_model');
 
-         $this->load->model('Global_location_model');
 
-     }
 
-     // 灯控列表
 
-     public function get() {
 
-         $userid = $this->get_user_info('id');
 
-         $role = $this->get_user_info('role');
 
-         $companyid = $this->get_user_info('companyid');
 
-         $project_id = $this->input->post('project_id', true);
 
-         $fields = $this->input->post('fields', true);
 
-         $field1 = $this->input->post('field1', true);
 
-         $project_id = intval($project_id);
 
-         $condition = array();
 
-         $pid_arr = array();
 
-         $timezone = null;
 
-         // 获取项目id
 
-         if ($project_id > 0) {
 
-             $pid_arr = $project_id;
 
-             $timezone = $this->Project_model->get_timezone_by_projectid($project_id);
 
-         }else{
 
-             $project_data = $this->Project_model->getMultiData($condition, 'P.id', $role, $companyid, $userid);
 
-             foreach ($project_data as $key => $value) {
 
-                 $pid_arr[] = $value['id'];
 
-             }
 
-         }
 
-         $filter = array();
 
-         $network_id = $this->input->post('network_id', true);
 
-         $section = $this->input->post('section', true);
 
-         // $type = intval($this->input->post('type', true));
 
-         if (!empty($network_id)) {
 
-             $filter['networkid'] = $network_id;
 
-         }
 
-         if (!empty($section)) {
 
-             $filter['section'] = $section;
 
-         }
 
-         $offline = $this->input->post('offline', true);
 
-         if (!empty($offline)) {
 
-             $filter['offline'] = $offline;
 
-         }
 
-         $online = $this->input->post('online', true);
 
-         if (!empty($online)) {
 
-             $filter['online'] = $online;
 
-         }
 
-         // 分页数据
 
-         $page = $this->input->post('page',true);
 
-         $count = $this->input->post('count',true);
 
-         $count = empty($count) ? 16 : $count;
 
-         $page = empty($page) ? 1 : $page;
 
-         $filter['count'] = $count;
 
-         $filter['page'] = $page;
 
-         $keyword = $this->input->post('keyword', true);
 
-         if (!empty($keyword)) {
 
-             $filter['keyword'] = $keyword;
 
-         }
 
-         $status = $this->input->post('status', true);
 
-         if (!empty($status)) {
 
-             $filter['status'] = $status;
 
-         }
 
-         $alarmtype = $this->input->post('alarmtype', true);
 
-         if (!empty($alarmtype)) {
 
-             $filter['alarmtype'] = $alarmtype;
 
-         }
 
-         // 记录用户选择的字段
 
-         if (!empty($field1)) {
 
-             $fieldData = array('userid'=>$userid,'lampfield'=>$field1);
 
-             $this->User_model->update_user_field($fieldData);
 
-         }
 
-         $lite = $this->input->post('lite', true);
 
-         $version = $this->session->userdata('version');
 
-         if ($lite == 1) {
 
-             if(empty($fields)){
 
-                 $fields = 'L.id, L.number';
 
-             }else{
 
-                 $fields .= ",L.updatetime";
 
-             }
 
-             $data['lamps'] = !empty($pid_arr) ? $this->Lamp_model->get_list_in('L.projectid', $pid_arr, $fields, $filter, $lite) : array();
 
-         } else {
 
-             if(empty($fields) || empty($field1)){
 
-                 $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,N.networkname as network_name,N.type as net_type,P.projectname as project_name,L.section,L.protocoltype as lampProtocoltype,L.loadtype,P.timezone as timezoneid,M.iccid';
 
-             }else{
 
-                 $fields .= ",L.id,L.projectid,L.section,L.protocoltype as lampProtocoltype,N.devstatus as net_devstatus,L.loadtype,P.timezone as timezoneid,M.iccid";
 
-             }
 
-             $download = intval($this->input->post('download',true));
 
-             if (!empty($download)) {
 
-                 unset($filter['count']);
 
-                 unset($filter['page']);
 
-             }
 
-             $data['lamps'] = !empty($pid_arr) ? $this->Lamp_model->get_list_in('L.projectid', $pid_arr, $fields, $filter) : array();
 
-             // var_dump(microtime());
 
-             $indexArr = [2=>'16',3=>'32',4=>'48',5=>'64'];
 
-             if (empty($version)) {
 
-                 $chargeStageMap = array(0=>'没有充电',1=>'启动充电',6=>'限流',7=>'充满',16=>'MPPT充电',32=>'均衡充电',48=>'提升充电',64=>'浮充',''=>'未知状态');
 
-                 $battstatusArr = array(0=>'空闲',1=>'放电',2=>'充电',3=>'一边充电一边放电',''=>'未知状态');
 
-                 $unknown = '未知状态';
 
-             }else{
 
-                 $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');
 
-                 $battstatusArr = array(0=>'Free',1=>'Discharge',2=>'Charge',3=>'Discharge while charging',''=>'Unknown state');
 
-                 $unknown = 'Unknown state';
 
-             }
 
-             if (!empty($download)) {
 
-                 $fieldArr = explode(',', $field1);
 
-                 $fieldArr[] = 'iccid';
 
-                 $temp = [];
 
-                 $i = 1;
 
-                 foreach ($data['lamps'] as $key => &$value) {
 
-                     $value['section'] = empty($value['section']) ? '' : $value['section'];
 
-                     if (isset($value['updatetime']) && $value['updatetime'] == '0000-00-00 00:00:00') {
 
-                         $value['updatetime'] = null;
 
-                     }
 
-                     if (array_key_exists('lamppower',$value)) {
 
-                         $value['lamppower']   = !empty($value['lamppower']) ? $value['lamppower'] : "0";
 
-                     }
 
-                     // 故障信息处理
 
-                     if (array_key_exists('isfaulted',$value)) {
 
-                         if ($value['isfaulted'] == 0) {
 
-                             if (empty($version)) {
 
-                                 $value['isfaulted'] = '无';
 
-                             }else{
 
-                                 $value['isfaulted'] = 'Nothing';
 
-                             }
 
-                             
 
-                         }else{
 
-                             $res = $this->Alarm_model->get_one_by_filter(array('lampid'=>$value['id'],'status'=>0),'stralarmtype');
 
-                             if (empty($version)) {
 
-                                 $stralarmtype = trim($res['stralarmtype']);
 
-                                 if (!empty($stralarmtype)) {
 
-                                     $alarmArr = explode(',', $stralarmtype);
 
-                                     $temp2 = array();
 
-                                     foreach ($alarmArr as $a) {
 
-                                         if ($a == '电池故障') {
 
-                                             $a = '电池电压异常';
 
-                                         }
 
-                                         $temp2[] = $a;
 
-                                     }
 
-                                     $value['isfaulted'] = implode(',', $temp2);
 
-                                 }else{
 
-                                     $value['isfaulted'] = '无';
 
-                                 }
 
-                             }else{
 
-                                 $stralarmtype = trim($res['stralarmtype']);
 
-                                 if (!empty($stralarmtype)) {
 
-                                     $alarmArr = explode(',', $stralarmtype);
 
-                                     $temp2 = array();
 
-                                     foreach ($alarmArr as $a) {
 
-                                         if ($a == '电池故障') {
 
-                                             $a = '电池电压异常';
 
-                                         }
 
-                                         if ($value['lampProtocoltype'] == 1) {
 
-                                             $temp[] = modbus_alarm_translate($a);
 
-                                         }else{
 
-                                             $temp[] = alarm_translate($a);
 
-                                         }
 
-                                     }
 
-                                     $value['isfaulted'] = implode(',', $temp2);
 
-                                 }else{
 
-                                     $value['isfaulted'] = 'Nothing';
 
-                                 }
 
-                                 
 
-                             }
 
-                         }
 
-                     }
 
-                     if (array_key_exists('lampstatus',$value)) {
 
-                         $value['lampstatus'] = !empty($value['lampstatus']) ? $value['lampstatus'] : "0";
 
-                     }
 
-                     if (array_key_exists('electricSOC',$value)) {
 
-                         $value['electricSOC'] = !empty($value['electricSOC']) ? $value['electricSOC'] : "0";
 
-                     }
 
-                     if (array_key_exists('battvoltage',$value)) {
 
-                         $value['battvoltage'] = !empty($value['battvoltage']) ? $value['battvoltage'] : "0";
 
-                     }
 
-                     if (array_key_exists('chargestage',$value)) {
 
-                         if ($value['lampProtocoltype'] == 1) {
 
-                             $value['chargestage'] = isset($indexArr[intval($value['chargestage'])]) ? $indexArr[intval($value['chargestage'])] : $value['chargestage'];
 
-                         }
 
-                         $value['chargestage'] = isset($value['chargestage']) ? $chargeStageMap[$value['chargestage']] : $unknown;
 
-                     }
 
-                     if (array_key_exists('battstatus',$value)) {
 
-                         $value['battstatus'] = isset($value['battstatus']) ? $battstatusArr[$value['battstatus']] : $unknown;
 
-                     }
 
-                     if (array_key_exists('overtimes',$value)) {
 
-                         $value['overtimes'] = !empty($value['overtimes']) ? $value['overtimes'] : 0;
 
-                     }
 
-                     if (array_key_exists('solarpower',$value)) {
 
-                         $value['solarpower'] = !empty($value['solarpower']) ? $value['solarpower'] : 0;
 
-                     }
 
-                     if (array_key_exists('iccid',$value)) {
 
-                         $value['iccid'] = !empty($value['iccid']) ? $value['iccid'] : '';
 
-                     }
 
-                     if (array_key_exists('updatetime',$value)) {
 
-                         $value['updatetime']   = !empty($value['updatetime']) ? $value['updatetime'] : '';
 
-                         // 通过时区修改时间
 
-                         if (!empty($value['updatetime'])) {
 
-                             $timezone = $this->Project_model->get_timezone_by_projectid($value['projectid']);
 
-                             if ($timezone['value'] != 8) {
 
-                                 $value['updatetime'] = date_change($value['updatetime'],8,$timezone['value']);
 
-                             }
 
-                         }
 
-                     }
 
-                     // 当前策略
 
-                     if (array_key_exists('policyid',$value)) {
 
-                         if (empty($value['policyid'])) {
 
-                             $value['policyid'] = '';
 
-                         }else{
 
-                             $policyData = $this->db->query('select name from policy_info where id = '.$value['policyid'])->row_array();
 
-                             if (empty($policyData) || empty($policyData['name'])) {
 
-                                 $value['policyid'] = '';
 
-                             }else{
 
-                                 $value['policyid'] = $policyData['name'];
 
-                             }
 
-                         }
 
-                     }
 
-                     
 
-                     $temp[$key][] = $i;
 
-                     foreach ($fieldArr as $f) {
 
-                         if ($f == 'lampstatus') {
 
-                             if (empty($version)) {
 
-                                 $temp[$key][] = empty($value[$f]) ? '关灯' : '开灯';
 
-                             }else{
 
-                                 $temp[$key][] = empty($value[$f]) ? 'off' : 'on';
 
-                             }
 
-                             continue;
 
-                         }
 
-                         if ($f == 'status') {
 
-                             if (empty($version)) {
 
-                                 $temp[$key][] = empty($value[$f]) ? '不在线' : '在线';
 
-                             }else{
 
-                                 $temp[$key][] = empty($value[$f]) ? 'off-line' : 'online';
 
-                             }
 
-                             continue;
 
-                         }
 
-                         if ($f == 'daychargemincurrent' || $f == 'daydischargemincurrent') {
 
-                             $t = floor($value[$f]/3600) < 10 ? '0'.floor($value[$f]/3600) : floor($value[$f]/3600);
 
-                             $m = ceil($value[$f]%3600/60) < 10 ? '0'.ceil($value[$f]%3600/60) : ceil($value[$f]%3600/60);
 
-                             $temp[$key][] = $t.':'.$m;
 
-                             continue;
 
-                         }
 
-                         $temp[$key][] = $value[$f];
 
-                     }
 
-                     $i ++;
 
-                 }
 
-                 $title1 = get_fields_by_type($field1,$version);
 
-                 if (empty($version)) {
 
-                     $title2 = array_merge(array('编号'),$title1);
 
-                 }else{
 
-                     $title2 = array_merge(array('Number'),$title1);
 
-                 }
 
-                 $title = [];
 
-                 foreach ($title2 as $t) {
 
-                     $title[0][] = $t;
 
-                 }
 
-                 $title[0][] = 'iccid';
 
-                 $data = array_merge($title,$temp);
 
-                 $path = push_excel($data,'lampinfoExcel_'.date('Ymd'));
 
-                 exit(json_result('0000', $this->response['0000'], array('path'=>$path)));
 
-             }
 
-             $timezone = null;
 
-             foreach ($data['lamps'] as $key => &$value) {
 
-                 $value['section'] = empty($value['section']) ? '' : $value['section'];
 
-                 if (isset($value['updatetime']) && $value['updatetime'] == '0000-00-00 00:00:00') {
 
-                     $value['updatetime'] = null;
 
-                 }
 
-                 if (array_key_exists('lamppower',$value)) {
 
-                     $value['lamppower']   = !empty($value['lamppower']) ? $value['lamppower'] : "0";
 
-                 }
 
-                 // 故障信息处理
 
-                 if (array_key_exists('isfaulted',$value)) {
 
-                     if ($value['isfaulted'] == 0) {
 
-                         if (empty($version)) {
 
-                             $value['isfaulted'] = '无';
 
-                         }else{
 
-                             $value['isfaulted'] = 'Nothing';
 
-                         }
 
-                         
 
-                     }else{
 
-                         $res = $this->Alarm_model->get_one_by_filter(array('lampid'=>$value['id'],'status'=>0),'stralarmtype');
 
-                         if (empty($version)) {
 
-                             // $value['isfaulted'] = empty($res['stralarmtype']) ? '无' : $res['stralarmtype'];
 
-                             $stralarmtype = trim($res['stralarmtype']);
 
-                             if (!empty($stralarmtype)) {
 
-                                 $alarmArr = explode(',', $stralarmtype);
 
-                                 $temp = array();
 
-                                 foreach ($alarmArr as $a) {
 
-                                     if ($a == '电池故障') {
 
-                                         $a = '电池电压异常';
 
-                                     }
 
-                                     $temp[] = $a;
 
-                                 }
 
-                                 $value['isfaulted'] = implode(',', $temp);
 
-                             }else{
 
-                                 $value['isfaulted'] = '无';
 
-                             }
 
-                         }else{
 
-                             $stralarmtype = trim($res['stralarmtype']);
 
-                             if (!empty($stralarmtype)) {
 
-                                 $alarmArr = explode(',', $stralarmtype);
 
-                                 $temp = array();
 
-                                 foreach ($alarmArr as $a) {
 
-                                     if ($a == '电池故障') {
 
-                                         $a = '电池电压异常';
 
-                                     }
 
-                                     if ($value['lampProtocoltype'] == 1) {
 
-                                         $temp[] = modbus_alarm_translate($a);
 
-                                     }else{
 
-                                         $temp[] = alarm_translate($a);
 
-                                     }
 
-                                     
 
-                                 }
 
-                                 $value['isfaulted'] = implode(',', $temp);
 
-                             }else{
 
-                                 $value['isfaulted'] = 'Nothing';
 
-                             }
 
-                             
 
-                         }
 
-                     }
 
-                     
 
-                 }
 
-                 if (array_key_exists('lampstatus',$value)) {
 
-                     $value['lampstatus'] = !empty($value['lampstatus']) ? $value['lampstatus'] : "0";
 
-                 }
 
-                 if (array_key_exists('electricSOC',$value)) {
 
-                     $value['electricSOC'] = !empty($value['electricSOC']) ? $value['electricSOC'] : "0";
 
-                 }
 
-                 if (array_key_exists('battvoltage',$value)) {
 
-                     $value['battvoltage'] = !empty($value['battvoltage']) ? $value['battvoltage'] : "0";
 
-                 }
 
-                 if (array_key_exists('chargestage',$value)) {
 
-                     if ($value['lampProtocoltype'] == 1) {
 
-                         $value['chargestage'] = isset($indexArr[intval($value['chargestage'])]) ? $indexArr[intval($value['chargestage'])] : $value['chargestage'];
 
-                     }
 
-                     $value['chargestage'] = isset($value['chargestage']) && isset($chargeStageMap[$value['chargestage']]) ? $chargeStageMap[$value['chargestage']] : $unknown;
 
-                 }
 
-                 if (array_key_exists('battstatus',$value)) {
 
-                     $value['battstatus'] = isset($value['battstatus']) ? $battstatusArr[$value['battstatus']] : $unknown;
 
-                 }
 
-                 if (array_key_exists('overtimes',$value)) {
 
-                     $value['overtimes'] = !empty($value['overtimes']) ? $value['overtimes'] : 0;
 
-                 }
 
-                 if (array_key_exists('solarpower',$value)) {
 
-                     $value['solarpower'] = !empty($value['solarpower']) ? $value['solarpower'] : 0;
 
-                 }
 
-                 // 当前策略
 
-                 if (array_key_exists('policyid',$value)) {
 
-                     if (empty($value['policyid'])) {
 
-                         $value['policyid'] = '';
 
-                     }else{
 
-                         $policyData = $this->db->query('select name from policy_info where id = '.$value['policyid'])->row_array();
 
-                         if (empty($policyData) || empty($policyData['name'])) {
 
-                             $value['policyid'] = '';
 
-                         }else{
 
-                             $value['policyid'] = $policyData['name'];
 
-                         }
 
-                     }
 
-                 }
 
-                 if (array_key_exists('updatetime',$value)) {
 
-                     $value['updatetime']   = !empty($value['updatetime']) ? $value['updatetime'] : '';
 
-                     // 通过时区修改时间
 
-                     if (!empty($value['updatetime'])) {
 
-                         // $value['timezoneid'];
 
-                         if($timezone === null) $timezone = $this->db->query('select value from timezone where id = '.$value['timezoneid'])->row_array();
 
-                         if ($timezone['value'] != 8) {
 
-                             $value['updatetime'] = date_change($value['updatetime'],8,$timezone['value']);
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-             if (!empty($project_id)) {
 
-                 $data['total_data']['install_num'] = $this->Lamp_model->getTotal(array('projectid'=>$project_id), $role, $companyid, $userid);
 
-                 $data['total_data']['light_num'] = $this->Lamp_model->get_light_count_by_project($project_id);
 
-                 $data['total_data']['online_num'] = $this->Lamp_model->get_online_count_by_project($project_id);
 
-                 $data['total_data']['fault_count'] = $this->Lamp_model->get_fault_count(array('L.projectid'=>$project_id,'L.isfaulted'=>1,'AI.status'=>0), $role, $companyid, $userid);
 
-                 $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;
 
-                 $data['total_data']['light_rate'] .= '%';
 
-                 $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;
 
-                 $data['total_data']['online_rate'] .= '%';
 
-                 $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;
 
-                 $data['total_data']['fault_rate'] .= '%';
 
-                 $data['total_data']['network_num'] = $this->Network_model->getTotalByProject($project_id);
 
-             }
 
-         }
 
-         unset($filter['page']);
 
-         unset($filter['count']);
 
-         $res = !empty($pid_arr) ? $this->Lamp_model->get_total('L.projectid', $pid_arr, 'L.id', $filter) : array();
 
-         $result = !empty($pid_arr) ? $this->Lamp_model->get_total('L.projectid', $pid_arr, 'L.id') : array();
 
-         $data['total'] = ceil($res/$count);
 
-         $data['count'] = $result;
 
-         $data['fault'] = $res;
 
-         exit(json_result('0000', $this->response['0000'], $data));
 
-     }
 
-     // 更新灯控信息
 
-     public function update() {
 
-         $role = $this->get_user_info('role');
 
-         $username = $this->input->post('username',true);
 
-         $client_key = $this->input->post('client_key',true);
 
-         $token = $this->input->post('token',true);
 
-         $type = intval($this->input->post('type',true));
 
-         $lamp_id = $this->input->post('lamp_id',true);
 
-         $clientid = $this->input->post('clientid',true);
 
-         $version = $this->session->userdata('version');
 
-         $lamp_id = $this->input->post('lamp_id', true);
 
-         if (empty($lamp_id)) {
 
-             exit(json_result('0411', $this->response['0411'], array()));
 
-         }
 
-         $ids = explode(',', $lamp_id);
 
-         $status = 1;
 
-         foreach ($ids as $v) {
 
-             $record['cmdtype'] = 0;
 
-             $record['relateid'] = $v;
 
-             $record['statuscmd'] = 0;
 
-             $record['updatetime'] = date("Y-m-d H:i:s");
 
-             $recordid = $this->Batmanage_model->get_record_id($record, 'batch_update_cmd');
 
-             if(!$recordid){
 
-                 exit(json_result('0009', $this->response['0009'], array()));
 
-             }
 
-             if ($recordid != -1) {
 
-                 $status = 0;
 
-                 $cmd = '{"cmd_type":"batch_update_cmd","cmd_id":'.$recordid.'}';
 
-                 $lampData = $this->Lamp_model->getOne($v,'N.status,N.protocoltype,L.protocoltype as lampprotocoltype');
 
-                 if ($lampData['protocoltype'] == 4 || $lampData['protocoltype'] == 6) {
 
-                     $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/update_asy';
 
-                     doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'lamp_id'=>$v]);
 
-                     continue;
 
-                 }
 
-                 if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
 
-                     $cmdret = send_cmd($cmd,1,0,$lampData['protocoltype']);
 
-                     if ($cmdret === false) {
 
-                         sleep(1);
 
-                         $cmdret = send_cmd($cmd,1,0,$lampData['protocoltype']);
 
-                     }
 
-                 }else{
 
-                     $cmdret = send_cmd($cmd,0,0,$lampData['protocoltype']);
 
-                     if ($cmdret === false) {
 
-                         sleep(1);
 
-                         $cmdret = send_cmd($cmd,0,0,$lampData['protocoltype']);
 
-                     }
 
-                 }
 
-                 if($cmdret === false){
 
-                     exit(json_result('0008', $this->response['0008'], array()));
 
-                 }
 
-             }
 
-         }
 
-         sleep(2);
 
-         if ($status) {
 
-             exit(json_result('0406', $this->response['0406'], array()));
 
-         }else{
 
-             exit(json_result('0407', $this->response['0407'], array()));
 
-         }
 
-     }
 
-     // 异步更新数据
 
-     public function update_asy(){
 
-         $lamp_id = $this->input->post('lamp_id',true);
 
-         $lampData = $this->Lamp_model->getOne($lamp_id,'L.number,N.protocoltype,L.protocoltype as lampprotocoltype,L.address,L.networkid,N.status,N.deviceid');
 
-         if ($lampData['lampprotocoltype'] == 0) {
 
-             $address = '';
 
-             if (mb_strlen($lampData['address']) >= 8) {
 
-                 $address = mb_substr($lampData['address'], -8);
 
-             }else{
 
-                 $address = '00000000';
 
-             }
 
-             $sendData = $address.'0300004646';
 
-             if ($lampData['protocoltype'] == 4) {
 
-                 $res = lampMqttCmd('/WE/TransIn/'.$lampData['address'],'/WE/TransOut/'.$lampData['address'],pack('H*','0001'.$sendData));
 
-             }else{
 
-                 // $cmd .= crc16(pack('H*',$sendData));
 
-                 device_cmd($lampData['deviceid'],pack('H*',$sendData));
 
-                 $cmd = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
 
-                 $info = send_lorawan_915($cmd);
 
-                 $res = array();
 
-                 if (!empty($info)) {
 
-                     $info = json_decode($info,true);
 
-                     if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
 
-                 }
 
-                 $file = fopen('./file/lorawan.txt', 'a+');
 
-                 fwrite($file, $res.'----'.date('Y-m-d H:i:s',time()).'
 
- ');
 
-                 fclose($file);
 
-                 // die;
 
-             }
 
-             
 
-             if (!empty($res)) {
 
-                 $msg1 = $res['msg'];
 
-                 // 数据校验
 
-                 $msg = mb_substr($msg1, 10);
 
-                 $t = 0;
 
-                 for ($i=0; $i < mb_strlen($msg) / 2 - 1; $i++) {
 
-                     $s = mb_substr($msg, $i*2,2);
 
-                     $t += base_convert($s, 16, 10);
 
-                 }
 
-                 $t = base_convert($t, 10, 16);
 
-                 if (mb_strlen($t) > 2) {
 
-                     $t = mb_substr($t, -2);
 
-                 }elseif($t <= 1){
 
-                     $t = '0'.$t;
 
-                 }
 
-                 if ($t == mb_substr($msg, -2)) {  
 
-                     // 校验通过,解析数据
 
-                     $updatetime = date('Y-m-d H:i:s',time());
 
-                     // 系统信息
 
-                     $systemInfoStr = mb_substr($msg1, 16,6);
 
-                     $systemInfo = array(
 
-                         'sysvoltage' => base_convert(mb_substr($systemInfoStr, 0,2), 16, 10),
 
-                         'syscurrent' => base_convert(mb_substr($systemInfoStr, 2,2), 16, 10),
 
-                         'temper' => bitDecode(mb_substr($systemInfoStr, 4,2)),
 
-                         'updatetime' => $updatetime,
 
-                         'lampid' => $lamp_id,
 
-                     );
 
-                     $this->db->insert('system_info_log', $systemInfo);
 
-                     // 路灯信息
 
-                     $lampInfoStr = mb_substr($msg1, 22,12);
 
-                     $lampInfo = array(
 
-                         'lampvoltage' => round(base_convert(mb_substr($lampInfoStr, 2,4), 16, 10)/10,1),
 
-                         'lampcurrent' => round(base_convert(mb_substr($lampInfoStr, 6,4), 16, 10)/100,2),
 
-                         'temper' => bitDecode(mb_substr($lampInfoStr, 10,2)),
 
-                         'updatetime' => $updatetime,
 
-                         'lampid' => $lamp_id,
 
-                     );
 
-                     $lampInfo['lamppower'] = round($lampInfo['lampvoltage'] * $lampInfo['lampcurrent'],2);
 
-                     $light = base_convert(mb_substr($lampInfoStr, 0,2), 16, 2);
 
-                     if (mb_strlen($light) == 8) {
 
-                         $lampInfo['lighteness'] = base_convert(mb_substr($light, -7), 2, 10);
 
-                         $lampInfo['lampstatus'] = 1;
 
-                     }else{
 
-                         $lampInfo['lighteness'] = 0;
 
-                         $lampInfo['lampstatus'] = 0;
 
-                     }
 
-                     $this->db->insert('lamp_info_log', $lampInfo);
 
-                     // 太阳能信息
 
-                     $solarInfoStr = mb_substr($msg1, 34,8);
 
-                     $solarInfo = array(
 
-                         'solarvoltage' => round(base_convert(mb_substr($solarInfoStr, 0,4), 16, 10)/10,1),
 
-                         'solarcurrent' => round(base_convert(mb_substr($solarInfoStr, 4,4), 16, 10)/100,2),
 
-                         'updatetime' => $updatetime,
 
-                         'lampid' => $lamp_id,
 
-                     );
 
-                     $solarInfo['solarpower'] = round($solarInfo['solarvoltage'] * $solarInfo['solarcurrent'],2);
 
-                     $this->db->insert('solar_info_log', $solarInfo);
 
-                     // 蓄电池信息
 
-                     $batterInfoStr = mb_substr($msg1, 42,70);
 
-                     $batteryInfo = array(
 
-                         'battvoltage' => round(base_convert(mb_substr($batterInfoStr, 2,4), 16, 10)/10,1),
 
-                         'chargecurrent' => round(base_convert(mb_substr($batterInfoStr, 6,4), 16, 10)/100,2),
 
-                         'discharcurrent' => round(base_convert(mb_substr($batterInfoStr, 10,4), 16, 10)/100,2),
 
-                         'batttemper' => bitDecode(mb_substr($batterInfoStr, 14,2)),
 
-                         'electrictotal' => base_convert(mb_substr($batterInfoStr, 16,4),16,10),
 
-                         'electricleft' => base_convert(mb_substr($batterInfoStr, 20,4),16,10),
 
-                         'electricSOC' => base_convert(mb_substr($batterInfoStr, 24,2),16,10),
 
-                         'voltagedaymin' => round(base_convert(mb_substr($batterInfoStr, 26,4),16,10)/10,1),
 
-                         'voltagedaymax' => round(base_convert(mb_substr($batterInfoStr, 30,4),16,10)/10,1),
 
-                         'daychargeah' => round(base_convert(mb_substr($batterInfoStr, 34,4),16,10)/10,1),
 
-                         'daydischarah' => round(base_convert(mb_substr($batterInfoStr, 38,4),16,10)/10,1),
 
-                         'daychargemaxpow' => round(base_convert(mb_substr($batterInfoStr, 42,4),16,10)/10,1),
 
-                         'daydischarmaxpow' => round(base_convert(mb_substr($batterInfoStr, 46,4),16,10)/10,1),
 
-                         'daychargemincurrent' => base_convert(mb_substr($batterInfoStr, 50,4),16,10),
 
-                         'daycharmaxcurrent' => round(base_convert(mb_substr($batterInfoStr, 54,4),16,10)/100,2),
 
-                         'daydischargemincurrent' => base_convert(mb_substr($batterInfoStr, 58,4),16,10),
 
-                         'daydischarmaxcurrent' => round(base_convert(mb_substr($batterInfoStr, 62,4),16,10)/100,2),
 
-                         'daybattmintemper' => bitDecode(mb_substr($batterInfoStr, 66,2)),
 
-                         'daybattmaxtemper' => bitDecode(mb_substr($batterInfoStr, 68,2)),
 
-                         'updatetime' => $updatetime,
 
-                         'lampid' => $lamp_id,
 
-                     );
 
-                     $batteryInfo['chargepower'] = round($batteryInfo['chargecurrent'] * $batteryInfo['battvoltage'],2);
 
-                     $batteryInfo['dischargepower'] = round($batteryInfo['discharcurrent'] * $batteryInfo['battvoltage'],2);
 
-                     $chargestage = base_convert(mb_substr($solarInfoStr, 0,2), 16, 2);
 
-                     $chargestage = mb_strlen($chargestage) < 8 ? mb_substr('00000000'.$chargestage, -8) : $chargestage;
 
-                     $batteryInfo['chargestage'] = base_convert(mb_substr($chargestage, 0,4).'0000', 2, 10);
 
-                     $batteryInfo['battstatus'] = base_convert(mb_substr($chargestage, -4), 2, 10);
 
-                     $this->db->insert('battery_info_log', $batteryInfo);
 
-                     // 发用电量信息
 
-                     $eleInfoStr = mb_substr($msg1, 112,24);
 
-                     $eleInfo = array(
 
-                         'daygeneration' => round(base_convert(mb_substr($eleInfoStr, 0,4),16,10)/10,1),
 
-                         'dayconsumption' => round(base_convert(mb_substr($eleInfoStr, 4,4),16,10)/10,1),
 
-                         'totalgeneration' => round(base_convert(mb_substr($eleInfoStr, 8,8),16,10)/10,1),
 
-                         'totalconsumption' => round(base_convert(mb_substr($eleInfoStr, 16,8),16,10)/10,1),
 
-                         'updatetime' => $updatetime,
 
-                         'lampid' => $lamp_id,
 
-                     );
 
-                     $this->db->insert('electric_info_log', $eleInfo);
 
-                     // 历史数据信息
 
-                     $historyInfoStr = mb_substr($msg1, 136,12);
 
-                     $historyInfo = array(
 
-                         'rundays' => base_convert(mb_substr($historyInfoStr, 0,4),16,10),
 
-                         'overtimes' => base_convert(mb_substr($historyInfoStr, 4,4),16,10),
 
-                         'fulltimes' => base_convert(mb_substr($historyInfoStr, 8,4),16,10),
 
-                         'updatetime' => $updatetime,
 
-                         'lampid' => $lamp_id,
 
-                     );
 
-                     $historyInfo['totalchargeah'] = $batteryInfo['daychargeah'];
 
-                     $historyInfo['totaldischarah'] = $batteryInfo['daydischarah'];
 
-                     $this->db->insert('history_info_log', $historyInfo);
 
-                     // 故障信息
 
-                     $alarmInfoStr = mb_substr($msg1, 148,8);
 
-                     $alarmInfo = array(
 
-                         'alarmtype' => 0,
 
-                         'stralarmtype' => '',
 
-                         'updatetime' => $updatetime,
 
-                         'lampid' => $lamp_id,
 
-                     );
 
-                     $alarmArr = array('0'=>'过放','1'=>'超压','2'=>'负载短路','3'=>'电池故障','4'=>'内部超温','5'=>'外部超温','7'=>'负载开路','8'=>'输出电容超压保护','10'=>'电池板短路','11'=>'电池板超压','13'=>'充电逆流','15'=>'锂电池低温关闭充电');
 
-                     $isfaulted = 0;
 
-                     if ($alarmInfoStr != '00000000') {
 
-                         $res = base_convert($alarmInfoStr, 16, 2);
 
-                         var_dump($res);
 
-                         $res = strrev($res);
 
-                         $t = array();
 
-                         for ($i=0; $i < mb_strlen($res); $i++) { 
 
-                             if (mb_substr($res, $i,1) == 1 && isset($alarmArr[$i])) {
 
-                                 $t[] = $alarmArr[$i];
 
-                             }
 
-                         }
 
-                         if (!empty($t)) {
 
-                             $alarmInfo['alarmtype'] = base_convert($alarmInfoStr, 16, 10);
 
-                             $alarmInfo['stralarmtype'] = implode(',', $t);
 
-                         }
 
-                     }
 
-                     if (!empty($alarmInfo['stralarmtype'])) {
 
-                         $this->db->insert('alarm_info_log', $alarmInfo);
 
-                         $isfaulted = 1;
 
-                     }
 
-                     $this->Lamp_model->update(array('id'=>$lamp_id),array('status'=>$lampInfo['lampstatus'],'lighteness'=>$lampInfo['lighteness'],'isfaulted'=>$isfaulted));
 
-                     if ($lampData['status'] == 0) $this->Network_model->update(array('id'=>$lampData['networkid']),array('status'=>1));
 
-                 }
 
-             }
 
-             
 
-         }elseif ($lampData['lampprotocoltype'] == 1) {
 
-             $sendData = '0103000a0002e409';
 
-             $res = lampMqttCmd('/WE/TransIn/'.$lampData['address'],'/WE/TransOut/'.$lampData['address'],pack('H*','0001'.$sendData));
 
-             if (!empty($res) && strtolower(crc16(pack('H*',mb_substr($res['msg'], 0,-4)))) == strtolower(mb_substr($res['msg'], -4))) {
 
-                 $sysInfoStr = $res['msg'];
 
-                 $sendData = '010300fd003115ee';
 
-                 $res = lampMqttCmd('/WE/TransIn/'.$lampData['address'],'/WE/TransOut/'.$lampData['address'],pack('H*','0001'.$sendData));
 
-                 if (!empty($res) && strtolower(crc16(pack('H*',mb_substr($res['msg'], 0,-4)))) == strtolower(mb_substr($res['msg'], -4))) {
 
-                     $data = array();
 
-                     $msg = $res['msg'];
 
-                     // 校验通过,解析数据
 
-                     $updatetime = date('Y-m-d H:i:s',time());
 
-                     // 系统信息
 
-                     $systemInfo = array(
 
-                         'sysvoltage' => base_convert(mb_substr($sysInfoStr, 6,2), 16, 10),
 
-                         'syscurrent' => base_convert(mb_substr($sysInfoStr, 8,2), 16, 10),
 
-                         'temper' => bitDecode(mb_substr($msg, 30,2)),
 
-                         'updatetime' => $updatetime,
 
-                         'lampid' => $lamp_id,
 
-                     );
 
-                     $this->db->insert('system_info_log', $systemInfo);
 
-                     // 路灯信息
 
-                     $lampInfoStr = mb_substr($msg, 34,12);
 
-                     $lampInfo = array(
 
-                         'lampvoltage' => round(base_convert(mb_substr($lampInfoStr, 0,4), 16, 10)/10,1),
 
-                         'lampcurrent' => round(base_convert(mb_substr($lampInfoStr, 4,4), 16, 10)/100,2),
 
-                         'updatetime' => $updatetime,
 
-                         'lampid' => $lamp_id,
 
-                         'lamppower' => base_convert(mb_substr($lampInfoStr, 8,4), 16, 10),
 
-                     );
 
-                     $light = base_convert(mb_substr($msg, 6,2), 16, 2);
 
-                     if (base_convert(mb_substr('00000000'.$light, -7), 2, 10) > 0) {
 
-                         $lampInfo['lighteness'] = base_convert(mb_substr($light, -7), 2, 10);
 
-                         $lampInfo['lampstatus'] = 1;
 
-                     }else{
 
-                         $lampInfo['lighteness'] = 0;
 
-                         $lampInfo['lampstatus'] = 0;
 
-                     }
 
-                     $this->db->insert('lamp_info_log', $lampInfo);
 
-                     // 太阳能信息
 
-                     $solarInfoStr = mb_substr($msg, 46,12);
 
-                     $solarInfo = array(
 
-                         'solarvoltage' => round(base_convert(mb_substr($solarInfoStr, 0,4), 16, 10)/10,1),
 
-                         'solarcurrent' => round(base_convert(mb_substr($solarInfoStr, 4,4), 16, 10)/100,2),
 
-                         'updatetime' => $updatetime,
 
-                         'lampid' => $lamp_id,
 
-                     );
 
-                     $solarInfo['solarpower'] = round($solarInfo['solarvoltage'] * $solarInfo['solarcurrent'],2);
 
-                     $this->db->insert('solar_info_log', $solarInfo);
 
-                     // 蓄电池信息
 
-                     // $batterInfoStr = mb_substr($msg, 42,70);
 
-                     $batteryInfo = array(
 
-                         'battvoltage' => round(base_convert(mb_substr($msg, 22,4), 16, 10)/10,1),
 
-                         'chargecurrent' => round(base_convert(mb_substr($msg, 26,4), 16, 10)/100,2),
 
-                         // 'discharcurrent' => round(base_convert(mb_substr($msg, 10,4), 16, 10)/100,2),
 
-                         'batttemper' => bitDecode(mb_substr($msg, 32,2)),
 
-                         // 'electrictotal' => base_convert(mb_substr($msg, 16,4),16,10),
 
-                         // 'electricleft' => base_convert(mb_substr($msg, 20,4),16,10),
 
-                         'electricSOC' => base_convert(mb_substr($msg, 18,4),16,10),
 
-                         'voltagedaymin' => round(base_convert(mb_substr($msg, 62,4),16,10)/10,1),
 
-                         'voltagedaymax' => round(base_convert(mb_substr($msg, 66,4),16,10)/10,1),
 
-                         'daychargeah' => base_convert(mb_substr($msg, 86,4),16,10),
 
-                         'daydischarah' => base_convert(mb_substr($msg, 90,4),16,10),
 
-                         'daychargemaxpow' => base_convert(mb_substr($msg, 78,4),16,10),
 
-                         'daydischarmaxpow' => base_convert(mb_substr($msg, 82,4),16,10),
 
-                         'daychargemincurrent' => base_convert(mb_substr($msg, 174,4),16,10),
 
-                         'daycharmaxcurrent' => round(base_convert(mb_substr($msg, 70,4),16,10)/100,2),
 
-                         'daydischargemincurrent' => base_convert(mb_substr($msg, 194,4),16,10),
 
-                         'daydischarmaxcurrent' => round(base_convert(mb_substr($msg, 74,4),16,10)/100,2),
 
-                         'daybattmintemper' => doubBitDecode(mb_substr($msg, 162,4)),
 
-                         'daybattmaxtemper' => doubBitDecode(mb_substr($msg, 158,4)),
 
-                         'updatetime' => $updatetime,
 
-                         'lampid' => $lamp_id,
 
-                     );
 
-                     $batteryInfo['chargepower'] = round($batteryInfo['chargecurrent'] * $batteryInfo['battvoltage'],2);
 
-                     // $batteryInfo['dischargepower'] = round($batteryInfo['discharcurrent'] * $batteryInfo['battvoltage'],2);
 
-                     $status = base_convert(mb_substr($msg, 8,2),16,10);
 
-                     if (mb_strlen($light) == 8 && mb_substr($light, 0,1) == 1) {
 
-                         $batteryInfo['battstatus'] = 1;
 
-                     }else{
 
-                         if ($status != 0){
 
-                             $batteryInfo['battstatus'] = 2;
 
-                         }else{
 
-                             $batteryInfo['battstatus'] = 0;
 
-                         }
 
-                     }
 
-                     $batteryInfo['chargestage'] = $status;
 
-                     $this->db->insert('battery_info_log', $batteryInfo);
 
-                     // 发用电量信息
 
-                     // $eleInfoStr = mb_substr($msg1, 112,24);
 
-                     $eleInfo = array(
 
-                         'daygeneration' => round(base_convert(mb_substr($msg, 94,4),16,10)/1000,3),
 
-                         'dayconsumption' => round(base_convert(mb_substr($msg, 98,4),16,10)/1000,3),
 
-                         'totalgeneration' => round(base_convert(mb_substr($msg, 134,8),16,10)/1000,3),
 
-                         'totalconsumption' => round(base_convert(mb_substr($msg, 142,8),16,10)/1000,3),
 
-                         'updatetime' => $updatetime,
 
-                         'lampid' => $lamp_id,
 
-                     );
 
-                     $this->db->insert('electric_info_log', $eleInfo);
 
-                     // 历史数据信息
 
-                     $historyInfoStr = mb_substr($msg, 102,28);
 
-                     $historyInfo = array(
 
-                         'rundays' => base_convert(mb_substr($historyInfoStr, 0,4),16,10),
 
-                         'overtimes' => base_convert(mb_substr($historyInfoStr, 4,4),16,10),
 
-                         'fulltimes' => base_convert(mb_substr($historyInfoStr, 8,4),16,10),
 
-                         'totalchargeah' => base_convert(mb_substr($historyInfoStr, 12,8),16,10),
 
-                         'totaldischarah' => base_convert(mb_substr($historyInfoStr, 20,8),16,10),
 
-                         'updatetime' => $updatetime,
 
-                         'lampid' => $lamp_id,
 
-                     );
 
-                     $this->db->insert('history_info_log', $historyInfo);
 
-                     // 故障信息
 
-                     $alarmInfoStr = mb_substr($msg, 10,8);
 
-                     $alarmInfo = array(
 
-                         'alarmtype' => 0,
 
-                         'stralarmtype' => '',
 
-                         'updatetime' => $updatetime,
 
-                         'lampid' => $lamp_id,
 
-                     );
 
-                     $alarmArr = array('10'=>'BMS过充保护','11'=>'电池低温保护,停止充电','12'=>'电池反接','13'=>'电容超压','14'=>'感应探头损坏','15'=>'负载开路','16'=>'蓄电池过放','17'=>'蓄电池超压','18'=>'欠压警告','19'=>'负载短路','20'=>'负载功率过大或负载过流','21'=>'控制器温度过高','22'=>'外部环境温度过高','23'=>'光伏输入功率过大','24'=>'光伏输入端短路','25'=>'光伏输入端超压','26'=>'太阳板逆流','27'=>'太阳板工作点超压','28'=>'太阳板反接');
 
-                     $isfaulted = 0;
 
-                     if ($alarmInfoStr != '00000000') {
 
-                         $res = base_convert($alarmInfoStr, 16, 2);
 
-                         var_dump($res);
 
-                         $res = strrev($res);
 
-                         $t = array();
 
-                         for ($i=0; $i < mb_strlen($res); $i++) { 
 
-                             if (mb_substr($res, $i,1) == 1 && isset($alarmArr[$i])) {
 
-                                 $t[] = $alarmArr[$i];
 
-                             }
 
-                         }
 
-                         if (!empty($t)) {
 
-                             $alarmInfo['alarmtype'] = base_convert($alarmInfoStr, 16, 10);
 
-                             $alarmInfo['stralarmtype'] = implode(',', $t);
 
-                         }
 
-                     }
 
-                     if (!empty($alarmInfo['stralarmtype'])) {
 
-                         $this->db->insert('alarm_info_log', $alarmInfo);
 
-                         $isfaulted = 1;
 
-                     }
 
-                     $this->Lamp_model->update(array('id'=>$lamp_id),array('status'=>$lampInfo['lampstatus'],'lighteness'=>$lampInfo['lighteness'],'isfaulted'=>$isfaulted));
 
-                     if ($lampData['status'] == 0) $this->Network_model->update(array('id'=>$lampData['networkid']),array('status'=>1));
 
-                 }
 
-             }
 
-             
 
-         }
 
-     }
 
-     // 灯控的开启/关闭(支持批量处理)
 
-     public function turnonoff(){
 
-         $username = $this->input->post('username',true);
 
-         $client_key = $this->input->post('client_key',true);
 
-         $token = $this->input->post('token',true);
 
-         $type = intval($this->input->post('type',true));
 
-         $lamp_id = $this->input->post('lamp_id',true);
 
-         $clientid = $this->input->post('clientid',true);
 
-         $version = $this->session->userdata('version');
 
-         $lampArr = explode(',', $lamp_id);
 
-         $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
 
-         $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/turnonoff_asy';
 
-         $time = 0;
 
-         if (count($lampList) > 1 && count($lampList) < 20){
 
-             $time = 60;
 
-         }elseif (count($lampList) >= 20 && count($lampList) < 40) {
 
-             $time = 120;
 
-         }elseif (count($lampList) >= 40 && count($lampList) < 60) {
 
-             $time = 180;
 
-         }elseif (count($lampList) >= 60 && count($lampList) < 80) {
 
-             $time = 240;
 
-         }elseif (count($lampList) >= 80) {
 
-             $time = 300;
 
-         }
 
-         foreach ($lampList as $l) {
 
-             if ($l['protocoltype'] == 0) {
 
-                 doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'type'=>$type,'lamp_id'=>$l['id']],30);
 
-             }else{
 
-                 doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'type'=>$type,'time'=>$time,'lamp_id'=>$l['id']]);
 
-                 usleep(100000);
 
-             }
 
-             // sleep(1);
 
-         }
 
-         
 
-         exit();
 
-     }
 
-     public function turnonoff_asy(){
 
-         set_time_limit(0);
 
-         $role = $this->get_user_info('role');
 
-         $lamp_id = intval($this->input->post('lamp_id',true));
 
-         $type = intval($this->input->post('type',true));
 
-         $mode = intval($this->input->post('mode',true));  
 
-         $clientid = $this->input->post('clientid',true);
 
-         $version = $this->input->post('version',true);
 
-         $time = $this->input->post('time',true);
 
-         $lampData = $this->Lamp_model->getOne($lamp_id,'L.number,N.protocoltype,N.deviceid,L.address,L.protocoltype as lampprotocoltype');
 
-         $data['cmdtype'] = 0;
 
-         $data['relateid'] = $lamp_id;
 
-         $data['statuscmd'] = $type;
 
-         $data['updatetime'] = date("Y-m-d H:i:s");
 
-         $recordid = $this->Batmanage_model->get_record_id($data, 'batch_switch_cmd');
 
-         if(!$recordid){
 
-             $data = ['code'=>'0009','status'=>2,'msg'=>$this->response['0009'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
 
-             exit(send_websocket($clientid,$data));
 
-         }
 
-         $cmd = '{"cmd_type":"batch_switch_cmd","cmd_id":'.$recordid.',"broadcast":'.$mode.'}';
 
-         if (($lampData['protocoltype'] == 4 || $lampData['protocoltype'] == 6) && $lampData['lampprotocoltype'] == 0) {
 
-             $address = '';
 
-             if (mb_strlen($lampData['address']) >= 8) {
 
-                 $address = mb_substr($lampData['address'], -8);
 
-             }else{
 
-                 $address = '00000000';
 
-             }
 
-             if ($type == 1) {
 
-                 $sendData = $address.'0603000104';
 
-             }else{
 
-                 $sendData = $address.'0603000003';
 
-             }
 
-             if ($lampData['protocoltype'] == 4) {
 
-                 $res = lampMqttCmd('/WE/TransIn/'.$lampData['address'],'/WE/TransOut/'.$lampData['address'],pack('H*','0001'.$sendData));
 
-             }else{
 
-                 device_cmd($lampData['deviceid'],pack('H*',$sendData));
 
-                 $cmd = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
 
-                 $info = send_lorawan_915($cmd);
 
-                 $res = array();
 
-                 if (!empty($info)) {
 
-                     $info = json_decode($info,true);
 
-                     if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
 
-                 }
 
-             }
 
-             if (empty($res)) {
 
-                 $data = ['code'=>'0016','status'=>2,'msg'=>$this->response['0016'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
 
-                 exit(send_websocket($clientid,$data));
 
-             }else{
 
-                 if (strtolower($res['msg']) == strtolower($sendData)) {
 
-                     $data = ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
 
-                     if ($type == 1) {
 
-                         $this->Lamp_model->update(array('id'=>$lampData['id']),array('status'=>1,'lighteness'=>100));
 
-                         $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$lamp_id),'P.projectname,N.networkname,L.number,L.address');
 
-                         $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯开灯",0);
 
-                         $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);
 
-                     }else{
 
-                         $this->Lamp_model->update(array('id'=>$lampData['id']),array('status'=>0,'lighteness'=>0));
 
-                         $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$lamp_id),'P.projectname,N.networkname,L.number,L.address');
 
-                         $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯关灯",0);
 
-                         $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);
 
-                     }
 
-                     exit(send_websocket($clientid,$data));
 
-                 }else{
 
-                     $data = ['code'=>'0010','status'=>2,'msg'=>$this->response['0010'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
 
-                     exit(send_websocket($clientid,$data));
 
-                 }
 
-             }
 
-             
 
-         }elseif ($lampData['protocoltype'] == 4 && $lampData['lampprotocoltype'] == 1) {
 
-             if ($type == 1) { // 开灯  0110df0a00020400640258eea0
 
-                 $lightness = 100;
 
-             }else{ // 关灯  0110df0a00020400000258af7f
 
-                 $lightness = 0;
 
-             }
 
-             $sendData = '0110df0a000204';
 
-             $lightness = empty($lightness) ? '0000' : '0000'.base_convert($lightness, 10, 16);
 
-             $sendData .= mb_strlen($lightness) == 4 ? $lightness : substr($lightness, -4);
 
-             $sendData .= mb_substr('0000'.base_convert(600, 10, 16), -4);
 
-             $sendData .= crc16(pack('H*',$sendData));
 
-             $file = fopen('./file/light.txt', 'a+');
 
-             fwrite($file, $sendData.'---'.date('Y-m-d H:i:s').'
 
- ');
 
-             fclose($file);
 
-             $res = lampMqttCmd('/WE/TransIn/'.$lampData['address'],'/WE/TransOut/'.$lampData['address'],pack('H*','0001'.$sendData));
 
-             if (empty($res)) {
 
-                 $data = ['code'=>'0016','status'=>2,'msg'=>$this->response['0016'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
 
-                 exit(send_websocket($clientid,$data));
 
-             }else{
 
-                 if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'], -4))) {
 
-                     $data = ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
 
-                     if ($type == 1) {
 
-                         $this->Lamp_model->update(array('id'=>$lampData['id']),array('status'=>1,'lighteness'=>100));
 
-                         $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$lamp_id),'P.projectname,N.networkname,L.number,L.address');
 
-                         $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯开灯",0);
 
-                         $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);
 
-                     }else{
 
-                         $this->Lamp_model->update(array('id'=>$lampData['id']),array('status'=>0,'lighteness'=>0));
 
-                         $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$lamp_id),'P.projectname,N.networkname,L.number,L.address');
 
-                         $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯关灯",0);
 
-                         $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);
 
-                     }
 
-                     exit(send_websocket($clientid,$data));
 
-                 }else{
 
-                     $data = ['code'=>'0010','status'=>2,'msg'=>$this->response['0010'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
 
-                     exit(send_websocket($clientid,$data));
 
-                 }
 
-             }
 
-         }elseif($lampData['protocoltype'] == 6 && $lampData['lampprotocoltype'] == 1){
 
-             if ($type == 1) { // 开灯
 
-                 $lightness = 100;
 
-             }else{ // 关灯
 
-                 $lightness = 0;
 
-             }
 
-             //网络协议类型 loraWAN915  //1 ModBus
 
-             $light_16 = doubBitSubStr(base_convert($lightness, 10, 16));
 
-             $time = doubBitSubStr(base_convert(3600, 10, 16));
 
-            // $cmd='0110DF0A000204'.$light_16.$time;
 
-             $cmd = '0110DF0A000204'.$light_16.$time;
 
-             $cmd .= crc16(pack('H*',$cmd));
 
-             device_cmd($lampData['deviceid'],pack('H*',$cmd));
 
-             $cmd = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
 
-             $res=send_lorawan_915($cmd);
 
-             if($res){
 
-                 $res=json_decode($res,true);
 
-                 if($res['info']=='success'){
 
-                     $lowanData['lighteness']=$lightness;
 
-                     if($lightness > 0){
 
-                         $lowanData['status']=1;
 
-                     }else{
 
-                         $lowanData['status']=0;
 
-                     }
 
-                     $this->Lamp_model->update(array('id'=>$lampData['id']),$lowanData);
 
-                     $dataSocket= ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-                     exit(send_websocket($clientid,$dataSocket));
 
-                 }else{
 
-                     $dataSocket = ['code'=>'0010','status'=>2,'msg'=>$this->response['0010'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-                     exit(send_websocket($clientid,$dataSocket));
 
-                 }
 
-             }
 
-         }
 
-         if (empty($time)) {
 
-             if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
 
-                 $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
 
-                 if ($cmdret === false) {
 
-                     sleep(1);
 
-                     $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
 
-                 }
 
-             }else{
 
-                 $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
 
-                 if ($cmdret === false) {
 
-                     sleep(1);
 
-                     $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
 
-                 }
 
-             }
 
-         }else{
 
-             if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
 
-                 $cmdret = send_cmd($cmd,1,$time,$lampData['protocoltype']);
 
-                 if ($cmdret === false) {
 
-                     sleep(1);
 
-                     $cmdret = send_cmd($cmd,1,$time,$lampData['protocoltype']);
 
-                 }
 
-             }else{
 
-                 $cmdret = send_cmd($cmd,0,$time,$lampData['protocoltype']);
 
-                 if ($cmdret === false) {
 
-                     sleep(1);
 
-                     $cmdret = send_cmd($cmd,0,$time,$lampData['protocoltype']);
 
-                 }
 
-             }
 
-         }
 
-         if (empty($cmdret)) {
 
-             $data = ['code'=>'0016','status'=>2,'msg'=>$this->response['0016'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
 
-             exit(send_websocket($clientid,$data));
 
-         }
 
-         if($cmdret === false){
 
-             $data = ['code'=>'0008','status'=>2,'msg'=>$this->response['0008'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
 
-             exit(send_websocket($clientid,$data));
 
-         } else {
 
-             $res = json_decode($cmdret, true);
 
-         }
 
-         if (isset($res['result']) && $res['result'] == true) {
 
-             $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$lamp_id),'P.projectname,N.networkname,L.number,L.address');
 
-             if ($type == 1) { // 开灯
 
-                 $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯开灯",0);
 
-                 $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);
 
-             }else{ // 关灯
 
-                 $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯关灯",0);
 
-                 $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);
 
-             }
 
-             $data = ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
 
-             exit(send_websocket($clientid,$data));
 
-         } else {
 
-             $version = $this->input->post('version',true);
 
-             if (empty($version)) {
 
-                 $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
 
-                 $data = ['code'=>'0010','status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
 
-                 exit(send_websocket($clientid,$data));
 
-             }else{
 
-                 $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
 
-                 $data = ['code'=>'other','status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
 
-                 exit(send_websocket($clientid,$data));
 
-             }
 
-         }
 
-     }
 
-     // 灯控调光
 
-     public function dimming(){
 
-         $username = $this->input->post('username',true);
 
-         $client_key = $this->input->post('client_key',true);
 
-         $token = $this->input->post('token',true);
 
-         $type = intval($this->input->post('type',true));
 
-         $lamp_id = $this->input->post('lamp_id',true);
 
-         $lightness = $this->input->post('lightness',true);
 
-         $clientid = $this->input->post('clientid',true);
 
-         $version = $this->session->userdata('version');
 
-         $lampArr = explode(',', $lamp_id);
 
-         $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
 
-         $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/dimming_asy';
 
-         $time = 0;
 
-         if (count($lampList) > 1 && count($lampList) < 20){
 
-             $time = 60;
 
-         }elseif (count($lampList) >= 20 && count($lampList) < 40) {
 
-             $time = 120;
 
-         }elseif (count($lampList) >= 40 && count($lampList) < 60) {
 
-             $time = 180;
 
-         }elseif (count($lampList) >= 60 && count($lampList) < 80) {
 
-             $time = 240;
 
-         }elseif (count($lampList) >= 80) {
 
-             $time = 300;
 
-         }
 
-         foreach ($lampList as $l) {
 
-             if ($l['protocoltype'] == 0) {
 
-                 doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'type'=>$type,'lamp_id'=>$l['id'],'lightness'=>$lightness,'time'=>$time],30);
 
-             }else{
 
-                 doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'type'=>$type,'lamp_id'=>$l['id'],'lightness'=>$lightness,'time'=>$time]);
 
-                 usleep(100000);
 
-             }
 
-             
 
-         }
 
-         
 
-         exit();
 
-     }
 
-     public function dimming_asy(){
 
-         set_time_limit(0);
 
-         $role = $this->get_user_info('role');
 
-         $lamp_id = intval($this->input->post('lamp_id',true));
 
-         $type = intval($this->input->post('type',true));
 
-         $lightness = intval($this->input->post('lightness',true));
 
-         $mode = intval($this->input->post('mode',true));
 
-         $clientid = $this->input->post('clientid',true);
 
-         $version = $this->input->post('version',true);
 
-         $time = $this->input->post('time',true);
 
-         if (empty($lamp_id)) {
 
-             exit(json_result('0400', $this->response['0400'], array()));
 
-         }
 
-         $lampData = $this->Lamp_model->getOne($lamp_id,'L.number,N.protocoltype,N.deviceid,L.address,L.protocoltype as lampprotocoltype');
 
-         if ($lampData['protocoltype'] == 4 && $lampData['lampprotocoltype'] == 0) {
 
-             $address = '';
 
-             if (mb_strlen($lampData['address']) >= 8) {
 
-                 $address = mb_substr($lampData['address'], -8);
 
-             }else{
 
-                 $address = '00000000';
 
-             }
 
-             $sum = 3 + 4 + $lightness;
 
-             $sum = base_convert($sum, 10, 16).'';
 
-             if (mb_strlen($sum) > 2) {
 
-                 $sum = mb_substr($sum, -2);
 
-             }elseif (mb_strlen($sum) <= 1) {
 
-                 $sum = '0'.$sum;
 
-             }
 
-             $lightness2 = base_convert($lightness, 10, 16).'';
 
-             if (mb_strlen($lightness2) == 1) {
 
-                 $lightness2 = '0'.$lightness2;
 
-             }
 
-             $sendData = $address.'060304'.$lightness2.$sum;
 
-             $res = lampMqttCmd('/WE/TransIn/'.$lampData['address'],'/WE/TransOut/'.$lampData['address'],pack('H*','0001'.$sendData));
 
-             if (empty($res)) {
 
-                 $data = ['code'=>'0016','status'=>2,'msg'=>$this->response['0016'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-                 exit(send_websocket($clientid,$data));
 
-             }else{
 
-                 if ($res['msg'] == $sendData) {
 
-                     $data = ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-                     if ($lightness != 0) {
 
-                         $this->Lamp_model->update(array('id'=>$lampData['id']),array('status'=>1,'lighteness'=>$lightness));
 
-                     }else{
 
-                         $this->Lamp_model->update(array('id'=>$lampData['id']),array('status'=>0,'lighteness'=>0));
 
-                     }
 
-                     $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$lamp_id),'P.projectname,N.networkname,L.number,L.address');
 
-                     $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯调光\"{$lightness}%\"",0);
 
-                     $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);
 
-                     exit(send_websocket($clientid,$data));
 
-                 }else{
 
-                     $data = ['code'=>'0010','status'=>2,'msg'=>$this->response['0010'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-                     exit(send_websocket($clientid,$data));
 
-                 }
 
-             }
 
-             
 
-         }elseif ($lampData['protocoltype'] == 4 && $lampData['lampprotocoltype'] == 1) {
 
-             
 
-             $sendData = '0110df0a000204';
 
-             $lightness2 = empty($lightness) ? '0000' : '0000'.base_convert($lightness, 10, 16);
 
-             $sendData .= strlen($lightness2) == 4 ? $lightness2 : substr($lightness2, -4);
 
-             $sendData .= mb_substr('0000'.base_convert(600, 10, 16), -4);
 
-             
 
-             $sendData .= crc16(pack('H*',$sendData));
 
-             $res = lampMqttCmd('/WE/TransIn/'.$lampData['address'],'/WE/TransOut/'.$lampData['address'],pack('H*','0001'.$sendData));
 
-             if (empty($res)) {
 
-                 $data = ['code'=>'0016','status'=>2,'msg'=>$this->response['0016'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-                 exit(send_websocket($clientid,$data));
 
-             }else{
 
-                 if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'], -4))) {
 
-                     $data = ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-                     if ($lightness != 0) {
 
-                         $this->Lamp_model->update(array('id'=>$lampData['id']),array('status'=>1,'lighteness'=>$lightness));
 
-                     }else{
 
-                         $this->Lamp_model->update(array('id'=>$lampData['id']),array('status'=>0,'lighteness'=>0));
 
-                     }
 
-                     $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$lamp_id),'P.projectname,N.networkname,L.number,L.address');
 
-                     $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯调光\"{$lightness}%\"",0);
 
-                     $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);
 
-                     exit(send_websocket($clientid,$data));
 
-                 }else{
 
-                     $data = ['code'=>'0010','status'=>2,'msg'=>$this->response['0010'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-                     exit(send_websocket($clientid,$data));
 
-                 }
 
-             }
 
-         }elseif($lampData['protocoltype'] == 6 && $lampData['lampprotocoltype'] == 1){
 
-             $light_16 = doubBitSubStr(base_convert($lightness, 10, 16));
 
-             $time = doubBitSubStr(base_convert(3600, 10, 16));
 
-             //$cmd = '0110DF0A02'.$light_16.$time;
 
-             $cmd='0110DF0A000204'.$light_16.$time;
 
-             $cmd .= crc16(pack('H*',$cmd));
 
-             device_cmd($lampData['deviceid'],pack('H*',$cmd));
 
-             $cmd = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
 
-            $res=send_lorawan_915($cmd);
 
-            if($res){
 
-             $file = fopen('./file/light.txt', 'a+');
 
-         fwrite($file, date('Y-m-d H:i:s').'----'.$res.'
 
- ');
 
-         fclose($file);
 
-                $res=json_decode($res,true);
 
-                if($res['info']=='success'){
 
-                    $lowanData['lighteness']=$lightness;
 
-                    if($lightness > 0){
 
-                        $lowanData['status']=1;
 
-                    }else{
 
-                        $lowanData['status']=0;
 
-                    }
 
-                    $this->Lamp_model->update(array('id'=>$lampData['id']),$lowanData);
 
-                    $dataSocket= ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-                    exit(send_websocket($clientid,$dataSocket));
 
-                }else{
 
-                    $dataSocket = ['code'=>'0010','status'=>2,'msg'=>$this->response['0010'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-                    exit(send_websocket($clientid,$dataSocket));
 
-                }
 
-            }
 
-            $dataSocket = ['code'=>'0010','status'=>2,'msg'=>$this->response['0010'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-             exit(send_websocket($clientid,$dataSocket));
 
-         }elseif($lampData['protocoltype'] == 6 && $lampData['lampprotocoltype'] == 0){
 
-             // $light_16 = doubBitSubStr(base_convert($lightness, 10, 16));
 
-             // $time = doubBitSubStr(base_convert(3600, 10, 16));
 
-             // //$cmd = '0110DF0A02'.$light_16.$time;
 
-             // $cmd='0110DF0A000204'.$light_16.$time;
 
-             // $cmd .= crc16(pack('H*',$cmd));
 
-             $address = '';
 
-             if (mb_strlen($lampData['address']) >= 8) {
 
-                 $address = mb_substr($lampData['address'], -8);
 
-             }else{
 
-                 $address = '00000000';
 
-             }
 
-             $sum = 3 + 4 + $lightness;
 
-             $sum = base_convert($sum, 10, 16).'';
 
-             if (mb_strlen($sum) > 2) {
 
-                 $sum = mb_substr($sum, -2);
 
-             }elseif (mb_strlen($sum) <= 1) {
 
-                 $sum = '0'.$sum;
 
-             }
 
-             $lightness2 = base_convert($lightness, 10, 16).'';
 
-             if (mb_strlen($lightness2) == 1) {
 
-                 $lightness2 = '0'.$lightness2;
 
-             }
 
-             $sendData = $address.'060304'.$lightness2.$sum;
 
-             $file = fopen('./file/cmd.txt', 'a+');
 
-         fwrite($file, date('Y-m-d H:i:s').'----'.$sendData.'
 
- ');
 
-         fclose($file);
 
-             device_cmd($lampData['deviceid'],pack('H*',$sendData));
 
-             $cmd = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
 
-             $res=send_lorawan_915($cmd);
 
-             if($res){
 
-                $res=json_decode($res,true);
 
-                if($res['info']=='success'){
 
-                    $lowanData['lighteness']=$lightness;
 
-                    if($lightness > 0){
 
-                        $lowanData['status']=1;
 
-                    }else{
 
-                        $lowanData['status']=0;
 
-                    }
 
-                    $this->Lamp_model->update(array('id'=>$lampData['id']),$lowanData);
 
-                    $dataSocket= ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-                    exit(send_websocket($clientid,$dataSocket));
 
-                }else{
 
-                    $dataSocket = ['code'=>'0010','status'=>2,'msg'=>$this->response['0010'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-                    exit(send_websocket($clientid,$dataSocket));
 
-                }
 
-             }
 
-             $dataSocket = ['code'=>'0010','status'=>2,'msg'=>$this->response['0010'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-             exit(send_websocket($clientid,$dataSocket));
 
-         }
 
-         $data['cmdtype'] = 0;
 
-         $data['relateid'] = $lamp_id;
 
-         $data['dimpercent'] = $lightness;
 
-         $data['updatetime'] = date("Y-m-d H:i:s");
 
-         $recordid = $this->Batmanage_model->get_record_id($data, 'batch_dim_cmd');
 
-         if(!$recordid){
 
-             $data = ['code'=>'0009','status'=>2,'msg'=>$this->response['0009'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-             exit(send_websocket($clientid,$data));
 
-         }
 
-         $cmd = '{"cmd_type":"batch_dim_cmd","cmd_id":'.$recordid.',"broadcast":'.$mode.'}';
 
-         // $cmdret = send_cmd($cmd);
 
-         if (empty($time)) {
 
-             if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
 
-                 $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
 
-                 if ($cmdret === false) {
 
-                     sleep(1);
 
-                     $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
 
-                 }
 
-             }else{
 
-                 $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
 
-                 if ($cmdret === false) {
 
-                     sleep(1);
 
-                     $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
 
-                 }
 
-             }
 
-         }else{
 
-             if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
 
-                 $cmdret = send_cmd($cmd,1,$time,$lampData['protocoltype']);
 
-                 if ($cmdret === false) {
 
-                     sleep(1);
 
-                     $cmdret = send_cmd($cmd,1,$time,$lampData['protocoltype']);
 
-                 }
 
-             }else{
 
-                 $cmdret = send_cmd($cmd,0,$time,$lampData['protocoltype']);
 
-                 if ($cmdret === false) {
 
-                     sleep(1);
 
-                     $cmdret = send_cmd($cmd,0,$time,$lampData['protocoltype']);
 
-                 }
 
-             }
 
-         }
 
-         
 
-         if (empty($cmdret)) {
 
-             $data = ['code'=>'0016','status'=>2,'msg'=>$this->response['0016'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-             exit(send_websocket($clientid,$data));
 
-         }
 
-         if($cmdret === false){
 
-             $data = ['code'=>'0008','status'=>2,'msg'=>$this->response['0008'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-             exit(send_websocket($clientid,$data));
 
-         } else {
 
-             $res = json_decode($cmdret, true);
 
-         }
 
-         if (isset($res['result']) && $res['result'] == true) {
 
-             $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$lamp_id),'P.projectname,N.networkname,L.number,L.address');
 
-             $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯调光\"{$lightness}%\"",0);
 
-             $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);
 
-             // exit(json_result('0000', $this->response['0000'], array('number'=>$lampData['number'])));
 
-             $data = ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-             exit(send_websocket($clientid,$data));
 
-         } else {
 
-             
 
-             if (empty($version)) {
 
-                 $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
 
-                 $data = ['code'=>'0010','status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-                 exit(send_websocket($clientid,$data));
 
-             }else{
 
-                 $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
 
-                 $data = ['code'=>'other','status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
 
-                 exit(send_websocket($clientid,$data));
 
-             }
 
-         }
 
-     }
 
-     // 删除灯控(支持批量处理)
 
-     public function del(){
 
-         $role = $this->get_user_info('role');
 
-         $lamp_ids = $this->input->post('lamp_ids',true);
 
-         if (empty($lamp_ids)) {
 
-             exit(json_result('0400', $this->response['0400'], array()));
 
-         }
 
-         $ids = explode(",", $lamp_ids);
 
-         // 获取灯控信息
 
-         $dataArr = $this->Lamp_model->getBatch($ids);
 
-         // 修改网络跟项目下灯控数量
 
-         foreach ($dataArr as $data) {
 
-             $data['faultcount'] = $data['isfaulted'] > 0 ? 1 : 0;
 
-             $this->Network_model->minus_lamp_count($data);
 
-             $this->Project_model->minus_lamp_count($data);
 
-             $netData = $this->Network_model->getOne($data['networkid'],'gatewaytype,protocoltype');
 
-             if ($netData['protocoltype'] == 6) $this->remove_device('00000000'.$data['address']);
 
-             if (isset($netData) && $netData['gatewaytype'] == 'direct' && !empty($data['networkid'])) {// 直连设备
 
-                 // 删除网络
 
-                 // $this->Zone_model->delOne($data['networkid']);
 
-                 $this->Network_model->delData(array('id'=>$data['networkid']));
 
-                 $cmdstr = '{"cmd_type":"delete_network_cmd","cmd_id":'.$data['networkid'].'}';
 
-                 // send_cmd($cmdstr,0);
 
-                 if (isset($netData['protocoltype']) && $netData['protocoltype'] != 0) {
 
-                     $cmdret = send_cmd($cmdstr,1,0,$netData['protocoltype']);
 
-                     if ($cmdret === false) {
 
-                         sleep(1);
 
-                         $cmdret = send_cmd($cmdstr,1,0,$netData['protocoltype']);
 
-                     }
 
-                 }else{
 
-                     $cmdret = send_cmd($cmdstr,0,0,$netData['protocoltype']);
 
-                     if ($cmdret === false) {
 
-                         sleep(1);
 
-                         $cmdret = send_cmd($cmdstr,0,0,$netData['protocoltype']);
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         // 删除灯控告警信息
 
-         $this->Alarm_model->delBatch($ids, 'lampid');
 
-         
 
-         foreach ($ids as $v) {
 
-             $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$v),'P.projectname,N.networkname,L.number,L.address');
 
-             $this->add_operation_log('delete',"删除\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯",0);
 
-             $this->add_operation_log('delete',"Delete lamp:\"{$lampData['number']}\".Wireless module address:\"{$lampData['address']}\".Project name:\"{$lampData['projectname']}\".Network name:\"{$lampData['networkname']}\"",0,1);
 
-         }
 
-         $this->Lamp_model->delBatch($ids);
 
-         exit(json_result('0000', $this->response['0000'], array()));
 
-     }
 
-     // 默认巡检设置
 
-     private function patrol_set($lampid,$time = null){
 
-         if (isset($time) && !empty($time)) {
 
-             $where['id']                = $lampid;
 
-             $update['patroltype']       = 1;
 
-             $update['patrolinterval']   = $time;
 
-             $update['updatetime']       = date("Y-m-d H:i:s");
 
-             $update['nextquerytime']    = date("Y-m-d H:i:s");
 
-             $ret = $this->Patrol_model->getOne($where['id']);
 
-             if(empty($ret)){
 
-                 $update['id'] = $where['id'];
 
-                 $this->Patrol_model->insert($update);
 
-             } else {
 
-                 $this->Patrol_model->update($where,$update);
 
-             }
 
-             $res = $this->Lamp_model->getOne($lampid,'N.protocoltype,L.address,L.protocoltype AS lampprotocoltype');
 
-             if ($res['protocoltype'] == 4) {
 
-                 $sendData = '000101A501';;
 
-                 $sendData .= bitSubStr(base_convert(intval($time/5), 10, 16));
 
-                 lampMqttCmd('/WE/WriteIn/'.$res['address'],'/WE/WriteOut/'.$res['address'],pack('H*', $sendData),0);
 
-                 return;
 
-             }
 
-             if (isset($res['protocoltype']) && $res['protocoltype'] != 0) {
 
-                 $cmd = '{"cmd_type":"set_patrol_cmd","cmd_id":'.$where['id'].',"broadcast":0}';
 
-                 $cmdret = send_cmd($cmd,1, 0,$res['protocoltype']);
 
-                 // if ($cmdret === false) {
 
-                 //     sleep(1);
 
-                 //     $cmdret = send_cmd($cmd,1, 0,$res['protocoltype']);
 
-                 // }
 
-             }
 
-         }
 
-         
 
-         //$cmd = '{"cmd_type":"patrol_cmd","cmd_id":'.$where['id'].',"broadcast":0}';
 
-         //$cmdret = send_cmd($cmd, 1);
 
-     }
 
-     // 更新负载设置
 
-     public function set_load(){
 
-         $username = $this->input->post('username',true);
 
-         $client_key = $this->input->post('client_key',true);
 
-         $token = $this->input->post('token',true);
 
-         $lamp_id = $this->input->post('lamp_id',true);
 
-         $clientid = $this->input->post('clientid',true);
 
-         $version = $this->session->userdata('version');
 
-         $workmode         = $this->input->post('workmode',true);
 
-         $worktimefirst    = $this->input->post('worktimefirst',true);
 
-         $workpowerfirst   = $this->input->post('workpowerfirst',true);
 
-         $worktimesencond  = $this->input->post('worktimesencond',true);
 
-         $workpowersencond = $this->input->post('workpowersencond',true);
 
-         $worktimethird    = $this->input->post('worktimethird',true);
 
-         $workpowerthird   = $this->input->post('workpowerthird',true);
 
-         $worktimeforth    = $this->input->post('worktimeforth',true);
 
-         $workpowerforth   = $this->input->post('workpowerforth',true);
 
-         $voptically       = $this->input->post('voptically',true);
 
-         $delaytime        = $this->input->post('delaytime',true);
 
-         $ledloadcurrent   = $this->input->post('ledloadcurrent',true);
 
-         $powercmd         = $this->input->post('powercmd',true);
 
-         $switchfeature    = $this->input->post('switchfeature',true);
 
-         $lampArr = explode(',', $lamp_id);
 
-         $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
 
-         $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/set_load_asy';
 
-         foreach ($lampList as $l) {
 
-             if ($l['protocoltype'] == 0) {
 
-                 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);
 
-             }else{
 
-                 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]);
 
-             }
 
-             
 
-         }
 
-         exit();
 
-     }
 
-     //更新负载设置
 
-     public function set_load_asy(){
 
-         $res = $this->load_update();
 
-         $clientid = $this->input->post('clientid',true);
 
-         $lampid = $this->input->post('lamp_id',true);
 
-         $data = $this->Lamp_model->getData(array('id'=>$lampid),'projectid,networkid,address,number');
 
-         if ($res['msg'] == '0000') {
 
-             $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
 
-             $networkData = $this->Network_model->getData(array('id'=>$data['networkid']),'networkname');
 
-             
 
-             $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯负载参数");
 
-             $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);
 
-         }
 
-         if (empty($res['result'])) {
 
-             // exit(json_result($res['msg'],$res['msg'],array('number'=>$data['number'])));
 
-             $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
 
-             exit(send_websocket($clientid,$data));
 
-         }else{
 
-             // exit(json_result($res['msg'],$this->response[$res['msg']],array('number'=>$data['number'])));
 
-             $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$this->response[$res['msg']],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
 
-             exit(send_websocket($clientid,$data));
 
-         }
 
-     }
 
-     //更新负载设置
 
-     private function load_update(){
 
-         $where['relateid']          = $this->input->post('lamp_id',true);
 
-         $update['relateid']          = $this->input->post('lamp_id',true);
 
-         $update['workmode']         = $this->input->post('workmode',true);
 
-         $update['worktimefirst']    = $this->input->post('worktimefirst',true);
 
-         $update['workpowerfirst']   = $this->input->post('workpowerfirst',true);
 
-         $update['worktimesencond']  = $this->input->post('worktimesencond',true);
 
-         $update['workpowersencond'] = $this->input->post('workpowersencond',true);
 
-         $update['worktimethird']    = $this->input->post('worktimethird',true);
 
-         $update['workpowerthird']   = $this->input->post('workpowerthird',true);
 
-         $update['worktimeforth']    = $this->input->post('worktimeforth',true);
 
-         $update['workpowerforth']   = $this->input->post('workpowerforth',true);
 
-         $update['voptically']       = $this->input->post('voptically',true);
 
-         $update['delaytime']        = $this->input->post('delaytime',true);
 
-         $update['ledloadcurrent']   = $this->input->post('ledloadcurrent',true);
 
-         $update['powercmd']         = $this->input->post('powercmd',true);
 
-         $update['switchfeature']    = $this->input->post('switchfeature',true);
 
-         $update['updatetime']       = date("Y-m-d H:i:s");
 
-         return $this->load_set($where, $update, 0);
 
-     }
 
-     private function load_set($where,$update,$broadcast){
 
-         $ret = $this->Load_model->get_data_by_filter(['relateid'=>$update['relateid'],'cmdtype'=>0]);
 
-         if(empty($ret)){
 
-             // $update['id'] = $where['id'];
 
-             $id = $this->Load_model->insert($update);
 
-         } else {
 
-             $this->Load_model->update(['id'=>$ret['id']],$update);
 
-             $id = $ret['id'];
 
-         }
 
-         
 
-         $cmd = '{"cmd_type":"load_param_cmd","cmd_id":'.$id.',"broadcast":'.$broadcast.'}';
 
-         $lampProData = $this->Lamp_model->getOne($where['relateid'],'N.protocoltype,L.address,L.protocoltype AS lampprotocoltype,N.deviceid');
 
-         if (($lampProData['protocoltype'] == 4 || $lampProData['protocoltype'] == 6) && $lampProData['lampprotocoltype'] == 0) {
 
-             $address = '';
 
-             if (mb_strlen($lampProData['address']) >= 8) {
 
-                 $address = mb_substr($lampProData['address'], -8);
 
-             }else{
 
-                 $address = '00000000';
 
-             }
 
-             $sendData = '10040010';
 
-             $sendData .= bitSubStr(base_convert($update['workmode'], 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['worktimefirst'], 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['workpowerfirst'], 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['worktimesencond'], 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['workpowersencond'], 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['worktimethird'], 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['workpowerthird'], 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['worktimeforth'], 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['workpowerforth'], 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['delaytime'], 10, 16));
 
-             $sendData .= doubBitSubStr(base_convert(intval($update['voptically']*10), 10, 16));
 
-             $sendData .= doubBitSubStr(base_convert(intval($update['ledloadcurrent']), 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['powercmd'], 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['switchfeature'], 10, 16));
 
-             $sum = 0;
 
-             for ($i=0; $i < mb_strlen($sendData)/2; $i++) { 
 
-                 if ($i == 0) continue;
 
-                 $t = mb_substr($sendData, $i*2,2);
 
-                 $sum += base_convert($t, 16, 10);
 
-             }
 
-             $sum = base_convert($sum, 10, 16);
 
-             $sendData .= bitSubStr($sum);
 
-             $sendData = $address.$sendData;
 
-             
 
-             if ($lampProData['protocoltype'] == 4) {
 
-                 $res = lampMqttCmd('/WE/TransIn/'.$lampProData['address'],'/WE/TransOut/'.$lampProData['address'],pack('H*','0001'.$sendData));
 
-             }else{
 
-                 device_cmd($lampProData['deviceid'],pack('H*',$sendData));
 
-                 $cmd = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
 
-                 $info = send_lorawan_915($cmd);
 
-                 $res = array();
 
-                 if (!empty($info)) {
 
-                     $info = json_decode($info,true);
 
-                     if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
 
-                 }
 
-             }
 
-             
 
-             if (empty($res)) {
 
-                 return array('result'=>1,'msg'=>'0016');
 
-             }else{
 
-                 $msg1 = $res['msg'];
 
-                 // 数据校验
 
-                 $msg = mb_substr($msg1, 10);
 
-                 $t = 0;
 
-                 for ($i=0; $i < mb_strlen($msg) / 2 - 1; $i++) {
 
-                     $s = mb_substr($msg, $i*2,2);
 
-                     $t += base_convert($s, 16, 10);
 
-                 }
 
-                 $t = base_convert($t, 10, 16);
 
-                 if (mb_strlen($t) > 2) {
 
-                     $t = mb_substr($t, -2);
 
-                 }elseif($t <= 1){
 
-                     $t = '0'.$t;
 
-                 }
 
-                 if ($t == mb_substr($msg, -2)) {  
 
-                     if (mb_substr($msg1, -10,-2) == '10040010') {
 
-                         return array('result'=>1,'msg'=>'0000');
 
-                     }else{
 
-                         return array('result'=>1,'msg'=>'0010');
 
-                     }
 
-                 }else{
 
-                     return array('result'=>1,'msg'=>'0010');
 
-                 }
 
-             }
 
-         }
 
-         if (isset($lampProData['protocoltype']) && $lampProData['protocoltype'] != 0) {
 
-             $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
 
-             if ($cmdret === false) {
 
-                 sleep(1);
 
-                 $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
 
-             }
 
-         }else{
 
-             $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
 
-             if ($cmdret === false) {
 
-                 sleep(1);
 
-                 $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
 
-             }
 
-         }
 
-         
 
-         $res = array(); 
 
-         if (empty($cmdret)){
 
-             $res['result'] = 1;
 
-             $res['msg']    = '0016';
 
-             return $res;
 
-         }
 
-         
 
-         if($cmdret === false){
 
-             $res['result'] = 1;
 
-             $res['msg']    = '0008';
 
-         } else {
 
-             $res = json_decode($cmdret, true);
 
-             if ($res['result'] == false) {
 
-                 $res['result'] = 0;
 
-                 $version = $this->input->post('version',true);
 
-                 if (empty($version)) {
 
-                     $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
 
-                 }else{
 
-                     $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
 
-                 }
 
-             } else {
 
-                 $res['result'] = 1;
 
-                 $res['msg']    = '0000';
 
-             }
 
-         }
 
-         return $res;
 
-     }
 
-     // 新控制器更新负载设置
 
-     public function modbus_set_load(){
 
-         $data['username'] = $this->input->post('username',true);
 
-         $data['client_key'] = $this->input->post('client_key',true);
 
-         $data['token'] = $this->input->post('token',true);
 
-         $data['lamp_id'] = $this->input->post('lamp_id',true);
 
-         $data['clientid'] = $this->input->post('clientid',true);
 
-         $data['version'] = $this->session->userdata('version');
 
-         // $data['light_voltage']         = $this->input->post('light_voltage',true);
 
-         $data['intell_power']    = $this->input->post('intell_power',true);
 
-         $data['load_current']   = $this->input->post('load_current',true);
 
-         $data['light_delay']  = $this->input->post('light_delay',true);
 
-         $data['induction_delay'] = $this->input->post('induction_delay',true);
 
-         $data['first_light_time']    = $this->input->post('first_light_time',true);
 
-         $data['first_light_human_power']   = $this->input->post('first_light_human_power',true);
 
-         $data['first_light_unman_power']    = $this->input->post('first_light_unman_power',true);
 
-         $data['second_light_time']   = $this->input->post('second_light_time',true);
 
-         $data['second_light_human_power']       = $this->input->post('second_light_human_power',true);
 
-         $data['second_light_unman_power']        = $this->input->post('second_light_unman_power',true);
 
-         $data['third_light_time']   = $this->input->post('third_light_time',true);
 
-         $data['third_light_human_power']         = $this->input->post('third_light_human_power',true);
 
-         $data['third_light_unman_power']    = $this->input->post('third_light_unman_power',true);
 
-         $data['fourth_light_time']    = $this->input->post('fourth_light_time',true);
 
-         $data['fourth_light_human_power']    = $this->input->post('fourth_light_human_power',true);
 
-         $data['fourth_light_unman_power']    = $this->input->post('fourth_light_unman_power',true);
 
-         $data['fifth_light_time']    = $this->input->post('fifth_light_time',true);
 
-         $data['fifth_light_human_power']    = $this->input->post('fifth_light_human_power',true);
 
-         $data['fifth_light_unman_power']    = $this->input->post('fifth_light_unman_power',true);
 
-         $data['sixth_light_time']    = $this->input->post('sixth_light_time',true);
 
-         $data['sixth_light_human_power']    = $this->input->post('sixth_light_human_power',true);
 
-         $data['sixth_light_unman_power']    = $this->input->post('sixth_light_unman_power',true);
 
-         $data['seventh_light_time']    = $this->input->post('seventh_light_time',true);
 
-         $data['seventh_light_human_power']    = $this->input->post('seventh_light_human_power',true);
 
-         $data['seventh_light_unman_power']    = $this->input->post('seventh_light_unman_power',true);
 
-         $data['eighth_light_time']    = $this->input->post('eighth_light_time',true);
 
-         $data['eighth_light_human_power']    = $this->input->post('eighth_light_human_power',true);
 
-         $data['eighth_light_unman_power']    = $this->input->post('eighth_light_unman_power',true);
 
-         $data['ninth_light_time']    = $this->input->post('ninth_light_time',true);
 
-         $data['ninth_light_human_power']    = $this->input->post('ninth_light_human_power',true);
 
-         $data['ninth_light_unman_power']    = $this->input->post('ninth_light_unman_power',true);
 
-         $data['tenth_light_time']    = $this->input->post('tenth_light_time',true);
 
-         $data['tenth_light_human_power']    = $this->input->post('tenth_light_human_power',true);
 
-         $data['tenth_light_unman_power']    = $this->input->post('tenth_light_unman_power',true);
 
-         $data['begin_voltage']    = $this->input->post('begin_voltage',true);
 
-         $data['end_voltage']    = $this->input->post('end_voltage',true);
 
-         $data['end_current']    = $this->input->post('end_current',true);
 
-         foreach ($data as $key => $value) {
 
-             $data[$key] = urldecode($value);
 
-         }
 
-         $file = fopen('../upload/test.txt', 'a+');
 
-         fwrite($file, json_encode($data).'------'.date('Y-m-d H:i:s').'
 
- ');
 
-         fclose($file);
 
-         $lampArr = explode(',', $data['lamp_id']);
 
-         $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
 
-         $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/modbus_set_load_asy';
 
-         foreach ($lampList as $l) {
 
-             $data['lamp_id'] = $l['id'];
 
-             if ($l['protocoltype'] == 0) {
 
-                 doAsyncRequest($url,$data,30);
 
-             }else{
 
-                 doAsyncRequest($url,$data);
 
-             }
 
-             
 
-         }
 
-         exit();
 
-     }
 
-     // 新控制器更新负载设置
 
-     public function modbus_set_load_asy(){
 
-         $res = $this->modbus_load_update();
 
-         $clientid = $this->input->post('clientid',true);
 
-         $lampid = $this->input->post('lamp_id',true);
 
-         $data = $this->Lamp_model->getData(array('id'=>$lampid),'projectid,networkid,address,number');
 
-         $file = fopen('../upload/test.txt', 'a+');
 
-         fwrite($file, '66666------'.date('Y-m-d H:i:s').'
 
- ');
 
-         fclose($file);
 
-         if ($res['msg'] == '0000') {
 
-             $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
 
-             $networkData = $this->Network_model->getData(array('id'=>$data['networkid']),'networkname');
 
-             
 
-             $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯负载参数");
 
-             $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);
 
-         }
 
-         if (empty($res['result'])) {
 
-             // exit(json_result($res['msg'],$res['msg'],array('number'=>$data['number'])));
 
-             $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
 
-             $file = fopen('../upload/test.txt', 'a+');
 
-         fwrite($file, json_encode($data).'---'.$clientid.'---'.date('Y-m-d H:i:s').'
 
- ');
 
-         fclose($file);
 
-             exit(send_websocket($clientid,$data));
 
-         }else{
 
-             // exit(json_result($res['msg'],$this->response[$res['msg']],array('number'=>$data['number'])));
 
-             $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$this->response[$res['msg']],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
 
-             $file = fopen('../upload/test.txt', 'a+');
 
-         fwrite($file, json_encode($data).'---'.$clientid.'---'.date('Y-m-d H:i:s').'
 
- ');
 
-         fclose($file);
 
-             exit(send_websocket($clientid,$data));
 
-         }
 
-     }
 
-     private function format_date($date){
 
-         return intval(explode(':', $date)[0]) * 3600 + intval(explode(':', $date)[1]) * 60;
 
-     }
 
-     // 新控制器更新负载设置
 
-     private function modbus_load_update(){
 
-         $where['relateid']          = $this->input->post('lamp_id',true);
 
-         $data['relateid']          = $this->input->post('lamp_id',true);
 
-         // $data['light_voltage']         = $this->input->post('light_voltage',true);
 
-         $data['intell_power']    = $this->input->post('intell_power',true);
 
-         $data['load_current']   = $this->input->post('load_current',true);
 
-         $data['light_delay']  = $this->input->post('light_delay',true);
 
-         $data['induction_delay'] = $this->input->post('induction_delay',true);
 
-         $data['first_light_time']    = $this->input->post('first_light_time',true);
 
-         // $data['first_light_time']    = $data['first_light_time'] * 3600;
 
-         $data['first_light_time']    = $this->format_date($data['first_light_time']);
 
-         $data['first_light_human_power']   = $this->input->post('first_light_human_power',true);
 
-         $data['first_light_unman_power']    = $this->input->post('first_light_unman_power',true);
 
-         $data['second_light_time']   = $this->input->post('second_light_time',true);
 
-         // $data['second_light_time']    = $data['second_light_time'] * 3600;
 
-         $data['second_light_time']    = $this->format_date($data['second_light_time']);
 
-         $data['second_light_human_power']       = $this->input->post('second_light_human_power',true);
 
-         $data['second_light_unman_power']        = $this->input->post('second_light_unman_power',true);
 
-         $data['third_light_time']   = $this->input->post('third_light_time',true);
 
-         // $data['third_light_time']    = $data['third_light_time'] * 3600;
 
-         $data['third_light_time']    = $this->format_date($data['third_light_time']);
 
-         $data['third_light_human_power']         = $this->input->post('third_light_human_power',true);
 
-         $data['third_light_unman_power']    = $this->input->post('third_light_unman_power',true);
 
-         $data['fourth_light_time']    = $this->input->post('fourth_light_time',true);
 
-         // $data['fourth_light_time']    = $data['fourth_light_time'] * 3600;
 
-         $data['fourth_light_time']    = $this->format_date($data['fourth_light_time']);
 
-         $data['fourth_light_human_power']    = $this->input->post('fourth_light_human_power',true);
 
-         $data['fourth_light_unman_power']    = $this->input->post('fourth_light_unman_power',true);
 
-         $data['fifth_light_time']    = $this->input->post('fifth_light_time',true);
 
-         // $data['fifth_light_time']    = $data['fifth_light_time'] * 3600;
 
-         $data['fifth_light_time']    = $this->format_date($data['fifth_light_time']);
 
-         $data['fifth_light_human_power']    = $this->input->post('fifth_light_human_power',true);
 
-         $data['fifth_light_unman_power']    = $this->input->post('fifth_light_unman_power',true);
 
-         $data['sixth_light_time']    = $this->input->post('sixth_light_time',true);
 
-         // $data['sixth_light_time']    = $data['sixth_light_time'] * 3600;
 
-         $data['sixth_light_time']    = $this->format_date($data['sixth_light_time']);
 
-         $data['sixth_light_human_power']    = $this->input->post('sixth_light_human_power',true);
 
-         $data['sixth_light_unman_power']    = $this->input->post('sixth_light_unman_power',true);
 
-         $data['seventh_light_time']    = $this->input->post('seventh_light_time',true);
 
-         // $data['seventh_light_time']    = $data['seventh_light_time'] * 3600;
 
-         $data['seventh_light_time']    = $this->format_date($data['seventh_light_time']);
 
-         $data['seventh_light_human_power']    = $this->input->post('seventh_light_human_power',true);
 
-         $data['seventh_light_unman_power']    = $this->input->post('seventh_light_unman_power',true);
 
-         $data['eighth_light_time']    = $this->input->post('eighth_light_time',true);
 
-         // $data['eighth_light_time']    = $data['eighth_light_time'] * 3600;
 
-         $data['eighth_light_time']    = $this->format_date($data['eighth_light_time']);
 
-         $data['eighth_light_human_power']    = $this->input->post('eighth_light_human_power',true);
 
-         $data['eighth_light_unman_power']    = $this->input->post('eighth_light_unman_power',true);
 
-         $data['ninth_light_time']    = $this->input->post('ninth_light_time',true);
 
-         // $data['ninth_light_time']    = $data['ninth_light_time'] * 3600;
 
-         $data['ninth_light_time']    = $this->format_date($data['ninth_light_time']);
 
-         $data['ninth_light_human_power']    = $this->input->post('ninth_light_human_power',true);
 
-         $data['ninth_light_unman_power']    = $this->input->post('ninth_light_unman_power',true);
 
-         $data['tenth_light_time']    = $this->input->post('tenth_light_time',true);
 
-         // $data['tenth_light_time']    = $data['tenth_light_time'] * 3600;
 
-         $data['tenth_light_time']    = $this->format_date($data['tenth_light_time']);
 
-         $data['tenth_light_human_power']    = $this->input->post('tenth_light_human_power',true);
 
-         $data['tenth_light_unman_power']    = $this->input->post('tenth_light_unman_power',true);
 
-         $data['begin_voltage']    = $this->input->post('begin_voltage',true);
 
-         $data['end_voltage']    = $this->input->post('end_voltage',true);
 
-         $data['end_current']    = $this->input->post('end_current',true);
 
-         $data['updatetime']       = date("Y-m-d H:i:s");
 
-         $file = fopen('../upload/test.txt', 'a+');
 
-         fwrite($file, json_encode($data).'------'.date('Y-m-d H:i:s').'
 
- ');
 
-         fclose($file);
 
-         return $this->modbus_load_set($where, $data, 0);
 
-     }
 
-     private function modbus_load_set($where,$update,$broadcast){
 
-         $file = fopen('../upload/test.txt', 'a+');
 
-         fwrite($file, '11111------'.date('Y-m-d H:i:s').'
 
- ');
 
-         fclose($file);
 
-         $ret = $this->Modbus_load_model->get_data_by_filter(['relateid'=>$update['relateid'],'cmdtype'=>0]);
 
-         if(empty($ret)){
 
-             // $update['id'] = $where['id'];
 
-             $id = $this->Modbus_load_model->insert($update);
 
-         } else {
 
-             $this->Modbus_load_model->update(['id'=>$ret['id']],$update);
 
-             $id = $ret['id'];
 
-         }
 
-         $file = fopen('../upload/test.txt', 'a+');
 
-         fwrite($file, '22222------'.date('Y-m-d H:i:s').'
 
- ');
 
-         fclose($file);
 
-         $cmd = '{"cmd_type":"modbus_load_param_cmd","cmd_id":'.$id.',"broadcast":'.$broadcast.'}';
 
-         $lampProData = $this->Lamp_model->getOne($where['relateid'],'N.protocoltype,N.deviceid,L.protocoltype as lampprotocoltype,L.address');
 
-         if ($lampProData['protocoltype'] == 4 && $lampProData['lampprotocoltype'] == 1) {
 
-             $sendData = '0103e08d0027a23b';
 
-             
 
-             $res = lampMqttCmd('/WE/TransIn/'.$lampProData['address'],'/WE/TransOut/'.$lampProData['address'],pack('H*','0001'.$sendData));
 
-             if (empty($res)) {
 
-                 return array('result'=>1,'msg'=>'0016');
 
-             }else{
 
-                 if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
 
-                     $old = $res['msg'];
 
-                     $data = array();
 
-                     $sendData = '';
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['load_current']*100), 10, 16)).''; // 负载电流
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['intell_power']), 10, 16)).''; // 智能功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['light_delay']), 10,16)).''; // 光控延时
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['induction_delay']), 10,16)).''; // 感应延时
 
-                     $sendData .= substr($old, 22,4);
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['first_light_time']), 10,16)).''; // 第一段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['first_light_human_power']), 10,16)).''; // 第一段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['first_light_unman_power']), 10,16)).''; // 第一段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['second_light_time']), 10,16)).''; // 第二段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['second_light_human_power']), 10,16)).''; // 第二段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['second_light_unman_power']), 10,16)).''; // 第二段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['third_light_time']), 10,16)).''; // 第三段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['third_light_human_power']), 10,16)).''; // 第三段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['third_light_unman_power']), 10,16)).''; // 第三段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_time']), 10,16)).''; // 第四段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_human_power']), 10,16)).''; // 第四段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_unman_power']), 10,16)).''; // 第四段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_time']), 10,16)).''; // 第五段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_human_power']), 10,16)).''; // 第五段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_unman_power']), 10,16)).''; // 第五段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_time']), 10,16)).''; // 第六段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_human_power']), 10,16)).''; // 第六段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_unman_power']), 10,16)).''; // 第六段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_time']), 10,16)).''; // 第七段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_human_power']), 10,16)).''; // 第七段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_unman_power']), 10,16)).''; // 第七段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_time']), 10,16)).''; // 第八段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_human_power']), 10,16)).''; // 第八段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_unman_power']), 10,16)).''; // 第八段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_time']), 10,16)).''; // 第九段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_human_power']), 10,16)).''; // 第九段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_unman_power']), 10,16)).''; // 第九段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_time']), 10,16)).''; // 晨亮时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_human_power']), 10,16)).''; // 晨亮有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_unman_power']), 10,16)).''; // 晨亮无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['begin_voltage']*10), 10, 16)).''; // 智能功率USER1起始电压
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['end_voltage']*10), 10, 16)).''; // 智能功率USER1终止电压
 
-                     $sendData .= substr($old, 154,4);
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['end_current']*100), 10, 16)).''; // 智能功率USER1终止电流
 
-                     $len = doubBitSubStr(base_convert(intval(mb_strlen($sendData)/4), 10, 16)).bitSubStr(base_convert(intval(mb_strlen($sendData)/2), 10, 16));
 
-                     $sendData = '0110e08d'.$len.$sendData;
 
-                     $sendData .= crc16(pack('H*',$sendData));
 
-                     $res = lampMqttCmd('/WE/TransIn/'.$lampProData['address'],'/WE/TransOut/'.$lampProData['address'],pack('H*','0001'.$sendData));
 
-                     if (empty($res)) {
 
-                         return array('result'=>1,'msg'=>'0016');
 
-                     }else{
 
-                         if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
 
-                             return array('result'=>1,'msg'=>'0000');
 
-                         }else{
 
-                             return array('result'=>1,'msg'=>'0010');
 
-                         }
 
-                     }
 
-                 }else{
 
-                     return array('result'=>1,'msg'=>'0010');
 
-                 }
 
-             }
 
-             
 
-         }elseif ($lampProData['protocoltype'] == 6 && $lampProData['lampprotocoltype'] == 1) {
 
-             $cmdLine='0103e08d0027a23b';
 
-             //$cmdLine .= crc16(pack('H*',$cmdLine));
 
-             device_cmd($lampProData['deviceid'],pack('H*',$cmdLine));
 
-             $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
 
-             $res=send_lorawan_915($cmdLine);
 
-             if (empty($res)) {
 
-                 return array('result'=>1,'msg'=>'0016');
 
-             }else {
 
-                 $res=json_decode($res,true);
 
-                 $cmdRes=$res['resInfo'];
 
-                 if(strtolower(substr($cmdRes, 0,6)) == '01034e'){
 
-                     $old = $cmdRes;
 
-                     $data = array();
 
-                     $sendData = '';
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['load_current']*100), 10, 16)).''; // 负载电流
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['intell_power']), 10, 16)).''; // 智能功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['light_delay']), 10,16)).''; // 光控延时
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['induction_delay']), 10,16)).''; // 感应延时
 
-                     $sendData .= substr($old, 22,4);
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['first_light_time']), 10,16)).''; // 第一段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['first_light_human_power']), 10,16)).''; // 第一段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['first_light_unman_power']), 10,16)).''; // 第一段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['second_light_time']), 10,16)).''; // 第二段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['second_light_human_power']), 10,16)).''; // 第二段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['second_light_unman_power']), 10,16)).''; // 第二段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['third_light_time']), 10,16)).''; // 第三段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['third_light_human_power']), 10,16)).''; // 第三段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['third_light_unman_power']), 10,16)).''; // 第三段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_time']), 10,16)).''; // 第四段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_human_power']), 10,16)).''; // 第四段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_unman_power']), 10,16)).''; // 第四段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_time']), 10,16)).''; // 第五段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_human_power']), 10,16)).''; // 第五段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_unman_power']), 10,16)).''; // 第五段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_time']), 10,16)).''; // 第六段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_human_power']), 10,16)).''; // 第六段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_unman_power']), 10,16)).''; // 第六段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_time']), 10,16)).''; // 第七段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_human_power']), 10,16)).''; // 第七段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_unman_power']), 10,16)).''; // 第七段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_time']), 10,16)).''; // 第八段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_human_power']), 10,16)).''; // 第八段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_unman_power']), 10,16)).''; // 第八段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_time']), 10,16)).''; // 第九段亮灯时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_human_power']), 10,16)).''; // 第九段有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_unman_power']), 10,16)).''; // 第九段无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_time']), 10,16)).''; // 晨亮时间
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_human_power']), 10,16)).''; // 晨亮有人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_unman_power']), 10,16)).''; // 晨亮无人功率
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['begin_voltage']*10), 10, 16)).''; // 智能功率USER1起始电压
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['end_voltage']*10), 10, 16)).''; // 智能功率USER1终止电压
 
-                     $sendData .= substr($old, 154,4);
 
-                     $sendData .= doubBitSubStr(base_convert(intval($update['end_current']*100), 10, 16)).''; // 智能功率USER1终止电流
 
-                     $len = strlen($sendData)/4;
 
-                     $con1 = substr($sendData,0,20*4);
 
-                     $con2 = substr($sendData,20*4);
 
-                     $address1='e08d';
 
-                     $address2=substr('0000'.base_convert(base_convert($address1,16,10)+20,10,16),-4);
 
-                     $l1=substr('0000'.base_convert(20,10,16),-4);
 
-                     $l2=substr('00'.base_convert(40,10,16),-2);
 
-                     $cmd1='0110'.$address1.$l1.$l2.$con1;
 
-                     $cmd1 .=crc16(pack('H*',$cmd1));
 
-                     device_cmd($lampProData['deviceid'],pack('H*',$cmd1));
 
-                     $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
 
-                     $res=send_lorawan_915($cmdLine);
 
-                     $file = fopen('./file/loraWan_loader.txt', 'a+');
 
-                     fwrite($file, date('Y-m-d H:i:s').'----'.$res.' 
 
- ');
 
-                     fwrite($file, date('Y-m-d H:i:s').'----'.$len.' 
 
- ');
 
-                     fwrite($file, date('Y-m-d H:i:s').'----'.$sendData.' 
 
- ');
 
-                     fwrite($file, date('Y-m-d H:i:s').'----'.$cmd1.' 
 
- ');
 
-                     fclose($file);
 
-                     if (empty($res)) {
 
-                         return array('result'=>1,'msg'=>'0016');
 
-                     }else{
 
-                         $res = json_decode($res,true);
 
-                         if (!isset($res['resInfo']) || strtolower($res['resInfo']) != '0110e08d001467ed') return array('result'=>1,'msg'=>'0016');
 
-                         $l1=substr('0000'.base_convert(19,10,16),-4);
 
-                         $l2=substr('00'.base_convert(38,10,16),-2);
 
-                         $cmd2='0110'.$address2.$l1.$l2.$con2;
 
-                         $cmd2 .=crc16(pack('H*',$cmd2));
 
-                         device_cmd($lampProData['deviceid'],pack('H*',$cmd2));
 
-                         $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
 
-                         $res=send_lorawan_915($cmdLine);
 
-                         $file = fopen('./file/loraWan_loader.txt', 'a+');
 
-                         fwrite($file, date('Y-m-d H:i:s').'----'.$res.' 
 
- ');
 
-                         fclose($file);
 
-                         if (empty($res)) {
 
-                             return array('result'=>1,'msg'=>'0016');
 
-                         }else{
 
-                             $res = json_decode($res,true);
 
-                             if (!isset($res['resInfo']) || strtolower($res['resInfo']) != '0110e0a10013e7e6') return array('result'=>1,'msg'=>'0016');
 
-                             return array('result'=>1,'msg'=>'0000');
 
-                         }
 
-                     }
 
- //                    $cmdLine='0110e08d00274e'.$sendData;
 
- //                    $cmdLine .= crc16(pack('H*',$cmdLine));
 
- //                    device_cmd($lampProData['deviceid'],pack('H*',$cmdLine));
 
- //                    $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
 
- //                    $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
 
- //                    $res=send_lorawan_915($cmdLine);
 
- //                    $file = fopen('./file/loraWan_loader.txt', 'a+');
 
- //                    fwrite($file, date('Y-m-d H:i:s').'----'.$res.' ');
 
- //                    fclose($file);
 
- //                    if (empty($res)) {
 
- //                        return array('result'=>1,'msg'=>'0016');
 
- //                    }else {
 
- //                        $file = fopen('./file/loraWan_loader.txt', 'a+');
 
- //                        fwrite($file, date('Y-m-d H:i:s').'----'.$res.' ');
 
- //                        fclose($file);
 
- //                        $res=json_decode($res,true);
 
- //                        $cmdRes2=$res['resInfo'];
 
- //                        if(strtolower(substr($cmdRes2,0,6))=='01034e') {
 
- //                            return array('result'=>1,'msg'=>'0000');
 
- //                        }else{
 
- //                            return array('result'=>1,'msg'=>'0010');
 
- //                        }
 
- //                    }
 
-                 }else{
 
-                     return array('result'=>1,'msg'=>'0010');
 
-                 }
 
-             }
 
-         }
 
-         $file = fopen('../upload/test.txt', 'a+');
 
-         fwrite($file, '333333------'.date('Y-m-d H:i:s').'
 
- ');
 
-         fclose($file);
 
-         if (isset($lampProData['protocoltype']) && $lampProData['protocoltype'] != 0) {
 
-             $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
 
-             if ($cmdret === false) {
 
-                 sleep(1);
 
-                 $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
 
-             }
 
-         }else{
 
-             $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
 
-             if ($cmdret === false) {
 
-                 sleep(1);
 
-                 $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
 
-             }
 
-         }
 
-         $file = fopen('../upload/test.txt', 'a+');
 
-         fwrite($file, '44444------'.date('Y-m-d H:i:s').'
 
- ');
 
-         fclose($file);
 
-         $res = array(); 
 
-         if (empty($cmdret)){
 
-             $res['result'] = 1;
 
-             $res['msg']    = '0016';
 
-             return $res;
 
-         }
 
-         
 
-         if($cmdret === false){
 
-             $res['result'] = 1;
 
-             $res['msg']    = '0008';
 
-         } else {
 
-             $res = json_decode($cmdret, true);
 
-             if ($res['result'] == false) {
 
-                 $res['result'] = 0;
 
-                 $version = $this->input->post('version',true);
 
-                 if (empty($version)) {
 
-                     $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
 
-                 }else{
 
-                     $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
 
-                 }
 
-             } else {
 
-                 $res['result'] = 1;
 
-                 $res['msg']    = '0000';
 
-             }
 
-         }
 
-         $file = fopen('../upload/test.txt', 'a+');
 
-         fwrite($file, '55555------'.date('Y-m-d H:i:s').'
 
- ');
 
-         fclose($file);
 
-         return $res;
 
-     }
 
-     // 更新蓄电池设置
 
-     public function set_battery(){
 
-         $username = $this->input->post('username',true);
 
-         $client_key = $this->input->post('client_key',true);
 
-         $token = $this->input->post('token',true);
 
-         $lamp_id = $this->input->post('lamp_id',true);
 
-         $clientid = $this->input->post('clientid',true);
 
-         $version = $this->session->userdata('version');
 
-         $batterytype              = $this->input->post('batterytype',true);//蓄电池类型
 
-         $capacity                 = $this->input->post('capacity',true);//蓄电池容量
 
-         $vpromotecharge           = $this->input->post('vpromotecharge',true);//提升充电电压/过充电压(锂电池)
 
-         $voverdischargerecover    = $this->input->post('voverdischargerecover',true);//过放恢复电压
 
-         $vpromoterecover          = $this->input->post('vpromoterecover',true);//提升充电恢复电压/过充返回电压(锂电池)
 
-         $voverdischarge           = $this->input->post('voverdischarge',true);//过放电压
 
-         if ($batterytype == 1) {
 
-             $vsystem                  = $this->input->post('vsystem',true);//系统电压
 
-             $minusnocharge            = $this->input->post('minusnocharge',true);//零下禁充
 
-             $chargemode               = $this->input->post('chargemode',true);//充电模式
 
-         } else {
 
-             $vovervoltage             = $this->input->post('vovervoltage',true);
 
-             $vlimitedcharge           = $this->input->post('vlimitedcharge',true);
 
-             $vbalancecharge           = $this->input->post('vbalancecharge',true);
 
-             $vfloatingcharge          = $this->input->post('vfloatingcharge',true);
 
-             $vundervoltagewarn        = $this->input->post('vundervoltagewarn',true);
 
-             $balancechargetime        = $this->input->post('balancechargetime',true);
 
-             $promotechargetime        = $this->input->post('promotechargetime',true);
 
-             $balanceinterval          = $this->input->post('balanceinterval',true);
 
-             $tempcompensation         = $this->input->post('tempcompensation',true);
 
-             $tempcompmax              = $this->input->post('tempcompmax',true);
 
-             $tempcompmin              = $this->input->post('tempcompmin',true);
 
-             $turnfloatingcurrent      = $this->input->post('turnfloatingcurrent',true);
 
-         }
 
-         $lampArr = explode(',', $lamp_id);
 
-         $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
 
-         $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/set_battery_asy';
 
-         foreach ($lampList as $l) {
 
-             if ($l['protocoltype'] == 0) {
 
-                 if ($batterytype == 1) {
 
-                     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);
 
-                 }else{
 
-                     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);
 
-                 }
 
-             }else{
 
-                 if ($batterytype == 1) {
 
-                     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]);
 
-                 }else{
 
-                     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]);
 
-                 }
 
-             }
 
-             
 
-         }
 
-         exit();
 
-     }
 
-     public function set_battery_asy(){
 
-         $res = $this->battery_update();
 
-         $clientid = $this->input->post('clientid',true);
 
-         $lampid = $this->input->post('lamp_id',true);
 
-         $data = $this->Lamp_model->getData(array('id'=>$lampid),'projectid,networkid,address,number');
 
-         if ($res['msg'] == '0000') {
 
-             $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
 
-             $networkData = $this->Network_model->getData(array('id'=>$data['networkid']),'networkname');
 
-             
 
-             $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯蓄电池参数");
 
-             $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);
 
-         }
 
-         if (empty($res['result'])) {
 
-             // exit(json_result($res['msg'],$res['msg'],array('number'=>$data['number'])));
 
-             $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
 
-             exit(send_websocket($clientid,$data));
 
-         }else{
 
-             // exit(json_result($res['msg'],$this->response[$res['msg']],array('number'=>$data['number'])));
 
-             $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$this->response[$res['msg']],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
 
-             exit(send_websocket($clientid,$data));
 
-         }
 
-     }
 
-     //更新蓄电池设置
 
-     private function battery_update(){
 
-         $where['id']                        = $this->input->post('lamp_id',true);//路灯id
 
-         $update['batterytype']              = $this->input->post('batterytype',true);//蓄电池类型
 
-         $update['capacity']                 = $this->input->post('capacity',true);//蓄电池容量
 
-         $update['vpromotecharge']           = $this->input->post('vpromotecharge',true);//提升充电电压/过充电压(锂电池)
 
-         $update['voverdischargerecover']    = $this->input->post('voverdischargerecover',true);//过放恢复电压
 
-         $update['vpromoterecover']          = $this->input->post('vpromoterecover',true);//提升充电恢复电压/过充返回电压(锂电池)
 
-         $update['voverdischarge']           = $this->input->post('voverdischarge',true);//过放电压
 
-         $update['updatetime']               = date("Y-m-d H:i:s");//更新时间
 
-         if ($update['batterytype'] == 1) {
 
-             $update['vovervoltage']             = $update['vpromotecharge']+2;//超压电压 
 
-             $update['vlimitedcharge']           = $update['vpromotecharge']+1;//充电限制电压
 
-             $update['vbalancecharge']           = $update['vpromotecharge'];//均衡充电电压
 
-             $update['vfloatingcharge']          = $update['vpromoterecover'];//浮充充电电压
 
-             $update['vundervoltagewarn']        = 12;//欠压警告电压
 
-             $update['balancechargetime']        = 2;//均衡充电时间
 
-             $update['promotechargetime']        = 2;//提升充电时间 
 
-             $update['balanceinterval']          = 30;//均衡充电间隔
 
-             $update['tempcompensation']         = 4;//温度补偿系数 
 
-             $update['tempcompmax']              = 60;//温度补偿最高温度 
 
-             $update['tempcompmin']              = 0;//温度补偿最低温度 
 
-             $update['turnfloatingcurrent']      = 1;//转浮充的电流 
 
-             $update['vsystem']                  = $this->input->post('vsystem',true);//系统电压
 
-             $update['minusnocharge']            = $this->input->post('minusnocharge',true);//零下禁充
 
-             $update['chargemode']               = $this->input->post('chargemode',true);//充电模式
 
-         } else {
 
-             $update['vovervoltage']             = $this->input->post('vovervoltage',true);
 
-             $update['vlimitedcharge']           = $this->input->post('vlimitedcharge',true);
 
-             $update['vbalancecharge']           = $this->input->post('vbalancecharge',true);
 
-             $update['vfloatingcharge']          = $this->input->post('vfloatingcharge',true);
 
-             $update['vundervoltagewarn']        = $this->input->post('vundervoltagewarn',true);
 
-             $update['balancechargetime']        = $this->input->post('balancechargetime',true);
 
-             $update['promotechargetime']        = $this->input->post('promotechargetime',true);
 
-             $update['balanceinterval']          = $this->input->post('balanceinterval',true);
 
-             $update['tempcompensation']         = $this->input->post('tempcompensation',true);
 
-             $update['tempcompmax']              = $this->input->post('tempcompmax',true);
 
-             $update['tempcompmin']              = $this->input->post('tempcompmin',true);
 
-             $update['turnfloatingcurrent']      = $this->input->post('turnfloatingcurrent',true);
 
-             $update['vsystem']                  = 12;
 
-             $update['minusnocharge']            = 0;
 
-             $update['chargemode']               = 0;
 
-         }
 
-         foreach ($update as $v) {
 
-             if ($v === null || $v === '') {
 
-                 $lampData = $this->Lamp_model->getOne($where['id'],'number');
 
-                 $data = ['code'=>'0007','status'=>2,'msg'=>$this->response['0007'],'data'=>['number'=>$lampData['number'],'type'=>'set_load','id'=>$where['id']]];
 
-                 exit(send_websocket($clientid,$data));
 
-             }
 
-         }
 
-         return $this->battery_set($where, $update, 0);     
 
-     }
 
-     private function battery_set($where,$update,$broadcast){
 
-         $ret = $this->Battery_model->getOne($where['id']);
 
-         if(empty($ret)){
 
-             $update['id'] = $where['id'];
 
-             $this->Battery_model->insert($update);
 
-         } else {
 
-             $this->Battery_model->update($where,$update);
 
-         }
 
-         
 
-         $cmd = '{"cmd_type":"battery_cmd","cmd_id":'.$where['id'].',"broadcast":'.$broadcast.'}';
 
-         $lampProData = $this->Lamp_model->getOne($where['id'],'N.protocoltype,L.address,L.protocoltype AS lampprotocoltype,N.deviceid');
 
-         if (($lampProData['protocoltype'] == 4 || $lampProData['protocoltype'] == 6) && $lampProData['lampprotocoltype'] == 0) {
 
-             $address = '';
 
-             if (mb_strlen($lampProData['address']) >= 8) {
 
-                 $address = mb_substr($lampProData['address'], -8);
 
-             }else{
 
-                 $address = '00000000';
 
-             }
 
-             $sendData = '10032820';
 
-             $sendData .= doubBitSubStr(base_convert($update['capacity'], 10, 16));
 
-             $sendData .= doubBitSubStr(base_convert(intval($update['vovervoltage']*10), 10, 16));
 
-             $sendData .= doubBitSubStr(base_convert(intval($update['vlimitedcharge']*10), 10, 16));
 
-             $sendData .= doubBitSubStr(base_convert(intval($update['vbalancecharge']*10), 10, 16));
 
-             $sendData .= doubBitSubStr(base_convert(intval($update['vpromotecharge']*10), 10, 16));
 
-             $sendData .= doubBitSubStr(base_convert(intval($update['vfloatingcharge']*10), 10, 16));
 
-             $sendData .= doubBitSubStr(base_convert(intval($update['vpromoterecover']*10), 10, 16));
 
-             $sendData .= doubBitSubStr(base_convert(intval($update['voverdischargerecover']*10), 10, 16));
 
-             $sendData .= doubBitSubStr(base_convert(intval($update['vundervoltagewarn']*10), 10, 16));
 
-             $sendData .= doubBitSubStr(base_convert(intval($update['voverdischarge']*10), 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['balancechargetime'], 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['promotechargetime'], 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['balanceinterval'], 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['tempcompensation'], 10, 16));
 
-             $sendData .= bitEncode($update['tempcompmax']);
 
-             $sendData .= bitEncode($update['tempcompmin']);
 
-             $sendData .= doubBitSubStr(base_convert(intval($update['turnfloatingcurrent']*100), 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['batterytype'], 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['vsystem'], 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['minusnocharge'], 10, 16));
 
-             $sendData .= bitSubStr(base_convert($update['chargemode'], 10, 16));
 
-             $sum = 0;
 
-             for ($i=0; $i < mb_strlen($sendData)/2; $i++) { 
 
-                 if ($i == 0) continue;
 
-                 $t = mb_substr($sendData, $i*2,2);
 
-                 $sum += base_convert($t, 16, 10);
 
-             }
 
-             $sum = base_convert($sum, 10, 16);
 
-             $sendData .= bitSubStr($sum);
 
-             $sendData = $address.$sendData;
 
-             
 
-             if ($lampProData['protocoltype'] == 4) {
 
-                 $res = lampMqttCmd('/WE/TransIn/'.$lampProData['address'],'/WE/TransOut/'.$lampProData['address'],pack('H*','0001'.$sendData));
 
-             }else{
 
-                 device_cmd($lampProData['deviceid'],pack('H*',$sendData));
 
-                 $cmd = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
 
-                 $info = send_lorawan_915($cmd);
 
-                 $res = array();
 
-                 if (!empty($info)) {
 
-                     $info = json_decode($info,true);
 
-                     if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
 
-                 }
 
-             }
 
-             
 
-             if (empty($res)) {
 
-                 return array('result'=>1,'msg'=>'0016');
 
-             }else{
 
-                 $msg1 = $res['msg'];
 
-                 // 数据校验
 
-                 $msg = mb_substr($msg1, 10);
 
-                 $t = 0;
 
-                 for ($i=0; $i < mb_strlen($msg) / 2 - 1; $i++) {
 
-                     $s = mb_substr($msg, $i*2,2);
 
-                     $t += base_convert($s, 16, 10);
 
-                 }
 
-                 $t = base_convert($t, 10, 16);
 
-                 if (mb_strlen($t) > 2) {
 
-                     $t = mb_substr($t, -2);
 
-                 }elseif($t <= 1){
 
-                     $t = '0'.$t;
 
-                 }
 
-                 if ($t == mb_substr($msg, -2)) {  
 
-                     if (mb_substr($msg1, -10,-2) == '10032820') {
 
-                         return array('result'=>1,'msg'=>'0000');
 
-                     }else{
 
-                         return array('result'=>1,'msg'=>'0010');
 
-                     }
 
-                 }else{
 
-                     return array('result'=>1,'msg'=>'0010');
 
-                 }
 
-             }
 
-         }
 
-         if (isset($lampProData['protocoltype']) && $lampProData['protocoltype'] != 0) {
 
-             $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
 
-             if ($cmdret === false) {
 
-                 sleep(1);
 
-                 $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
 
-             }
 
-         }else{
 
-             $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
 
-             if ($cmdret === false) {
 
-                 sleep(1);
 
-                 $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
 
-             }
 
-         }
 
-         $res = array(); 
 
-         if (empty($cmdret)){
 
-             $res['result'] = 1;
 
-             $res['msg']    = '0016';
 
-             return $res;
 
-         }
 
-         if($cmdret === false){
 
-             $res['result'] = 1;
 
-             $res['msg']    = '0008';
 
-         } else {
 
-             $res = json_decode($cmdret, true);
 
-             if ($res['result'] == false) {
 
-                 $res['result'] = 0;
 
-                 $version = $this->input->post('version',true);
 
-                 if (empty($version)) {
 
-                     $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
 
-                 }else{
 
-                     $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
 
-                 }
 
-             } else {
 
-                 $res['result'] = 1;
 
-                 $res['msg']    = '0000';
 
-             }
 
-         }
 
-         return $res;
 
-     }
 
-     // 新控制器更新蓄电池设置
 
-     public function modbus_set_battery(){
 
-         $username = $this->input->post('username',true);
 
-         $client_key = $this->input->post('client_key',true);
 
-         $token = $this->input->post('token',true);
 
-         $lamp_id = $this->input->post('lamp_id',true);
 
-         $clientid = $this->input->post('clientid',true);
 
-         $version = $this->session->userdata('version');
 
-         $light_voltage         = $this->input->post('light_voltage',true);
 
-         $battery_type              = $this->input->post('battery_type',true);
 
-         $li_battery_type              = $this->input->post('li_battery_type',true);
 
-         $system_voltage                 = $this->input->post('system_voltage',true);
 
-         $increa_char_voltage           = $this->input->post('increa_char_voltage',true);
 
-         $float_char_voltage    = $this->input->post('float_char_voltage',true);
 
-         $over_dischar_return_voltage          = $this->input->post('over_dischar_return_voltage',true);
 
-         $over_dischar_voltage           = $this->input->post('over_dischar_voltage',true);
 
-         $char_upper_limit_temper           = $this->input->post('char_upper_limit_temper',true);
 
-         $char_lower_limit_temper           = $this->input->post('char_lower_limit_temper',true);
 
-         $dischar_upper_limit_temper           = $this->input->post('dischar_upper_limit_temper',true);
 
-         $dischar_lower_limit_temper           = $this->input->post('dischar_lower_limit_temper',true);
 
-         if ($battery_type == 0) {
 
-             $overvoltage                  = $this->input->post('overvoltage',true);
 
-             $char_limit_voltage            = $this->input->post('char_limit_voltage',true);
 
-             $equ_char_voltage               = $this->input->post('equ_char_voltage',true);
 
-             $increa_char_return_voltage               = $this->input->post('increa_char_return_voltage',true);
 
-             $equ_char_time               = $this->input->post('equ_char_time',true) * 60;
 
-             $increa_char_time               = $this->input->post('increa_char_time',true) * 60;
 
-             $balan_char_interval               = $this->input->post('balan_char_interval',true);
 
-             $temper_compen_coeff               = $this->input->post('temper_compen_coeff',true);
 
-         }
 
-         $lampArr = explode(',', $lamp_id);
 
-         $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
 
-         $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/modbus_set_battery_asy';
 
-         $file = fopen('../upload/ModBus.txt', 'a+');
 
-         fwrite($file, $li_battery_type.'----'.date('Y-m-d H:i:s').'
 
- ');
 
-         fclose($file);
 
-         foreach ($lampList as $l) {
 
-             if ($l['protocoltype'] == 0) {
 
-                 if ($battery_type == 1) {
 
-                     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);
 
-                 }else{
 
-                     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);
 
-                 }
 
-             }else{
 
-                 if ($battery_type == 1) {
 
-                     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]);
 
-                 }else{
 
-                     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]);
 
-                 }
 
-             }
 
-             
 
-         }
 
-         exit();
 
-     }
 
-     public function modbus_set_battery_asy(){
 
-         $res = $this->modbus_battery_update();
 
-         $clientid = $this->input->post('clientid',true);
 
-         $lampid = $this->input->post('lamp_id',true);
 
-         $data = $this->Lamp_model->getData(array('id'=>$lampid),'projectid,networkid,address,number');
 
-         if ($res['msg'] == '0000') {
 
-             $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
 
-             $networkData = $this->Network_model->getData(array('id'=>$data['networkid']),'networkname');
 
-             
 
-             $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯蓄电池参数");
 
-             $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);
 
-         }
 
-         if (empty($res['result'])) {
 
-             // exit(json_result($res['msg'],$res['msg'],array('number'=>$data['number'])));
 
-             $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
 
-             exit(send_websocket($clientid,$data));
 
-         }else{
 
-             // exit(json_result($res['msg'],$this->response[$res['msg']],array('number'=>$data['number'])));
 
-             $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$this->response[$res['msg']],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
 
-             exit(send_websocket($clientid,$data));
 
-         }
 
-     }
 
-     //新控制器更新蓄电池设置
 
-     private function modbus_battery_update(){
 
-         $where['id']                        = $this->input->post('lamp_id',true);//路灯id
 
-         $update['light_voltage']              = $this->input->post('light_voltage',true);
 
-         $update['battery_type']              = $this->input->post('battery_type',true);
 
-         $update['li_battery_type']              = intval($this->input->post('li_battery_type',true));
 
-         $update['system_voltage']                 = $this->input->post('system_voltage',true);
 
-         $update['increa_char_voltage']           = $this->input->post('increa_char_voltage',true);
 
-         $update['float_char_voltage']    = $this->input->post('float_char_voltage',true);
 
-         $update['over_dischar_return_voltage']          = $this->input->post('over_dischar_return_voltage',true);
 
-         $update['over_dischar_voltage']           = $this->input->post('over_dischar_voltage',true);
 
-         $update['char_upper_limit_temper']           = $this->input->post('char_upper_limit_temper',true);
 
-         $update['char_lower_limit_temper']           = $this->input->post('char_lower_limit_temper',true);
 
-         $update['dischar_upper_limit_temper']           = $this->input->post('dischar_upper_limit_temper',true);
 
-         $update['dischar_lower_limit_temper']           = $this->input->post('dischar_lower_limit_temper',true);
 
-         if ($update['battery_type'] == 0) {
 
-             $update['overvoltage']                  = $this->input->post('overvoltage',true);
 
-             $update['char_limit_voltage']            = $this->input->post('char_limit_voltage',true);
 
-             $update['equ_char_voltage']               = $this->input->post('equ_char_voltage',true);
 
-             $update['increa_char_return_voltage']               = $this->input->post('increa_char_return_voltage',true);
 
-             $update['equ_char_time']               = $this->input->post('equ_char_time',true);
 
-             $update['increa_char_time']               = $this->input->post('increa_char_time',true);
 
-             $update['balan_char_interval']               = $this->input->post('balan_char_interval',true);
 
-             $update['temper_compen_coeff']               = $this->input->post('temper_compen_coeff',true);
 
-         }
 
-         foreach ($update as $v) {
 
-             if ($v === null || $v === '') {
 
-                 $lampData = $this->Lamp_model->getOne($where['id'],'number');
 
-                 $data = ['code'=>'0007','status'=>2,'msg'=>$this->response['0007'],'data'=>['number'=>$lampData['number'],'type'=>'set_load','id'=>$where['id']]];
 
-                 exit(send_websocket($clientid,$data));
 
-             }
 
-         }
 
-         
 
-         return $this->modbus_battery_set($where, $update, 0);     
 
-     }
 
-     private function modbus_battery_set($where,$update,$broadcast){
 
-         $file = fopen('../upload/ModBus.txt', 'a+');
 
-         fwrite($file, $update['li_battery_type'].'----'.date('Y-m-d H:i:s').'
 
- ');
 
-         fclose($file);
 
-         $ret = $this->Modbus_battery_model->getOne($where['id']);
 
-         if(empty($ret)){
 
-             $update['id'] = $where['id'];
 
-             $this->Modbus_battery_model->insert($update);
 
-         } else {
 
-             $this->Modbus_battery_model->update($where,$update);
 
-         }
 
-         
 
-         $cmd = '{"cmd_type":"modbus_battery_cmd","cmd_id":'.$where['id'].',"broadcast":'.$broadcast.'}';
 
-         $lampProData = $this->Lamp_model->getOne($where['id'],'N.protocoltype,N.deviceid,L.address,L.protocoltype as lampprotocoltype');
 
-         if ($lampProData['protocoltype'] == 4 && $lampProData['lampprotocoltype'] == 1) {
 
-             $sendData = '0103e0020020d212';
 
-             
 
-             $res = lampMqttCmd('/WE/TransIn/'.$lampProData['address'],'/WE/TransOut/'.$lampProData['address'],pack('H*','0001'.$sendData));
 
-             if (empty($res)) {
 
-                 return array('result'=>1,'msg'=>'0016');
 
-             }else{
 
-                 if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
 
-                     $old = $res['msg'];
 
-                     $data = array();
 
-                     $sendData = '';
 
-                     $sendData .= substr($old, 6,4);
 
-                     if ($update['battery_type'] == 0) { // 铅酸电池
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['system_voltage']), 10, 16)).''; // 系统电压
 
-                         $sendData .= doubBitSubStr(base_convert(10, 10, 16)).''; // 电池类型
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['overvoltage']*10), 10, 16)).''; // 超压电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['char_limit_voltage']*10), 10, 16)).''; // 充电限制电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['equ_char_voltage']*10), 10, 16)).''; // 均衡充电电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10), 10, 16)).''; // 提升充电电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['float_char_voltage']*10), 10, 16)).''; // 浮充充电电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_return_voltage']*10), 10, 16)).''; // 提升充电返回电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_return_voltage']*10), 10, 16)).''; // 过放返回电压
 
-                         $sendData .= substr($old, 46,4);
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10), 10, 16)).''; // 过放电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10 - 5), 10, 16)).''; // 放电限制电压
 
-                         $sendData .= substr($old, 58,8);
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['equ_char_time']), 10, 16)).''; // 均衡充电时间
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_time']), 10, 16)).''; // 提升充电时间
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['balan_char_interval']), 10, 16)).''; // 均衡充电间隔
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['temper_compen_coeff']), 10, 16)).''; // 温度补偿系数
 
-                         $sendData .= substr($old, 82,2);
 
-                         $sendData .= bitEncode(intval($update['char_upper_limit_temper'])).''; // 充电上限温度
 
-                         $sendData .= substr($old, 86,2);
 
-                         $sendData .= bitEncode(intval($update['char_lower_limit_temper'])).''; // 充电下限温度
 
-                         $sendData .= substr($old, 90,2);
 
-                         $sendData .= bitEncode(intval($update['dischar_upper_limit_temper'])).''; // 放电上限温度
 
-                         $sendData .= substr($old, 94,2);
 
-                         $sendData .= bitEncode(intval($update['dischar_lower_limit_temper'])).''; // 放电下限温度
 
-                         $sendData .= substr($old, 98,24);
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['light_voltage']), 10, 16)).''; // 光控电压
 
-                         $sendData .= substr($old, 126,6);
 
-                         // 特殊功能系统电压设置
 
-                         $str = substr('00000000'.base_convert(substr($old, 132,6), 16, 2), -8);
 
-                         if ($update['system_voltage'] == 12) {
 
-                             $str = substr($str, 0,3).'00'.substr($str, -3);
 
-                         }else{
 
-                             $str = substr($str, 0,3).'01'.substr($str, -3);
 
-                         }
 
-                         // 特殊功能系统电池类型设置
 
-                         $str = '0'.substr($str, -7);
 
-                         $sendData .= bitSubStr(base_convert($str, 2, 16));
 
-                     }else{ // 锂电池
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['system_voltage']), 10, 16)).''; // 系统电压
 
-                         $sendData .= doubBitSubStr(base_convert(11, 10, 16)).''; // 电池类型
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10 + 2), 10, 16)).''; // 超压电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10 + 1), 10, 16)).''; // 充电限制电压
 
-                         $sendData .= substr($old, 26,4);
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10), 10, 16)).''; // 充电电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['float_char_voltage']*10), 10, 16)).''; // 充电返回电压
 
-                         $sendData .= substr($old, 38,4);
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_return_voltage']*10), 10, 16)).''; // 过放返回电压
 
-                         $sendData .= substr($old, 46,4);
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10), 10, 16)).''; // 过放电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10 - 5), 10, 16)).''; // 放电限制电压
 
-                         $sendData .= substr($old, 58,24);
 
-                         $sendData .= substr($old, 82,2);
 
-                         $sendData .= bitEncode(intval($update['char_upper_limit_temper'])).''; // 充电上限温度
 
-                         $sendData .= substr($old, 86,2);
 
-                         $sendData .= bitEncode(intval($update['char_lower_limit_temper'])).''; // 充电下限温度
 
-                         $sendData .= substr($old, 90,2);
 
-                         $sendData .= bitEncode(intval($update['dischar_upper_limit_temper'])).''; // 放电上限温度
 
-                         $sendData .= substr($old, 94,2);
 
-                         $sendData .= bitEncode(intval($update['dischar_lower_limit_temper'])).''; // 放电下限温度
 
-                         $sendData .= substr($old, 98,24);
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['light_voltage']), 10, 16)).''; // 光控电压
 
-                         $sendData .= substr($old, 126,6);
 
-                         // 特殊功能系统电压设置
 
-                         $str = substr('00000000'.base_convert(substr($old, 132,6), 16, 2), -8);
 
-                         if ($update['system_voltage'] == 12) {
 
-                             $str = substr($str, 0,3).'00'.substr($str, -3);
 
-                         }elseif ($update['system_voltage'] == 24) {
 
-                             $str = substr($str, 0,3).'01'.substr($str, -3);
 
-                         }elseif ($update['system_voltage'] == 3) {
 
-                             $str = substr($str, 0,3).'10'.substr($str, -3);
 
-                         }else{
 
-                             $str = substr($str, 0,3).'11'.substr($str, -3);
 
-                         }
 
-                         // 特殊功能系统电池类型设置
 
-                         $str = '1'.substr($str, -7);
 
-                         $sendData .= bitSubStr(base_convert($str, 2, 16));
 
-                     }
 
-                     $len = doubBitSubStr(base_convert(intval(mb_strlen($sendData)/4), 10, 16)).bitSubStr(base_convert(intval(mb_strlen($sendData)/2), 10, 16));
 
-                     $sendData = '0110e002'.$len.$sendData;
 
-                     $sendData .= crc16(pack('H*',$sendData));
 
-                     $res = lampMqttCmd('/WE/TransIn/'.$lampProData['address'],'/WE/TransOut/'.$lampProData['address'],pack('H*','0001'.$sendData));
 
-                     if (empty($res)) {
 
-                         return array('result'=>1,'msg'=>'0016');
 
-                     }else{
 
-                         if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
 
-                             return array('result'=>1,'msg'=>'0000');
 
-                         }else{
 
-                             return array('result'=>1,'msg'=>'0010');
 
-                         }
 
-                     }
 
-                 }else{
 
-                     return array('result'=>1,'msg'=>'0010');
 
-                 }
 
-             }
 
-             
 
-         }
 
-         elseif ($lampProData['protocoltype']==6 && $lampProData['lampprotocoltype'] == 1){
 
-             $cmdLine='0103e0020020d212';
 
-             device_cmd($lampProData['deviceid'],pack('H*',$cmdLine));
 
-             $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
 
-             $res=send_lorawan_915($cmdLine);
 
-             if (empty($res)) {
 
-                 return array('result'=>1,'msg'=>'0016');
 
-             }else{
 
-                 $res=json_decode($res,true);
 
-                 $cmdRes=$res['resInfo'];
 
-                 if(strtolower(substr($cmdRes,0,6))=='010340'){
 
-                     $old = $cmdRes;
 
-                     $data = array();
 
-                     $sendData = '';
 
-                     $sendData .= substr($old, 6,4);
 
-                     if ($update['battery_type'] == 0) { // 铅酸电池
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['system_voltage']), 10, 16)).''; // 系统电压
 
-                         $sendData .= doubBitSubStr(base_convert(10, 10, 16)).''; // 电池类型
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['overvoltage']*10), 10, 16)).''; // 超压电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['char_limit_voltage']*10), 10, 16)).''; // 充电限制电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['equ_char_voltage']*10), 10, 16)).''; // 均衡充电电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10), 10, 16)).''; // 提升充电电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['float_char_voltage']*10), 10, 16)).''; // 浮充充电电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_return_voltage']*10), 10, 16)).''; // 提升充电返回电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_return_voltage']*10), 10, 16)).''; // 过放返回电压
 
-                         $sendData .= substr($old, 46,4);
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10), 10, 16)).''; // 过放电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10 - 5), 10, 16)).''; // 放电限制电压
 
-                         $sendData .= substr($old, 58,8);
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['equ_char_time']), 10, 16)).''; // 均衡充电时间
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_time']), 10, 16)).''; // 提升充电时间
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['balan_char_interval']), 10, 16)).''; // 均衡充电间隔
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['temper_compen_coeff']), 10, 16)).''; // 温度补偿系数
 
-                         $sendData .= substr($old, 82,2);
 
-                         $sendData .= bitEncode(intval($update['char_upper_limit_temper'])).''; // 充电上限温度
 
-                         $sendData .= substr($old, 86,2);
 
-                         $sendData .= bitEncode(intval($update['char_lower_limit_temper'])).''; // 充电下限温度
 
-                         $sendData .= substr($old, 90,2);
 
-                         $sendData .= bitEncode(intval($update['dischar_upper_limit_temper'])).''; // 放电上限温度
 
-                         $sendData .= substr($old, 94,2);
 
-                         $sendData .= bitEncode(intval($update['dischar_lower_limit_temper'])).''; // 放电下限温度
 
-                         $sendData .= substr($old, 98,24);
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['light_voltage']), 10, 16)).''; // 光控电压
 
-                         $sendData .= substr($old, 126,6);
 
-                         // 特殊功能系统电压设置
 
-                         $str = substr('00000000'.base_convert(substr($old, 132,6), 16, 2), -8);
 
-                         if ($update['system_voltage'] == 12) {
 
-                             $str = substr($str, 0,3).'00'.substr($str, -3);
 
-                         }else{
 
-                             $str = substr($str, 0,3).'01'.substr($str, -3);
 
-                         }
 
-                         // 特殊功能系统电池类型设置
 
-                         $str = '0'.substr($str, -7);
 
-                         $sendData .= bitSubStr(base_convert($str, 2, 16));
 
-                     }else{ // 锂电池
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['system_voltage']), 10, 16)).''; // 系统电压
 
-                         $sendData .= doubBitSubStr(base_convert(11, 10, 16)).''; // 电池类型
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10 + 2), 10, 16)).''; // 超压电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10 + 1), 10, 16)).''; // 充电限制电压
 
-                         $sendData .= substr($old, 26,4);
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10), 10, 16)).''; // 充电电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['float_char_voltage']*10), 10, 16)).''; // 充电返回电压
 
-                         $sendData .= substr($old, 38,4);
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_return_voltage']*10), 10, 16)).''; // 过放返回电压
 
-                         $sendData .= substr($old, 46,4);
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10), 10, 16)).''; // 过放电压
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10 - 5), 10, 16)).''; // 放电限制电压
 
-                         $sendData .= substr($old, 58,24);
 
-                         $sendData .= substr($old, 82,2);
 
-                         $sendData .= bitEncode(intval($update['char_upper_limit_temper'])).''; // 充电上限温度
 
-                         $sendData .= substr($old, 86,2);
 
-                         $sendData .= bitEncode(intval($update['char_lower_limit_temper'])).''; // 充电下限温度
 
-                         $sendData .= substr($old, 90,2);
 
-                         $sendData .= bitEncode(intval($update['dischar_upper_limit_temper'])).''; // 放电上限温度
 
-                         $sendData .= substr($old, 94,2);
 
-                         $sendData .= bitEncode(intval($update['dischar_lower_limit_temper'])).''; // 放电下限温度
 
-                         $sendData .= substr($old, 98,24);
 
-                         $sendData .= doubBitSubStr(base_convert(intval($update['light_voltage']), 10, 16)).''; // 光控电压
 
-                         $sendData .= substr($old, 126,6);
 
-                         // 特殊功能系统电压设置
 
-                         $str = substr('00000000'.base_convert(substr($old, 132,6), 16, 2), -8);
 
-                         if ($update['system_voltage'] == 12) {
 
-                             $str = substr($str, 0,3).'00'.substr($str, -3);
 
-                         }elseif ($update['system_voltage'] == 24) {
 
-                             $str = substr($str, 0,3).'01'.substr($str, -3);
 
-                         }elseif ($update['system_voltage'] == 3) {
 
-                             $str = substr($str, 0,3).'10'.substr($str, -3);
 
-                         }else{
 
-                             $str = substr($str, 0,3).'11'.substr($str, -3);
 
-                         }
 
-                         // 特殊功能系统电池类型设置
 
-                         $str = '1'.substr($str, -7);
 
-                         $sendData .= bitSubStr(base_convert($str, 2, 16));
 
-                     }
 
- //                    $cmdLine='0110e002002040'.$sendData;
 
- //                    $cmdLine .= crc16(pack('H*',$cmdLine));
 
-                     // $file = fopen('./file/loraWan_battery.txt', 'a+');
 
-                     // fwrite($file, date('Y-m-d H:i:s').'----'.$cmdLine.' ');
 
-                     $len = strlen($sendData)/4;
 
-                     $con1 = substr($sendData, 0, $len/2*4);
 
-                     $con2 = substr($sendData, $len/2*4);
 
-                     $address1 = 'e002';
 
-                     $address2 = substr('0000'.base_convert(base_convert($address1, 16, 10) + $len/2, 10, 16), -4);
 
-                     $l1 = substr('0000'.base_convert($len/2, 10, 16), -4);
 
-                     $l2 = substr('00'.base_convert($len/2, 10, 16), -2);
 
-                     $cmd1 = '0110'.$address1.$l1.$l2.$con1;
 
-                     $cmd1 .= crc16(pack('H*',$cmd1));
 
-                     device_cmd($lampProData['deviceid'],pack('H*',$cmd1));
 
-                     $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
 
-                     $res=send_lorawan_915($cmdLine);
 
-                     // $cmd2 = '0110'.$address2.$l1.$l2.$con2;
 
-                     // $cmd2 .= crc16(pack('H*',$cmd2));
 
-                     // $res = device_cmd($lampProData['deviceid'],pack('H*',$cmd2));
 
-                     // device_cmd($lampProData['deviceid'],pack('H*',$cmdLine));
 
-                     
 
-                     // fwrite($file, date('Y-m-d H:i:s').'----battery');
 
-                     // fclose($file);
 
-                     if (empty($res)) {
 
-                         return array('result'=>1,'msg'=>'0016');
 
-                     }else{
 
-                         $cmd2 = '0110'.$address2.$l1.$l2.$con2;
 
-                         $cmd2 .= crc16(pack('H*',$cmd2));
 
-                         device_cmd($lampProData['deviceid'],pack('H*',$cmd2));
 
-                         $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
 
-                         $res=send_lorawan_915($cmdLine);
 
-                         if (empty($res)) {
 
-                             return array('result'=>1,'msg'=>'0016');
 
-                         }else{
 
-                             return array('result'=>1,'msg'=>'0000');
 
-                         }
 
-                         // $file = fopen('./file/loraWan_battery.txt', 'a+');
 
-                         // fwrite($file, date('Y-m-d H:i:s').'----'.$res.' ');
 
-                         // fclose($file);
 
-                         // $res=json_decode($res,true);
 
-                         // $cmdRes2=$res['resInfo'];
 
-                         // if(strtolower(substr($cmdRes2,0,6))=='010340') {
 
-                         //     return array('result'=>1,'msg'=>'0000');
 
-                         // }else{
 
-                         //     return array('result'=>1,'msg'=>'0010');
 
-                         // }
 
-                     }
 
-                 }else{
 
-                     return array('result' => 1, 'msg' => '0010');
 
-                 }
 
-             }
 
-         }
 
- //        elseif($lampProData['protocoltype']==7){}
 
-         if (isset($lampProData['protocoltype']) && $lampProData['protocoltype'] != 0) {
 
-             $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
 
-             if ($cmdret === false) {
 
-                 sleep(1);
 
-                 $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
 
-             }
 
-         }else{
 
-             $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
 
-             if ($cmdret === false) {
 
-                 sleep(1);
 
-                 $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
 
-             }
 
-         }
 
-         $res = array(); 
 
-         if (empty($cmdret)){
 
-             $res['result'] = 1;
 
-             $res['msg']    = '0016';
 
-             return $res;
 
-         }
 
-         if($cmdret === false){
 
-             $res['result'] = 1;
 
-             $res['msg']    = '0008';
 
-         } else {
 
-             $res = json_decode($cmdret, true);
 
-             if ($res['result'] == false) {
 
-                 $res['result'] = 0;
 
-                 $version = $this->input->post('version',true);
 
-                 if (empty($version)) {
 
-                     $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
 
-                 }else{
 
-                     $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
 
-                 }
 
-             } else {
 
-                 $res['result'] = 1;
 
-                 $res['msg']    = '0000';
 
-             }
 
-         }
 
-         return $res;
 
-     }
 
-     // 添加编辑灯控
 
-     public function save() {
 
-         $role = $this->get_user_info('role');
 
-         
 
-         $where['id']       = $this->input->post('lamp_id',true);
 
-         $data['projectid'] = $this->input->post('project_id',true);
 
-         $data['number']    = $this->input->post('lamp_no',true);
 
-         $data['section']   = $this->input->post('section',true);
 
-         $data['address']   = $this->input->post('address',true);
 
-         $data['longitude'] = doubleval($this->input->post('longitude',true));
 
-         $data['latitude']  = doubleval($this->input->post('latitude',true));
 
-         
 
-         if ($data['longitude'] < -180 || $data['longitude'] > 180) exit(json_result('0421',$this->response['0421']));
 
-         if ($data['latitude'] < -90 || $data['latitude'] > 90) exit(json_result('0422',$this->response['0422']));
 
-         $poleheight = $this->input->post('poleheight',true);
 
-         if ($poleheight !== null) {
 
-             $data['poleheight'] = $poleheight;
 
-         }
 
-         $data["polediameter"] = $this->input->post('polediameter',true);
 
-         $data["polematerial"] = $this->input->post('polematerial',true);
 
-         $data["lighttype"] = $this->input->post('lighttype',true);
 
-         $data["lamptype"] = $this->input->post('lamptype',true);
 
-         $data["boardtype"] = $this->input->post('boardtype',true);
 
-         $data["batterytype"] = $this->input->post('batterytype',true);
 
-         $data["boardpower"] = $this->input->post('boardpower',true);
 
-         $data["batteryah"] = $this->input->post('batteryah',true);
 
-         $wattage = $this->input->post('wattage',true);
 
-         $data["wattage"] = !empty($wattage) ? $wattage : 0;
 
-         $supplier = $this->input->post('supplier',true);
 
-         $data["supplier"] = !empty($supplier) ? $supplier : '';
 
-         $upazilla = $this->input->post('upazilla',true);
 
-         $data["upazilla"] = !empty($upazilla) ? $upazilla : '';
 
-         $po = $this->input->post('po',true);
 
-         $data["po"] = !empty($po) ? $po : '';
 
-         $manu = $this->input->post('manu',true);
 
-         $data["manu"] = !empty($manu) ? $manu : '';
 
-         $loadtype = $this->input->post('loadtype',true);
 
-         if ($loadtype !== null) {
 
-             $data["loadtype"] = intval($loadtype);
 
-         }
 
-         // 控制器协议类型
 
-         $lampprotocoltype = intval($this->input->post('lampprotocoltype',true));
 
-         if ($lampprotocoltype > 1 || $lampprotocoltype < 0) exit(json_result('0415', $this->response['0415'], array()));
 
-         $devicetype = intval($this->input->post('devicetype',true));
 
-         $data['devicetype'] = $devicetype;
 
-         $version = $this->session->userdata('version');
 
-         if ($devicetype <= 0) {
 
-             $data['networkid'] = $this->input->post('network_id',true);
 
-             if (empty($data['networkid'])) exit(json_result('0405', $this->response['0405'], array()));
 
-         }
 
-         if (strlen($data['number']) > 6) exit(json_result('0408', $this->response['0408'], array()));
 
-         if (empty($data['projectid'])) exit(json_result('0308', $this->response['0308'], array()));
 
-         if (empty($data['number'])) exit(json_result('0409', $this->response['0409'], array()));
 
-         if (!is_numeric($data['number']) || $data['number'] < 1 || $data['number'] > 999999) exit(json_result('0410', $this->response['0410'], array()));
 
-         if (empty($data['address'])) exit(json_result('0805', $this->response['0805'], array()));
 
-         if (is_float($data['address'])) {
 
-             $data['address'] = strval(intval(ceil($data['address'])));
 
-         }
 
-         $is_true = preg_match('/^[A-Fa-f0-9]+$/', $data['address']);
 
-         if (empty($is_true)) exit(json_result('0414', $this->response['0414'], array()));
 
-         $data = remove_null_params($data);
 
-         if (empty($where['id'])) {
 
-             if ($this->Lamp_model->getDataCount(array('number'=>$data['number'], 'projectid'=>$data['projectid'])) > 0) {
 
-                 exit(json_result('0401', $this->response['0401'], array()));
 
-             }   
 
-             $lamp = $this->Lamp_model->queryData(array('address'=>$data['address']));
 
-             if (!empty($lamp)) {
 
-                 $projectid = $lamp['projectid'];
 
-                 $networkid = $lamp['networkid'];
 
-                 $number = $lamp['number'];
 
-                 $networkname = $this->Network_model->getNameById($networkid);
 
-                 $projectname = $this->Project_model->getNameById($projectid);
 
-                 if (empty($version)) {
 
-                     exit(json_result('0404', "该无线模块地址已被【{$projectname}】项目,【{$networkname}】网络,【{$number}】编号的灯控使用", array()));
 
-                 }else{
 
-                     exit(json_result('other', "The wireless module address has been used by \"{$projectname}\" project, \"{$networkname}\" network, and \"{$number}\" serial number", array()));
 
-                 }
 
-                 
 
-                 
 
-             }
 
-             $projectData = $this->Project_model->getOne($data['projectid'],'patrolinterval,cityid');
 
-             if (empty($data['longitude']) && empty($data['latitude']) && !empty($projectData['cityid'])) {
 
-                 $globalData = $this->Global_location_model->getOne($projectData['cityid'],'longitude,latitude');
 
-                 $data['longitude'] = $globalData['longitude'];
 
-                 $data['latitude'] = $globalData['latitude'];
 
-             }
 
-             $timezone = $this->Project_model->get_timezone_by_projectid($data['projectid']);       
 
-             $data['createtime'] = get_time_by_timezone($timezone['value']);
 
-             $data['updatetime'] = get_time_by_timezone($timezone['value']);
 
-             if ($devicetype > 0) {
 
-                 // 直连设备
 
-                 $number = mb_strlen($data['address']) == 13 ? base_convert($data['address'], 16, 10) : $data['address'];
 
-                 if ($this->Network_model->getDataCount(array('devicesn'=>$number))) {
 
-                     exit(json_result('0413',$this->response['0413'],array()));
 
-                 }
 
-                 $networkData['networkid'] = $number;
 
-                 $networkData['gatewaytype'] = 'direct';
 
-                 $networkData['devicetype'] = $lampprotocoltype == 1 ? 3 : 0;
 
-                 $protocoltype = $this->input->post('protocoltype',true);
 
-                 if ($protocoltype === null) {
 
-                     exit(json_result('0007',$this->response['0007'],array()));
 
-                 }
 
-                 if($protocoltype==6){
 
-                     $this->add_device($data['address']);
 
-                 }
 
- //                if($protocoltype==7){
 
- //                    $onenet=$this->add_device_onenet($data['address']);
 
- //                    if(is_object($onenet)){
 
- //                        $onenet=json_decode($onenet,true);
 
- //                    }
 
- //
 
- //                    $onenetId= $onenet['data']['device_id'];
 
- //                    if($onenetId){
 
- //                        $data['deviceid']=$onenetId;
 
- //                    }
 
- //                }
 
-                 $networkData['protocoltype'] = empty($protocoltype) ? 0 : $protocoltype;
 
-                 $networkData['projectid'] = $data['projectid'];
 
-                 $networkData['devicesn'] = $number;
 
-                 $networkData['networkname'] = $number;
 
-                 $networkData['type'] = 1;
 
-                 $networkData['createtime'] = date('Y-m-d H:i:s',time());
 
-                 $data['networkid'] = $this->Network_model->insert($networkData);
 
-             }
 
-             $data['protocoltype'] = $lampprotocoltype;
 
-             $lampid = $this->Lamp_model->insert($data);
 
-             $data['faultcount'] = 0;
 
-             $this->Network_model->add_lamp_count($data);
 
-             $this->Project_model->add_lamp_count($data);
 
-             //设置路灯默认巡检时间
 
-             
 
-             $this->patrol_set($lampid,$projectData['patrolinterval']);
 
-             $lampId = intval($lampid);
 
-             $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
 
-             $networkData = $this->Network_model->getData(array('id'=>$data['networkid']),'networkname');
 
-             $this->add_operation_log('insert',"在\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里添加编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯",0);
 
-             $this->add_operation_log('insert',"Add lamp:\"{$data['number']}\".Wireless module address:\"{$data['address']}\".Project name:\"{$projectData['projectname']}\".Network name:\"{$networkData['networkname']}\".",0,1);
 
-         } else {
 
-             if ($this->Lamp_model->getDataCount(array('number'=>$data['number'], 'projectid'=>$data['projectid']), $where['id']) > 0) {
 
-                 exit(json_result('0401', $this->response['0401'], array()));
 
-             }
 
-             $lamp = $this->Lamp_model->queryData(array('address'=>$data['address'], 'id !='=>$where['id']));
 
-             if (!empty($lamp)) {
 
-                 $projectid = $lamp['projectid'];
 
-                 $networkid = $lamp['networkid'];
 
-                 $number = $lamp['number'];
 
-                 $networkname = $this->Network_model->getNameById($networkid);
 
-                 $projectname = $this->Project_model->getNameById($projectid);
 
-                 if (empty($version)) {
 
-                     exit(json_result('0404', "该无线模块地址已被【{$projectname}】项目,【{$networkname}】网络,【{$number}】编号的灯控使用", array()));
 
-                 }else{
 
-                     exit(json_result('other', "The wireless module address has been used by \"{$projectname}\" project, \"{$networkname}\" network, and \"{$number}\" serial number", array()));
 
-                 }
 
-             }
 
-             $old = $this->Lamp_model->getOne($where['id'],'L.networkid,L.isfaulted,L.projectid,L.address,N.type,N.protocoltype');
 
-             
 
-             if ($old['projectid'] != $data['projectid']) {
 
-                 $new['projectid']  = $data['projectid'];
 
-                 $new['faultcount'] = $old['isfaulted'] > 0 ? 1 : 0;
 
-                 $this->Project_model->add_lamp_count($new);
 
-                 $old['faultcount'] = $new['faultcount'];
 
-                 $this->Project_model->minus_lamp_count($old);
 
-             }
 
-             if ($devicetype > 0) {
 
-                 $protocoltype = $this->input->post('protocoltype',true);
 
-                 if ($protocoltype === null) {
 
-                     exit(json_result('0007',$this->response['0007'],array()));
 
-                 }
 
-                 $oldNet = $this->Network_model->getData(array('id'=>$old['networkid']));
 
-                 $netDeviceType = $lampprotocoltype == 1 ? 3 : 0;
 
-                 if ($old['type'] == 0) {
 
-                     $number = mb_strlen($data['address']) == 13 ? base_convert($data['address'], 16, 10) : $data['address'];
 
-                     if ($this->Network_model->getDataCount(array('devicesn'=>$number))) {
 
-                         exit(json_result('0413',$this->response['0413'],array()));
 
-                     }
 
-                     $networkData['networkid'] = $number;
 
-                     $networkData['gatewaytype'] = 'direct';
 
-                     $networkData['devicetype'] = $lampprotocoltype == 1 ? 3 : 0;
 
-                     $networkData['protocoltype'] = empty($protocoltype) ? 0 : $protocoltype;
 
-                     $networkData['projectid'] = $data['projectid'];
 
-                     $networkData['devicesn'] = $number;
 
-                     $networkData['networkname'] = $number;
 
-                     $networkData['type'] = 1;
 
-                     $networkData['lampcount'] = 1;
 
-                     $networkData['createtime'] = date('Y-m-d H:i:s',time());
 
-                     $data['networkid'] = $this->Network_model->insert($networkData);
 
-                     $data['info_cache'] = '';
 
-                     if($protocoltype==6){
 
-                         if ($old['protocoltype'] == 6) $this->remove_device($old['address']);
 
-                         $this->add_device($data['address']);
 
-                     }
 
-                 }elseif (empty($oldNet) || $old['address'] != $data['address'] || $netDeviceType != $oldNet['devicetype']) {
 
-                     // 删除原有网络
 
-                     // $this->Zone_model->delZone($old['networkid']);
 
-                     $this->Network_model->delData(array('id'=>$old['networkid']));
 
-                     $cmdstr = '{"cmd_type":"delete_network_cmd","cmd_id":'.$old['networkid'].'}';
 
-                     if (isset($oldNet['protocoltype']) && $oldNet['protocoltype'] != 0) {
 
-                         $cmdret = send_cmd($cmdstr,1,0,$oldNet['protocoltype']);
 
-                         if ($cmdret === false) {
 
-                             sleep(1);
 
-                             $cmdret = send_cmd($cmdstr,1,0,$oldNet['protocoltype']);
 
-                         }
 
-                     }else{
 
-                         $cmdret = send_cmd($cmdstr,0,0,$oldNet['protocoltype']);
 
-                         if ($cmdret === false) {
 
-                             sleep(1);
 
-                             $cmdret = send_cmd($cmdstr,0,0,$oldNet['protocoltype']);
 
-                         }
 
-                     }
 
-                     $number = mb_strlen($data['address']) == 13 ? base_convert($data['address'], 16, 10) : $data['address'];
 
-                     if ($this->Network_model->getDataCount(array('devicesn'=>$number))) {
 
-                         exit(json_result('0413',$this->response['0413'],array()));
 
-                     }
 
-                     $networkData['networkid'] = $number;
 
-                     $networkData['gatewaytype'] = 'direct';
 
-                     $networkData['devicetype'] = $lampprotocoltype == 1 ? 3 : 0;
 
-                     $networkData['protocoltype'] = empty($protocoltype) ? 0 : $protocoltype;
 
-                     $networkData['projectid'] = $data['projectid'];
 
-                     $networkData['devicesn'] = $number;
 
-                     $networkData['networkname'] = $number;
 
-                     $networkData['type'] = 1;
 
-                     $networkData['lampcount'] = 1;
 
-                     $networkData['createtime'] = date('Y-m-d H:i:s',time());
 
-                     $data['networkid'] = $this->Network_model->insert($networkData);
 
-                     $data['info_cache'] = '';
 
-                     if($protocoltype==6){
 
-                         if ($old['protocoltype'] == 6) $this->remove_device($old['address']);
 
-                         $this->add_device($data['address']);
 
-                     }
 
- //                    if($protocoltype==7){
 
- //                        if ($old['protocoltype'] == 7) $this->remove_device_onenet($old['address']);
 
- //                        $onenet= $this->add_device_onenet($data['address']);
 
- //                        $onenetId= $onenet['data']['device_id'];
 
- //                        if($onenetId){
 
- //                            $data['deviceid']=$onenetId;
 
- //                        }
 
- //                    }
 
-                 }else{
 
-                     if ($protocoltype != $oldNet['protocoltype']) {
 
-                         $number = mb_strlen($data['address']) == 13 ? base_convert($data['address'], 16, 10) : $data['address'];
 
-                         $this->Network_model->update(array('id'=>$old['networkid']),array('protocoltype'=>$protocoltype,'projectid'=>$data['projectid'],'networkid'=>$number));
 
-                         $cmdstr = '{"cmd_type":"delete_network_cmd","cmd_id":'.$old['networkid'].'}';
 
-                         if (isset($oldNet['protocoltype']) && $oldNet['protocoltype'] != 0) {
 
-                             $cmdret = send_cmd($cmdstr,1,0,$oldNet['protocoltype']);
 
-                             if ($cmdret === false) {
 
-                                 sleep(1);
 
-                                 $cmdret = send_cmd($cmdstr,1,0,$oldNet['protocoltype']);
 
-                             }
 
-                         }else{
 
-                             $cmdret = send_cmd($cmdstr,0,0,$oldNet['protocoltype']);
 
-                             if ($cmdret === false) {
 
-                                 sleep(1);
 
-                                 $cmdret = send_cmd($cmdstr,0,0,$oldNet['protocoltype']);
 
-                             }
 
-                         }
 
-                         if ($old['protocoltype'] == 6) $this->remove_device($old['address']);
 
-                         if($protocoltype==6) $this->add_device($data['address']);
 
- //                        if($protocoltype==7){
 
- //                                 $this->remove_device_onenet($old['address']);
 
- //                                $onenet=$this->add_device_onenet($data['address']);
 
- //                                $onenetId= $onenet['data']['device_id'];
 
- //                                if($onenetId){
 
- //                                    $data['deviceid']=$onenetId;
 
- //                               }
 
- //                        }
 
-                     }
 
-                 }
 
-                 
 
-             }else{
 
-                 if ($old['type'] == 1) {
 
-                     $this->Network_model->delData(array('id'=>$old['networkid']));
 
-                     $cmdstr = '{"cmd_type":"delete_network_cmd","cmd_id":'.$old['networkid'].'}';
 
-                     if (isset($old['protocoltype']) && $old['protocoltype'] != 0) {
 
-                         $cmdret = send_cmd($cmdstr,1,0,$old['protocoltype']);
 
-                         if ($cmdret === false) {
 
-                             sleep(1);
 
-                             $cmdret = send_cmd($cmdstr,1,0,$old['protocoltype']);
 
-                         }
 
-                     }else{
 
-                         $cmdret = send_cmd($cmdstr,0,0,$old['protocoltype']);
 
-                         if ($cmdret === false) {
 
-                             sleep(1);
 
-                             $cmdret = send_cmd($cmdstr,0,0,$old['protocoltype']);
 
-                         }
 
-                     }
 
-                 }
 
-                 if ($old['networkid'] != $data['networkid']) {
 
-                     $new['networkid']  = $data['networkid'];
 
-                     $new['faultcount'] = $old['isfaulted'] > 0 ? 1 : 0;
 
-                     $this->Network_model->add_lamp_count($new);
 
-                     $old['faultcount'] = $new['faultcount'];
 
-                     $this->Network_model->minus_lamp_count($old);
 
-                     if ($old['type'] == 1) {
 
-                         // 删除原有网络
 
-                         $this->Network_model->delData(array('id'=>$old['networkid']));
 
-                         $cmdstr = '{"cmd_type":"delete_network_cmd","cmd_id":'.$old['networkid'].'}';
 
-                         if (isset($old['protocoltype']) && $old['protocoltype'] != 0) {
 
-                             $cmdret = send_cmd($cmdstr,1,0,$old['protocoltype']);
 
-                             if ($cmdret === false) {
 
-                                 sleep(1);
 
-                                 $cmdret = send_cmd($cmdstr,1,0,$old['protocoltype']);
 
-                             }
 
-                         }else{
 
-                             $cmdret = send_cmd($cmdstr,0,0,$old['protocoltype']);
 
-                             if ($cmdret === false) {
 
-                                 sleep(1);
 
-                                 $cmdret = send_cmd($cmdstr,0,0,$old['protocoltype']);
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-             
 
-             $data['updatetime'] = date("Y-m-d H:i:s",time());
 
-             $data['protocoltype'] = $lampprotocoltype;
 
-             $this->Lamp_model->update($where,$data);
 
-             $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
 
-             if (empty($data['networkid'])) {
 
-                 $networkData = $this->Network_model->getData(array('id'=>$old['networkid']),'networkname');
 
-             }else{
 
-                 $networkData = $this->Network_model->getData(array('id'=>$data['networkid']),'networkname');
 
-             }
 
-             $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯信息");
 
-             $this->add_operation_log('update',"Update lamp:\"{$data['number']}\".Wireless module address:\"{$data['address']}\".Project name:\"{$projectData['projectname']}\".Network name:\"{$networkData['networkname']}\".",0,1);
 
-             //更新负载设置和蓄电池设置
 
-             if (isset($_POST['workmode'])) {
 
-                 $res = $this->load_update();
 
-                 if ($res['result'] == 1) {
 
-                     $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯负载参数");
 
-                     $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);
 
-                 }else{
 
-                     exit(json_result($res['msg'],$res['msg'],array()));
 
-                 }
 
-             }
 
-             if (isset($_POST['capacity'])) {
 
-                 $res = $this->battery_update();
 
-                 if ($res['result'] == 1) {
 
-                     $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯蓄电池参数");
 
-                     $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);
 
-                 }else{
 
-                     exit(json_result($res['msg'],$res['msg'],array()));
 
-                 }
 
-             }
 
-             $lampId = intval($where['id']);
 
-         }
 
-         exit(json_result('0000', $this->response['0000'], array("id" => $lampId)));
 
-     }
 
-     // 获取灯控详情
 
-     public function detail() {
 
-         $lamp_id = $this->input->post('lamp_id', true);
 
-         $address = $this->input->post('address', true);
 
-         if (empty($lamp_id) && empty($address)) {
 
-             exit(json_result('0400', $this->response['0400'], array()));
 
-         }
 
-         if (empty($lamp_id)) {
 
-             $lamp = $this->Lamp_model->queryData(array('address'=>$address));
 
-             if (empty($lamp)) {
 
-                 exit(json_result('0006', $this->response['0006'], array()));
 
-             }
 
-             $lamp_id = $lamp['id'];
 
-         }
 
-         
 
-         $fields = "L.isfaulted,L.number,L.networkid as network_id,L.projectid,L.address,L.longitude,L.latitude,L.section,L.boardpower,L.polematerial,L.polediameter,L.poleheight,L.lighttype,L.lamptype,L.boardtype,L.batteryah,L.loadpower,L.status,N.id as networkid,L.updatetime,L.batterytype,L.status as lampstatus,L.temper as temper,L.sysvoltage,L.syscurrent,L.lampvoltage,L.lampcurrent,L.solarvoltage,L.solarpower,L.solarcurrent,L.daychargemincurrent,L.daydischargemincurrent,L.totalchargeah,L.battvoltage,L.batttemper,L.electricleft,L.voltagedaymin,L.voltagedaymax,L.battstatus,L.chargestage,L.overtimes,L.rundays,L.fulltimes,L.totalgeneration,L.totalconsumption,L.electricSOC,L.lighteness,L.lamppower,L.loadtype,N.protocoltype,N.devicetype,L.protocoltype as lampprotocoltype";
 
-         $res = $this->Lamp_model->getOne($lamp_id,$fields);
 
-         if (empty($res)) {
 
-             exit(json_result('0006', $this->response['0006'], array()));
 
-         }
 
-         $timezone = $this->Project_model->get_timezone_by_projectid($res['projectid']);
 
-         $res["updatetime"] = date_change($res["updatetime"],8,$timezone['value']);
 
-         $data = array(
 
-             "lamp_no" => $res["number"], 
 
-             "network_no" => $this->Network_model->getNameById($res["networkid"], "networkid"), 
 
-             "networkname" => $this->Network_model->getNameById($res["networkid"], "networkname"), 
 
-             "project_name" => $this->Project_model->getNameById($res["projectid"]), 
 
-             "project_id"=> $res['projectid'],
 
-             "isfaulted" => intval($res["isfaulted"]), 
 
-             "address" => $res["address"], 
 
-             "longitude" => $res["longitude"], 
 
-             "latitude" => $res["latitude"], 
 
-             "section" => $res["section"], 
 
-             "location" => $res["section"], 
 
-             "boardpower" => floatval($res["boardpower"]), 
 
-             "poleheight" => floatval($res['poleheight']),
 
-             "polediameter" => floatval($res['polediameter']),
 
-             "polematerial" => $res['polematerial'],
 
-             "lighttype" => $res['lighttype'],
 
-             "lamptype" => $res['lamptype'],
 
-             "boardtype" => $res['boardtype'], 
 
-             "batteryah" => floatval($res["batteryah"]),
 
-             "loadpower" => floatval($res["loadpower"]),
 
-             "network_id" => $res["networkid"],
 
-             'status' => $res['status'],
 
-             'batterytype' => $res['batterytype'],
 
-             'lampstatus'=>$res['lampstatus'],
 
-             'temper'=>intval($res['temper']),
 
-             'sysvoltage'=>floatval($res['sysvoltage']),
 
-             'syscurrent'=>floatval($res['syscurrent']),
 
-             'lampvoltage'=>floatval($res['lampvoltage']),
 
-             'lampcurrent'=>floatval($res['lampcurrent']),
 
-             'solarvoltage'=>floatval($res['solarvoltage']),
 
-             'solarpower'=>floatval($res['solarpower']),
 
-             'solarcurrent'=>floatval($res['solarcurrent']),
 
-             'lighttime'=>intval($res['daychargemincurrent']),
 
-             'chargetime'=>intval($res['daydischargemincurrent']),
 
-             'totalchargeah'=>intval($res['totalchargeah']),
 
-             'battvoltage'=>floatval($res['battvoltage']),
 
-             'batttemper'=>floatval($res['batttemper']),
 
-             'electricleft'=>floatval($res['electricleft']),
 
-             'voltagedaymin'=>floatval($res['voltagedaymin']),
 
-             'voltagedaymax'=>floatval($res['voltagedaymax']),
 
-             'battstatus'=>intval($res['battstatus']),
 
-             'chargestage'=>intval($res['chargestage']),
 
-             'overtimes'=>intval($res['overtimes']),
 
-             'rundays'=>intval($res['rundays']),
 
-             'fulltimes'=>intval($res['fulltimes']),
 
-             'totalgeneration'=>floatval($res['totalgeneration']),
 
-             'totalconsumption'=>floatval($res['totalconsumption']),
 
-             'electricSOC'=>intval($res['electricSOC']),
 
-             "updatetime" => $res["updatetime"], 
 
-             'lighteness' => intval($res['lighteness']),
 
-             'lamppower' => floatval($res['lamppower']),
 
-             'nightlength' => 10,
 
-             'solartime' => 10,
 
-             'loadtype' => $res['loadtype'],
 
-             'protocoltype' => intval($res['protocoltype']),
 
-             'devicetype' => intval($res['devicetype']),
 
-             'lampprotocoltype' => intval($res['lampprotocoltype']),
 
-         );
 
-         exit(json_result('0000',$this->response['0000'],$data));
 
-     }
 
-     // 获取负载参数设置
 
-     public function load_setting() {
 
-         $lamp_id = $this->input->post('lamp_id', true);
 
-         if (empty($lamp_id)) {
 
-             exit(json_result('0400', $this->response['0400'], array()));
 
-         }
 
-         $lampData = $this->Lamp_model->getOne($lamp_id,'N.status,N.protocoltype,L.protocoltype as lampprotocoltype,L.address,L.number,N.deviceid');
 
-         $cmd = '{"cmd_type":"get_load_setting_cmd","cmd_id":'.$lamp_id.',"broadcast":0}';
 
-         
 
-         if (($lampData['protocoltype'] == 4 || $lampData['protocoltype'] == 6) && $lampData['lampprotocoltype'] == 0) {
 
-             $address = '';
 
-             if (mb_strlen($lampData['address']) >= 8) {
 
-                 $address = mb_substr($lampData['address'], -8);
 
-             }else{
 
-                 $address = '00000000';
 
-             }
 
-             $sendData = $address.'0304001014';
 
-             
 
-             if ($lampData['protocoltype'] == 4) {
 
-                 $res = lampMqttCmd('/WE/TransIn/'.$lampData['address'],'/WE/TransOut/'.$lampData['address'],pack('H*','0001'.$sendData));
 
-             }else{
 
-                 device_cmd($lampData['deviceid'],pack('H*',$sendData));
 
-                 $cmd = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
 
-                 $info = send_lorawan_915($cmd);
 
-                 $res = array();
 
-                 if (!empty($info)) {
 
-                     $info = json_decode($info,true);
 
-                     if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
 
-                 }
 
-             }
 
-             
 
-             if (empty($res)) {
 
-                 exit(json_result('0016', $this->response['0016']));
 
-             }else{
 
-                 $msg1 = $res['msg'];
 
-                 // 数据校验
 
-                 $msg = mb_substr($msg1, 10);
 
-                 $t = 0;
 
-                 for ($i=0; $i < mb_strlen($msg) / 2 - 1; $i++) {
 
-                     $s = mb_substr($msg, $i*2,2);
 
-                     $t += base_convert($s, 16, 10);
 
-                 }
 
-                 $t = base_convert($t, 10, 16);
 
-                 if (mb_strlen($t) > 2) {
 
-                     $t = mb_substr($t, -2);
 
-                 }elseif($t <= 1){
 
-                     $t = '0'.$t;
 
-                 }
 
-                 if ($t == mb_substr($msg, -2)) {  
 
-                     $data = array();
 
-                     $dataStr = mb_substr($msg1, 16);
 
-                     $data['workmode']         = base_convert(mb_substr($dataStr, 0,2), 16, 10);
 
-                     $data['worktimefirst']    = base_convert(mb_substr($dataStr, 2,2), 16, 10);
 
-                     $data['workpowerfirst']   = base_convert(mb_substr($dataStr, 4,2), 16, 10);
 
-                     $data['worktimesencond']  = base_convert(mb_substr($dataStr, 6,2), 16, 10);
 
-                     $data['workpowersencond'] = base_convert(mb_substr($dataStr, 8,2), 16, 10);
 
-                     $data['worktimethird']    = base_convert(mb_substr($dataStr, 10,2), 16, 10);
 
-                     $data['workpowerthird']   = base_convert(mb_substr($dataStr, 12,2), 16, 10);
 
-                     $data['worktimeforth']    = base_convert(mb_substr($dataStr, 14,2), 16, 10);
 
-                     $data['workpowerforth']   = base_convert(mb_substr($dataStr, 16,2), 16, 10);
 
-                     $data['delaytime']        = base_convert(mb_substr($dataStr, 18,2), 16, 10);
 
-                     $data['voptically']       = round(base_convert(mb_substr($dataStr, 20,4), 16, 10)/10,1);
 
-                     $data['ledloadcurrent']   = round(base_convert(mb_substr($dataStr, 24,4), 16, 10),2);
 
-                     $data['powercmd']         = base_convert(mb_substr($dataStr, 28,2), 16, 10);
 
-                     $data['switchfeature']    = base_convert(mb_substr($dataStr, 30,2), 16, 10);
 
-                     exit(json_result('0000', $this->response['0000'], $data));
 
-                 }else{
 
-                     exit(json_result('0010', $this->response['0010']));
 
-                 }
 
-             }
 
-             
 
-         }
 
-         if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
 
-             $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
 
-             if ($cmdret === false) {
 
-                 sleep(1);
 
-                 $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
 
-             }
 
-         }else{
 
-             $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
 
-             if ($cmdret === false) {
 
-                 sleep(1);
 
-                 $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
 
-             }
 
-         }
 
-         if (empty($cmdret)) exit(json_result('0016',$this->response['0016']));
 
-         $r = json_decode($cmdret, true);
 
-         if ($r['result'] == false) {
 
-             $version = $this->session->userdata('version');
 
-             if (empty($version)) {
 
-                 $msg = empty($r['msg']) ? '未知错误' : transfer_error_tips($r['msg']);
 
-             }else{
 
-                 $msg = empty($r['msg']) ? 'Unknown error' : $r['msg'];
 
-             }
 
-             exit(json_result('other', $msg));
 
-         }
 
-         $res = $this->Load_model->get_data_by_filter(['relateid'=>$lamp_id,'cmdtype'=>0]);
 
-         if (empty($res)) {
 
-             $update['workmode']         = '0';
 
-             $update['worktimefirst']    = '4';
 
-             $update['workpowerfirst']   = '80';
 
-             $update['worktimesencond']  = '1';
 
-             $update['workpowersencond'] = '40';
 
-             $update['worktimethird']    = '8';
 
-             $update['workpowerthird']   = '30';
 
-             $update['worktimeforth']    = '0';
 
-             $update['workpowerforth']   = '30';
 
-             $update['voptically']       = '5';
 
-             $update['delaytime']        = '1';
 
-             $update['ledloadcurrent']   = '330';
 
-             $update['powercmd']         = '1';
 
-             $update['switchfeature']    = '1';
 
-             $res = $update;
 
-         }
 
-         unset($res['id']);
 
-         unset($res['updatetime']);
 
-         $data = $res;
 
-         
 
-         exit(json_result('0000', $this->response['0000'], $data));
 
-     }
 
-     // 新控制器获取参数设置
 
-     public function modbus_get_load() {
 
-         $lamp_id = $this->input->post('lamp_id', true);
 
-         if (empty($lamp_id)) {
 
-             exit(json_result('0400', $this->response['0400'], array()));
 
-         }
 
-         // 
 
-         $lampData = $this->Lamp_model->getOne($lamp_id,'N.status,N.deviceid,N.protocoltype,L.address,L.protocoltype as lampprotocoltype,L.number');
 
-         $cmd = '{"cmd_type":"get_load_setting_cmd","cmd_id":'.$lamp_id.',"broadcast":0}';
 
-         if ($lampData['protocoltype'] == 4 && $lampData['lampprotocoltype'] == 1) {
 
-             $sendData = '0103e08d0027a23b';
 
-             
 
-             $res = lampMqttCmd('/WE/TransIn/'.$lampData['address'],'/WE/TransOut/'.$lampData['address'],pack('H*','0001'.$sendData));
 
-             if (empty($res)) {
 
-                 exit(json_result('0016', $this->response['0016']));
 
-             }else{
 
-                 if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
 
-                     $data = array();
 
-                     $data['load_current'] = number_format(base_convert(substr($res['msg'], 6,4), 16, 10)/100,2).''; // 负载电流
 
-                     $data['intell_power'] = base_convert(substr($res['msg'], 10,4), 16, 10).''; // 智能功率
 
-                     $data['light_delay'] = base_convert(substr($res['msg'], 14,4), 16, 10).''; // 光控延时
 
-                     $data['induction_delay'] = base_convert(substr($res['msg'], 18,4), 16, 10).''; // 感应延时
 
-                     $data['first_light_time'] = base_convert(substr($res['msg'], 26,4), 16, 10).''; // 第一段亮灯时间
 
-                     $data['first_light_human_power'] = base_convert(substr($res['msg'], 30,4), 16, 10).''; // 第一段有人功率
 
-                     $data['first_light_unman_power'] = base_convert(substr($res['msg'], 34,4), 16, 10).''; // 第一段无人功率
 
-                     $data['second_light_time'] = base_convert(substr($res['msg'], 38,4), 16, 10).''; // 第二段亮灯时间
 
-                     $data['second_light_human_power'] = base_convert(substr($res['msg'], 42,4), 16, 10).''; // 第二段有人功率
 
-                     $data['second_light_unman_power'] = base_convert(substr($res['msg'], 46,4), 16, 10).''; // 第二段无人功率
 
-                     $data['third_light_time'] = base_convert(substr($res['msg'], 50,4), 16, 10).''; // 第三段亮灯时间
 
-                     $data['third_light_human_power'] = base_convert(substr($res['msg'], 54,4), 16, 10).''; // 第三段有人功率
 
-                     $data['third_light_unman_power'] = base_convert(substr($res['msg'], 58,4), 16, 10).''; // 第三段无人功率
 
-                     $data['fourth_light_time'] = base_convert(substr($res['msg'], 62,4), 16, 10).''; // 第四段亮灯时间
 
-                     $data['fourth_light_human_power'] = base_convert(substr($res['msg'], 66,4), 16, 10).''; // 第四段有人功率
 
-                     $data['fourth_light_unman_power'] = base_convert(substr($res['msg'], 70,4), 16, 10).''; // 第四段无人功率
 
-                     $data['fifth_light_time'] = base_convert(substr($res['msg'], 74,4), 16, 10).''; // 第五段亮灯时间
 
-                     $data['fifth_light_human_power'] = base_convert(substr($res['msg'], 78,4), 16, 10).''; // 第五段有人功率
 
-                     $data['fifth_light_unman_power'] = base_convert(substr($res['msg'], 82,4), 16, 10).''; // 第五段无人功率
 
-                     $data['sixth_light_time'] = base_convert(substr($res['msg'], 86,4), 16, 10).''; // 第六段亮灯时间
 
-                     $data['sixth_light_human_power'] = base_convert(substr($res['msg'], 90,4), 16, 10).''; // 第六段有人功率
 
-                     $data['sixth_light_unman_power'] = base_convert(substr($res['msg'], 94,4), 16, 10).''; // 第六段无人功率
 
-                     $data['seventh_light_time'] = base_convert(substr($res['msg'], 98,4), 16, 10).''; // 第七段亮灯时间
 
-                     $data['seventh_light_human_power'] = base_convert(substr($res['msg'], 102,4), 16, 10).''; // 第七段有人功率
 
-                     $data['seventh_light_unman_power'] = base_convert(substr($res['msg'], 106,4), 16, 10).''; // 第七段无人功率
 
-                     $data['eighth_light_time'] = base_convert(substr($res['msg'], 110,4), 16, 10).''; // 第八段亮灯时间
 
-                     $data['eighth_light_human_power'] = base_convert(substr($res['msg'], 114,4), 16, 10).''; // 第八段有人功率
 
-                     $data['eighth_light_unman_power'] = base_convert(substr($res['msg'], 118,4), 16, 10).''; // 第八段无人功率
 
-                     $data['ninth_light_time'] = base_convert(substr($res['msg'], 122,4), 16, 10).''; // 第九段亮灯时间
 
-                     $data['ninth_light_human_power'] = base_convert(substr($res['msg'], 126,4), 16, 10).''; // 第九段有人功率
 
-                     $data['ninth_light_unman_power'] = base_convert(substr($res['msg'], 130,4), 16, 10).''; // 第九段无人功率
 
-                     $data['tenth_light_time'] = base_convert(substr($res['msg'], 134,4), 16, 10).''; // 晨亮时间
 
-                     $data['tenth_light_human_power'] = base_convert(substr($res['msg'], 138,4), 16, 10).''; // 晨亮有人功率
 
-                     $data['tenth_light_unman_power'] = base_convert(substr($res['msg'], 142,4), 16, 10).''; // 晨亮无人功率
 
-                     $data['begin_voltage'] = round(base_convert(substr($res['msg'], 146,4), 16, 10)/10,1).''; // 智能功率USER1起始电压
 
-                     $data['end_voltage'] = round(base_convert(substr($res['msg'], 150,4), 16, 10)/10,1).''; // 智能功率USER1终止电压
 
-                     $data['end_current'] = round(base_convert(substr($res['msg'], 158,4), 16, 10)/100,2).''; // 智能功率USER1终止电流
 
-                     $res = $data;
 
-                 }else{
 
-                     exit(json_result('0010', $this->response['0010']));
 
-                 }
 
-             }
 
-             
 
-         }
 
-         elseif($lampData['protocoltype'] == 6 && $lampData['lampprotocoltype'] == 1){
 
-             //$sendData = '0103e08d0027a23ba5';
 
-            $cmdLine='0103e08d0027a23b';
 
-            //$cmdLine .= crc16(pack('H*',$cmdLine));
 
-            device_cmd($lampData['deviceid'],pack('H*',$cmdLine));
 
-             $cmdLine = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
 
-            $res=send_lorawan_915($cmdLine);
 
-            if($res){
 
-                $res=json_decode($res,true);
 
-                if($res['info']=='success'){
 
-                    $res = $this->Modbus_load_model->get_data_by_filter(['relateid'=>$lamp_id,'cmdtype'=>0]);
 
-                }else{
 
-                    exit(json_result('0010', $this->response['0010']));
 
-                }
 
-            }else{
 
-                exit(json_result('0010', $this->response['0010']));
 
-             }
 
-         }
 
-         else{
 
-             $cmdret = '';
 
-             if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
 
-                 $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
 
-                 if ($cmdret === false) {
 
-                     sleep(1);
 
-                     $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
 
-                 }
 
-             }else{
 
-                 $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
 
-                 if ($cmdret === false) {
 
-                     sleep(1);
 
-                     $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
 
-                 }
 
-             }
 
-             if (empty($cmdret)) exit(json_result('0016',$this->response['0016']));
 
-             $r = json_decode($cmdret, true);
 
-             if ($r['result'] == false) {
 
-                 $version = $this->session->userdata('version');
 
-                 if (empty($version)) {
 
-                     $msg = empty($r['msg']) ? '未知错误' : transfer_error_tips($r['msg']);
 
-                 }else{
 
-                     $msg = empty($r['msg']) ? 'Unknown error' : $r['msg'];
 
-                 }
 
-                 exit(json_result('other', $msg));
 
-             }
 
-             $res = $this->Modbus_load_model->get_data_by_filter(['relateid'=>$lamp_id,'cmdtype'=>0]);
 
-             if (empty($res)) {
 
-                 exit(json_result('0417', $this->response['0417']));
 
-             }
 
-         }
 
-         
 
-         $res['load_current'] = number_format($res['load_current'],2);
 
-         $res['end_current'] = number_format($res['end_current'],2);
 
-         $res['end_voltage'] = number_format($res['end_voltage'],1);
 
-         $res['begin_voltage'] = number_format($res['begin_voltage'],1);
 
-         $res['first_light_time'] = $this->format_date_en($res['first_light_time']);
 
-         $res['second_light_time'] = $this->format_date_en($res['second_light_time']);
 
-         $res['third_light_time'] = $this->format_date_en($res['third_light_time']);
 
-         $res['fourth_light_time'] = $this->format_date_en($res['fourth_light_time']);
 
-         $res['fifth_light_time'] = $this->format_date_en($res['fifth_light_time']);
 
-         $res['sixth_light_time'] = $this->format_date_en($res['sixth_light_time']);
 
-         $res['seventh_light_time'] = $this->format_date_en($res['seventh_light_time']);
 
-         $res['eighth_light_time'] = $this->format_date_en($res['eighth_light_time']);
 
-         $res['ninth_light_time'] = $this->format_date_en($res['ninth_light_time']);
 
-         $res['tenth_light_time'] = $this->format_date_en($res['tenth_light_time']);
 
-         unset($res['id']);
 
-         unset($res['updatetime']);
 
-         $data = $res;
 
-         
 
-         exit(json_result('0000', $this->response['0000'], $data));
 
-     }
 
-     private function format_date_en($date){
 
-         $h = floor($date/3600);
 
-         $h = $h > 10 ? $h : '0'.$h;
 
-         $t = floor($date%3600/60);
 
-         $t = $t > 10 ? $t : '0'.$t;
 
-         return $h.':'.$t;
 
-     }
 
-     // 获取默认参数值
 
-     public function get_data(){
 
-         $type = $this->input->post('type',true);
 
-         $type = empty($type) ? 1 : $type;
 
-         $update = array();
 
-         if ($type == 1) { // 负载参数
 
-             $update['workmode']         = '0';
 
-             $update['worktimefirst']    = '4';
 
-             $update['workpowerfirst']   = '80';
 
-             $update['worktimesencond']  = '1';
 
-             $update['workpowersencond'] = '40';
 
-             $update['worktimethird']    = '8';
 
-             $update['workpowerthird']   = '30';
 
-             $update['worktimeforth']    = '0';
 
-             $update['workpowerforth']   = '30';
 
-             $update['voptically']       = '5';
 
-             $update['delaytime']        = '1';
 
-             $update['ledloadcurrent']   = '330';
 
-             $update['powercmd']         = '1';
 
-             $update['switchfeature']    = '1';
 
-         }else{  // 蓄电池参数
 
-             $update['batterytype']              = '0';//蓄电池类型
 
-             $update['capacity0']                 = '200';//蓄电池容量
 
-             $update['capacity1']                 = '';//蓄电池容量
 
-             $update['vovervoltage']             = '16';//超压电压 
 
-             $update['vlimitedcharge']           = '15.5';//充电限制电压
 
-             $update['vbalancecharge']           = '14.6';//均衡充电电压
 
-             $update['vpromotecharge0']           = '14.4';//提升充电电压/过充电压(锂电池)
 
-             $update['vpromotecharge1']           = '12.6';//提升充电电压/过充电压(锂电池)
 
-             $update['vfloatingcharge']          = '13.8';//浮充充电电压
 
-             $update['vpromoterecover0']          = '13.2';//提升充电恢复电压/过充返回电压(锂电池)
 
-             $update['vpromoterecover1']          = '12';//提升充电恢复电压/过充返回电压(锂电池)
 
-             $update['voverdischargerecover0']    = '12.6';//过放恢复电压
 
-             $update['voverdischargerecover1']    = '11';//过放恢复电压
 
-             $update['vundervoltagewarn']        = '12';//欠压警告电压
 
-             $update['voverdischarge0']           = '11';//过放电压
 
-             $update['voverdischarge1']           = '9';//过放电压
 
-             $update['balancechargetime']        = '1';//均衡充电时间
 
-             $update['promotechargetime']        = '4';//提升充电时间 
 
-             $update['balanceinterval']          = '30';//均衡充电间隔
 
-             $update['tempcompensation']         = '-4';//温度补偿系数
 
-             $update['tempcompmax']              = '99';//温度补偿最高温度 
 
-             $update['tempcompmin']              = '-40';//温度补偿最低温度 
 
-             $update['turnfloatingcurrent']      = '20';//转浮充的电流 
 
-             $update['vsystem']                  = '12';//系统电压
 
-             $update['minusnocharge']            = '0';//零下禁充
 
-             $update['chargemode']               = '0';//充电模式
 
-         }
 
-         exit(json_result('0000',$this->response['0000'],$update));
 
-     }
 
-     // 新控制器获取默认参数值
 
-     public function modbus_get_data(){
 
-         $type = $this->input->post('type',true);
 
-         $type = empty($type) ? 1 : $type;
 
-         $update = array();
 
-         if ($type == 1) { // 负载参数
 
-             $update['intell_power']    = '2';
 
-             $update['load_current']   = '0.9';
 
-             $update['light_delay']  = '20';
 
-             $update['induction_delay'] = '20';
 
-             $update['first_light_time']    = '00:30';
 
-             $update['first_light_human_power']   = '50';
 
-             $update['first_light_unman_power']    = '20';
 
-             $update['second_light_time']   = '00:30';
 
-             $update['second_light_human_power']       = '70';
 
-             $update['second_light_unman_power']        = '30';
 
-             $update['third_light_time']   = '02:00';
 
-             $update['third_light_human_power']         = '100';
 
-             $update['third_light_unman_power']    = '50';
 
-             $update['fourth_light_time']    = '00:30';
 
-             $update['fourth_light_human_power']    = '70';
 
-             $update['fourth_light_unman_power']    = '30';
 
-             $update['fifth_light_time']    = '00:30';
 
-             $update['fifth_light_human_power']    = '70';
 
-             $update['fifth_light_unman_power']    = '30';
 
-             $update['sixth_light_time']    = '04:00';
 
-             $update['sixth_light_human_power']    = '30';
 
-             $update['sixth_light_unman_power']    = '10';
 
-             $update['seventh_light_time']    = '00:00';
 
-             $update['seventh_light_human_power']    = '100';
 
-             $update['seventh_light_unman_power']    = '50';
 
-             $update['eighth_light_time']    = '00:00';
 
-             $update['eighth_light_human_power']    = '100';
 
-             $update['eighth_light_unman_power']    = '50';
 
-             $update['ninth_light_time']    = '00:00';
 
-             $update['ninth_light_human_power']    = '100';
 
-             $update['ninth_light_unman_power']    = '50';
 
-             $update['tenth_light_time']    = '00:00';
 
-             $update['tenth_light_human_power']    = '50';
 
-             $update['tenth_light_unman_power']    = '20';
 
-             $update['begin_voltage']    = '11.5';
 
-             $update['end_voltage']    = '10.0';
 
-             $update['end_current']    = '0.15';
 
-         }else{  // 蓄电池参数
 
-             $update['light_voltage']         = '5';
 
-             $update['system_voltage']              = '12';
 
-             $update['battery_type']                 = '0';
 
-             $update['li_battery_type']        = '0';
 
-             $update['overvoltage']                 = '15.5';
 
-             $update['char_limit_voltage']             = '15.0'; 
 
-             $update['equ_char_voltage']           = '14.6';
 
-             $update['increa_char_voltage']           = '14.4';
 
-             $update['float_char_voltage']           = '13.8';
 
-             $update['increa_char_return_voltage']           = '13.2';
 
-             $update['over_dischar_return_voltage']          = '12.6';
 
-             $update['over_dischar_voltage']          = '11.1';
 
-             $update['equ_char_time']          = '120';
 
-             $update['increa_char_time']    = '120';
 
-             $update['balan_char_interval']    = '30';
 
-             $update['temper_compen_coeff']        = '5';
 
-             $update['char_upper_limit_temper']           = '60';
 
-             $update['char_lower_limit_temper']           = '-30';
 
-             $update['dischar_upper_limit_temper']        = '60';
 
-             $update['dischar_lower_limit_temper']        = '-30';
 
-         }
 
-         exit(json_result('0000',$this->response['0000'],$update));
 
-     }
 
-     // 获取蓄电池设置
 
-     public function battery_setting() {
 
-         $lamp_id = $this->input->post('lamp_id', true);
 
-         if (empty($lamp_id)) {
 
-             exit(json_result('0400', $this->response['0400'], array()));
 
-         }
 
-         $lampData = $this->Lamp_model->getOne($lamp_id,'N.status,N.protocoltype,L.address,L.protocoltype as lampprotocoltype,L.number,N.deviceid');
 
-         // if ($lampData['status'] == 0) {
 
-         //     exit(json_result('0412',$this->response['0412'],array()));
 
-         // }
 
-         $cmd = '{"cmd_type":"get_battery_setting_cmd","cmd_id":'.$lamp_id.',"broadcast":0}';
 
-         if (($lampData['protocoltype'] == 4 || $lampData['protocoltype'] == 6) && $lampData['lampprotocoltype'] == 0) {
 
-             $address = '';
 
-             if (mb_strlen($lampData['address']) >= 8) {
 
-                 $address = mb_substr($lampData['address'], -8);
 
-             }else{
 
-                 $address = '00000000';
 
-             }
 
-             $sendData = $address.'030328204b';
 
-             
 
-             if ($lampData['protocoltype'] == 4) {
 
-                 $res = lampMqttCmd('/WE/TransIn/'.$lampData['address'],'/WE/TransOut/'.$lampData['address'],pack('H*','0001'.$sendData));
 
-             }else{
 
-                 device_cmd($lampData['deviceid'],pack('H*',$sendData));
 
-                 $cmd = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
 
-                 $info = send_lorawan_915($cmd);
 
-                 $res = array();
 
-                 if (!empty($info)) {
 
-                     $info = json_decode($info,true);
 
-                     if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
 
-                 }
 
-             }
 
-             if (empty($res)) {
 
-                 exit(json_result('0016', $this->response['0016']));
 
-             }else{
 
-                 $msg1 = $res['msg'];
 
-                 // 数据校验
 
-                 $msg = mb_substr($msg1, 10);
 
-                 $t = 0;
 
-                 for ($i=0; $i < mb_strlen($msg) / 2 - 1; $i++) {
 
-                     $s = mb_substr($msg, $i*2,2);
 
-                     $t += base_convert($s, 16, 10);
 
-                 }
 
-                 $t = base_convert($t, 10, 16);
 
-                 if (mb_strlen($t) > 2) {
 
-                     $t = mb_substr($t, -2);
 
-                 }elseif($t <= 1){
 
-                     $t = '0'.$t;
 
-                 }
 
-                 if ($t == mb_substr($msg, -2)) {  
 
-                     $data = array();
 
-                     $dataStr = mb_substr($msg1, 16);
 
-                     $data['batterytype']              = base_convert(mb_substr($dataStr, 56,2), 16, 10);//蓄电池类型
 
-                     $data['capacity']                 = base_convert(mb_substr($dataStr, 0,4), 16, 10);//蓄电池容量
 
-                     $data['capacity1']                 = base_convert(mb_substr($dataStr, 0,4), 16, 10);//蓄电池容量
 
-                     $data['vovervoltage']             = round(base_convert(mb_substr($dataStr, 4,4), 16, 10)/10,1);//超压电压 
 
-                     $data['vlimitedcharge']           = round(base_convert(mb_substr($dataStr, 8,4), 16, 10)/10,1);//充电限制电压
 
-                     $data['vbalancecharge']           = round(base_convert(mb_substr($dataStr, 12,4), 16, 10)/10,1);//均衡充电电压
 
-                     $data['vpromotecharge0']           = round(base_convert(mb_substr($dataStr, 16,4), 16, 10)/10,1);//提升充电电压/过充电压(锂电池)
 
-                     $data['vpromotecharge1']           = round(base_convert(mb_substr($dataStr, 16,4), 16, 10)/10,1);//提升充电电压/过充电压(锂电池)
 
-                     $data['vfloatingcharge']          = round(base_convert(mb_substr($dataStr, 20,4), 16, 10)/10,1);//浮充充电电压
 
-                     $data['vpromoterecover0']          = round(base_convert(mb_substr($dataStr, 24,4), 16, 10)/10,1);//提升充电恢复电压/过充返回电压(锂电池)
 
-                     $data['vpromoterecover1']          = round(base_convert(mb_substr($dataStr, 24,4), 16, 10)/10,1);//提升充电恢复电压/过充返回电压(锂电池)
 
-                     $data['voverdischargerecover0']    = round(base_convert(mb_substr($dataStr, 28,4), 16, 10)/10,1);//过放恢复电压
 
-                     $data['voverdischargerecover1']    = round(base_convert(mb_substr($dataStr, 28,4), 16, 10)/10,1);//过放恢复电压
 
-                     $data['vundervoltagewarn']        = round(base_convert(mb_substr($dataStr, 32,4), 16, 10)/10,1);//欠压警告电压
 
-                     $data['voverdischarge0']           = round(base_convert(mb_substr($dataStr, 36,4), 16, 10)/10,1);//过放电压
 
-                     $data['voverdischarge1']           = round(base_convert(mb_substr($dataStr, 36,4), 16, 10)/10,1);//过放电压
 
-                     $data['balancechargetime']        = base_convert(mb_substr($dataStr, 40,2), 16, 10);//均衡充电时间
 
-                     $data['promotechargetime']        = base_convert(mb_substr($dataStr, 42,2), 16, 10);//提升充电时间 
 
-                     $data['balanceinterval']          = base_convert(mb_substr($dataStr, 44,2), 16, 10);//均衡充电间隔
 
-                     $data['tempcompensation']         = base_convert(mb_substr($dataStr, 46,2), 16, 10);//温度补偿系数
 
-                     $data['tempcompmax']              = bitDecode(mb_substr($dataStr, 48,2));//温度补偿最高温度 
 
-                     $data['tempcompmin']              = bitDecode(mb_substr($dataStr, 50,2));//温度补偿最低温度 
 
-                     $data['turnfloatingcurrent']      = round(base_convert(mb_substr($dataStr, 52,4), 16, 10)/100,2);//转浮充的电流 
 
-                     $data['vsystem']                  = base_convert(mb_substr($dataStr, 58,2), 16, 10);//系统电压
 
-                     $data['minusnocharge']            = base_convert(mb_substr($dataStr, 60,2), 16, 10);//零下禁充
 
-                     $data['chargemode']               = base_convert(mb_substr($dataStr, 62,2), 16, 10);//充电模式
 
-                     exit(json_result('0000', $this->response['0000'], $data));
 
-                 }else{
 
-                     exit(json_result('0010', $this->response['0010']));
 
-                 }
 
-             }
 
-             
 
-         }
 
-         if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
 
-             $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
 
-             if ($cmdret === false) {
 
-                 sleep(1);
 
-                 $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
 
-             }
 
-         }else{
 
-             $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
 
-             if ($cmdret === false) {
 
-                 sleep(1);
 
-                 $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
 
-             }
 
-         }
 
-         if (empty($cmdret)) exit(json_result('0016',$this->response['0016']));
 
-         $r = json_decode($cmdret, true);
 
-         if ($r['result'] == false) {
 
-             $version = $this->session->userdata('version');
 
-             if (empty($version)) {
 
-                 $msg = empty($r['msg']) ? '未知错误' : transfer_error_tips($r['msg']);
 
-             }else{
 
-                 $msg = empty($r['msg']) ? 'Unknown error' : $r['msg'];
 
-             }
 
-             exit(json_result('other', $msg));
 
-         }
 
-         $res = $this->Battery_model->getOne($lamp_id);
 
-         if (empty($res)) {
 
-             $update['batterytype']              = '0';//蓄电池类型
 
-             $update['capacity0']                 = '200';//蓄电池容量
 
-             $update['capacity1']                 = '';//蓄电池容量
 
-             $update['vovervoltage']             = '16';//超压电压 
 
-             $update['vlimitedcharge']           = '15.5';//充电限制电压
 
-             $update['vbalancecharge']           = '14.6';//均衡充电电压
 
-             $update['vpromotecharge0']           = '14.4';//提升充电电压/过充电压(锂电池)
 
-             $update['vpromotecharge1']           = '12.6';//提升充电电压/过充电压(锂电池)
 
-             $update['vfloatingcharge']          = '13.8';//浮充充电电压
 
-             $update['vpromoterecover0']          = '13.2';//提升充电恢复电压/过充返回电压(锂电池)
 
-             $update['vpromoterecover1']          = '12';//提升充电恢复电压/过充返回电压(锂电池)
 
-             $update['voverdischargerecover0']    = '12.6';//过放恢复电压
 
-             $update['voverdischargerecover1']    = '11';//过放恢复电压
 
-             $update['vundervoltagewarn']        = '12';//欠压警告电压
 
-             $update['voverdischarge0']           = '11';//过放电压
 
-             $update['voverdischarge1']           = '9';//过放电压
 
-             $update['balancechargetime']        = '1';//均衡充电时间
 
-             $update['promotechargetime']        = '4';//提升充电时间 
 
-             $update['balanceinterval']          = '30';//均衡充电间隔
 
-             $update['tempcompensation']         = '-4';//温度补偿系数
 
-             $update['tempcompmax']              = '99';//温度补偿最高温度 
 
-             $update['tempcompmin']              = '-40';//温度补偿最低温度 
 
-             $update['turnfloatingcurrent']      = '20';//转浮充的电流 
 
-             $update['vsystem']                  = '12';//系统电压
 
-             $update['minusnocharge']            = '0';//零下禁充
 
-             $update['chargemode']               = '0';//充电模式
 
-             $res = $update;
 
-         }
 
-         $data = $res;
 
-         unset($data['id']);
 
-         unset($data['updatetime']);
 
-         exit(json_result('0000', $this->response['0000'], $data));
 
-     }
 
-     // 新控制器获取蓄电池设置
 
-     public function modbus_get_battery() {
 
-         $lamp_id = $this->input->post('lamp_id', true);
 
-         if (empty($lamp_id)) {
 
-             exit(json_result('0400', $this->response['0400'], array()));
 
-         }
 
-         $lampData = $this->Lamp_model->getOne($lamp_id,'N.status,N.protocoltype,N.deviceid,L.address,L.protocoltype as lampprotocoltype,L.number');
 
-         // if ($lampData['status'] == 0) {
 
-         //     exit(json_result('0412',$this->response['0412'],array()));
 
-         // }
 
-         $cmd = '{"cmd_type":"get_battery_setting_cmd","cmd_id":'.$lamp_id.',"broadcast":0}';
 
-         if ($lampData['protocoltype'] == 4 && $lampData['lampprotocoltype'] == 1) {
 
-             $sendData = '0103e0020020d212';
 
-             
 
-             $res = lampMqttCmd('/WE/TransIn/'.$lampData['address'],'/WE/TransOut/'.$lampData['address'],pack('H*','0001'.$sendData));
 
-             if (empty($res)) {
 
-                 exit(json_result('0016', $this->response['0016']));
 
-             }else{
 
-                 
 
-                 if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
 
-                     $data = array();
 
-                     $data['system_voltage'] = base_convert(substr($res['msg'], 10,4), 16, 10).''; // 系统电压
 
-                     if (substr('00000000'.base_convert(substr($res['msg'], 132,2), 16, 2), -8,1) == 1) {  // 电池类型
 
-                         $data['battery_type'] = '1';
 
-                     }else{
 
-                         $data['battery_type'] = '0';
 
-                     }
 
-                     $data['overvoltage'] = round(base_convert(substr($res['msg'], 18,4), 16, 10)/10,1).''; // 超压电压
 
-                     $data['char_limit_voltage'] = round(base_convert(substr($res['msg'], 22,4), 16, 10)/10,1).''; // 充电限制电压
 
-                     $data['equ_char_voltage'] = round(base_convert(substr($res['msg'], 26,4), 16, 10)/10,1).''; // 均衡充电电压
 
-                     $data['increa_char_voltage'] = round(base_convert(substr($res['msg'], 30,4), 16, 10)/10,1).''; // 提升充电电压
 
-                     $data['float_char_voltage'] = round(base_convert(substr($res['msg'], 34,4), 16, 10)/10,1).''; // 浮充充电电压
 
-                     $data['increa_char_return_voltage'] = round(base_convert(substr($res['msg'], 38,4), 16, 10)/10,1).''; // 提升充电返回电压
 
-                     $data['over_dischar_return_voltage'] = round(base_convert(substr($res['msg'], 42,4), 16, 10)/10,1).''; // 过放返回电压
 
-                     $data['over_dischar_voltage'] = round(base_convert(substr($res['msg'], 50,4), 16, 10)/10,1).''; // 过放电压
 
-                     $data['equ_char_time'] = base_convert(substr($res['msg'], 66,4), 16, 10).''; // 均衡充电时间
 
-                     $data['increa_char_time'] = base_convert(substr($res['msg'], 70,4), 16, 10).''; // 提升充电时间
 
-                     $data['balan_char_interval'] = base_convert(substr($res['msg'], 74,4), 16, 10).''; // 均衡充电间隔
 
-                     $data['temper_compen_coeff'] = base_convert(substr($res['msg'], 78,4), 16, 10).''; // 温度补偿系数
 
-                     $data['char_upper_limit_temper'] = bitDecode(substr($res['msg'], 84,2)).''; // 充电上限温度
 
-                     $data['char_lower_limit_temper'] = bitDecode(substr($res['msg'], 88,2)).''; // 充电下限温度
 
-                     $data['dischar_upper_limit_temper'] = bitDecode(substr($res['msg'], 92,2)).''; // 放电上限温度
 
-                     $data['dischar_lower_limit_temper'] = bitDecode(substr($res['msg'], 96,2)).''; // 放电下限温度
 
-                     $data['light_voltage'] = base_convert(substr($res['msg'], 122,4),16,10).''; // 光控电压
 
-                     $info = $this->Modbus_battery_model->getOne($lamp_id);
 
-                     if (empty($info)) {
 
-                         $data['li_battery_type'] = '0';
 
-                     }else{
 
-                         $data['li_battery_type'] = $info['li_battery_type'];
 
-                     }
 
-                     $res = $data;
 
-                 }else{
 
-                     exit(json_result('0010', $this->response['0010']));
 
-                 }
 
-             }
 
-             
 
-         }
 
-         elseif ($lampData['protocoltype'] == 6 && $lampData['lampprotocoltype'] == 1){
 
-             $sendData = '0103e0020020d212';
 
-             $cmdLine='0103e0020020d212';
 
-             //$cmdLine .= crc16(pack('H*',$cmdLine));
 
-             device_cmd($lampData['deviceid'],pack('H*',$cmdLine));
 
-             $cmdLine = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
 
-             $res=send_lorawan_915($cmdLine);
 
-             if($res){
 
-                 $res=json_decode($res,true);
 
-                 //var_dump($res);exit();
 
-                 if($res['info']=='success'){
 
-                     $res =$this->Modbus_battery_model->getOne($lamp_id);
 
-                     if (empty($res)) {
 
-                         $res['li_battery_type'] = '0';
 
-                     }
 
-                     
 
-                 }else{
 
-                     exit(json_result('0010', $this->response['0010']));
 
-                 }
 
-             }else{
 
-                 exit(json_result('0010', $this->response['0010']));
 
-             }
 
-         }
 
-         else{
 
-             if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
 
-                 $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
 
-                 if ($cmdret === false) {
 
-                     sleep(1);
 
-                     $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
 
-                 }
 
-             }else{
 
-                 $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
 
-                 if ($cmdret === false) {
 
-                     sleep(1);
 
-                     $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
 
-                 }
 
-             }
 
-             if (empty($cmdret)) exit(json_result('0016',$this->response['0016']));
 
-             $r = json_decode($cmdret, true);
 
-             if ($r['result'] == false) {
 
-                 $version = $this->session->userdata('version');
 
-                 if (empty($version)) {
 
-                     $msg = empty($r['msg']) ? '未知错误' : transfer_error_tips($r['msg']);
 
-                 }else{
 
-                     $msg = empty($r['msg']) ? 'Unknown error' : $r['msg'];
 
-                 }
 
-                 exit(json_result('other', $msg));
 
-             }
 
-             $res = $this->Modbus_battery_model->getOne($lamp_id);
 
-             if (empty($res)) {
 
-                 exit(json_result('0417', $this->response['0417']));
 
-             }
 
-         }
 
-         
 
-         $data = $res;
 
-         $data['equ_char_time'] = intval($data['equ_char_time']/60).'';
 
-         $data['increa_char_time'] = intval($data['increa_char_time']/60).'';
 
-         unset($data['id']);
 
-         unset($data['updatetime']);
 
-         exit(json_result('0000', $this->response['0000'], $data));
 
-     }
 
-     // 灯控相关数据
 
-     public function viewloglist() {
 
-         $lamp_id = $this->input->post('lamp_id',true);
 
-         $datatype = $this->input->post('datatype',true);
 
-         if (empty($lamp_id)) {
 
-             exit(json_result('0400', $this->response['0400'], array()));
 
-         }
 
-         if ($datatype == 'lampinfo') {
 
-             $fields = "L.isfaulted,L.number,L.networkid as network_id,L.projectid,L.address,L.longitude,L.latitude,L.section,L.boardpower,L.polematerial,L.polediameter,L.poleheight,L.lighttype,L.lamptype,L.boardtype,L.batteryah,L.loadpower,L.devstatus,N.id as networkid,L.logtime as updatetime,L.batterytype,L.devicetype,N.protocoltype,L.protocoltype as lampprotocoltype,N.status,N.simid,N.packageSurplus,N.monthly_can_usage,N.monthly_already_usage,L.loadtype";
 
-             $res = $this->Lamp_model->getOne($lamp_id,$fields);
 
-             if (empty($res)) {
 
-                 exit(json_result('0000', $this->response['0006'], array()));
 
-             }
 
-             $version = $this->session->userdata('version');
 
-             if (!empty($version)) {
 
-                 $polemaType = ['冷镀锌'=>'Cold galvanizing','热镀锌'=>'Hot galvanizing','不锈钢'=>'Stainless steel'];
 
-                 $lightType = ['LED灯'=>'LED lamp','钠灯'=>'Sodium lamp','节能灯'=>'Energy saving light'];
 
-                 $lampType = ['太阳能路灯'=>'Solar street lamp','景观灯'=>'Landscape lamp','庭院灯'=>'Garden light'];
 
-                 $boardType = ['单晶板'=>'Single crystal plate','多晶板'=>'Polycrystalline plate','非晶板'=>'Amorphous plate'];
 
-                 $batteryType = ['铅酸电池'=>'Lead acid battery','锂电池'=>'Lithium battery'];
 
-                 if (!preg_match('/^[A-Za-z\s*]+$/', $res['polematerial'])) {
 
-                     $res['polematerial'] = empty($polemaType[$res['polematerial']]) ? '' : $polemaType[$res['polematerial']];
 
-                 }
 
-                 if (!preg_match('/^[A-Za-z\s*]+$/', $res['lighttype'])) {
 
-                     $res['lighttype'] = empty($lightType[$res['lighttype']]) ? '' : $lightType[$res['lighttype']];
 
-                 }
 
-                 if (!preg_match('/^[A-Za-z\s*]+$/', $res['lamptype'])) {
 
-                     $res['lamptype'] = empty($lampType[$res['lamptype']]) ? '' : $lampType[$res['lamptype']];
 
-                 }
 
-                 if (!preg_match('/^[A-Za-z\s*]+$/', $res['boardtype'])) {
 
-                     $res['boardtype'] = empty($boardType[$res['boardtype']]) ? '' : $boardType[$res['boardtype']];
 
-                 }
 
-                 if (!preg_match('/^[A-Za-z\s*]+$/', $res['batterytype'])) {
 
-                     $res['batterytype'] = empty($batteryType[$res['batterytype']]) ? '' : $batteryType[$res['batterytype']];
 
-                 }
 
-             }else{
 
-                 $polemaType = ['Cold galvanizing'=>'冷镀锌','Hot galvanizing'=>'热镀锌','Stainless steel'=>'不锈钢'];
 
-                 $lightType = ['LED lamp'=>'LED灯','Sodium lamp'=>'钠灯','Energy saving light'=>'节能灯'];
 
-                 $lampType = ['Solar street lamp'=>'太阳能路灯','Landscape lamp'=>'景观灯','Garden light'=>'庭院灯'];
 
-                 $boardType = ['Single crystal plate'=>'单晶板','Polycrystalline plate'=>'多晶板','Amorphous plate'=>'非晶板'];
 
-                 $batteryType = ['Lead acid battery'=>'铅酸电池','Lithium battery'=>'锂电池'];
 
-                 if (preg_match('/^[A-Za-z\s*]+$/', $res['polematerial'])) {
 
-                     $res['polematerial'] = empty($polemaType[$res['polematerial']]) ? '' : $polemaType[$res['polematerial']];
 
-                 }
 
-                 if (preg_match('/^[A-Za-z\s*]+$/', $res['lighttype'])) {
 
-                     $res['lighttype'] = empty($lightType[$res['lighttype']]) ? '' : $lightType[$res['lighttype']];
 
-                 }
 
-                 if (preg_match('/^[A-Za-z\s*]+$/', $res['lamptype'])) {
 
-                     $res['lamptype'] = empty($lampType[$res['lamptype']]) ? '' : $lampType[$res['lamptype']];
 
-                 }
 
-                 if (preg_match('/^[A-Za-z\s*]+$/', $res['boardtype'])) {
 
-                     $res['boardtype'] = empty($boardType[$res['boardtype']]) ? '' : $boardType[$res['boardtype']];
 
-                 }
 
-                 if (preg_match('/^[A-Za-z\s*]+$/', $res['batterytype'])) {
 
-                     $res['batterytype'] = empty($batteryType[$res['batterytype']]) ? '' : $batteryType[$res['batterytype']];
 
-                 }
 
-             }
 
-             // if ($res["isfaulted"] == 0) {
 
-             //     $res["isfaulted"] = '正常';
 
-             // }else{
 
-                 
 
-             // }
 
-             $alarmData = $this->Alarm_model->get_one_by_filter(array('lampid'=>$lamp_id,'status'=>0),'stralarmtype');
 
-             $version = $this->session->userdata('version');
 
-             if (empty($version)) {
 
-                 $res['isfaulted'] = empty($alarmData['stralarmtype']) ? '正常' : $alarmData['stralarmtype'];
 
-             }else{
 
-                 $res['isfaulted'] = empty($alarmData['stralarmtype']) ? 'Normal' : alarm_translate($alarmData['stralarmtype']);
 
-             }
 
-             
 
-             $timezone = $this->Project_model->get_timezone_by_projectid($res['projectid']);
 
-             $res["updatetime"] = date_change($res["updatetime"],8,$timezone['value']);
 
-             $data = array(
 
-                 "lamp_no" => $res["number"], 
 
-                 "network_no" => $this->Network_model->getNameById($res["networkid"], "networkid"), 
 
-                 "networkname" => $this->Network_model->getNameById($res["networkid"], "networkname"), 
 
-                 "project_name" => $this->Project_model->getNameById($res["projectid"]), 
 
-                 "address" => $res["address"], 
 
-                 "longitude" => $res["longitude"], 
 
-                 "latitude" => $res["latitude"], 
 
-                 "isfaulted" => $res["isfaulted"], 
 
-                 "section" => $res["section"], 
 
-                 "boardpower" => floatval($res["boardpower"]), 
 
-                 "poleheight" => $res['poleheight'],
 
-                 "polediameter" => $res['polediameter'],
 
-                 "polematerial" => $res['polematerial'],
 
-                 "lighttype" => $res['lighttype'],
 
-                 "lamptype" => $res['lamptype'],
 
-                 "boardtype" => $res['boardtype'], 
 
-                 "batteryah" => $res["batteryah"],
 
-                 "loadpower" => $res["loadpower"],
 
-                 "network_id" => $res["networkid"],
 
-                 'status' => $res['status'],
 
-                 'batterytype' => $res['batterytype'],
 
-                 'devicetype' => $res['devicetype'],
 
-                 'protocoltype'=>$res['protocoltype'],
 
-                 "simid" => empty($res["simid"]) ? '' : $res["simid"], 
 
-                 "packageSurplus" => $res["packageSurplus"], 
 
-                 "updatetime" => $res["updatetime"], 
 
-                 "lampprotocoltype" => $res['lampprotocoltype'],
 
-                 'loadtype' => $res['loadtype']
 
-             );
 
-             $msisdn = $this->db->query('select iccid from msisdn where imsi = "'.$res['simid'].'"')->row_array();
 
-             if (empty($msisdn) || empty($msisdn['iccid'])) {
 
-                 $data['iccid'] = '';
 
-             }else{
 
-                 $data['iccid'] = $msisdn['iccid'];
 
-             }
 
-             if ($res['protocoltype'] != 1 && $res['protocoltype'] != 2) {
 
-                 unset($data['packageSurplus']);
 
-                 unset($data['simid']);
 
-             }
 
-             $username = $this->get_user_info('username');
 
-             if ($username == 'zns') {
 
-                 $data['monthly_can_usage'] = intval($res['monthly_can_usage']);
 
-                 $data['monthly_already_usage'] = round($res['monthly_already_usage'],2);
 
-             }
 
-             exit(json_result('0000', $this->response['0000'], $data));
 
-         }
 
-         $table_map = array('system_info_log','lamp_info_log','solar_info_log','battery_info_log','electric_info_log','history_info_log');
 
-         $table = in_array($datatype, $table_map) ? $datatype : 'system_info_log';
 
-         $res = $this->Lamp_model->getForInfoLog($lamp_id, $table);
 
-         $lampData = $this->Lamp_model->getOne($lamp_id,'L.projectid');
 
-         $timezone = $this->Project_model->get_timezone_by_projectid($lampData['projectid']);
 
-         if(!$res){
 
-             exit(json_result('0000', $this->response['0006'], array()));
 
-         }
 
-         if ($table == 'battery_info_log') {
 
-             if (isset($res['daydischargemincurrent'])) {
 
-                 $h = floor($res['daydischargemincurrent']/3600);
 
-                 $h = $h < 10 ? '0'.$h : $h;
 
-                 $i = intval($res['daydischargemincurrent']/60)%60;
 
-                 $i = $i < 10 ? '0'.$i : $i;
 
-                 $res['daydischargemincurrent'] = $h.':'.$i;
 
-             }
 
-             if (isset($res['daychargemincurrent'])) {
 
-                 $h = floor($res['daychargemincurrent']/3600);
 
-                 $h = $h < 10 ? '0'.$h : $h;
 
-                 $i = intval($res['daychargemincurrent']/60)%60;
 
-                 $i = $i < 10 ? '0'.$i : $i;
 
-                 $res['daychargemincurrent'] = $h.':'.$i;
 
-             }
 
-         }
 
-         $res["updatetime"] = date_change($res["updatetime"],8,$timezone['value']);
 
-         exit(json_result('0000', $this->response['0000'], $res));
 
-     }
 
-     // 灯控历史数据
 
-     public function historylist() {
 
-         $lamp_id = $this->input->post('lamp_id',true);
 
-         $datatype = $this->input->post('datatype',true);
 
-         $page = intval($this->input->post('page',true));
 
-         $count = intval($this->input->post('count',true));
 
-         if (empty($lamp_id)) {
 
-             exit(json_result('0400', $this->response['0400'], array()));
 
-         }
 
-         $date = $this->input->post('date',true);
 
-         $beginDate = '';
 
-         $endDate = '';
 
-         if (!empty($date)) {
 
-             $dateArr = explode('/', $date);
 
-             $beginDate = $dateArr[0];
 
-             $endDate = $dateArr[1];
 
-             if (strtotime($endDate) - strtotime($beginDate) > 31*24*3600) {
 
-                 exit(json_result('0418',$this->response['0418']));
 
-             }
 
-         }
 
-         if($page < 1){
 
-             $page = 1;
 
-         }
 
-         // 按时区修改时间
 
-         $lampData = $this->Lamp_model->getOne($lamp_id,'L.projectid,L.protocoltype');
 
-         $timezone = $this->Project_model->get_timezone_by_projectid($lampData['projectid']);
 
-         $download = intval($this->input->post('download'));
 
-         if (!empty($download)) {
 
-             if (empty($date)) exit(json_result('0007',$this->response['0007']));
 
-             $dataList = array();
 
-             // 路灯
 
-             $lampInfoList = $this->Lamp_model->getListForInfoLog($lamp_id, 'lamp_info_log', null, null,$beginDate,$endDate);
 
-             $temp = array();
 
-             $i = 1;
 
-             if (empty($this->version)) {
 
-                 $lampStatusArr = array(0=>'关灯',1=>'开灯');
 
-                 $title = array('序号','亮度%','状态','电压(V)','电流(A)','功率(W)','当天放电最大功率(W)','当天亮灯时长(HH:MM)','更新时间');
 
-                 $name = '路灯';
 
-             }else{
 
-                 $lampStatusArr = array(0=>'off',1=>'on');
 
-                 $title = array('Number','Brightness%','Status','Voltage(V)','Current(A)','Power(W)','Maximum discharge power of the day(W)','dayLighting duration(HH:MM)','Update time');
 
-                 $name = 'Lamp';
 
-             }
 
-             foreach ($lampInfoList as $k=>$l) {
 
-                 $temp[$k][] = $i;
 
-                 $temp[$k][] = $l['lighteness'];
 
-                 $temp[$k][] = $lampStatusArr[$l['lampstatus']];
 
-                 $temp[$k][] = $l['lampvoltage'];
 
-                 $temp[$k][] = $l['lampcurrent'];
 
-                 $temp[$k][] = $l['lamppower'];
 
-                 $temp[$k][] = $l['daydischarmaxpow'];
 
-                 $h = floor($l['daychargemincurrent'] / 3600);
 
-                 $h = $h < 10 ? '0'.$h : $h;
 
-                 $m = ceil($l['daychargemincurrent'] % 3600 / 60);
 
-                 $m = $m < 10 ? '0'.$m : $m;
 
-                 $temp[$k][] = $h.':'.$m;
 
-                 $temp[$k][] = date_change($l["updatetime"],8,$timezone['value']);
 
-                 $i ++;
 
-             }
 
-             $title1 = [];
 
-             foreach ($title as $t) {
 
-                 $title1[0][] = $t;
 
-             }
 
-             $data = array_merge($title1,$temp);
 
-             $dataList[] = array('name'=>$name,'dataList'=>$data);
 
-             // 蓄电池
 
-             $lampInfoList = $this->Lamp_model->getListForInfoLog($lamp_id, 'battery_info_log', null, null,$beginDate,$endDate);
 
-             $temp = array();
 
-             $i = 1;
 
-             if (empty($this->version)) {
 
-                 $name = '蓄电池';
 
-                 if ($lampData['protocoltype'] == 2) {
 
-                     $chargeStageMap = array(0=>'没有充电',1=>'等待',2=>'初始',3=>'MPPT',4=>'恒压',5=>'恒流',6=>'浮充',7=>'均衡',8=>'提升',9=>'涓流',10=>'完成',''=>'未知状态');
 
-                 }else{
 
-                     $chargeStageMap = array(0=>'没有充电',1=>'启动充电',6=>'限流',7=>'充满',16=>'MPPT充电',32=>'均衡充电',48=>'提升充电',64=>'浮充',''=>'未知状态');
 
-                 }
 
-                 $battstatusArr = array(0=>'空闲',1=>'放电',2=>'充电',3=>'一边充电一边放电',''=>'未知状态');
 
-                 $unknown = '未知状态';
 
-                 $title = array('序号','系统电压(V)','系统电流(A)','MOS温度(℃)','电池状态','充电阶段','电池电压(V)','当天最低电压(V)','当天最高电压(V)','电池温度(℃)','当天电池最低温度(℃)','当天电池最高温度(℃)','更新时间');
 
-             }else{
 
-                 $name = 'Battery';
 
-                 if ($lampData['protocoltype'] == 2) {
 
-                     $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');
 
-                 }else{
 
-                     $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');
 
-                 }
 
-                 $battstatusArr = array(0=>'Free',1=>'Discharge',2=>'Charge',3=>'Discharge while charging',''=>'Unknown state');
 
-                 $unknown = 'Unknown state';
 
-                 $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');
 
-             }
 
-             $indexArr = [2=>'16',3=>'32',4=>'48',5=>'64'];
 
-             foreach ($lampInfoList as $k=>$l) {
 
-                 $temp[$k][] = $i;
 
-                 $temp[$k][] = $l['sysvoltage'];
 
-                 $temp[$k][] = $l['syscurrent'];
 
-                 $temp[$k][] = $l['temper'];
 
-                 $temp[$k][] = $battstatusArr[$l['battstatus']];
 
-                 if ($lampData['protocoltype'] == 1) {
 
-                     $l['chargestage'] = isset($indexArr[intval($l['chargestage'])]) ? $indexArr[intval($l['chargestage'])] : $l['chargestage'];
 
-                 }
 
-                 $temp[$k][] = $chargeStageMap[$l['chargestage']];
 
-                 $temp[$k][] = $l['battvoltage'];
 
-                 $temp[$k][] = $l['voltagedaymin'];
 
-                 $temp[$k][] = $l['voltagedaymax'];
 
-                 $temp[$k][] = $l['batttemper'];
 
-                 $temp[$k][] = $l['daybattmintemper'];
 
-                 $temp[$k][] = $l['daybattmaxtemper'];
 
-                 $temp[$k][] = date_change($l["updatetime"],8,$timezone['value']);
 
-                 $i ++;
 
-             }
 
-             $title1 = [];
 
-             foreach ($title as $t) {
 
-                 $title1[0][] = $t;
 
-             }
 
-             $data = array_merge($title1,$temp);
 
-             $dataList[] = array('name'=>$name,'dataList'=>$data);
 
-             // 太阳能
 
-             $lampInfoList = $this->Lamp_model->getListForInfoLog($lamp_id, 'solar_info_log', null, null,$beginDate,$endDate);
 
-             $temp = array();
 
-             $i = 1;
 
-             if (empty($this->version)) {
 
-                 $name = '太阳能';
 
-                 $title = array('序号','电压(V)','电流(A)','功率(W)','当天充电最大功率(WH)','当天充电时长(HH:MM)','更新时间');
 
-             }else{
 
-                 $name = 'Solar';
 
-                 $title = array('Number','Voltage(V)','Current(A)','Power(W)','Maximum Power Charged on the Day(W)','day Charging time(HH:MM)','Update time');
 
-             }
 
-             foreach ($lampInfoList as $k=>$l) {
 
-                 $temp[$k][] = $i;
 
-                 $temp[$k][] = $l['solarvoltage'];
 
-                 $temp[$k][] = $l['solarcurrent'];
 
-                 $temp[$k][] = $l['solarpower'];
 
-                 $temp[$k][] = $l['daychargemaxpow'];
 
-                 $h = floor($l['daydischargemincurrent'] / 3600);
 
-                 $h = $h < 10 ? '0'.$h : $h;
 
-                 $m = ceil($l['daydischargemincurrent'] % 3600 / 60);
 
-                 $m = $m < 10 ? '0'.$m : $m;
 
-                 $temp[$k][] = $h.':'.$m;
 
-                 $temp[$k][] = date_change($l["updatetime"],8,$timezone['value']);
 
-                 $i ++;
 
-             }
 
-             $title1 = [];
 
-             foreach ($title as $t) {
 
-                 $title1[0][] = $t;
 
-             }
 
-             $data = array_merge($title1,$temp);
 
-             $dataList[] = array('name'=>$name,'dataList'=>$data);
 
-             // 系统
 
-             $lampInfoList = $this->Lamp_model->getListForInfoLog($lamp_id, 'system_info_log', null, null,$beginDate,$endDate);
 
-             $temp = array();
 
-             $i = 1;
 
-             if (empty($this->version)) {
 
-                 $name = '系统';
 
-                 $title = array('序号','系统电压(V)','系统电流(A)','控制器温度(℃)','更新时间');
 
-             }else{
 
-                 $name = 'System';
 
-                 $title = array('Serial number','System voltage(V)','System current(A)','Control temperature(℃)','Update time');
 
-             }
 
-             foreach ($lampInfoList as $k=>$l) {
 
-                 $temp[$k][] = $i;
 
-                 $temp[$k][] = $l['sysvoltage'];
 
-                 $temp[$k][] = $l['syscurrent'];
 
-                 $temp[$k][] = $l['temper'];
 
-                 $temp[$k][] = date_change($l["updatetime"],8,$timezone['value']);
 
-                 $i ++;
 
-             }
 
-             $title1 = [];
 
-             foreach ($title as $t) {
 
-                 $title1[0][] = $t;
 
-             }
 
-             $data = array_merge($title1,$temp);
 
-             $dataList[] = array('name'=>$name,'dataList'=>$data);
 
-             $path = push_excel($dataList,'historyInfoExcel_'.date('Ymd'),1);
 
-             exit(json_result('0000', $this->response['0000'], array('path'=>$path)));
 
-         }
 
-         // $table_map = array('system_info_log','lamp_info_log','solar_info_log','battery_info_log','electric_info_log','history_info_log');
 
-         $table = !empty($datatype) ? $datatype : 'system_info_log';
 
-         $total = $this->Lamp_model->getCountForInfoLog($lamp_id, $table,$beginDate,$endDate);
 
-         $res = $this->Lamp_model->getListForInfoLog($lamp_id, $table, ($page-1)*$count, $count,$beginDate,$endDate);
 
-         if(!$res){
 
-             exit(json_result('0000', $this->response['0006'], array()));
 
-         }else{
 
-             
 
-             $arr = [2=>'16',3=>'32',4=>'48',5=>'64'];
 
-             foreach ($res as &$v) {
 
-                 $v['updatetime'] = date_change($v["updatetime"],8,$timezone['value']);
 
-                 if ($lampData['protocoltype'] == 1 && $table == 'battery_info_log') {
 
-                     $v['chargestage'] = isset($arr[intval($v['chargestage'])]) ? $arr[intval($v['chargestage'])] : $v['chargestage'];
 
-                 }
 
-                 if ($table == 'system_info_log') {
 
-                     $v['lampid'] = $v['number'];
 
-                 }
 
-             }
 
-         }
 
-         $data = array(
 
-             'total' => ceil($total/$count),
 
-             'list' => $res
 
-         );
 
-         exit(json_result('0000', $this->response['0000'], $data));
 
-     }
 
-     // 修改路灯位置
 
-     public function change_location(){
 
-         $id = intval($this->input->post('id',true));
 
-         $type = intval($this->input->post('type',true));
 
-         $data['longitude'] = $this->input->post('longitude',true);
 
-         $data['latitude'] = $this->input->post('latitude',true);
 
-         $data['updatetime'] = date('Y-m-d H:i:s',time());
 
-         if (empty($id) || empty($type) || !isset($data['longitude']) || !isset($data['latitude'])) {
 
-             exit(json_result('0007',$this->response['0007'],array()));
 
-         }
 
-         if ($type == 1) {
 
-             $this->Lamp_model->update(array('id'=>$id),$data);
 
-         }else{
 
-             $this->Videomonitor_model->update(array('id'=>$id),$data);
 
-         }
 
-         exit(json_result('0000',$this->response['0000'],array()));
 
-     }
 
-     // 同步巡检
 
-     public function set_patrol_cmd(){
 
-         $id = intval($this->input->post('id',true));
 
-         if (empty($id)) exit(json_result('0007',$this->response['0007'],array('id'=>$id)));
 
-         $lampData = $this->Lamp_model->getOne($id,'N.status,N.protocoltype,L.address,L.protocoltype as lampprotocoltype,L.number');
 
-         $cmd = '{"cmd_type":"set_sync_patrol_cmd","cmd_id":'.$id.'}';
 
-         $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
 
-         if ($cmdret === false) {
 
-             sleep(1);
 
-             $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
 
-         }
 
-         exit(json_result('0000',$this->response['0000'],array('id'=>$id)));
 
-     }
 
-     // 批量添加
 
-     public function batchInput(){
 
-         set_time_limit(0);
 
-         $userid = $this->get_user_info('id');
 
-         // $idArr = $this->get_section_by_user();
 
-         $role = $this->get_user_info('role');
 
-         $companyid = $this->get_user_info('companyid');
 
-         $type = intval($this->input->post('type',true));
 
-         if ($type == 1) {  // 验证
 
-             $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'];
 
-             $data = $this->batch_import($indexArr);
 
-         }else{  // 创建
 
-             $data = $this->input->post('lampData',true);
 
-             if (empty($data)) exit(json_result('0007',$this->response['0007']));
 
-             $data = json_decode($data,true);
 
-         }
 
-         foreach ($data as &$v) {
 
-             if ($type == 1 && is_float($v['address'])) $v['address'] = strval(intval(ceil($v['address'])));
 
-         }
 
-         $res = 0;
 
-         $typeArr = ['tcp'=>0,'nb'=>1,'gprs'=>2,'lorawan'=>3,'MQTT'=>4,'WeLoraWan'=>6,'onenet'=>7];
 
-         $this->db->trans_begin();
 
-         $msg = [];
 
-         $patrol_arr = array();
 
-         foreach ($data as $key => $value) {
 
-             $num = $key+2;
 
-             $value['number'] = trim($value['number']);
 
-             if (!isset($value['number']) || $value['number'] == '') {
 
-                 $res = 1;
 
-                 if (empty($this->version)) {
 
-                     $msg[] = '表格第'.$num.'行,路灯编号不能为空';
 
-                 }else{
 
-                     $msg[] = 'Table '.$num.' row.Lamp number can not be empty';
 
-                 }
 
-             }else{
 
-                 if (!is_numeric($value['number']) && ($value['number'] <= 0 || $value['number'] > 999999)) {
 
-                     $res = 1;
 
-                     if (empty($this->version)) {
 
-                         $msg[] = '表格第'.$num.'行,路灯编号为1-999999的数字';
 
-                     }else{
 
-                         $msg[] = 'Table '.$num.' row.The number of street lamps numbered 1-999999';
 
-                     }
 
-                 }
 
-             }
 
-             // if ($type == 1 && is_float($value['address'])) {
 
-             //     $value['address'] = strval(intval(ceil($value['address'])));
 
-             // }
 
-             $value['address'] = trim($value['address']);
 
-             if (empty($value['address'])) {
 
-                 $res = 1;
 
-                 
 
-                 if (empty($this->version)) {
 
-                     $msg[] = '表格第'.$num.'行,无线模块地址不能为空';
 
-                 }else{
 
-                     $msg[] = 'Table '.$num.' row.Address of the wireless module can not be empty';
 
-                 }
 
-             }else{
 
-                 $status = preg_match('/^[A-Fa-f0-9]+$/', $value['address']);
 
-                 if (empty($status)) {
 
-                     $res = 1;
 
-                     if (empty($this->version)) {
 
-                         $msg[] = '表格第'.$num.'行,无线模块地址为16进制数';
 
-                     }else{
 
-                         $msg[] = 'Table '.$num.' row.Wireless module address is 16 hexadecimal';
 
-                     }
 
-                 }else{
 
-                     if ($this->Lamp_model->getDataCount(['address'=>$value['address']])) {
 
-                         $res = 1;
 
-                         if (empty($this->version)) {
 
-                             $msg[] = '表格第'.$num.'行,无线模块地址已经存在';
 
-                         }else{
 
-                             $msg[] = 'Table '.$num.' row.Wireless module address already exists';
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-             $value['company_name'] = trim($value['company_name']);
 
-             if (empty($value['company_name'])) {
 
-                 $res = 1;
 
-                 if (empty($this->version)) {
 
-                     $msg[] = '表格第'.$num.'行,公司名称不能为空';
 
-                 }else{
 
-                     $msg[] = 'Table '.$num.' row.Company name cannot be empty';
 
-                 }
 
-             }
 
-             $value['projectid'] = trim($value['projectid']);
 
-             if (empty($value['projectid'])) {
 
-                 $res = 1;
 
-                 if (empty($this->version)) {
 
-                     $msg[] = '表格第'.$num.'行,项目名称不能为空';
 
-                 }else{
 
-                     $msg[] = 'Table '.$num.' row.Project name cannot be empty';
 
-                 }
 
-             }else{
 
-                 $projectid = $this->Project_model->project_exist_by_name($role,$userid,$companyid,$value['projectid'],$value['company_name']);
 
-                 if (empty($projectid)) {
 
-                     $res = 1;
 
-                     if (empty($this->version)) {
 
-                         $msg[] = '表格第'.$num.'行,项目不存在,请先创建项目';
 
-                     }else{
 
-                         $msg[] = 'Table '.$num.' row.The project does not exist. Please create a project first.';
 
-                     }
 
-                     $value['projectid'] = 0;
 
-                 }else{
 
-                     $value['projectid'] = $projectid;
 
-                     $this->Project_model->add_lamp_count(array('faultcount'=>0,'projectid'=>$projectid));
 
-                     if ($this->Lamp_model->getDataCount(['projectid'=>$value['projectid'],'number'=>$value['number']])) {
 
-                         $res = 1;
 
-                         if (empty($this->version)) {
 
-                             $msg[] = '表格第'.$num.'行,路灯编号已经存在';
 
-                         }else{
 
-                             $msg[] = 'Table '.$num.' row lamp number already exists';
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-             if($type != 1 && isset($typeArr[$value['protocoltype']]) && $typeArr[$value['protocoltype']] == 6){
 
-                 $this->add_device($value['address']);
 
-             }
 
-             $value['devicetype'] = intval(trim($value['devicetype']));
 
-             if ($value['devicetype'] == 0) { // 非直连设备
 
-                 unset($value['protocoltype']);
 
-                 $value['networkid'] = trim($value['networkid']);
 
-                 if (empty($value['networkid'])) {
 
-                     $res = 1;
 
-                     if (empty($this->version)) {
 
-                         $msg[] = '表格第'.$num.'行,非直连设备网络编号不能为空';
 
-                     }else{
 
-                         $msg[] = 'Table '.$num.' row.Indirect device network number cannot be empty';
 
-                     }
 
-                 }else{
 
-                     $networkid = $this->Network_model->project_exist_by_name($value['projectid'],$value['networkid']);
 
-                     // var_dump($this->db->last_query());die;
 
-                     if (empty($networkid)) {
 
-                         $res = 1;
 
-                         if (empty($this->version)) {
 
-                             $msg[] = '表格第'.$num.'行,网络不存在,请先添加网络';
 
-                         }else{
 
-                             $msg[] = 'Table '.$num.' row.The network does not exist. Please add the network first';
 
-                         }
 
-                     }else{
 
-                         $this->Network_model->add_lamp_count(array('faultcount'=>0,'networkid'=>$networkid));
 
-                         $value['networkid'] = $networkid;
 
-                     }
 
-                 }
 
-             }elseif ($value['devicetype'] == 1) { // 直连设备
 
-                 $value['protocoltype'] = trim($value['protocoltype']);
 
-                 if (empty($value['protocoltype'])) {
 
-                     $res = 1;
 
-                     if (empty($this->version)) {
 
-                         $msg[] = '表格第'.$num.'行,协议类型不能为空';
 
-                     }else{
 
-                         $msg[] = 'Table '.$num.' row.Protocol type cannot be empty';
 
-                     }
 
-                 }else{
 
-                     if (!isset($typeArr[$value['protocoltype']])) {
 
-                         $res = 1;
 
-                         if (empty($this->version)) {
 
-                             $msg[] = '表格第'.$num.'行,协议类型不存在';
 
-                         }else{
 
-                             $msg[] = 'Table '.$num.' row.Protocol type not exists';
 
-                         }
 
-                     }
 
-                 }
 
-                 // 创建直连网络
 
-                 $number = mb_strlen($value['address']) == 13 ? base_convert($value['address'], 16, 10) : $value['address'];
 
-                 if ($this->Network_model->getDataCount(array('devicesn'=>$number))) {
 
-                     $res = 1;
 
-                     if (empty($this->version)) {
 
-                         $msg[] = '表格第'.$num.'行,无线模块地址已被使用';
 
-                     }else{
 
-                         $msg[] = 'Table '.$num.' row.Wireless module address has been used';
 
-                     }
 
-                 }
 
-                 // 创建网络
 
-                 // $networkData['id']  = $this->Zone_model->insert(array(
 
-                 //     'name'   => $number,
 
-                 //     'parent' => $value['projectid'] ,
 
-                 //     'level'  => 4
 
-                 // )); 
 
-                 // 控制器协议类型
 
-                 $value['lampprotocoltype'] = trim($value['lampprotocoltype']);
 
-                 if (empty($value['lampprotocoltype'])) {
 
-                     $networkData['devicetype'] = 0;
 
-                 }else{
 
-                     if ($value['lampprotocoltype'] == 'ModBus') {
 
-                         $networkData['devicetype'] = 3;
 
-                     }elseif ($value['lampprotocoltype'] == 'LightBus') {
 
-                         $networkData['devicetype'] = 0;
 
-                     }else{
 
-                         $networkData['devicetype'] = 0;
 
-                     }
 
-                 }
 
-                 $networkData['networkid'] = $number;
 
-                 $networkData['gatewaytype'] = 'direct';
 
-                 $networkData['protocoltype'] = empty($typeArr[$value['protocoltype']]) ? 'tcp' : $typeArr[$value['protocoltype']];
 
-                 $networkData['projectid'] = $value['projectid'];
 
-                 $networkData['devicesn'] = $number;
 
-                 $networkData['networkname'] = $number;
 
-                 $networkData['type'] = 1;
 
-                 $networkData['lampcount'] = 1;
 
-                 $networkData['createtime'] = date('Y-m-d H:i:s',time());
 
-                 if ($type == 1) {
 
-                     $this->db->insert('network',$networkData);
 
-                     $value['networkid'] = $this->db->insert_id();
 
-                 }else{
 
-                     $value['networkid'] = $this->Network_model->insert($networkData);
 
-                 }
 
-             }else{
 
-                 $res = 1;
 
-                 if (empty($this->version)) {
 
-                     $msg[] = '表格第'.$num.'行,设备类型选择错误';
 
-                 }else{
 
-                     $msg[] = 'Table '.$num.' row.Device type selection error';
 
-                 }
 
-             }
 
-             unset($value['protocoltype']);
 
-             // 控制器协议类型
 
-             $value['lampprotocoltype'] = trim($value['lampprotocoltype']);
 
-             if (empty($value['lampprotocoltype'])) {
 
-                 $res = 1;
 
-                 
 
-                 if (empty($this->version)) {
 
-                     $msg[] = '表格第'.$num.'行,协议类型不能为空';
 
-                 }else{
 
-                     $msg[] = 'Table '.$num.' row.Protocol type can not be empty';
 
-                 }
 
-             }else{
 
-                 if ($value['lampprotocoltype'] == 'ModBus') {
 
-                     $value['protocoltype'] = 1;
 
-                 }elseif ($value['lampprotocoltype'] == 'LightBus') {
 
-                     $value['protocoltype'] = 0;
 
-                 }else{
 
-                     $res = 1;
 
-                     if (empty($this->version)) {
 
-                         $msg[] = '表格第'.$num.'行,协议类型选择错误';
 
-                     }else{
 
-                         $msg[] = 'Table '.$num.' row.Protocol type selection error';
 
-                     }
 
-                 }
 
-             }
 
-             unset($value['lampprotocoltype']);
 
-             $value['longitude'] = trim($value['longitude']);
 
-             if (empty($value['longitude'])) {
 
-                 $value['longitude'] = 0;
 
-             }else{
 
-                 if (!is_numeric($value['longitude']) || $value['longitude'] > 180 || $value['longitude'] < -180) {
 
-                     $res = 1;
 
-                     if (empty($this->version)) {
 
-                         $msg[] = '表格第'.$num.'行,经度输入错误';
 
-                     }else{
 
-                         $msg[] = 'Table '.$num.' row longitude input error';
 
-                     }
 
-                 }
 
-             }
 
-             $value['latitude'] = trim($value['latitude']);
 
-             if (empty($value['latitude'])) {
 
-                 $value['latitude'] = 0;
 
-             }else{
 
-                 if (!is_numeric($value['latitude']) || $value['latitude'] > 90 || $value['latitude'] < -90) {
 
-                     $res = 1;
 
-                     if (empty($this->version)) {
 
-                         $msg[] = '表格第'.$num.'行,纬度输入错误';
 
-                     }else{
 
-                         $msg[] = 'Table '.$num.' row latitude input error';
 
-                     }
 
-                 }
 
-             }
 
-             if (empty($value['latitude']) && empty($value['longitude']) && !empty($value['projectid'])) {
 
-                 $projectData = $this->Project_model->getOne($value['projectid'],'cityid');
 
-                 if (!empty($projectData['cityid'])){
 
-                     $globalData = $this->Global_location_model->getOne($projectData['cityid'],'longitude,latitude');
 
-                     $value['longitude'] = $globalData['longitude'];
 
-                     $value['latitude'] = $globalData['latitude'];
 
-                 }
 
-             }
 
-             
 
-             $value['loadtype'] = trim($value['loadtype']);
 
-             if (empty($value['loadtype']) || $value['loadtype'] == '0' || $value['loadtype'] == '1') {
 
-                 if ($value['loadtype'] == '1') {
 
-                     $value['loadtype'] = 1;
 
-                 }else{
 
-                     $value['loadtype'] = 0;
 
-                 }
 
-             }else{
 
-                 $res = 1;
 
-                 if (empty($this->version)) {
 
-                     $msg[] = '表格第'.$num.'行,负载类型错误';
 
-                 }else{
 
-                     $msg[] = 'Table '.$num.' row load type error';
 
-                 }
 
-             }
 
-             if (trim($value['poleheight']) == '') {
 
-                 unset($value['poleheight']);
 
-             }else{
 
-                 $value['poleheight'] = trim($value['poleheight']);
 
-             }
 
-             if (trim($value['polediameter']) == '') {
 
-                 unset($value['polediameter']);
 
-             }else{
 
-                 $value['polediameter'] = trim($value['polediameter']);
 
-             }
 
-             if (trim($value['section']) == '') {
 
-                 $value['section'] = '';
 
-                 $res = 1;
 
-                 if (empty($this->version)) {
 
-                     $msg[] = '表格第'.$num.'行,路段不能为空';
 
-                 }else{
 
-                     $msg[] = 'Table '.$num.' row. Section cannot be empty';
 
-                 }
 
-             }else{
 
-                 $value['section'] = trim($value['section']);
 
-             }
 
-             if (trim($value['polematerial']) == '') {
 
-                 unset($value['polematerial']);
 
-             }else{
 
-                 $value['polematerial'] = trim($value['polematerial']);
 
-             }
 
-             if (trim($value['lighttype']) == '') {
 
-                 unset($value['lighttype']);
 
-             }else{
 
-                 $value['lighttype'] = trim($value['lighttype']);
 
-             }
 
-             if (trim($value['lamptype']) == '') {
 
-                 unset($value['lamptype']);
 
-             }else{
 
-                 $value['lamptype'] = trim($value['lamptype']);
 
-             }
 
-             if (trim($value['boardtype']) == '') {
 
-                 unset($value['boardtype']);
 
-             }else{
 
-                 $value['boardtype'] = trim($value['boardtype']);
 
-             }
 
-             if (trim($value['batterytype']) == '') {
 
-                 unset($value['batterytype']);
 
-             }else{
 
-                 $value['batterytype'] = trim($value['batterytype']);
 
-             }
 
-             if (trim($value['boardpower']) == '') {
 
-                 unset($value['boardpower']);
 
-             }else{
 
-                 $value['boardpower'] = trim($value['boardpower']);
 
-             }
 
-             if (trim($value['batteryah']) == '') {
 
-                 unset($value['batteryah']);
 
-             }else{
 
-                 $value['batteryah'] = trim($value['batteryah']);
 
-             }
 
-             if (trim($value['loadpower']) == '') {
 
-                 unset($value['loadpower']);
 
-             }else{
 
-                 $value['loadpower'] = trim($value['loadpower']);
 
-             }
 
-             $value['createtime'] = date('Y-m-d H:i:s',time());
 
-             $value['updatetime'] = date('Y-m-d H:i:s',time());
 
-             unset($value['company_name']);
 
-             if (is_null($value['networkid'])) {
 
-                 unset($value['networkid']);
 
-             }
 
-             $lamp_id = $this->Lamp_model->insert($value);
 
-             $patrol_arr[] = array('lampid'=>$lamp_id,'projectid'=>$value['projectid']);
 
-         }
 
-         if ($res || $type == 1) {
 
-             $this->db->trans_rollback();
 
-             exit(json_result('0000',$this->response['0000'],['lampData'=>$data,'msg'=>$msg]));
 
-         }else{
 
-             $this->db->trans_commit();
 
-             foreach ($patrol_arr as $patrol) {
 
-                 $projectData = $this->Project_model->getOne($patrol['projectid'],'patrolinterval');
 
-                 $this->patrol_set($patrol['lampid'],$projectData['patrolinterval']);
 
-             }
 
-             exit(json_result('0000',$this->response['0000']));
 
-         }
 
-     }
 
-     // 路灯历史数据曲线图
 
-     public function lamp_history_data(){
 
-         $type = intval($this->input->post('type',true));
 
-         $lampId = intval($this->input->post('lampid',true));
 
-         if (empty($lampId)) exit(json_result('0007',$this->response['0007']));
 
-         $dateType = intval($this->input->post('dateType',true));
 
-         $date = $this->input->post('date',true);
 
-         if ($dateType == 0) { // 一天
 
-             $beginDate = date('Y-m-d H:i:s',time()-24*3600);
 
-             $endDate = date('Y-m-d H:i:s',time());
 
-         }elseif ($dateType == 1) {  // 三天
 
-             $beginDate = date('Y-m-d H:i:s',time()-24*3600*3);
 
-             $endDate = date('Y-m-d H:i:s',time());
 
-         }elseif ($dateType == 2) {  //  七天
 
-             $beginDate = date('Y-m-d H:i:s',time()-24*3600*7);
 
-             $endDate = date('Y-m-d H:i:s',time());
 
-         }elseif ($dateType == 3) {  //  十四天
 
-             $beginDate = date('Y-m-d H:i:s',time()-24*3600*14);
 
-             $endDate = date('Y-m-d H:i:s',time());
 
-         }else{   // 选择时间
 
-             if (empty($date)) exit(json_result('0007',$this->response['0007']));
 
-             $res = explode('/', $date);
 
-             if (strtotime($res[1]) - strtotime($res[0]) > 31 * 24 * 3600) {
 
-                 exit(json_result('0418',$this->response['0418']));
 
-             }
 
-             $beginDate = $res[0];
 
-             $endDate = $res[1];
 
-         }
 
-         $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";        
 
-         $lampData = $this->db->query($sql)->row_array();
 
-         $timezoneData = $this->Project_model->get_timezone_by_projectid($lampData['id']);
 
-         $timezone = $timezoneData['value'];
 
-         $data = ['patrolinterval'=>$lampData['patrolinterval']];
 
-         if ($type == 0) {  // 路灯
 
-             $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.'"';
 
-             $list = $this->db->query($sql)->result_array();
 
-             $temp = [];
 
-             foreach ($list as $value) {
 
-                 $temp['lampvoltage'][] = $value['lampvoltage'];
 
-                 $temp['lampcurrent'][] = $value['lampcurrent'];
 
-                 $temp['lamppower'][] = $value['lamppower'];
 
-                 $temp['lighteness'][] = $value['lighteness'];
 
-                 $temp['updatetime'][] = date_change($value['updatetime'],8,$timezone);
 
-                 if (!isset($data['maxcurrent'])) {
 
-                     $data['maxcurrent'] = $value['lampcurrent'];
 
-                 }else{
 
-                     $data['maxcurrent'] = $value['lampcurrent'] > $data['maxcurrent'] ? $value['lampcurrent'] : $data['maxcurrent'];
 
-                 }
 
-                 if (!isset($data['maxdaychargemincurrent'])) {
 
-                     $data['maxdaychargemincurrent'] = $value['daychargemincurrent'];
 
-                 }else{
 
-                     $data['maxdaychargemincurrent'] = $value['daychargemincurrent'] > $data['maxdaychargemincurrent'] ? $value['daychargemincurrent'] : $data['maxdaychargemincurrent'];
 
-                 }
 
-                 if (!isset($data['mincurrent'])) {
 
-                     $data['mincurrent'] = $value['lampcurrent'];
 
-                 }else{
 
-                     $data['mincurrent'] = $value['lampcurrent'] < $data['mincurrent'] ? $value['lampcurrent'] : $data['mincurrent'];
 
-                 }
 
-                 if (!isset($data['maxvoltage'])) {
 
-                     $data['maxvoltage'] = $value['lampvoltage'];
 
-                 }else{
 
-                     $data['maxvoltage'] = $value['lampvoltage'] > $data['maxvoltage'] ? $value['lampvoltage'] : $data['maxvoltage'];
 
-                 }
 
-                 if (!isset($data['minvoltage'])) {
 
-                     $data['minvoltage'] = $value['lampvoltage'];
 
-                 }else{
 
-                     $data['minvoltage'] = $value['lampvoltage'] < $data['minvoltage'] ? $value['lampvoltage'] : $data['minvoltage'];
 
-                 }
 
-                 if (!isset($data['maxpower'])) {
 
-                     $data['maxpower'] = $value['lamppower'];
 
-                 }else{
 
-                     $data['maxpower'] = $value['lamppower'] > $data['maxpower'] ? $value['lamppower'] : $data['maxpower'];
 
-                 }
 
-                 if (!isset($data['minpower'])) {
 
-                     $data['minpower'] = $value['lamppower'];
 
-                 }else{
 
-                     $data['minpower'] = $value['lamppower'] < $data['minpower'] ? $value['lamppower'] : $data['minpower'];
 
-                 }
 
-                 if (!isset($data['maxlighteness'])) {
 
-                     $data['maxlighteness'] = $value['lighteness'];
 
-                 }else{
 
-                     $data['maxlighteness'] = $value['lighteness'] > $data['maxlighteness'] ? $value['lighteness'] : $data['maxlighteness'];
 
-                 }
 
-                 if (!isset($data['minlighteness'])) {
 
-                     $data['minlighteness'] = $value['lighteness'];
 
-                 }else{
 
-                     $data['minlighteness'] = $value['lighteness'] < $data['minlighteness'] ? $value['lighteness'] : $data['minlighteness'];
 
-                 }
 
-                 if (isset($data['maxdaychargemincurrent'])) {
 
-                     $h = floor($data['maxdaychargemincurrent'] / 3600);
 
-                     $h = $h < 10 ? '0'.$h : $h;
 
-                     $m = floor($data['maxdaychargemincurrent'] / 3600 % 60);
 
-                     $m = $m < 10 ? '0'.$m : $m;
 
-                     $data['maxdaychargemincurrent'] = $h.':'.$m;
 
-                 }
 
-             }
 
-             $list = $temp;
 
-         }elseif ($type == 1) { // 蓄电池
 
-             $sql = 'select battvoltage,chargecurrent,discharcurrent,batttemper,chargepower,dischargepower,updatetime from battery_info_log where lampid = '.$lampId.' and updatetime >= "'.$beginDate.'" and updatetime <= "'.$endDate.'"';
 
-             $list = $this->db->query($sql)->result_array();
 
-             $temp = [];
 
-             foreach ($list as $value) {
 
-                 $temp['battvoltage'][] = $value['battvoltage'];
 
-                 $temp['updatetime'][] = date_change($value['updatetime'],8,$timezone);
 
-                 
 
-                 $batterycurrent = $value['chargecurrent'] > 0 ? $value['chargecurrent'] : 0 - $value['discharcurrent'];
 
-                 $temp['batterycurrent'][] = $batterycurrent;
 
-                 
 
-                 $batterypower = $value['chargepower'] > 0 ? $value['chargepower'] : 0 - $value['dischargepower'];
 
-                 $temp['batterypower'][] = $batterypower;
 
-                 if (!isset($data['maxcurrent'])) {
 
-                     $data['maxcurrent'] = $batterycurrent;
 
-                 }else{
 
-                     $data['maxcurrent'] = $batterycurrent > $data['maxcurrent'] ? $batterycurrent : $data['maxcurrent'];
 
-                 }
 
-                 if (!isset($data['mincurrent'])) {
 
-                     $data['mincurrent'] = $batterycurrent;
 
-                 }else{
 
-                     $data['mincurrent'] = $batterycurrent < $data['mincurrent'] ? $batterycurrent : $data['mincurrent'];
 
-                 }
 
-                 if (!isset($data['maxvoltage'])) {
 
-                     $data['maxvoltage'] = $value['battvoltage'];
 
-                 }else{
 
-                     $data['maxvoltage'] = $value['battvoltage'] > $data['maxvoltage'] ? $value['battvoltage'] : $data['maxvoltage'];
 
-                 }
 
-                 if (!isset($data['minvoltage'])) {
 
-                     $data['minvoltage'] = $value['battvoltage'];
 
-                 }else{
 
-                     $data['minvoltage'] = $value['battvoltage'] < $data['minvoltage'] ? $value['battvoltage'] : $data['minvoltage'];
 
-                 }
 
-                 if (!isset($data['maxbatttemper'])) {
 
-                     $data['maxbatttemper'] = $value['batttemper'];
 
-                 }else{
 
-                     $data['maxbatttemper'] = $value['batttemper'] > $data['maxbatttemper'] ? $value['batttemper'] : $data['maxbatttemper'];
 
-                 }
 
-                 if (!isset($data['minbatttemper'])) {
 
-                     $data['minbatttemper'] = $value['batttemper'];
 
-                 }else{
 
-                     $data['minbatttemper'] = $value['batttemper'] < $data['minbatttemper'] ? $value['batttemper'] : $data['minbatttemper'];
 
-                 }
 
-                 if (!isset($data['maxpower'])) {
 
-                     $data['maxpower'] = round($batterypower,2);
 
-                 }else{
 
-                     $data['maxpower'] = $batterypower > $data['maxpower'] ? round($batterypower,2) : round($data['maxpower'],2);
 
-                 }
 
-                 if (!isset($data['minpower'])) {
 
-                     $data['minpower'] = round($batterypower,2);
 
-                 }else{
 
-                     $data['minpower'] = $batterypower < $data['minpower'] ? round($batterypower,2) : round($data['minpower'],2);
 
-                 }
 
-             }
 
-             $list = $temp;
 
-         }elseif ($type == 2) {  // 太阳能板
 
-             $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.'"';
 
-             $list = $this->db->query($sql)->result_array();
 
-             $temp = [];
 
-             foreach ($list as $value) {
 
-                 $temp['solarvoltage'][] = $value['solarvoltage'];
 
-                 $temp['solarcurrent'][] = $value['solarcurrent'];
 
-                 $temp['solarpower'][] = $value['solarpower'];
 
-                 $temp['updatetime'][] = date_change($value['updatetime'],8,$timezone);
 
-                 if (!isset($data['maxcurrent'])) {
 
-                     $data['maxcurrent'] = $value['solarcurrent'];
 
-                 }else{
 
-                     $data['maxcurrent'] = $value['solarcurrent'] > $data['maxcurrent'] ? $value['solarcurrent'] : $data['maxcurrent'];
 
-                 }
 
-                 if (!isset($data['mincurrent'])) {
 
-                     $data['mincurrent'] = $value['solarcurrent'];
 
-                 }else{
 
-                     $data['mincurrent'] = $value['solarcurrent'] < $data['mincurrent'] ? $value['solarcurrent'] : $data['mincurrent'];
 
-                 }
 
-                 if (!isset($data['maxvoltage'])) {
 
-                     $data['maxvoltage'] = $value['solarvoltage'];
 
-                 }else{
 
-                     $data['maxvoltage'] = $value['solarvoltage'] > $data['maxvoltage'] ? $value['solarvoltage'] : $data['maxvoltage'];
 
-                 }
 
-                 if (!isset($data['maxdaydischargemincurrent'])) {
 
-                     $data['maxdaydischargemincurrent'] = $value['daydischargemincurrent'];
 
-                 }else{
 
-                     $data['maxdaydischargemincurrent'] = $value['daydischargemincurrent'] > $data['maxdaydischargemincurrent'] ? $value['daydischargemincurrent'] : $data['maxdaydischargemincurrent'];
 
-                 }
 
-                 if (!isset($data['minvoltage'])) {
 
-                     $data['minvoltage'] = $value['solarvoltage'];
 
-                 }else{
 
-                     $data['minvoltage'] = $value['solarvoltage'] < $data['minvoltage'] ? $value['solarvoltage'] : $data['minvoltage'];
 
-                 }
 
-                 if (!isset($data['maxpower'])) {
 
-                     $data['maxpower'] = $value['solarpower'];
 
-                 }else{
 
-                     $data['maxpower'] = $value['solarpower'] > $data['maxpower'] ? $value['solarpower'] : $data['maxpower'];
 
-                 }
 
-                 if (!isset($data['minpower'])) {
 
-                     $data['minpower'] = $value['solarpower'];
 
-                 }else{
 
-                     $data['minpower'] = $value['solarpower'] < $data['minpower'] ? $value['solarpower'] : $data['minpower'];
 
-                 }
 
-                 if (isset($data['maxdaydischargemincurrent'])) {
 
-                     $h = floor($data['maxdaydischargemincurrent'] / 3600);
 
-                     $h = $h < 10 ? '0'.$h : $h;
 
-                     $m = floor($data['maxdaydischargemincurrent'] / 3600 % 60);
 
-                     $m = $m < 10 ? '0'.$m : $m;
 
-                     $data['maxdaydischargemincurrent'] = $h.':'.$m;
 
-                 }
 
-             }
 
-             $list = $temp;
 
-         }else{  // 电量
 
-             $sql = 'select daychargeah,daydischarah,updatetime from battery_info_log where lampid = '.$lampId.' and updatetime >= "'.$beginDate.'" and updatetime <= "'.$endDate.'"';
 
-             $list = $this->db->query($sql)->result_array();
 
-             $temp = [];
 
-             foreach ($list as $value) {
 
-                 $temp['daychargeah'][] = $value['daychargeah'];
 
-                 $temp['daydischarah'][] = $value['daydischarah'];
 
-                 $temp['updatetime'][] = date_change($value['updatetime'],8,$timezone);
 
-                 if (!isset($data['maxdaychargeah'])) {
 
-                     $data['maxdaychargeah'] = $value['daychargeah'];
 
-                 }else{
 
-                     $data['maxdaychargeah'] = $value['daychargeah'] > $data['maxdaychargeah'] ? $value['daychargeah'] : $data['maxdaychargeah'];
 
-                 }
 
-                 if (!isset($data['mindaychargeah'])) {
 
-                     $data['mindaychargeah'] = $value['daychargeah'];
 
-                 }else{
 
-                     $data['mindaychargeah'] = $value['daychargeah'] < $data['mindaychargeah'] ? $value['daychargeah'] : $data['mindaychargeah'];
 
-                 }
 
-                 if (!isset($data['maxdaydischarah'])) {
 
-                     $data['maxdaydischarah'] = $value['daydischarah'];
 
-                 }else{
 
-                     $data['maxdaydischarah'] = $value['daydischarah'] > $data['maxdaydischarah'] ? $value['daydischarah'] : $data['maxdaydischarah'];
 
-                 }
 
-                 if (!isset($data['mindaydischarah'])) {
 
-                     $data['mindaydischarah'] = $value['daydischarah'];
 
-                 }else{
 
-                     $data['mindaydischarah'] = $value['daydischarah'] < $data['mindaydischarah'] ? $value['daydischarah'] : $data['mindaydischarah'];
 
-                 }
 
-             }
 
-             $list = $temp;
 
-         }
 
-         if (empty($list)) {
 
-             $data['list'] = new StdClass();
 
-         }else{
 
-             $data['list'] = $list;
 
-         }
 
-         exit(json_result('0000',$this->response['0000'],$data));
 
-     }
 
- }
 
 
  |