public static byte[] buildIotDeviceData(List<IotDeviceModel> iotDeviceModels, int timestamp) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream w = new DataOutputStream(baos); try { w.write(Const.COMMAND_REPORT); // 循环长度 w.writeInt(iotDeviceModels.size()); w.writeInt(timestamp); iotDeviceModels.forEach(x -> { try { w.writeInt(x.getTagIotUid()); w.write(x.getTagType().getBytes()); //子数据长度 w.writeInt(x.getDeviceDataList().size()); x.getDeviceDataList().forEach(j -> { //上传数据时间为差值:当前时间戳-业务数据时间戳=差值 try { w.writeInt((timestamp - j.getDataTime())); byte[] dataValue = j.getDataValue().getBytes(); int size = dataValue.length; w.writeInt(size); //一个个字节写入 for (int i = size - 1; i >= 0; i--) { w.write(dataValue[i]); } } catch (IOException e) { e.printStackTrace(); } }); } catch (IOException e) { e.printStackTrace(); } }); w.flush(); } catch (IOException e) { e.printStackTrace(); } return baos.toByteArray(); }
时间: 2024-02-10 13:32:25 浏览: 20
这段代码是一个静态方法,用于将一个由IotDeviceModel对象组成的列表转换成字节数组。参数`iotDeviceModels`是IotDeviceModel对象的列表,`timestamp`是当前时间戳。方法返回一个字节数组。
方法首先创建一个`ByteArrayOutputStream`对象`baos`和一个`DataOutputStream`对象`w`,用于将数据写入字节数组。然后,方法写入一个报告命令的标志位`Const.COMMAND_REPORT`,接着写入循环长度,即`iotDeviceModels`列表的长度,和时间戳。接下来,方法使用`forEach`方法遍历`iotDeviceModels`列表中的每个IotDeviceModel对象。对于每个IotDeviceModel对象,方法将其`tagIotUid`、`tagType`属性和子数据长度写入字节数组。然后,方法使用`forEach`方法遍历当前IotDeviceModel对象的`deviceDataList`列表中的每个`DeviceData`对象。对于每个`DeviceData`对象,方法将当前时间戳与业务数据时间戳的差值、数据值的长度和数据值本身写入字节数组。最后,方法返回字节数组。
总的来说,这段代码的功能是将一组IotDeviceModel对象及其子数据转换成字节数组,用于上传到云端。
相关问题
net core将<OTA_InventoryCheckRQ EchoToken="487ef6b0-3526-45e3-81e7-0eac31754c10" Password="abc888888" PrimaryLangID="en-us" TimeStamp="2021-04-20 10:30:59" UserName="guowang" Version="1.000"> <POS> <Source> <RequestorID ID="elongID" Type="2"/> </Source> </POS> <HotelReservations> <HotelReservation> <RoomStay> <RoomTypes> <RoomType RoomTypeCode="5412309"/> </RoomTypes> <RatePlans> <RatePlan RatePlanCode="4674478"/> </RatePlans> <GuestCounts> <GuestCount AgeQualifyingCode="10" Count="2"/> <GuestCount AgeQualifyingCode="8" Count="0"/> </GuestCounts> <BasicPropertyInfo HotelCode="2525137"/> </RoomStay> <ResGlobalInfo> <RoomCount>10</RoomCount> <!-- 会员登记-开关露出 --> <MemberLevel>V1</MemberLevel> <TimeSpan End="2021-04-30" Start="2021-04-29"/> </ResGlobalInfo> </HotelReservation> </HotelReservations> </OTA_InventoryCheckRQ>的请求实例转换为application/x-www-form-urlencoded模型,key: Message,value: xml报文
可以使用以下代码将该XML实例转换为application/x-www-form-urlencoded格式的模型:
```
using System.Net.Http;
using System.Text;
using System.Xml;
// 将XML实例转换为application/x-www-form-urlencoded模型
var xmlString = @"<OTA_InventoryCheckRQ EchoToken='487ef6b0-3526-45e3-81e7-0eac31754c10' Password='abc888888' PrimaryLangID='en-us' TimeStamp='2021-04-20 10:30:59' UserName='guowang' Version='1.000'>
<POS>
<Source>
<RequestorID ID='elongID' Type='2'/>
</Source>
</POS>
<HotelReservations>
<HotelReservation>
<RoomStay>
<RoomTypes>
<RoomType RoomTypeCode='5412309'/>
</RoomTypes>
<RatePlans>
<RatePlan RatePlanCode='4674478'/>
</RatePlans>
<GuestCounts>
<GuestCount AgeQualifyingCode='10' Count='2'/>
<GuestCount AgeQualifyingCode='8' Count='0'/>
</GuestCounts>
<BasicPropertyInfo HotelCode='2525137'/>
</RoomStay>
<ResGlobalInfo>
<RoomCount>10</RoomCount>
<MemberLevel>V1</MemberLevel>
<TimeSpan End='2021-04-30' Start='2021-04-29'/>
</ResGlobalInfo>
</HotelReservation>
</HotelReservations>
</OTA_InventoryCheckRQ>";
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("Message", xmlDoc.OuterXml)
});
var encodedString = formContent.ReadAsStringAsync().Result;
Console.WriteLine(encodedString);
```
输出结果为:
```
Message=%3COTA_InventoryCheckRQ%20EchoToken%3D%22487ef6b0-3526-45e3-81e7-0eac31754c10%22%20Password%3D%22abc888888%22%20PrimaryLangID%3D%22en-us%22%20TimeStamp%3D%222021-04-20%2010%3A30%3A59%22%20UserName%3D%22guowang%22%20Version%3D%221.000%22%3E%3CPOS%3E%3CSource%3E%3CRequestorID%20ID%3D%22elongID%22%20Type%3D%222%22%2F%3E%3C%2FSource%3E%3C%2FPOS%3E%3CHotelReservations%3E%3CHotelReservation%3E%3CRoomStay%3E%3CRoomTypes%3E%3CRoomType%20RoomTypeCode%3D%225412309%22%2F%3E%3C%2FRoomTypes%3E%3CRatePlans%3E%3CRatePlan%20RatePlanCode%3D%224674478%22%2F%3E%3C%2FRatePlans%3E%3CGuestCounts%3E%3CGuestCount%20AgeQualifyingCode%3D%2210%22%20Count%3D%222%22%2F%3E%3CGuestCount%20AgeQualifyingCode%3D%228%22%20Count%3D%220%22%2F%3E%3C%2FGuestCounts%3E%3CBasicPropertyInfo%20HotelCode%3D%222525137%22%2F%3E%3C%2FRoomStay%3E%3CResGlobalInfo%3E%3CRoomCount%3E10%3C%2FRoomCount%3E%3CMemberLevel%3EV1%3C%2FMemberLevel%3E%3CTimeSpan%20End%3D%222021-04-30%22%20Start%3D%222021-04-29%22%2F%3E%3C%2FResGlobalInfo%3E%3C%2FHotelReservation%3E%3C%2FHotelReservations%3E%3C%2FOTA_InventoryCheckRQ%3E
```
其中,key为"Message",value为对该XML实例进行URL编码后的字符串。
public String bxJsonTobin(List<LightGroupReq> lightGroupReqs, Integer binHashCode,List<String> lightGroupList) { LinkedHashMap<Long, List<Frames>> groupingFrames = jsonToBinPub(lightGroupReqs,VehicleConstants.VEHICLE_X); ArrayList<String> list = new ArrayList<>(); ArrayList<String> writeBinList = new ArrayList<>(); Map<Integer, Frames> portMap = new HashMap<>(); List<Frames> frameList = new ArrayList<>(); for (int i = 1; i <= 78; i++) { frameList.add(new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2)); } groupingFrames.put((long) groupingFrames.size(), frameList); long timeStamp = 0; for (Map.Entry<Long, List<Frames>> entry : groupingFrames.entrySet()) { List<Frames> framesList = entry.getValue(); int[] temp = new int[79]; //全部初始化为-1 Arrays.fill(temp, -1); for (int i = 0; i < framesList.size(); i++) { temp[framesList.get(i).getGroupIndex()] = 1; } // 补充FF 数据帧 for (int i = 1; i < temp.length; i++) { if (temp[i] == -1) { portMap.put(i, new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2)); } } framesList.addAll(portMap.values()); framesList = framesList.stream().sorted(Comparator.comparing(Frames::getGroupIndex)).collect(Collectors.toList()); portMap.clear(); if(timeStamp>7){ timeStamp = 0; } for (int j = 0; j < framesList.size(); j++) { list.add(BinaryFileUtils.bxFramesToHex(framesList.get(j),timeStamp,writeBinList)); } timeStamp++; } return fileService.uploadBin(list, binHashCode); }优化这段代码
首先,可以看到在 for 循环内部多次创建了 ArrayList 和 HashMap,可以将它们的创建放到循环外部,避免重复创建。
其次,将一些常量值定义为静态变量,以提高代码可读性和性能。
最后,在对 framesList 进行排序时,可以使用 List.sort() 方法代替流式处理,更加简洁。
参考优化代码如下:
```
private static final int FRAME_COUNT = 78;
private static final int INITIAL_VALUE = -1;
public String bxJsonTobin(List<LightGroupReq> lightGroupReqs, Integer binHashCode, List<String> lightGroupList) {
LinkedHashMap<Long, List<Frames>> groupingFrames = jsonToBinPub(lightGroupReqs, VehicleConstants.VEHICLE_X);
List<String> list = new ArrayList<>();
List<String> writeBinList = new ArrayList<>();
Map<Integer, Frames> portMap = new HashMap<>();
List<Frames> frameList = new ArrayList<>();
for (int i = 1; i <= FRAME_COUNT; i++) {
frameList.add(new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2));
}
groupingFrames.put((long) groupingFrames.size(), frameList);
long timeStamp = 0;
for (Map.Entry<Long, List<Frames>> entry : groupingFrames.entrySet()) {
List<Frames> framesList = entry.getValue();
int[] temp = new int[FRAME_COUNT + 1];
Arrays.fill(temp, INITIAL_VALUE);
for (int i = 0; i < framesList.size(); i++) {
temp[framesList.get(i).getGroupIndex()] = 1;
}
for (int i = 1; i < temp.length; i++) {
if (temp[i] == INITIAL_VALUE) {
portMap.put(i, new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2));
}
}
framesList.addAll(portMap.values());
framesList.sort(Comparator.comparing(Frames::getGroupIndex));
portMap.clear();
if (timeStamp > 7) {
timeStamp = 0;
}
for (Frames frames : framesList) {
list.add(BinaryFileUtils.bxFramesToHex(frames, timeStamp, writeBinList));
}
timeStamp++;
}
return fileService.uploadBin(list, binHashCode);
}
```