1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216 |
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- include_once(FCPATH . 'application/controllers/Base_Controller.php');
- class Lampcontrol extends Base_Controller {
-
- private $chargeStageMap = array(0=>'没有充电',16=>'MPPT充电',32=>'均衡充电',48=>'提升充电',64=>'浮充',''=>'未知状态');
- public function __construct() {
- parent::__construct();
- $this->load->model('Company_model');
- $this->load->model('Project_model');
- $this->load->model('Network_model');
- $this->load->model('Lamp_model');
- $this->load->model('Batmanage_model');
- $this->load->model('Patrol_model');
- $this->load->model('Load_model');
- $this->load->model('Battery_model');
- $this->load->model('Alarm_model');
- // $this->load->model('Zone_model');
- $this->load->model('Videomonitor_model');
- $this->load->model('Modbus_load_model');
- $this->load->model('Modbus_battery_model');
- $this->load->model('Global_location_model');
- $this->load->model('RealtimeInfoLog_model');
- }
- // 灯控列表
- public function get() {
- $userid = $this->get_user_info('id');
- $role = $this->get_user_info('role');
- $company = $this->input->post('company',true);
- $project_id = intval($this->input->post('project_id', true));
- if (empty($project_id)) exit(json_result('0007',$this->response['0007']));
- $fields = $this->input->post('fields', true);
- $field1 = $this->input->post('field1', true);
- $pid_arr = $project_id;
- $timezone = $this->Project_model->get_timezone_by_projectid($project_id);
- $filter = array('L.projectid'=>$project_id);
- $network_id = $this->input->post('network_id', true);
- $section = $this->input->post('section', true);
- $type = intval($this->input->post('type', true));
- $company = intval($this->input->post('company', true));
- if (!empty($type) && !empty($company)) {
-
- if ($type == 2) {
- $filter['L.manu'] = $company;
- }elseif ($type == 3) {
- $filter['L.supplier'] = $company;
- }elseif ($type == 4) {
- $filter['L.po'] = $company;
- }else {
- $filter['L.upazilla'] = $company;
- }
- }
-
- if (!empty($network_id)) $filter['L.networkid'] = $network_id;
- if (!empty($section)) $filter['L.section'] = $section;
- $offline = $this->input->post('offline', true);
- if (!empty($offline)) $filter['L.netstatus'] = 0;
- $online = $this->input->post('online', true);
- if (!empty($online)) $filter['L.netstatus'] = 1;
- // 分页数据
- $page = intval($this->input->post('page',true));
- $page = empty($page) ? 1 : $page;
- $count = intval($this->input->post('count',true));
- $limit = empty($count) ? 10 : $count;
- $offset = ($page - 1)*$count;
- $keyword = $this->input->post('keyword', true);
- if ($keyword !== '' && $keyword !== null) $filter['L.number|L.section|L.address'] = $keyword;
- $status = $this->input->post('status', true);
- if (!empty($status)) {
- $filter['L.isfaulted'] = $status;
- $filter['AI.stralarmtype !='] = '';
- $filter['AI.status'] = 0;
- }
- $alarmtype = $this->input->post('alarmtype', true);
- if (!empty($alarmtype)) $filter['AI.alarmtype'] = $alarmtype;
- // 记录用户选择的字段
- if (!empty($field1)) {
- $fieldData = array('userid'=>$userid,'lampfield'=>$field1);
- $this->User_model->update_user_field($fieldData);
- }
- $lite = $this->input->post('lite', true);
- $version = $this->session->userdata('version');
- if ($lite == 1) {
- if(empty($fields)){
- $fields = 'L.id, L.number';
- }else{
- $fields .= ",RI.updatetime";
- }
- $join = array();
- $data['lamps'] = $this->Lamp_model->get_list_by_join($filter, $fields,$limit, $offset, $join, "L.number ASC", NUll, 'L');
- } else {
- if(empty($fields) || empty($field1)){
- $fields = 'L.id, L.number, IFNULL(L.status,0) as status, L.isfaulted, L.address, L.lamppower, L.electricSOC, L.battvoltage, L.chargestage, RI.updatetime, RI.updatetime as modifytime,L.overtimes,L.boardpower,L.lighteness as light,L.projectid,L.devstatus as online,P.projectname as project_name,L.section,L.loadtype,P.timezone as timezoneid';
- }else{
- $fields .= ",L.id,L.projectid,L.section,L.loadtype,P.timezone as timezoneid";
- }
- $download = intval($this->input->post('download',true));
- if (!empty($download)) {
- $limit = null;
- $offset = null;
- }
- $join = array();
- $join[] = ['table'=>'project as P', 'cond'=>'L.projectid = P.id', 'type'=>'left'];
- $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'];
- $join[] = ['table'=>'(select t1.* from history_info_log t1, (select lampid, max(updatetime) as maxtime from history_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as HI', 'cond'=>'L.id = HI.lampid', 'type'=>'left'];
- $join[] = ['table'=>'(select t1.* from realtime_info_log t1, (select lampid, max(updatetime) as maxtime from realtime_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as RI', 'cond'=>'L.id = RI.lampid', 'type'=>'left'];
- $data['lamps'] = $this->Lamp_model->get_list_by_join($filter, $fields,$limit, $offset, $join, "L.number ASC", NUll, 'L');
- // $data['lamps'] = $this->Lamp_model->get_list_in('L.projectid', $pid_arr, $fields, $filter);
- // var_dump(microtime());
- $indexArr = [2=>'16',3=>'32',4=>'48',5=>'64'];
- if (empty($version)) {
- $chargeStageMap = array(0=>'没有充电',1=>'启动充电',6=>'限流',7=>'充满',16=>'MPPT充电',32=>'均衡充电',48=>'提升充电',64=>'浮充',''=>'未知状态');
- $battstatusArr = array(0=>'空闲',1=>'放电',2=>'充电',3=>'一边充电一边放电',''=>'未知状态');
- $unknown = '未知状态';
- }else{
- $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');
- $battstatusArr = array(0=>'Free',1=>'Discharge',2=>'Charge',3=>'Discharge while charging',''=>'Unknown state');
- $unknown = 'Unknown state';
- }
- if (!empty($download)) {
- $fieldArr = explode(',', $field1);
- $fieldArr[] = 'iccid';
- $temp = [];
- $i = 1;
- foreach ($data['lamps'] as $key => &$value) {
- $value['section'] = empty($value['section']) ? '' : $value['section'];
- if (isset($value['updatetime']) && $value['updatetime'] == '0000-00-00 00:00:00') {
- $value['updatetime'] = null;
- }
- if (array_key_exists('lamppower',$value)) {
- $value['lamppower'] = !empty($value['lamppower']) ? $value['lamppower'] : "0";
- }
- // 故障信息处理
- if (array_key_exists('isfaulted',$value)) {
- if ($value['isfaulted'] == 0) {
- if (empty($version)) {
- $value['isfaulted'] = '无';
- }else{
- $value['isfaulted'] = 'Nothing';
- }
-
- }else{
- $res = $this->Alarm_model->get_one_by_filter(array('lampid'=>$value['id'],'status'=>0),'stralarmtype');
- if (empty($version)) {
- $stralarmtype = trim($res['stralarmtype']);
- if (!empty($stralarmtype)) {
- $alarmArr = explode(',', $stralarmtype);
- $temp2 = array();
- foreach ($alarmArr as $a) {
- if ($a == '电池故障') {
- $a = '电池电压异常';
- }
- $temp2[] = $a;
- }
- $value['isfaulted'] = implode(',', $temp2);
- }else{
- $value['isfaulted'] = '无';
- }
- }else{
- $stralarmtype = trim($res['stralarmtype']);
- if (!empty($stralarmtype)) {
- $alarmArr = explode(',', $stralarmtype);
- $temp2 = array();
- foreach ($alarmArr as $a) {
- if ($a == '电池故障') {
- $a = '电池电压异常';
- }
- $temp[] = alarm_translate($a);
- }
- $value['isfaulted'] = implode(',', $temp2);
- }else{
- $value['isfaulted'] = 'Nothing';
- }
-
- }
- }
- }
- if (array_key_exists('lampstatus',$value)) {
- $value['lampstatus'] = !empty($value['lampstatus']) ? $value['lampstatus'] : "0";
- }
- if (array_key_exists('electricSOC',$value)) {
- $value['electricSOC'] = !empty($value['electricSOC']) ? $value['electricSOC'] : "0";
- }
- if (array_key_exists('battvoltage',$value)) {
- $value['battvoltage'] = !empty($value['battvoltage']) ? $value['battvoltage'] : "0";
- }
- if (array_key_exists('chargestage',$value)) {
- $value['chargestage'] = isset($value['chargestage']) ? $chargeStageMap[$value['chargestage']] : $unknown;
- }
- if (array_key_exists('battstatus',$value)) {
- $value['battstatus'] = isset($value['battstatus']) ? $battstatusArr[$value['battstatus']] : $unknown;
- }
- if (array_key_exists('overtimes',$value)) {
- $value['overtimes'] = !empty($value['overtimes']) ? $value['overtimes'] : 0;
- }
- if (array_key_exists('solarpower',$value)) {
- $value['solarpower'] = !empty($value['solarpower']) ? $value['solarpower'] : 0;
- }
- if (array_key_exists('iccid',$value)) {
- $value['iccid'] = !empty($value['iccid']) ? $value['iccid'] : '';
- }
- if (array_key_exists('updatetime',$value)) {
- $value['updatetime'] = !empty($value['updatetime']) ? $value['updatetime'] : '';
- // 通过时区修改时间
- if (!empty($value['updatetime'])) {
- $timezone = $this->Project_model->get_timezone_by_projectid($value['projectid']);
- if ($timezone['value'] != 8) {
- $value['updatetime'] = date_change($value['updatetime'],8,$timezone['value']);
- }
- }
- }
- // 当前策略
- if (array_key_exists('policyid',$value)) {
- if (empty($value['policyid'])) {
- $value['policyid'] = '';
- }else{
- $policyData = $this->db->query('select name from policy_info where id = '.$value['policyid'])->row_array();
- if (empty($policyData) || empty($policyData['name'])) {
- $value['policyid'] = '';
- }else{
- $value['policyid'] = $policyData['name'];
- }
- }
- }
-
- $temp[$key][] = $i;
- foreach ($fieldArr as $f) {
- if ($f == 'lampstatus') {
- if (empty($version)) {
- $temp[$key][] = empty($value[$f]) ? '关灯' : '开灯';
- }else{
- $temp[$key][] = empty($value[$f]) ? 'off' : 'on';
- }
- continue;
- }
- if ($f == 'status') {
- if (empty($version)) {
- $temp[$key][] = empty($value[$f]) ? '不在线' : '在线';
- }else{
- $temp[$key][] = empty($value[$f]) ? 'off-line' : 'online';
- }
- continue;
- }
- if ($f == 'daychargemincurrent' || $f == 'daydischargemincurrent') {
- $t = floor($value[$f]/3600) < 10 ? '0'.floor($value[$f]/3600) : floor($value[$f]/3600);
- $m = ceil($value[$f]%3600/60) < 10 ? '0'.ceil($value[$f]%3600/60) : ceil($value[$f]%3600/60);
- $temp[$key][] = $t.':'.$m;
- continue;
- }
- $temp[$key][] = $value[$f];
- }
- $i ++;
- }
- $title1 = get_fields_by_type($field1,$version);
- if (empty($version)) {
- $title2 = array_merge(array('编号'),$title1);
- }else{
- $title2 = array_merge(array('Number'),$title1);
- }
- $title = [];
- foreach ($title2 as $t) {
- $title[0][] = $t;
- }
- $title[0][] = 'iccid';
- $data = array_merge($title,$temp);
- $path = push_excel($data,'lampinfoExcel_'.date('Ymd'));
- exit(json_result('0000', $this->response['0000'], array('path'=>$path)));
- }
- $timezone = null;
- foreach ($data['lamps'] as $key => &$value) {
- $value['section'] = empty($value['section']) ? '' : $value['section'];
- if (isset($value['updatetime']) && $value['updatetime'] == '0000-00-00 00:00:00') {
- $value['updatetime'] = null;
- }
- if (array_key_exists('lamppower',$value)) {
- $value['lamppower'] = !empty($value['lamppower']) ? $value['lamppower'] : "0";
- }
- // 故障信息处理
- if (array_key_exists('isfaulted',$value)) {
- if ($value['isfaulted'] == 0) {
- if (empty($version)) {
- $value['isfaulted'] = '无';
- }else{
- $value['isfaulted'] = 'Nothing';
- }
-
- }else{
- $res = $this->Alarm_model->get_one_by_filter(array('lampid'=>$value['id'],'status'=>0),'stralarmtype');
- if (empty($version)) {
- // $value['isfaulted'] = empty($res['stralarmtype']) ? '无' : $res['stralarmtype'];
- $stralarmtype = trim($res['stralarmtype']);
- if (!empty($stralarmtype)) {
- $alarmArr = explode(',', $stralarmtype);
- $temp = array();
- foreach ($alarmArr as $a) {
- if ($a == '电池故障') {
- $a = '电池电压异常';
- }
- $temp[] = $a;
- }
- $value['isfaulted'] = implode(',', $temp);
- }else{
- $value['isfaulted'] = '无';
- }
- }else{
- $stralarmtype = trim($res['stralarmtype']);
- if (!empty($stralarmtype)) {
- $alarmArr = explode(',', $stralarmtype);
- $temp = array();
- foreach ($alarmArr as $a) {
- if ($a == '电池故障') {
- $a = '电池电压异常';
- }
- $temp[] = alarm_translate($a);
- }
- $value['isfaulted'] = implode(',', $temp);
- }else{
- $value['isfaulted'] = 'Nothing';
- }
-
- }
- }
-
- }
- if (array_key_exists('lampstatus',$value)) {
- $value['lampstatus'] = !empty($value['lampstatus']) ? $value['lampstatus'] : "0";
- }
- if (array_key_exists('electricSOC',$value)) {
- $value['electricSOC'] = !empty($value['electricSOC']) ? $value['electricSOC'] : "0";
- }
- if (array_key_exists('battvoltage',$value)) {
- $value['battvoltage'] = !empty($value['battvoltage']) ? $value['battvoltage'] : "0";
- }
- if (array_key_exists('chargestage',$value)) {
- $value['chargestage'] = isset($value['chargestage']) && isset($chargeStageMap[$value['chargestage']]) ? $chargeStageMap[$value['chargestage']] : $unknown;
- }
- if (array_key_exists('battstatus',$value)) {
- $value['battstatus'] = isset($value['battstatus']) ? $battstatusArr[$value['battstatus']] : $unknown;
- }
- if (array_key_exists('overtimes',$value)) {
- $value['overtimes'] = !empty($value['overtimes']) ? $value['overtimes'] : 0;
- }
- if (array_key_exists('solarpower',$value)) {
- $value['solarpower'] = !empty($value['solarpower']) ? $value['solarpower'] : 0;
- }
- // 当前策略
- if (array_key_exists('policyid',$value)) {
- if (empty($value['policyid'])) {
- $value['policyid'] = '';
- }else{
- $policyData = $this->db->query('select name from policy_info where id = '.$value['policyid'])->row_array();
- if (empty($policyData) || empty($policyData['name'])) {
- $value['policyid'] = '';
- }else{
- $value['policyid'] = $policyData['name'];
- }
- }
- }
- if (array_key_exists('updatetime',$value)) {
- $value['updatetime'] = !empty($value['updatetime']) ? $value['updatetime'] : '';
- // 通过时区修改时间
- if (!empty($value['updatetime'])) {
- // $value['timezoneid'];
- if($timezone === null) $timezone = $this->db->query('select value from timezone where id = '.$value['timezoneid'])->row_array();
- if ($timezone['value'] != 8) {
- $value['updatetime'] = date_change($value['updatetime'],0,$timezone['value']);
- }
- }
- }
- }
- if (!empty($project_id)) {
-
- }
- }
- unset($filter['page']);
- unset($filter['count']);
- $data['total_data']['install_num'] = $this->Lamp_model->get_count(array('projectid'=>$project_id));
- $data['total_data']['light_num'] = $this->Lamp_model->get_count(array('projectid'=>$project_id,'netstatus'=>1,'status'=>1));
- $data['total_data']['online_num'] = $this->Lamp_model->get_count(array('projectid'=>$project_id,'netstatus'=>1));
- $data['total_data']['fault_count'] = $this->Lamp_model->get_fault_count(array('L.projectid'=>$project_id,'L.isfaulted'=>1,'AI.status'=>0), $role, '', $userid,$project_id);
- $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;
- $data['total_data']['light_rate'] .= '%';
- $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;
- $data['total_data']['online_rate'] .= '%';
- $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;
- $data['total_data']['fault_rate'] .= '%';
- // $data['total_data']['network_num'] = $this->Network_model->get_count(['projectid'=>$project_id,'type'=>1]);
- $res = $this->Lamp_model->get_list_by_multi_join($filter, 'count(*) total',$limit, $offset, $join, "L.number ASC", NUll, 'L',true);
- $result = !empty($pid_arr) ? $this->Lamp_model->get_total('L.projectid', $pid_arr, 'L.id') : array();
- $result = $this->Lamp_model->get_count(['projectid'=>$pid_arr]);
- $data['total'] = $res['total'];
- $data['count'] = $result;
- $data['fault'] = $res['total'];
- exit(json_result('0000', $this->response['0000'], $data));
- }
- // 更新灯控信息
- public function update() {
- $role = $this->get_user_info('role');
- $username = $this->input->post('username',true);
- $client_key = $this->input->post('client_key',true);
- $token = $this->input->post('token',true);
- $type = intval($this->input->post('type',true));
- $lamp_id = $this->input->post('lamp_id',true);
- $clientid = $this->input->post('clientid',true);
- $version = $this->session->userdata('version');
- $lamp_id = $this->input->post('lamp_id', true);
- if (empty($lamp_id)) {
- exit(json_result('0411', $this->response['0411'], array()));
- }
- $ids = explode(',', $lamp_id);
- $status = 1;
- foreach ($ids as $v) {
- $record['cmdtype'] = 0;
- $record['relateid'] = $v;
- $record['statuscmd'] = 0;
- $record['updatetime'] = date("Y-m-d H:i:s");
- $recordid = $this->Batmanage_model->get_record_id($record, 'batch_update_cmd');
- if(!$recordid){
- exit(json_result('0009', $this->response['0009'], array()));
- }
- if ($recordid != -1) {
- $status = 0;
- $cmd = '{"cmd_type":"batch_update_cmd","cmd_id":'.$recordid.'}';
- $lampData = $this->Lamp_model->getOne($v,'N.status,N.protocoltype,L.protocoltype as lampprotocoltype');
- if ($lampData['protocoltype'] == 4 || $lampData['protocoltype'] == 6) {
- $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/update_asy';
- doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'lamp_id'=>$v]);
- continue;
- }
- if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
- $cmdret = send_cmd($cmd,1,0,$lampData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,1,0,$lampData['protocoltype']);
- }
- }else{
- $cmdret = send_cmd($cmd,0,0,$lampData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,0,0,$lampData['protocoltype']);
- }
- }
- if($cmdret === false){
- exit(json_result('0008', $this->response['0008'], array()));
- }
- }
- }
- sleep(2);
- if ($status) {
- exit(json_result('0406', $this->response['0406'], array()));
- }else{
- exit(json_result('0407', $this->response['0407'], array()));
- }
- }
- // 异步更新数据
- public function update_asy(){
- $lamp_id = $this->input->post('lamp_id',true);
- $lampData = $this->Lamp_model->getOne($lamp_id,'L.number,N.protocoltype,L.protocoltype as lampprotocoltype,L.address,L.networkid,N.status,N.deviceid');
- if ($lampData['lampprotocoltype'] == 0) {
- $address = '';
- if (mb_strlen($lampData['address']) >= 8) {
- $address = mb_substr($lampData['address'], -8);
- }else{
- $address = '00000000';
- }
- $sendData = $address.'0300004646';
- if ($lampData['protocoltype'] == 4) {
- $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*','0001'.$sendData));
- }else{
- // $cmd .= crc16(pack('H*',$sendData));
- device_cmd($lampData['deviceid'],pack('H*',$sendData));
- $cmd = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
- $info = send_lorawan_915($cmd);
- $res = array();
- if (!empty($info)) {
- $info = json_decode($info,true);
- if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
- }
- $file = fopen('./file/lorawan.txt', 'a+');
- fwrite($file, $res.'----'.date('Y-m-d H:i:s',time()).'
- ');
- fclose($file);
- // die;
- }
-
- if (!empty($res)) {
- $msg1 = $res['msg'];
- // 数据校验
- $msg = mb_substr($msg1, 10);
- $t = 0;
- for ($i=0; $i < mb_strlen($msg) / 2 - 1; $i++) {
- $s = mb_substr($msg, $i*2,2);
- $t += base_convert($s, 16, 10);
- }
- $t = base_convert($t, 10, 16);
- if (mb_strlen($t) > 2) {
- $t = mb_substr($t, -2);
- }elseif($t <= 1){
- $t = '0'.$t;
- }
- if ($t == mb_substr($msg, -2)) {
- // 校验通过,解析数据
- $updatetime = date('Y-m-d H:i:s',time());
- // 系统信息
- $systemInfoStr = mb_substr($msg1, 16,6);
- $systemInfo = array(
- 'sysvoltage' => base_convert(mb_substr($systemInfoStr, 0,2), 16, 10),
- 'syscurrent' => base_convert(mb_substr($systemInfoStr, 2,2), 16, 10),
- 'temper' => bitDecode(mb_substr($systemInfoStr, 4,2)),
- 'updatetime' => $updatetime,
- 'lampid' => $lamp_id,
- );
- $this->db->insert('system_info_log', $systemInfo);
- // 路灯信息
- $lampInfoStr = mb_substr($msg1, 22,12);
- $lampInfo = array(
- 'lampvoltage' => round(base_convert(mb_substr($lampInfoStr, 2,4), 16, 10)/10,1),
- 'lampcurrent' => round(base_convert(mb_substr($lampInfoStr, 6,4), 16, 10)/100,2),
- 'temper' => bitDecode(mb_substr($lampInfoStr, 10,2)),
- 'updatetime' => $updatetime,
- 'lampid' => $lamp_id,
- );
- $lampInfo['lamppower'] = round($lampInfo['lampvoltage'] * $lampInfo['lampcurrent'],2);
- $light = base_convert(mb_substr($lampInfoStr, 0,2), 16, 2);
- if (mb_strlen($light) == 8) {
- $lampInfo['lighteness'] = base_convert(mb_substr($light, -7), 2, 10);
- $lampInfo['lampstatus'] = 1;
- }else{
- $lampInfo['lighteness'] = 0;
- $lampInfo['lampstatus'] = 0;
- }
- $this->db->insert('lamp_info_log', $lampInfo);
- // 太阳能信息
- $solarInfoStr = mb_substr($msg1, 34,8);
- $solarInfo = array(
- 'solarvoltage' => round(base_convert(mb_substr($solarInfoStr, 0,4), 16, 10)/10,1),
- 'solarcurrent' => round(base_convert(mb_substr($solarInfoStr, 4,4), 16, 10)/100,2),
- 'updatetime' => $updatetime,
- 'lampid' => $lamp_id,
- );
- $solarInfo['solarpower'] = round($solarInfo['solarvoltage'] * $solarInfo['solarcurrent'],2);
- $this->db->insert('solar_info_log', $solarInfo);
- // 蓄电池信息
- $batterInfoStr = mb_substr($msg1, 42,70);
- $batteryInfo = array(
- 'battvoltage' => round(base_convert(mb_substr($batterInfoStr, 2,4), 16, 10)/10,1),
- 'chargecurrent' => round(base_convert(mb_substr($batterInfoStr, 6,4), 16, 10)/100,2),
- 'discharcurrent' => round(base_convert(mb_substr($batterInfoStr, 10,4), 16, 10)/100,2),
- 'batttemper' => bitDecode(mb_substr($batterInfoStr, 14,2)),
- 'electrictotal' => base_convert(mb_substr($batterInfoStr, 16,4),16,10),
- 'electricleft' => base_convert(mb_substr($batterInfoStr, 20,4),16,10),
- 'electricSOC' => base_convert(mb_substr($batterInfoStr, 24,2),16,10),
- 'voltagedaymin' => round(base_convert(mb_substr($batterInfoStr, 26,4),16,10)/10,1),
- 'voltagedaymax' => round(base_convert(mb_substr($batterInfoStr, 30,4),16,10)/10,1),
- 'daychargeah' => round(base_convert(mb_substr($batterInfoStr, 34,4),16,10)/10,1),
- 'daydischarah' => round(base_convert(mb_substr($batterInfoStr, 38,4),16,10)/10,1),
- 'daychargemaxpow' => round(base_convert(mb_substr($batterInfoStr, 42,4),16,10)/10,1),
- 'daydischarmaxpow' => round(base_convert(mb_substr($batterInfoStr, 46,4),16,10)/10,1),
- 'daychargemincurrent' => base_convert(mb_substr($batterInfoStr, 50,4),16,10),
- 'daycharmaxcurrent' => round(base_convert(mb_substr($batterInfoStr, 54,4),16,10)/100,2),
- 'daydischargemincurrent' => base_convert(mb_substr($batterInfoStr, 58,4),16,10),
- 'daydischarmaxcurrent' => round(base_convert(mb_substr($batterInfoStr, 62,4),16,10)/100,2),
- 'daybattmintemper' => bitDecode(mb_substr($batterInfoStr, 66,2)),
- 'daybattmaxtemper' => bitDecode(mb_substr($batterInfoStr, 68,2)),
- 'updatetime' => $updatetime,
- 'lampid' => $lamp_id,
- );
- $batteryInfo['chargepower'] = round($batteryInfo['chargecurrent'] * $batteryInfo['battvoltage'],2);
- $batteryInfo['dischargepower'] = round($batteryInfo['discharcurrent'] * $batteryInfo['battvoltage'],2);
- $chargestage = base_convert(mb_substr($solarInfoStr, 0,2), 16, 2);
- $chargestage = mb_strlen($chargestage) < 8 ? mb_substr('00000000'.$chargestage, -8) : $chargestage;
- $batteryInfo['chargestage'] = base_convert(mb_substr($chargestage, 0,4).'0000', 2, 10);
- $batteryInfo['battstatus'] = base_convert(mb_substr($chargestage, -4), 2, 10);
- $this->db->insert('battery_info_log', $batteryInfo);
- // 发用电量信息
- $eleInfoStr = mb_substr($msg1, 112,24);
- $eleInfo = array(
- 'daygeneration' => round(base_convert(mb_substr($eleInfoStr, 0,4),16,10)/10,1),
- 'dayconsumption' => round(base_convert(mb_substr($eleInfoStr, 4,4),16,10)/10,1),
- 'totalgeneration' => round(base_convert(mb_substr($eleInfoStr, 8,8),16,10)/10,1),
- 'totalconsumption' => round(base_convert(mb_substr($eleInfoStr, 16,8),16,10)/10,1),
- 'updatetime' => $updatetime,
- 'lampid' => $lamp_id,
- );
- $this->db->insert('electric_info_log', $eleInfo);
- // 历史数据信息
- $historyInfoStr = mb_substr($msg1, 136,12);
- $historyInfo = array(
- 'rundays' => base_convert(mb_substr($historyInfoStr, 0,4),16,10),
- 'overtimes' => base_convert(mb_substr($historyInfoStr, 4,4),16,10),
- 'fulltimes' => base_convert(mb_substr($historyInfoStr, 8,4),16,10),
- 'updatetime' => $updatetime,
- 'lampid' => $lamp_id,
- );
- $historyInfo['totalchargeah'] = $batteryInfo['daychargeah'];
- $historyInfo['totaldischarah'] = $batteryInfo['daydischarah'];
- $this->db->insert('history_info_log', $historyInfo);
- // 故障信息
- $alarmInfoStr = mb_substr($msg1, 148,8);
- $alarmInfo = array(
- 'alarmtype' => 0,
- 'stralarmtype' => '',
- 'updatetime' => $updatetime,
- 'lampid' => $lamp_id,
- );
- $alarmArr = array('0'=>'过放','1'=>'超压','2'=>'负载短路','3'=>'电池故障','4'=>'内部超温','5'=>'外部超温','7'=>'负载开路','8'=>'输出电容超压保护','10'=>'电池板短路','11'=>'电池板超压','13'=>'充电逆流','15'=>'锂电池低温关闭充电');
- $isfaulted = 0;
- if ($alarmInfoStr != '00000000') {
- $res = base_convert($alarmInfoStr, 16, 2);
- var_dump($res);
- $res = strrev($res);
- $t = array();
- for ($i=0; $i < mb_strlen($res); $i++) {
- if (mb_substr($res, $i,1) == 1 && isset($alarmArr[$i])) {
- $t[] = $alarmArr[$i];
- }
- }
- if (!empty($t)) {
- $alarmInfo['alarmtype'] = base_convert($alarmInfoStr, 16, 10);
- $alarmInfo['stralarmtype'] = implode(',', $t);
- }
- }
- if (!empty($alarmInfo['stralarmtype'])) {
- $this->db->insert('alarm_info_log', $alarmInfo);
- $isfaulted = 1;
- }
- $this->Lamp_model->update(array('id'=>$lamp_id),array('status'=>$lampInfo['lampstatus'],'lighteness'=>$lampInfo['lighteness'],'isfaulted'=>$isfaulted));
- if ($lampData['status'] == 0) $this->Network_model->update(array('id'=>$lampData['networkid']),array('status'=>1));
- }
- }
-
- }elseif ($lampData['lampprotocoltype'] == 1) {
- $sendData = '0103000a0002e409';
- $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*','0001'.$sendData));
- if (!empty($res) && strtolower(crc16(pack('H*',mb_substr($res['msg'], 0,-4)))) == strtolower(mb_substr($res['msg'], -4))) {
- $sysInfoStr = $res['msg'];
- $sendData = '010300fd003115ee';
- $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*','0001'.$sendData));
- if (!empty($res) && strtolower(crc16(pack('H*',mb_substr($res['msg'], 0,-4)))) == strtolower(mb_substr($res['msg'], -4))) {
- $data = array();
- $msg = $res['msg'];
- // 校验通过,解析数据
- $updatetime = date('Y-m-d H:i:s',time());
- // 系统信息
- $systemInfo = array(
- 'sysvoltage' => base_convert(mb_substr($sysInfoStr, 6,2), 16, 10),
- 'syscurrent' => base_convert(mb_substr($sysInfoStr, 8,2), 16, 10),
- 'temper' => bitDecode(mb_substr($msg, 30,2)),
- 'updatetime' => $updatetime,
- 'lampid' => $lamp_id,
- );
- $this->db->insert('system_info_log', $systemInfo);
- // 路灯信息
- $lampInfoStr = mb_substr($msg, 34,12);
- $lampInfo = array(
- 'lampvoltage' => round(base_convert(mb_substr($lampInfoStr, 0,4), 16, 10)/10,1),
- 'lampcurrent' => round(base_convert(mb_substr($lampInfoStr, 4,4), 16, 10)/100,2),
- 'updatetime' => $updatetime,
- 'lampid' => $lamp_id,
- 'lamppower' => base_convert(mb_substr($lampInfoStr, 8,4), 16, 10),
- );
- $light = base_convert(mb_substr($msg, 6,2), 16, 2);
- if (base_convert(mb_substr('00000000'.$light, -7), 2, 10) > 0) {
- $lampInfo['lighteness'] = base_convert(mb_substr($light, -7), 2, 10);
- $lampInfo['lampstatus'] = 1;
- }else{
- $lampInfo['lighteness'] = 0;
- $lampInfo['lampstatus'] = 0;
- }
- $this->db->insert('lamp_info_log', $lampInfo);
- // 太阳能信息
- $solarInfoStr = mb_substr($msg, 46,12);
- $solarInfo = array(
- 'solarvoltage' => round(base_convert(mb_substr($solarInfoStr, 0,4), 16, 10)/10,1),
- 'solarcurrent' => round(base_convert(mb_substr($solarInfoStr, 4,4), 16, 10)/100,2),
- 'updatetime' => $updatetime,
- 'lampid' => $lamp_id,
- );
- $solarInfo['solarpower'] = round($solarInfo['solarvoltage'] * $solarInfo['solarcurrent'],2);
- $this->db->insert('solar_info_log', $solarInfo);
- // 蓄电池信息
- // $batterInfoStr = mb_substr($msg, 42,70);
- $batteryInfo = array(
- 'battvoltage' => round(base_convert(mb_substr($msg, 22,4), 16, 10)/10,1),
- 'chargecurrent' => round(base_convert(mb_substr($msg, 26,4), 16, 10)/100,2),
- // 'discharcurrent' => round(base_convert(mb_substr($msg, 10,4), 16, 10)/100,2),
- 'batttemper' => bitDecode(mb_substr($msg, 32,2)),
- // 'electrictotal' => base_convert(mb_substr($msg, 16,4),16,10),
- // 'electricleft' => base_convert(mb_substr($msg, 20,4),16,10),
- 'electricSOC' => base_convert(mb_substr($msg, 18,4),16,10),
- 'voltagedaymin' => round(base_convert(mb_substr($msg, 62,4),16,10)/10,1),
- 'voltagedaymax' => round(base_convert(mb_substr($msg, 66,4),16,10)/10,1),
- 'daychargeah' => base_convert(mb_substr($msg, 86,4),16,10),
- 'daydischarah' => base_convert(mb_substr($msg, 90,4),16,10),
- 'daychargemaxpow' => base_convert(mb_substr($msg, 78,4),16,10),
- 'daydischarmaxpow' => base_convert(mb_substr($msg, 82,4),16,10),
- 'daychargemincurrent' => base_convert(mb_substr($msg, 174,4),16,10),
- 'daycharmaxcurrent' => round(base_convert(mb_substr($msg, 70,4),16,10)/100,2),
- 'daydischargemincurrent' => base_convert(mb_substr($msg, 194,4),16,10),
- 'daydischarmaxcurrent' => round(base_convert(mb_substr($msg, 74,4),16,10)/100,2),
- 'daybattmintemper' => doubBitDecode(mb_substr($msg, 162,4)),
- 'daybattmaxtemper' => doubBitDecode(mb_substr($msg, 158,4)),
- 'updatetime' => $updatetime,
- 'lampid' => $lamp_id,
- );
- $batteryInfo['chargepower'] = round($batteryInfo['chargecurrent'] * $batteryInfo['battvoltage'],2);
- // $batteryInfo['dischargepower'] = round($batteryInfo['discharcurrent'] * $batteryInfo['battvoltage'],2);
- $status = base_convert(mb_substr($msg, 8,2),16,10);
- if (mb_strlen($light) == 8 && mb_substr($light, 0,1) == 1) {
- $batteryInfo['battstatus'] = 1;
- }else{
- if ($status != 0){
- $batteryInfo['battstatus'] = 2;
- }else{
- $batteryInfo['battstatus'] = 0;
- }
- }
- $batteryInfo['chargestage'] = $status;
- $this->db->insert('battery_info_log', $batteryInfo);
- // 发用电量信息
- // $eleInfoStr = mb_substr($msg1, 112,24);
- $eleInfo = array(
- 'daygeneration' => round(base_convert(mb_substr($msg, 94,4),16,10)/1000,3),
- 'dayconsumption' => round(base_convert(mb_substr($msg, 98,4),16,10)/1000,3),
- 'totalgeneration' => round(base_convert(mb_substr($msg, 134,8),16,10)/1000,3),
- 'totalconsumption' => round(base_convert(mb_substr($msg, 142,8),16,10)/1000,3),
- 'updatetime' => $updatetime,
- 'lampid' => $lamp_id,
- );
- $this->db->insert('electric_info_log', $eleInfo);
- // 历史数据信息
- $historyInfoStr = mb_substr($msg, 102,28);
- $historyInfo = array(
- 'rundays' => base_convert(mb_substr($historyInfoStr, 0,4),16,10),
- 'overtimes' => base_convert(mb_substr($historyInfoStr, 4,4),16,10),
- 'fulltimes' => base_convert(mb_substr($historyInfoStr, 8,4),16,10),
- 'totalchargeah' => base_convert(mb_substr($historyInfoStr, 12,8),16,10),
- 'totaldischarah' => base_convert(mb_substr($historyInfoStr, 20,8),16,10),
- 'updatetime' => $updatetime,
- 'lampid' => $lamp_id,
- );
- $this->db->insert('history_info_log', $historyInfo);
- // 故障信息
- $alarmInfoStr = mb_substr($msg, 10,8);
- $alarmInfo = array(
- 'alarmtype' => 0,
- 'stralarmtype' => '',
- 'updatetime' => $updatetime,
- 'lampid' => $lamp_id,
- );
- $alarmArr = array('10'=>'BMS过充保护','11'=>'电池低温保护,停止充电','12'=>'电池反接','13'=>'电容超压','14'=>'感应探头损坏','15'=>'负载开路','16'=>'蓄电池过放','17'=>'蓄电池超压','18'=>'欠压警告','19'=>'负载短路','20'=>'负载功率过大或负载过流','21'=>'控制器温度过高','22'=>'外部环境温度过高','23'=>'光伏输入功率过大','24'=>'光伏输入端短路','25'=>'光伏输入端超压','26'=>'太阳板逆流','27'=>'太阳板工作点超压','28'=>'太阳板反接');
- $isfaulted = 0;
- if ($alarmInfoStr != '00000000') {
- $res = base_convert($alarmInfoStr, 16, 2);
- var_dump($res);
- $res = strrev($res);
- $t = array();
- for ($i=0; $i < mb_strlen($res); $i++) {
- if (mb_substr($res, $i,1) == 1 && isset($alarmArr[$i])) {
- $t[] = $alarmArr[$i];
- }
- }
- if (!empty($t)) {
- $alarmInfo['alarmtype'] = base_convert($alarmInfoStr, 16, 10);
- $alarmInfo['stralarmtype'] = implode(',', $t);
- }
- }
- if (!empty($alarmInfo['stralarmtype'])) {
- $this->db->insert('alarm_info_log', $alarmInfo);
- $isfaulted = 1;
- }
- $this->Lamp_model->update(array('id'=>$lamp_id),array('status'=>$lampInfo['lampstatus'],'lighteness'=>$lampInfo['lighteness'],'isfaulted'=>$isfaulted));
- if ($lampData['status'] == 0) $this->Network_model->update(array('id'=>$lampData['networkid']),array('status'=>1));
- }
- }
-
- }
- }
- // 灯控的开启/关闭(支持批量处理)
- public function turnonoff(){
- $username = $this->input->post('username',true);
- $client_key = $this->input->post('client_key',true);
- $token = $this->input->post('token',true);
- $type = intval($this->input->post('type',true));
- $lamp_id = $this->input->post('lamp_id',true);
- $clientid = $this->input->post('clientid',true);
- $version = $this->session->userdata('version');
- $lampArr = explode(',', $lamp_id);
- $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/dimming_asy';
- $lightness = empty($type) ? 0 : 100;
- foreach ($lampArr as $l) {
- doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'type'=>$type,'lamp_id'=>$l,'lightness'=>$lightness]);
- }
-
- exit();
- }
- // 灯控调光
- public function dimming(){
- $username = $this->input->post('username',true);
- $client_key = $this->input->post('client_key',true);
- $token = $this->input->post('token',true);
- $lamp_id = $this->input->post('lamp_id',true);
- $lightness = $this->input->post('lightness',true);
- $clientid = $this->input->post('clientid',true);
- $version = $this->session->userdata('version');
- $lampArr = explode(',', $lamp_id);
- // $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
- $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/dimming_asy';
- foreach ($lampArr as $l) {
- doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'lamp_id'=>$l,'lightness'=>$lightness]);
- }
-
- exit();
- }
- public function dimming_asy(){
- set_time_limit(0);
- $lamp_id = intval($this->input->post('lamp_id',true));
- $lightness = intval($this->input->post('lightness',true));
- $clientid = $this->input->post('clientid',true);
- $version = $this->input->post('version',true);
- if (empty($lamp_id)) {
- exit(json_result('0400', $this->response['0400'], array()));
- }
- $lampData = $this->Lamp_model->get_one(['id'=>$lamp_id],'number,address');
- $seq = get_seq();
- $seq = '01';
- $sendData = '12'.PROTOCOL_VERSION.$seq.'0000000000';
- $sendData .= $lightness > 0 ? '01' : '00';
- $lightness2 = empty($lightness) ? '00' : '00'.base_convert($lightness, 10, 16);
- $sendData .= strlen($lightness2) == 2 ? $lightness2 : substr($lightness2, -2);
- $sendData .= '00';
- $sendData .= crc16(pack('H*',$sendData));
- $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*',$sendData),30);
- if (empty($res)) {
- $data = ['code'=>'0016','status'=>2,'msg'=>$this->response['0016'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
- exit(send_websocket($clientid,$data));
- }else{
- $res['msg'] = unpack('H*', $res['msg'])[1];
- if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'], -4)) && strtolower(substr($res['msg'], 18,2)) == '01') {
- if ($lightness != 0) {
- $this->Lamp_model->update(array('id'=>$lampData['id']),array('status'=>1,'lighteness'=>$lightness));
- }else{
- $this->Lamp_model->update(array('id'=>$lampData['id']),array('status'=>0,'lighteness'=>0));
- }
- $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$lamp_id),'P.projectname,N.networkname,L.number,L.address');
- $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯调光\"{$lightness}%\"",0);
- $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);
- $data = ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
- exit(send_websocket($clientid,$data));
- }else{
- $data = ['code'=>'0010','status'=>2,'msg'=>$this->response['0010'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
- exit(send_websocket($clientid,$data));
- }
- }
- }
- // 删除灯控(支持批量处理)
- public function del(){
- $role = $this->get_user_info('role');
- $lamp_ids = $this->input->post('lamp_ids',true);
- if (empty($lamp_ids)) {
- exit(json_result('0400', $this->response['0400'], array()));
- }
- $ids = explode(",", $lamp_ids);
- // 获取灯控信息
- $dataArr = $this->Lamp_model->getBatch($ids);
- // 修改网络跟项目下灯控数量
- foreach ($dataArr as $data) {
- $data['faultcount'] = $data['isfaulted'] > 0 ? 1 : 0;
- $this->Network_model->minus_lamp_count($data);
- $this->Project_model->minus_lamp_count($data);
- $netData = $this->Network_model->getOne($data['networkid'],'gatewaytype,protocoltype');
- if ($netData['protocoltype'] == 6) $this->remove_device('00000000'.$data['address']);
- if (isset($netData) && $netData['gatewaytype'] == 'direct' && !empty($data['networkid'])) {// 直连设备
- // 删除网络
- // $this->Zone_model->delOne($data['networkid']);
- $this->Network_model->delData(array('id'=>$data['networkid']));
- $cmdstr = '{"cmd_type":"delete_network_cmd","cmd_id":'.$data['networkid'].'}';
- // send_cmd($cmdstr,0);
- if (isset($netData['protocoltype']) && $netData['protocoltype'] != 0) {
- $cmdret = send_cmd($cmdstr,1,0,$netData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmdstr,1,0,$netData['protocoltype']);
- }
- }else{
- $cmdret = send_cmd($cmdstr,0,0,$netData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmdstr,0,0,$netData['protocoltype']);
- }
- }
- }
- }
- // 删除灯控告警信息
- $this->Alarm_model->delBatch($ids, 'lampid');
-
- foreach ($ids as $v) {
- $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$v),'P.projectname,N.networkname,L.number,L.address');
- $this->add_operation_log('delete',"删除\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯",0);
- $this->add_operation_log('delete',"Delete lamp:\"{$lampData['number']}\".Wireless module address:\"{$lampData['address']}\".Project name:\"{$lampData['projectname']}\".Network name:\"{$lampData['networkname']}\"",0,1);
- }
- $this->Lamp_model->delBatch($ids);
- exit(json_result('0000', $this->response['0000'], array()));
- }
- // 默认巡检设置
- private function patrol_set($lampid,$time = null){
- if (isset($time) && !empty($time)) {
- $where['id'] = $lampid;
- $update['patroltype'] = 1;
- $update['patrolinterval'] = $time;
- $update['updatetime'] = date("Y-m-d H:i:s");
- $update['nextquerytime'] = date("Y-m-d H:i:s");
- $ret = $this->Patrol_model->get_one($where);
- if(empty($ret)){
- $update['id'] = $where['id'];
- $this->Patrol_model->add($update);
- } else {
- $this->Patrol_model->update($update,$where);
- }
- $res = $this->Lamp_model->getOne($lampid,'N.protocoltype,L.address,L.protocoltype AS lampprotocoltype');
- if ($res['protocoltype'] == 4) {
- $sendData = '000101A501';;
- $sendData .= bitSubStr(base_convert(intval($time/5), 10, 16));
- lampMqttCmd('/lamp/WriteIn/'.$res['address'],'/lamp/WriteOut/'.$res['address'],pack('H*', $sendData),0);
- return;
- }
- if (isset($res['protocoltype']) && $res['protocoltype'] != 0) {
- $cmd = '{"cmd_type":"set_patrol_cmd","cmd_id":'.$where['id'].',"broadcast":0}';
- $cmdret = send_cmd($cmd,1, 0,$res['protocoltype']);
- // if ($cmdret === false) {
- // sleep(1);
- // $cmdret = send_cmd($cmd,1, 0,$res['protocoltype']);
- // }
- }
- }
-
- //$cmd = '{"cmd_type":"patrol_cmd","cmd_id":'.$where['id'].',"broadcast":0}';
- //$cmdret = send_cmd($cmd, 1);
- }
- // 更新负载设置
- public function set_load(){
- $username = $this->input->post('username',true);
- $client_key = $this->input->post('client_key',true);
- $token = $this->input->post('token',true);
- $lamp_id = $this->input->post('lamp_id',true);
- $clientid = $this->input->post('clientid',true);
- $version = $this->session->userdata('version');
- $workmode = $this->input->post('workmode',true);
- $worktimefirst = $this->input->post('worktimefirst',true);
- $workpowerfirst = $this->input->post('workpowerfirst',true);
- $worktimesencond = $this->input->post('worktimesencond',true);
- $workpowersencond = $this->input->post('workpowersencond',true);
- $worktimethird = $this->input->post('worktimethird',true);
- $workpowerthird = $this->input->post('workpowerthird',true);
- $worktimeforth = $this->input->post('worktimeforth',true);
- $workpowerforth = $this->input->post('workpowerforth',true);
- $voptically = $this->input->post('voptically',true);
- $delaytime = $this->input->post('delaytime',true);
- $ledloadcurrent = $this->input->post('ledloadcurrent',true);
- $powercmd = $this->input->post('powercmd',true);
- $switchfeature = $this->input->post('switchfeature',true);
- $lampArr = explode(',', $lamp_id);
- $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
- $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/set_load_asy';
- foreach ($lampList as $l) {
- if ($l['protocoltype'] == 0) {
- 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);
- }else{
- 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]);
- }
-
- }
- exit();
- }
- //更新负载设置
- public function set_load_asy(){
- $res = $this->load_update();
- $clientid = $this->input->post('clientid',true);
- $lampid = $this->input->post('lamp_id',true);
- $data = $this->Lamp_model->getData(array('id'=>$lampid),'projectid,networkid,address,number');
- if ($res['msg'] == '0000') {
- $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
- $networkData = $this->Network_model->getData(array('id'=>$data['networkid']),'networkname');
-
- $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯负载参数");
- $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);
- }
- if (empty($res['result'])) {
- // exit(json_result($res['msg'],$res['msg'],array('number'=>$data['number'])));
- $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
- exit(send_websocket($clientid,$data));
- }else{
- // exit(json_result($res['msg'],$this->response[$res['msg']],array('number'=>$data['number'])));
- $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$this->response[$res['msg']],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
- exit(send_websocket($clientid,$data));
- }
- }
- //更新负载设置
- private function load_update(){
- $where['relateid'] = $this->input->post('lamp_id',true);
- $update['relateid'] = $this->input->post('lamp_id',true);
- $update['workmode'] = $this->input->post('workmode',true);
- $update['worktimefirst'] = $this->input->post('worktimefirst',true);
- $update['workpowerfirst'] = $this->input->post('workpowerfirst',true);
- $update['worktimesencond'] = $this->input->post('worktimesencond',true);
- $update['workpowersencond'] = $this->input->post('workpowersencond',true);
- $update['worktimethird'] = $this->input->post('worktimethird',true);
- $update['workpowerthird'] = $this->input->post('workpowerthird',true);
- $update['worktimeforth'] = $this->input->post('worktimeforth',true);
- $update['workpowerforth'] = $this->input->post('workpowerforth',true);
- $update['voptically'] = $this->input->post('voptically',true);
- $update['delaytime'] = $this->input->post('delaytime',true);
- $update['ledloadcurrent'] = $this->input->post('ledloadcurrent',true);
- $update['powercmd'] = $this->input->post('powercmd',true);
- $update['switchfeature'] = $this->input->post('switchfeature',true);
- $update['updatetime'] = date("Y-m-d H:i:s");
- return $this->load_set($where, $update, 0);
- }
- private function load_set($where,$update,$broadcast){
- $ret = $this->Load_model->get_data_by_filter(['relateid'=>$update['relateid'],'cmdtype'=>0]);
- if(empty($ret)){
- // $update['id'] = $where['id'];
- $id = $this->Load_model->insert($update);
- } else {
- $this->Load_model->update(['id'=>$ret['id']],$update);
- $id = $ret['id'];
- }
-
- $cmd = '{"cmd_type":"load_param_cmd","cmd_id":'.$id.',"broadcast":'.$broadcast.'}';
- $lampProData = $this->Lamp_model->getOne($where['relateid'],'N.protocoltype,L.address,L.protocoltype AS lampprotocoltype,N.deviceid');
- if (($lampProData['protocoltype'] == 4 || $lampProData['protocoltype'] == 6) && $lampProData['lampprotocoltype'] == 0) {
- $address = '';
- if (mb_strlen($lampProData['address']) >= 8) {
- $address = mb_substr($lampProData['address'], -8);
- }else{
- $address = '00000000';
- }
- $sendData = '10040010';
- $sendData .= bitSubStr(base_convert($update['workmode'], 10, 16));
- $sendData .= bitSubStr(base_convert($update['worktimefirst'], 10, 16));
- $sendData .= bitSubStr(base_convert($update['workpowerfirst'], 10, 16));
- $sendData .= bitSubStr(base_convert($update['worktimesencond'], 10, 16));
- $sendData .= bitSubStr(base_convert($update['workpowersencond'], 10, 16));
- $sendData .= bitSubStr(base_convert($update['worktimethird'], 10, 16));
- $sendData .= bitSubStr(base_convert($update['workpowerthird'], 10, 16));
- $sendData .= bitSubStr(base_convert($update['worktimeforth'], 10, 16));
- $sendData .= bitSubStr(base_convert($update['workpowerforth'], 10, 16));
- $sendData .= bitSubStr(base_convert($update['delaytime'], 10, 16));
- $sendData .= doubBitSubStr(base_convert(intval($update['voptically']*10), 10, 16));
- $sendData .= doubBitSubStr(base_convert(intval($update['ledloadcurrent']), 10, 16));
- $sendData .= bitSubStr(base_convert($update['powercmd'], 10, 16));
- $sendData .= bitSubStr(base_convert($update['switchfeature'], 10, 16));
- $sum = 0;
- for ($i=0; $i < mb_strlen($sendData)/2; $i++) {
- if ($i == 0) continue;
- $t = mb_substr($sendData, $i*2,2);
- $sum += base_convert($t, 16, 10);
- }
- $sum = base_convert($sum, 10, 16);
- $sendData .= bitSubStr($sum);
- $sendData = $address.$sendData;
-
- if ($lampProData['protocoltype'] == 4) {
- $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampProData['address'],'/IDCOL/CmdOutput/'.$lampProData['address'],pack('H*','0001'.$sendData));
- }else{
- device_cmd($lampProData['deviceid'],pack('H*',$sendData));
- $cmd = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
- $info = send_lorawan_915($cmd);
- $res = array();
- if (!empty($info)) {
- $info = json_decode($info,true);
- if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
- }
- }
-
- if (empty($res)) {
- return array('result'=>1,'msg'=>'0016');
- }else{
- $msg1 = $res['msg'];
- // 数据校验
- $msg = mb_substr($msg1, 10);
- $t = 0;
- for ($i=0; $i < mb_strlen($msg) / 2 - 1; $i++) {
- $s = mb_substr($msg, $i*2,2);
- $t += base_convert($s, 16, 10);
- }
- $t = base_convert($t, 10, 16);
- if (mb_strlen($t) > 2) {
- $t = mb_substr($t, -2);
- }elseif($t <= 1){
- $t = '0'.$t;
- }
- if ($t == mb_substr($msg, -2)) {
- if (mb_substr($msg1, -10,-2) == '10040010') {
- return array('result'=>1,'msg'=>'0000');
- }else{
- return array('result'=>1,'msg'=>'0010');
- }
- }else{
- return array('result'=>1,'msg'=>'0010');
- }
- }
- }
- if (isset($lampProData['protocoltype']) && $lampProData['protocoltype'] != 0) {
- $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
- }
- }else{
- $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
- }
- }
-
- $res = array();
- if (empty($cmdret)){
- $res['result'] = 1;
- $res['msg'] = '0016';
- return $res;
- }
-
- if($cmdret === false){
- $res['result'] = 1;
- $res['msg'] = '0008';
- } else {
- $res = json_decode($cmdret, true);
- if ($res['result'] == false) {
- $res['result'] = 0;
- $version = $this->input->post('version',true);
- if (empty($version)) {
- $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
- }else{
- $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
- }
- } else {
- $res['result'] = 1;
- $res['msg'] = '0000';
- }
- }
- return $res;
- }
- // 新控制器更新负载设置
- public function modbus_set_load(){
- $data['username'] = $this->input->post('username',true);
- $data['client_key'] = $this->input->post('client_key',true);
- $data['token'] = $this->input->post('token',true);
- $data['lamp_id'] = $this->input->post('lamp_id',true);
- $data['clientid'] = $this->input->post('clientid',true);
- $data['version'] = $this->session->userdata('version');
- // $data['light_voltage'] = $this->input->post('light_voltage',true);
- $data['intell_power'] = $this->input->post('intell_power',true);
- $data['load_current'] = $this->input->post('load_current',true);
- $data['light_delay'] = $this->input->post('light_delay',true);
- $data['induction_delay'] = $this->input->post('induction_delay',true);
- $data['first_light_time'] = $this->input->post('first_light_time',true);
- $data['first_light_human_power'] = $this->input->post('first_light_human_power',true);
- $data['first_light_unman_power'] = $this->input->post('first_light_unman_power',true);
- $data['second_light_time'] = $this->input->post('second_light_time',true);
- $data['second_light_human_power'] = $this->input->post('second_light_human_power',true);
- $data['second_light_unman_power'] = $this->input->post('second_light_unman_power',true);
- $data['third_light_time'] = $this->input->post('third_light_time',true);
- $data['third_light_human_power'] = $this->input->post('third_light_human_power',true);
- $data['third_light_unman_power'] = $this->input->post('third_light_unman_power',true);
- $data['fourth_light_time'] = $this->input->post('fourth_light_time',true);
- $data['fourth_light_human_power'] = $this->input->post('fourth_light_human_power',true);
- $data['fourth_light_unman_power'] = $this->input->post('fourth_light_unman_power',true);
- $data['fifth_light_time'] = $this->input->post('fifth_light_time',true);
- $data['fifth_light_human_power'] = $this->input->post('fifth_light_human_power',true);
- $data['fifth_light_unman_power'] = $this->input->post('fifth_light_unman_power',true);
- $data['sixth_light_time'] = $this->input->post('sixth_light_time',true);
- $data['sixth_light_human_power'] = $this->input->post('sixth_light_human_power',true);
- $data['sixth_light_unman_power'] = $this->input->post('sixth_light_unman_power',true);
- $data['seventh_light_time'] = $this->input->post('seventh_light_time',true);
- $data['seventh_light_human_power'] = $this->input->post('seventh_light_human_power',true);
- $data['seventh_light_unman_power'] = $this->input->post('seventh_light_unman_power',true);
- $data['eighth_light_time'] = $this->input->post('eighth_light_time',true);
- $data['eighth_light_human_power'] = $this->input->post('eighth_light_human_power',true);
- $data['eighth_light_unman_power'] = $this->input->post('eighth_light_unman_power',true);
- $data['ninth_light_time'] = $this->input->post('ninth_light_time',true);
- $data['ninth_light_human_power'] = $this->input->post('ninth_light_human_power',true);
- $data['ninth_light_unman_power'] = $this->input->post('ninth_light_unman_power',true);
- $data['tenth_light_time'] = $this->input->post('tenth_light_time',true);
- $data['tenth_light_human_power'] = $this->input->post('tenth_light_human_power',true);
- $data['tenth_light_unman_power'] = $this->input->post('tenth_light_unman_power',true);
- $data['begin_voltage'] = $this->input->post('begin_voltage',true);
- $data['end_voltage'] = $this->input->post('end_voltage',true);
- $data['end_current'] = $this->input->post('end_current',true);
- foreach ($data as $key => $value) {
- $data[$key] = urldecode($value);
- }
- $file = fopen('../upload/test.txt', 'a+');
- fwrite($file, json_encode($data).'------'.date('Y-m-d H:i:s').'
- ');
- fclose($file);
- $lampArr = explode(',', $data['lamp_id']);
- $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
- $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/modbus_set_load_asy';
- foreach ($lampList as $l) {
- $data['lamp_id'] = $l['id'];
- if ($l['protocoltype'] == 0) {
- doAsyncRequest($url,$data,30);
- }else{
- doAsyncRequest($url,$data);
- }
-
- }
- exit();
- }
- // 新控制器更新负载设置
- public function modbus_set_load_asy(){
- $res = $this->modbus_load_update();
- $clientid = $this->input->post('clientid',true);
- $lampid = $this->input->post('lamp_id',true);
- $data = $this->Lamp_model->getData(array('id'=>$lampid),'projectid,networkid,address,number');
- $file = fopen('../upload/test.txt', 'a+');
- fwrite($file, '66666------'.date('Y-m-d H:i:s').'
- ');
- fclose($file);
- if ($res['msg'] == '0000') {
- $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
- $networkData = $this->Network_model->getData(array('id'=>$data['networkid']),'networkname');
-
- $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯负载参数");
- $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);
- }
- if (empty($res['result'])) {
- // exit(json_result($res['msg'],$res['msg'],array('number'=>$data['number'])));
- $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
- $file = fopen('../upload/test.txt', 'a+');
- fwrite($file, json_encode($data).'---'.$clientid.'---'.date('Y-m-d H:i:s').'
- ');
- fclose($file);
- exit(send_websocket($clientid,$data));
- }else{
- // exit(json_result($res['msg'],$this->response[$res['msg']],array('number'=>$data['number'])));
- $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$this->response[$res['msg']],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
- $file = fopen('../upload/test.txt', 'a+');
- fwrite($file, json_encode($data).'---'.$clientid.'---'.date('Y-m-d H:i:s').'
- ');
- fclose($file);
- exit(send_websocket($clientid,$data));
- }
- }
- private function format_date($date){
- return intval(explode(':', $date)[0]) * 3600 + intval(explode(':', $date)[1]) * 60;
- }
- // 新控制器更新负载设置
- private function modbus_load_update(){
- $where['relateid'] = $this->input->post('lamp_id',true);
- $data['relateid'] = $this->input->post('lamp_id',true);
- // $data['light_voltage'] = $this->input->post('light_voltage',true);
- $data['intell_power'] = $this->input->post('intell_power',true);
- $data['load_current'] = $this->input->post('load_current',true);
- $data['light_delay'] = $this->input->post('light_delay',true);
- $data['induction_delay'] = $this->input->post('induction_delay',true);
- $data['first_light_time'] = $this->input->post('first_light_time',true);
- // $data['first_light_time'] = $data['first_light_time'] * 3600;
- $data['first_light_time'] = $this->format_date($data['first_light_time']);
- $data['first_light_human_power'] = $this->input->post('first_light_human_power',true);
- $data['first_light_unman_power'] = $this->input->post('first_light_unman_power',true);
- $data['second_light_time'] = $this->input->post('second_light_time',true);
- // $data['second_light_time'] = $data['second_light_time'] * 3600;
- $data['second_light_time'] = $this->format_date($data['second_light_time']);
- $data['second_light_human_power'] = $this->input->post('second_light_human_power',true);
- $data['second_light_unman_power'] = $this->input->post('second_light_unman_power',true);
- $data['third_light_time'] = $this->input->post('third_light_time',true);
- // $data['third_light_time'] = $data['third_light_time'] * 3600;
- $data['third_light_time'] = $this->format_date($data['third_light_time']);
- $data['third_light_human_power'] = $this->input->post('third_light_human_power',true);
- $data['third_light_unman_power'] = $this->input->post('third_light_unman_power',true);
- $data['fourth_light_time'] = $this->input->post('fourth_light_time',true);
- // $data['fourth_light_time'] = $data['fourth_light_time'] * 3600;
- $data['fourth_light_time'] = $this->format_date($data['fourth_light_time']);
- $data['fourth_light_human_power'] = $this->input->post('fourth_light_human_power',true);
- $data['fourth_light_unman_power'] = $this->input->post('fourth_light_unman_power',true);
- $data['fifth_light_time'] = $this->input->post('fifth_light_time',true);
- // $data['fifth_light_time'] = $data['fifth_light_time'] * 3600;
- $data['fifth_light_time'] = $this->format_date($data['fifth_light_time']);
- $data['fifth_light_human_power'] = $this->input->post('fifth_light_human_power',true);
- $data['fifth_light_unman_power'] = $this->input->post('fifth_light_unman_power',true);
- $data['sixth_light_time'] = $this->input->post('sixth_light_time',true);
- // $data['sixth_light_time'] = $data['sixth_light_time'] * 3600;
- $data['sixth_light_time'] = $this->format_date($data['sixth_light_time']);
- $data['sixth_light_human_power'] = $this->input->post('sixth_light_human_power',true);
- $data['sixth_light_unman_power'] = $this->input->post('sixth_light_unman_power',true);
- $data['seventh_light_time'] = $this->input->post('seventh_light_time',true);
- // $data['seventh_light_time'] = $data['seventh_light_time'] * 3600;
- $data['seventh_light_time'] = $this->format_date($data['seventh_light_time']);
- $data['seventh_light_human_power'] = $this->input->post('seventh_light_human_power',true);
- $data['seventh_light_unman_power'] = $this->input->post('seventh_light_unman_power',true);
- $data['eighth_light_time'] = $this->input->post('eighth_light_time',true);
- // $data['eighth_light_time'] = $data['eighth_light_time'] * 3600;
- $data['eighth_light_time'] = $this->format_date($data['eighth_light_time']);
- $data['eighth_light_human_power'] = $this->input->post('eighth_light_human_power',true);
- $data['eighth_light_unman_power'] = $this->input->post('eighth_light_unman_power',true);
- $data['ninth_light_time'] = $this->input->post('ninth_light_time',true);
- // $data['ninth_light_time'] = $data['ninth_light_time'] * 3600;
- $data['ninth_light_time'] = $this->format_date($data['ninth_light_time']);
- $data['ninth_light_human_power'] = $this->input->post('ninth_light_human_power',true);
- $data['ninth_light_unman_power'] = $this->input->post('ninth_light_unman_power',true);
- $data['tenth_light_time'] = $this->input->post('tenth_light_time',true);
- // $data['tenth_light_time'] = $data['tenth_light_time'] * 3600;
- $data['tenth_light_time'] = $this->format_date($data['tenth_light_time']);
- $data['tenth_light_human_power'] = $this->input->post('tenth_light_human_power',true);
- $data['tenth_light_unman_power'] = $this->input->post('tenth_light_unman_power',true);
- $data['begin_voltage'] = $this->input->post('begin_voltage',true);
- $data['end_voltage'] = $this->input->post('end_voltage',true);
- $data['end_current'] = $this->input->post('end_current',true);
- $data['updatetime'] = date("Y-m-d H:i:s");
- $file = fopen('../upload/test.txt', 'a+');
- fwrite($file, json_encode($data).'------'.date('Y-m-d H:i:s').'
- ');
- fclose($file);
- return $this->modbus_load_set($where, $data, 0);
- }
- private function modbus_load_set($where,$update,$broadcast){
- $file = fopen('../upload/test.txt', 'a+');
- fwrite($file, '11111------'.date('Y-m-d H:i:s').'
- ');
- fclose($file);
- $ret = $this->Modbus_load_model->get_data_by_filter(['relateid'=>$update['relateid'],'cmdtype'=>0]);
- if(empty($ret)){
- // $update['id'] = $where['id'];
- $id = $this->Modbus_load_model->insert($update);
- } else {
- $this->Modbus_load_model->update(['id'=>$ret['id']],$update);
- $id = $ret['id'];
- }
- $file = fopen('../upload/test.txt', 'a+');
- fwrite($file, '22222------'.date('Y-m-d H:i:s').'
- ');
- fclose($file);
- $cmd = '{"cmd_type":"modbus_load_param_cmd","cmd_id":'.$id.',"broadcast":'.$broadcast.'}';
- $lampProData = $this->Lamp_model->getOne($where['relateid'],'N.protocoltype,N.deviceid,L.protocoltype as lampprotocoltype,L.address');
- if ($lampProData['protocoltype'] == 4 && $lampProData['lampprotocoltype'] == 1) {
- $sendData = '0103e08d0027a23b';
-
- $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampProData['address'],'/IDCOL/CmdOutput/'.$lampProData['address'],pack('H*','0001'.$sendData));
- if (empty($res)) {
- return array('result'=>1,'msg'=>'0016');
- }else{
- if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
- $old = $res['msg'];
- $data = array();
- $sendData = '';
- $sendData .= doubBitSubStr(base_convert(intval($update['load_current']*100), 10, 16)).''; // 负载电流
- $sendData .= doubBitSubStr(base_convert(intval($update['intell_power']), 10, 16)).''; // 智能功率
- $sendData .= doubBitSubStr(base_convert(intval($update['light_delay']), 10,16)).''; // 光控延时
- $sendData .= doubBitSubStr(base_convert(intval($update['induction_delay']), 10,16)).''; // 感应延时
- $sendData .= substr($old, 22,4);
- $sendData .= doubBitSubStr(base_convert(intval($update['first_light_time']), 10,16)).''; // 第一段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['first_light_human_power']), 10,16)).''; // 第一段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['first_light_unman_power']), 10,16)).''; // 第一段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['second_light_time']), 10,16)).''; // 第二段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['second_light_human_power']), 10,16)).''; // 第二段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['second_light_unman_power']), 10,16)).''; // 第二段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['third_light_time']), 10,16)).''; // 第三段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['third_light_human_power']), 10,16)).''; // 第三段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['third_light_unman_power']), 10,16)).''; // 第三段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_time']), 10,16)).''; // 第四段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_human_power']), 10,16)).''; // 第四段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_unman_power']), 10,16)).''; // 第四段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_time']), 10,16)).''; // 第五段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_human_power']), 10,16)).''; // 第五段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_unman_power']), 10,16)).''; // 第五段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_time']), 10,16)).''; // 第六段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_human_power']), 10,16)).''; // 第六段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_unman_power']), 10,16)).''; // 第六段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_time']), 10,16)).''; // 第七段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_human_power']), 10,16)).''; // 第七段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_unman_power']), 10,16)).''; // 第七段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_time']), 10,16)).''; // 第八段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_human_power']), 10,16)).''; // 第八段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_unman_power']), 10,16)).''; // 第八段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_time']), 10,16)).''; // 第九段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_human_power']), 10,16)).''; // 第九段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_unman_power']), 10,16)).''; // 第九段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_time']), 10,16)).''; // 晨亮时间
- $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_human_power']), 10,16)).''; // 晨亮有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_unman_power']), 10,16)).''; // 晨亮无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['begin_voltage']*10), 10, 16)).''; // 智能功率USER1起始电压
- $sendData .= doubBitSubStr(base_convert(intval($update['end_voltage']*10), 10, 16)).''; // 智能功率USER1终止电压
- $sendData .= substr($old, 154,4);
- $sendData .= doubBitSubStr(base_convert(intval($update['end_current']*100), 10, 16)).''; // 智能功率USER1终止电流
- $len = doubBitSubStr(base_convert(intval(mb_strlen($sendData)/4), 10, 16)).bitSubStr(base_convert(intval(mb_strlen($sendData)/2), 10, 16));
- $sendData = '0110e08d'.$len.$sendData;
- $sendData .= crc16(pack('H*',$sendData));
- $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampProData['address'],'/IDCOL/CmdOutput/'.$lampProData['address'],pack('H*','0001'.$sendData));
- if (empty($res)) {
- return array('result'=>1,'msg'=>'0016');
- }else{
- if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
- return array('result'=>1,'msg'=>'0000');
- }else{
- return array('result'=>1,'msg'=>'0010');
- }
- }
- }else{
- return array('result'=>1,'msg'=>'0010');
- }
- }
-
- }elseif ($lampProData['protocoltype'] == 6 && $lampProData['lampprotocoltype'] == 1) {
- $cmdLine='0103e08d0027a23b';
- //$cmdLine .= crc16(pack('H*',$cmdLine));
- device_cmd($lampProData['deviceid'],pack('H*',$cmdLine));
- $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
- $res=send_lorawan_915($cmdLine);
- if (empty($res)) {
- return array('result'=>1,'msg'=>'0016');
- }else {
- $res=json_decode($res,true);
- $cmdRes=$res['resInfo'];
- if(strtolower(substr($cmdRes, 0,6)) == '01034e'){
- $old = $cmdRes;
- $data = array();
- $sendData = '';
- $sendData .= doubBitSubStr(base_convert(intval($update['load_current']*100), 10, 16)).''; // 负载电流
- $sendData .= doubBitSubStr(base_convert(intval($update['intell_power']), 10, 16)).''; // 智能功率
- $sendData .= doubBitSubStr(base_convert(intval($update['light_delay']), 10,16)).''; // 光控延时
- $sendData .= doubBitSubStr(base_convert(intval($update['induction_delay']), 10,16)).''; // 感应延时
- $sendData .= substr($old, 22,4);
- $sendData .= doubBitSubStr(base_convert(intval($update['first_light_time']), 10,16)).''; // 第一段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['first_light_human_power']), 10,16)).''; // 第一段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['first_light_unman_power']), 10,16)).''; // 第一段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['second_light_time']), 10,16)).''; // 第二段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['second_light_human_power']), 10,16)).''; // 第二段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['second_light_unman_power']), 10,16)).''; // 第二段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['third_light_time']), 10,16)).''; // 第三段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['third_light_human_power']), 10,16)).''; // 第三段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['third_light_unman_power']), 10,16)).''; // 第三段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_time']), 10,16)).''; // 第四段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_human_power']), 10,16)).''; // 第四段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['fourth_light_unman_power']), 10,16)).''; // 第四段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_time']), 10,16)).''; // 第五段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_human_power']), 10,16)).''; // 第五段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['fifth_light_unman_power']), 10,16)).''; // 第五段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_time']), 10,16)).''; // 第六段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_human_power']), 10,16)).''; // 第六段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['sixth_light_unman_power']), 10,16)).''; // 第六段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_time']), 10,16)).''; // 第七段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_human_power']), 10,16)).''; // 第七段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['seventh_light_unman_power']), 10,16)).''; // 第七段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_time']), 10,16)).''; // 第八段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_human_power']), 10,16)).''; // 第八段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['eighth_light_unman_power']), 10,16)).''; // 第八段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_time']), 10,16)).''; // 第九段亮灯时间
- $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_human_power']), 10,16)).''; // 第九段有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['ninth_light_unman_power']), 10,16)).''; // 第九段无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_time']), 10,16)).''; // 晨亮时间
- $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_human_power']), 10,16)).''; // 晨亮有人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['tenth_light_unman_power']), 10,16)).''; // 晨亮无人功率
- $sendData .= doubBitSubStr(base_convert(intval($update['begin_voltage']*10), 10, 16)).''; // 智能功率USER1起始电压
- $sendData .= doubBitSubStr(base_convert(intval($update['end_voltage']*10), 10, 16)).''; // 智能功率USER1终止电压
- $sendData .= substr($old, 154,4);
- $sendData .= doubBitSubStr(base_convert(intval($update['end_current']*100), 10, 16)).''; // 智能功率USER1终止电流
- $len = strlen($sendData)/4;
- $con1 = substr($sendData,0,20*4);
- $con2 = substr($sendData,20*4);
- $address1='e08d';
- $address2=substr('0000'.base_convert(base_convert($address1,16,10)+20,10,16),-4);
- $l1=substr('0000'.base_convert(20,10,16),-4);
- $l2=substr('00'.base_convert(40,10,16),-2);
- $cmd1='0110'.$address1.$l1.$l2.$con1;
- $cmd1 .=crc16(pack('H*',$cmd1));
- device_cmd($lampProData['deviceid'],pack('H*',$cmd1));
- $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
- $res=send_lorawan_915($cmdLine);
- $file = fopen('./file/loraWan_loader.txt', 'a+');
- fwrite($file, date('Y-m-d H:i:s').'----'.$res.'
- ');
- fwrite($file, date('Y-m-d H:i:s').'----'.$len.'
- ');
- fwrite($file, date('Y-m-d H:i:s').'----'.$sendData.'
- ');
- fwrite($file, date('Y-m-d H:i:s').'----'.$cmd1.'
- ');
- fclose($file);
- if (empty($res)) {
- return array('result'=>1,'msg'=>'0016');
- }else{
- $res = json_decode($res,true);
- if (!isset($res['resInfo']) || strtolower($res['resInfo']) != '0110e08d001467ed') return array('result'=>1,'msg'=>'0016');
- $l1=substr('0000'.base_convert(19,10,16),-4);
- $l2=substr('00'.base_convert(38,10,16),-2);
- $cmd2='0110'.$address2.$l1.$l2.$con2;
- $cmd2 .=crc16(pack('H*',$cmd2));
- device_cmd($lampProData['deviceid'],pack('H*',$cmd2));
- $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
- $res=send_lorawan_915($cmdLine);
- $file = fopen('./file/loraWan_loader.txt', 'a+');
- fwrite($file, date('Y-m-d H:i:s').'----'.$res.'
- ');
- fclose($file);
- if (empty($res)) {
- return array('result'=>1,'msg'=>'0016');
- }else{
- $res = json_decode($res,true);
- if (!isset($res['resInfo']) || strtolower($res['resInfo']) != '0110e0a10013e7e6') return array('result'=>1,'msg'=>'0016');
- return array('result'=>1,'msg'=>'0000');
- }
- }
- // $cmdLine='0110e08d00274e'.$sendData;
- // $cmdLine .= crc16(pack('H*',$cmdLine));
- // device_cmd($lampProData['deviceid'],pack('H*',$cmdLine));
- // $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
- // $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
- // $res=send_lorawan_915($cmdLine);
- // $file = fopen('./file/loraWan_loader.txt', 'a+');
- // fwrite($file, date('Y-m-d H:i:s').'----'.$res.' ');
- // fclose($file);
- // if (empty($res)) {
- // return array('result'=>1,'msg'=>'0016');
- // }else {
- // $file = fopen('./file/loraWan_loader.txt', 'a+');
- // fwrite($file, date('Y-m-d H:i:s').'----'.$res.' ');
- // fclose($file);
- // $res=json_decode($res,true);
- // $cmdRes2=$res['resInfo'];
- // if(strtolower(substr($cmdRes2,0,6))=='01034e') {
- // return array('result'=>1,'msg'=>'0000');
- // }else{
- // return array('result'=>1,'msg'=>'0010');
- // }
- // }
- }else{
- return array('result'=>1,'msg'=>'0010');
- }
- }
- }
- $file = fopen('../upload/test.txt', 'a+');
- fwrite($file, '333333------'.date('Y-m-d H:i:s').'
- ');
- fclose($file);
- if (isset($lampProData['protocoltype']) && $lampProData['protocoltype'] != 0) {
- $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
- }
- }else{
- $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
- }
- }
- $file = fopen('../upload/test.txt', 'a+');
- fwrite($file, '44444------'.date('Y-m-d H:i:s').'
- ');
- fclose($file);
- $res = array();
- if (empty($cmdret)){
- $res['result'] = 1;
- $res['msg'] = '0016';
- return $res;
- }
-
- if($cmdret === false){
- $res['result'] = 1;
- $res['msg'] = '0008';
- } else {
- $res = json_decode($cmdret, true);
- if ($res['result'] == false) {
- $res['result'] = 0;
- $version = $this->input->post('version',true);
- if (empty($version)) {
- $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
- }else{
- $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
- }
- } else {
- $res['result'] = 1;
- $res['msg'] = '0000';
- }
- }
- $file = fopen('../upload/test.txt', 'a+');
- fwrite($file, '55555------'.date('Y-m-d H:i:s').'
- ');
- fclose($file);
- return $res;
- }
- // 更新蓄电池设置
- public function set_battery(){
- $username = $this->input->post('username',true);
- $client_key = $this->input->post('client_key',true);
- $token = $this->input->post('token',true);
- $lamp_id = $this->input->post('lamp_id',true);
- $clientid = $this->input->post('clientid',true);
- $version = $this->session->userdata('version');
- $batterytype = $this->input->post('batterytype',true);//蓄电池类型
- $capacity = $this->input->post('capacity',true);//蓄电池容量
- $vpromotecharge = $this->input->post('vpromotecharge',true);//提升充电电压/过充电压(锂电池)
- $voverdischargerecover = $this->input->post('voverdischargerecover',true);//过放恢复电压
- $vpromoterecover = $this->input->post('vpromoterecover',true);//提升充电恢复电压/过充返回电压(锂电池)
- $voverdischarge = $this->input->post('voverdischarge',true);//过放电压
- if ($batterytype == 1) {
- $vsystem = $this->input->post('vsystem',true);//系统电压
- $minusnocharge = $this->input->post('minusnocharge',true);//零下禁充
- $chargemode = $this->input->post('chargemode',true);//充电模式
- } else {
- $vovervoltage = $this->input->post('vovervoltage',true);
- $vlimitedcharge = $this->input->post('vlimitedcharge',true);
- $vbalancecharge = $this->input->post('vbalancecharge',true);
- $vfloatingcharge = $this->input->post('vfloatingcharge',true);
- $vundervoltagewarn = $this->input->post('vundervoltagewarn',true);
- $balancechargetime = $this->input->post('balancechargetime',true);
- $promotechargetime = $this->input->post('promotechargetime',true);
- $balanceinterval = $this->input->post('balanceinterval',true);
- $tempcompensation = $this->input->post('tempcompensation',true);
- $tempcompmax = $this->input->post('tempcompmax',true);
- $tempcompmin = $this->input->post('tempcompmin',true);
- $turnfloatingcurrent = $this->input->post('turnfloatingcurrent',true);
- }
- $lampArr = explode(',', $lamp_id);
- $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
- $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/set_battery_asy';
- foreach ($lampList as $l) {
- if ($l['protocoltype'] == 0) {
- if ($batterytype == 1) {
- 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);
- }else{
- 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);
- }
- }else{
- if ($batterytype == 1) {
- 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]);
- }else{
- 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]);
- }
- }
-
- }
- exit();
- }
- public function set_battery_asy(){
- $res = $this->battery_update();
- $clientid = $this->input->post('clientid',true);
- $lampid = $this->input->post('lamp_id',true);
- $data = $this->Lamp_model->getData(array('id'=>$lampid),'projectid,networkid,address,number');
- if ($res['msg'] == '0000') {
- $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
- $networkData = $this->Network_model->getData(array('id'=>$data['networkid']),'networkname');
-
- $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯蓄电池参数");
- $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);
- }
- if (empty($res['result'])) {
- // exit(json_result($res['msg'],$res['msg'],array('number'=>$data['number'])));
- $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
- exit(send_websocket($clientid,$data));
- }else{
- // exit(json_result($res['msg'],$this->response[$res['msg']],array('number'=>$data['number'])));
- $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$this->response[$res['msg']],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
- exit(send_websocket($clientid,$data));
- }
- }
- //更新蓄电池设置
- private function battery_update(){
- $where['id'] = $this->input->post('lamp_id',true);//路灯id
- $update['batterytype'] = $this->input->post('batterytype',true);//蓄电池类型
- $update['capacity'] = $this->input->post('capacity',true);//蓄电池容量
- $update['vpromotecharge'] = $this->input->post('vpromotecharge',true);//提升充电电压/过充电压(锂电池)
- $update['voverdischargerecover'] = $this->input->post('voverdischargerecover',true);//过放恢复电压
- $update['vpromoterecover'] = $this->input->post('vpromoterecover',true);//提升充电恢复电压/过充返回电压(锂电池)
- $update['voverdischarge'] = $this->input->post('voverdischarge',true);//过放电压
- $update['updatetime'] = date("Y-m-d H:i:s");//更新时间
- if ($update['batterytype'] == 1) {
- $update['vovervoltage'] = $update['vpromotecharge']+2;//超压电压
- $update['vlimitedcharge'] = $update['vpromotecharge']+1;//充电限制电压
- $update['vbalancecharge'] = $update['vpromotecharge'];//均衡充电电压
- $update['vfloatingcharge'] = $update['vpromoterecover'];//浮充充电电压
- $update['vundervoltagewarn'] = 12;//欠压警告电压
- $update['balancechargetime'] = 2;//均衡充电时间
- $update['promotechargetime'] = 2;//提升充电时间
- $update['balanceinterval'] = 30;//均衡充电间隔
- $update['tempcompensation'] = 4;//温度补偿系数
- $update['tempcompmax'] = 60;//温度补偿最高温度
- $update['tempcompmin'] = 0;//温度补偿最低温度
- $update['turnfloatingcurrent'] = 1;//转浮充的电流
- $update['vsystem'] = $this->input->post('vsystem',true);//系统电压
- $update['minusnocharge'] = $this->input->post('minusnocharge',true);//零下禁充
- $update['chargemode'] = $this->input->post('chargemode',true);//充电模式
- } else {
- $update['vovervoltage'] = $this->input->post('vovervoltage',true);
- $update['vlimitedcharge'] = $this->input->post('vlimitedcharge',true);
- $update['vbalancecharge'] = $this->input->post('vbalancecharge',true);
- $update['vfloatingcharge'] = $this->input->post('vfloatingcharge',true);
- $update['vundervoltagewarn'] = $this->input->post('vundervoltagewarn',true);
- $update['balancechargetime'] = $this->input->post('balancechargetime',true);
- $update['promotechargetime'] = $this->input->post('promotechargetime',true);
- $update['balanceinterval'] = $this->input->post('balanceinterval',true);
- $update['tempcompensation'] = $this->input->post('tempcompensation',true);
- $update['tempcompmax'] = $this->input->post('tempcompmax',true);
- $update['tempcompmin'] = $this->input->post('tempcompmin',true);
- $update['turnfloatingcurrent'] = $this->input->post('turnfloatingcurrent',true);
- $update['vsystem'] = 12;
- $update['minusnocharge'] = 0;
- $update['chargemode'] = 0;
- }
- foreach ($update as $v) {
- if ($v === null || $v === '') {
- $lampData = $this->Lamp_model->getOne($where['id'],'number');
- $data = ['code'=>'0007','status'=>2,'msg'=>$this->response['0007'],'data'=>['number'=>$lampData['number'],'type'=>'set_load','id'=>$where['id']]];
- exit(send_websocket($clientid,$data));
- }
- }
- return $this->battery_set($where, $update, 0);
- }
- private function battery_set($where,$update,$broadcast){
- $ret = $this->Battery_model->getOne($where['id']);
- if(empty($ret)){
- $update['id'] = $where['id'];
- $this->Battery_model->insert($update);
- } else {
- $this->Battery_model->update($where,$update);
- }
-
- $cmd = '{"cmd_type":"battery_cmd","cmd_id":'.$where['id'].',"broadcast":'.$broadcast.'}';
- $lampProData = $this->Lamp_model->getOne($where['id'],'N.protocoltype,L.address,L.protocoltype AS lampprotocoltype,N.deviceid');
- if (($lampProData['protocoltype'] == 4 || $lampProData['protocoltype'] == 6) && $lampProData['lampprotocoltype'] == 0) {
- $address = '';
- if (mb_strlen($lampProData['address']) >= 8) {
- $address = mb_substr($lampProData['address'], -8);
- }else{
- $address = '00000000';
- }
- $sendData = '10032820';
- $sendData .= doubBitSubStr(base_convert($update['capacity'], 10, 16));
- $sendData .= doubBitSubStr(base_convert(intval($update['vovervoltage']*10), 10, 16));
- $sendData .= doubBitSubStr(base_convert(intval($update['vlimitedcharge']*10), 10, 16));
- $sendData .= doubBitSubStr(base_convert(intval($update['vbalancecharge']*10), 10, 16));
- $sendData .= doubBitSubStr(base_convert(intval($update['vpromotecharge']*10), 10, 16));
- $sendData .= doubBitSubStr(base_convert(intval($update['vfloatingcharge']*10), 10, 16));
- $sendData .= doubBitSubStr(base_convert(intval($update['vpromoterecover']*10), 10, 16));
- $sendData .= doubBitSubStr(base_convert(intval($update['voverdischargerecover']*10), 10, 16));
- $sendData .= doubBitSubStr(base_convert(intval($update['vundervoltagewarn']*10), 10, 16));
- $sendData .= doubBitSubStr(base_convert(intval($update['voverdischarge']*10), 10, 16));
- $sendData .= bitSubStr(base_convert($update['balancechargetime'], 10, 16));
- $sendData .= bitSubStr(base_convert($update['promotechargetime'], 10, 16));
- $sendData .= bitSubStr(base_convert($update['balanceinterval'], 10, 16));
- $sendData .= bitSubStr(base_convert($update['tempcompensation'], 10, 16));
- $sendData .= bitEncode($update['tempcompmax']);
- $sendData .= bitEncode($update['tempcompmin']);
- $sendData .= doubBitSubStr(base_convert(intval($update['turnfloatingcurrent']*100), 10, 16));
- $sendData .= bitSubStr(base_convert($update['batterytype'], 10, 16));
- $sendData .= bitSubStr(base_convert($update['vsystem'], 10, 16));
- $sendData .= bitSubStr(base_convert($update['minusnocharge'], 10, 16));
- $sendData .= bitSubStr(base_convert($update['chargemode'], 10, 16));
- $sum = 0;
- for ($i=0; $i < mb_strlen($sendData)/2; $i++) {
- if ($i == 0) continue;
- $t = mb_substr($sendData, $i*2,2);
- $sum += base_convert($t, 16, 10);
- }
- $sum = base_convert($sum, 10, 16);
- $sendData .= bitSubStr($sum);
- $sendData = $address.$sendData;
-
- if ($lampProData['protocoltype'] == 4) {
- $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampProData['address'],'/IDCOL/CmdOutput/'.$lampProData['address'],pack('H*','0001'.$sendData));
- }else{
- device_cmd($lampProData['deviceid'],pack('H*',$sendData));
- $cmd = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
- $info = send_lorawan_915($cmd);
- $res = array();
- if (!empty($info)) {
- $info = json_decode($info,true);
- if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
- }
- }
-
- if (empty($res)) {
- return array('result'=>1,'msg'=>'0016');
- }else{
- $msg1 = $res['msg'];
- // 数据校验
- $msg = mb_substr($msg1, 10);
- $t = 0;
- for ($i=0; $i < mb_strlen($msg) / 2 - 1; $i++) {
- $s = mb_substr($msg, $i*2,2);
- $t += base_convert($s, 16, 10);
- }
- $t = base_convert($t, 10, 16);
- if (mb_strlen($t) > 2) {
- $t = mb_substr($t, -2);
- }elseif($t <= 1){
- $t = '0'.$t;
- }
- if ($t == mb_substr($msg, -2)) {
- if (mb_substr($msg1, -10,-2) == '10032820') {
- return array('result'=>1,'msg'=>'0000');
- }else{
- return array('result'=>1,'msg'=>'0010');
- }
- }else{
- return array('result'=>1,'msg'=>'0010');
- }
- }
- }
- if (isset($lampProData['protocoltype']) && $lampProData['protocoltype'] != 0) {
- $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
- }
- }else{
- $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
- }
- }
- $res = array();
- if (empty($cmdret)){
- $res['result'] = 1;
- $res['msg'] = '0016';
- return $res;
- }
- if($cmdret === false){
- $res['result'] = 1;
- $res['msg'] = '0008';
- } else {
- $res = json_decode($cmdret, true);
- if ($res['result'] == false) {
- $res['result'] = 0;
- $version = $this->input->post('version',true);
- if (empty($version)) {
- $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
- }else{
- $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
- }
- } else {
- $res['result'] = 1;
- $res['msg'] = '0000';
- }
- }
- return $res;
- }
- // 新控制器更新蓄电池设置
- public function modbus_set_battery(){
- $username = $this->input->post('username',true);
- $client_key = $this->input->post('client_key',true);
- $token = $this->input->post('token',true);
- $lamp_id = $this->input->post('lamp_id',true);
- $clientid = $this->input->post('clientid',true);
- $version = $this->session->userdata('version');
- $light_voltage = $this->input->post('light_voltage',true);
- $battery_type = $this->input->post('battery_type',true);
- $li_battery_type = $this->input->post('li_battery_type',true);
- $system_voltage = $this->input->post('system_voltage',true);
- $increa_char_voltage = $this->input->post('increa_char_voltage',true);
- $float_char_voltage = $this->input->post('float_char_voltage',true);
- $over_dischar_return_voltage = $this->input->post('over_dischar_return_voltage',true);
- $over_dischar_voltage = $this->input->post('over_dischar_voltage',true);
- $char_upper_limit_temper = $this->input->post('char_upper_limit_temper',true);
- $char_lower_limit_temper = $this->input->post('char_lower_limit_temper',true);
- $dischar_upper_limit_temper = $this->input->post('dischar_upper_limit_temper',true);
- $dischar_lower_limit_temper = $this->input->post('dischar_lower_limit_temper',true);
- if ($battery_type == 0) {
- $overvoltage = $this->input->post('overvoltage',true);
- $char_limit_voltage = $this->input->post('char_limit_voltage',true);
- $equ_char_voltage = $this->input->post('equ_char_voltage',true);
- $increa_char_return_voltage = $this->input->post('increa_char_return_voltage',true);
- $equ_char_time = $this->input->post('equ_char_time',true) * 60;
- $increa_char_time = $this->input->post('increa_char_time',true) * 60;
- $balan_char_interval = $this->input->post('balan_char_interval',true);
- $temper_compen_coeff = $this->input->post('temper_compen_coeff',true);
- }
- $lampArr = explode(',', $lamp_id);
- $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
- $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/modbus_set_battery_asy';
- foreach ($lampList as $l) {
- if ($l['protocoltype'] == 0) {
- if ($battery_type == 1) {
- 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);
- }else{
- 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);
- }
- }else{
- if ($battery_type == 1) {
- 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]);
- }else{
- 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]);
- }
- }
-
- }
- exit();
- }
- public function modbus_set_battery_asy(){
- $res = $this->modbus_battery_update();
- $clientid = $this->input->post('clientid',true);
- $lampid = $this->input->post('lamp_id',true);
- $data = $this->Lamp_model->getData(array('id'=>$lampid),'projectid,networkid,address,number');
- if ($res['msg'] == '0000') {
- $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
- $networkData = $this->Network_model->getData(array('id'=>$data['networkid']),'networkname');
-
- $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯蓄电池参数");
- $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);
- }
- if (empty($res['result'])) {
- // exit(json_result($res['msg'],$res['msg'],array('number'=>$data['number'])));
- $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
- exit(send_websocket($clientid,$data));
- }else{
- // exit(json_result($res['msg'],$this->response[$res['msg']],array('number'=>$data['number'])));
- $data = ['code'=>$res['msg'],'status'=>2,'msg'=>$this->response[$res['msg']],'data'=>['number'=>$data['number'],'type'=>'set_load','id'=>$lamp_id]];
- exit(send_websocket($clientid,$data));
- }
- }
- //新控制器更新蓄电池设置
- private function modbus_battery_update(){
- $where['id'] = $this->input->post('lamp_id',true);//路灯id
- $update['light_voltage'] = $this->input->post('light_voltage',true);
- $update['battery_type'] = $this->input->post('battery_type',true);
- $update['li_battery_type'] = intval($this->input->post('li_battery_type',true));
- $update['system_voltage'] = $this->input->post('system_voltage',true);
- $update['increa_char_voltage'] = $this->input->post('increa_char_voltage',true);
- $update['float_char_voltage'] = $this->input->post('float_char_voltage',true);
- $update['over_dischar_return_voltage'] = $this->input->post('over_dischar_return_voltage',true);
- $update['over_dischar_voltage'] = $this->input->post('over_dischar_voltage',true);
- $update['char_upper_limit_temper'] = $this->input->post('char_upper_limit_temper',true);
- $update['char_lower_limit_temper'] = $this->input->post('char_lower_limit_temper',true);
- $update['dischar_upper_limit_temper'] = $this->input->post('dischar_upper_limit_temper',true);
- $update['dischar_lower_limit_temper'] = $this->input->post('dischar_lower_limit_temper',true);
- if ($update['battery_type'] == 0) {
- $update['overvoltage'] = $this->input->post('overvoltage',true);
- $update['char_limit_voltage'] = $this->input->post('char_limit_voltage',true);
- $update['equ_char_voltage'] = $this->input->post('equ_char_voltage',true);
- $update['increa_char_return_voltage'] = $this->input->post('increa_char_return_voltage',true);
- $update['equ_char_time'] = $this->input->post('equ_char_time',true);
- $update['increa_char_time'] = $this->input->post('increa_char_time',true);
- $update['balan_char_interval'] = $this->input->post('balan_char_interval',true);
- $update['temper_compen_coeff'] = $this->input->post('temper_compen_coeff',true);
- }
- foreach ($update as $v) {
- if ($v === null || $v === '') {
- $lampData = $this->Lamp_model->getOne($where['id'],'number');
- $data = ['code'=>'0007','status'=>2,'msg'=>$this->response['0007'],'data'=>['number'=>$lampData['number'],'type'=>'set_load','id'=>$where['id']]];
- exit(send_websocket($clientid,$data));
- }
- }
-
- return $this->modbus_battery_set($where, $update, 0);
- }
- private function modbus_battery_set($where,$update,$broadcast){
- $ret = $this->Modbus_battery_model->getOne($where['id']);
- if(empty($ret)){
- $update['id'] = $where['id'];
- $this->Modbus_battery_model->insert($update);
- } else {
- $this->Modbus_battery_model->update($where,$update);
- }
-
- $cmd = '{"cmd_type":"modbus_battery_cmd","cmd_id":'.$where['id'].',"broadcast":'.$broadcast.'}';
- $lampProData = $this->Lamp_model->getOne($where['id'],'N.protocoltype,N.deviceid,L.address,L.protocoltype as lampprotocoltype');
- if ($lampProData['protocoltype'] == 4 && $lampProData['lampprotocoltype'] == 1) {
- $sendData = '0103e0020020d212';
-
- $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampProData['address'],'/IDCOL/CmdOutput/'.$lampProData['address'],pack('H*','0001'.$sendData));
- if (empty($res)) {
- return array('result'=>1,'msg'=>'0016');
- }else{
- if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
- $old = $res['msg'];
- $data = array();
- $sendData = '';
- $sendData .= substr($old, 6,4);
- if ($update['battery_type'] == 0) { // 铅酸电池
- $sendData .= doubBitSubStr(base_convert(intval($update['system_voltage']), 10, 16)).''; // 系统电压
- $sendData .= doubBitSubStr(base_convert(10, 10, 16)).''; // 电池类型
- $sendData .= doubBitSubStr(base_convert(intval($update['overvoltage']*10), 10, 16)).''; // 超压电压
- $sendData .= doubBitSubStr(base_convert(intval($update['char_limit_voltage']*10), 10, 16)).''; // 充电限制电压
- $sendData .= doubBitSubStr(base_convert(intval($update['equ_char_voltage']*10), 10, 16)).''; // 均衡充电电压
- $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10), 10, 16)).''; // 提升充电电压
- $sendData .= doubBitSubStr(base_convert(intval($update['float_char_voltage']*10), 10, 16)).''; // 浮充充电电压
- $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_return_voltage']*10), 10, 16)).''; // 提升充电返回电压
- $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_return_voltage']*10), 10, 16)).''; // 过放返回电压
- $sendData .= substr($old, 46,4);
- $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10), 10, 16)).''; // 过放电压
- $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10 - 5), 10, 16)).''; // 放电限制电压
- $sendData .= substr($old, 58,8);
- $sendData .= doubBitSubStr(base_convert(intval($update['equ_char_time']), 10, 16)).''; // 均衡充电时间
- $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_time']), 10, 16)).''; // 提升充电时间
- $sendData .= doubBitSubStr(base_convert(intval($update['balan_char_interval']), 10, 16)).''; // 均衡充电间隔
- $sendData .= doubBitSubStr(base_convert(intval($update['temper_compen_coeff']), 10, 16)).''; // 温度补偿系数
- $sendData .= substr($old, 82,2);
- $sendData .= bitEncode(intval($update['char_upper_limit_temper'])).''; // 充电上限温度
- $sendData .= substr($old, 86,2);
- $sendData .= bitEncode(intval($update['char_lower_limit_temper'])).''; // 充电下限温度
- $sendData .= substr($old, 90,2);
- $sendData .= bitEncode(intval($update['dischar_upper_limit_temper'])).''; // 放电上限温度
- $sendData .= substr($old, 94,2);
- $sendData .= bitEncode(intval($update['dischar_lower_limit_temper'])).''; // 放电下限温度
- $sendData .= substr($old, 98,24);
- $sendData .= doubBitSubStr(base_convert(intval($update['light_voltage']), 10, 16)).''; // 光控电压
- $sendData .= substr($old, 126,6);
- // 特殊功能系统电压设置
- $str = substr('00000000'.base_convert(substr($old, 132,6), 16, 2), -8);
- if ($update['system_voltage'] == 12) {
- $str = substr($str, 0,3).'00'.substr($str, -3);
- }else{
- $str = substr($str, 0,3).'01'.substr($str, -3);
- }
- // 特殊功能系统电池类型设置
- $str = '0'.substr($str, -7);
- $sendData .= bitSubStr(base_convert($str, 2, 16));
- }else{ // 锂电池
- $sendData .= doubBitSubStr(base_convert(intval($update['system_voltage']), 10, 16)).''; // 系统电压
- $sendData .= doubBitSubStr(base_convert(11, 10, 16)).''; // 电池类型
- $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10 + 2), 10, 16)).''; // 超压电压
- $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10 + 1), 10, 16)).''; // 充电限制电压
- $sendData .= substr($old, 26,4);
- $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10), 10, 16)).''; // 充电电压
- $sendData .= doubBitSubStr(base_convert(intval($update['float_char_voltage']*10), 10, 16)).''; // 充电返回电压
- $sendData .= substr($old, 38,4);
- $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_return_voltage']*10), 10, 16)).''; // 过放返回电压
- $sendData .= substr($old, 46,4);
- $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10), 10, 16)).''; // 过放电压
- $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10 - 5), 10, 16)).''; // 放电限制电压
- $sendData .= substr($old, 58,24);
- $sendData .= substr($old, 82,2);
- $sendData .= bitEncode(intval($update['char_upper_limit_temper'])).''; // 充电上限温度
- $sendData .= substr($old, 86,2);
- $sendData .= bitEncode(intval($update['char_lower_limit_temper'])).''; // 充电下限温度
- $sendData .= substr($old, 90,2);
- $sendData .= bitEncode(intval($update['dischar_upper_limit_temper'])).''; // 放电上限温度
- $sendData .= substr($old, 94,2);
- $sendData .= bitEncode(intval($update['dischar_lower_limit_temper'])).''; // 放电下限温度
- $sendData .= substr($old, 98,24);
- $sendData .= doubBitSubStr(base_convert(intval($update['light_voltage']), 10, 16)).''; // 光控电压
- $sendData .= substr($old, 126,6);
- // 特殊功能系统电压设置
- $str = substr('00000000'.base_convert(substr($old, 132,6), 16, 2), -8);
- if ($update['system_voltage'] == 12) {
- $str = substr($str, 0,3).'00'.substr($str, -3);
- }elseif ($update['system_voltage'] == 24) {
- $str = substr($str, 0,3).'01'.substr($str, -3);
- }elseif ($update['system_voltage'] == 3) {
- $str = substr($str, 0,3).'10'.substr($str, -3);
- }else{
- $str = substr($str, 0,3).'11'.substr($str, -3);
- }
- // 特殊功能系统电池类型设置
- $str = '1'.substr($str, -7);
- $sendData .= bitSubStr(base_convert($str, 2, 16));
- }
- $len = doubBitSubStr(base_convert(intval(mb_strlen($sendData)/4), 10, 16)).bitSubStr(base_convert(intval(mb_strlen($sendData)/2), 10, 16));
- $sendData = '0110e002'.$len.$sendData;
- $sendData .= crc16(pack('H*',$sendData));
- $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampProData['address'],'/IDCOL/CmdOutput/'.$lampProData['address'],pack('H*','0001'.$sendData));
- if (empty($res)) {
- return array('result'=>1,'msg'=>'0016');
- }else{
- if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
- return array('result'=>1,'msg'=>'0000');
- }else{
- return array('result'=>1,'msg'=>'0010');
- }
- }
- }else{
- return array('result'=>1,'msg'=>'0010');
- }
- }
-
- }
- elseif ($lampProData['protocoltype']==6 && $lampProData['lampprotocoltype'] == 1){
- $cmdLine='0103e0020020d212';
- device_cmd($lampProData['deviceid'],pack('H*',$cmdLine));
- $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
- $res=send_lorawan_915($cmdLine);
- if (empty($res)) {
- return array('result'=>1,'msg'=>'0016');
- }else{
- $res=json_decode($res,true);
- $cmdRes=$res['resInfo'];
- if(strtolower(substr($cmdRes,0,6))=='010340'){
- $old = $cmdRes;
- $data = array();
- $sendData = '';
- $sendData .= substr($old, 6,4);
- if ($update['battery_type'] == 0) { // 铅酸电池
- $sendData .= doubBitSubStr(base_convert(intval($update['system_voltage']), 10, 16)).''; // 系统电压
- $sendData .= doubBitSubStr(base_convert(10, 10, 16)).''; // 电池类型
- $sendData .= doubBitSubStr(base_convert(intval($update['overvoltage']*10), 10, 16)).''; // 超压电压
- $sendData .= doubBitSubStr(base_convert(intval($update['char_limit_voltage']*10), 10, 16)).''; // 充电限制电压
- $sendData .= doubBitSubStr(base_convert(intval($update['equ_char_voltage']*10), 10, 16)).''; // 均衡充电电压
- $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10), 10, 16)).''; // 提升充电电压
- $sendData .= doubBitSubStr(base_convert(intval($update['float_char_voltage']*10), 10, 16)).''; // 浮充充电电压
- $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_return_voltage']*10), 10, 16)).''; // 提升充电返回电压
- $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_return_voltage']*10), 10, 16)).''; // 过放返回电压
- $sendData .= substr($old, 46,4);
- $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10), 10, 16)).''; // 过放电压
- $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10 - 5), 10, 16)).''; // 放电限制电压
- $sendData .= substr($old, 58,8);
- $sendData .= doubBitSubStr(base_convert(intval($update['equ_char_time']), 10, 16)).''; // 均衡充电时间
- $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_time']), 10, 16)).''; // 提升充电时间
- $sendData .= doubBitSubStr(base_convert(intval($update['balan_char_interval']), 10, 16)).''; // 均衡充电间隔
- $sendData .= doubBitSubStr(base_convert(intval($update['temper_compen_coeff']), 10, 16)).''; // 温度补偿系数
- $sendData .= substr($old, 82,2);
- $sendData .= bitEncode(intval($update['char_upper_limit_temper'])).''; // 充电上限温度
- $sendData .= substr($old, 86,2);
- $sendData .= bitEncode(intval($update['char_lower_limit_temper'])).''; // 充电下限温度
- $sendData .= substr($old, 90,2);
- $sendData .= bitEncode(intval($update['dischar_upper_limit_temper'])).''; // 放电上限温度
- $sendData .= substr($old, 94,2);
- $sendData .= bitEncode(intval($update['dischar_lower_limit_temper'])).''; // 放电下限温度
- $sendData .= substr($old, 98,24);
- $sendData .= doubBitSubStr(base_convert(intval($update['light_voltage']), 10, 16)).''; // 光控电压
- $sendData .= substr($old, 126,6);
- // 特殊功能系统电压设置
- $str = substr('00000000'.base_convert(substr($old, 132,6), 16, 2), -8);
- if ($update['system_voltage'] == 12) {
- $str = substr($str, 0,3).'00'.substr($str, -3);
- }else{
- $str = substr($str, 0,3).'01'.substr($str, -3);
- }
- // 特殊功能系统电池类型设置
- $str = '0'.substr($str, -7);
- $sendData .= bitSubStr(base_convert($str, 2, 16));
- }else{ // 锂电池
- $sendData .= doubBitSubStr(base_convert(intval($update['system_voltage']), 10, 16)).''; // 系统电压
- $sendData .= doubBitSubStr(base_convert(11, 10, 16)).''; // 电池类型
- $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10 + 2), 10, 16)).''; // 超压电压
- $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10 + 1), 10, 16)).''; // 充电限制电压
- $sendData .= substr($old, 26,4);
- $sendData .= doubBitSubStr(base_convert(intval($update['increa_char_voltage']*10), 10, 16)).''; // 充电电压
- $sendData .= doubBitSubStr(base_convert(intval($update['float_char_voltage']*10), 10, 16)).''; // 充电返回电压
- $sendData .= substr($old, 38,4);
- $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_return_voltage']*10), 10, 16)).''; // 过放返回电压
- $sendData .= substr($old, 46,4);
- $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10), 10, 16)).''; // 过放电压
- $sendData .= doubBitSubStr(base_convert(intval($update['over_dischar_voltage']*10 - 5), 10, 16)).''; // 放电限制电压
- $sendData .= substr($old, 58,24);
- $sendData .= substr($old, 82,2);
- $sendData .= bitEncode(intval($update['char_upper_limit_temper'])).''; // 充电上限温度
- $sendData .= substr($old, 86,2);
- $sendData .= bitEncode(intval($update['char_lower_limit_temper'])).''; // 充电下限温度
- $sendData .= substr($old, 90,2);
- $sendData .= bitEncode(intval($update['dischar_upper_limit_temper'])).''; // 放电上限温度
- $sendData .= substr($old, 94,2);
- $sendData .= bitEncode(intval($update['dischar_lower_limit_temper'])).''; // 放电下限温度
- $sendData .= substr($old, 98,24);
- $sendData .= doubBitSubStr(base_convert(intval($update['light_voltage']), 10, 16)).''; // 光控电压
- $sendData .= substr($old, 126,6);
- // 特殊功能系统电压设置
- $str = substr('00000000'.base_convert(substr($old, 132,6), 16, 2), -8);
- if ($update['system_voltage'] == 12) {
- $str = substr($str, 0,3).'00'.substr($str, -3);
- }elseif ($update['system_voltage'] == 24) {
- $str = substr($str, 0,3).'01'.substr($str, -3);
- }elseif ($update['system_voltage'] == 3) {
- $str = substr($str, 0,3).'10'.substr($str, -3);
- }else{
- $str = substr($str, 0,3).'11'.substr($str, -3);
- }
- // 特殊功能系统电池类型设置
- $str = '1'.substr($str, -7);
- $sendData .= bitSubStr(base_convert($str, 2, 16));
- }
- // $file = fopen('./file/loraWan_battery.txt', 'a+');
- // fwrite($file, date('Y-m-d H:i:s').'----'.$cmdLine.' ');
- $len = strlen($sendData)/4;
- $con1 = substr($sendData, 0, $len/2*4);
- $con2 = substr($sendData, $len/2*4);
- $address1 = 'e002';
- $address2 = substr('0000'.base_convert(base_convert($address1, 16, 10) + $len/2, 10, 16), -4);
- $l1 = substr('0000'.base_convert($len/2, 10, 16), -4);
- $l2 = substr('00'.base_convert($len/2, 10, 16), -2);
- $cmd1 = '0110'.$address1.$l1.$l2.$con1;
- $cmd1 .= crc16(pack('H*',$cmd1));
- device_cmd($lampProData['deviceid'],pack('H*',$cmd1));
- $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
- $res=send_lorawan_915($cmdLine);
- // $cmd2 = '0110'.$address2.$l1.$l2.$con2;
- // $cmd2 .= crc16(pack('H*',$cmd2));
- // $res = device_cmd($lampProData['deviceid'],pack('H*',$cmd2));
- // device_cmd($lampProData['deviceid'],pack('H*',$cmdLine));
-
- // fwrite($file, date('Y-m-d H:i:s').'----battery');
- // fclose($file);
- if (empty($res)) {
- return array('result'=>1,'msg'=>'0016');
- }else{
- $cmd2 = '0110'.$address2.$l1.$l2.$con2;
- $cmd2 .= crc16(pack('H*',$cmd2));
- device_cmd($lampProData['deviceid'],pack('H*',$cmd2));
- $cmdLine = '{"type":"cmd","deviceId":"'.$lampProData['address'].'"}';
- $res=send_lorawan_915($cmdLine);
- if (empty($res)) {
- return array('result'=>1,'msg'=>'0016');
- }else{
- return array('result'=>1,'msg'=>'0000');
- }
- // $file = fopen('./file/loraWan_battery.txt', 'a+');
- // fwrite($file, date('Y-m-d H:i:s').'----'.$res.' ');
- // fclose($file);
- // $res=json_decode($res,true);
- // $cmdRes2=$res['resInfo'];
- // if(strtolower(substr($cmdRes2,0,6))=='010340') {
- // return array('result'=>1,'msg'=>'0000');
- // }else{
- // return array('result'=>1,'msg'=>'0010');
- // }
- }
- }else{
- return array('result' => 1, 'msg' => '0010');
- }
- }
- }
- if (isset($lampProData['protocoltype']) && $lampProData['protocoltype'] != 0) {
- $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,1,30,$lampProData['protocoltype']);
- }
- }else{
- $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,0,30,$lampProData['protocoltype']);
- }
- }
- $res = array();
- if (empty($cmdret)){
- $res['result'] = 1;
- $res['msg'] = '0016';
- return $res;
- }
- if($cmdret === false){
- $res['result'] = 1;
- $res['msg'] = '0008';
- } else {
- $res = json_decode($cmdret, true);
- if ($res['result'] == false) {
- $res['result'] = 0;
- $version = $this->input->post('version',true);
- if (empty($version)) {
- $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
- }else{
- $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
- }
- } else {
- $res['result'] = 1;
- $res['msg'] = '0000';
- }
- }
- return $res;
- }
- // 添加编辑灯控
- public function save() {
- $role = $this->get_user_info('role');
-
- $where['id'] = $this->input->post('lamp_id',true);
- $data['projectid'] = $this->input->post('project_id',true);
- $data['number'] = $this->input->post('lamp_no',true);
- $data['section'] = $this->input->post('section',true);
- $data['address'] = $this->input->post('address',true);
- $data['longitude'] = doubleval($this->input->post('longitude',true));
- $data['latitude'] = doubleval($this->input->post('latitude',true));
-
- if ($data['longitude'] < -180 || $data['longitude'] > 180) exit(json_result('0421',$this->response['0421']));
- if ($data['latitude'] < -90 || $data['latitude'] > 90) exit(json_result('0422',$this->response['0422']));
- $poleheight = $this->input->post('poleheight',true);
- if ($poleheight !== null) {
- $data['poleheight'] = $poleheight;
- }
- $data["polediameter"] = $this->input->post('polediameter',true);
- $data["polematerial"] = $this->input->post('polematerial',true);
- $data["lighttype"] = $this->input->post('lighttype',true);
- $data["lamptype"] = $this->input->post('lamptype',true);
- $data["boardtype"] = $this->input->post('boardtype',true);
- $data["batterytype"] = $this->input->post('batterytype',true);
- $data["boardpower"] = $this->input->post('boardpower',true);
- $data["batteryah"] = $this->input->post('batteryah',true);
- $wattage = $this->input->post('wattage',true);
- $data["wattage"] = !empty($wattage) ? $wattage : 0;
- $supplier = $this->input->post('supplier',true);
- $data["supplier"] = !empty($supplier) ? $supplier : '';
- $upazilla = $this->input->post('upazilla',true);
- $data["upazilla"] = !empty($upazilla) ? $upazilla : '';
- $po = $this->input->post('po',true);
- $data["po"] = !empty($po) ? $po : '';
- $manu = $this->input->post('manu',true);
- $data["manu"] = !empty($manu) ? $manu : '';
- $protocoltype = $this->input->post('protocoltype',true);
- $data['protocoltype'] = $protocoltype;
- $loadtype = $this->input->post('loadtype',true);
- if ($loadtype !== null) {
- $data["loadtype"] = intval($loadtype);
- }
- // 控制器协议类型
- $version = $this->session->userdata('version');
- if (strlen($data['number']) > 6) exit(json_result('0408', $this->response['0408'], array()));
- if (empty($data['projectid'])) exit(json_result('0308', $this->response['0308'], array()));
- if (empty($data['number'])) exit(json_result('0409', $this->response['0409'], array()));
- if (!is_numeric($data['number']) || $data['number'] < 1 || $data['number'] > 999999) exit(json_result('0410', $this->response['0410'], array()));
- if (empty($data['address'])) exit(json_result('0805', $this->response['0805'], array()));
- if (is_float($data['address'])) {
- $data['address'] = strval(intval(ceil($data['address'])));
- }
- $is_true = preg_match('/^[A-Fa-f0-9]+$/', $data['address']);
- if (empty($is_true)) exit(json_result('0414', $this->response['0414'], array()));
- $data = remove_null_params($data);
- if (empty($where['id'])) {
- if ($this->Lamp_model->getDataCount(array('number'=>$data['number'], 'projectid'=>$data['projectid'])) > 0) {
- exit(json_result('0401', $this->response['0401'], array()));
- }
- $lamp = $this->Lamp_model->get_one(array('address'=>$data['address']),'projectid,number');
- if (!empty($lamp)) {
- $projectid = $lamp['projectid'];
- $number = $lamp['number'];
- $projectname = $this->Project_model->getNameById($projectid);
- if (empty($version)) {
- exit(json_result('0404', "该无线模块地址已被【{$projectname}】项目,【{$number}】编号的灯控使用", array()));
- }else{
- exit(json_result('other', "The wireless module address has been used by \"{$projectname}\" project, and \"{$number}\" serial number", array()));
- }
- }
- $projectData = $this->Project_model->get_one(['id'=>$data['projectid']],'patrolinterval,cityid,projectname');
- if (empty($data['longitude']) && empty($data['latitude']) && !empty($projectData['cityid'])) {
- $globalData = $this->Global_location_model->getOne($projectData['cityid'],'longitude,latitude');
- $data['longitude'] = $globalData['longitude'];
- $data['latitude'] = $globalData['latitude'];
- }
- $data['createtime'] = date('Y-m-d H:i:s',time());
- $lampid = $this->Lamp_model->add($data);
- $this->Project_model->add_lamp_count(array('lampcount'=>1,'faultcount'=>0,'projectid'=>$data['projectid']));
- $lampId = intval($lampid);
- $this->add_operation_log('insert',"在\"{$projectData['projectname']}\"项目里添加编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯",0);
- $this->add_operation_log('insert',"Add lamp:\"{$data['number']}\".Wireless module address:\"{$data['address']}\".Project name:\"{$projectData['projectname']}\"",0,1);
- } else {
- if ($this->Lamp_model->getDataCount(array('number'=>$data['number'], 'projectid'=>$data['projectid']), $where['id']) > 0) {
- exit(json_result('0401', $this->response['0401'], array()));
- }
- $lamp = $this->Lamp_model->get_one(array('address'=>$data['address'], 'id !='=>$where['id']),'projectid,number');
- if (!empty($lamp)) {
- $projectid = $lamp['projectid'];
- $number = $lamp['number'];
- $projectname = $this->Project_model->getNameById($projectid);
- if (empty($version)) {
- exit(json_result('0404', "该无线模块地址已被【{$projectname}】项目,【{$number}】编号的灯控使用", array()));
- }else{
- exit(json_result('other', "The wireless module address has been used by \"{$projectname}\" project, and \"{$number}\" serial number", array()));
- }
- }
- $old = $this->Lamp_model->get_one($where,'isfaulted,projectid,address,protocoltype');
- if ($old['projectid'] != $data['projectid']) {
- $new['projectid'] = $data['projectid'];
- $new['faultcount'] = $old['isfaulted'] > 0 ? 1 : 0;
- $this->Project_model->add_lamp_count($new);
- $old['faultcount'] = $new['faultcount'];
- $this->Project_model->minus_lamp_count($old);
- }
- // $oldNet = $this->Network_model->getData(array('id'=>$old['networkid']));
- if ($old['address'] != $data['address'] || $protocoltype != $old['protocoltype']) {
- }
- $this->Lamp_model->update($data,$where);
- $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
- $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯信息");
- $this->add_operation_log('update',"Update lamp:\"{$data['number']}\".Wireless module address:\"{$data['address']}\".Project name:\"{$projectData['projectname']}\".",0,1);
- //更新负载设置和蓄电池设置
- if (isset($_POST['workmode'])) {
- $res = $this->load_update();
- if ($res['result'] == 1) {
- $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯负载参数");
- $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);
- }else{
- exit(json_result($res['msg'],$res['msg'],array()));
- }
- }
- if (isset($_POST['capacity'])) {
- $res = $this->battery_update();
- if ($res['result'] == 1) {
- $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络里的编号为\"{$data['number']}\"、无线模块地址为\"{$data['address']}\"的路灯蓄电池参数");
- $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);
- }else{
- exit(json_result($res['msg'],$res['msg'],array()));
- }
- }
- $lampId = intval($where['id']);
- }
- exit(json_result('0000', $this->response['0000'], array("id" => $lampId)));
- }
- // 获取负载参数设置
- public function load_setting() {
- $lamp_id = $this->input->post('lamp_id', true);
- if (empty($lamp_id)) {
- exit(json_result('0400', $this->response['0400'], array()));
- }
- $lampData = $this->Lamp_model->getOne($lamp_id,'N.status,N.protocoltype,L.protocoltype as lampprotocoltype,L.address,L.number,N.deviceid');
- $cmd = '{"cmd_type":"get_load_setting_cmd","cmd_id":'.$lamp_id.',"broadcast":0}';
-
- if (($lampData['protocoltype'] == 4 || $lampData['protocoltype'] == 6) && $lampData['lampprotocoltype'] == 0) {
- $address = '';
- if (mb_strlen($lampData['address']) >= 8) {
- $address = mb_substr($lampData['address'], -8);
- }else{
- $address = '00000000';
- }
- $sendData = $address.'0304001014';
-
- if ($lampData['protocoltype'] == 4) {
- $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*','0001'.$sendData));
- }else{
- device_cmd($lampData['deviceid'],pack('H*',$sendData));
- $cmd = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
- $info = send_lorawan_915($cmd);
- $res = array();
- if (!empty($info)) {
- $info = json_decode($info,true);
- if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
- }
- }
-
- if (empty($res)) {
- exit(json_result('0016', $this->response['0016']));
- }else{
- $msg1 = $res['msg'];
- // 数据校验
- $msg = mb_substr($msg1, 10);
- $t = 0;
- for ($i=0; $i < mb_strlen($msg) / 2 - 1; $i++) {
- $s = mb_substr($msg, $i*2,2);
- $t += base_convert($s, 16, 10);
- }
- $t = base_convert($t, 10, 16);
- if (mb_strlen($t) > 2) {
- $t = mb_substr($t, -2);
- }elseif($t <= 1){
- $t = '0'.$t;
- }
- if ($t == mb_substr($msg, -2)) {
- $data = array();
- $dataStr = mb_substr($msg1, 16);
- $data['workmode'] = base_convert(mb_substr($dataStr, 0,2), 16, 10);
- $data['worktimefirst'] = base_convert(mb_substr($dataStr, 2,2), 16, 10);
- $data['workpowerfirst'] = base_convert(mb_substr($dataStr, 4,2), 16, 10);
- $data['worktimesencond'] = base_convert(mb_substr($dataStr, 6,2), 16, 10);
- $data['workpowersencond'] = base_convert(mb_substr($dataStr, 8,2), 16, 10);
- $data['worktimethird'] = base_convert(mb_substr($dataStr, 10,2), 16, 10);
- $data['workpowerthird'] = base_convert(mb_substr($dataStr, 12,2), 16, 10);
- $data['worktimeforth'] = base_convert(mb_substr($dataStr, 14,2), 16, 10);
- $data['workpowerforth'] = base_convert(mb_substr($dataStr, 16,2), 16, 10);
- $data['delaytime'] = base_convert(mb_substr($dataStr, 18,2), 16, 10);
- $data['voptically'] = round(base_convert(mb_substr($dataStr, 20,4), 16, 10)/10,1);
- $data['ledloadcurrent'] = round(base_convert(mb_substr($dataStr, 24,4), 16, 10),2);
- $data['powercmd'] = base_convert(mb_substr($dataStr, 28,2), 16, 10);
- $data['switchfeature'] = base_convert(mb_substr($dataStr, 30,2), 16, 10);
- exit(json_result('0000', $this->response['0000'], $data));
- }else{
- exit(json_result('0010', $this->response['0010']));
- }
- }
-
- }
- if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
- $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
- }
- }else{
- $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
- }
- }
- if (empty($cmdret)) exit(json_result('0016',$this->response['0016']));
- $r = json_decode($cmdret, true);
- if ($r['result'] == false) {
- $version = $this->session->userdata('version');
- if (empty($version)) {
- $msg = empty($r['msg']) ? '未知错误' : transfer_error_tips($r['msg']);
- }else{
- $msg = empty($r['msg']) ? 'Unknown error' : $r['msg'];
- }
- exit(json_result('other', $msg));
- }
- $res = $this->Load_model->get_data_by_filter(['relateid'=>$lamp_id,'cmdtype'=>0]);
- if (empty($res)) {
- $update['workmode'] = '0';
- $update['worktimefirst'] = '4';
- $update['workpowerfirst'] = '80';
- $update['worktimesencond'] = '1';
- $update['workpowersencond'] = '40';
- $update['worktimethird'] = '8';
- $update['workpowerthird'] = '30';
- $update['worktimeforth'] = '0';
- $update['workpowerforth'] = '30';
- $update['voptically'] = '5';
- $update['delaytime'] = '1';
- $update['ledloadcurrent'] = '330';
- $update['powercmd'] = '1';
- $update['switchfeature'] = '1';
- $res = $update;
- }
- unset($res['id']);
- unset($res['updatetime']);
- $data = $res;
-
- exit(json_result('0000', $this->response['0000'], $data));
- }
- // 新控制器获取参数设置
- public function modbus_get_load() {
- $lamp_id = $this->input->post('lamp_id', true);
- if (empty($lamp_id)) {
- exit(json_result('0400', $this->response['0400'], array()));
- }
- //
- $lampData = $this->Lamp_model->getOne($lamp_id,'N.status,N.deviceid,N.protocoltype,L.address,L.protocoltype as lampprotocoltype,L.number');
- $cmd = '{"cmd_type":"get_load_setting_cmd","cmd_id":'.$lamp_id.',"broadcast":0}';
- if ($lampData['protocoltype'] == 4 && $lampData['lampprotocoltype'] == 1) {
- $sendData = '0103e08d0027a23b';
-
- $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*','0001'.$sendData));
- if (empty($res)) {
- exit(json_result('0016', $this->response['0016']));
- }else{
- if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
- $data = array();
- $data['load_current'] = number_format(base_convert(substr($res['msg'], 6,4), 16, 10)/100,2).''; // 负载电流
- $data['intell_power'] = base_convert(substr($res['msg'], 10,4), 16, 10).''; // 智能功率
- $data['light_delay'] = base_convert(substr($res['msg'], 14,4), 16, 10).''; // 光控延时
- $data['induction_delay'] = base_convert(substr($res['msg'], 18,4), 16, 10).''; // 感应延时
- $data['first_light_time'] = base_convert(substr($res['msg'], 26,4), 16, 10).''; // 第一段亮灯时间
- $data['first_light_human_power'] = base_convert(substr($res['msg'], 30,4), 16, 10).''; // 第一段有人功率
- $data['first_light_unman_power'] = base_convert(substr($res['msg'], 34,4), 16, 10).''; // 第一段无人功率
- $data['second_light_time'] = base_convert(substr($res['msg'], 38,4), 16, 10).''; // 第二段亮灯时间
- $data['second_light_human_power'] = base_convert(substr($res['msg'], 42,4), 16, 10).''; // 第二段有人功率
- $data['second_light_unman_power'] = base_convert(substr($res['msg'], 46,4), 16, 10).''; // 第二段无人功率
- $data['third_light_time'] = base_convert(substr($res['msg'], 50,4), 16, 10).''; // 第三段亮灯时间
- $data['third_light_human_power'] = base_convert(substr($res['msg'], 54,4), 16, 10).''; // 第三段有人功率
- $data['third_light_unman_power'] = base_convert(substr($res['msg'], 58,4), 16, 10).''; // 第三段无人功率
- $data['fourth_light_time'] = base_convert(substr($res['msg'], 62,4), 16, 10).''; // 第四段亮灯时间
- $data['fourth_light_human_power'] = base_convert(substr($res['msg'], 66,4), 16, 10).''; // 第四段有人功率
- $data['fourth_light_unman_power'] = base_convert(substr($res['msg'], 70,4), 16, 10).''; // 第四段无人功率
- $data['fifth_light_time'] = base_convert(substr($res['msg'], 74,4), 16, 10).''; // 第五段亮灯时间
- $data['fifth_light_human_power'] = base_convert(substr($res['msg'], 78,4), 16, 10).''; // 第五段有人功率
- $data['fifth_light_unman_power'] = base_convert(substr($res['msg'], 82,4), 16, 10).''; // 第五段无人功率
- $data['sixth_light_time'] = base_convert(substr($res['msg'], 86,4), 16, 10).''; // 第六段亮灯时间
- $data['sixth_light_human_power'] = base_convert(substr($res['msg'], 90,4), 16, 10).''; // 第六段有人功率
- $data['sixth_light_unman_power'] = base_convert(substr($res['msg'], 94,4), 16, 10).''; // 第六段无人功率
- $data['seventh_light_time'] = base_convert(substr($res['msg'], 98,4), 16, 10).''; // 第七段亮灯时间
- $data['seventh_light_human_power'] = base_convert(substr($res['msg'], 102,4), 16, 10).''; // 第七段有人功率
- $data['seventh_light_unman_power'] = base_convert(substr($res['msg'], 106,4), 16, 10).''; // 第七段无人功率
- $data['eighth_light_time'] = base_convert(substr($res['msg'], 110,4), 16, 10).''; // 第八段亮灯时间
- $data['eighth_light_human_power'] = base_convert(substr($res['msg'], 114,4), 16, 10).''; // 第八段有人功率
- $data['eighth_light_unman_power'] = base_convert(substr($res['msg'], 118,4), 16, 10).''; // 第八段无人功率
- $data['ninth_light_time'] = base_convert(substr($res['msg'], 122,4), 16, 10).''; // 第九段亮灯时间
- $data['ninth_light_human_power'] = base_convert(substr($res['msg'], 126,4), 16, 10).''; // 第九段有人功率
- $data['ninth_light_unman_power'] = base_convert(substr($res['msg'], 130,4), 16, 10).''; // 第九段无人功率
- $data['tenth_light_time'] = base_convert(substr($res['msg'], 134,4), 16, 10).''; // 晨亮时间
- $data['tenth_light_human_power'] = base_convert(substr($res['msg'], 138,4), 16, 10).''; // 晨亮有人功率
- $data['tenth_light_unman_power'] = base_convert(substr($res['msg'], 142,4), 16, 10).''; // 晨亮无人功率
- $data['begin_voltage'] = round(base_convert(substr($res['msg'], 146,4), 16, 10)/10,1).''; // 智能功率USER1起始电压
- $data['end_voltage'] = round(base_convert(substr($res['msg'], 150,4), 16, 10)/10,1).''; // 智能功率USER1终止电压
- $data['end_current'] = round(base_convert(substr($res['msg'], 158,4), 16, 10)/100,2).''; // 智能功率USER1终止电流
- $res = $data;
- }else{
- exit(json_result('0010', $this->response['0010']));
- }
- }
-
- }
- elseif($lampData['protocoltype'] == 6 && $lampData['lampprotocoltype'] == 1){
- //$sendData = '0103e08d0027a23ba5';
- $cmdLine='0103e08d0027a23b';
- //$cmdLine .= crc16(pack('H*',$cmdLine));
- device_cmd($lampData['deviceid'],pack('H*',$cmdLine));
- $cmdLine = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
- $res=send_lorawan_915($cmdLine);
- if($res){
- $res=json_decode($res,true);
- if($res['info']=='success'){
- $res = $this->Modbus_load_model->get_data_by_filter(['relateid'=>$lamp_id,'cmdtype'=>0]);
- }else{
- exit(json_result('0010', $this->response['0010']));
- }
- }else{
- exit(json_result('0010', $this->response['0010']));
- }
- }
- else{
- $cmdret = '';
- if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
- $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
- }
- }else{
- $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
- }
- }
- if (empty($cmdret)) exit(json_result('0016',$this->response['0016']));
- $r = json_decode($cmdret, true);
- if ($r['result'] == false) {
- $version = $this->session->userdata('version');
- if (empty($version)) {
- $msg = empty($r['msg']) ? '未知错误' : transfer_error_tips($r['msg']);
- }else{
- $msg = empty($r['msg']) ? 'Unknown error' : $r['msg'];
- }
- exit(json_result('other', $msg));
- }
- $res = $this->Modbus_load_model->get_data_by_filter(['relateid'=>$lamp_id,'cmdtype'=>0]);
- if (empty($res)) {
- exit(json_result('0417', $this->response['0417']));
- }
- }
-
- $res['load_current'] = number_format($res['load_current'],2);
- $res['end_current'] = number_format($res['end_current'],2);
- $res['end_voltage'] = number_format($res['end_voltage'],1);
- $res['begin_voltage'] = number_format($res['begin_voltage'],1);
- $res['first_light_time'] = $this->format_date_en($res['first_light_time']);
- $res['second_light_time'] = $this->format_date_en($res['second_light_time']);
- $res['third_light_time'] = $this->format_date_en($res['third_light_time']);
- $res['fourth_light_time'] = $this->format_date_en($res['fourth_light_time']);
- $res['fifth_light_time'] = $this->format_date_en($res['fifth_light_time']);
- $res['sixth_light_time'] = $this->format_date_en($res['sixth_light_time']);
- $res['seventh_light_time'] = $this->format_date_en($res['seventh_light_time']);
- $res['eighth_light_time'] = $this->format_date_en($res['eighth_light_time']);
- $res['ninth_light_time'] = $this->format_date_en($res['ninth_light_time']);
- $res['tenth_light_time'] = $this->format_date_en($res['tenth_light_time']);
- unset($res['id']);
- unset($res['updatetime']);
- $data = $res;
-
- exit(json_result('0000', $this->response['0000'], $data));
- }
- private function format_date_en($date){
- $h = floor($date/3600);
- $h = $h > 10 ? $h : '0'.$h;
- $t = floor($date%3600/60);
- $t = $t > 10 ? $t : '0'.$t;
- return $h.':'.$t;
- }
- // 获取默认参数值
- public function get_data(){
- $type = $this->input->post('type',true);
- $type = empty($type) ? 1 : $type;
- $update = array();
- if ($type == 1) { // 负载参数
- $update['workmode'] = '0';
- $update['worktimefirst'] = '4';
- $update['workpowerfirst'] = '80';
- $update['worktimesencond'] = '1';
- $update['workpowersencond'] = '40';
- $update['worktimethird'] = '8';
- $update['workpowerthird'] = '30';
- $update['worktimeforth'] = '0';
- $update['workpowerforth'] = '30';
- $update['voptically'] = '5';
- $update['delaytime'] = '1';
- $update['ledloadcurrent'] = '330';
- $update['powercmd'] = '1';
- $update['switchfeature'] = '1';
- }else{ // 蓄电池参数
- $update['batterytype'] = '0';//蓄电池类型
- $update['capacity0'] = '200';//蓄电池容量
- $update['capacity1'] = '';//蓄电池容量
- $update['vovervoltage'] = '16';//超压电压
- $update['vlimitedcharge'] = '15.5';//充电限制电压
- $update['vbalancecharge'] = '14.6';//均衡充电电压
- $update['vpromotecharge0'] = '14.4';//提升充电电压/过充电压(锂电池)
- $update['vpromotecharge1'] = '12.6';//提升充电电压/过充电压(锂电池)
- $update['vfloatingcharge'] = '13.8';//浮充充电电压
- $update['vpromoterecover0'] = '13.2';//提升充电恢复电压/过充返回电压(锂电池)
- $update['vpromoterecover1'] = '12';//提升充电恢复电压/过充返回电压(锂电池)
- $update['voverdischargerecover0'] = '12.6';//过放恢复电压
- $update['voverdischargerecover1'] = '11';//过放恢复电压
- $update['vundervoltagewarn'] = '12';//欠压警告电压
- $update['voverdischarge0'] = '11';//过放电压
- $update['voverdischarge1'] = '9';//过放电压
- $update['balancechargetime'] = '1';//均衡充电时间
- $update['promotechargetime'] = '4';//提升充电时间
- $update['balanceinterval'] = '30';//均衡充电间隔
- $update['tempcompensation'] = '-4';//温度补偿系数
- $update['tempcompmax'] = '99';//温度补偿最高温度
- $update['tempcompmin'] = '-40';//温度补偿最低温度
- $update['turnfloatingcurrent'] = '20';//转浮充的电流
- $update['vsystem'] = '12';//系统电压
- $update['minusnocharge'] = '0';//零下禁充
- $update['chargemode'] = '0';//充电模式
- }
- exit(json_result('0000',$this->response['0000'],$update));
- }
- // 新控制器获取默认参数值
- public function modbus_get_data(){
- $type = $this->input->post('type',true);
- $type = empty($type) ? 1 : $type;
- $update = array();
- if ($type == 1) { // 负载参数
- $update['intell_power'] = '2';
- $update['load_current'] = '0.9';
- $update['light_delay'] = '20';
- $update['induction_delay'] = '20';
- $update['first_light_time'] = '00:30';
- $update['first_light_human_power'] = '50';
- $update['first_light_unman_power'] = '20';
- $update['second_light_time'] = '00:30';
- $update['second_light_human_power'] = '70';
- $update['second_light_unman_power'] = '30';
- $update['third_light_time'] = '02:00';
- $update['third_light_human_power'] = '100';
- $update['third_light_unman_power'] = '50';
- $update['fourth_light_time'] = '00:30';
- $update['fourth_light_human_power'] = '70';
- $update['fourth_light_unman_power'] = '30';
- $update['fifth_light_time'] = '00:30';
- $update['fifth_light_human_power'] = '70';
- $update['fifth_light_unman_power'] = '30';
- $update['sixth_light_time'] = '04:00';
- $update['sixth_light_human_power'] = '30';
- $update['sixth_light_unman_power'] = '10';
- $update['seventh_light_time'] = '00:00';
- $update['seventh_light_human_power'] = '100';
- $update['seventh_light_unman_power'] = '50';
- $update['eighth_light_time'] = '00:00';
- $update['eighth_light_human_power'] = '100';
- $update['eighth_light_unman_power'] = '50';
- $update['ninth_light_time'] = '00:00';
- $update['ninth_light_human_power'] = '100';
- $update['ninth_light_unman_power'] = '50';
- $update['tenth_light_time'] = '00:00';
- $update['tenth_light_human_power'] = '50';
- $update['tenth_light_unman_power'] = '20';
- $update['begin_voltage'] = '11.5';
- $update['end_voltage'] = '10.0';
- $update['end_current'] = '0.15';
- }else{ // 蓄电池参数
- $update['light_voltage'] = '5';
- $update['system_voltage'] = '12';
- $update['battery_type'] = '0';
- $update['li_battery_type'] = '0';
- $update['overvoltage'] = '15.5';
- $update['char_limit_voltage'] = '15.0';
- $update['equ_char_voltage'] = '14.6';
- $update['increa_char_voltage'] = '14.4';
- $update['float_char_voltage'] = '13.8';
- $update['increa_char_return_voltage'] = '13.2';
- $update['over_dischar_return_voltage'] = '12.6';
- $update['over_dischar_voltage'] = '11.1';
- $update['equ_char_time'] = '120';
- $update['increa_char_time'] = '120';
- $update['balan_char_interval'] = '30';
- $update['temper_compen_coeff'] = '5';
- $update['char_upper_limit_temper'] = '60';
- $update['char_lower_limit_temper'] = '-30';
- $update['dischar_upper_limit_temper'] = '60';
- $update['dischar_lower_limit_temper'] = '-30';
- }
- exit(json_result('0000',$this->response['0000'],$update));
- }
- // 获取蓄电池设置
- public function battery_setting() {
- $lamp_id = $this->input->post('lamp_id', true);
- if (empty($lamp_id)) {
- exit(json_result('0400', $this->response['0400'], array()));
- }
- $lampData = $this->Lamp_model->getOne($lamp_id,'N.status,N.protocoltype,L.address,L.protocoltype as lampprotocoltype,L.number,N.deviceid');
- // if ($lampData['status'] == 0) {
- // exit(json_result('0412',$this->response['0412'],array()));
- // }
- $cmd = '{"cmd_type":"get_battery_setting_cmd","cmd_id":'.$lamp_id.',"broadcast":0}';
- if (($lampData['protocoltype'] == 4 || $lampData['protocoltype'] == 6) && $lampData['lampprotocoltype'] == 0) {
- $address = '';
- if (mb_strlen($lampData['address']) >= 8) {
- $address = mb_substr($lampData['address'], -8);
- }else{
- $address = '00000000';
- }
- $sendData = $address.'030328204b';
-
- if ($lampData['protocoltype'] == 4) {
- $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*','0001'.$sendData));
- }else{
- device_cmd($lampData['deviceid'],pack('H*',$sendData));
- $cmd = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
- $info = send_lorawan_915($cmd);
- $res = array();
- if (!empty($info)) {
- $info = json_decode($info,true);
- if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
- }
- }
- if (empty($res)) {
- exit(json_result('0016', $this->response['0016']));
- }else{
- $msg1 = $res['msg'];
- // 数据校验
- $msg = mb_substr($msg1, 10);
- $t = 0;
- for ($i=0; $i < mb_strlen($msg) / 2 - 1; $i++) {
- $s = mb_substr($msg, $i*2,2);
- $t += base_convert($s, 16, 10);
- }
- $t = base_convert($t, 10, 16);
- if (mb_strlen($t) > 2) {
- $t = mb_substr($t, -2);
- }elseif($t <= 1){
- $t = '0'.$t;
- }
- if ($t == mb_substr($msg, -2)) {
- $data = array();
- $dataStr = mb_substr($msg1, 16);
- $data['batterytype'] = base_convert(mb_substr($dataStr, 56,2), 16, 10);//蓄电池类型
- $data['capacity'] = base_convert(mb_substr($dataStr, 0,4), 16, 10);//蓄电池容量
- $data['capacity1'] = base_convert(mb_substr($dataStr, 0,4), 16, 10);//蓄电池容量
- $data['vovervoltage'] = round(base_convert(mb_substr($dataStr, 4,4), 16, 10)/10,1);//超压电压
- $data['vlimitedcharge'] = round(base_convert(mb_substr($dataStr, 8,4), 16, 10)/10,1);//充电限制电压
- $data['vbalancecharge'] = round(base_convert(mb_substr($dataStr, 12,4), 16, 10)/10,1);//均衡充电电压
- $data['vpromotecharge0'] = round(base_convert(mb_substr($dataStr, 16,4), 16, 10)/10,1);//提升充电电压/过充电压(锂电池)
- $data['vpromotecharge1'] = round(base_convert(mb_substr($dataStr, 16,4), 16, 10)/10,1);//提升充电电压/过充电压(锂电池)
- $data['vfloatingcharge'] = round(base_convert(mb_substr($dataStr, 20,4), 16, 10)/10,1);//浮充充电电压
- $data['vpromoterecover0'] = round(base_convert(mb_substr($dataStr, 24,4), 16, 10)/10,1);//提升充电恢复电压/过充返回电压(锂电池)
- $data['vpromoterecover1'] = round(base_convert(mb_substr($dataStr, 24,4), 16, 10)/10,1);//提升充电恢复电压/过充返回电压(锂电池)
- $data['voverdischargerecover0'] = round(base_convert(mb_substr($dataStr, 28,4), 16, 10)/10,1);//过放恢复电压
- $data['voverdischargerecover1'] = round(base_convert(mb_substr($dataStr, 28,4), 16, 10)/10,1);//过放恢复电压
- $data['vundervoltagewarn'] = round(base_convert(mb_substr($dataStr, 32,4), 16, 10)/10,1);//欠压警告电压
- $data['voverdischarge0'] = round(base_convert(mb_substr($dataStr, 36,4), 16, 10)/10,1);//过放电压
- $data['voverdischarge1'] = round(base_convert(mb_substr($dataStr, 36,4), 16, 10)/10,1);//过放电压
- $data['balancechargetime'] = base_convert(mb_substr($dataStr, 40,2), 16, 10);//均衡充电时间
- $data['promotechargetime'] = base_convert(mb_substr($dataStr, 42,2), 16, 10);//提升充电时间
- $data['balanceinterval'] = base_convert(mb_substr($dataStr, 44,2), 16, 10);//均衡充电间隔
- $data['tempcompensation'] = base_convert(mb_substr($dataStr, 46,2), 16, 10);//温度补偿系数
- $data['tempcompmax'] = bitDecode(mb_substr($dataStr, 48,2));//温度补偿最高温度
- $data['tempcompmin'] = bitDecode(mb_substr($dataStr, 50,2));//温度补偿最低温度
- $data['turnfloatingcurrent'] = round(base_convert(mb_substr($dataStr, 52,4), 16, 10)/100,2);//转浮充的电流
- $data['vsystem'] = base_convert(mb_substr($dataStr, 58,2), 16, 10);//系统电压
- $data['minusnocharge'] = base_convert(mb_substr($dataStr, 60,2), 16, 10);//零下禁充
- $data['chargemode'] = base_convert(mb_substr($dataStr, 62,2), 16, 10);//充电模式
- exit(json_result('0000', $this->response['0000'], $data));
- }else{
- exit(json_result('0010', $this->response['0010']));
- }
- }
-
- }
- if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
- $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
- }
- }else{
- $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
- }
- }
- if (empty($cmdret)) exit(json_result('0016',$this->response['0016']));
- $r = json_decode($cmdret, true);
- if ($r['result'] == false) {
- $version = $this->session->userdata('version');
- if (empty($version)) {
- $msg = empty($r['msg']) ? '未知错误' : transfer_error_tips($r['msg']);
- }else{
- $msg = empty($r['msg']) ? 'Unknown error' : $r['msg'];
- }
- exit(json_result('other', $msg));
- }
- $res = $this->Battery_model->getOne($lamp_id);
- if (empty($res)) {
- $update['batterytype'] = '0';//蓄电池类型
- $update['capacity0'] = '200';//蓄电池容量
- $update['capacity1'] = '';//蓄电池容量
- $update['vovervoltage'] = '16';//超压电压
- $update['vlimitedcharge'] = '15.5';//充电限制电压
- $update['vbalancecharge'] = '14.6';//均衡充电电压
- $update['vpromotecharge0'] = '14.4';//提升充电电压/过充电压(锂电池)
- $update['vpromotecharge1'] = '12.6';//提升充电电压/过充电压(锂电池)
- $update['vfloatingcharge'] = '13.8';//浮充充电电压
- $update['vpromoterecover0'] = '13.2';//提升充电恢复电压/过充返回电压(锂电池)
- $update['vpromoterecover1'] = '12';//提升充电恢复电压/过充返回电压(锂电池)
- $update['voverdischargerecover0'] = '12.6';//过放恢复电压
- $update['voverdischargerecover1'] = '11';//过放恢复电压
- $update['vundervoltagewarn'] = '12';//欠压警告电压
- $update['voverdischarge0'] = '11';//过放电压
- $update['voverdischarge1'] = '9';//过放电压
- $update['balancechargetime'] = '1';//均衡充电时间
- $update['promotechargetime'] = '4';//提升充电时间
- $update['balanceinterval'] = '30';//均衡充电间隔
- $update['tempcompensation'] = '-4';//温度补偿系数
- $update['tempcompmax'] = '99';//温度补偿最高温度
- $update['tempcompmin'] = '-40';//温度补偿最低温度
- $update['turnfloatingcurrent'] = '20';//转浮充的电流
- $update['vsystem'] = '12';//系统电压
- $update['minusnocharge'] = '0';//零下禁充
- $update['chargemode'] = '0';//充电模式
- $res = $update;
- }
- $data = $res;
- unset($data['id']);
- unset($data['updatetime']);
- exit(json_result('0000', $this->response['0000'], $data));
- }
- // 新控制器获取蓄电池设置
- public function modbus_get_battery() {
- $lamp_id = $this->input->post('lamp_id', true);
- if (empty($lamp_id)) {
- exit(json_result('0400', $this->response['0400'], array()));
- }
- $lampData = $this->Lamp_model->getOne($lamp_id,'N.status,N.protocoltype,N.deviceid,L.address,L.protocoltype as lampprotocoltype,L.number');
- // if ($lampData['status'] == 0) {
- // exit(json_result('0412',$this->response['0412'],array()));
- // }
- $cmd = '{"cmd_type":"get_battery_setting_cmd","cmd_id":'.$lamp_id.',"broadcast":0}';
- if ($lampData['protocoltype'] == 4 && $lampData['lampprotocoltype'] == 1) {
- $sendData = '0103e0020020d212';
-
- $res = lampMqttCmd('/IDCOL/CmdInput/'.$lampData['address'],'/IDCOL/CmdOutput/'.$lampData['address'],pack('H*','0001'.$sendData));
- if (empty($res)) {
- exit(json_result('0016', $this->response['0016']));
- }else{
-
- if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'],-4))) {
- $data = array();
- $data['system_voltage'] = base_convert(substr($res['msg'], 10,4), 16, 10).''; // 系统电压
- if (substr('00000000'.base_convert(substr($res['msg'], 132,2), 16, 2), -8,1) == 1) { // 电池类型
- $data['battery_type'] = '1';
- }else{
- $data['battery_type'] = '0';
- }
- $data['overvoltage'] = round(base_convert(substr($res['msg'], 18,4), 16, 10)/10,1).''; // 超压电压
- $data['char_limit_voltage'] = round(base_convert(substr($res['msg'], 22,4), 16, 10)/10,1).''; // 充电限制电压
- $data['equ_char_voltage'] = round(base_convert(substr($res['msg'], 26,4), 16, 10)/10,1).''; // 均衡充电电压
- $data['increa_char_voltage'] = round(base_convert(substr($res['msg'], 30,4), 16, 10)/10,1).''; // 提升充电电压
- $data['float_char_voltage'] = round(base_convert(substr($res['msg'], 34,4), 16, 10)/10,1).''; // 浮充充电电压
- $data['increa_char_return_voltage'] = round(base_convert(substr($res['msg'], 38,4), 16, 10)/10,1).''; // 提升充电返回电压
- $data['over_dischar_return_voltage'] = round(base_convert(substr($res['msg'], 42,4), 16, 10)/10,1).''; // 过放返回电压
- $data['over_dischar_voltage'] = round(base_convert(substr($res['msg'], 50,4), 16, 10)/10,1).''; // 过放电压
- $data['equ_char_time'] = base_convert(substr($res['msg'], 66,4), 16, 10).''; // 均衡充电时间
- $data['increa_char_time'] = base_convert(substr($res['msg'], 70,4), 16, 10).''; // 提升充电时间
- $data['balan_char_interval'] = base_convert(substr($res['msg'], 74,4), 16, 10).''; // 均衡充电间隔
- $data['temper_compen_coeff'] = base_convert(substr($res['msg'], 78,4), 16, 10).''; // 温度补偿系数
- $data['char_upper_limit_temper'] = bitDecode(substr($res['msg'], 84,2)).''; // 充电上限温度
- $data['char_lower_limit_temper'] = bitDecode(substr($res['msg'], 88,2)).''; // 充电下限温度
- $data['dischar_upper_limit_temper'] = bitDecode(substr($res['msg'], 92,2)).''; // 放电上限温度
- $data['dischar_lower_limit_temper'] = bitDecode(substr($res['msg'], 96,2)).''; // 放电下限温度
- $data['light_voltage'] = base_convert(substr($res['msg'], 122,4),16,10).''; // 光控电压
- $info = $this->Modbus_battery_model->getOne($lamp_id);
- if (empty($info)) {
- $data['li_battery_type'] = '0';
- }else{
- $data['li_battery_type'] = $info['li_battery_type'];
- }
- $res = $data;
- }else{
- exit(json_result('0010', $this->response['0010']));
- }
- }
-
- }
- elseif ($lampData['protocoltype'] == 6 && $lampData['lampprotocoltype'] == 1){
- $sendData = '0103e0020020d212';
- $cmdLine='0103e0020020d212';
- //$cmdLine .= crc16(pack('H*',$cmdLine));
- device_cmd($lampData['deviceid'],pack('H*',$cmdLine));
- $cmdLine = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
- $res=send_lorawan_915($cmdLine);
- if($res){
- $res=json_decode($res,true);
- //var_dump($res);exit();
- if($res['info']=='success'){
- $res =$this->Modbus_battery_model->getOne($lamp_id);
- if (empty($res)) {
- $res['li_battery_type'] = '0';
- }
-
- }else{
- exit(json_result('0010', $this->response['0010']));
- }
- }else{
- exit(json_result('0010', $this->response['0010']));
- }
- }
- else{
- if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
- $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
- }
- }else{
- $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
- }
- }
- if (empty($cmdret)) exit(json_result('0016',$this->response['0016']));
- $r = json_decode($cmdret, true);
- if ($r['result'] == false) {
- $version = $this->session->userdata('version');
- if (empty($version)) {
- $msg = empty($r['msg']) ? '未知错误' : transfer_error_tips($r['msg']);
- }else{
- $msg = empty($r['msg']) ? 'Unknown error' : $r['msg'];
- }
- exit(json_result('other', $msg));
- }
- $res = $this->Modbus_battery_model->getOne($lamp_id);
- if (empty($res)) {
- exit(json_result('0417', $this->response['0417']));
- }
- }
-
- $data = $res;
- $data['equ_char_time'] = intval($data['equ_char_time']/60).'';
- $data['increa_char_time'] = intval($data['increa_char_time']/60).'';
- unset($data['id']);
- unset($data['updatetime']);
- exit(json_result('0000', $this->response['0000'], $data));
- }
- // 灯控相关数据
- public function viewloglist() {
- $lamp_id = $this->input->post('lamp_id',true);
- $datatype = $this->input->post('datatype',true);
- if (empty($lamp_id)) {
- exit(json_result('0400', $this->response['0400'], array()));
- }
- if ($datatype == 'lampinfo') {
- $fields = "number,projectid,address,longitude,latitude,section,boardpower,lamptype,boardtype,batteryah,loadpower,devstatus,loadtype,supplier,upazilla,po,manu,protocoltype,updatetime,status";
- $res = $this->Lamp_model->get_one(['id'=>$lamp_id],$fields);
- if (empty($res)) {
- exit(json_result('0000', $this->response['0006'], array()));
- }
- $version = $this->session->userdata('version');
- if (!empty($version)) {
- $polemaType = ['冷镀锌'=>'Cold galvanizing','热镀锌'=>'Hot galvanizing','不锈钢'=>'Stainless steel'];
- $lightType = ['LED灯'=>'LED lamp','钠灯'=>'Sodium lamp','节能灯'=>'Energy saving light'];
- $lampType = ['太阳能路灯'=>'Solar street lamp','景观灯'=>'Landscape lamp','庭院灯'=>'Garden light'];
- $boardType = ['单晶板'=>'Single crystal plate','多晶板'=>'Polycrystalline plate','非晶板'=>'Amorphous plate'];
- $batteryType = ['铅酸电池'=>'Lead acid battery','锂电池'=>'Lithium battery'];
- if (!preg_match('/^[A-Za-z\s*]+$/', $res['lamptype'])) {
- $res['lamptype'] = empty($lampType[$res['lamptype']]) ? '' : $lampType[$res['lamptype']];
- }
- if (!preg_match('/^[A-Za-z\s*]+$/', $res['boardtype'])) {
- $res['boardtype'] = empty($boardType[$res['boardtype']]) ? '' : $boardType[$res['boardtype']];
- }
- }else{
- $polemaType = ['Cold galvanizing'=>'冷镀锌','Hot galvanizing'=>'热镀锌','Stainless steel'=>'不锈钢'];
- $lightType = ['LED lamp'=>'LED灯','Sodium lamp'=>'钠灯','Energy saving light'=>'节能灯'];
- $lampType = ['Solar street lamp'=>'太阳能路灯','Landscape lamp'=>'景观灯','Garden light'=>'庭院灯'];
- $boardType = ['Single crystal plate'=>'单晶板','Polycrystalline plate'=>'多晶板','Amorphous plate'=>'非晶板'];
- $batteryType = ['Lead acid battery'=>'铅酸电池','Lithium battery'=>'锂电池'];
- if (preg_match('/^[A-Za-z\s*]+$/', $res['lamptype'])) {
- $res['lamptype'] = empty($lampType[$res['lamptype']]) ? '' : $lampType[$res['lamptype']];
- }
- if (preg_match('/^[A-Za-z\s*]+$/', $res['boardtype'])) {
- $res['boardtype'] = empty($boardType[$res['boardtype']]) ? '' : $boardType[$res['boardtype']];
- }
- }
- // if ($res["isfaulted"] == 0) {
- // $res["isfaulted"] = '正常';
- // }else{
-
- // }
- $alarmData = $this->Alarm_model->get_one_by_filter(array('lampid'=>$lamp_id,'status'=>0),'stralarmtype');
- $version = $this->session->userdata('version');
- if (empty($version)) {
- $res['isfaulted'] = empty($alarmData['stralarmtype']) ? '正常' : $alarmData['stralarmtype'];
- }else{
- $res['isfaulted'] = empty($alarmData['stralarmtype']) ? 'Normal' : alarm_translate($alarmData['stralarmtype']);
- }
-
- $timezone = $this->Project_model->get_timezone_by_projectid($res['projectid']);
- $res["updatetime"] = date_change($res["updatetime"],8,$timezone['value']);
- $data = array(
- "lamp_no" => $res["number"],
- "project_name" => $this->Project_model->getNameById($res["projectid"]),
- "address" => $res["address"],
- "longitude" => $res["longitude"],
- "latitude" => $res["latitude"],
- "isfaulted" => $res["devstatus"],
- "section" => $res["section"],
- "boardpower" => floatval($res["boardpower"]),
- "lamptype" => $res['lamptype'],
- "boardtype" => $res['boardtype'],
- "batteryah" => $res["batteryah"],
- "loadpower" => $res["loadpower"],
- 'status' => $res['status'],
- // 'batterytype' => $res['batterytype'],
- 'protocoltype'=>intval($res['protocoltype']),
- 'supplier'=>intval($res['supplier']),
- 'upazilla'=>intval($res['upazilla']),
- 'po'=>intval($res['po']),
- 'manu'=>intval($res['manu']),
- "updatetime" => $res["updatetime"],
- 'loadtype' => $res['loadtype']
- );
- // $msisdn = $this->db->query('select iccid from msisdn where imsi = "'.$res['simid'].'"')->row_array();
- // if (empty($msisdn) || empty($msisdn['iccid'])) {
- // $data['iccid'] = '';
- // }else{
- // $data['iccid'] = $msisdn['iccid'];
- // }
- // if ($res['protocoltype'] != 1 && $res['protocoltype'] != 2) {
- // unset($data['packageSurplus']);
- // unset($data['simid']);
- // }
- // $username = $this->get_user_info('username');
- // if ($username == 'zns') {
- // $data['monthly_can_usage'] = intval($res['monthly_can_usage']);
- // $data['monthly_already_usage'] = round($res['monthly_already_usage'],2);
- // }
- exit(json_result('0000', $this->response['0000'], $data));
- }
- $table_map = array('system_info_log','lamp_info_log','solar_info_log','battery_info_log','electric_info_log','history_info_log');
- $table = in_array($datatype, $table_map) ? $datatype : 'system_info_log';
- $res = $this->Lamp_model->getForInfoLog($lamp_id, $table);
- $lampData = $this->Lamp_model->get_one(['id'=>$lamp_id],'projectid');
- $timezone = $this->Project_model->get_timezone_by_projectid($lampData['projectid']);
- if(!$res){
- exit(json_result('0000', $this->response['0006'], array()));
- }
- if ($table == 'battery_info_log') {
- if (isset($res['daydischargemincurrent'])) {
- $h = floor($res['daydischargemincurrent']/3600);
- $h = $h < 10 ? '0'.$h : $h;
- $i = intval($res['daydischargemincurrent']/60)%60;
- $i = $i < 10 ? '0'.$i : $i;
- $res['daydischargemincurrent'] = $h.':'.$i;
- }
- if (isset($res['daychargemincurrent'])) {
- $h = floor($res['daychargemincurrent']/3600);
- $h = $h < 10 ? '0'.$h : $h;
- $i = intval($res['daychargemincurrent']/60)%60;
- $i = $i < 10 ? '0'.$i : $i;
- $res['daychargemincurrent'] = $h.':'.$i;
- }
- }
- $res["updatetime"] = date_change($res["updatetime"],8,$timezone['value']);
- exit(json_result('0000', $this->response['0000'], $res));
- }
- // 灯控历史数据
- public function historylist() {
- $lamp_id = $this->input->post('lamp_id',true);
- $datatype = $this->input->post('datatype',true);
- $page = intval($this->input->post('page',true));
- $count = intval($this->input->post('count',true));
- $count = empty($count) ? 16 : $count;
- if (empty($lamp_id)) {
- exit(json_result('0400', $this->response['0400'], array()));
- }
- $date = $this->input->post('date',true);
- $beginDate = '';
- $endDate = '';
- // 按时区修改时间
- $lampData = $this->Lamp_model->get_one(['id'=>$lamp_id],'projectid,protocoltype');
- $timezone = $this->Project_model->get_timezone_by_projectid($lampData['projectid']);
- if (!empty($date)) {
- $dateArr = explode('/', $date);
- $beginDate = date('Y-m-d H:i:s',strtotime($dateArr[0]) - 8*3600 + $timezone['value']*3600);
- $endDate = date('Y-m-d H:i:s',strtotime($dateArr[1]) - 8*3600 + $timezone['value']*3600);
- // $beginDate = $dateArr[0];
- // $endDate = $dateArr[1];
- if (strtotime($endDate) - strtotime($beginDate) > 31*24*3600) {
- exit(json_result('0418',$this->response['0418']));
- }
- }else{
- $beginDate = date('Y-m-d H:i:s',strtotime(date('Y-m-d 00:00:00',time())) - 8*3600 + $timezone['value']*3600);
- $endDate = date('Y-m-d H:i:s',strtotime(date('Y-m-d 23:59:59',time())) - 8*3600 + $timezone['value']*3600);
- }
- if($page < 1){
- $page = 1;
- }
-
- $download = intval($this->input->post('download'));
- if (!empty($download)) {
- if (empty($date)) exit(json_result('0007',$this->response['0007']));
- $dataList = array();
- // 路灯
- $lampInfoList = $this->Lamp_model->getListForInfoLog($lamp_id, 'lamp_info_log', null, null,$beginDate,$endDate);
- $temp = array();
- $i = 1;
- if (empty($this->version)) {
- $lampStatusArr = array(0=>'关灯',1=>'开灯');
- $title = array('序号','亮度%','状态','电压(V)','电流(A)','功率(W)','当天放电最大功率(W)','当天亮灯时长(HH:MM)','更新时间');
- $name = '路灯';
- }else{
- $lampStatusArr = array(0=>'off',1=>'on');
- $title = array('Number','Brightness%','Status','Voltage(V)','Current(A)','Power(W)','Maximum discharge power of the day(W)','dayLighting duration(HH:MM)','Update time');
- $name = 'Lamp';
- }
- foreach ($lampInfoList as $k=>$l) {
- $temp[$k][] = $i;
- $temp[$k][] = $l['lighteness'];
- $temp[$k][] = $lampStatusArr[$l['lampstatus']];
- $temp[$k][] = $l['lampvoltage'];
- $temp[$k][] = $l['lampcurrent'];
- $temp[$k][] = $l['lamppower'];
- $temp[$k][] = $l['daydischarmaxpow'];
- $h = floor($l['daychargemincurrent'] / 3600);
- $h = $h < 10 ? '0'.$h : $h;
- $m = ceil($l['daychargemincurrent'] % 3600 / 60);
- $m = $m < 10 ? '0'.$m : $m;
- $temp[$k][] = $h.':'.$m;
- $temp[$k][] = date_change($l["updatetime"],8,$timezone['value']);
- $i ++;
- }
- $title1 = [];
- foreach ($title as $t) {
- $title1[0][] = $t;
- }
- $data = array_merge($title1,$temp);
- $dataList[] = array('name'=>$name,'dataList'=>$data);
- // 蓄电池
- $lampInfoList = $this->Lamp_model->getListForInfoLog($lamp_id, 'battery_info_log', null, null,$beginDate,$endDate);
- $temp = array();
- $i = 1;
- if (empty($this->version)) {
- $name = '蓄电池';
- if ($lampData['protocoltype'] == 2) {
- $chargeStageMap = array(0=>'没有充电',1=>'等待',2=>'初始',3=>'MPPT',4=>'恒压',5=>'恒流',6=>'浮充',7=>'均衡',8=>'提升',9=>'涓流',10=>'完成',''=>'未知状态');
- }else{
- $chargeStageMap = array(0=>'没有充电',1=>'启动充电',6=>'限流',7=>'充满',16=>'MPPT充电',32=>'均衡充电',48=>'提升充电',64=>'浮充',''=>'未知状态');
- }
- $battstatusArr = array(0=>'空闲',1=>'放电',2=>'充电',3=>'一边充电一边放电',''=>'未知状态');
- $unknown = '未知状态';
- $title = array('序号','系统电压(V)','系统电流(A)','MOS温度(℃)','电池状态','充电阶段','电池电压(V)','当天最低电压(V)','当天最高电压(V)','电池温度(℃)','当天电池最低温度(℃)','当天电池最高温度(℃)','更新时间');
- }else{
- $name = 'Battery';
- if ($lampData['protocoltype'] == 2) {
- $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');
- }else{
- $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');
- }
- $battstatusArr = array(0=>'Free',1=>'Discharge',2=>'Charge',3=>'Discharge while charging',''=>'Unknown state');
- $unknown = 'Unknown state';
- $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');
- }
- $indexArr = [2=>'16',3=>'32',4=>'48',5=>'64'];
- foreach ($lampInfoList as $k=>$l) {
- $temp[$k][] = $i;
- $temp[$k][] = $l['sysvoltage'];
- $temp[$k][] = $l['syscurrent'];
- $temp[$k][] = $l['temper'];
- $temp[$k][] = $battstatusArr[$l['battstatus']];
- if ($lampData['protocoltype'] == 1) {
- $l['chargestage'] = isset($indexArr[intval($l['chargestage'])]) ? $indexArr[intval($l['chargestage'])] : $l['chargestage'];
- }
- $temp[$k][] = $chargeStageMap[$l['chargestage']];
- $temp[$k][] = $l['battvoltage'];
- $temp[$k][] = $l['voltagedaymin'];
- $temp[$k][] = $l['voltagedaymax'];
- $temp[$k][] = $l['batttemper'];
- $temp[$k][] = $l['daybattmintemper'];
- $temp[$k][] = $l['daybattmaxtemper'];
- $temp[$k][] = date_change($l["updatetime"],8,$timezone['value']);
- $i ++;
- }
- $title1 = [];
- foreach ($title as $t) {
- $title1[0][] = $t;
- }
- $data = array_merge($title1,$temp);
- $dataList[] = array('name'=>$name,'dataList'=>$data);
- // 太阳能
- $lampInfoList = $this->Lamp_model->getListForInfoLog($lamp_id, 'solar_info_log', null, null,$beginDate,$endDate);
- $temp = array();
- $i = 1;
- if (empty($this->version)) {
- $name = '太阳能';
- $title = array('序号','电压(V)','电流(A)','功率(W)','当天充电最大功率(WH)','当天充电时长(HH:MM)','更新时间');
- }else{
- $name = 'Solar';
- $title = array('Number','Voltage(V)','Current(A)','Power(W)','Maximum Power Charged on the Day(W)','day Charging time(HH:MM)','Update time');
- }
- foreach ($lampInfoList as $k=>$l) {
- $temp[$k][] = $i;
- $temp[$k][] = $l['solarvoltage'];
- $temp[$k][] = $l['solarcurrent'];
- $temp[$k][] = $l['solarpower'];
- $temp[$k][] = $l['daychargemaxpow'];
- $h = floor($l['daydischargemincurrent'] / 3600);
- $h = $h < 10 ? '0'.$h : $h;
- $m = ceil($l['daydischargemincurrent'] % 3600 / 60);
- $m = $m < 10 ? '0'.$m : $m;
- $temp[$k][] = $h.':'.$m;
- $temp[$k][] = date_change($l["updatetime"],8,$timezone['value']);
- $i ++;
- }
- $title1 = [];
- foreach ($title as $t) {
- $title1[0][] = $t;
- }
- $data = array_merge($title1,$temp);
- $dataList[] = array('name'=>$name,'dataList'=>$data);
- // 系统
- $lampInfoList = $this->Lamp_model->getListForInfoLog($lamp_id, 'system_info_log', null, null,$beginDate,$endDate);
- $temp = array();
- $i = 1;
- if (empty($this->version)) {
- $name = '系统';
- $title = array('序号','系统电压(V)','系统电流(A)','控制器温度(℃)','更新时间');
- }else{
- $name = 'System';
- $title = array('Serial number','System voltage(V)','System current(A)','Control temperature(℃)','Update time');
- }
- foreach ($lampInfoList as $k=>$l) {
- $temp[$k][] = $i;
- $temp[$k][] = $l['sysvoltage'];
- $temp[$k][] = $l['syscurrent'];
- $temp[$k][] = $l['temper'];
- $temp[$k][] = date_change($l["updatetime"],8,$timezone['value']);
- $i ++;
- }
- $title1 = [];
- foreach ($title as $t) {
- $title1[0][] = $t;
- }
- $data = array_merge($title1,$temp);
- $dataList[] = array('name'=>$name,'dataList'=>$data);
- $path = push_excel($dataList,'historyInfoExcel_'.date('Ymd'),1);
- exit(json_result('0000', $this->response['0000'], array('path'=>$path)));
- }
- // $table_map = array('system_info_log','lamp_info_log','solar_info_log','battery_info_log','electric_info_log','history_info_log');
- $table = !empty($datatype) ? $datatype : 'system_info_log';
- if ($table == 'lamp_info_log') {
- $table = 'realtime_info_log';
- $total = $this->RealtimeInfoLog_model->get_count(['lampid'=>$lamp_id,'updatetime >='=>$beginDate,'updatetime <='=>$endDate]);
- $res = $this->RealtimeInfoLog_model->get_list(['lampid'=>$lamp_id,'updatetime >='=>$beginDate,'updatetime <='=>$endDate], 'loadvoltage as lampvoltage,loadcurrent as lampcurrent,loadpower as lamppower,updatetime',$count, ($page-1)*$count, 'updatetime desc,id desc', NUll);
- // $res = $this->Lamp_model->getListForInfoLog($lamp_id, $table, ($page-1)*$count, $count,$beginDate,$endDate);
- }elseif ($table == 'system_info_log') {
- $total = $this->Lamp_model->getCountForInfoLog($lamp_id, $table,$beginDate,$endDate);
- $res = $this->Lamp_model->getListForInfoLog($lamp_id, $table, ($page-1)*$count, $count,$beginDate,$endDate);
- }elseif ($table == 'battery_info_log') {
- $join = [
- // ['table'=>'history_info_log as HI','cond'=>'HI.lampid = RI.lampid and HI.updatetime = RI.updatetime','type'=>'inner'],
- ];
- $where = ['lampid'=>$lamp_id,'updatetime >='=>$beginDate,'updatetime <='=>$endDate];
- $total = $this->RealtimeInfoLog_model->get_count($where,'count(*) as total');
- $fields = 'batvoltage as battvoltage,updatetime,DayOverDisChgTimes as overtimes';
- $res = $this->RealtimeInfoLog_model->get_list($where, $fields,$count, ($page-1)*$count, 'updatetime desc,id desc');
- }else{
- $total = $this->Lamp_model->getCountForInfoLog($lamp_id, $table,$beginDate,$endDate);
- $res = $this->Lamp_model->getListForInfoLog($lamp_id, $table, ($page-1)*$count, $count,$beginDate,$endDate);
- }
-
- if(!$res){
- exit(json_result('0000', $this->response['0006'], array()));
- }else{
-
- $arr = [2=>'16',3=>'32',4=>'48',5=>'64'];
- foreach ($res as &$v) {
- $v['updatetime'] = date_change($v["updatetime"],8,$timezone['value']);
- if ($lampData['protocoltype'] == 1 && $table == 'battery_info_log') {
- $v['chargestage'] = isset($arr[intval($v['chargestage'])]) ? $arr[intval($v['chargestage'])] : $v['chargestage'];
- }
- if ($table == 'system_info_log') {
- $v['lampid'] = $v['number'];
- }
- }
- }
- $data = array(
- 'total' => ceil($total/$count),
- 'list' => $res
- );
- exit(json_result('0000', $this->response['0000'], $data));
- }
- // 修改路灯位置
- public function change_location(){
- $id = intval($this->input->post('id',true));
- $type = intval($this->input->post('type',true));
- $data['longitude'] = $this->input->post('longitude',true);
- $data['latitude'] = $this->input->post('latitude',true);
- $data['updatetime'] = date('Y-m-d H:i:s',time());
- if (empty($id) || empty($type) || !isset($data['longitude']) || !isset($data['latitude'])) {
- exit(json_result('0007',$this->response['0007'],array()));
- }
- if ($type == 1) {
- $this->Lamp_model->update($data,array('id'=>$id));
- }else{
- $this->Videomonitor_model->update($data,array('id'=>$id));
- }
- exit(json_result('0000',$this->response['0000'],array()));
- }
- // 同步巡检
- public function set_patrol_cmd(){
- $id = intval($this->input->post('id',true));
- if (empty($id)) exit(json_result('0007',$this->response['0007'],array('id'=>$id)));
- $lampData = $this->Lamp_model->getOne($id,'N.status,N.protocoltype,L.address,L.protocoltype as lampprotocoltype,L.number');
- $cmd = '{"cmd_type":"set_sync_patrol_cmd","cmd_id":'.$id.'}';
- $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
- if ($cmdret === false) {
- sleep(1);
- $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
- }
- exit(json_result('0000',$this->response['0000'],array('id'=>$id)));
- }
- // 批量添加
- public function batchInput(){
- set_time_limit(0);
- $userid = $this->get_user_info('id');
- // $idArr = $this->get_section_by_user();
- $role = $this->get_user_info('role');
- $companyid = $this->get_user_info('companyid');
- $type = intval($this->input->post('type',true));
- if ($type == 1) { // 验证
- $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'];
- $data = $this->batch_import($indexArr);
- }else{ // 创建
- $data = $this->input->post('lampData',true);
- if (empty($data)) exit(json_result('0007',$this->response['0007']));
- $data = json_decode($data,true);
- }
- foreach ($data as &$v) {
- if ($type == 1 && is_float($v['address'])) $v['address'] = strval(intval(ceil($v['address'])));
- }
- $res = 0;
- $typeArr = ['tcp'=>0,'nb'=>1,'gprs'=>2,'lorawan'=>3,'MQTT'=>4,'WeLoraWan'=>6,'onenet'=>7];
- $this->db->trans_begin();
- $msg = [];
- $patrol_arr = array();
- foreach ($data as $key => $value) {
- $num = $key+2;
- $value['number'] = trim($value['number']);
- if (!isset($value['number']) || $value['number'] == '') {
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,路灯编号不能为空';
- }else{
- $msg[] = 'Table '.$num.' row.Lamp number can not be empty';
- }
- }else{
- if (!is_numeric($value['number']) && ($value['number'] <= 0 || $value['number'] > 999999)) {
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,路灯编号为1-999999的数字';
- }else{
- $msg[] = 'Table '.$num.' row.The number of street lamps numbered 1-999999';
- }
- }
- }
- // if ($type == 1 && is_float($value['address'])) {
- // $value['address'] = strval(intval(ceil($value['address'])));
- // }
- $value['address'] = trim($value['address']);
- if (empty($value['address'])) {
- $res = 1;
-
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,无线模块地址不能为空';
- }else{
- $msg[] = 'Table '.$num.' row.Address of the wireless module can not be empty';
- }
- }else{
- $status = preg_match('/^[A-Fa-f0-9]+$/', $value['address']);
- if (empty($status)) {
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,无线模块地址为16进制数';
- }else{
- $msg[] = 'Table '.$num.' row.Wireless module address is 16 hexadecimal';
- }
- }else{
- if ($this->Lamp_model->getDataCount(['address'=>$value['address']])) {
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,无线模块地址已经存在';
- }else{
- $msg[] = 'Table '.$num.' row.Wireless module address already exists';
- }
- }
- }
- }
- $value['company_name'] = trim($value['company_name']);
- if (empty($value['company_name'])) {
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,公司名称不能为空';
- }else{
- $msg[] = 'Table '.$num.' row.Company name cannot be empty';
- }
- }
- $value['projectid'] = trim($value['projectid']);
- if (empty($value['projectid'])) {
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,项目名称不能为空';
- }else{
- $msg[] = 'Table '.$num.' row.Project name cannot be empty';
- }
- }else{
- $projectid = $this->Project_model->project_exist_by_name($role,$userid,$companyid,$value['projectid'],$value['company_name']);
- if (empty($projectid)) {
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,项目不存在,请先创建项目';
- }else{
- $msg[] = 'Table '.$num.' row.The project does not exist. Please create a project first.';
- }
- $value['projectid'] = 0;
- }else{
- $value['projectid'] = $projectid;
- $this->Project_model->add_lamp_count(array('faultcount'=>0,'projectid'=>$projectid));
- if ($this->Lamp_model->getDataCount(['projectid'=>$value['projectid'],'number'=>$value['number']])) {
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,路灯编号已经存在';
- }else{
- $msg[] = 'Table '.$num.' row lamp number already exists';
- }
- }
- }
- }
- if($type != 1 && isset($typeArr[$value['protocoltype']]) && $typeArr[$value['protocoltype']] == 6){
- $this->add_device($value['address']);
- }
- $value['devicetype'] = intval(trim($value['devicetype']));
- if ($value['devicetype'] == 0) { // 非直连设备
- unset($value['protocoltype']);
- $value['networkid'] = trim($value['networkid']);
- if (empty($value['networkid'])) {
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,非直连设备网络编号不能为空';
- }else{
- $msg[] = 'Table '.$num.' row.Indirect device network number cannot be empty';
- }
- }else{
- $networkid = $this->Network_model->project_exist_by_name($value['projectid'],$value['networkid']);
- // var_dump($this->db->last_query());die;
- if (empty($networkid)) {
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,网络不存在,请先添加网络';
- }else{
- $msg[] = 'Table '.$num.' row.The network does not exist. Please add the network first';
- }
- }else{
- $this->Network_model->add_lamp_count(array('faultcount'=>0,'networkid'=>$networkid));
- $value['networkid'] = $networkid;
- }
- }
- }elseif ($value['devicetype'] == 1) { // 直连设备
- $value['protocoltype'] = trim($value['protocoltype']);
- if (empty($value['protocoltype'])) {
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,协议类型不能为空';
- }else{
- $msg[] = 'Table '.$num.' row.Protocol type cannot be empty';
- }
- }else{
- if (!isset($typeArr[$value['protocoltype']])) {
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,协议类型不存在';
- }else{
- $msg[] = 'Table '.$num.' row.Protocol type not exists';
- }
- }
- }
- // 创建直连网络
- $number = mb_strlen($value['address']) == 13 ? base_convert($value['address'], 16, 10) : $value['address'];
- if ($this->Network_model->getDataCount(array('devicesn'=>$number))) {
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,无线模块地址已被使用';
- }else{
- $msg[] = 'Table '.$num.' row.Wireless module address has been used';
- }
- }
- // 创建网络
- // $networkData['id'] = $this->Zone_model->insert(array(
- // 'name' => $number,
- // 'parent' => $value['projectid'] ,
- // 'level' => 4
- // ));
- // 控制器协议类型
- $value['lampprotocoltype'] = trim($value['lampprotocoltype']);
- if (empty($value['lampprotocoltype'])) {
- $networkData['devicetype'] = 0;
- }else{
- if ($value['lampprotocoltype'] == 'ModBus') {
- $networkData['devicetype'] = 3;
- }elseif ($value['lampprotocoltype'] == 'LightBus') {
- $networkData['devicetype'] = 0;
- }else{
- $networkData['devicetype'] = 0;
- }
- }
- $networkData['networkid'] = $number;
- $networkData['gatewaytype'] = 'direct';
- $networkData['protocoltype'] = empty($typeArr[$value['protocoltype']]) ? 'tcp' : $typeArr[$value['protocoltype']];
- $networkData['projectid'] = $value['projectid'];
- $networkData['devicesn'] = $number;
- $networkData['networkname'] = $number;
- $networkData['type'] = 1;
- $networkData['lampcount'] = 1;
- $networkData['createtime'] = date('Y-m-d H:i:s',time());
- if ($type == 1) {
- $this->db->insert('network',$networkData);
- $value['networkid'] = $this->db->insert_id();
- }else{
- $value['networkid'] = $this->Network_model->insert($networkData);
- }
- }else{
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,设备类型选择错误';
- }else{
- $msg[] = 'Table '.$num.' row.Device type selection error';
- }
- }
- unset($value['protocoltype']);
- // 控制器协议类型
- $value['lampprotocoltype'] = trim($value['lampprotocoltype']);
- if (empty($value['lampprotocoltype'])) {
- $res = 1;
-
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,协议类型不能为空';
- }else{
- $msg[] = 'Table '.$num.' row.Protocol type can not be empty';
- }
- }else{
- if ($value['lampprotocoltype'] == 'ModBus') {
- $value['protocoltype'] = 1;
- }elseif ($value['lampprotocoltype'] == 'LightBus') {
- $value['protocoltype'] = 0;
- }else{
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,协议类型选择错误';
- }else{
- $msg[] = 'Table '.$num.' row.Protocol type selection error';
- }
- }
- }
- unset($value['lampprotocoltype']);
- $value['longitude'] = trim($value['longitude']);
- if (empty($value['longitude'])) {
- $value['longitude'] = 0;
- }else{
- if (!is_numeric($value['longitude']) || $value['longitude'] > 180 || $value['longitude'] < -180) {
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,经度输入错误';
- }else{
- $msg[] = 'Table '.$num.' row longitude input error';
- }
- }
- }
- $value['latitude'] = trim($value['latitude']);
- if (empty($value['latitude'])) {
- $value['latitude'] = 0;
- }else{
- if (!is_numeric($value['latitude']) || $value['latitude'] > 90 || $value['latitude'] < -90) {
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,纬度输入错误';
- }else{
- $msg[] = 'Table '.$num.' row latitude input error';
- }
- }
- }
- if (empty($value['latitude']) && empty($value['longitude']) && !empty($value['projectid'])) {
- $projectData = $this->Project_model->getOne($value['projectid'],'cityid');
- if (!empty($projectData['cityid'])){
- $globalData = $this->Global_location_model->getOne($projectData['cityid'],'longitude,latitude');
- $value['longitude'] = $globalData['longitude'];
- $value['latitude'] = $globalData['latitude'];
- }
- }
-
- $value['loadtype'] = trim($value['loadtype']);
- if (empty($value['loadtype']) || $value['loadtype'] == '0' || $value['loadtype'] == '1') {
- if ($value['loadtype'] == '1') {
- $value['loadtype'] = 1;
- }else{
- $value['loadtype'] = 0;
- }
- }else{
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,负载类型错误';
- }else{
- $msg[] = 'Table '.$num.' row load type error';
- }
- }
- if (trim($value['poleheight']) == '') {
- unset($value['poleheight']);
- }else{
- $value['poleheight'] = trim($value['poleheight']);
- }
- if (trim($value['polediameter']) == '') {
- unset($value['polediameter']);
- }else{
- $value['polediameter'] = trim($value['polediameter']);
- }
- if (trim($value['section']) == '') {
- $value['section'] = '';
- $res = 1;
- if (empty($this->version)) {
- $msg[] = '表格第'.$num.'行,路段不能为空';
- }else{
- $msg[] = 'Table '.$num.' row. Section cannot be empty';
- }
- }else{
- $value['section'] = trim($value['section']);
- }
- if (trim($value['polematerial']) == '') {
- unset($value['polematerial']);
- }else{
- $value['polematerial'] = trim($value['polematerial']);
- }
- if (trim($value['lighttype']) == '') {
- unset($value['lighttype']);
- }else{
- $value['lighttype'] = trim($value['lighttype']);
- }
- if (trim($value['lamptype']) == '') {
- unset($value['lamptype']);
- }else{
- $value['lamptype'] = trim($value['lamptype']);
- }
- if (trim($value['boardtype']) == '') {
- unset($value['boardtype']);
- }else{
- $value['boardtype'] = trim($value['boardtype']);
- }
- if (trim($value['batterytype']) == '') {
- unset($value['batterytype']);
- }else{
- $value['batterytype'] = trim($value['batterytype']);
- }
- if (trim($value['boardpower']) == '') {
- unset($value['boardpower']);
- }else{
- $value['boardpower'] = trim($value['boardpower']);
- }
- if (trim($value['batteryah']) == '') {
- unset($value['batteryah']);
- }else{
- $value['batteryah'] = trim($value['batteryah']);
- }
- if (trim($value['loadpower']) == '') {
- unset($value['loadpower']);
- }else{
- $value['loadpower'] = trim($value['loadpower']);
- }
- $value['createtime'] = date('Y-m-d H:i:s',time());
- $value['updatetime'] = date('Y-m-d H:i:s',time());
- unset($value['company_name']);
- if (is_null($value['networkid'])) {
- unset($value['networkid']);
- }
- $lamp_id = $this->Lamp_model->insert($value);
- $patrol_arr[] = array('lampid'=>$lamp_id,'projectid'=>$value['projectid']);
- }
- if ($res || $type == 1) {
- $this->db->trans_rollback();
- exit(json_result('0000',$this->response['0000'],['lampData'=>$data,'msg'=>$msg]));
- }else{
- $this->db->trans_commit();
- foreach ($patrol_arr as $patrol) {
- $projectData = $this->Project_model->getOne($patrol['projectid'],'patrolinterval');
- $this->patrol_set($patrol['lampid'],$projectData['patrolinterval']);
- }
- exit(json_result('0000',$this->response['0000']));
- }
- }
- // 路灯历史数据曲线图
- public function lamp_history_data(){
- $type = intval($this->input->post('type',true));
- $lampId = intval($this->input->post('lampid',true));
- if (empty($lampId)) exit(json_result('0007',$this->response['0007']));
- $dateType = intval($this->input->post('dateType',true));
- $date = $this->input->post('date',true);
- if ($dateType == 0) { // 一天
- $beginDate = date('Y-m-d H:i:s',time()-24*3600);
- $endDate = date('Y-m-d H:i:s',time());
- }elseif ($dateType == 1) { // 三天
- $beginDate = date('Y-m-d H:i:s',time()-24*3600*3);
- $endDate = date('Y-m-d H:i:s',time());
- }elseif ($dateType == 2) { // 七天
- $beginDate = date('Y-m-d H:i:s',time()-24*3600*7);
- $endDate = date('Y-m-d H:i:s',time());
- }elseif ($dateType == 3) { // 十四天
- $beginDate = date('Y-m-d H:i:s',time()-24*3600*14);
- $endDate = date('Y-m-d H:i:s',time());
- }else{ // 选择时间
- if (empty($date)) exit(json_result('0007',$this->response['0007']));
- $res = explode('/', $date);
- if (strtotime($res[1]) - strtotime($res[0]) > 31 * 24 * 3600) {
- exit(json_result('0418',$this->response['0418']));
- }
- $beginDate = $res[0];
- $endDate = $res[1];
- }
- $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";
- $lampData = $this->db->query($sql)->row_array();
- $timezoneData = $this->Project_model->get_timezone_by_projectid($lampData['id']);
- $timezone = $timezoneData['value'];
- $data = ['patrolinterval'=>$lampData['patrolinterval']];
- if ($type == 0) { // 路灯
- $sql = 'select LI.loadvoltage as lampvoltage,LI.loadcurrent as lampcurrent,LI.loadpower as lamppower,LI.updatetime from realtime_info_log as LI where LI.lampid = '.$lampId.' and LI.updatetime >= "'.$beginDate.'" and LI.updatetime <= "'.$endDate.'"';
- $list = $this->db->query($sql)->result_array();
- $temp = [];
- foreach ($list as $value) {
- $temp['lampvoltage'][] = $value['lampvoltage'];
- $temp['lampcurrent'][] = $value['lampcurrent'];
- $temp['lamppower'][] = $value['lamppower'];
- // $temp['lighteness'][] = $value['lighteness'];
- $temp['updatetime'][] = date_change($value['updatetime'],8,$timezone);
- if (!isset($data['maxcurrent'])) {
- $data['maxcurrent'] = $value['lampcurrent'];
- }else{
- $data['maxcurrent'] = $value['lampcurrent'] > $data['maxcurrent'] ? $value['lampcurrent'] : $data['maxcurrent'];
- }
- // if (!isset($data['maxdaychargemincurrent'])) {
- // $data['maxdaychargemincurrent'] = $value['daychargemincurrent'];
- // }else{
- // $data['maxdaychargemincurrent'] = $value['daychargemincurrent'] > $data['maxdaychargemincurrent'] ? $value['daychargemincurrent'] : $data['maxdaychargemincurrent'];
- // }
- if (!isset($data['mincurrent'])) {
- $data['mincurrent'] = $value['lampcurrent'];
- }else{
- $data['mincurrent'] = $value['lampcurrent'] < $data['mincurrent'] ? $value['lampcurrent'] : $data['mincurrent'];
- }
- if (!isset($data['maxvoltage'])) {
- $data['maxvoltage'] = $value['lampvoltage'];
- }else{
- $data['maxvoltage'] = $value['lampvoltage'] > $data['maxvoltage'] ? $value['lampvoltage'] : $data['maxvoltage'];
- }
- if (!isset($data['minvoltage'])) {
- $data['minvoltage'] = $value['lampvoltage'];
- }else{
- $data['minvoltage'] = $value['lampvoltage'] < $data['minvoltage'] ? $value['lampvoltage'] : $data['minvoltage'];
- }
- if (!isset($data['maxpower'])) {
- $data['maxpower'] = $value['lamppower'];
- }else{
- $data['maxpower'] = $value['lamppower'] > $data['maxpower'] ? $value['lamppower'] : $data['maxpower'];
- }
- if (!isset($data['minpower'])) {
- $data['minpower'] = $value['lamppower'];
- }else{
- $data['minpower'] = $value['lamppower'] < $data['minpower'] ? $value['lamppower'] : $data['minpower'];
- }
- // if (!isset($data['maxlighteness'])) {
- // $data['maxlighteness'] = $value['lighteness'];
- // }else{
- // $data['maxlighteness'] = $value['lighteness'] > $data['maxlighteness'] ? $value['lighteness'] : $data['maxlighteness'];
- // }
- // if (!isset($data['minlighteness'])) {
- // $data['minlighteness'] = $value['lighteness'];
- // }else{
- // $data['minlighteness'] = $value['lighteness'] < $data['minlighteness'] ? $value['lighteness'] : $data['minlighteness'];
- // }
- // if (isset($data['maxdaychargemincurrent'])) {
- // $h = floor($data['maxdaychargemincurrent'] / 3600);
- // $h = $h < 10 ? '0'.$h : $h;
- // $m = floor($data['maxdaychargemincurrent'] / 3600 % 60);
- // $m = $m < 10 ? '0'.$m : $m;
- // $data['maxdaychargemincurrent'] = $h.':'.$m;
- // }
- }
- $list = $temp;
- }elseif ($type == 1) { // 蓄电池
- $sql = 'select batvoltage as battvoltage,batcurrent as chargecurrent,chgtemper as batttemper,batpower as chargepower,updatetime from realtime_info_log where lampid = '.$lampId.' and updatetime >= "'.$beginDate.'" and updatetime <= "'.$endDate.'"';
- $list = $this->db->query($sql)->result_array();
- $temp = [];
- foreach ($list as $value) {
- $temp['battvoltage'][] = $value['battvoltage'];
- $temp['updatetime'][] = date_change($value['updatetime'],8,$timezone);
-
- $batterycurrent = $value['chargecurrent'];
- $temp['batterycurrent'][] = $batterycurrent;
-
- $batterypower = $value['chargepower'];
- $temp['batterypower'][] = $batterypower;
- if (!isset($data['maxcurrent'])) {
- $data['maxcurrent'] = $batterycurrent;
- }else{
- $data['maxcurrent'] = $batterycurrent > $data['maxcurrent'] ? $batterycurrent : $data['maxcurrent'];
- }
- if (!isset($data['mincurrent'])) {
- $data['mincurrent'] = $batterycurrent;
- }else{
- $data['mincurrent'] = $batterycurrent < $data['mincurrent'] ? $batterycurrent : $data['mincurrent'];
- }
- if (!isset($data['maxvoltage'])) {
- $data['maxvoltage'] = $value['battvoltage'];
- }else{
- $data['maxvoltage'] = $value['battvoltage'] > $data['maxvoltage'] ? $value['battvoltage'] : $data['maxvoltage'];
- }
- if (!isset($data['minvoltage'])) {
- $data['minvoltage'] = $value['battvoltage'];
- }else{
- $data['minvoltage'] = $value['battvoltage'] < $data['minvoltage'] ? $value['battvoltage'] : $data['minvoltage'];
- }
- if (!isset($data['maxbatttemper'])) {
- $data['maxbatttemper'] = $value['batttemper'];
- }else{
- $data['maxbatttemper'] = $value['batttemper'] > $data['maxbatttemper'] ? $value['batttemper'] : $data['maxbatttemper'];
- }
- if (!isset($data['minbatttemper'])) {
- $data['minbatttemper'] = $value['batttemper'];
- }else{
- $data['minbatttemper'] = $value['batttemper'] < $data['minbatttemper'] ? $value['batttemper'] : $data['minbatttemper'];
- }
- if (!isset($data['maxpower'])) {
- $data['maxpower'] = round($batterypower,2);
- }else{
- $data['maxpower'] = $batterypower > $data['maxpower'] ? round($batterypower,2) : round($data['maxpower'],2);
- }
- if (!isset($data['minpower'])) {
- $data['minpower'] = round($batterypower,2);
- }else{
- $data['minpower'] = $batterypower < $data['minpower'] ? round($batterypower,2) : round($data['minpower'],2);
- }
- }
- $list = $temp;
- }elseif ($type == 2) { // 太阳能板
- $sql = 'select SI.panelvoltage as solarvoltage,SI.panelcurrent as solarcurrent,SI.panelpower as solarpower,SI.updatetime from realtime_info_log as SI where SI.lampid = '.$lampId.' and SI.updatetime >= "'.$beginDate.'" and SI.updatetime <= "'.$endDate.'"';
- $list = $this->db->query($sql)->result_array();
- $temp = [];
- foreach ($list as $value) {
- $temp['solarvoltage'][] = $value['solarvoltage'];
- $temp['solarcurrent'][] = $value['solarcurrent'];
- $temp['solarpower'][] = $value['solarpower'];
- $temp['updatetime'][] = date_change($value['updatetime'],8,$timezone);
- if (!isset($data['maxcurrent'])) {
- $data['maxcurrent'] = $value['solarcurrent'];
- }else{
- $data['maxcurrent'] = $value['solarcurrent'] > $data['maxcurrent'] ? $value['solarcurrent'] : $data['maxcurrent'];
- }
- if (!isset($data['mincurrent'])) {
- $data['mincurrent'] = $value['solarcurrent'];
- }else{
- $data['mincurrent'] = $value['solarcurrent'] < $data['mincurrent'] ? $value['solarcurrent'] : $data['mincurrent'];
- }
- if (!isset($data['maxvoltage'])) {
- $data['maxvoltage'] = $value['solarvoltage'];
- }else{
- $data['maxvoltage'] = $value['solarvoltage'] > $data['maxvoltage'] ? $value['solarvoltage'] : $data['maxvoltage'];
- }
- // if (!isset($data['maxdaydischargemincurrent'])) {
- // $data['maxdaydischargemincurrent'] = $value['daydischargemincurrent'];
- // }else{
- // $data['maxdaydischargemincurrent'] = $value['daydischargemincurrent'] > $data['maxdaydischargemincurrent'] ? $value['daydischargemincurrent'] : $data['maxdaydischargemincurrent'];
- // }
- if (!isset($data['minvoltage'])) {
- $data['minvoltage'] = $value['solarvoltage'];
- }else{
- $data['minvoltage'] = $value['solarvoltage'] < $data['minvoltage'] ? $value['solarvoltage'] : $data['minvoltage'];
- }
- if (!isset($data['maxpower'])) {
- $data['maxpower'] = $value['solarpower'];
- }else{
- $data['maxpower'] = $value['solarpower'] > $data['maxpower'] ? $value['solarpower'] : $data['maxpower'];
- }
- if (!isset($data['minpower'])) {
- $data['minpower'] = $value['solarpower'];
- }else{
- $data['minpower'] = $value['solarpower'] < $data['minpower'] ? $value['solarpower'] : $data['minpower'];
- }
- if (isset($data['maxdaydischargemincurrent'])) {
- $h = floor($data['maxdaydischargemincurrent'] / 3600);
- $h = $h < 10 ? '0'.$h : $h;
- $m = floor($data['maxdaydischargemincurrent'] / 3600 % 60);
- $m = $m < 10 ? '0'.$m : $m;
- $data['maxdaydischargemincurrent'] = $h.':'.$m;
- }
- }
- $list = $temp;
- }else{ // 电量
- $sql = 'select daychargeah,daydischarah,updatetime from battery_info_log where lampid = '.$lampId.' and updatetime >= "'.$beginDate.'" and updatetime <= "'.$endDate.'"';
- $list = $this->db->query($sql)->result_array();
- $temp = [];
- foreach ($list as $value) {
- $temp['daychargeah'][] = $value['daychargeah'];
- $temp['daydischarah'][] = $value['daydischarah'];
- $temp['updatetime'][] = date_change($value['updatetime'],8,$timezone);
- if (!isset($data['maxdaychargeah'])) {
- $data['maxdaychargeah'] = $value['daychargeah'];
- }else{
- $data['maxdaychargeah'] = $value['daychargeah'] > $data['maxdaychargeah'] ? $value['daychargeah'] : $data['maxdaychargeah'];
- }
- if (!isset($data['mindaychargeah'])) {
- $data['mindaychargeah'] = $value['daychargeah'];
- }else{
- $data['mindaychargeah'] = $value['daychargeah'] < $data['mindaychargeah'] ? $value['daychargeah'] : $data['mindaychargeah'];
- }
- if (!isset($data['maxdaydischarah'])) {
- $data['maxdaydischarah'] = $value['daydischarah'];
- }else{
- $data['maxdaydischarah'] = $value['daydischarah'] > $data['maxdaydischarah'] ? $value['daydischarah'] : $data['maxdaydischarah'];
- }
- if (!isset($data['mindaydischarah'])) {
- $data['mindaydischarah'] = $value['daydischarah'];
- }else{
- $data['mindaydischarah'] = $value['daydischarah'] < $data['mindaydischarah'] ? $value['daydischarah'] : $data['mindaydischarah'];
- }
- }
- $list = $temp;
- }
- if (empty($list)) {
- $data['list'] = new StdClass();
- }else{
- $data['list'] = $list;
- }
- exit(json_result('0000',$this->response['0000'],$data));
- }
- }
|