/// <summary> /// 初使化设备 /// </summary> public bool InitDevice() { List <SmartWeb.Models.Component> list = new List <SmartWeb.Models.Component>(); using (var con = new MyContext()) { list = con.Component.Where(c => c.Type != ComponentType.WEB && c.Line == 0).ToList(); } if (list.Count > 0) { var deviceInfos = ProtoBDManager.DeviceQuery(null); foreach (var item in deviceInfos) { var type = ManagerHelp.GetComponentType((int)item.factory); string toId = ""; if (list.Where(c => c.Type == type).Any()) { toId = list.FirstOrDefault(c => c.Type == type).Cid; } //海康和大华组件尚未启动则不需要发送组件注册消息 if (toId == "") { continue; } assembly.SendDeveiceAdd(item, toId); } return(true); } else { ManagerHelp.isInit = false; return(false); } }
public IActionResult Save(string strEmbed) { try { string shipId = base.user.ShipId; int code = 1; string msg = ""; if (shipId == "") { return(new JsonResult(new { code = 1, msg = "船不存在,无法添加数据" })); } var model = JsonConvert.DeserializeObject <DeviceViewModel>(strEmbed); //陆地端远程添加设备 if (base.user.IsLandHome) { string tokenstr = HttpContext.Session.GetString("comtoken"); string identity = ManagerHelp.GetLandToId(tokenstr); if (string.IsNullOrEmpty(identity)) { return(new JsonResult(new { code = 1, msg = "当前船舶已失联,请重新连接" })); } string devComId = ManagerHelp.GetLandToId(tokenstr, ManagerHelp.GetComponentType(model.Factory)); if (string.IsNullOrEmpty(devComId)) { return(new JsonResult(new { code = 1, msg = Enum.GetName(typeof(Device.Factory), Convert.ToInt32(model.Factory)) + "组件未启动" })); } Device emb = GetProtoDevice(model); if (!string.IsNullOrEmpty(model.Id)) { assembly.SendDeveiceUpdate(emb, shipId + ":" + identity, model.Id); } else { assembly.SendDeveiceAdd(emb, shipId + ":" + identity); } code = GetResult(); } else { string identity = ManagerHelp.GetShipToId(ManagerHelp.GetComponentType(model.Factory)); if (identity == "") { return(new JsonResult(new { code = 1, msg = Enum.GetName(typeof(Device.Factory), model.Factory) + "组件未启动" })); } Device device = new Device(); if (!string.IsNullOrEmpty(model.Id)) { device = _context.Device.FirstOrDefault(c => c.Id == model.Id); if (device == null) { return(new JsonResult(new { code = 1, msg = "数据不存在" })); } } device.IP = model.IP; device.Name = model.Name; device.Nickname = model.Nickname; device.Password = model.Password; device.Port = model.Port; device.type = (Device.Type)model.Type; device.factory = (Device.Factory)model.Factory; device.Enable = model.Enable; if (string.IsNullOrEmpty(model.Id)) { device.Id = Guid.NewGuid().ToString(); _context.Device.Add(device); _context.SaveChanges(); model.Id = device.Id; Device emb = GetProtoDevice(model); assembly.SendDeveiceAdd(emb, identity); } else { Device emb = GetProtoDevice(model); _context.Device.Update(device); _context.SaveChanges(); assembly.SendDeveiceUpdate(emb, identity, device.Id); } code = GetResult(); if (code == -2) { Device emb = GetProtoDevice(model); assembly.SendDeveiceAdd(emb, identity); code = GetResult(); } } if (code == 400) { msg = "请求超时。。。"; } else if (code != 0) { msg = "请输入正确的设备参数"; } return(new JsonResult(new { code = code, msg = msg })); } catch (Exception ex) { _logger.LogError(ex, "保存设备信息异常", strEmbed); return(new JsonResult(new { code = 1, msg = "数据保存失败!" + ex.Message })); } }