Lampcontrol.php 293 KB

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