160 lines
5.4 KiB
C#
160 lines
5.4 KiB
C#
using Common.Models.PubClass;
|
||
using Common.Models.UnqTables;
|
||
using Server.MyClass.Class;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using System.Web.Http;
|
||
|
||
namespace Server.Controllers.OpenManagement
|
||
{
|
||
public class DeviceController : DefaultController
|
||
{
|
||
/// <summary>
|
||
/// 连接客户端
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPost, ErrorFilter]
|
||
public WebResult Connection()
|
||
{
|
||
var Id = GetString("Id", true);
|
||
var InternetIP = GetString("InternetIP", true);
|
||
var ClientVersion = GetString("ClientVersion", true);
|
||
Version v = null;
|
||
if (Id.Length != 32) return PutData("Id 建议生成32位的唯一数,保证设备的准确性!");
|
||
if (!Regex.Match(InternetIP, @"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})").Success) return PutData("IP信息不准确,请务必查询真实外网IP!");
|
||
if (!Version.TryParse(ClientVersion, out v)) return PutData("您的版本信息有误,请输入一个正确的版本信息!");
|
||
|
||
Device Device = Db.Queryable<Device>().Single(f => f.Id == Id);
|
||
if (Device == null)
|
||
{
|
||
Device = new Device()
|
||
{
|
||
Id = Id
|
||
};
|
||
}
|
||
Device.Version = ClientVersion;
|
||
Device.InternetIP = InternetIP;
|
||
Device.Token = System.Guid.NewGuid().ToString();
|
||
Device.LoginTime = DateTime.Now;
|
||
Db.Storageable(Device).ExecuteCommand();
|
||
|
||
if (Client.OnlineDevices.ContainsKey(Id))
|
||
{
|
||
Client.OnlineDevices[Id] = new MyClass.Class.DeviceSession();
|
||
}
|
||
else
|
||
{
|
||
if (!Client.OnlineDevices.TryAdd(Id, new MyClass.Class.DeviceSession())) return PutError;
|
||
}
|
||
|
||
return PutData(new
|
||
{
|
||
Token = Device.Token,
|
||
MysqlName = Client.Config.MysqlName,
|
||
MysqlHost = Client.Config.MysqlHost,
|
||
MysqlPort = Client.Config.MysqlPort,
|
||
MysqlPass = Client.Config.MysqlPass,
|
||
MysqlUser = Client.Config.MysqlUser
|
||
});
|
||
}
|
||
|
||
|
||
[HttpPost, ErrorFilter]
|
||
public WebResult Reading()
|
||
{
|
||
|
||
var session = CheckLoginDevice();
|
||
if (session == null) return PutData("机器人登录失效,请重新登录");
|
||
|
||
//如果服务端重启了,有可能就找不到,所以这里需要自动添加
|
||
try
|
||
{
|
||
session.IsReading = true;
|
||
session.ReadTime = DateTime.Now;
|
||
List<DeviceMessage> ResposeMsgs = new List<DeviceMessage>();
|
||
var time = DateTime.Now.AddSeconds(180);
|
||
while (time > DateTime.Now)
|
||
{
|
||
if (session.Messages.Count > 0)
|
||
{
|
||
var Msg = session.Messages.Dequeue();
|
||
if (Msg != null) ResposeMsgs.Add(Msg);
|
||
}
|
||
else
|
||
{
|
||
if (ResposeMsgs.Count > 0) break;
|
||
Thread.Sleep(100);
|
||
continue;
|
||
}
|
||
}
|
||
|
||
return PutData(ResposeMsgs);
|
||
}
|
||
finally
|
||
{
|
||
session.IsReading = false;
|
||
}
|
||
|
||
|
||
}
|
||
|
||
[HttpPost, ErrorFilter]
|
||
public WebResult Reply()
|
||
{
|
||
var device = CheckLoginDevice();
|
||
if (device == null) return PutData("机器人登录失效,请重新登录");
|
||
|
||
//要回复的ID
|
||
var Replyid = GetString("Msgid", true);
|
||
var Content = GetString("Content", true);
|
||
Client.ReplyMessages.Add(Replyid, new DeviceReplyMessage() { Replyid = Replyid, Content = Content, CreateTime = DateTime.Now });
|
||
return PutSuccess;
|
||
|
||
}
|
||
|
||
[HttpPost, ErrorFilter]
|
||
public WebResult SetRobotId()
|
||
{
|
||
var session = CheckLoginDevice();
|
||
if (session == null) return PutData("机器人登录失效,请重新登录");
|
||
|
||
var robotId = GetInt("RobotId", true);
|
||
if (robotId == 0) return PutData("设置失败,机器人的编号不能为0");
|
||
if (robotId != device.RobotId)
|
||
{
|
||
var oldRobots = Client.OnlineDevices.Where(x => x.Value.RobotId == robotId && x.Key != device.Id).ToList();
|
||
foreach (var item in oldRobots)
|
||
{
|
||
Client.OnlineDevices.TryRemove(item.Key, out _);
|
||
}
|
||
session.RobotId = robotId;
|
||
device.RobotId = robotId;
|
||
session.RobotId = robotId;
|
||
Db.Storageable(device).ExecuteCommand();
|
||
}
|
||
return PutSuccess;
|
||
}
|
||
|
||
private Device device;
|
||
private DeviceSession CheckLoginDevice()
|
||
{
|
||
var Id = GetString("Id", true);
|
||
var Token = GetString("Token", true);
|
||
device = Db.Queryable<Device>().WithCache().Single(f => f.Id == Id);
|
||
if (Client.OnlineDevices.ContainsKey(Id) && device != null && device.Token == Token)
|
||
{
|
||
return Client.OnlineDevices[Id];
|
||
}
|
||
return null;
|
||
|
||
|
||
|
||
}
|
||
}
|
||
}
|