Lampcontrol.php 292 KB

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