/// <summary> /// 初使化船状态 /// </summary> public void InitStatus() { using (var con = new MyContext()) { var components = con.Component.Where(c => c.Type == ComponentType.AI && c.Line == 0).ToList(); if (components.Count > 0) { var ship = con.Ship.FirstOrDefault(); if (ship != null) { foreach (var item in components) { assembly.SendStatusSet(ship, StatusRequest.Type.SAIL, item.Cid); } } } } }
/// <summary> /// 保存船状态 /// </summary> /// <param name="ship"></param> /// <returns></returns> public IActionResult Save(string id, string name, int type) { try { if (!base.user.EnableConfigure) { new JsonResult(new { code = 1, msg = "您没有权限修改数据!" }); } int code = 1; string errMsg = ""; if (base.user.IsLandHome) { string cid = base.user.ShipId; string tokenstr = HttpContext.Session.GetString("comtoken"); string identity = ManagerHelp.GetLandToId(tokenstr); if (string.IsNullOrEmpty(identity)) { return(new JsonResult(new { code = 1, msg = "当前船舶已失联,请重新连接" })); } Ship ship = new Ship() { type = (Ship.Type)type, Name = name, Flag = type == 1 ? true : false }; string toId = cid + ":" + identity; assembly.SendStatusSet(ship, StatusRequest.Type.SAIL, toId); assembly.SendStatusSet(ship, StatusRequest.Type.NAME, toId); code = GetResult(); if (code == 0) { var component = _context.Component.FirstOrDefault(c => c.Cid == cid); if (component != null) { var landship = _context.Ship.FirstOrDefault(c => c.Id == component.ShipId); if (landship != null) { landship.Name = ship.Name; landship.type = ship.type; //航行类型为:自动时,默认状态为停港 landship.Flag = ship.Flag; _context.Ship.Update(landship); _context.SaveChanges(); } } } } else { #region 船舶端修改船状态 if (!string.IsNullOrEmpty(id)) { var ship = _context.Ship.FirstOrDefault(c => c.Id == id); if (ship != null) { ship.Name = name; ship.type = (Ship.Type)type; //航行类型为:自动时,默认状态为停港 ship.Flag = type == 1 ? true : false; } _context.Ship.Update(ship); _context.SaveChanges(); SendMqMsg(ship); code = 0; } #endregion } if (code == 400) { errMsg = "网络超时。。。"; } else if (code != 0) { errMsg = "数据保存失败"; } return(new JsonResult(new { code = code, msg = errMsg })); } catch (Exception ex) { _logger.LogError("保存船信息异常Save(" + id + "," + name + "," + type + ")" + ex.Message); return(new JsonResult(new { code = 0, msg = "数据保存失败" + ex.Message })); } }