Merge branch 'dev-V0.4.5' of https://gitee.com/iotkit-open-source/iotkit-parent into dev-V0.4.5

V0.5.x
xiwa 2023-06-13 08:28:26 +08:00
commit c6ea4a9759
6 changed files with 127 additions and 83 deletions

View File

@ -29,15 +29,16 @@ public class ThingModelMessage {
public static final String TYPE_LIFETIME = "lifetime"; public static final String TYPE_LIFETIME = "lifetime";
public static final String TYPE_STATE = "state"; public static final String TYPE_STATE = "state";
public static final String TYPE_PROPERTY = "property"; public static final String TYPE_PROPERTY = "property";
public static final String TYPE_EVENT = "event"; public static final String TYPE_EVENT = "event";
public static final String TYPE_SERVICE = "service"; public static final String TYPE_SERVICE = "service";
public static final String TYPE_CONFIG = "config";
public static final String TYPE_OTA = "ota";
public static final String TYPE_CONFIG = "config";
public static final String ID_PROPERTY_GET = "get"; public static final String ID_PROPERTY_GET = "get";
public static final String ID_PROPERTY_SET = "set"; public static final String ID_PROPERTY_SET = "set";
public static final String ID_CONFIG_GET = "get"; public static final String ID_CONFIG_GET = "get";
public static final String ID_CONFIG_SET = "set"; public static final String ID_CONFIG_SET = "set";
public static final String ID_DEREGISTER = "deregister"; public static final String ID_DEREGISTER = "deregister";
private String id; private String id;

View File

@ -23,6 +23,10 @@ public class OtaPackage implements Id<String> {
private String sign; private String sign;
private Boolean isDiff;
private String md5;
private String name; private String name;
private String desc; private String desc;

View File

@ -23,6 +23,10 @@ public class TbOtaPackage {
private String sign; private String sign;
private Boolean isDiff;
private String md5;
private String name; private String name;
private String desc; private String desc;

View File

@ -1,29 +1,28 @@
var mid = 1;
var mid=1; function getMid() {
function getMid(){
mid++; mid++;
if(mid>10000){ if (mid > 10000) {
mid=1; mid = 1;
} }
return mid+""; return mid + "";
} }
this.decode = function (msg) { this.decode = function (msg) {
//对msg进行解析并返回物模型数据 //对msg进行解析并返回物模型数据
var content=msg.content; var content = msg.content;
var topic = content.topic; var topic = content.topic;
var payload = content.payload; var payload = content.payload;
var identifier = topic.substring(topic.lastIndexOf("/") + 1); var identifier = topic.substring(topic.lastIndexOf("/") + 1);
//透传上报 //透传上报
if(topic.endsWith("/event/rawReport")){ if (topic.endsWith("/event/rawReport")) {
var rst= component.transparentDecode(payload.params); var rst = component.transparentDecode(payload.params);
if(!rst){ if (!rst) {
return null; return null;
} }
rst.occur=new Date().getTime(); rst.occur = new Date().getTime();
rst.time=new Date().getTime(); rst.time = new Date().getTime();
return rst; return rst;
} }
@ -33,55 +32,67 @@ this.decode = function (msg) {
mid: msg.mid, mid: msg.mid,
productKey: msg.productKey, productKey: msg.productKey,
deviceName: msg.deviceName, deviceName: msg.deviceName,
type:"property", type: "property",
identifier: "report", //属性上报 identifier: "report", //属性上报
occur: new Date().getTime(), //时间戳,设备上的事件或数据产生的本地时间 occur: new Date().getTime(), //时间戳,设备上的事件或数据产生的本地时间
time: new Date().getTime(), //时间戳,消息上报时间 time: new Date().getTime(), //时间戳,消息上报时间
data: payload.params, data: payload.params,
}; };
} else if (topic.indexOf("/ota/") > 0) {
//事件上报
return {
mid: msg.mid,
productKey: msg.productKey,
deviceName: msg.deviceName,
type: "ota",
identifier: "ota",
occur: new Date().getTime(),
time: new Date().getTime(),
data: payload.params,
};
} else if (topic.indexOf("/event/") > 0) { } else if (topic.indexOf("/event/") > 0) {
//事件上报 //事件上报
return { return {
mid: msg.mid, mid: msg.mid,
productKey: msg.productKey, productKey: msg.productKey,
deviceName: msg.deviceName, deviceName: msg.deviceName,
type:"event", type: "event",
identifier: identifier, identifier: identifier,
occur: new Date().getTime(), occur: new Date().getTime(),
time: new Date().getTime(), time: new Date().getTime(),
data: payload.params, data: payload.params,
}; };
}else if(topic.endsWith("/service/property/set_reply")){ } else if (topic.endsWith("/service/property/set_reply")) {
//属性设置回复 //属性设置回复
return { return {
mid: msg.mid, mid: msg.mid,
productKey: msg.productKey, productKey: msg.productKey,
deviceName: msg.deviceName, deviceName: msg.deviceName,
type:"property", type: "property",
identifier: identifier, identifier: identifier,
occur: new Date().getTime(), occur: new Date().getTime(),
time: new Date().getTime(), time: new Date().getTime(),
code: payload.code code: payload.code
}; };
}else if(topic.endsWith("/config/set_reply")){ } else if (topic.endsWith("/config/set_reply")) {
//设备配置设置回复 //设备配置设置回复
return { return {
mid: msg.mid, mid: msg.mid,
productKey: msg.productKey, productKey: msg.productKey,
deviceName: msg.deviceName, deviceName: msg.deviceName,
type:"config", type: "config",
identifier: "set_reply", identifier: "set_reply",
occur: new Date().getTime(), occur: new Date().getTime(),
time: new Date().getTime(), time: new Date().getTime(),
code: payload.code code: payload.code
}; };
}else if(topic.endsWith("/config/get")){ } else if (topic.endsWith("/config/get")) {
//设备配置获取 //设备配置获取
return { return {
mid: msg.mid, mid: msg.mid,
productKey: msg.productKey, productKey: msg.productKey,
deviceName: msg.deviceName, deviceName: msg.deviceName,
type:"config", type: "config",
identifier: "get", identifier: "get",
occur: new Date().getTime(), occur: new Date().getTime(),
time: new Date().getTime(), time: new Date().getTime(),
@ -93,7 +104,7 @@ this.decode = function (msg) {
mid: msg.mid, mid: msg.mid,
productKey: msg.productKey, productKey: msg.productKey,
deviceName: msg.deviceName, deviceName: msg.deviceName,
type:"service", type: "service",
identifier: identifier, identifier: identifier,
occur: new Date().getTime(), occur: new Date().getTime(),
time: new Date().getTime(), time: new Date().getTime(),
@ -104,86 +115,89 @@ this.decode = function (msg) {
return null; return null;
}; };
this.encode = function (service,device) { this.encode = function (service, device) {
var deviceMid=getMid(); var deviceMid = getMid();
var method="thing.service."; var method = "thing.service.";
var topic="/sys/"+service.productKey+"/"+service.deviceName+"/c/service/"; var topic = "/sys/" + service.productKey + "/" + service.deviceName + "/c/service/";
var params={}; var params = {};
//透传下发 //透传下发
if(device.transparent){ if (device.transparent) {
var rst=component.transparentEncode(service,device); var rst = component.transparentEncode(service, device);
topic="/sys/"+rst.productKey+"/"+rst.deviceName+"/c/service/rawSend"; topic = "/sys/" + rst.productKey + "/" + rst.deviceName + "/c/service/rawSend";
params.model=rst.content.model; params.model = rst.content.model;
params.deviceName=rst.content.deviceName; params.deviceName = rst.content.deviceName;
params.data=rst.content.data; params.data = rst.content.data;
return { return {
productKey:rst.productKey, productKey: rst.productKey,
deviceName:rst.deviceName, deviceName: rst.deviceName,
mid:rst.mid, mid: rst.mid,
content:{ content: {
topic:topic, topic: topic,
payload:JSON.stringify({ payload: JSON.stringify({
id:rst.mid, id: rst.mid,
method:method+"rawSend", method: method + "rawSend",
params:params params: params
}) })
} }
} }
} }
var type=service.type; var type = service.type;
var identifier=service.identifier; var identifier = service.identifier;
if(type=="property"){ if (type == "property") {
method+="property."+identifier; method += "property." + identifier;
topic+="property/"+identifier; topic += "property/" + identifier;
}else if(type=="service"){ } else if (type == "service") {
method+=identifier; method += identifier;
topic+=identifier; topic += identifier;
}else if(type=="config"){ } else if (type == "ota") {
method += identifier;
topic = "/ota/device/upgrade/" + service.productKey + "/" + service.deviceName;
} else if (type == "config") {
//设备配置下发 //设备配置下发
method+=identifier; method += identifier;
topic="/sys/"+service.productKey+"/"+service.deviceName+"/c/config/"+identifier; topic = "/sys/" + service.productKey + "/" + service.deviceName + "/c/config/" + identifier;
}else if(type="lifetime"){ } else if (type = "lifetime") {
//子设备注销下发 //子设备注销下发
method+=identifier; method += identifier;
topic="/sys/"+service.productKey+"/"+service.deviceName+"/c/deregister"; topic = "/sys/" + service.productKey + "/" + service.deviceName + "/c/deregister";
} }
if(type=="property" && identifier=="get" ){ if (type == "property" && identifier == "get") {
var listParams = [] var listParams = []
for(var p in service.params){ for (var p in service.params) {
listParams.push(service.params[p]); listParams.push(service.params[p]);
} }
return { return {
productKey:service.productKey, productKey: service.productKey,
deviceName:service.deviceName, deviceName: service.deviceName,
mid:deviceMid, mid: deviceMid,
content:{ content: {
topic:topic, topic: topic,
payload:JSON.stringify({ payload: JSON.stringify({
id:deviceMid, id: deviceMid,
method:method, method: method,
params: listParams params: listParams
}) })
} }
} }
}else{ } else {
for(var p in service.params){ for (var p in service.params) {
params[p]=service.params[p]; params[p] = service.params[p];
} }
return { return {
productKey:service.productKey, productKey: service.productKey,
deviceName:service.deviceName, deviceName: service.deviceName,
mid:deviceMid, mid: deviceMid,
content:{ content: {
topic:topic, topic: topic,
payload:JSON.stringify({ payload: JSON.stringify({
id:deviceMid, id: deviceMid,
method:method, method: method,
params:params params: params
}) })
} }
} }

View File

@ -73,8 +73,10 @@ public class DeviceService {
args, ThingModelMessage.TYPE_SERVICE, service); args, ThingModelMessage.TYPE_SERVICE, service);
} }
public String otaUpgrade(String token, boolean checkOwner) { public String otaUpgrade(String deviceId, boolean checkOwner, Object data) {
return null; DeviceInfo device = getAndCheckDevice(deviceId, checkOwner);
return send(deviceId, device.getProductKey(), device.getDeviceName(),
data, ThingModelMessage.TYPE_OTA, "OTA");
} }
/** /**

View File

@ -7,6 +7,7 @@ import cc.iotkit.data.manager.IOtaDeviceData;
import cc.iotkit.data.manager.IOtaPackageData; import cc.iotkit.data.manager.IOtaPackageData;
import cc.iotkit.model.alert.AlertConfig; import cc.iotkit.model.alert.AlertConfig;
import cc.iotkit.model.ota.OtaPackage; import cc.iotkit.model.ota.OtaPackage;
import com.google.gson.JsonObject;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -63,9 +64,27 @@ public class OtaService {
/** /**
* *
*/ */
public void startUpgrade() { public void startUpgrade(String otaId, String deviceId) {
OtaPackage otaPackage = iOtaPackageData.findById(otaId);
//构建升级包 //构建升级包
deviceService.otaUpgrade("", true); JsonObject buildOtaPackage = buildOtaPackage(otaPackage);
String id = deviceService.otaUpgrade(deviceId, true, buildOtaPackage);
}
private JsonObject buildOtaPackage(OtaPackage otaPackage) {
JsonObject ota = new JsonObject();
JsonObject extData = new JsonObject();
extData.addProperty("key1", "测试1");
extData.addProperty("key2", "测试2");
ota.addProperty("size", otaPackage.getSize());
ota.addProperty("sign", otaPackage.getSign());
ota.addProperty("version", otaPackage.getVersion());
ota.addProperty("isDiff", Boolean.toString(otaPackage.getIsDiff()));
ota.addProperty("url", otaPackage.getUrl());
ota.addProperty("signMethod", "MD5");
ota.addProperty("module", "MCU");
ota.add("extData", extData);
return ota;
} }
} }