Lampcontrol.php 248 KB

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