{
"specs": [
{
"dataType": {
"specs": {
"step": 1
},
"type": "date"
}
},
{
"dataType": {
"specs": {
"length": 128,
"rule": ""
},
"type": "text"
}
},
{
"dataType": {
"specs": [
{
"dataType": {
"specs": {
"unit": "min",
"range": [
1,
2
],
"step": 1
},
"type": "int"
}
}
],
"type": "struct"
}
}
],
"type": "struct"
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.CollectionUtils;
import java.util.HashMap;
public class ThingModelConvettUtil {
/**
* 事件类型
*/
private final String TYPE = "type";
/**
* 数据类型
*/
private final String DATA_TYPE = "dataType";
/**
* 最小值
*/
private final String MIN = "min";
/**
* 最大值
*/
private final String MAX = "max";
/**
* 步长
*/
private final String STEP = "step";
/**
* 单位
*/
private final String UNIT = "unit";
/**
* 单位名称
*/
private final String UNIT_NAME = "unitName";
/**
* 正则表达式
*/
private final String RULE = "rule";
// 数组specs 中的元素
private final String ITEM = "item";
private final String SPECS = "specs";
private HashMap<String, Integer> dataTypeMap = new HashMap<>();
// bool text int float array struct enum date
private void initDataType() {
dataTypeMap.put("Bytes", 0);
dataTypeMap.put("float", 1);
dataTypeMap.put("int", 2);
dataTypeMap.put("text", 3);
dataTypeMap.put("date", 4);
dataTypeMap.put("bool", 5);
dataTypeMap.put("text", 6);
dataTypeMap.put("array", 7);
dataTypeMap.put("struct", 8);
}
// 处理数组类型的specs
private JSONObject dealSpecs4Array(String arraySpecs) {
JSONObject jsonObject = JSON.parseObject(arraySpecs);
JSONObject itemObject = jsonObject.getJSONObject(ITEM);
JSONObject dataTypeObject = itemObject.getJSONObject(DATA_TYPE);
int type = dataTypeObject.getIntValue(TYPE);
dataTypeObject.put(TYPE, dataTypeMap.get(type));
if (type == 8) {
JSONArray array = dealSpecs4Struct(dataTypeObject.getString(SPECS));
dataTypeObject.put(SPECS, array);
}
return jsonObject;
}
// 处理结构体类型的specs
private JSONArray dealSpecs4Struct(String specs) {
JSONArray struct = JSON.parseArray(specs);
if (CollectionUtils.isEmpty(struct)) {
return new JSONArray();
}
for (int i = 0; i < struct.size(); i++) {
JSONObject jsonObject = struct.getJSONObject(i);
JSONObject dataTypeObject = jsonObject.getJSONObject(DATA_TYPE);
Integer type = dataTypeObject.getInteger(TYPE);
String specsInner = dataTypeObject.getString(SPECS);
if (type == 8) {
JSONArray array = dealSpecs4Struct(specsInner);
dataTypeObject.remove(SPECS);
dataTypeObject.put(TYPE, systemConfig.dataTypeMap.get(type));
dataTypeObject.put(SPECS, array);
//System.err.println("结构体类型 array = " + array);
//System.err.println("结构体类型 jsonObject = " + jsonObject);
structArray.set(i, jsonObject);
}else if (type == 7) {
JSONObject arraySpecsObject = dealSpecs4Array(specsInner);
dataTypeObject.remove(SPECS);
dataTypeObject.put(SPECS, arraySpecsObject);
dataTypeObject.put(TYPE, dataTypeMap.get(type));
struct.set(i, jsonObject);
}
}
return struct;
}
private Object getObject(String specs, int type) {
Object specsAfterObject;
if (type == 8) {
JSONArray array = dealSpecs4Struct(specs);
specsAfterObject = array;
} else {
specsAfterObject = dealSpecs2JSONObject(type, specs);
}
return specsAfterObject;
}
private JSONObject dealSpecs2JSONObject(int type, String specs) {
JSONObject ret = new JSONObject();
switch (type) {
case 0:
JSONObject byteObject = JSON.parseObject(specs);
byteObject.remove(MIN);
byteObject.remove(MAX);
byteObject.remove(UNIT);
byteObject.remove(STEP);
ret = byteObject;
break;
case 1:
case 2:
ret = dealDataType4Numerical(specs);
break;
case 3:
JSONObject text = JSON.parseObject(specs);
text.put(RULE, "");
ret = text;
break;
case 4:
break;
case 5:
JSONObject bool = JSON.parseObject(specs);
ret = bool;
break;
case 6:
ret = JSON.parseObject(specs);
break;
case 7:
ret = dealSpecs4Array(specs);
break;
}
return ret;
}
// 处理数值类型
private JSONObject dealDataType4Numerical(String specs) {
// float、int 类型转换
JSONObject specsObject = JSON.parseObject(specs);
Object min = specsObject.get(MIN);
Object max = specsObject.get(MAX);
specsObject.remove(MIN);
specsObject.remove(MAX);
System.out.println("min " + min + " max: " + max);
specsObject.put(RANGE, new ArrayList<>(Arrays.asList(min, max)));
return specsObject;
}
}