Commit 27b79990 by xmh

大端模式改为小端模式

1 parent 360652c3
...@@ -32,10 +32,10 @@ public class BaseModel { ...@@ -32,10 +32,10 @@ public class BaseModel {
public static BaseModel from(ByteBuf byteBuf) { public static BaseModel from(ByteBuf byteBuf) {
long deviceId = byteBuf.readUnsignedInt(); long deviceId = byteBuf.readUnsignedIntLE();
long length = byteBuf.readUnsignedInt(); long length = byteBuf.readUnsignedIntLE();
long firstInt = byteBuf.readUnsignedInt(); long firstInt = byteBuf.readUnsignedIntLE();
long secondInt = byteBuf.readUnsignedInt(); long secondInt = byteBuf.readUnsignedIntLE();
BaseModel model; BaseModel model;
switch ((int) firstInt) { switch ((int) firstInt) {
...@@ -65,15 +65,6 @@ public class BaseModel { ...@@ -65,15 +65,6 @@ public class BaseModel {
return (int) data; return (int) data;
} }
public static byte[] toBytes(int n) {
byte[] b = new byte[4];
b[3] = (byte) (n & 0xff);
b[2] = (byte) (n >> 8 & 0xff);
b[1] = (byte) (n >> 16 & 0xff);
b[0] = (byte) (n >> 24 & 0xff);
return b;
}
/** /**
* 将 model 转换为 byteBuf * 将 model 转换为 byteBuf
* *
...@@ -82,10 +73,10 @@ public class BaseModel { ...@@ -82,10 +73,10 @@ public class BaseModel {
public void to(ByteBuf byteBuf) { public void to(ByteBuf byteBuf) {
encodeData(); encodeData();
byteBuf.writeInt(toInt(deviceId)); byteBuf.writeIntLE(toInt(deviceId));
byteBuf.writeInt(toInt(length)); byteBuf.writeIntLE(toInt(length));
byteBuf.writeInt(toInt(protocol)); byteBuf.writeIntLE(toInt(protocol));
byteBuf.writeInt(toInt(flag)); byteBuf.writeIntLE(toInt(flag));
if (data != null && data.length > 0) { if (data != null && data.length > 0) {
byteBuf.writeBytes(data); byteBuf.writeBytes(data);
} }
......
...@@ -2,6 +2,10 @@ package com.viontech.model; ...@@ -2,6 +2,10 @@ package com.viontech.model;
import com.fasterxml.jackson.core.util.ByteArrayBuilder; import com.fasterxml.jackson.core.util.ByteArrayBuilder;
import com.viontech.Application; import com.viontech.Application;
import com.viontech.utils.Utils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
...@@ -59,43 +63,45 @@ public class BehaviorModel extends BaseModel { ...@@ -59,43 +63,45 @@ public class BehaviorModel extends BaseModel {
this.length = 16L + 448L + picture.length; this.length = 16L + 448L + picture.length;
this.protocol = 0x00040006L; this.protocol = 0x00040006L;
this.flag = Application.REAL_TIME; this.flag = Application.REAL_TIME;
ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
buffer.writeIntLE(serialNum);
buffer.writeIntLE(recordingStartSecond);
buffer.writeIntLE(recordingStartMillisecond);
buffer.writeIntLE(recordingEndSecond);
buffer.writeIntLE(recordingEndMillisecond);
buffer.writeIntLE(eventStartSecond);
buffer.writeIntLE(eventStartMillisecond);
buffer.writeIntLE(eventEndSecond);
buffer.writeIntLE(eventEndMillisecond);
buffer.writeIntLE(laneNo);
buffer.writeIntLE(pictureSize);
buffer.writeIntLE(pictureWidth);
buffer.writeIntLE(pictureHeight);
buffer.writeIntLE(x);
buffer.writeIntLE(y);
buffer.writeBytes(licensePlateText);
buffer.writeBytes(recordingPath);
buffer.writeIntLE(vehicleType);
buffer.writeIntLE(color1);
buffer.writeIntLE(speed);
buffer.writeIntLE(direction);
buffer.writeBytes(eventLocation);
buffer.writeBytes(eventSnapshotPath);
buffer.writeIntLE(color2);
buffer.writeIntLE(color3);
buffer.writeIntLE(color1Weights);
buffer.writeIntLE(color2Weights);
buffer.writeIntLE(color3Weights);
buffer.writeIntLE(vehicleCategory);
buffer.writeIntLE(reservedField);
buffer.writeIntLE(recordingDuration);
buffer.writeIntLE(faceWidth);
buffer.writeIntLE(faceHeight);
buffer.writeBytes(reservedField2);
buffer.writeBytes(picture);
ByteArrayBuilder builder = new ByteArrayBuilder(Math.toIntExact(this.length - 16)); this.data = new byte[buffer.readableBytes()];
builder.appendFourBytes(serialNum); buffer.readBytes(this.data);
builder.appendFourBytes(recordingStartSecond); buffer.release();
builder.appendFourBytes(recordingStartMillisecond);
builder.appendFourBytes(recordingEndSecond);
builder.appendFourBytes(recordingEndMillisecond);
builder.appendFourBytes(eventStartSecond);
builder.appendFourBytes(eventStartMillisecond);
builder.appendFourBytes(eventEndSecond);
builder.appendFourBytes(eventEndMillisecond);
builder.appendFourBytes(laneNo);
builder.appendFourBytes(pictureSize);
builder.appendFourBytes(pictureWidth);
builder.appendFourBytes(pictureHeight);
builder.appendFourBytes(x);
builder.appendFourBytes(y);
builder.write(licensePlateText);
builder.write(recordingPath);
builder.appendFourBytes(vehicleType);
builder.appendFourBytes(color1);
builder.appendFourBytes(speed);
builder.appendFourBytes(direction);
builder.write(eventLocation);
builder.write(eventSnapshotPath);
builder.appendFourBytes(color2);
builder.appendFourBytes(color3);
builder.appendFourBytes(color1Weights);
builder.appendFourBytes(color2Weights);
builder.appendFourBytes(color3Weights);
builder.appendFourBytes(vehicleCategory);
builder.appendFourBytes(reservedField);
builder.appendFourBytes(recordingDuration);
builder.appendFourBytes(faceWidth);
builder.appendFourBytes(faceHeight);
builder.write(reservedField2);
builder.write(picture);
this.data = builder.resetAndGetFirstSegment();
} }
} }
package com.viontech.model; package com.viontech.model;
import com.fasterxml.jackson.core.util.ByteArrayBuilder;
import com.viontech.Application; import com.viontech.Application;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
...@@ -35,31 +36,32 @@ public class FlowModel extends BaseModel { ...@@ -35,31 +36,32 @@ public class FlowModel extends BaseModel {
this.protocol = 0x00040005L; this.protocol = 0x00040005L;
this.flag = Application.REAL_TIME; this.flag = Application.REAL_TIME;
ByteArrayBuilder builder = new ByteArrayBuilder(Math.toIntExact(this.length - 16));
builder.appendFourBytes(serialNum); ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
builder.appendFourBytes(time); buffer.writeIntLE(serialNum);
builder.appendFourBytes(timeDuring); buffer.writeIntLE(time);
for (int value : laneNoAndType) { buffer.writeIntLE(timeDuring);
builder.appendFourBytes(value); for (int i : laneNoAndType) {
buffer.writeIntLE(i);
} }
for (int value : traffic) { for (int i : traffic) {
builder.appendFourBytes(value); buffer.writeIntLE(i);
} }
for (int value : avgSpeed) { for (int i : avgSpeed) {
builder.appendFourBytes(value); buffer.writeIntLE(i);
} }
for (int value : maxQueueLen) { for (int i : maxQueueLen) {
builder.appendFourBytes(value); buffer.writeIntLE(i);
} }
for (int value : avgDistanceBetweenVehicle) { for (int i : avgDistanceBetweenVehicle) {
builder.appendFourBytes(value); buffer.writeIntLE(i);
} }
for (int value : trafficSupplement) { for (int i : trafficSupplement) {
builder.appendFourBytes(value); buffer.writeIntLE(i);
} }
builder.write(reservedField); buffer.writeBytes(reservedField);
this.data = new byte[buffer.readableBytes()];
this.data = builder.resetAndGetFirstSegment(); buffer.readBytes(this.data);
buffer.release();
} }
} }
package com.viontech.model; package com.viontech.model;
import com.fasterxml.jackson.core.util.ByteArrayBuilder; import com.fasterxml.jackson.core.util.ByteArrayBuilder;
import com.sun.xml.internal.stream.util.BufferAllocator;
import com.viontech.Application; import com.viontech.Application;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
...@@ -99,53 +102,54 @@ public class TrafficModel extends BaseModel { ...@@ -99,53 +102,54 @@ public class TrafficModel extends BaseModel {
this.protocol = 0x00020004L; this.protocol = 0x00020004L;
this.length = 16L + 452L + picture1.length + picture2.length + 4; this.length = 16L + 452L + picture1.length + picture2.length + 4;
this.flag = Application.REAL_TIME; this.flag = Application.REAL_TIME;
ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
buffer.writeIntLE(serialNum);
buffer.writeIntLE(discernTime);
buffer.writeIntLE(discernMillisecondTime);
buffer.writeBytes(plateText);
buffer.writeIntLE(plateType);
buffer.writeIntLE(confidence);
buffer.writeIntLE(lane);
buffer.writeIntLE(vehicleType);
buffer.writeIntLE(secondPictureSize);
buffer.writeIntLE(secondPictureWidth);
buffer.writeIntLE(secondPictureHeight);
buffer.writeIntLE(firstPictureSize);
buffer.writeIntLE(firstPictureWidth);
buffer.writeIntLE(firstPictureHeight);
buffer.writeIntLE(patternLeft);
buffer.writeIntLE(patternTop);
buffer.writeIntLE(patternRight);
buffer.writeIntLE(patternBottom);
buffer.writeIntLE(color);
buffer.writeIntLE(speed);
buffer.writeIntLE(direction);
buffer.writeIntLE(manufacturerLogo);
buffer.writeBytes(routeLocation);
buffer.writeBytes(videoPath);
buffer.writeBytes(picturePath);
buffer.writeIntLE(reservedField);
buffer.writeIntLE(reservedField2);
buffer.writeIntLE(reservedField3);
buffer.writeIntLE(vehicleCategory);
buffer.writeIntLE(plateStructure);
buffer.writeIntLE(illegalType);
buffer.writeIntLE(reservedField4);
buffer.writeIntLE(secondPictureTime);
buffer.writeIntLE(secondPictureMillisecondTime);
buffer.writeIntLE(redLightBeginTime);
buffer.writeIntLE(redLightBeginMillisecondTime);
buffer.writeIntLE(redLightEndTime);
buffer.writeIntLE(redLightEndMillisecondTime);
buffer.writeIntLE(speedLimit);
buffer.writeIntLE(overSpeedThreshold);
buffer.writeBytes(reservedField5);
buffer.writeBytes(picture1);
buffer.writeBytes(picture2);
byte[] bytes = new byte[buffer.readableBytes()];
buffer.readBytes(bytes);
buffer.release();
ByteArrayBuilder builder = new ByteArrayBuilder(Math.toIntExact(this.length - 16 - 4));
builder.appendFourBytes(serialNum);
builder.appendFourBytes(discernTime);
builder.appendFourBytes(discernMillisecondTime);
builder.write(plateText);
builder.appendFourBytes(plateType);
builder.appendFourBytes(confidence);
builder.appendFourBytes(lane);
builder.appendFourBytes(vehicleType);
builder.appendFourBytes(secondPictureSize);
builder.appendFourBytes(secondPictureWidth);
builder.appendFourBytes(secondPictureHeight);
builder.appendFourBytes(firstPictureSize);
builder.appendFourBytes(firstPictureWidth);
builder.appendFourBytes(firstPictureHeight);
builder.appendFourBytes(patternLeft);
builder.appendFourBytes(patternTop);
builder.appendFourBytes(patternRight);
builder.appendFourBytes(patternBottom);
builder.appendFourBytes(color);
builder.appendFourBytes(speed);
builder.appendFourBytes(direction);
builder.appendFourBytes(manufacturerLogo);
builder.write(routeLocation);
builder.write(videoPath);
builder.write(picturePath);
builder.appendFourBytes(reservedField);
builder.appendFourBytes(reservedField2);
builder.appendFourBytes(reservedField3);
builder.appendFourBytes(vehicleCategory);
builder.appendFourBytes(plateStructure);
builder.appendFourBytes(illegalType);
builder.appendFourBytes(reservedField4);
builder.appendFourBytes(secondPictureTime);
builder.appendFourBytes(secondPictureMillisecondTime);
builder.appendFourBytes(redLightBeginTime);
builder.appendFourBytes(redLightBeginMillisecondTime);
builder.appendFourBytes(redLightEndTime);
builder.appendFourBytes(redLightEndMillisecondTime);
builder.appendFourBytes(speedLimit);
builder.appendFourBytes(overSpeedThreshold);
builder.write(reservedField5);
builder.write(picture1);
builder.write(picture2);
byte[] bytes = builder.resetAndGetFirstSegment();
int count = bytes.length; int count = bytes.length;
// crc32 // crc32
CRC32 crc32 = new CRC32(); CRC32 crc32 = new CRC32();
......
...@@ -37,10 +37,11 @@ public class NettyReceiverHandler extends ChannelInboundHandlerAdapter { ...@@ -37,10 +37,11 @@ public class NettyReceiverHandler extends ChannelInboundHandlerAdapter {
if (PropertiesConfiguration.username.equals(logindata.getUsername()) && if (PropertiesConfiguration.username.equals(logindata.getUsername()) &&
PropertiesConfiguration.password.equals(logindata.getPassword())) { PropertiesConfiguration.password.equals(logindata.getPassword())) {
log.info("注册成功");
ChannelGroup.registered(deviceId, ctx.channel()); ChannelGroup.registered(deviceId, ctx.channel());
result.setFlag(0L); result.setFlag(0L);
} else { } else {
log.info("注册失败,用户名:[{}],密码:[{}]", logindata.getUsername(), logindata.getPassword());
result.setFlag(1L); result.setFlag(1L);
} }
ctx.writeAndFlush(result).await(); ctx.writeAndFlush(result).await();
......
...@@ -15,6 +15,8 @@ import org.springframework.beans.factory.annotation.Value; ...@@ -15,6 +15,8 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.nio.ByteOrder;
/** /**
* . * .
* *
...@@ -40,7 +42,8 @@ public class NettyServer implements CommandLineRunner { ...@@ -40,7 +42,8 @@ public class NettyServer implements CommandLineRunner {
.childHandler(new ChannelInitializer<SocketChannel>() { .childHandler(new ChannelInitializer<SocketChannel>() {
@Override @Override
protected void initChannel(SocketChannel ch) { protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024 * 1024 * 1000, 4, 4, -8, 0)); ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN, 1024 * 1024 * 1000, 4, 4, -8, 0, true));
ch.pipeline().addLast(new ByteToMessageCodecHandler()); ch.pipeline().addLast(new ByteToMessageCodecHandler());
ch.pipeline().addLast(new NettyReceiverHandler()); ch.pipeline().addLast(new NettyReceiverHandler());
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!