private void FillToolStatusTable(DataTable dtToolStatus) { DataRow row = null; if (!dtToolStatus.Columns.Contains("PointID")) { dtToolStatus.Columns.Add("PointID", typeof(int)); } if (!dtToolStatus.Columns.Contains("InterlockStatus")) { dtToolStatus.Columns.Add("InterlockStatus", typeof(string)); } if (!dtToolStatus.Columns.Contains("InterlockState")) { dtToolStatus.Columns.Add("InterlockState", typeof(bool)); } if (!dtToolStatus.Columns.Contains("InterlockError")) { dtToolStatus.Columns.Add("InterlockError", typeof(bool)); } if (!dtToolStatus.Columns.Contains("IsInterlocked")) { dtToolStatus.Columns.Add("IsInterlocked", typeof(bool)); } int blockId = 0; BlockState blockState; var blocks = Session.Query <Block>().ToList(); Dictionary <int, BlockState> dict = new Dictionary <int, BlockState>(); var points = Session.Query <Point>().ToList(); var interlocks = Session.Query <ActionInstance>().Where(x => x.ActionName == "Interlock").ToList(); foreach (DataRow dr in dtToolStatus.Rows) { row = dr; blockState = null; if (dr["ResourceID"] != DBNull.Value) { dr.SetField("InterlockStatus", "Blank"); dr.SetField("InterlockState", false); dr.SetField("InterlockError", false); dr.SetField("IsInterlocked", true); if (dr["PointID"] == DBNull.Value) { var inst = interlocks.FirstOrDefault(x => x.ActionID == row.Field <int>("ResourceID")); if (inst != null) { dr.SetField("PointID", inst.Point); } else { dr.SetField("PointID", 0); } } int pointId = dr.Field <int>("PointID"); if (pointId > 0) { Point point = points.First(x => x.PointID == pointId); blockId = point.Block.BlockID; if (dict.ContainsKey(blockId)) { blockState = dict[blockId]; if (blockState == null) { row.SetField("InterlockStatus", "Wago block fault detected."); row.SetField("InterlockError", true); } } else { //We try to get returned data try { BlockResponse resp = GetBlockState(blockId); blockState = resp.EnsureSuccess().BlockState; } catch (Exception ex) { string errmsg = ex.ToString(); row.SetField("InterlockStatus", "Wago block fault detected."); row.SetField("InterlockError", true); } //blockState might be null here if there was a fault but that's ok dict.Add(blockId, blockState); } if (blockState != null) { bool pointState = blockState.GetPointState(point.PointID); row.SetField("InterlockState", pointState); if (pointState) { row.SetField("InterlockStatus", "Tool Enabled"); } else { row.SetField("InterlockStatus", "Tool Disabled"); } } } else { //this is not an error row.SetField("InterlockStatus", "No interlock for resource."); row.SetField("IsInterlocked", false); } } } }
public static BlockState GetBlockState(int blockId) { BlockResponse resp = ServiceProvider.Current.Control.GetBlockState(blockId); return(resp.EnsureSuccess().BlockState); }