增加物联配置,成功向服务端提交芯片唯一标识、版本、类型、平台

This commit is contained in:
大石头 2017-08-27 00:39:20 +08:00
parent 22a1931e35
commit 97a2303c2f
4 changed files with 126 additions and 59 deletions

View File

@ -13,14 +13,14 @@
LinkClient* LinkClient::Current = nullptr;
//static void BroadcastHelloTask(void* param);
LinkClient::LinkClient()
: Routes(String::Compare)
{
Opened = false;
Status = 0;
Cfg = LinkConfig::Current;
LoginTime = 0;
LastSend = 0;
LastActive = 0;
@ -36,6 +36,7 @@ void LinkClient::Open()
if (Opened) return;
TS("LinkClient::Open");
assert(Cfg, "未指定物联配置Cfg");
// 令牌客户端定时任务
_task = Sys.AddTask(&LinkClient::LoopTask, this, 0, 1000, "物联客户");
@ -62,7 +63,7 @@ void LinkClient::CheckNet()
// 检测主链接
if (!mst)
{
auto uri = Server;
auto uri = Cfg->Uri();
// 创建连接服务器的Socket
auto socket = Socket::CreateRemote(uri);
if (!socket) return;
@ -115,7 +116,6 @@ void LinkClient::LoopTask()
// 登录成功后,心跳一次,把数据同步上去
Sys.Sleep(1000);
if (Status >= 2) Ping();
break;
case 2:
@ -156,36 +156,6 @@ void LinkClient::OnReceive(LinkMessage& msg)
auto js = msg.Create();
/*// 带有code是响应
auto jcode = js["code"];
if (!jcode.IsNull()) {
// 错误处理
int code = js["code"].AsInt();
if (code)
{
#if DEBUG
debug_printf("<= code=%d error=");
js["data"].AsString().Show(true);
#endif // DEBUG
return;
}
// 正常响应
auto act = js["action"].AsString();
auto args = js["result"];
if (act == "login")
OnLogin(args);
else if (act == "ping")
OnLogin(args);
}
// 没有code是请求
else {
auto action = js["action"];
auto args = js["args"];
}*/
auto act = js["action"].AsString();
if (act == "login")
@ -198,22 +168,13 @@ void LinkClient::OnReceive(LinkMessage& msg)
}
bool LinkClient::Send(const LinkMessage& msg) {
return Master->Send(msg.GetBuffer());
}
bool LinkClient::Invoke(const String& action, const Json& args) {
// 消息缓冲区,跳过头部
char cs[512];
/*String str(&cs[sizeof(LinkMessage)], sizeof(cs) - sizeof(LinkMessage), false);
str.SetLength(0);*/
// 构造内容
/*str += "{\"action\":\"";
str += action;
str += "\",\"args\":";
str += args;
str += "}";*/
// 格式化消息
auto& msg = *(LinkMessage*)cs;
@ -250,19 +211,31 @@ void LinkClient::Login()
Json json;
json.Add("user", User);
// 已有用户名则直接登录,否则用机器码注册
auto user = Cfg->User();
if (user) {
json.Add("user", user);
// 原始密码对盐值进行加密,得到登录密码
auto now = DateTime::Now().TotalMs();
auto arr = Buffer(&now, 8);
ByteArray bs;
bs = arr;
RC4::Encrypt(arr, Pass);
// 散列明文和密码连接在一起
auto pass = bs.ToHex();
pass += arr.ToHex();
// 原始密码对盐值进行加密,得到登录密码
auto now = DateTime::Now().TotalMs();
auto arr = Buffer(&now, 8);
ByteArray bs;
bs = arr;
RC4::Encrypt(arr, Cfg->Pass());
// 散列明文和密码连接在一起
auto pass = bs.ToHex();
pass += arr.ToHex();
json.Add("pass", pass);
json.Add("pass", pass);
}
else {
json.Add("user", Buffer(Sys.ID, 16).ToHex());
}
ushort code = _REV16(Sys.Code);
json.Add("type", Buffer(&code, 2).ToHex());
json.Add("agent", Sys.Name);
json.Add("version", Version(Sys.Ver).ToString());
Invoke("Device/Login", json);
}

View File

@ -7,6 +7,7 @@
#include "Message\Json.h"
#include "LinkMessage.h"
#include "LinkConfig.h"
// 物联客户端
class LinkClient
@ -21,11 +22,9 @@ public:
int Delay; // 心跳延迟。一条心跳指令从发出到收到所花费的时间
int MaxNotActive; // 最大不活跃时间ms超过该时间时重启系统。默认0
NetUri Server;
String User;
String Pass;
LinkConfig* Cfg;
Socket* Master; // 主链接。服务器长连接
Socket* Master; // 主链接。服务器长连接
DataStore Store; // 数据存储区
Dictionary<cstring, IDelegate*> Routes; // 路由集合

63
Link/LinkConfig.cpp Normal file
View File

@ -0,0 +1,63 @@
#include "Net\IPEndPoint.h"
#include "LinkConfig.h"
LinkConfig* LinkConfig::Current = nullptr;
LinkConfig::LinkConfig() : ConfigBase()
{
_Name = "LinkCfg";
_Start = &Length;
_End = &TagEnd;
Init();
}
void LinkConfig::Init()
{
ConfigBase::Init();
Length = Size();
}
void LinkConfig::Show() const
{
#if DEBUG
debug_printf("LinkConfig::物联配置:\r\n");
debug_printf("\t服务: %s \r\n", _Server);
debug_printf("\t登录: %s \r\n", _User);
debug_printf("\t密码: %s \r\n", _Pass);
#endif
}
LinkConfig* LinkConfig::Create(cstring server)
{
// Flash最后一块作为配置区
if (Config::Current == nullptr) Config::Current = &Config::CreateFlash();
static LinkConfig tc;
if (!Current)
{
LinkConfig::Current = &tc;
tc.Init();
tc.Load();
auto uri = tc.Uri();
// 默认 UDP 不允许 unknown
if (uri.Type == NetType::Unknown) uri.Type = NetType::Udp;
// 如果服务器配置为空,则使用外部地址
auto svr = tc.Server();
if (!svr)
{
svr = server;
tc.Save();
}
tc.Show();
}
return &tc;
}

32
Link/LinkConfig.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef __LinkConfig_H__
#define __LinkConfig_H__
#include "..\Config.h"
#include "Net\NetUri.h"
// 配置信息
class LinkConfig : public ConfigBase
{
public:
byte Length; // 数据长度
char _User[16]; // 登录名
char _Pass[16]; // 登录密码
char _Server[32]; // 服务器
byte TagEnd; // 数据区结束标识符
LinkConfig();
virtual void Init();
virtual void Show() const;
String User() { return String(_User, sizeof(_User), false); }
String Pass() { return String(_Pass, sizeof(_Pass), false); }
String Server() { return String(_Server, sizeof(_Server), false); }
NetUri Uri() { return NetUri(Server()); }
static LinkConfig* Current;
static LinkConfig* Create(cstring server);
};
#endif