12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790 |
- package com.welampiot.controller;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.welampiot.common.BaseResult;
- import com.welampiot.common.InterfaceResultEnum;
- import com.welampiot.configuration.*;
- import com.welampiot.dto.*;
- import com.welampiot.service.*;
- import com.welampiot.utils.AESUtils;
- import com.welampiot.utils.JwtUtil;
- import com.welampiot.utils.ToolUtils;
- import com.welampiot.vo.*;
- import org.apache.commons.lang3.ArrayUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.util.DigestUtils;
- import org.springframework.web.bind.annotation.CrossOrigin;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- import javax.servlet.http.HttpServletRequest;
- import java.io.IOException;
- import java.nio.charset.StandardCharsets;
- import java.text.DecimalFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import java.util.stream.Collectors;
- @RestController
- @CrossOrigin
- @RequestMapping("/lamp")
- public class LampController {
- @Autowired
- private LampService lampService;
- @Autowired
- private ToolUtils toolUtils;
- @Autowired
- private NetworkService networkService;
- @Autowired
- private GroupService groupService;
- @Autowired
- private GlobalLocationService globalLocationService;
- @Autowired
- private SectionService sectionService;
- @Autowired
- private SolarDevService solarDevService;
- @Autowired
- private SolarInfoLogService solarInfoLogService;
- @Autowired
- private LampPoleService lampPoleService;
- @Autowired
- private QuHuiPlc quHuiPlc;
- @Autowired
- private ZhongLightPlc zhongLightPlc;
- @Autowired
- private AESUtils aesUtils;
- @Autowired
- private PlcAddressService plcAddressService;
- @Autowired
- private FreqMapConfig freqMapConfig;
- @Autowired
- private JwtUtil jwtUtil;
- @Autowired
- private LastSqlInterceptor lastSqlInterceptor; //获取sql语句
- /**
- * 灯控列表
- * @param request
- * @return
- */
- @RequestMapping(value = "/getlist",method = RequestMethod.POST)
- public BaseResult<InfoResponseVO> getList(HttpServletRequest request){
- Integer version = request.getParameter("version") == null ? 0 : Integer.parseInt(request.getParameter("version"));
- Integer page = request.getParameter("page") == null ? 1 : Integer.parseInt(request.getParameter("page"));
- Integer count = request.getParameter("count") == null ? 16 : Integer.parseInt(request.getParameter("count"));
- Integer online = request.getParameter("online") == null || request.getParameter("online") == "" ? null : Integer.parseInt(request.getParameter("online"));
- Integer light_status = request.getParameter("light_status") == null || request.getParameter("light_status") == "" ? null : Integer.parseInt(request.getParameter("light_status"));
- String keyword = request.getParameter("keyword") == null ? "" : request.getParameter("keyword");
- Integer limit = count;
- Integer offset = (page-1)*count;
- List sectionList = toolUtils.getSectionList(request);
- LampListResponseVO lampListResponseVO = new LampListResponseVO();
- if (keyword.length() > 0) lampListResponseVO.setKeyword(keyword);
- lampListResponseVO.setSectionList(sectionList);
- lampListResponseVO.setVersion(version);
- lampListResponseVO.setLimit(limit);
- lampListResponseVO.setOffset(offset);
- if (online != null) lampListResponseVO.setOnline(online);
- if (light_status != null) lampListResponseVO.setLightStatus(light_status);
- List<LampInfoDTO> listByVO = lampService.getListByVO(lampListResponseVO);
- LampListResponseVO lampListResponseVO1 = new LampListResponseVO();
- lampListResponseVO1.setList(listByVO);
- LampCountVO lampCountVO = new LampCountVO();
- lampCountVO.setSectionList(sectionList);
- if (online != null) lampCountVO.setOnlineStatus(online);
- if (light_status != null) lampCountVO.setLampStatus(light_status);
- if (keyword.length() > 0) lampCountVO.setKeyword(keyword);
- Integer countByVO = lampService.getCountByVO(lampCountVO);
- lampListResponseVO1.setTotal(countByVO/count);
- lampListResponseVO1.setTotal2(countByVO);
- lampCountVO = new LampCountVO();
- lampCountVO.setSectionList(sectionList);
- countByVO = lampService.getCountByVO(lampCountVO);
- lampListResponseVO1.setDevTotal(countByVO);
- lampCountVO.setOnlineStatus(1);
- countByVO = lampService.getCountByVO(lampCountVO);
- lampListResponseVO1.setOnlineCount(countByVO);
- lampCountVO.setLampStatus(1);
- countByVO = lampService.getCountByVO(lampCountVO);
- lampListResponseVO1.setLightCount(countByVO);
- lampCountVO.setAlarmStatus(1);
- lampListResponseVO1.setAlarmCount(countByVO);
- return BaseResult.success(lampListResponseVO1);
- }
- /**
- * 灯控详情
- * @param request
- * @return
- */
- @RequestMapping(value = "/details",method = RequestMethod.POST)
- public BaseResult<LampInfoDTO> details(HttpServletRequest request){
- Integer version = request.getParameter("version") == null ? 0 : Integer.parseInt(request.getParameter("version"));
- Integer lampId = request.getParameter("lampId") == null ? 0 : Integer.parseInt(request.getParameter("lampId"));
- LampInfoDTO detailsById = lampService.getDetailsById(lampId,version);
- return BaseResult.success(detailsById);
- }
- @RequestMapping(value = "/getkk", method = RequestMethod.POST)
- public Integer kk(HttpServletRequest request) {
- toolUtils.addOpertaionLog(request,null,null,null,null,null,null,null,null,null,null,null,null,null);
- return 0;
- }
- /**
- * 添加编辑灯控
- * @param request
- * @return
- */
- @RequestMapping(value = "/save",method = RequestMethod.POST)
- @Transactional
- public BaseResult save(HttpServletRequest request){
- Integer version = request.getParameter("version") == null ? 0 : Integer.parseInt(request.getParameter("version"));
- Integer lampId = request.getParameter("lampId") == null || request.getParameter("lampId").length() == 0? 0 : Integer.parseInt(request.getParameter("lampId"));
- String area = request.getParameter("areaId");// == null ? 0 : Integer.parseInt(request.getParameter("areaId"));
- if (area == null || area.equals("0")) return toolUtils.response(InterfaceResultEnum.LACK_AREA_ERROR,version);
- Integer areaId = Integer.parseInt(area);
- String section = request.getParameter("sectionId"); // == null ? 0 : Integer.parseInt(request.getParameter("sectionId"));
- if (section == null || section.equals("0")) return toolUtils.response(InterfaceResultEnum.LACK_SECTION_ERROR,version);
- Integer sectionId = Integer.parseInt(section);
- String protocol = request.getParameter("protocolType");// == null ? 0 : Integer.parseInt(request.getParameter("protocolType"));
- if (protocol == null) return toolUtils.response(InterfaceResultEnum.LACK_PROTOCOL_TYPE_ERROR,version);
- Integer protocolType = Integer.parseInt(protocol);
- String control = request.getParameter("controlType");// == null ? 0 : Integer.parseInt(request.getParameter("controlType"));
- if (control == null) return toolUtils.response(InterfaceResultEnum.LACK_LOOP_CONTROL_TYPE_ERROR,version);
- Integer controlType = Integer.parseInt(control);
- Integer power = request.getParameter("power") == null ? 0 : Integer.parseInt(request.getParameter("power"));
- String installDate = request.getParameter("installDate") == null ? "" : request.getParameter("installDate");
- String expirationDate = request.getParameter("expirationDate") == null ? "" : request.getParameter("expirationDate");
- String number = request.getParameter("number");
- if (number == null || number.length() == 0) return toolUtils.response(InterfaceResultEnum.LIGHT_NUMBER_ERROR,version);
- if (number.length() == 1 && number.equals("0")) return toolUtils.response(InterfaceResultEnum.LIGHT_NUMBER_ZERO_ERROR,version);
- if (number.length() > 6) return toolUtils.response(InterfaceResultEnum.LIGHT_NUMBER_LENGTH_ERROR,version);
- String regex = "^[A-Za-z0-9_]+$";
- if (number.matches(regex) == false) return toolUtils.response(InterfaceResultEnum.LIGHT_NUMBER_NAME_ERROR,version);
- if (number.length() < 6){
- //初全长度为6
- number = "000000"+number;
- number = number.substring(number.length() - 6,number.length());
- }
- String longitude = request.getParameter("longitude") == null || request.getParameter("longitude").length() == 0 ? "0" : request.getParameter("longitude");
- String latitude = request.getParameter("latitude") == null || request.getParameter("latitude").length() == 0 ? "0" : request.getParameter("latitude");
- String name = request.getParameter("name");
- String sn = request.getParameter("sn");
- if (sn == null || sn.length() == 0) return toolUtils.response(InterfaceResultEnum.LACK_ADDRESS_ERROR,version);
- sn = sn.trim();//去除两边的空格
- String devAddr = request.getParameter("devAddr");
- Integer bindStatus = 1;
- if (protocolType == 11 && (controlType == 4 || controlType == 8) ){
- if ( devAddr == null || devAddr.length() == 0) return toolUtils.response(InterfaceResultEnum.LACK_LIGHT_SERIAL_Number_ERROR,version);
- if (Integer.parseInt(devAddr) < 10) return toolUtils.response(InterfaceResultEnum.LIGHT_SERIAL_Number_RANGE_ERROR,version);
- bindStatus = 0;
- }
- String freqId = request.getParameter("freqId");
- if (protocolType == 6 && freqId == null) return toolUtils.response(InterfaceResultEnum.LACK_FREQUENCY_ERROR,version);
- String loopNumber = request.getParameter("loopNumber");
- if (protocolType == 5){
- //zigbee灯具所属的回路
- if (loopNumber == null || loopNumber.equals("0")) return toolUtils.response(InterfaceResultEnum.LACK_ZIGBEE_LOOP_NUMBER_ERROR,version);
- }
- String macAddress = request.getParameter("macAddress");
- if (protocolType == 11 || protocolType == 5){
- //macAddress的值不能为空
- if (macAddress == null) return toolUtils.response(InterfaceResultEnum.LACK_PLC_MACADDRESS__ERROR,version);
- }
- if (controlType == 26 || controlType == 27){
- if (macAddress != null && sn != null){
- bindStatus = 0;
- //微自然plc
- this.savePlcAddress(macAddress,sn);
- }
- }
- String groupId = request.getParameter("groupId");
- String lampPoleId = request.getParameter("lampPoleId");
- Date day = new Date();
- SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String updateTime = sdf.format(day);
- Integer userId = toolUtils.getUser(request).getId();
- if (lampId == 0){ // 添加
- //同一个路段下不能有相同的编号
- LampInfoDTO lampInfo = new LampInfoDTO();
- lampInfo.setSectionId(sectionId);
- lampInfo.setNumber(number);
- //若两灯的,只添加其中一个的就要判断了
- // Integer byVO = lampService.findByVO(lampInfo);
- // if (byVO > 0) return toolUtils.response(InterfaceResultEnum.NUMBER_EXIST_ERROR,version);
- lampInfo.setSectionId(null);
- lampInfo.setNumber(null);
- lampInfo.setAddress(sn);
- NetworkDTO networkData = new NetworkDTO();
- //同一个plc下不允许有相同的序号/相同灯的地址
- Integer header = 0; //灯头:0代表没,1代表灯头1,2代表灯头2
- Integer plcNetCount = 0; //网关, 0:没有创建 ,1: 创建过了
- if (protocolType == 11 && controlType != 26 && controlType != 27){
- String otherDevAddr = ""; //另外一个
- lampInfo.setMacAddress(macAddress);
- if (controlType == 4 || controlType == 11 || controlType == 12 || controlType == 14 ){
- //同一个路段下编号不允相同
- LampInfoDTO newLamp = new LampInfoDTO();
- newLamp.setNumber(number);
- newLamp.setSectionId(sectionId);
- Integer byVO = lampService.findByVO(newLamp);
- if (byVO > 0) return toolUtils.response(InterfaceResultEnum.NUMBER_EXIST_ERROR,version);
- //单灯的情况下是不允许有重复地址的
- //灯地址
- Integer macAddressTotal = lampService.checkData(lampInfo);
- if (macAddressTotal > 0) return toolUtils.response(InterfaceResultEnum.PLC_ADDRESS_EXIST_ERROR,version);
- } else if (controlType == 8) {
- //双灯的情况,检查是否有两个了,若有则不能重复,若没有,则要知道有了哪个灯头
- List<LampInfoDTO> dtoList = lampService.getListByDto(lampInfo);
- if (dtoList != null && dtoList.size() == 2){
- //已经存在
- return toolUtils.response(InterfaceResultEnum.PLC_ADDRESS_EXIST_ERROR,version);
- }else {
- //不存在或只存在一个灯头
- if (dtoList != null && dtoList.size() == 1){
- //存在一个灯头
- header = dtoList.get(0).getMode() + 1;
- otherDevAddr = dtoList.get(0).getDevAddr();
- }else {
- //同一个路段下编号不允相同
- LampInfoDTO newLamp = new LampInfoDTO();
- newLamp.setNumber(number);
- newLamp.setSectionId(sectionId);
- Integer byVO = lampService.findByVO(newLamp);
- if (byVO > 0) return toolUtils.response(InterfaceResultEnum.NUMBER_EXIST_ERROR,version);
- }
- }
- }
- //灯序号
- if (header == 0){
- lampInfo.setMacAddress(null);
- lampInfo.setDevAddr(devAddr);
- Integer devAddrTotal = lampService.checkData(lampInfo);
- if (devAddrTotal > 0) return toolUtils.response(InterfaceResultEnum.PLC_SERIAL_NUMBER_EXIST_ERROR,version);
- lampInfo.setMacAddress(macAddress);
- }else {
- //双灯只添加其中一个
- if (!devAddr.equals(otherDevAddr)){
- //跟原来的不一样
- return toolUtils.response(InterfaceResultEnum.PLC_SERIAL_NUMBER_SAME__ERROR,version);
- }else {
- lampInfo.setDevAddr(devAddr);
- }
- }
- }else {
- //同一个路段下编号不允相同
- LampInfoDTO newLamp = new LampInfoDTO();
- newLamp.setNumber(number);
- newLamp.setSectionId(sectionId);
- Integer byVO = lampService.findByVO(newLamp);
- if (byVO > 0) return toolUtils.response(InterfaceResultEnum.NUMBER_EXIST_ERROR,version);
- }
- if (protocolType == 13 || controlType == 26 || controlType == 27){
- lampInfo.setMacAddress(macAddress);
- //lampInfo.setAddress(sn);
- Integer devAddrTotal = lampService.checkData(lampInfo);
- if (devAddrTotal > 0) return toolUtils.response(InterfaceResultEnum.PLC_ADDRESS_EXIST_ERROR,version);
- }
- if (protocolType == 5){
- //zigbee设备先判断有没有添加网关
- //NetworkDTO network = new NetworkDTO();
- networkData.setDeviceSn(sn);
- Integer networkTotal = networkService.checkNetworkData(networkData);
- if (networkTotal == 0) return toolUtils.response(InterfaceResultEnum.LACK_PLC_ADDRESS__ERROR,version);
- //获取网关id
- NetworkDTO network = networkService.getSimpleData(networkData);
- lampInfo.setNetworkId(network.getId());
- }
- Integer checkAddressTotal = lampService.checkData(lampInfo);
- if (protocolType != 11 && protocolType != 5 && protocolType != 13 && checkAddressTotal > 0){
- LampInfoDTO dataByVO = lampService.getDataByVO(lampInfo);
- //section
- SectionDTO sectionDTO = new SectionDTO();
- sectionDTO.setId(dataByVO.getSectionId());
- sectionDTO= sectionService.getDataByVO(sectionDTO);
- //location
- GlobalLocationDTO areaData = new GlobalLocationDTO();
- areaData.setId(sectionDTO.getPid());
- areaData = globalLocationService.getDataByDto(areaData);
- String chinaMsg = "无线模块地址已存在,区域: "+areaData.getChineseName()+"-->路段:"+sectionDTO.getName()+"-->路灯编号:"+dataByVO.getNumber();
- String engMsg = "The wireless address already exists,area: "+areaData.getEnglishName()+"-->section:"+sectionDTO.getName()+"-->number:"+dataByVO.getNumber();
- String ruMsg = "Беспроводной адрес уже существует,область: "+areaData.getRuName()+"-->участок дороги:"+sectionDTO.getName()+"-->Номер фонаря:"+dataByVO.getNumber();
- if (version == 0){
- String code = "0403";
- if (version == 0){
- BaseResult<Object> objectBaseResult = new BaseResult<>(code, chinaMsg, new Object());
- return objectBaseResult;
- } else if (version == 1) {
- BaseResult<Object> objectBaseResult = new BaseResult<>(code, engMsg, new Object());
- return objectBaseResult;
- }else {
- BaseResult<Object> objectBaseResult = new BaseResult<>(code, ruMsg, new Object());
- return objectBaseResult;
- }
- }
- }
- lampInfo.setBindStatus(bindStatus);
- //String netAddress = "";
- if (protocolType == 0 || protocolType == 3 || protocolType == 4 || protocolType == 5 || protocolType == 6 || protocolType == 7 || protocolType == 8 || protocolType == 9 || protocolType == 10 || protocolType == 11 || protocolType == 13){
- lampInfo.setNetType(1);
- //直连设备
- if (protocolType == 11 || protocolType == 5){
- //netAddress = sn;
- //plc下有单灯控制器和双灯控制器
- lampInfo.setBindStatus(0);
- }else {
- sn = sn.length() == 13 ? String.valueOf(Integer.parseInt(sn,10)) : sn;
- }
- //networkData = new NetworkDTO();
- networkData.setDeviceSn(sn);
- NetworkDTO simpleData = networkService.getSimpleData(networkData);
- //zigbee设备 plc
- if (protocolType == 5 && controlType == 13){
- //先添加控制器,再添加灯具
- lampInfo.setMacAddress(macAddress);
- //灯地址
- Integer macAddressTotal = lampService.checkData(lampInfo);
- if (macAddressTotal > 0) return toolUtils.response(InterfaceResultEnum.PLC_ADDRESS_EXIST_ERROR,version);
- Object lightObject = this.createCtrollLight(macAddress,simpleData.getUId(),Integer.parseInt(loopNumber),longitude,latitude,name);
- if (lightObject == null){
- return toolUtils.response(InterfaceResultEnum.CREATE_LIGHT_ERROR,version);
- }else {
- Integer times = ((ZhongLightPlcVO)lightObject).getTimes();
- Integer errorCode = ((ZhongLightPlcVO)lightObject).getErrorCode();
- if (errorCode > 0){
- if (times == 3) return toolUtils.response(InterfaceResultEnum.CREATE_CONTROL_ERROR,version);
- if (times == 4) return toolUtils.response(InterfaceResultEnum.CREATE_CONTROL_LIGHT_ERROR,version);
- }else {
- //成功
- lampInfo.setLampCtrId(((ZhongLightPlcVO)lightObject).getLampCtrlId());
- lampInfo.setUid(((ZhongLightPlcVO)lightObject).getUid());
- lampInfo.setBindStatus(1);
- }
- }
- }
- if (protocolType != 11 && protocolType != 13 && protocolType != 5 && simpleData != null){
- lampInfo.setAddress(null);
- lampInfo.setNetworkId(simpleData.getId());
- LampInfoDTO lamp = lampService.getDataByVO(lampInfo);
- if (lamp != null){
- networkService.deleteNetworkData(simpleData.getId());
- }
- lampInfo.setAddress(sn);
- }
- // plc只创建一个网络就可以了
- if (simpleData != null){
- if (protocolType == 11 && (controlType != 26 && controlType != 27)){
- plcNetCount = 1;
- }
- }
- networkData.setGatewayType("direct");
- networkData.setProtocolType(protocolType);
- networkData.setAreaId(areaId);
- networkData.setSectionId(sectionId);
- if (protocolType == 10) networkData.setDeviceType(4);
- if (protocolType == 8 && (controlType == 29 || controlType == 33 || controlType == 19)) networkData.setDeviceType(4);
- if (protocolType == 3 && controlType == 9) networkData.setDeviceType(4);
- if (controlType == 16 || controlType == 20) networkData.setDeviceType(5);
- networkData.setNetworkName(sn);
- networkData.setNetType(1);
- networkData.setCreateTime(updateTime);
- networkData.setUserId(userId);
- if (protocolType == 6) this.addDevice(sn,freqId);
- if (protocolType == 7){
- //添加onenet
- String deviceOnenet = toolUtils.addDeviceOnenet(sn);
- if (deviceOnenet != null && deviceOnenet.length() != 0){
- JSONObject onenetObject = JSON.parseObject(deviceOnenet);
- JSONObject data = onenetObject.getJSONObject("data");
- String onenetId = data.getString("device_id");
- if (onenetId != null && onenetId.length() != 0){
- lampInfo.setDeviceId(onenetId);
- networkData.setDeviceId(onenetId);
- }
- }
- }
- }
- if (protocolType == 10 && (controlType == 7 || controlType == 6 || controlType == 15 || controlType == 19 || controlType == 21 || controlType == 30 || controlType == 31 || controlType == 32 || controlType == 35)) lampInfo.setMode(1);
- if (protocolType == 8 && (controlType == 19 || controlType == 23 || controlType == 33 || controlType == 37)) lampInfo.setMode(1);
- if (protocolType == 11 && controlType != 26 && controlType != 27){
- if (plcNetCount == 0){
- if (controlType != 4 && controlType != 8){
- //广东启慧 - plc
- String url = "concentrator/deviceRegister";
- JSONObject qihuiObject = new JSONObject();
- qihuiObject.put("deviceId",sn);
- //添加网关
- String result = this.quiHuiPlcDevice(url, qihuiObject, 4);
- if (result == null || result.length() == 0){
- return toolUtils.response(InterfaceResultEnum.SEND_CMD_TIME_OUT,version);
- }else {
- JSONObject parseObject = JSON.parseObject(result);
- Integer code = parseObject.getInteger("code");
- if (code == 200){
- networkService.add(networkData);
- lampInfo.setNetworkId(networkData.getId());
- } else {
- String msg = parseObject.getString("msg");
- BaseResult<Object> objectBaseResult = new BaseResult<>(code.toString(), msg, new Object());
- return objectBaseResult;
- }
- }
- }else {
- //力合微 plc
- networkService.add(networkData);
- lampInfo.setNetworkId(networkData.getId());
- }
- }else {
- NetworkDTO network = networkService.getSimpleData(networkData);
- lampInfo.setNetworkId(network.getId());
- }
- } else {
- if (protocolType == 1){
- networkData.setGatewayType("direct");
- networkData.setProtocolType(protocolType);
- networkData.setAreaId(areaId);
- networkData.setSectionId(sectionId);
- networkData.setDeviceSn(sn);
- networkData.setNetworkName(sn);
- networkData.setNetType(1);
- networkData.setCreateTime(updateTime);
- }
- networkService.add(networkData);
- lampInfo.setNetworkId(networkData.getId());
- }
- SectionDTO sectionDTO = new SectionDTO();
- sectionDTO.setId(sectionId);
- sectionDTO = sectionService.getDataByVO(sectionDTO);
- GlobalLocationDTO areaDTO = new GlobalLocationDTO();
- areaDTO.setId(sectionDTO.getPid());
- areaDTO = globalLocationService.getDataByDto(areaDTO);
- lampInfo.setCreateTime(updateTime);
- //添加plc控制器节点 - 启慧
- if (protocolType == 11 && (controlType == 11 || controlType == 12 || controlType == 14)) {
- String url = "childnode/deviceRegister";
- String nodeType = this.selectNodeType(power);
- if (nodeType == null || nodeType.length() == 0) return toolUtils.response(InterfaceResultEnum.POWER_SELECT_ERROR,version);
- JSONObject qiObject = new JSONObject();
- if (groupId != null && groupId.length() != 0){
- String[] groupIds = groupId.split(",");
- List<GroupDTO> list = groupService.getListByIds(groupIds);
- //有分组 (只有: 1- 255)
- for (GroupDTO item:list) {
- Integer groupNumber = Integer.parseInt(item.getNumber());
- if (groupNumber < 1 || groupNumber > 255){
- //分组没有在规定的范围内
- return toolUtils.response(InterfaceResultEnum.GROUP_NUMBER_RAND_ERROR,version);
- }else {
- //注意:nodeId是12位数的,不够时,前面补0
- String nodeId = "";
- String zero = "";
- if (macAddress.length() <= 12){
- for (int i = 0; i < 12 - macAddress.length();i++){
- zero += "0";
- }
- nodeId = zero+macAddress;
- }else {
- //大于12位数了
- return toolUtils.response(InterfaceResultEnum.FILL_LAMP_ADDRESS__ERROR,version);
- }
- qiObject.put("nodeId",nodeId);
- qiObject.put("nodeType",nodeType);
- qiObject.put("deviceId",sn);
- qiObject.put("groupNum",groupNumber);
- }
- }
- }else {
- //没有分组的
- //注意:nodeId是12位数的,不够时,前面补0
- String nodeId = "";
- String zero = "";
- if (macAddress.length() <= 12){
- for (int i = 0; i < 12 - macAddress.length();i++){
- zero += "0";
- }
- nodeId = zero+macAddress;
- }else {
- //大于12位数了
- return toolUtils.response(InterfaceResultEnum.FILL_LAMP_ADDRESS__ERROR,version);
- }
- qiObject.put("nodeId",nodeId);
- qiObject.put("nodeType",nodeType);
- qiObject.put("deviceId",sn);
- }
- String respone = this.quiHuiPlcDevice(url, qiObject, 4);
- if (respone == null || respone.length() == 0){
- return toolUtils.response(InterfaceResultEnum.SEND_CMD_TIME_OUT,version);
- }else {
- JSONObject parseObject = JSON.parseObject(respone);
- Integer code = parseObject.getInteger("code");
- if (code != 200){
- String msg = parseObject.getString("msg");
- BaseResult<Object> objectBaseResult = new BaseResult<>(code.toString(), msg, new Object());
- return objectBaseResult;
- }else {
- lampInfo.setBindStatus(1);
- }
- }
- }
- lampInfo.setControlType(controlType);
- lampInfo.setRatedPower(power);
- lampInfo.setLongitude(Float.valueOf(longitude));
- lampInfo.setLatitude(Float.valueOf(latitude));
- lampInfo.setAreaId(areaId);
- lampInfo.setSectionId(sectionId);
- lampInfo.setAddress(sn);
- lampInfo.setNumber(number);
- lampInfo.setName(name);
- if (protocolType == 11 && controlType == 8 && header > 0){
- //添加其中一个灯头
- if (header == 1){//灯头1
- //设置灯头2
- lampInfo.setMode(1);
- }else {//灯头2
- //设置灯头1
- lampInfo.setMode(0);
- }
- lampService.addByDto(lampInfo);
- }else {
- lampService.addByDto(lampInfo);
- }
- //修改了 4G 双控 plc 双控 nb 双控 蓝牙双灯
- if (protocolType == 10 && (controlType == 5 || controlType == 22 || controlType == 34) || (protocolType == 11 && controlType == 8 && header == 0) || (protocolType == 3 && controlType == 9) || controlType == 25 || controlType == 27 || controlType ==29){
- lampInfo.setMode(1);
- lampService.addByDto(lampInfo);
- }
- String lastSql = lastSqlInterceptor.sqlContent; //插入的sql语句
- if (lampPoleId != null && lampPoleId.length() != 0){
- toolUtils.setPoleType(Integer.parseInt(lampPoleId),0);
- }
- // 添加到分组
- if (groupId != null && groupId.length() != 0){
- String[] groupIdArr = groupId.split(",");
- List<GroupDTO> groupDTOList = groupService.getListByIds(groupIdArr);
- for (GroupDTO item:groupDTOList) {
- String[] lampArr = item.getLampId().split(",");
- //是否包含某个元素
- boolean ifContain = Arrays.asList(lampArr).contains(lampInfo.getId());
- if (ifContain == false){
- //转换成list
- List<String> list = new ArrayList<>(Arrays.asList(lampArr));
- list.add(lampInfo.getId().toString());
- //list数组按规定拼接成字符串
- String newLampId= list.stream().collect(Collectors.joining(","));
- //更新
- item.setLampId(newLampId);
- groupService.updateGroupLampId(item);
- }
- }
- }
- networkData = networkService.getSimpleData(networkData);
- Integer lampCount = networkData.getLampCount();
- if (controlType == 1 || controlType == 3 || controlType == 5 || controlType == 8 || controlType == 9 || controlType == 22){
- //更新lampcount
- //如果两灯就是添加2个
- if (lampCount == null || lampCount == 0){
- networkData.setLampCount(2);
- }else {
- if (header > 0 && controlType == 8){
- //只增加一个
- lampCount = lampCount + 1;
- networkData.setLampCount(lampCount);
- }else {
- lampCount = lampCount + 2;
- networkData.setLampCount(lampCount);
- }
- }
- networkService.updateSimDate(networkData);
- }else {
- //单灯
- //如果两灯就是添加1个
- if (protocolType != 11 && protocolType != 5){
- networkData.setLampCount(1);
- networkService.updateSimDate(networkData);
- }else if((protocolType == 11 || protocolType == 5) && controlType != 26 && controlType != 27) {
- //plc可以带很多灯 ,//zigbee也是带多个灯
- if (lampCount == null || lampCount == 0){
- networkData.setLampCount(1);
- }else {
- lampCount = lampCount + 1;
- networkData.setLampCount(lampCount);
- }
- networkService.updateSimDate(networkData);
- }
- }
- String cRemark = "添加路灯 区域:"+areaDTO.getChineseName()+"->路段:"+sectionDTO.getName()+"->路灯编号:"+number+" id:"+lampInfo.getId();
- String eRemark = "Add lamp area:"+areaDTO.getEnglishName()+"->section:"+sectionDTO.getName()+"->number:"+number+" id:"+lampInfo.getId();
- String rRemark = "Добавить зону светильников:"+areaDTO.getRuName()+"->участок дороги:"+sectionDTO.getName()+"->нумерация:"+number+" id:"+lampInfo.getId();
- toolUtils.addOpertaionLog(request, "添加", cRemark, 0, 1, 1, number, areaDTO.getChineseName(), sectionDTO.getName(), sn, sectionId, sectionDTO.getId(), lampInfo.getId(), lastSql);
- toolUtils.addOpertaionLog(request, "Insert", eRemark, 1,1,1, number, areaDTO.getEnglishName(), sectionDTO.getName(), sn, sectionId, sectionDTO.getId(), lampInfo.getId(), lastSql);
- toolUtils.addOpertaionLog(request,"Подкючено",rRemark,2,1,1,number,areaDTO.getRuName(),sectionDTO.getName(),sn,sectionId,sectionDTO.getId(), lampInfo.getId(),lastSql);
- return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
- }else { // 编辑
- // LampInfoDTO oldLamp = lampService.getDetailsById(lampId,version);
- // if (oldLamp.getProtocolType() != protocolType){
- // NetworkDTO networkDTO = new NetworkDTO();
- // networkDTO.setId(oldLamp.getNetworkId());
- // networkDTO.setNetworkName(sn);
- // networkDTO.setDeviceSn(sn);
- // networkDTO.setAreaId(areaId);
- // networkDTO.setSectionId(sectionId);
- // if (protocolType == 10) networkDTO.setDeviceType(4);
- // networkDTO.setProtocolType(protocolType);
- // networkService.update(networkDTO);
- // }
- LampInfoDTO lampInfoDTO = new LampInfoDTO();
- lampInfoDTO.setId(lampId);
- lampInfoDTO.setNumber(number);
- lampInfoDTO.setSectionId(sectionId);
- lampInfoDTO.setName(name);
- // lampInfoDTO.setNetworkId(oldLamp.getNetworkId());
- lampInfoDTO.setLatitude(Float.valueOf(latitude));
- lampInfoDTO.setLongitude(Float.valueOf(longitude));
- lampInfoDTO.setNumber(number);
- lampInfoDTO.setSn(sn);
- lampInfoDTO.setAreaId(areaId);
- lampInfoDTO.setControlType(controlType);
- lampInfoDTO.setProtocolType(protocolType);
- lampInfoDTO.setInstallDate(installDate);
- lampInfoDTO.setExpirationDate(expirationDate);
- lampInfoDTO.setRatedPower(power);
- return lampService.update(lampInfoDTO);
- }
- }
- //微自然的plc
- private void savePlcAddress(String netAddress,String lampAddress){
- PlcAddressDTO plcAddressDTO = new PlcAddressDTO();
- plcAddressDTO.setLampAddress(lampAddress);
- plcAddressDTO.setNetAddress(netAddress);
- Integer total = plcAddressService.checkDataByVO(plcAddressDTO);
- if (total == 0){
- PlcAddressDTO result = plcAddressService.getMaxPvid(plcAddressDTO);
- if (result == null){
- plcAddressDTO.setPvid(1);
- }else {
- plcAddressDTO.setPvid(result.getPvid() + 1);
- }
- plcAddressService.insertDataByVO(plcAddressDTO);
- }
- }
- // 添加设备
- protected String addDevice(String deviceId,String freqId){
- Map<String, FreqConfig> maps = freqMapConfig.getMaps();
- FreqConfig freqConfig = maps.get(freqId);
- String deviceProfileID = "";
- String applicationID = "";
- if (freqConfig != null){
- deviceProfileID = freqConfig.getDeviceProfileID();
- applicationID = freqConfig.getApplicationID();
- }
- JSONObject device = new JSONObject();
- device.put("applicationID",applicationID);
- device.put("description",deviceId);
- device.put("devEUI","00000000"+deviceId);
- device.put("deviceProfileID",deviceProfileID);
- device.put("name",deviceId);
- device.put("referenceAltitude",0);
- device.put("skipFCntCheck",true);
- device.put("tags",new Object());
- device.put("variables",new Object());
- JSONObject data = new JSONObject();
- data.put("device",device);
- String token = jwtUtil.signatureToToken();
- String url = "http://47.112.108.98:8090/api/devices";
- String respone = null;
- try {
- respone = toolUtils.sendHttp("post", url, data.toString(), token, null, 5, null, ToolUtils.ContentTypeEnum.CONTENT_TYPE_JSON,null);
- System.out.println("=====dddd:"+respone);
- if (respone == null){
- respone = jwtUtil.setActivate(deviceId);
- System.out.println("=====再次:"+respone);
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- return null;
- }
- /**
- * 删除灯控
- * @param request
- * @return
- */
- @RequestMapping(value = "/del",method = RequestMethod.POST)
- public BaseResult del(HttpServletRequest request){
- Integer version = request.getParameter("version") == null ? 0 : Integer.parseInt(request.getParameter("version"));
- String lampId = request.getParameter("lampId");
- if (lampId == null || lampId.length() == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
- String[] split = lampId.split(",");
- for (String id :split) {
- int l = Integer.parseInt(id);
- lampService.deleteById(l);
- }
- return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
- }
- /**
- * 灯控调光
- * @param request
- * @return
- */
- @RequestMapping(value = "/dimming",method = RequestMethod.POST)
- public BaseResult dimming(HttpServletRequest request){
- Integer version = request.getParameter("version") == null ? 0 : Integer.parseInt(request.getParameter("version"));
- Integer light = request.getParameter("light") == null ? 0 : Integer.parseInt(request.getParameter("light"));
- Integer isGroup = request.getParameter("isGroup") == null ? 0 : Integer.parseInt(request.getParameter("isGroup"));
- Integer id = request.getParameter("id") == null ? 0 : Integer.parseInt(request.getParameter("id"));
- LampInfoDTO lamp = lampService.getDetailsById(id, version);
- light = light > 100 ? 100 : light;
- Date day = new Date();
- SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String updateTime = sdf.format(day);
- if (lamp.getProtocolType() == 10){ // 4G
- String sendTopic = "/Lamp/TransIn/"+lamp.getSn();
- String backTopic = "/Lamp/TransOut/"+lamp.getSn();
- if (isGroup == 1){
- String cmd;
- if (lamp.getMode() == 0){ // B 路
- cmd = "0110DF090102";
- }else { // A 路
- cmd = "0110DF0B0102";
- }
- if (light == 0){
- cmd += "5F";
- }else {
- cmd += "A0";
- }
- cmd += toolUtils.getCRC(cmd.getBytes());
- String s = toolUtils.sendMqttCmd(sendTopic, "0232"+cmd, backTopic, 10);
- if (s == null || s.length() == 0){
- return toolUtils.response(InterfaceResultEnum.SEND_CMD_TIME_OUT,version);
- }else {
- if (lamp.getMode() == 0){ // A 路
- cmd = "0106DF0A";
- }else { // B 路
- cmd = "0106DF0C";
- }
- String lightStr = "0000"+Integer.toHexString(light).toString();
- lightStr = lightStr.substring(-4);
- cmd += lightStr;
- cmd += toolUtils.getCRC(cmd.getBytes());
- s = toolUtils.sendMqttCmd(sendTopic, "0232"+cmd, backTopic, 10);
- if (s == null || s.length() == 0){
- return toolUtils.response(InterfaceResultEnum.SEND_CMD_TIME_OUT,version);
- }else {
- LampInfoDTO lampInfoDTO = new LampInfoDTO(lamp.getId());
- lampInfoDTO.setLighteness(light);
- lampInfoDTO.setPolicyId(0);
- lampInfoDTO.setPolicyType(0);
- lampService.updateLight(lampInfoDTO);
- return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
- }
- }
- }else {
- String cmd;
- if (lamp.getMode() == 0){ // A 路
- cmd = "0106DF0A";
- }else { // B 路
- cmd = "0106DF0C";
- }
- String lightStr = "0000"+Integer.toHexString(light).toString();
- lightStr = lightStr.substring(-4);
- cmd += lightStr;
- cmd += toolUtils.getCRC(cmd.getBytes());
- String s = toolUtils.sendMqttCmd(sendTopic, "0232"+cmd, backTopic, 10);
- if (s == null || s.length() == 0){
- return toolUtils.response(InterfaceResultEnum.SEND_CMD_TIME_OUT,version);
- }else {
- LampInfoDTO lampInfoDTO = new LampInfoDTO(lamp.getId());
- lampInfoDTO.setLighteness(light);
- lampService.updateLight(lampInfoDTO);
- return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
- }
- }
- } else if (lamp.getProtocolType() == 11) {//plc灯
- if (lamp.getControlType() == 8 || lamp.getControlType() == 4){
- //力合微
- String rthirdId = "LME:"+ lamp.getSn().toLowerCase();
- String thirdId = rthirdId + "-light:"+lamp.getDevAddr();
- Integer lampNumber = lamp.getMode() + 1;
- //当前时间
- String dataType = "adjustLightController";
- //对象
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("thirdId",thirdId);
- jsonObject.put("RthirdId",rthirdId);
- jsonObject.put("lampNumber",lampNumber);
- jsonObject.put("dateTime",updateTime);
- jsonObject.put("dataType",dataType);
- jsonObject.put("brightness",light);
- String objectString = jsonObject.toString();
- //发送命令(获取数据)
- String sendTopic = "LME/streetLight/In/adjustLightController/"+rthirdId;
- String resTopic = "LME/streetLight/Out/lightStatus";
- String s = toolUtils.plcSendMqttCmd(sendTopic, objectString, resTopic, 20);
- if (s == null || s.length() == 0){
- return toolUtils.response(InterfaceResultEnum.SEND_CMD_TIME_OUT,version);
- }
- } else if (lamp.getControlType() == 11 || lamp.getControlType() == 12 || lamp.getControlType() == 14) {
- //广东启慧
- String url = "concentrator/singleLightDimmer";
- String macAddress = lamp.getMacAddress();
- if (lamp.getMacAddress().length() < 12){
- StringBuffer stringBuffer = new StringBuffer();
- for (int i = 0; i < 12 - lamp.getMacAddress().length(); i++){
- stringBuffer.append('0');
- }
- String zero = stringBuffer.toString();
- macAddress = zero+macAddress;
- }
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("deviceId",lamp.getSn());
- jsonObject.put("nodeId",macAddress);
- jsonObject.put("ab",lamp.getMode()+1);
- jsonObject.put("luminance",light);
- //发送命令-调光
- String result = this.quiHuiPlcDevice(url, jsonObject, 4);
- if (result == null || result.length() == 0){
- return toolUtils.response(InterfaceResultEnum.SEND_CMD_TIME_OUT,version);
- }else {
- JSONObject parseObject = JSON.parseObject(result);
- Integer code = parseObject.getInteger("code");
- if (code == 200){
- LampInfoDTO lampInfoDTO = new LampInfoDTO(lamp.getId());
- lampInfoDTO.setLighteness(light);
- lampInfoDTO.setUpdateTime(updateTime);
- lampService.updateLight(lampInfoDTO);
- return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
- } else {
- String msg = parseObject.getString("msg");
- BaseResult<Object> objectBaseResult = new BaseResult<>(code.toString(), msg, new Object());
- return objectBaseResult;
- }
- }
- }
- } else if (lamp.getProtocolType() == 5) {
- if (lamp.getControlType() == 13){
- //中灯的plc,协议是zigbee
- Object zhogMsg = this.zhongDimming(lamp,light);
- if (zhogMsg == null){
- return toolUtils.response(InterfaceResultEnum.LACK_UNKNOWN_ERROR,version);
- }else {
- Integer times = ((ZhongLightPlcVO) zhogMsg).getTimes();
- Integer errorCode = ((ZhongLightPlcVO) zhogMsg).getErrorCode(); //错误码
- Integer succCode = ((ZhongLightPlcVO) zhogMsg).getSuccCode(); //错误码
- if (errorCode > 0){
- //失败
- if (times == 1){
- //获取token返回的错误
- if (errorCode == 1){
- return toolUtils.response(InterfaceResultEnum.PLC_CALL_ERROR,version);
- }else if (errorCode == 2) {
- return toolUtils.response(InterfaceResultEnum.PLC_KEY_ERROR,version);
- }else {
- return toolUtils.response(InterfaceResultEnum.PLC_token_ERROR,version);
- }
- } else if (times == 2) {
- //login时返回的错误
- if (errorCode == 1){
- return toolUtils.response(InterfaceResultEnum.USER_EXISTS_ERROR,version);
- } else if (errorCode == 2) {
- return toolUtils.response(InterfaceResultEnum.USER_DISABLED_ERROR,version);
- }else {
- return toolUtils.response(InterfaceResultEnum.USER_LOGIN_ERROR,version);
- }
- }else {
- //第三次
- if (errorCode == 780){
- return toolUtils.response(InterfaceResultEnum.DEVICE_OFFLINE_ERROR,version);
- } else if (errorCode == 201) {
- return toolUtils.response(InterfaceResultEnum.DEVICE_TIMEOUT_ERROR,version);
- } else if (errorCode == 203) {
- return toolUtils.response(InterfaceResultEnum.DEVICE_CONNECTED_ERROR,version);
- }else {
- return toolUtils.response(InterfaceResultEnum.INVALID_TOKEN_ERROR,version);
- }
- }
- }
- if (succCode == 0 && times == 3 && errorCode == 0){
- //调光成功
- LampInfoDTO lampInfoDTO = new LampInfoDTO(lamp.getId());
- lampInfoDTO.setLighteness(light);
- lampInfoDTO.setUpdateTime(updateTime);
- lampService.updateLight(lampInfoDTO);
- return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
- }
- }
- }
- }
- return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
- }
- private String selectNodeType(Integer power){
- String nodeType = "";
- switch (power){
- case 30:
- nodeType = "01";
- break;
- case 40:
- nodeType = "02";
- break;
- case 50:
- nodeType = "50";
- break;
- case 60:
- nodeType = "06";
- break;
- case 75:
- nodeType = "0A";
- break;
- case 80:
- nodeType = "12";
- break;
- case 90:
- nodeType = "15";
- break;
- case 100:
- nodeType = "19";
- break;
- case 120:
- nodeType = "1D";
- break;
- case 150:
- nodeType = "2C";
- break;
- case 160:
- nodeType = "27";
- break;
- case 180:
- nodeType = "36";
- break;
- case 185:
- nodeType = "38";
- break;
- case 200:
- nodeType = "40";
- break;
- case 240:
- nodeType = "45";
- break;
- case 260:
- nodeType = "48";
- break;
- default:
- nodeType = "";
- }
- return nodeType;
- }
- //中灯 - 调光
- private Object getToken(){
- String url = zhongLightPlc.getZhongUrl()+"query_token";
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("OperatorID",zhongLightPlc.getOperatorID());
- jsonObject.put("OperatorSecret",zhongLightPlc.getOperatorSecret());
- String objectString = jsonObject.toString();
- //aes128位,模式:cbc,填充模式: pkcs5padding,返回是base64
- //hmac-md5加密32位后,小写要转换成大写
- String bodyStr = this.encryptAndMd5(objectString);
- String result = null;
- try {
- result = toolUtils.sendHttp("post", url, bodyStr,3, ToolUtils.ContentTypeEnum.CONTENT_TYPE_JSON,null);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- if (result == null || result.length() == 0){
- return null;
- }else {
- //System.out.println("====result:"+result);
- JSONObject parseObject = JSON.parseObject(result);
- String data = parseObject.getString("Data");
- Integer ret = parseObject.getInteger("Ret");
- //{"Ret": 0, "Msg": "Successfully", "Data": "U/jy5Jq0OPFRB3xFNvLAUcEPg3eZjSSuG9KOKcCtu4/CqJgFI/sWTQyHrdiEcpC2sk0EofcZ3eurM4A7N0LFRbYRaiiiyK9I6vcWh8JyLLSIEUEsGSh66NjcxzdqrQX4LOYducgwxA+KZAh7d9iNIdeMhEpAIxJbM5PcTHwtcjOOygde0lLZdU3QDHvTltFmMh7HNvzd6/quXpYvfQoBMZpR9D81Ly7BaHljFqSzKBGaarW/Xbv6MnReqJo/IMla4l1nKjsjCiEHo2aAOGO0zLgRMzJ/t78tjAxf5V09Amb/M5K6/vw5AvMN/F/0e+fHzzfMEvlJ0RU/nqGpvjGrtFrgwioa6IjnZsB52yBWR2HNHENjDq1nUVs9AQ6hfyJb", "Sig": "7C4A204206F4F99DB8427C90F11A85B3"}
- String dataString = null;
- if (ret == 0){
- //解密
- try {
- //{"OperatorID": "MA5G0DCR3", "SuccStat": 0, "AccessToken": "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJNQTVHMERDUjMiLCJpYXQiOjE2OTY5MzQ1MzEsInN1YiI6InVHeGR0OE5QNURZWStxWUQiLCJleHAiOjE2OTY5NDE3MzJ9.KIgJtlInWxLAyz2WYerbia5LqBVabLh6xB2M3TvRXYU", "TokenAvailableTime": 7200, "FailReason": 0}
- dataString = aesUtils.decrypt(zhongLightPlc.getEncryptKey(),zhongLightPlc.getIv(),data);
- JSONObject dataObject = JSON.parseObject(dataString);
- Integer succStat = dataObject.getInteger("SuccStat");
- Integer failReason = dataObject.getInteger("FailReason");
- ZhongLightPlcVO zhongLightPlcVO = new ZhongLightPlcVO();
- zhongLightPlcVO.setTimes(1);
- zhongLightPlcVO.setErrorCode(failReason);
- if (succStat == 0){
- //成功
- String accessToken = dataObject.getString("AccessToken");
- System.out.println("===eee:"+accessToken);
- zhongLightPlcVO.setToken(accessToken);
- zhongLightPlcVO.setSuccCode(0);
- //return accessToken; //有效期是2个小时
- }else {
- //失败
- //zhongLightPlcVO.setErrorCode(failReason);
- // return failReason.toString();
- // if (failReason == 1){
- // toolUtils.response(InterfaceResultEnum.PLC_CALL_ERROR,0);
- // } else if (failReason == 2) {
- // toolUtils.response(InterfaceResultEnum.PLC_KEY_ERROR,0);
- // }else {
- // toolUtils.response(InterfaceResultEnum.PLC_token_ERROR,0);
- // }
- }
- return zhongLightPlcVO;
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- }
- return null;
- }
- private Object zhongPlcLogin(ZhongLightPlcVO tokenObject){
- String url = zhongLightPlc.getZhongUrl()+"user/v1_0/user_login";
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("UserName",zhongLightPlc.getZhongUserName());
- jsonObject.put("Password",zhongLightPlc.getZhongPassword());
- String objectString = jsonObject.toString();
- //aes128位,模式:cbc,填充模式: pkcs5padding
- //hmac-md5加密后,小写要转换成大写
- String bodyJsonString = this.encryptAndMd5(objectString);
- String respone = null;
- try {
- respone = toolUtils.sendHttp("post",url,bodyJsonString,tokenObject.getToken(),null,4,"Bearer", ToolUtils.ContentTypeEnum.CONTENT_TYPE_JSON,null);
- JSONObject parseObject = JSON.parseObject(respone);
- System.out.println("======ddd:"+parseObject);
- Integer ret = parseObject.getInteger("Ret");
- if (ret == 0){
- //解密
- String data = parseObject.getString("Data");
- // {"Role":1,"SuccStat":0,"Type":"3","Menus":[{"Title":"首页","Children":[{"Title":"首页","Code":"181"}],"Code":"18"},{"Title":"智慧照明","Children":[{"Title":"统计概览","Code":"2c"},{"Title":"基本信息","Code":"22"},{"Title":"实时监控","Code":"23"},{"Title":"实时监控","Code":"2b"},{"Title":"回路监控","Code":"24"},{"Title":"回路监控","Code":"2f"},{"Title":"场景策略","Code":"26"},{"Title":"历史数据","Code":"27"},{"Title":"故障告警","Code":"28"}],"Code":"2"},{"Title":"地图","Children":[{"Title":"地图","Code":"31"}],"Code":"3"},{"Title":"设备","Children":[{"Title":"监控H","Code":"52"},{"Title":"充电桩","Code":"54"},{"Title":"气象","Code":"55"},{"Title":"信息屏X","Code":"5a"},{"Title":"报警X","Code":"5e"},{"Title":"一键告警S","Code":"5x"},{"Title":"广播S","Code":"5d"},{"Title":"NB井盖","Code":"5z"},{"Title":"WiFi-AP","Code":"59"}],"Code":"5"},{"Title":"配置","Children":[{"Title":"参数配置","Code":"43"},{"Title":"网关配置","Code":"44"},{"Title":"策略配置","Code":"49"},{"Title":"项目配置","Code":"45"},{"Title":"首页配置","Code":"4a"},
- // {"Title":"操作记录","Code":"9"}],"Code":"4"}],"ProjectId":"5ecd00d726b0673bf8d0f034","OrganizationId":"5ecd009826b0673bf8d0f032","SessionId":"27BA3664948557E419D6CFD260BD72B4","DivisionId":"5ecd006926b0673bf8d0f02c"}
- String decryptJson = aesUtils.decrypt(zhongLightPlc.getEncryptKey(), zhongLightPlc.getIv(), data);
- JSONObject resultObject = JSON.parseObject(decryptJson);
- //0:成功;1:用户名密码对应用户不存在;2:该用户已被禁用;3~99:自定义
- Integer code = resultObject.getInteger("SuccStat");
- ZhongLightPlcVO loginZhongVO = new ZhongLightPlcVO();
- loginZhongVO.setSuccCode(code);
- loginZhongVO.setErrorCode(code);
- loginZhongVO.setTimes(2);
- if (code == 0){
- //成功
- String projectId = resultObject.getString("ProjectId");
- String organizationId = resultObject.getString("OrganizationId");
- String sessionId = resultObject.getString("SessionId");
- String divisionId = resultObject.getString("DivisionId");
- loginZhongVO.setProjectId(projectId);
- loginZhongVO.setOrganizationId(organizationId);
- loginZhongVO.setSessionId(sessionId);
- loginZhongVO.setDivisionId(divisionId);
- loginZhongVO.setToken(tokenObject.getToken());
- }
- return loginZhongVO;
- }
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- return null;
- }
- private Object zhongDimming(LampInfoDTO lamp,Integer light){
- //获取token
- Object tokenObject = this.getToken();
- if (tokenObject == null ){
- //失败
- return null;
- }else {
- Integer errorCode = ((ZhongLightPlcVO) tokenObject).getErrorCode();
- if (errorCode > 0) return tokenObject; //也是失败的
- }
- //登录
- Object loginObject = this.zhongPlcLogin((ZhongLightPlcVO) tokenObject);
- if (loginObject == null ){
- //失败
- return null;
- }else {
- Integer errorCode = ((ZhongLightPlcVO) loginObject).getErrorCode();
- if (errorCode > 0) return loginObject; //也是失败的
- }
- //开始调光
- String url = zhongLightPlc.getZhongUrl() + "commons/v1_0/send";
- List arrayList = new ArrayList();
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("code",1);
- jsonObject.put("headSerial",1);
- jsonObject.put("ep",1);
- jsonObject.put("gatewayAddr","0x"+lamp.getSn().toLowerCase());
- jsonObject.put("addr","0x"+lamp.getMacAddress());
- jsonObject.put("uid",lamp.getLampCtrId());
- JSONObject controlObject = new JSONObject();
- controlObject.put("devtype",23);
- controlObject.put("bri",light);
- jsonObject.put("control",controlObject);
- arrayList.add(jsonObject);
- JSONObject dataObject = new JSONObject();
- dataObject.put("platform",1);
- dataObject.put("cmds",arrayList);
- String dataObjectString = dataObject.toString();
- //aes128位,模式:cbc,填充模式: pkcs5padding
- //hmac-md5加密后,小写要转换成大写
- String bodyStr = this.encryptAndMd5(dataObjectString);
- String token = ((ZhongLightPlcVO) loginObject).getToken();
- String sessionId = ((ZhongLightPlcVO) loginObject).getSessionId();
- // String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJNQTVHMERDUjMiLCJpYXQiOjE2OTcwMTkwMzQsInN1YiI6InVHeGR0OE5QNURZWStxWUQiLCJleHAiOjE2OTcwMjYyMzV9.AN-Af_x7qmxAEb736fo6bgrcgd02cjXZTB0QYa-oMOc";
- // String sessionId = "25D085219FEBFD8D6587F0B984FF7F4E";
- String respone = null;
- try {
- respone = toolUtils.sendHttp("post",url,bodyStr,token,sessionId,4,"Bearer", ToolUtils.ContentTypeEnum.CONTENT_TYPE_JSON,null);
- System.out.println("=====diming:"+respone);
- JSONObject parseObject = JSON.parseObject(respone);
- Integer ret = parseObject.getInteger("Ret");
- //{"Ret": 0, "Msg": "Successfully", "Data": "/FZGtGawB4O9lTtef3Up4w8rUWVjXvpWXk7P36M792A=", "Sig": "57D14850E6FED20D242D0C958DAE8403"}
- ZhongLightPlcVO zhongLightPlcVO = new ZhongLightPlcVO();
- zhongLightPlcVO.setTimes(3);
- if (ret == 0){
- //成功
- String data = parseObject.getString("Data");
- //解密
- String decryptJson = aesUtils.decrypt(zhongLightPlc.getEncryptKey(), zhongLightPlc.getIv(), data);
- //解释json数组
- JSONArray objects = JSON.parseArray(decryptJson);
- JSONObject resultbject = (JSONObject)objects.get(0);
- Integer resultCode = resultbject.getInteger("result");
- zhongLightPlcVO.setErrorCode(resultCode);//是否调光成功
- zhongLightPlcVO.setSuccCode(0);
- System.out.println("===dd:"+zhongLightPlcVO);
- return zhongLightPlcVO;
- }else if (ret == 4002){
- //Invalid token
- zhongLightPlcVO.setErrorCode(ret);
- zhongLightPlcVO.setSuccCode(ret);
- return zhongLightPlcVO;
- }
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- return null;
- }
- /* 0. 最先要有了网关
- * 1. 先添加控制器
- * 2. 再添加灯具
- */
- //添加设备 - 控制器
- private Object createCtrollLight(String macAddress,String uId,Integer zibeeSectionId,String longitude, String latitude,String name){
- Object tokenObject = this.getToken();
- if (tokenObject == null ){
- //失败
- return null;
- }else {
- Integer errorCode = ((ZhongLightPlcVO) tokenObject).getErrorCode();
- if (errorCode > 0) return tokenObject; //也是失败的
- }
- //登录
- Object loginObject = this.zhongPlcLogin((ZhongLightPlcVO) tokenObject);
- if (loginObject == null ){
- //失败
- return null;
- }else {
- Integer errorCode = ((ZhongLightPlcVO) loginObject).getErrorCode();
- if (errorCode > 0) return loginObject; //也是失败的
- }
- //创建控制器
- String url = zhongLightPlc.getZhongUrl() + "commons/v1_0/create";
- //type: 23(v3控制器)
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("type",23);
- jsonObject.put("gateway_id",uId);
- jsonObject.put("section_id",zibeeSectionId);
- jsonObject.put("division_id",((ZhongLightPlcVO) loginObject).getDivisionId());
- jsonObject.put("organization_id",((ZhongLightPlcVO) loginObject).getOrganizationId());
- jsonObject.put("project_id",((ZhongLightPlcVO) loginObject).getProjectId());
- jsonObject.put("lamp_post_type",20);
- jsonObject.put("lamp_post_number",macAddress);
- JSONObject controlObject = new JSONObject();
- controlObject.put("platform",1);
- controlObject.put("type",3);
- controlObject.put("addr","0x"+macAddress);
- controlObject.put("property",jsonObject);
- String dataObjectString = controlObject.toString();
- //aes128位,模式:cbc,填充模式: pkcs5padding
- //hmac-md5加密后,小写要转换成大写
- String bodyStr = this.encryptAndMd5(dataObjectString);
- //发送请求
- String token = ((ZhongLightPlcVO) loginObject).getToken();
- String sessionId = ((ZhongLightPlcVO) loginObject).getSessionId();
- String respone = null;
- try {
- respone = toolUtils.sendHttp("post", url, bodyStr, token, sessionId, 5,"Bearer", ToolUtils.ContentTypeEnum.CONTENT_TYPE_JSON,null);
- System.out.println("===dd:"+respone);
- JSONObject parseObject = JSON.parseObject(respone);
- Integer ret = parseObject.getInteger("Ret");
- //{"Ret": 0, "Msg": "Successfully", "Data": "/FZGtGawB4O9lTtef3Up4w8rUWVjXvpWXk7P36M792A=", "Sig": "57D14850E6FED20D242D0C958DAE8403"}
- ZhongLightPlcVO zhongLightPlcVO = new ZhongLightPlcVO();
- zhongLightPlcVO.setTimes(3);
- if (ret == 0){
- //成功
- String data = parseObject.getString("Data");
- //解密
- String decryptJson = aesUtils.decrypt(zhongLightPlc.getEncryptKey(), zhongLightPlc.getIv(), data);
- //解释json
- JSONObject resultbject = JSON.parseObject(decryptJson);
- Integer resultCode = resultbject.getInteger("result");
- zhongLightPlcVO.setErrorCode(resultCode);//是否调光成功
- if (resultCode == 0){
- //成功
- zhongLightPlcVO.setSuccCode(0);
- String lampCtrId = resultbject.getString("uid");
- //zhongLightPlcVO.setLampCtrlId(lampCtrId);
- ((ZhongLightPlcVO) loginObject).setLampCtrlId(lampCtrId);
- //创建灯具
- Object lightObject = this.createLight(macAddress, lampCtrId, loginObject, longitude, latitude, name);
- if (lightObject != null){
- ((ZhongLightPlcVO)lightObject).setLampCtrlId(lampCtrId);
- return lightObject;
- }
- }else {
- //创建失败
- return zhongLightPlcVO;
- }
- }
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- return null;
- }
- private Object createLight(String macAddress,String lampCtrId,Object loginObject, String longitude,String latitude,String name){
- String url = zhongLightPlc.getZhongUrl() + "commons/v1_0/create";
- //zigbee设备网关会设置变比和轮询时间,所以与网关设置一样就可以了,poll_interval与formula
- //port_id:是针对双灯控制器的
- //type: 5是zigbee的控制器
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("type",5);
- jsonObject.put("encryption",1);
- jsonObject.put("poll_interval",15);
- jsonObject.put("formula",1);
- jsonObject.put("division_id",((ZhongLightPlcVO) loginObject).getDivisionId());
- jsonObject.put("organization_id",((ZhongLightPlcVO) loginObject).getOrganizationId());
- jsonObject.put("project_id",((ZhongLightPlcVO) loginObject).getProjectId());
- jsonObject.put("longitude",longitude);
- jsonObject.put("latitude",latitude);
- jsonObject.put("port_id",1);
- jsonObject.put("lamp_ctrl_id",((ZhongLightPlcVO) loginObject).getLampCtrlId());
- jsonObject.put("name",name);
- JSONObject dataObject = new JSONObject();
- dataObject.put("platform",1);
- dataObject.put("type",4);
- dataObject.put("addr","0x"+macAddress);
- dataObject.put("property",jsonObject);
- String dataObjectString = dataObject.toString();
- String bodyStr = this.encryptAndMd5(dataObjectString);
- //发送请求
- String token = ((ZhongLightPlcVO) loginObject).getToken();
- String sessionId = ((ZhongLightPlcVO) loginObject).getSessionId();
- String respone = null;
- try {
- respone = toolUtils.sendHttp("post", url, bodyStr, token, sessionId, 5,"Bearer", ToolUtils.ContentTypeEnum.CONTENT_TYPE_JSON,null);
- System.out.println("===light:"+respone);
- JSONObject parseObject = JSON.parseObject(respone);
- Integer ret = parseObject.getInteger("Ret");
- //{"Ret": 0, "Msg": "Successfully", "Data": "/FZGtGawB4O9lTtef3Up4w8rUWVjXvpWXk7P36M792A=", "Sig": "57D14850E6FED20D242D0C958DAE8403"}
- ZhongLightPlcVO zhongLightPlcVO = new ZhongLightPlcVO();
- zhongLightPlcVO.setTimes(4);
- if (ret == 0){
- //成功
- String data = parseObject.getString("Data");
- //解密
- String decryptJson = aesUtils.decrypt(zhongLightPlc.getEncryptKey(), zhongLightPlc.getIv(), data);
- //解释json
- JSONObject resultbject = JSON.parseObject(decryptJson);
- Integer resultCode = resultbject.getInteger("result");
- zhongLightPlcVO.setErrorCode(resultCode);//是否调光成功
- if (resultCode == 0){
- //成功
- zhongLightPlcVO.setSuccCode(0);
- String uid = resultbject.getString("uid");
- zhongLightPlcVO.setUid(uid);
- }
- return zhongLightPlcVO;
- }
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- return null;
- }
- //
- private String encryptAndMd5(String data){
- String key = zhongLightPlc.getEncryptKey();
- String iv = zhongLightPlc.getIv();
- String encryptStr = null;
- //测试
- //String objectString = "{\"OperatorID\":\"MA5G0DCR3\",\"OperatorSecret\":\"uGxdt8NP5DYY+qYD\"}";
- try {
- //aes128位,模式:cbc,填充模式: pkcs5padding,返回是base64
- encryptStr = aesUtils.encrypt(key,iv,data);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- String seq = "001";
- Date date = new Date();
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
- String day = dateFormat.format(date);
- String sig = zhongLightPlc.getOperatorID()+encryptStr+day+seq;
- //hmac-md5加密32位后,小写要转换成大写 0B692762DA3A4BE698E859FA8B7F1DF3
- String hamc = null;
- byte[] bytes = zhongLightPlc.getSigSecret().getBytes(StandardCharsets.UTF_8);
- try {
- hamc = aesUtils.encodeHmacMD5(sig.getBytes(), bytes);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- //转换成大写
- String upperHamc = hamc.toUpperCase();
- JSONObject bodyJson = new JSONObject();
- bodyJson.put("OperatorID",zhongLightPlc.getOperatorID());
- bodyJson.put("Data",encryptStr);
- bodyJson.put("TimeStamp",day);
- bodyJson.put("Seq",seq);
- bodyJson.put("Sig",upperHamc);
- String bodyJsonString = bodyJson.toString();
- return bodyJsonString;
- }
- //广东启慧-调光/集中器设备注册(网关)/控制器
- private String quiHuiPlcDevice(String url,JSONObject params,Integer timeOut) {
- String plcUrl = quHuiPlc.getUrl()+url;
- Random random = new Random();
- int min = 10000;
- int max = 99999999;
- String id = "wecloud_"+(random.nextInt(max-min) + min);
- long timestamp = System.currentTimeMillis() / 1000;//单位是秒
- String sign = quHuiPlc.getClientId()+timestamp+id+quHuiPlc.getClientSecret();
- String md5Sign = DigestUtils.md5DigestAsHex(sign.getBytes()); //md5加密
- HashMap<String, Object> map = new HashMap<>();
- map.put("ver","1.0");
- map.put("client_id",quHuiPlc.getClientId());
- map.put("timestamp",timestamp);
- map.put("id",id);
- map.put("sign",md5Sign);
- map.put("params",params);
- ObjectMapper mapper = new ObjectMapper();
- String json = null;
- try {
- json = mapper.writeValueAsString(map).toString();
- } catch (JsonProcessingException e) {
- throw new RuntimeException(e);
- }
- //发送post请求
- String result = null;
- try {
- result = toolUtils.sendHttp("post",plcUrl, json, timeOut, ToolUtils.ContentTypeEnum.CONTENT_TYPE_JSON,null);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- return result;
- }
- //广东启慧-集中器设备注册(网关)
- // private String plcDevice(String url,String params,Integer timeOut){
- // String plcUrl = quHuiPlc.getUrl()+url;
- // Random random = new Random();
- // int min = 10000;
- // int max = 99999999;
- // String id = "wecloud_"+(random.nextInt(max-min) + min);
- // long timestamp = System.currentTimeMillis() / 1000;//单位是秒
- // String sign = quHuiPlc.getClientId()+timestamp+id+quHuiPlc.getClientSecret();
- // String md5Sign = DigestUtils.md5DigestAsHex(sign.getBytes()); //md5加密
- //
- // HashMap<String, Object> map = new HashMap<>();
- // map.put("ver","1.0");
- // map.put("client_id",quHuiPlc.getClientId());
- // map.put("timestamp",timestamp); //用map,timestamp不会打包成字符串,jsonObject会打包成字符串的
- // map.put("id",id);
- // map.put("sign",md5Sign);
- // map.put("params",params);
- // ObjectMapper mapper = new ObjectMapper();
- // String json = null;
- // try {
- // json = mapper.writeValueAsString(map).toString();
- // } catch (JsonProcessingException e) {
- // throw new RuntimeException(e);
- // }
- // //发送post请求
- // String result = null;
- // try {
- // result = toolUtils.sendHttp("post",plcUrl, json, timeOut, ToolUtils.ContentTypeEnum.CONTENT_TYPE_JSON);
- // } catch (IOException e) {
- // throw new RuntimeException(e);
- // }
- // return result;
- // }
- /**
- * 新建分组选择灯控
- * @param request
- * @return
- */
- @RequestMapping(value = "/selectLamp",method = RequestMethod.POST)
- public BaseResult selectLamp(HttpServletRequest request){
- String version = request.getParameter("version") == null ? "0" : request.getParameter("version");
- String type = request.getParameter("type") == null ? "0" : request.getParameter("type");
- String groupid = request.getParameter("groupid") == null || request.getParameter("groupid").length() == 0? "0" : request.getParameter("groupid");
- System.out.println(toolUtils);
- List sectionList = toolUtils.getSectionList(request);
- GroupVO groupVO = new GroupVO();
- groupVO.setVersion(Integer.valueOf(version));
- groupVO.setId(Integer.valueOf(groupid));
- groupVO.setSectionList(sectionList);
- GroupDTO group = groupService.getDetailByVO(groupVO);
- String[] lampArr = null;
- if (group != null && group.getLampId() != null){
- lampArr = group.getLampId().split(",");
- }
- LampListResponseVO lampListResponseVO = new LampListResponseVO();
- lampListResponseVO.setVersion(Integer.valueOf(version));
- lampListResponseVO.setSectionList(sectionList);
- List<LampInfoDTO> lampList = lampService.groupSelectLamp(lampListResponseVO);
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- List list = new ArrayList<>();
- for (LampInfoDTO lamp :lampList) {
- if (lampArr != null && ArrayUtils.contains(lampArr,lamp.getId().toString())){
- lamp.setSelect(1);
- }else {
- if (type.equals("1")) continue;
- lamp.setSelect(0);
- }
- if (lamp.getLighteness() == 0){
- lamp.setLampStatusStr("关");
- }else {
- lamp.setLampStatusStr("开");
- }
- list.add(lamp);
- }
- LampListResponseVO lampListResponseVO1 = new LampListResponseVO();
- lampListResponseVO1.setList(list);
- return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,Integer.parseInt(version),lampListResponseVO1);
- }
- /**
- * 灯控下拉
- * @param request
- * @return
- */
- @RequestMapping(value = "/nav",method = RequestMethod.POST)
- public BaseResult nav(HttpServletRequest request) {
- List sectionList = toolUtils.getSectionList(request);
- Integer version = (Integer) toolUtils.getRequestContent(request,"version",1);
- Integer lampType = (Integer) toolUtils.getRequestContent(request,"lampType",1);
- LampListResponseVO lampListResponseVO = new LampListResponseVO();
- lampListResponseVO.setSectionList(sectionList);
- if (lampType == 1){
- lampListResponseVO.setLampType(lampType);
- }
- List<LampInfoDTO> navByVO = lampService.getNavByVO(lampListResponseVO);
- ListResponseVO listResponseVO = new ListResponseVO();
- listResponseVO.setList(navByVO);
- return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,listResponseVO);
- }
- /**
- * 频段信息列表
- * @param request
- * @return
- */
- @RequestMapping(value = "/freqList",method = RequestMethod.POST)
- public BaseResult freqList(HttpServletRequest request) {
- Integer version = (Integer) toolUtils.getRequestContent(request,"version",1);
- ArrayList<FreqDTO> freqDTOS = new ArrayList<>();
- FreqDTO freqDTO = new FreqDTO();
- freqDTO.setId(1);
- freqDTO.setName("US915");
- freqDTOS.add(freqDTO);
- freqDTO = new FreqDTO();
- freqDTO.setId(2);
- freqDTO.setName("EU868");
- freqDTOS.add(freqDTO);
- freqDTO = new FreqDTO();
- freqDTO.setId(3);
- freqDTO.setName("AS923");
- freqDTOS.add(freqDTO);
- ListResponseVO listResponseVO = new ListResponseVO();
- listResponseVO.setList(freqDTOS);
- return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,listResponseVO);
- }
- /**
- * 获取经纬度
- * @param request
- * @return
- */
- @RequestMapping(value = "/sectionData",method = RequestMethod.POST)
- public BaseResult sectionData(HttpServletRequest request) {
- Integer version = (Integer) toolUtils.getRequestContent(request,"version",1);
- Integer sectionId = (Integer) toolUtils.getRequestContent(request,"sectionId",1);
- Integer areaId = (Integer) toolUtils.getRequestContent(request,"areaId",1);
- LampInfoDTO lampInfoDTO = new LampInfoDTO();
- if ((sectionId == null || sectionId.intValue() == 0) && (areaId == null || areaId.intValue() == 0)) {
- lampInfoDTO.setLongitude(114.06807299140166f);
- lampInfoDTO.setLatitude(22.5324106360218f);
- return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,lampInfoDTO);
- }
- if (areaId == null || areaId.intValue() == 0){
- SectionDTO oneById = sectionService.getOneById(sectionId);
- GlobalLocationDTO oneById2 = globalLocationService.getOneById(oneById.getPid());
- lampInfoDTO.setLongitude(oneById2.getLongitude());
- lampInfoDTO.setLatitude(oneById2.getLatitude());
- }else {
- GlobalLocationDTO oneById = globalLocationService.getOneById(areaId);
- lampInfoDTO.setLongitude(oneById.getLongitude());
- lampInfoDTO.setLatitude(oneById.getLatitude());
- }
- return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,lampInfoDTO);
- }
- /**
- * 获取太阳能控制器设备列表
- * @param request 根据sectionId进行筛选
- * @return 返回设备列表
- */
- @RequestMapping(value = "/getSolarList", method = RequestMethod.POST)
- public BaseResult<?> getSolarList(HttpServletRequest request) {
- int version = (int) toolUtils.getRequestContent(request,"version",1);
- int online = (int) toolUtils.getRequestContent(request,"online",1);
- int page = request.getParameter("page") == null ? 1 : Integer.parseInt(request.getParameter("page"));
- int count = request.getParameter("count") == null ? 16 : Integer.parseInt(request.getParameter("count"));
- String keyword = (String) toolUtils.getRequestContent(request,"online",2);
- SolarDevDTO dto = new SolarDevDTO();
- dto.setPage(count * (page - 1));
- dto.setCount(count);
- dto.setVersion(version);
- dto.setOnline(online);
- dto.setKeyword(keyword);
- dto.setSectionList(toolUtils.getSectionList(request));
- SolarDevVO solarDevVO = solarDevService.getSolarListByDTO(dto);
- return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,solarDevVO);
- }
- /**
- * 获取太阳能统计数据
- * @param request 设备id
- * @return 返回太阳能统计数据
- */
- @RequestMapping(value = "/solarStatistics", method = RequestMethod.POST)
- public BaseResult<?> solarStatistics(HttpServletRequest request) throws ParseException {
- int version = (int) toolUtils.getRequestContent(request,"version",1);
- int id = (int) toolUtils.getRequestContent(request,"id",1);
- if (id == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
- Integer timezone = solarInfoLogService.getTimezoneBySolarId(id);
- if (timezone == null) return toolUtils.response(InterfaceResultEnum.PARAM_FAIL,version);
- long l = System.currentTimeMillis() - timezone * 3600 * 1000;
- long startTime;
- long endTime;
- startTime = l - 24L * 3600 * 1000 * 29;
- endTime = l;
- HashMap<String, Integer> objectObjectHashMap = new HashMap<>();
- long timeT = startTime;
- int i = 0;
- List<Object> dateList = new ArrayList<>();
- List<Object> lightTimeList = new ArrayList<>();
- List<Object> comList = new ArrayList<>();
- List<Object> eleList = new ArrayList<>();
- List<Object> totalLightTime = new ArrayList<>();
- List<Object> totalCom = new ArrayList<>();
- List<Object> totalEle = new ArrayList<>();
- List<SolarInfoLogDTO> list = new ArrayList<>();
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- while (timeT <= endTime) {
- objectObjectHashMap.put(simpleDateFormat.format(new Date(timeT)),i);
- dateList.add(simpleDateFormat.format(new Date(timeT + timezone * 3600 * 1000)));
- lightTimeList.add(0);
- eleList.add(0);
- comList.add(0);
- totalLightTime.add(0);
- totalCom.add(0);
- totalEle.add(0);
- list.add(null);
- timeT += 3600 * 1000 * 24;
- i++;
- }
- List<SolarInfoLogDTO> solarInfoLogList = solarInfoLogService.getSolarInfoLogByDTO(id);
- for (SolarInfoLogDTO solarInfoLogDTO : solarInfoLogList) {
- Date date = new Date(simpleDateFormat.parse(solarInfoLogDTO.getMaxTime()).getTime());
- String s = simpleDateFormat.format(date);
- if (objectObjectHashMap.containsKey(s)){
- Integer integer = objectObjectHashMap.get(s);
- list.set(integer,solarInfoLogDTO);
- }
- }
- DecimalFormat decimalFormat = new DecimalFormat("0.000");
- for (SolarInfoLogDTO solarInfoLogDTO : list) {
- if (solarInfoLogDTO != null) {
- Date date = new Date(simpleDateFormat.parse(solarInfoLogDTO.getMaxTime()).getTime());
- String s = simpleDateFormat.format(date);
- Integer integer = null;
- if (objectObjectHashMap.containsKey(s)){
- integer = objectObjectHashMap.get(s);
- list.set(integer,solarInfoLogDTO);
- }
- if (solarInfoLogDTO.getMaxTotalLightTime() != null && solarInfoLogDTO.getMinTotalLightTime() != null && integer != null) {
- totalLightTime.set(integer,Integer.parseInt(solarInfoLogDTO.getMaxTotalLightTime()));
- Integer dayLightTime = Integer.parseInt(solarInfoLogDTO.getMaxTotalLightTime()) - Integer.parseInt(solarInfoLogDTO.getMinTotalLightTime());
- lightTimeList.set(integer,dayLightTime);
- }
- if (solarInfoLogDTO.getMaxTotalConsumption() != null && solarInfoLogDTO.getMinTotalConsumption() != null && integer != null) {
- totalCom.set(integer,Float.parseFloat(solarInfoLogDTO.getMaxTotalConsumption()));
- Float dayCom = Float.parseFloat(solarInfoLogDTO.getMaxTotalConsumption()) - Float.parseFloat(solarInfoLogDTO.getMinTotalConsumption());
- comList.set(integer,Float.parseFloat(decimalFormat.format(dayCom)));
- }
- if (solarInfoLogDTO.getMaxTotalEleSave() != null && solarInfoLogDTO.getMinTotalEleSave() != null && integer != null) {
- totalEle.set(integer,Float.parseFloat(solarInfoLogDTO.getMaxTotalEleSave()));
- Float daySave = Float.parseFloat(solarInfoLogDTO.getMaxTotalEleSave()) - Float.parseFloat(solarInfoLogDTO.getMinTotalEleSave());
- eleList.set(integer,Float.parseFloat(decimalFormat.format(daySave)));
- }
- }
- }
- SolarInfoLogVO solarInfoLogVO = new SolarInfoLogVO();
- solarInfoLogVO.setDateList(dateList);
- solarInfoLogVO.setLightTimeList(lightTimeList);
- solarInfoLogVO.setComList(comList);
- solarInfoLogVO.setEleList(eleList);
- solarInfoLogVO.setTotalLightTime(totalLightTime);
- solarInfoLogVO.setTotalCom(totalCom);
- solarInfoLogVO.setTotalEle(totalEle);
- return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,solarInfoLogVO);
- }
- /**
- * 新建分组下选择灯杆
- * @param request sectionList,groupId,sectionId,type
- * @return 新建分组下选择灯杆
- */
- @RequestMapping(value = "/selectLampPole", method = RequestMethod.POST)
- public BaseResult<?> lampPoleData(HttpServletRequest request) {
- Integer version = (Integer) toolUtils.getRequestContent(request,"version",1);
- Integer type = (Integer) toolUtils.getRequestContent(request,"type",1);
- Integer groupId = (Integer) toolUtils.getRequestContent(request,"groupId",1);
- List<LampPoleDTO> lampPoleList;
- if (type == 1) { // 查询分组下的灯杆
- if (groupId == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
- GroupDTO groupDTO = groupService.getGroupDTOById(groupId);
- if (groupDTO == null) return toolUtils.response(InterfaceResultEnum.PARAM_FAIL,version);
- if (groupDTO.getDevType() == 0) return toolUtils.response(InterfaceResultEnum.PARAM_FAIL,version);
- String lampId = groupDTO.getLampId();
- List<String> lampIds = Arrays.asList(lampId.split(","));
- lampPoleList = lampPoleService.getLampPoleByLampIds(lampIds);
- lampPoleList.forEach(dto -> dto.setSelect(1));
- } else { //根据路段查询灯杆,分组可选
- Integer sectionId = (Integer) toolUtils.getRequestContent(request,"sectionId",1);
- LampPoleVO lampPoleVO = new LampPoleVO();
- lampPoleVO.setVersion(version);
- lampPoleVO.setSectionList(toolUtils.getSectionList(request));
- lampPoleVO.setSectionId(sectionId);
- if (groupId == 0) {
- lampPoleList = lampPoleService.getSectionLampPoleList(lampPoleVO);
- lampPoleList.forEach(dto -> dto.setSelect(0));
- } else {
- GroupDTO groupDTO = groupService.getGroupDTOById(groupId);
- if (groupDTO == null) return toolUtils.response(InterfaceResultEnum.PARAM_FAIL,version);
- if (groupDTO.getDevType() == 0) return toolUtils.response(InterfaceResultEnum.PARAM_FAIL,version);
- lampPoleList = lampPoleService.getSectionLampPoleList(lampPoleVO);
- String lampId = groupDTO.getLampId();
- String[] lampIds = lampId.split(",");
- for (LampPoleDTO l : lampPoleList) {
- boolean isFlag = false;
- for (String s : lampIds) {
- if (l.getId().equals(Integer.valueOf(s))) {
- l.setSelect(1);
- isFlag = true;
- break;
- }
- }
- if (!isFlag) {
- l.setSelect(0);
- }
- }
- }
- }
- LampPoleVO lampPoleVO = new LampPoleVO();
- lampPoleVO.setList(lampPoleList);
- return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,lampPoleVO);
- }
- }
|