int64 recipe_id = 1;
string recipe_code = 2;
string dimension = 3;
repeated TempRecord oven_temp_list = 4;
repeated TempRecord probe_temp_list = 5;
}
message TempRecord {
int32 temp = 1;
int64 timestamp = 2;
}
注:protobuf属性解释可以参照另一篇文章,传送门:Protobuf 属性解释
在Java中的写法,代码如下:
@Override
public Boolean insertRecord(String record) throws InvalidProtocolBufferException {
//ProtoBuf组装
DCookRecordsProto.ProgramRecordValue.Builder value1 = DCookRecordsProto.ProgramRecordValue.newBuilder();
value1.setContentCode(“xxx”)
.setContentVersion(1)
.setDimension(“xx#xx”);
for (int i = 0; i < 2; i++) {
DCookRecordsProto.TempRecord.Builder tempRecord = DCookRecordsProto.TempRecord.newBuilder();
tempRecord.setTemp(100 + i + 1).setTimestamp(System.currentTimeMillis());
value1.addOvenTempList(tempRecord);
value1.addProbeTempList(tempRecord);
}
DCookRecordsProto.DCookRecord.Builder cookRecord = DCookRecordsProto.DCookRecord.newBuilder();
cookRecord.setRecordType(1)
.setRecordProducer(“d”)
.setRecordStartTime(System.currentTimeMillis())
.setRecordEndTime(System.currentTimeMillis())
.setRecordValue(Any.pack(value1.build()));
DCookRecordsProto.DCookRecords.Builder cookRecords = DCookRecordsProto.DCookRecords.newBuilder();
cookRecords.addRecords(cookRecord);
byte[] bytes = cookRecords.build().toByteArray();
//Base64 Encoded
String encoded = Base64.getEncoder().encodeToString(bytes);
System.out.println(“---------encoded----------”);
System.out.println(encoded);
//ProtoBuf反解析
final String did = LoginUtils.getDid();
List recordPoList = Lists.newArrayList();
byte[] decoded = Base64.getDecoder().decode(record);
DCookRecordsProto.DCookRecords records = DCookRecordsProto.DCookRecords.parseFrom(decoded);
if (!(ObjectUtils.isEmpty(records) || CollectionUtils.isEmpty(records.getRecordsList()))) {
for (DCookRecordsProto.DCookRecord record1 : records.getRecordsList()) {
RmCookRecordPo recordPo = new RmCookRecordPo();
recordPo.setDid(did);
if (record1.getRecordStartTime() > 0) {
Instant instant = Instant.ofEpochMilli(record1.getRecordStartTime());
recordPo.setRecordStartTime(LocalDateTime.ofInstant(instant, ZoneId.systemDefault()));
}
if (record1.getRecordEndTime() > 0) {
Instant instant = Instant.ofEpochMilli(record1.getRecordEndTime());
recordPo.setRecordEndTime(LocalDateTime.ofInstant(instant, ZoneId.systemDefault()));
}
recordPo.setRecordProducer(record1.getRecordProducer());
recordPo.setRecordType(record1.getRecordType());
if (record1.getRecordType() == 1 || record1.getRecordType() == 2) {
DCookRecordsProto.ProgramRecordValue recordValue = record1.getRecordValue()
.unpack(DCookRecordsProto.ProgramRecordValue.class);
ProgramRecordValue programRecordValue = new ProgramRecordValue();
programRecordValue.setContentCode(recordValue.getContentCode());
programRecordValue.setContentVersion(recordValue.getContentVersion());
programRecordValue.setDimension(recordValue.getDimension());
if (!CollectionUtils.isEmpty(recordValue.getOvenTempListList())) {
List tempRecordList = Lists.newArrayList();
for (DCookRecordsProto.TempRecord ovenTemp : recordValue.getOvenTempListList()) {
TempRecord tempRecord = new TempRecord();
tempRecord.setTemp(ovenTemp.getTemp());
tempRecord.setTimestamp(ovenTemp.getTimestamp());
tempRecordList.add(tempRecord);
}
programRecordValue.setOvenTempListList(tempRecordList);
}
if (!CollectionUtils.isEmpty(recordValue.getProbeTempListList())) {
List tempRecordList = Lists.newArrayList();
for (DCookRecordsProto.TempRecord protoTemp : recordValue.getProbeTempListList()) {
TempRecord tempRecord = new TempRecord();
tempRecord.setTemp(protoTemp.getTemp());
tempRecord.setTimestamp(protoTemp.getTimestamp());
tempRecordList.add(tempRecord);
}
programRecordValue.setProbeTempListList(tempRecordList);
}
recordPo.setRecordValue(JSONObject.toJSONString(programRecordValue));
} else if (record1.getRecordType() == 2) {
DCookRecordsProto.RecipeRecordValue recordValue = record1.getRecordValue()
.unpack(DCookRecordsProto.RecipeRecordValue.class);
RecipeRecordValue recipeRecordValue = new RecipeRecordValue();
recipeRecordValue.setRecipeId(recordValue.getRecipeId());
recipeRecordValue.setRecipeCode(recordValue.getRecipeCode());
recipeRecordValue.setDimension(recordValue.getDimension());
if (!CollectionUtils.isEmpty(recordValue.getOvenTempListList())) {
List tempRecordList = Lists.newArrayList();
for (DCookRecordsProto.TempRecord ovenTemp : recordValue.getOvenTempListList()) {
TempRecord tempRecord = new TempRecord();
tempRecord.setTemp(ovenTemp.getTemp());
tempRecord.setTimestamp(ovenTemp.getTimestamp());
tempRecordList.add(tempRecord);
}
recipeRecordValue.setOvenTempListList(tempRecordList);
}
if (!CollectionUtils.isEmpty(recordValue.getProbeTempListList())) {
List tempRecordList = Lists.newArrayList();
for (DCookRecordsProto.TempRecord protoTemp : recordValue.getProbeTempListList()) {
TempRecord tempRecord = new TempRecord();
tempRecord.setTemp(protoTemp.getTemp());
tempRecord.setTimestamp(protoTemp.getTimestamp());
tempRecordList.add(tempRecord);
}
recipeRecordValue.setProbeTempListList(tempRecordList);
}
recordPo.setRecordValue(JSONObject.toJSONString(recipeRecordValue));
}