Lampcontrol.php 248 KB

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