1、Iot 物联网协议
internet of things 物联网协议java 服务如何制作? java netty 服务器gitee连接 下面的代码实际上是对协议的定义,如何变成动态的协议?我的方案就是使用js代码来定义协议,java读取js配置文件来做,可以调到2 。注意 1和2 是两个java项目工程,是两个地址
public class Message {
private final Charset charset = Charset.forName("utf-8");
private byte magicType;
private long deviceid; //设备
private byte cmd;//命令
private byte length;
private String body;
public Message(){
}
public Message(byte magicType,long deviceid, byte cmd, byte[] data) {
this.magicType = magicType; //0x69
this.deviceid = deviceid;
this.cmd =cmd;
this.length = (byte)data.length;
this.body = new String(data, charset);
}
public Message(byte magicType, long deviceid, byte cmd, String body) {
this.magicType = magicType;
this.cmd = cmd;
this.deviceid = deviceid;
this.length = (byte)body.getBytes(charset).length;
this.body = body;
}
public String GetBody()
{
return this.body;
}
public byte GetCmd()
{
return this.cmd;
}
}
代码里面可以发布到redis里面 可以在nodejs里面订阅等等,java来发布 以下使用nodejs订阅消息
var redis = require("redis");
var client = redis.createClient(6379, "192.168.1.222");
var client1 = redis.createClient(6379, "192.168.1.222");
client1.on('connect', function () {
client1.subscribe("draw");
});
client1.on("message", function (channel, message) {
console.log(typeof (message));
var obj = JSON.parse(message);
console.log("普通订阅接收到来自" + channel + "的信息:" + obj.session);
if (message == "quit") {
client1.unsubscribe("draw");
console.log("普通订阅操作已经取消");
//client2.quite();
}
});
/*
var client2 = redis.createClient("6379", "192.168.1.222");
client2.on('connect', function () {
client2.psubscribe("draw*");
});
client2.on("pmessage", function (p, channel, message) {
console.log("批量订阅接收到来自" + channel + "的信息:" + message);
if (message == "quit") {
client2.punsubscribe("draw*");
console.log("批量订阅操作已经取消");
//client2.quite();
}
});
*/
/*
function getRedisData() {
//客户端连接redis成功后执行回调
client.on("ready", function () {
//订阅消息
client.subscribe("chat");
client.subscribe("chat1");
console.log("订阅成功。。。");
});
client.on("error", function (error) {
console.log("Redis Error " + error);
});
//监听订阅成功事件
client.on("subscribe", function (channel, count) {
console.log("client subscribed to " + channel + "," + count + "total subscriptions");
});
//收到消息后执行回调,message是redis发布的消息
client.on("message", function (channel, message) {
console.log("我接收到信息了" + message);
//dealWithMsg(message);
});
//监听取消订阅事件
client.on("unsubscribe", function (channel, count) {
console.log("client unsubscribed from" + channel + ", " + count + " total subscriptions")
});
}
function dealWithMsg(message) {
//按照message查询内容
client1.zscore("key", message, function (err, reply) {
console.log(message + "的内容是:" + reply);
});
}
getRedisData();
*/
2、使用java读取一种协议,协议记录在js文件中
以下是一个javascript的协议函数
function getprotocol() {
var protocol_define = {
protocol: {
contentlen: { offset: 1, len: 2, includeht: 1 }, //includeht 为0 不包含头部尾部 1 包含头部和其他
deviceid: { offset: 3, len: 4 },
cmd: { offset: 7, len: 2 },
headfields: {
len: 8, field: [
{ name: "time", offset: 9, ftype: "byte", len: 6 },
{ name: "version", offset: 15, ftype: "byte", len: 1 },
{ name: "aftercmd", offset: 16, ftype: "byte", len: 1 } //后继指令
]
},
bodyfields: [
{
//type: string float double uint32 int32 uint16 int16 int8 uint8
//ftype bige 大端格式数据 lite小端数据格式
des: "上行自动采集命令",
cmd: 0x0202, //
dir: "up", //上行命令
field: [
{ name: "temp", type: "float", ftype: "bige", len: 4 },
{ name: "high", type: "int16", ftype: "bige", len: 2 },
{ name: "a", ftype: "float", ftype: "bige", len: 4 }]
},
{
des: "心跳",
cmd: 0x0201, //heart
field: [
{ name: "temp2", type: "float", ftype: "bige", len: 2 },
{ name: "tt", type: "string", ftype: "", len: 4 }
]
},
{
des: "登陆包",
cmd: 0x0101,
field: [
{ name: "test", type: "integer", ftype: "bige", len: 4 }
]
}
],
crc: { len: 2 }
},
url: "http://192.168.1.237:10011/postdata",//post 数据的远程地址
push: ["192.168.1.237"], //数据推流到另外的服务器或者服务器组
port: "8051" //tcp端口启动为8051
}
return JSON.stringify(protocol_define);
};
如何使用Java读取?
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import java.io.IOException;
import javax.script.ScriptException;
import com.zhongda.IoT.common.scriptjs;
当然还是使用sprintboot等框架做一些工作,java比较简单,不像c++,当你编译一个google的v8 解析引擎,就发现需要各种的版本,确实不方便,当然也可以选择自己写,不过造轮子确实费时间
gitee 链接 读取javascript配置文件