1,节点写入响应携带数据
2,主机收到节点写入响应获取携带数据,更新缓存 3,主机收到远程写入请求不操作本地缓存,直接转发给节点,由节点响应来作为应答远程请求的响应 4,主机收到远程读取请求,每5秒转发一次给节点,此时不会从缓存拿数据做响应,其它时候直接从缓存拿数据做响应,不转发指令 5,节点主动上报由读取响应改为写入请求,为了获得微网重发机制支持,确保上报成功
This commit is contained in:
parent
961f987b86
commit
7de8ea99b5
|
@ -43,20 +43,24 @@ bool DataMessage::ReadData(const Array& bs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 写入数据
|
// 写入数据
|
||||||
bool DataMessage::WriteData(DataStore& ds)
|
bool DataMessage::WriteData(DataStore& ds, bool withData)
|
||||||
{
|
{
|
||||||
TS("DataMessage::WriteData");
|
TS("DataMessage::WriteData");
|
||||||
|
|
||||||
Length = _Src.Remain();
|
Length = _Src.Remain();
|
||||||
if(!Write(ds.Data.Length() - Offset)) return false;
|
if(!Write(ds.Data.Length() - Offset)) return false;
|
||||||
|
|
||||||
ds.Write(Offset, Array(_Src.Current(), Length));
|
Array dat(_Src.Current(), Length);
|
||||||
|
ds.Write(Offset, dat);
|
||||||
|
|
||||||
|
// 如果携带数据,则把这一段数据附加到后面
|
||||||
|
if(withData) _Dest.Write(dat);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 写入数据
|
// 写入数据
|
||||||
bool DataMessage::WriteData(Array& bs)
|
bool DataMessage::WriteData(Array& bs, bool withData)
|
||||||
{
|
{
|
||||||
TS("DataMessage::WriteData");
|
TS("DataMessage::WriteData");
|
||||||
|
|
||||||
|
@ -64,7 +68,11 @@ bool DataMessage::WriteData(Array& bs)
|
||||||
Length = _Src.Remain();
|
Length = _Src.Remain();
|
||||||
if(!Write(bs.Length() - Offset)) return false;
|
if(!Write(bs.Length() - Offset)) return false;
|
||||||
|
|
||||||
bs.Copy(_Src.Current(), Length, Offset);
|
Array dat(_Src.Current(), Length);
|
||||||
|
bs.Copy(dat, Offset);
|
||||||
|
|
||||||
|
// 如果携带数据,则把这一段数据附加到后面
|
||||||
|
if(withData) _Dest.Write(dat);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,10 +15,10 @@ public:
|
||||||
DataMessage(const Message& msg, Stream& dest);
|
DataMessage(const Message& msg, Stream& dest);
|
||||||
|
|
||||||
bool ReadData(const DataStore& ds);
|
bool ReadData(const DataStore& ds);
|
||||||
bool WriteData(DataStore& ds);
|
bool WriteData(DataStore& ds, bool withData);
|
||||||
|
|
||||||
bool ReadData(const Array& bs);
|
bool ReadData(const Array& bs);
|
||||||
bool WriteData(Array& bs);
|
bool WriteData(Array& bs, bool withData);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Stream _Src;
|
Stream _Src;
|
||||||
|
|
|
@ -193,15 +193,12 @@ void TinyClient::OnWrite(const TinyMessage& msg)
|
||||||
|
|
||||||
bool rt = true;
|
bool rt = true;
|
||||||
if(dm.Offset < 64)
|
if(dm.Offset < 64)
|
||||||
rt = dm.WriteData(Store);
|
rt = dm.WriteData(Store, true);
|
||||||
else if(dm.Offset < 128)
|
else if(dm.Offset < 128)
|
||||||
{
|
{
|
||||||
dm.Offset -= 64;
|
dm.Offset -= 64;
|
||||||
//DataStore ds;
|
|
||||||
//ds.Data.Set(Cfg, Cfg->Length);
|
|
||||||
//rt = dm.WriteData(ds);
|
|
||||||
Array bs(Cfg, Cfg->Length);
|
Array bs(Cfg, Cfg->Length);
|
||||||
rt = dm.WriteData(bs);
|
rt = dm.WriteData(bs, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.Error = !rt;
|
rs.Error = !rt;
|
||||||
|
@ -250,14 +247,14 @@ bool TinyClient::Report(uint offset, byte dat)
|
||||||
bool TinyClient::Report(uint offset, const Array& bs)
|
bool TinyClient::Report(uint offset, const Array& bs)
|
||||||
{
|
{
|
||||||
TinyMessage msg;
|
TinyMessage msg;
|
||||||
msg.Code = 0x05;
|
msg.Code = 0x06;
|
||||||
|
|
||||||
auto ms = msg.ToStream();
|
auto ms = msg.ToStream();
|
||||||
ms.WriteEncodeInt(offset);
|
ms.WriteEncodeInt(offset);
|
||||||
ms.Write(bs);
|
ms.Write(bs);
|
||||||
msg.Length = ms.Position();
|
msg.Length = ms.Position();
|
||||||
|
|
||||||
return Reply(msg);
|
return Send(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TinyClient::ReportAsync(uint offset)
|
void TinyClient::ReportAsync(uint offset)
|
||||||
|
|
|
@ -124,8 +124,16 @@ bool TinyServer::OnReceive(TinyMessage& msg)
|
||||||
OnReadReply(msg, *dv);
|
OnReadReply(msg, *dv);
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
|
// 系统指令不会被转发,这里修改为用户指令
|
||||||
|
msg.Code = 0x16;
|
||||||
case 0x16:
|
case 0x16:
|
||||||
//OnWriteReply(msg, *dv);
|
if(!msg.Reply)
|
||||||
|
{
|
||||||
|
auto rs = msg.CreateReply();
|
||||||
|
if(OnWrite(msg, rs, *dv)) Send(rs);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
OnWriteReply(msg, *dv);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,7 +184,10 @@ bool TinyServer::Dispatch(TinyMessage& msg)
|
||||||
|
|
||||||
// 避免频繁读取。间隔秒数
|
// 避免频繁读取。间隔秒数
|
||||||
if(dv->LastRead + 5 < now)
|
if(dv->LastRead + 5 < now)
|
||||||
|
{
|
||||||
dv->LastRead = now;
|
dv->LastRead = now;
|
||||||
|
rt = false;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
fw = false;
|
fw = false;
|
||||||
|
|
||||||
|
@ -185,14 +196,15 @@ bool TinyServer::Dispatch(TinyMessage& msg)
|
||||||
case 6:
|
case 6:
|
||||||
case 0x16:
|
case 0x16:
|
||||||
{
|
{
|
||||||
auto now = Sys.Ms();
|
/*auto now = Sys.Ms();
|
||||||
rt = OnWrite(msg, rs, *dv);
|
rt = OnWrite(msg, rs, *dv);
|
||||||
|
|
||||||
// 避免频繁写入。间隔毫秒数
|
// 避免频繁写入。间隔毫秒数
|
||||||
if(dv->LastWrite + 500 < now)
|
if(dv->LastWrite + 500 < now)
|
||||||
dv->LastWrite = now;
|
dv->LastWrite = now;
|
||||||
else
|
else
|
||||||
fw = false;
|
fw = false;*/
|
||||||
|
rt = false;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -477,7 +489,7 @@ bool TinyServer::OnPing(const TinyMessage& msg)
|
||||||
响应:1起始 + N数据
|
响应:1起始 + N数据
|
||||||
错误:错误码2 + 1起始 + 1大小
|
错误:错误码2 + 1起始 + 1大小
|
||||||
*/
|
*/
|
||||||
bool TinyServer::OnRead(const Message& msg, Message& rs, Device& dv)
|
bool TinyServer::OnRead(const Message& msg, Message& rs, const Device& dv)
|
||||||
{
|
{
|
||||||
if(msg.Reply) return false;
|
if(msg.Reply) return false;
|
||||||
if(msg.Length < 2) return false;
|
if(msg.Length < 2) return false;
|
||||||
|
@ -492,12 +504,11 @@ bool TinyServer::OnRead(const Message& msg, Message& rs, Device& dv)
|
||||||
|
|
||||||
bool rt = true;
|
bool rt = true;
|
||||||
if(dm.Offset < 64)
|
if(dm.Offset < 64)
|
||||||
rt = dm.ReadData(Array(dv.Store, ArrayLength(dv.Store)));
|
rt = dm.ReadData(dv.GetStore());
|
||||||
else if(dm.Offset < 128)
|
else if(dm.Offset < 128)
|
||||||
{
|
{
|
||||||
dm.Offset -= 64;
|
dm.Offset -= 64;
|
||||||
Array bs(dv.Cfg, dv.Cfg->Length);
|
rt = dm.ReadData(dv.GetConfig());
|
||||||
rt = dm.ReadData(bs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.Error = !rt;
|
rs.Error = !rt;
|
||||||
|
@ -507,32 +518,6 @@ bool TinyServer::OnRead(const Message& msg, Message& rs, Device& dv)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取响应,服务端趁机缓存一份。
|
|
||||||
bool TinyServer::OnReadReply(const TinyMessage& msg, Device& dv)
|
|
||||||
{
|
|
||||||
if(!msg.Reply || msg.Error) return false;
|
|
||||||
if(msg.Length < 2) return false;
|
|
||||||
|
|
||||||
TS("TinyServer::OnReadReply");
|
|
||||||
|
|
||||||
auto ms = msg.ToStream();
|
|
||||||
uint offset = ms.ReadEncodeInt();
|
|
||||||
|
|
||||||
auto bs = dv.GetStore();
|
|
||||||
int remain = bs.Capacity() - offset;
|
|
||||||
if(remain < 0) return false;
|
|
||||||
|
|
||||||
uint len = ms.Remain();
|
|
||||||
if(len > remain) len = remain;
|
|
||||||
// 保存一份到缓冲区
|
|
||||||
if(len > 0)
|
|
||||||
{
|
|
||||||
bs.Copy(ms.Current(), len, offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
请求:1起始 + N数据
|
请求:1起始 + N数据
|
||||||
响应:1起始 + 1大小
|
响应:1起始 + 1大小
|
||||||
|
@ -552,14 +537,14 @@ bool TinyServer::OnWrite(const Message& msg, Message& rs, Device& dv)
|
||||||
bool rt = true;
|
bool rt = true;
|
||||||
if(dm.Offset < 64)
|
if(dm.Offset < 64)
|
||||||
{
|
{
|
||||||
Array bs(dv.Store, ArrayLength(dv.Store));
|
auto bs = dv.GetStore();
|
||||||
rt = dm.WriteData(bs);
|
rt = dm.WriteData(bs, false);
|
||||||
}
|
}
|
||||||
else if(dm.Offset < 128)
|
else if(dm.Offset < 128)
|
||||||
{
|
{
|
||||||
dm.Offset -= 64;
|
dm.Offset -= 64;
|
||||||
Array bs(dv.Cfg, dv.Cfg->Length);
|
auto bs = dv.GetConfig();
|
||||||
rt = dm.WriteData(bs);
|
rt = dm.WriteData(bs, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.Error = !rt;
|
rs.Error = !rt;
|
||||||
|
@ -568,6 +553,49 @@ bool TinyServer::OnWrite(const Message& msg, Message& rs, Device& dv)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 读取响应,服务端趁机缓存一份。
|
||||||
|
bool TinyServer::OnReadReply(const Message& msg, Device& dv)
|
||||||
|
{
|
||||||
|
if(!msg.Reply || msg.Error) return false;
|
||||||
|
if(msg.Length < 2) return false;
|
||||||
|
|
||||||
|
TS("TinyServer::OnReadReply");
|
||||||
|
|
||||||
|
return OnWriteReply(msg, dv);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 节点的写入响应,偏移和长度之后可能携带有数据
|
||||||
|
bool TinyServer::OnWriteReply(const Message& msg, Device& dv)
|
||||||
|
{
|
||||||
|
if(!msg.Reply || msg.Error) return false;
|
||||||
|
if(msg.Length <= 2) return false;
|
||||||
|
|
||||||
|
TS("TinyServer::OnWriteReply");
|
||||||
|
|
||||||
|
auto rs = ((TinyMessage&)msg).CreateReply();
|
||||||
|
auto ms = rs.ToStream();
|
||||||
|
|
||||||
|
DataMessage dm(msg, ms);
|
||||||
|
|
||||||
|
bool rt = true;
|
||||||
|
if(dm.Offset < 64)
|
||||||
|
{
|
||||||
|
auto bs = dv.GetStore();
|
||||||
|
rt = dm.WriteData(bs, false);
|
||||||
|
}
|
||||||
|
else if(dm.Offset < 128)
|
||||||
|
{
|
||||||
|
dm.Offset -= 64;
|
||||||
|
auto bs = dv.GetConfig();
|
||||||
|
rt = dm.WriteData(bs, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
rs.Error = !rt;
|
||||||
|
rs.Length = ms.Position();
|
||||||
|
|
||||||
|
return Send(rs);
|
||||||
|
}
|
||||||
|
|
||||||
//设置zigbee的通道,2401无效
|
//设置zigbee的通道,2401无效
|
||||||
void TinyServer::SetChannel(byte channel)
|
void TinyServer::SetChannel(byte channel)
|
||||||
{
|
{
|
||||||
|
|
|
@ -69,11 +69,12 @@ public:
|
||||||
bool OnPing(const TinyMessage& msg);
|
bool OnPing(const TinyMessage& msg);
|
||||||
|
|
||||||
// 读取
|
// 读取
|
||||||
bool OnRead(const Message& msg, Message& rs, Device& dv);
|
bool OnRead(const Message& msg, Message& rs, const Device& dv);
|
||||||
bool OnReadReply(const TinyMessage& msg, Device& dv);
|
bool OnReadReply(const Message& msg, Device& dv);
|
||||||
|
|
||||||
// 写入
|
// 写入
|
||||||
bool OnWrite(const Message& msg, Message& rs, Device& dv);
|
bool OnWrite(const Message& msg, Message& rs, Device& dv);
|
||||||
|
bool OnWriteReply(const Message& msg, Device& dv);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue