Coder Social home page Coder Social logo

jetlinks / demo-protocol Goto Github PK

View Code? Open in Web Editor NEW
60.0 13.0 68.0 98 KB

此协议已弃用,请使用官方协议 https://github.com/jetlinks/jetlinks-official-protocol

Home Page: https://github.com/jetlinks/jetlinks-official-protocol

Java 100.00%

demo-protocol's Introduction

demo-protocol's People

Contributors

445990772 avatar 790712946 avatar city-north avatar jlleitschuh avatar wangzheng0723 avatar zhou-hao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

demo-protocol's Issues

TCP直连的产品物模型有多属性的设备demo吗

我现在在产品物模型里面添加了温度与湿度两个属性,然后在TemperatureReport类里面补充了湿度这个属性

@Getter
@Setter
@AllArgsConstructor(staticName = "of")
@NoArgsConstructor
public class TemperatureReport implements TcpPayload, TcpDeviceMessage {

    private long deviceId;

    private float temperature;

    private float humidity;

    @Override
    public DeviceMessage toDeviceMessage() {
        ReportPropertyMessage message = new ReportPropertyMessage();
       message.setProperties(Collections.singletonMap("temperature", temperature));
       /* Map<String, Object> map =new HashMap();
        map.put("temperature", temperature);
        map.put("humidity", humidity);*/
        message.getProperties().put("humidity", humidity);
        message.setDeviceId(String.valueOf(deviceId));
        message.setTimestamp(System.currentTimeMillis());
        return message;
    }

    @Override
    public byte[] toBytes() {
        //前8位为设备ID,后4位为温度值,低位字节在前.
        byte[] data = new byte[16];
        BytesUtils.numberToLe(data, deviceId, 0, 8);
        BytesUtils.numberToLe(data, Float.floatToIntBits(temperature), 8, 4);
        BytesUtils.numberToLe(data, Float.floatToIntBits(humidity), 12, 4);
        return data;
    }

    @Override
    public void fromBytes(byte[] bytes, int offset) {
        this.deviceId = BytesUtils.leToLong(bytes, offset, 8);
        this.temperature = BytesUtils.leToFloat(bytes, offset + 8, 4);
        this.humidity = BytesUtils.leToFloat(bytes, offset + 12, 4);
    }

    @Override
    public String toString() {
        return "TemperatureReport{" +
                "deviceId=" + deviceId +
                ", temperature=" + temperature +
                ", humidity=" + humidity +
                '}';
    }
}

然后在发送给服务器端时也进行补充了这两个属性

 vertx.createNetClient()
                .connect(8088, "127.0.0.1", result -> {
                    if (result.succeeded()) {
                        byte[] login=DemoTcpMessage.of(MessageType.AUTH_REQ, AuthRequest.of(1000, key)).toBytes();

                        NetSocket socket = result.result();
                        socket.handler(buffer -> {
                            // TODO: 2020/3/2 粘拆包处理
                            DemoTcpMessage tcpMessage = DemoTcpMessage.of(buffer.getBytes());
                            System.out.println(tcpMessage);
                            //认证通过后定时上报温度数据
                            if (tcpMessage.getType() == MessageType.AUTH_RES && ((AuthResponse) tcpMessage.getData()).getStatus() == TcpStatus.SUCCESS) {
                                Flux.interval(Duration.ofSeconds(1))
                                        .flatMap(t -> Flux.just(
                                                DemoTcpMessage.of(MessageType.REPORT_TEMPERATURE,
                                                      //  TemperatureReport.of(deviceId, (float) ThreadLocalRandom.current().nextDouble(20D, 50D)))
                                                        TemperatureReport.of(deviceId, (float)12.4,(float)30))
                                                        .toBytes()

                                             /*   DemoTcpMessage.of(MessageType.FIRE_ALARM,
                                                        FireAlarm.builder()
                                                                .point(ThreadLocalRandom.current().nextInt())
                                                                .lat(102.234F)
                                                                .lnt(122.122F)
                                                                .deviceId(deviceId)
                                                                .build()).toBytes()*/
                                        ))
                                        .map(Buffer::buffer)
                                        .window(1)//一次性发送2条数据
                                        .flatMap(list -> list.reduce(Buffer::appendBuffer))
                                        .doOnNext(buf -> socket.write(buf, res -> {
                                            log.debug("send : {}", Hex.encodeHexString(buf.getBytes()));
                                            if (!res.succeeded()) {
                                                res.cause().printStackTrace();
                                            }
                                        }))
                                        .subscribe();
                            }
                        }).write(Buffer.buffer(login), res -> {
                            log.debug("send auth req:{}",Hex.encodeHexString(login));
                            if (!res.succeeded()) {
                                res.cause().printStackTrace();
                            }
                        }).exceptionHandler(Throwable::printStackTrace);

                    } else {
                        result.cause().printStackTrace();
                        System.exit(0);
                    }
                });

但是我服务器只收到了温度属性,查看上报日志json是这样的

{
  "temperature": 12.4
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.