Lampcontrol.php 268 KB

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