Lampcontrol.php 291 KB

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