Lampcontrol.php 248 KB

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