/// <summary>登录</summary> /// <param name="code"></param> /// <param name="name"></param> /// <param name="ip"></param> /// <returns></returns> protected virtual Device Login(String code, String name, String ip) { var dv = Device; if (dv != null) { return(dv); } dv = Device.FindByCode(code); if (dv == null) { dv = new Device { Name = name, Code = code, Enable = true, }; dv.Insert(); } if (!dv.Enable) { throw new Exception($"[{dv.Name}/{dv.Code}]禁止登录"); } dv.Logins++; dv.LocalIP = ip; dv.LastLogin = DateTime.Now; dv.LastLoginIP = ip; dv.SaveAsync(); Device = dv; // 登录历史 var hi = new DeviceHistory { DeviceID = dv.ID, Name = dv.Name, Action = "登录", Success = true, CreateDeviceID = dv.ID, }; hi.SaveAsync(); return(dv); }
protected virtual DeviceHistory WriteHistory(String action, IDevice dv) { var ip = Remote?.EndPoint.Address + ""; var hi = new DeviceHistory { DeviceID = dv.ID, Name = dv.Name, Action = action, Success = true, CreateDeviceID = dv.ID, CreateTime = DateTime.Now, CreateIP = ip, }; hi.SaveAsync(); return(hi); }
/// <summary>保存操作历史</summary> /// <param name="action"></param> /// <param name="success"></param> /// <param name="content"></param> protected override void SaveHistory(String action, Boolean success, String content) { var hi = new DeviceHistory(); if (Current is Device dv) { if (hi.DeviceID == 0) { hi.DeviceID = dv.ID; } if (hi.Name.IsNullOrEmpty()) { hi.Name = dv + ""; } hi.Version = dv.Version; hi.CompileTime = dv.CompileTime; } else if (Online is DeviceOnline olt) { if (hi.DeviceID == 0) { hi.DeviceID = olt.DeviceID; } if (hi.Name.IsNullOrEmpty()) { hi.Name = olt.Name; } } hi.Action = action; hi.Success = success; hi.Remark = content; hi.CreateTime = DateTime.Now; if (Session is INetSession ns) { hi.CreateIP = ns.Remote + ""; } hi.SaveAsync(); }
protected override void OnReceive(ReceivedEventArgs e) { //base.OnReceive(e); var str = e.Packet.ToStr(); if (str.IsNullOrEmpty()) { return; } var dic = new JsonParser(str).Decode() as IDictionary <String, Object>; if (dic == null || dic.Count == 0) { return; } ManageProvider.UserHost = Remote.Host; object result = null; var cmd = dic["cmd"] + ""; // 输出日志 if (Host.CommandLog) { WriteLog("<={0}", str.Trim()); } else { WriteLog("<={0}", cmd); } var remark = ""; try { switch (cmd) { case "dHeartbeat": result = Heartbeat(cmd, dic); break; case "dRecord": result = UploadRecord(cmd, dic); break; } // 处理结果,做出响应 if (result != null) { var js = result.ToJson(); if (Host.CommandLog) { WriteLog("=>{0}", js.Trim()); } Send(js.GetBytes()); } } catch (Exception ex) { remark = ex.GetTrue()?.Message; } finally { if (cmd != "dHeartbeat") { var dv = Device ?? new Device(); // 写入历史 var hi = new DeviceHistory { DeviceID = dv.ID, Name = dv.Name, Action = cmd, Success = result != null, CreateDeviceID = dv.ID, Remark = remark, }; hi.SaveAsync(); } } }