Lampcontrol.php 247 KB

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