Lampcontrol.php 247 KB

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