public ActionResult SetParam(string endpoint, string name, string value, int did) { JsonResult result = new JsonResult(); bool bSuccess = false; string operation = ""; int slot = 0; try { OLPInfo olpInfo = new OLPInfo(); var arrPoint = endpoint.Split(':'); if (arrPoint.Length == 3) { slot = int.Parse(arrPoint[2]); TcpClientService tcp = new TcpClientService(arrPoint[0], int.Parse(arrPoint[1])); tcp.Connect(); OLPCommService service = new OLPCommService(tcp, slot); olpInfo.RefreshData(service); string methodName = "Set" + name.Replace("_", ""); var methodInfo = service.GetType().GetMethod(methodName); if (methodInfo != null) { //获取设置项 object[] arrDescription = methodInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if (arrDescription != null && arrDescription.Length > 0) { DescriptionAttribute desc = (DescriptionAttribute)arrDescription[0]; if (desc != null) { operation = desc.Description; } } //获取方法参数信息 var paramInfo = methodInfo.GetParameters(); object[] paramObject = new object[paramInfo.Length]; int i = 0; foreach (var e in paramInfo) { paramObject[i] = Convert.ChangeType(value, e.ParameterType); } var ret = (bool)methodInfo.Invoke(service, paramObject); if (ret) { bSuccess = true; result.Data = new { Code = "", Data = "设置成功" }; } else { result.Data = new { Code = "101", Data = "设置失败" }; } } else { result.Data = new { Code = "102", Data = "设置失败,未找到设置参数的方法" }; } tcp.Dispose(); } else { result.Data = new { Code = "100", Data = "设置失败,请求参数错误" }; } //日志记录 using (var ctx = new GlsunViewEntities()) { var d = ctx.Device.Find(did); var log = ctx.DeviceOperationLog.Create(); var user = ctx.User.Where(u => u.ULoginName == HttpContext.User.Identity.Name).FirstOrDefault(); //基本信息 log.DID = d.ID; log.DName = d.DName; log.DAddress = d.DAddress; log.SID = d.Subnet.ID; log.SName = d.Subnet.SName; log.SAddress = d.Subnet.SAddress; log.UID = user.ID; log.ULoginName = user.ULoginName; log.UName = user.UName; //业务信息 log.DOLCardSN = olpInfo.Serial_Number; log.DOLCardType = "OLP"; log.DOLDeviceSlot = short.Parse(slot.ToString()); log.DOLOperationDetials = operation; log.DOLOperationType = "板卡配置"; log.DOLOperationResult = bSuccess ? "成功" : "失败"; log.DOLOperationTime = DateTime.Now; log.Remark = ""; ctx.DeviceOperationLog.Add(log); ctx.SaveChanges(); } } catch (Exception ex) { result.Data = new { Code = "Exception", Data = "设置时发生异常" }; } return(result); }
/// <summary> /// OLP配置 /// </summary> /// <param name="info"></param> /// <param name="ip"></param> /// <param name="port"></param> /// <param name="slot"></param> /// <returns></returns> public ActionResult SetConfiguration(OLPInfo info, int mfId, string ip, int port, int slot, string configItems) { JsonResult result = new JsonResult(); var tcp = TcpClientServicePool.GetService(ip, port); if (tcp != null) { try { var arrProperty = configItems.Split(','); List <string> listException = new List <string>(); OLPCommService service = new OLPCommService(tcp, slot); List <string> listMethod = new List <string>(); Type t = info.GetType(); List <DeviceOperationLog> logs = new List <DeviceOperationLog>(); foreach (var p in arrProperty.OrderBy(e => e)) { //获取属性 var prop = t.GetProperty(p); if (prop != null) { string name = prop.Name; object value = prop.GetValue(info); //设置方法名 string methodName = "Set" + name.Replace("_", ""); var methodInfo = service.GetType().GetMethod(methodName); if (methodInfo != null) { listMethod.Add(methodInfo.Name); string operation = ""; //获取设置项说明 object[] arrDescription = methodInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if (arrDescription != null && arrDescription.Length > 0) { DescriptionAttribute desc = (DescriptionAttribute)arrDescription[0]; if (desc != null) { operation = desc.Description; } } //获取方法参数信息 var paramInfo = methodInfo.GetParameters(); object[] paramObject = new object[paramInfo.Length]; foreach (var e in paramInfo) { paramObject[0] = Convert.ChangeType(value, e.ParameterType); } var ret = (bool)methodInfo.Invoke(service, paramObject); if (!ret) { listException.Add(operation); } var log = new DeviceOperationLog { DOLCardSN = info.Serial_Number, DOLCardType = "OLP", DOLDeviceSlot = short.Parse(slot.ToString()), DOLOperationDetials = operation, DOLOperationType = "板卡配置", DOLOperationResult = ret ? "成功" : "失败", DOLOperationTime = DateTime.Now, Remark = "" }; logs.Add(log); } } } using (var ctx = new GlsunViewEntities()) { MachineFrame frame = ctx.MachineFrame.Find(mfId); var user = ctx.User.Where(u => u.ULoginName == HttpContext.User.Identity.Name).FirstOrDefault(); foreach (var log in logs) { //基本信息 log.DID = frame.ID; log.DName = frame.MFName; log.DAddress = frame.MFIP; log.UID = user.ID; log.ULoginName = user.ULoginName; log.UName = user.UName; } ctx.DeviceOperationLog.AddRange(logs); ctx.SaveChanges(); } if (listException.Count == 0) { result.Data = new { Code = "", Data = "配置成功" }; } else { string data = "配置失败:"; foreach (var e in listException) { data += e + " "; } result.Data = new { Code = "Exception", Data = data }; } } catch (Exception ex) { result.Data = new { Code = "Exception", Data = ex.Message }; } finally { TcpClientServicePool.FreeService(tcp); } } else { result.Data = new { Code = "Exception", Data = "获取TCP连接失败" }; } return(result); }