diff --git a/TinyNet/DevicesManagement.cpp b/TinyNet/DevicesManagement.cpp index 655c5c82..99ab7600 100644 --- a/TinyNet/DevicesManagement.cpp +++ b/TinyNet/DevicesManagement.cpp @@ -1,5 +1,5 @@ #include "DevicesManagement.h" - +#include "Message\BinaryPair.h" DevicesManagement* DevicesManagement::Current = nullptr; @@ -260,85 +260,154 @@ int DevicesManagement::WriteIDs(Stream &ms) return len; } -bool DevicesManagement::DeviceProcess(const Message& msg) +bool DevicesManagement::DeviceProcess(String &act, const Message& msg) { + TS("DevicesManagement::DeviceProcess"); // 仅处理来自云端的请求 if (msg.Reply) return false; - TS("DevicesManagement::DeviceProcess"); + DeviceAtions devact = DeviceAtions::Register; + if (act == "Device/List")devact = DeviceAtions::List; + if (act == "Device/Update")devact = DeviceAtions::Update; + if (act == "Device/Delete")devact = DeviceAtions::Delete; + if (act == "Device/ListIDs")devact = DeviceAtions::ListIDs; + // if (act == "Device/Register")devact = DeviceAtions::Register; + // if (act == "Device/Online")devact = DeviceAtions::Online; + // if (act == "Device/Offline")devact = DeviceAtions::Offline; + if (devact == DeviceAtions::Register)debug_printf("不支持的命令\r\n"); auto ms = msg.ToStream(); + BinaryPair bp(ms); + // 建立Reply + auto tmsg = (*(TokenMessage*)&msg).CreateReply(); - DeviceMessage dm; - if (!dm.GetBaseInfo(ms))return false; - auto dv = FindDev(dm.Id); - - TokenMessage rs; - rs.Code = 0x21; - - if (dv) - { - // 外部处理 - if (_DevProcess)_DevProcess(dm.Action, dv, _ClbkParam); - - } - switch (dm.Action) + switch (devact) { case DeviceAtions::List: { - SendDevices(dm.Action, nullptr); - return true; - } - case DeviceAtions::Update: - { - // 云端要更新网关设备信息 - if (!dv) + // 获取需要发送的IDs + ByteArray idss; + bp.Get("IDs", idss); + // 准备写入器 + MemoryStream ms2; + BinaryPair bp2(ms2); + // 写入命令类型 + bp2.Set("Action", act); + // 设备DevsInfo数据 + for (int i = 0; i < idss.Length(); i++) // 判定依据需要修改 { - rs.Error = true; - } - else - { - // 获取信息 - dm.GetMsgInfo(ms, dv); - SaveDev(); - // 拿到写后的信息 - DeviceMessage dmrs; - dmrs.pDev = dv; - dmrs.Action = dm.Action; - dmrs.WriteMessage(rs); + // 获取数据ms + MemoryStream dvms; + // 序列化一个DevInfo到ms + if (!GetDevInfo(idss[i], dvms))continue; + // 转换为ByteArray + ByteArray dvbs(dvms.GetBuffer(), dvms.Position()); + String countstr; + countstr += i; + // 写入DevInfo + bp2.Set(countstr, dvbs); } + // 将所有数据写入Data + tmsg.SetData(Buffer(ms2.GetBuffer(), ms2.Position())); // 发送 - if (Port)Port->Reply(rs); + //Port->Reply(tmsg); } break; + + case DeviceAtions::Delete: + { + // 拿到操作对象 + byte addr = 0; + bp.Get("ID", addr); + DeleteDev(addr); + // 拿到操作对象s + ByteArray idss; + bp.Get("IDs", idss); + for (int i = 0; i < idss.Length(); i++)DeleteDev(idss[i]); + // 准备回复数据 + if (addr == 0x00 && idss.Length() == 0) + { + debug_printf("DeleteDev Error\r\n"); + tmsg.Error = true; + } + } + break; + case DeviceAtions::ListIDs: + { + MemoryStream ms2; + BinaryPair bp2(ms2); + + bp2.Set("Action", act); + // 获取IDs ByteArray + MemoryStream idsms; + WriteIDs(idsms); + // for (int i = 0; i < DevArr.Length(); i++) + // { + // if (DevArr[i] == nullptr)continue; + // idsms.Write(DevArr[i]->Address); + // } + ByteArray idbs(idsms.GetBuffer(), idsms.Position()); + // 写入名值对 + bp2.Set("IDs", idbs); + + tmsg.SetData(Buffer(ms2.GetBuffer(), ms2.Position())); + } + break; + /*case DeviceAtions::Update: + break; case DeviceAtions::Register: break; case DeviceAtions::Online: break; case DeviceAtions::Offline: - break; - case DeviceAtions::Delete: - debug_printf("节~~1点删除 ID=0x%02X\r\n", id); - { - //auto dv2 = FindDev(id); - if (dv == nullptr) - { - rs.Error = true; - if (Port)Port->Reply(rs); - return false; - } - - // 云端要删除本地设备信息 - bool flag = DeleteDev(id); - rs.Error = !flag; - if (Port)Port->Reply(rs); - } - break; + break;*/ default: - debug_printf("无法识别的节点操作 Act=%d ID=0x%02X\r\n", (byte)act, id); + debug_printf("不支持的设备操作指令!!\r\n"); + tmsg.Error = true; break; } + return Port->Reply(tmsg); +} +// 获取设备信息到流 +bool DevicesManagement::GetDevInfo(byte id, MemoryStream &ms) +{ + if (id == 0x00)return false; + Device * dv = FindDev(id); + return GetDevInfo(dv, ms); +} + +// 获取设备信息到流 +bool DevicesManagement::GetDevInfo(Device *dv, MemoryStream &ms) +{ + if (dv != nullptr)return false; + + BinaryPair bp(ms); + + MemoryStream dvms; + BinaryPair dvbp(dvms); + bp.Set("ID", dv->Address); + + byte login = dv->Logined ? 1 : 0; + bp.Set("Online", login); + bp.Set("Kind", dv->Kind); + bp.Set("LastActive", dv->LastTime); + bp.Set("RegisterTime", dv->RegTime); + bp.Set("LoginTime", dv->LoginTime); + // bp.Set("HardID", dv->Logins); + + bp.Set("Version", dv->Version); + bp.Set("DataSize", dv->DataSize); + bp.Set("ConfigSize", dv->ConfigSize); + + bp.Set("SleepTime", dv->SleepTime); + bp.Set("Offline", dv->OfflineTime); + bp.Set("PingTime", dv->PingTime); + + bp.Set("HardID", dv->HardID); + bp.Set("Name", dv->Name); + + bp.Set("Password", dv->Pass); return true; } @@ -411,17 +480,19 @@ bool DevicesManagement::SendDevices(DeviceAtions act, const Device* dv) void DevicesManagement::SendDevicesIDs() { - TokenMessage msg; - msg.Code = 0x21; - auto act = DeviceAtions::ListIDs; + TokenMessage msg(0x08); + + // 获取IDList + MemoryStream idms; + WriteIDs(idms); + ByteArray idbs(idms.GetBuffer(), idms.Position()); MemoryStream ms; - ms.Write((byte)act); - WriteIDs(ms); - - msg.Length = ms.Position(); - msg.Data = ms.GetBuffer(); + BinaryPair bp(ms); + bp.Set("Action", String("Device/ListIDs")); + bp.Set("IDs", idbs); + msg.SetData(Buffer(ms.GetBuffer(), ms.Position())); if (Port)Port->Send(msg); } diff --git a/TinyNet/DevicesManagement.h b/TinyNet/DevicesManagement.h index 320446d5..6eb3baf2 100644 --- a/TinyNet/DevicesManagement.h +++ b/TinyNet/DevicesManagement.h @@ -45,7 +45,9 @@ public: // 发送时刻再绑定?! 如果绑定失败报错? TokenClient * Port = nullptr; - bool DeviceProcess(const Message& msg); + bool DeviceProcess(String &act, const Message& msg); + bool GetDevInfo(byte id, MemoryStream &ms); + bool GetDevInfo(Device *dv, MemoryStream &ms); bool SendDevices(DeviceAtions act, const Device* dv = nullptr); void SendDevicesIDs(); // 设备状态变更上报 diff --git a/TinyNet/TinyServer.h b/TinyNet/TinyServer.h index c6883fd7..77883176 100644 --- a/TinyNet/TinyServer.h +++ b/TinyNet/TinyServer.h @@ -6,7 +6,7 @@ #include "TinyMessage.h" #include "TinyConfig.h" -#include "TinyNet\Device.h" +#include "TokenNet\Device.h" #include "TinyNet\DevicesManagement.h" /******************************** TinyServer ********************************/ diff --git a/TokenNet/DeviceBody.h b/TokenNet/DeviceBody.h index 86b91872..0ee9d7d6 100644 --- a/TokenNet/DeviceBody.h +++ b/TokenNet/DeviceBody.h @@ -6,7 +6,7 @@ //#include "TokenNet\TokenClient.h" -#include "TinyNet\Device.h" +#include "TokenNet\Device.h" #include "TinyNet\DevicesManagement.h" // tokendistributive diff --git a/TokenNet/Gateway.cpp b/TokenNet/Gateway.cpp index 9c730509..cff20bbe 100644 --- a/TokenNet/Gateway.cpp +++ b/TokenNet/Gateway.cpp @@ -3,6 +3,7 @@ #include "Security\MD5.h" #include "Security\Crc.h" +#include "..\Message\BinaryPair.h" // 循环间隔 #define LOOP_Interval 10000 @@ -137,7 +138,37 @@ bool Gateway::OnRemote(const TokenMessage& msg) { TS("Gateway::OnRemote"); - switch (msg.Code) + if (msg.Code == 0x20) + { + OnMode(msg); + } + + if (msg.Code == 0x08 || msg.Code == 0x02) + { + auto ms = msg.ToStream(); + BinaryPair bp(ms); + if (bp.Get("Device/List") || (msg.Code == 0x02 && msg.Reply&&Client->Token != 0)) + { + // 登录以后自动发送设备列表和设备信息 + // 遍历发送所有设备信息 + pDevMgmt->SendDevices(DeviceAtions::List, nullptr); + return true; + } + + String act; + bp.Get("Action", act); + if (act.StartsWith("Device/")) + { + // 由他内部回复数据 + pDevMgmt->DeviceProcess(act, msg); + return true; + } + //if (act.StartsWith("USART/"))xxx(); + //if (act.StartsWith("IO/"))xxx2(); + } + + + /*switch (msg.Code) { case 0x02: // 登录以后自动发送设备列表和设备信息 @@ -154,7 +185,7 @@ bool Gateway::OnRemote(const TokenMessage& msg) case 0x21: return pDevMgmt->DeviceProcess(msg); - } + }*/ // 应用级消息转发 if (msg.Code >= 0x10 && !msg.Error && msg.Length <= Server->Control->Port->MaxSize - TinyMessage::MinSize) diff --git a/TokenNet/HelloMessage.cpp b/TokenNet/HelloMessage.cpp index 8d096277..5f613774 100644 --- a/TokenNet/HelloMessage.cpp +++ b/TokenNet/HelloMessage.cpp @@ -18,7 +18,7 @@ HelloMessage::HelloMessage() : Cipher(1), Key(0) LocalTime = Time.Now().TotalMicroseconds(); Cipher[0] = 1; - Protocol = 2; + Protocol = 17; Port = 0; } @@ -47,9 +47,30 @@ bool HelloMessage::Read(Stream& ms) bp.Get("ErrorCode", ErrCode); if(ErrCode == 0xFE || ErrCode == 0xFD) { - bp.Get("Protocol", Protocol); - bp.Get("Server", Server); - bp.Get("Port", Port); + ByteArray uri; + if (bp.Get("Redirect", uri)) + { + MemoryStream urims(uri.GetBuffer(), uri.Length()); + BinaryPair uribp(urims); + + uint prcl = 0x00; // 服务店 ProtocolType 17 为UDP + uribp.Get("ProtocolType", prcl); + prcl >>= 24; // 大小端问题 + Protocol = prcl; + uribp.Get("Host", Server); + uint uintPort; // 服务端 Port 为 int 类型 + uribp.Get("Port", uintPort); + Port = uintPort >> 16; + Port = _REV16(Port); // 大小端问题 + + /*uint prcl = 0x00; // 服务店 ProtocolType 17 为UDP + bp.Get("ProtocolType", prcl); + Protocol = prcl == 0x11 ? 0x02 : 0x01; // Protocol; // 协议,TCP=1/UDP=2 + bp.Get("Host", Server); + uint uintPort; // 服务端 Port 为 int 类型 + bp.Get("Port", uintPort); + Port = (*(int*)&uintPort);*/ + } bp.Get("VisitToken", VisitToken); return false; diff --git a/TokenNet/HelloMessage.h b/TokenNet/HelloMessage.h index caa035e6..14eccad9 100644 --- a/TokenNet/HelloMessage.h +++ b/TokenNet/HelloMessage.h @@ -23,7 +23,7 @@ public: byte ErrCode; // 错误码 String ErrMsg; // 错误信息 - byte Protocol; // 协议,TCP=1/UDP=2 + byte Protocol; // 协议,17为UDP 6为TCP String Server; // 服务器地址。可能是域名或IP ushort Port; // 本地端口 String VisitToken; //访问令牌 diff --git a/TokenNet/TokenClient.cpp b/TokenNet/TokenClient.cpp index 889d632e..a0b1fdaf 100644 --- a/TokenNet/TokenClient.cpp +++ b/TokenNet/TokenClient.cpp @@ -363,6 +363,9 @@ void TokenClient::OnRegister(TokenMessage& msg ,Controller* ctrl) Status = 0; + auto ctrl2 = dynamic_cast(ctrl); + if (ctrl2) ctrl2->Key.SetLength(0); + Sys.SetTask(_task, true, 0); } diff --git a/TokenNet/TokenController.cpp b/TokenNet/TokenController.cpp index 14d47286..d453a487 100644 --- a/TokenNet/TokenController.cpp +++ b/TokenNet/TokenController.cpp @@ -223,8 +223,15 @@ bool TokenController::OnReceive(Message& msg) // 加解密。握手不加密,登录响应不加密 //Encrypt(msg, Key); - Buffer bs(msg.Data, msg.Length + 2); - if (!Decrypt(bs, Key)) return false; + if (msg.Code != 0x01) + { + Buffer bs(msg.Data, msg.Length + 2); + if (!Decrypt(bs, Key)) + { + debug_printf("TokenController::OnReceive 解密失败\r\n"); + return false; + } + } ShowMessage("Recv", msg); diff --git a/TokenNet/TokenDataMessage.cpp b/TokenNet/TokenDataMessage.cpp index bca973b3..06860a3d 100644 --- a/TokenNet/TokenDataMessage.cpp +++ b/TokenNet/TokenDataMessage.cpp @@ -25,6 +25,7 @@ bool TokenDataMessage::Read(Stream& ms) bp.Get("Start", Start); bp.Get("Size", Size); bp.Get("MemoryData", Data); + return true; } // 把消息写入数据流中