Lampcontrol.php 292 KB

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