Lampcontrol.php 248 KB

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