Commit 664eaf1e by huangjy

feat,修改机构同步iot

parent eee34240
...@@ -42,12 +42,27 @@ public class IotCommonService { ...@@ -42,12 +42,27 @@ public class IotCommonService {
return request; return request;
} }
public ResponseMessage sendPut(String url, HttpRequest request) throws IOException {
Response response = request.put();
ResponseMessage responseMessage = getResponseMessage(url, response);
return responseMessage;
}
public ResponseMessage sendGet(String url, HttpRequest request) throws IOException {
Response response = request.get();
ResponseMessage responseMessage = getResponseMessage(url, response);
return responseMessage;
}
public ResponseMessage sendPost(String url, HttpRequest request) throws IOException { public ResponseMessage sendPost(String url, HttpRequest request) throws IOException {
Response response = request.post(); Response response = request.post();
ResponseMessage responseMessage = getResponseMessage(url, response);
return responseMessage;
}
private static ResponseMessage getResponseMessage(String url, Response response) throws IOException {
Object result = JSON.parse(response.asBytes()); Object result = JSON.parse(response.asBytes());
ResponseMessage responseMessage = JSON.parseObject(result.toString(), ResponseMessage.class); ResponseMessage responseMessage = JSON.parseObject(result.toString(), ResponseMessage.class);
log.info("接口:{},返回信息:{}",url,JSON.toJSONString(responseMessage)); log.info("接口:{},返回信息:{}", url,JSON.toJSONString(responseMessage));
return responseMessage; return responseMessage;
} }
...@@ -72,7 +87,9 @@ public class IotCommonService { ...@@ -72,7 +87,9 @@ public class IotCommonService {
IotQueryParam iotQueryParam = new IotQueryParam(); IotQueryParam iotQueryParam = new IotQueryParam();
iotQueryParam.setPageIndex(0); iotQueryParam.setPageIndex(0);
iotQueryParam.setPageSize(pageSize); iotQueryParam.setPageSize(pageSize);
buildSort("timestamp");
iotQueryParam.setSorts(buildSort("timestamp"));
iotQueryParam.setTerms(Lists.newArrayList()); iotQueryParam.setTerms(Lists.newArrayList());
return iotQueryParam; return iotQueryParam;
} }
......
...@@ -18,6 +18,7 @@ import lombok.extern.slf4j.Slf4j; ...@@ -18,6 +18,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -31,10 +32,53 @@ public class IotOrgService extends IotCommonService{ ...@@ -31,10 +32,53 @@ public class IotOrgService extends IotCommonService{
public static final String DEVICE_PRODUCT_PREFIX_URL = "device-product/"; public static final String DEVICE_PRODUCT_PREFIX_URL = "device-product/";
public void getIotOrgInfo(String orgId) {
String url = iotUrl + "organization/" + orgId;
HttpRequest request = new SimpleHttpRequest(url, httpClient);
request.headers(headerUtils.createHeadersOfParams(new HashMap<>()));
try {
ResponseMessage responseMessage = sendGet(url, request);
if (responseMessage.getStatus() == 200) {
OrganizationEntity organizationEntity = JSON.parseObject(responseMessage.getResult().toString(), OrganizationEntity.class);
}
} catch (IOException e) {
log.error("调用:{}接口异常:{}",url,e.getMessage());
}
}
/**
* 更新iod
* @param orgId
* @param orgName
*/
public void updateIotOrgInfo(String orgId,String orgName) {
String url = iotUrl + "organization/" + orgId;
Map<String,Object> reqMap = Maps.newHashMap();
reqMap.put("name",orgName);
String body = JSON.toJSONString(reqMap);
HttpRequest request = new SimpleHttpRequest(url, httpClient);
request.headers(headerUtils.createHeadersOfJsonString(body));
request.requestBody(body);
try {
ResponseMessage responseMessage = sendPut(url, request);
if (responseMessage.getStatus() == 200) {
log.info("更新机构成功");
} else {
log.error("更新机构失败:{}",responseMessage.getMessage());
}
} catch (IOException e) {
log.error("调用:{}接口异常:{}",url,e.getMessage());
}
}
/** /**
* iot同步新增组织机构 * iot同步新增组织机构
*/ */
public OrganizationEntity syncTenantInfoToIot(PlatTenantVO platTenantVO) { public OrganizationEntity addIotOrg(PlatTenantVO platTenantVO) {
String url = iotUrl + "organization"; String url = iotUrl + "organization";
Map<String,Object> reqMap = Maps.newHashMap(); Map<String,Object> reqMap = Maps.newHashMap();
reqMap.put("name",platTenantVO.getName()); reqMap.put("name",platTenantVO.getName());
...@@ -45,7 +89,7 @@ public class IotOrgService extends IotCommonService{ ...@@ -45,7 +89,7 @@ public class IotOrgService extends IotCommonService{
request.headers(headerUtils.createHeadersOfJsonString(body)); request.headers(headerUtils.createHeadersOfJsonString(body));
request.requestBody(body); request.requestBody(body);
try { try {
ResponseMessage responseMessage = sendPost(url, request); ResponseMessage responseMessage = sendPost(url, request);
if (responseMessage.getStatus() == 200) { if (responseMessage.getStatus() == 200) {
OrganizationEntity organizationEntity = JSON.parseObject(responseMessage.getResult().toString(), OrganizationEntity.class); OrganizationEntity organizationEntity = JSON.parseObject(responseMessage.getResult().toString(), OrganizationEntity.class);
log.info("新增机构成功,机构id:{}",organizationEntity.getId()); log.info("新增机构成功,机构id:{}",organizationEntity.getId());
......
...@@ -4,13 +4,16 @@ import com.alibaba.fastjson.JSON; ...@@ -4,13 +4,16 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.makeit.module.iot.dto.IotQueryParam; import com.makeit.module.iot.dto.IotQueryParam;
import com.makeit.module.iot.dto.Term;
import com.makeit.module.iot.util.HttpRequest; import com.makeit.module.iot.util.HttpRequest;
import com.makeit.module.iot.vo.DeviceInstanceEntity; import com.makeit.module.iot.vo.DeviceInstanceEntity;
import com.makeit.module.iot.vo.DeviceOperationLogEntity; import com.makeit.module.iot.vo.DeviceOperationLogEntity;
import com.makeit.module.iot.vo.IotPagerResult; import com.makeit.module.iot.vo.IotPagerResult;
import com.makeit.module.iot.vo.ResponseMessage; import com.makeit.module.iot.vo.ResponseMessage;
import com.makeit.utils.data.convert.JsonUtil; import com.makeit.utils.data.convert.JsonUtil;
import com.makeit.utils.old.StringUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
...@@ -38,7 +41,6 @@ public class IotProductDeviceService extends IotCommonService { ...@@ -38,7 +41,6 @@ public class IotProductDeviceService extends IotCommonService {
HttpRequest request = buildRequest(url, body); HttpRequest request = buildRequest(url, body);
try { try {
ResponseMessage responseMessage = sendPost(url, request); ResponseMessage responseMessage = sendPost(url, request);
if (responseMessage.getStatus() == 200) { if (responseMessage.getStatus() == 200) {
IotPagerResult pagerResult = JSON.parseObject(responseMessage.getResult().toString(), IotPagerResult.class); IotPagerResult pagerResult = JSON.parseObject(responseMessage.getResult().toString(), IotPagerResult.class);
...@@ -54,16 +56,89 @@ public class IotProductDeviceService extends IotCommonService { ...@@ -54,16 +56,89 @@ public class IotProductDeviceService extends IotCommonService {
} }
/** /**
* 获取最新一条设备日志
* @param deviceId
* @return
*/
public DeviceOperationLogEntity getLastDeviceLog(String deviceId) {
List<DeviceOperationLogEntity> deviceOperationLogEntities = getDeviceLog(deviceId, 1, "");
return CollectionUtils.isNotEmpty(deviceOperationLogEntities) ? deviceOperationLogEntities.get(0) : null;
}
/**
*
* 根据类型查询设备日志
* text: "事件上报", value: "event"}
* {text: "读取属性", value: "readProperty"}
* {text: "修改属性", value: "writeProperty"}
* {text: "修改属性回复", value: "writePropertyReply"}
* {text: "属性上报", value: "reportProperty"}
* {text: "读取属性回复", value: "readPropertyReply"}
* {text: "子设备消息", value: "child"}
* {text: "子设备消息回复", value: "childReply"}
* {text: "调用功能", value: "functionInvoke"}
* {text: "调用功能回复", value: "functionReply"}
* {text: "设备注册", value: "register"}
* {text: "设备注销", value: "unregister"}
* {text: "读取固件信息", value: "readFirmware"}
* {text: "读取固件信息回复", value: "readFirmwareReply"}
* {text: "上报固件信息", value: "reportFirmware"}
* {text: "拉取固件信息", value: "pullFirmware"}
* {text: "拉取固件信息回复", value: "pullFirmwareReply"}
* {text: "推送固件信息", value: "upgradeFirmware"}
* {text: "推送固件信息回复", value: "upgradeFirmwareReply"}
* {text: "固件更新进度", value: "upgradeFirmwareProgress"}
* {text: "日志", value: "log"}
* {text: "标签更新", value: "tag"}
* {text: "离线", value: "offline"}
* {text: "上线", value: "online"}
* {text: "其它", value: "other"}
* {text: "透传", value: "direct"}
* {text: "应答", value: "acknowledge"}
* {text: "上报物模型", value: "metadata"}
* {text: "状态检查", value: "stateCheck"}
* {text: "状态检查回复", value: "stateCheckReply"}
* {text: "断开连接", value: "disconnect"}
* {text: "断开连接回复", value: "disconnectReply"}
* {text: "上报数采数据", value: "reportCollectorData"}
* {text: "读取数采数据", value: "readCollectorData"}
* {text: "读取数采数据回复", value: "readCollectorDataReply"}
* {text: "修改数采数据", value: "writeCollectorData"}
* {text: "修改数采数据回复", value: "writeCollectorDataReply"}
*
* @param deviceId
* @param typeValue
* @return
*/
public List<DeviceOperationLogEntity> getDeviceLogByType(String deviceId,String typeValue) {
return getDeviceLog(deviceId, 10,typeValue);
}
/**
* 获取设备的日志 * 获取设备的日志
* *
* @param deviceId * @param deviceId
* @param typeValue
*/ */
public List<DeviceOperationLogEntity> getDeviceLog(String deviceId) { public List<DeviceOperationLogEntity> getDeviceLog(String deviceId, int pageSize, String typeValue) {
String url = iotUrl + DEVICE_PREFIX_URL + deviceId + "/logs"; String url = iotUrl + DEVICE_PREFIX_URL + deviceId + "/logs";
IotQueryParam iotQueryParam = buildQueryParam(10); IotQueryParam iotQueryParam = buildQueryParam(pageSize);
if (StringUtils.isNotEmpty(typeValue)) {
List<Term> terms = Lists.newArrayList();
Term term = Term.builder()
.column("type")
.termType("eq")
.type(Term.Type.or)
.value(typeValue)
.terms(Lists.newArrayList())
.options(Lists.newArrayList())
.build();
terms.add(term);
iotQueryParam.setTerms(terms);
}
String body = JsonUtil.toJson(iotQueryParam); String body = JsonUtil.toJson(iotQueryParam);
HttpRequest request = buildRequest(url, body); HttpRequest request = buildRequest(url, body);
try { try {
...@@ -82,4 +157,7 @@ public class IotProductDeviceService extends IotCommonService { ...@@ -82,4 +157,7 @@ public class IotProductDeviceService extends IotCommonService {
} }
} }
...@@ -27,6 +27,32 @@ public class HeaderUtils { ...@@ -27,6 +27,32 @@ public class HeaderUtils {
@Value("${iot.secureKey}") @Value("${iot.secureKey}")
private String secureKey; private String secureKey;
public Map<String, String> createHeadersOfParams(Map<String, Object> params) {
//时间戳
String xTimestamp = String.valueOf(new Date().getTime());
//将参数按ASCII排序后拼接为k1=v1&k2=v2的格式
String paramString = new TreeMap<>(params).entrySet()
.stream()
.map(e -> e.getKey().concat("=").concat(String.valueOf(e.getValue())))
.collect(Collectors.joining("&"));
System.out.println(paramString);
//param+X-Timestamp+SecureKey通过MD5加密
MessageDigest digest = DigestUtils.getMd5Digest();
System.out.println(paramString + xTimestamp + secureKey);
digest.update(paramString.getBytes());
digest.update(xTimestamp.getBytes());
digest.update(secureKey.getBytes());
Map<String, String> headers = new HashMap<>();
headers.put("X-Sign", Hex.encodeHexString(digest.digest()));
headers.put("X-Client-Id", clientId);
headers.put("X-Timestamp", xTimestamp);
return headers;
}
public Map<String, String> createHeadersOfJsonString(String jsonString) { public Map<String, String> createHeadersOfJsonString(String jsonString) {
//时间戳 //时间戳
String xTimestamp = String.valueOf(new Date().getTime()); String xTimestamp = String.valueOf(new Date().getTime());
......
...@@ -2,6 +2,7 @@ package com.makeit.module.iot.vo; ...@@ -2,6 +2,7 @@ package com.makeit.module.iot.vo;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
import org.hibernate.validator.constraints.Length;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -9,11 +10,9 @@ import java.util.Map; ...@@ -9,11 +10,9 @@ import java.util.Map;
@Data @Data
public class OrganizationEntity { public class OrganizationEntity {
@Schema(description = "ID") @Schema(description = "ID")
private String id; private String id;
@Schema(description = "编码") @Schema(description = "编码")
private String code; private String code;
...@@ -43,4 +42,24 @@ public class OrganizationEntity { ...@@ -43,4 +42,24 @@ public class OrganizationEntity {
private Long createTime; private Long createTime;
private List<OrganizationEntity> children; private List<OrganizationEntity> children;
@Schema(
description = "父节点ID"
)
private String parentId;
@Schema(
description = "树结构路径"
)
private String path;
@Schema(
description = "排序序号"
)
private Long sortIndex;
@Schema(
description = "树层级"
)
private Integer level;
} }
...@@ -7,10 +7,7 @@ import com.makeit.common.dto.StatusDTO; ...@@ -7,10 +7,7 @@ import com.makeit.common.dto.StatusDTO;
import com.makeit.common.page.PageReqDTO; import com.makeit.common.page.PageReqDTO;
import com.makeit.common.page.PageVO; import com.makeit.common.page.PageVO;
import com.makeit.dto.platform.device.PlatDeviceDTO; import com.makeit.dto.platform.device.PlatDeviceDTO;
import com.makeit.dto.platform.space.PlatBedDeviceQueryDTO; import com.makeit.dto.platform.space.*;
import com.makeit.dto.platform.space.PlatBedEditDTO;
import com.makeit.dto.platform.space.PlatBedQueryDTO;
import com.makeit.dto.platform.space.PlatSpaceDeviceQueryDTO;
import com.makeit.entity.platform.device.PlatDevice; import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.entity.platform.space.PlatBed; import com.makeit.entity.platform.space.PlatBed;
import com.makeit.entity.platform.space.PlatRoom; import com.makeit.entity.platform.space.PlatRoom;
...@@ -206,4 +203,9 @@ public class PlatBedServiceImpl extends ServiceImpl<PlatBedMapper, PlatBed> impl ...@@ -206,4 +203,9 @@ public class PlatBedServiceImpl extends ServiceImpl<PlatBedMapper, PlatBed> impl
} }
return data; return data;
} }
@Override
public void bindDevice(PlatRoomBindDeviceDTO dto) {
}
} }
...@@ -206,7 +206,7 @@ implements PlatTenantService { ...@@ -206,7 +206,7 @@ implements PlatTenantService {
//新租户同步到iot //新租户同步到iot
PlatTenantVO platTenantVO = new PlatTenantVO(); PlatTenantVO platTenantVO = new PlatTenantVO();
platTenantVO.setName(dto.getName()); platTenantVO.setName(dto.getName());
OrganizationEntity organizationEntity = iotOrgService.syncTenantInfoToIot(platTenantVO); OrganizationEntity organizationEntity = iotOrgService.addIotOrg(platTenantVO);
tntTenant.setIotOrgId(organizationEntity.getId()); tntTenant.setIotOrgId(organizationEntity.getId());
save(tntTenant); save(tntTenant);
...@@ -235,6 +235,8 @@ implements PlatTenantService { ...@@ -235,6 +235,8 @@ implements PlatTenantService {
PlatTenant platTenant = getById(tntTenant.getId()); PlatTenant platTenant = getById(tntTenant.getId());
updateById(tntTenant); updateById(tntTenant);
iotOrgService.updateIotOrgInfo(tntTenant.getIotOrgId(),dto.getName());
//更新用户的tenantId //更新用户的tenantId
if(!StringUtils.equals(dto.getPlatUserId(),platTenant.getPlatUserId())) { if(!StringUtils.equals(dto.getPlatUserId(),platTenant.getPlatUserId())) {
platUserService.updatePlatUserTenantId(null, dto.getPlatUserId()); platUserService.updatePlatUserTenantId(null, dto.getPlatUserId());
......
...@@ -23,7 +23,7 @@ public class IotTest { ...@@ -23,7 +23,7 @@ public class IotTest {
void syncTenantInfoToIot() { void syncTenantInfoToIot() {
PlatTenantVO platTenantVO = new PlatTenantVO(); PlatTenantVO platTenantVO = new PlatTenantVO();
platTenantVO.setName("lxl2"); platTenantVO.setName("lxl2");
iotOrgService.syncTenantInfoToIot(platTenantVO); iotOrgService.addIotOrg(platTenantVO);
} }
...@@ -49,6 +49,26 @@ public class IotTest { ...@@ -49,6 +49,26 @@ public class IotTest {
@Test @Test
void getDeviceLog() { void getDeviceLog() {
iotProductDeviceService.getDeviceLog("1694547143952007168"); iotProductDeviceService.getDeviceLog("1694547143952007168", 10, "");
}
@Test
void getDeviceLogByType() {
iotProductDeviceService.getDeviceLogByType("1694547143952007168", "online");
}
@Test
void getLastDeviceLog() {
iotProductDeviceService.getLastDeviceLog("1694547143952007168");
}
@Test
void getIotOrgInfo() {
iotOrgService.getIotOrgInfo("1698964909267415040");
}
@Test
void updateIotOrgInfo() {
iotOrgService.updateIotOrgInfo("1698964909267415040","lxl2234");
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment