|
@@ -18,6 +18,8 @@ import org.apache.commons.codec.DecoderException;
|
|
|
import org.apache.commons.codec.binary.Hex;
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.DigestUtils;
|
|
@@ -73,6 +75,8 @@ public class LampController {
|
|
|
private ZhongLightPlc zhongLightPlc;
|
|
|
@Autowired
|
|
|
private AESUtils aesUtils;
|
|
|
+ @Autowired
|
|
|
+ private PlcAddressService plcAddressService;
|
|
|
|
|
|
/**
|
|
|
* 灯控列表
|
|
@@ -152,24 +156,254 @@ public class LampController {
|
|
|
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"));
|
|
|
- Integer areaId = request.getParameter("areaId") == null ? 0 : Integer.parseInt(request.getParameter("areaId"));
|
|
|
- Integer sectionId = request.getParameter("sectionId") == null ? 0 : Integer.parseInt(request.getParameter("sectionId"));
|
|
|
+ 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);
|
|
|
|
|
|
- Integer protocolType = request.getParameter("protocolType") == null ? 0 : Integer.parseInt(request.getParameter("protocolType"));
|
|
|
- Integer controlType = request.getParameter("controlType") == null ? 0 : Integer.parseInt(request.getParameter("controlType"));
|
|
|
+ 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("install_date") == null ? "" : request.getParameter("install_date");
|
|
|
- String expirationDate = request.getParameter("expiration_date") == null ? "" : request.getParameter("expiration_date");
|
|
|
+ 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 (controlType == 26 || controlType == 27){
|
|
|
+ if (macAddress != null && sn != null){
|
|
|
+ bindStatus = 0;
|
|
|
+ //微自然plc
|
|
|
+ this.savePlcAddress(macAddress,sn);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
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);
|
|
|
+ //同一个plc下不允许有相同的序号/相同灯的地址
|
|
|
+ if (protocolType == 11 && controlType != 26 && controlType != 27){
|
|
|
+ lampInfo.setMacAddress(macAddress);
|
|
|
+ //灯地址
|
|
|
+ Integer macAddressTotal = lampService.checkData(lampInfo);
|
|
|
+ if (macAddressTotal > 0) return toolUtils.response(InterfaceResultEnum.PLC_ADDRESS_EXIST_ERROR,version);
|
|
|
+ //灯序号
|
|
|
+ lampInfo.setMacAddress(null);
|
|
|
+ lampInfo.setDevAddr(devAddr);
|
|
|
+ Integer devAddrTotal = lampService.checkData(lampInfo);
|
|
|
+ if (devAddrTotal > 0) return toolUtils.response(InterfaceResultEnum.PLC_SERIAL_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 networkData = new NetworkDTO();
|
|
|
+ networkData.setDeviceSn(sn);
|
|
|
+ Integer networkTotal = networkService.checkNetworkData(networkData);
|
|
|
+ if (networkTotal == 0) return toolUtils.response(InterfaceResultEnum.LACK_PLC_ADDRESS__ERROR,version);
|
|
|
+ }
|
|
|
+ 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.getId());
|
|
|
+ sectionDTO= sectionService.getDataByVO(sectionDTO);
|
|
|
+ //location
|
|
|
+ GlobalLocationDTO areaData = new GlobalLocationDTO();
|
|
|
+ areaData.setId(sectionDTO.getPid());
|
|
|
+ areaData = globalLocationService.getDataByDto(areaData);
|
|
|
+
|
|
|
+ String chinaMsg = "无线地址已存在,区域: "+areaData.getChineseName()+"-->路段:"+sectionDTO.getName()+"-->路灯编号:"+lampInfo.getNumber();
|
|
|
+ String engMsg = "The wireless address already exists,area: "+areaData.getEnglishName()+"-->section:"+sectionDTO.getName()+"-->number:"+lampInfo.getNumber();
|
|
|
+ String ruMsg = "Беспроводной адрес уже существует,область: "+areaData.getRuName()+"-->участок дороги:"+sectionDTO.getName()+"-->Номер фонаря:"+lampInfo.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 {
|
|
|
+ netAddress = sn.length() == 13 ? String.valueOf(Integer.parseInt(sn,10)) : sn;
|
|
|
+ }
|
|
|
+ NetworkDTO networkData = new NetworkDTO();
|
|
|
+ networkData.setDeviceSn(sn);
|
|
|
+ NetworkDTO simpleData = networkService.getSimpleData(networkData);
|
|
|
+ //zigbee设备
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ if (protocolType != 11 && simpleData != null){
|
|
|
+ return toolUtils.response(InterfaceResultEnum.DEVICE_ADDRESS_EXITS_ERROR,version);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// if ($protocoltype == 0 || $protocoltype == 3 || $protocoltype == 4 || $protocoltype == 5 || $protocoltype == 6
|
|
|
+// || $protocoltype == 7 || $protocoltype == 8 || $protocoltype == 9 || $protocoltype == 10 || $protocoltype == 11 || $protocoltype == 13) {
|
|
|
+// $data['net_type'] = 1;
|
|
|
+//
|
|
|
+// if ( $protocoltype != 11 && $protocoltype != 13 && $protocoltype != 5 && !empty($net_info)) {
|
|
|
+// $lamp_info = $this->Lampinfo_model->get_one(array('networkid'=>$net_info['id']),'id');
|
|
|
+// if (!empty($lamp_info)) {
|
|
|
+// $this->Network_model->delete(array('id'=>$net_info['id']));
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// // if ( $protocoltype != 11 && $this->Network_model->getDataCount(array('devicesn'=>$number))) {
|
|
|
+// // exit(json_result('0309',$this->response['0309'],array()));
|
|
|
+// // }
|
|
|
+// // plc只创建一个网络就可以了
|
|
|
+// $plcNetCount = 0; //0:没有创建 ,1: 创建过了
|
|
|
+// if ($this->Network_model->getDataCount(array('devicesn'=>$number))) {
|
|
|
+// if ($protocoltype == 11 && ($data['control_type'] != 26 && $data['control_type'] != 27)){
|
|
|
+// $plcNetCount = 1;
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// $networkData['gatewaytype'] = 'direct';
|
|
|
+// $networkData['protocoltype'] = empty($protocoltype) ? 0 : $protocoltype;
|
|
|
+// $networkData['areaid'] = $data['areaid'];
|
|
|
+// $networkData['sectionid'] = $data['sectionid'];
|
|
|
+// $networkData['devicesn'] = $number;
|
|
|
+// if ($protocoltype == 10) $networkData['devicetype'] = 4;
|
|
|
+// if ($protocoltype == 8 && ($data['control_type'] == 29 || $data['control_type'] == 33)) $networkData['devicetype'] = 4;
|
|
|
+// if ($protocoltype == 3 && $data['control_type'] == 9) $networkData['devicetype'] = 4;
|
|
|
+// if ($protocoltype == 8 && $data['control_type'] == 19) $networkData['devicetype'] = 4;
|
|
|
+// if ($control_type == 16 || $control_type == 20) {
|
|
|
+// $networkData['devicetype'] = 5;
|
|
|
+// }
|
|
|
+// $networkData['network_name'] = $number;
|
|
|
+// $networkData['net_type'] = 1;
|
|
|
+// $networkData['createtime'] = date('Y-m-d H:i:s',time());
|
|
|
+// $networkData['userId'] = $userId;
|
|
|
+//
|
|
|
+// //$data['networkid'] = $this->Network_model->add($networkData);
|
|
|
+// if ($protocoltype == 6) $this->add_device($data['address'],$freqId);
|
|
|
+// if($protocoltype ==7 ) {
|
|
|
+// $onenet=$this->add_device_onenet($data['address']);
|
|
|
+// $file = fopen('./file/onenet_group_cmdURL.txt', 'a+');
|
|
|
+// fwrite($file, date('Y-m-d H:i:s').'----'.json_encode($onenet).' ');
|
|
|
+// fclose($file);
|
|
|
+// if (isset($onenet["data"])) {
|
|
|
+// $onenetId=$onenet['data']['device_id'];
|
|
|
+// if($onenetId){
|
|
|
+// $data['deviceid']=$onenetId;
|
|
|
+// $networkData['deviceid']=$onenetId;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+ //添加网关
|
|
|
+ NetworkDTO networkDTO = new NetworkDTO();
|
|
|
+ networkDTO.setDeviceSn(sn);
|
|
|
+ //查询有没有网关
|
|
|
+ Integer count = networkService.checkNetworkData(networkDTO);
|
|
|
+ if (count == 0){
|
|
|
+ //添加网关
|
|
|
+ //networkService.addNetworkData();
|
|
|
+ }
|
|
|
+ //添加灯控
|
|
|
LampInfoDTO lampInfoDTO = new LampInfoDTO();
|
|
|
lampInfoDTO.setProtocolType(protocolType);
|
|
|
lampInfoDTO.setSn(sn);
|
|
@@ -185,6 +419,9 @@ public class LampController {
|
|
|
lampInfoDTO.setInstallDate(installDate);
|
|
|
lampInfoDTO.setExpirationDate(expirationDate);
|
|
|
lampInfoDTO.setRatedPower(power);
|
|
|
+ lampInfoDTO.setDevAddr(devAddr);
|
|
|
+ lampInfoDTO.setFreqId(Integer.parseInt(freqId));
|
|
|
+ lampInfoDTO.setZegbeeSectionId(loopNumber);
|
|
|
return lampService.add(lampInfoDTO);
|
|
|
}else { // 编辑
|
|
|
// LampInfoDTO oldLamp = lampService.getDetailsById(lampId,version);
|
|
@@ -218,7 +455,22 @@ public class LampController {
|
|
|
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);
|
|
|
+ }
|
|
|
+ }
|
|
|
/**
|
|
|
* 删除灯控
|
|
|
* @param request
|
|
@@ -635,77 +887,164 @@ public class LampController {
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
-// $this->getToken(); //获取token
|
|
|
-// if (!empty($this->token)) {
|
|
|
-// $this->plcLogin(); //获取ProjectId,OrganizationId,SessionId,DivisionId
|
|
|
-//
|
|
|
-// if (!empty($this->sessionId)){
|
|
|
-// $this->header = array(
|
|
|
-// 'Content-Type: application/json',
|
|
|
-// "Authorization: Bearer " . "{$this->token}",
|
|
|
-// "SessionId: " . "{$this->sessionId}"
|
|
|
-// );
|
|
|
-//
|
|
|
-// //1:网关, 4: 控制器 和 灯具
|
|
|
-// $plcUrl = $this->zhongUrl . 'commons/v1_0/send';
|
|
|
-//
|
|
|
-// $this->curl = curl_init();
|
|
|
-// //gatewayAddr 一定要小写
|
|
|
-// if ($adjustment == 0){
|
|
|
-// //开/关灯
|
|
|
-// $cmds = [array('code'=>1,'headSerial'=>1,'ep'=>1,'gatewayAddr'=>'0x'.strtolower($gatewayAddr),'addr'=>'0x'.$controlAddr,'uid'=>$lamp_ctrl_id,
|
|
|
-// 'control'=>array('devtype'=>23,'on'=>intval($bright))
|
|
|
-// )];
|
|
|
-// }else{
|
|
|
-// $cmds = [array('code'=>1,'headSerial'=>1,'ep'=>1,'gatewayAddr'=>'0x'.strtolower($gatewayAddr),'addr'=>'0x'.$controlAddr,'uid'=>$lamp_ctrl_id,
|
|
|
-// 'control'=>array('devtype'=>23,'bri'=>intval($bright),'port'=>1)
|
|
|
-// )];
|
|
|
-// }
|
|
|
-// $data = array('platform' => 1, 'cmds' => $cmds);
|
|
|
-//
|
|
|
-//
|
|
|
-// $timeStamp = date('YmdHis');
|
|
|
-// $data = json_encode($data);
|
|
|
-//
|
|
|
-// //加密
|
|
|
-// $res = $this->encrypt($data);
|
|
|
-// $seq = '001';
|
|
|
-// $sig = $this->OperatorID . $res . $timeStamp . $seq;
|
|
|
-// //hmac-md5加密后,小写要转换成大写
|
|
|
-// $sig = strtoupper($this->hmac($sig, $this->sigSecret));
|
|
|
-// $body = array('OperatorID' => $this->OperatorID, 'Data' => $res, 'TimeStamp' => $timeStamp, 'Seq' => $seq, 'Sig' => $sig);
|
|
|
-// $body = json_encode($body);
|
|
|
-//
|
|
|
-// $result = $this->curlSendCmd($body, $plcUrl,10);
|
|
|
-// $result = json_decode($result);
|
|
|
-// if (empty($result)){
|
|
|
-// // exit(json_result('0918', $this->response['0918']));
|
|
|
-// return '命令下发超时';
|
|
|
-//
|
|
|
-// }else{
|
|
|
-// if($result->Ret == 0){
|
|
|
-// //解密
|
|
|
-// $res = $this->decrypt($result->Data);
|
|
|
-// $res = json_decode($res);
|
|
|
-// if ($res[0]->result == 780) {
|
|
|
-// //离线
|
|
|
-// return '设备离线';
|
|
|
-// } elseif ($res[0]->result == 201) {
|
|
|
-// return '控制超时';
|
|
|
-// }elseif ($res[0]->result == 203){
|
|
|
-// //设备连接异常 / 设备离线
|
|
|
-// return '设备连接异常';
|
|
|
-// }elseif ($res[0]->result == 0){
|
|
|
-// return '成功';
|
|
|
-// }
|
|
|
-//
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// }
|
|
|
-// }
|
|
|
}
|
|
|
+ /* 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);
|
|
|
+ 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);
|
|
|
+ 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();
|