Lampcontrol.php 290 KB

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