using DTS.Common.Enums; using DTS.Common.Interface.DASFactory; using DTS.Common.Interface.DASFactory.Config; using DTS.Common.Utilities; using DTS.Common.Utilities.Logging; using DTS.DASLib.Command.SLICE; using System; using System.Collections.Generic; namespace DTS.DASLib.Service { public class EIDReader { /// /// returns true if the device supports QueryOneWireID /// false otherwise. /// this is a bit strange, since QueryOneWireID is a SLICE Command /// and only slice devices should support it, and only some slice /// devices at that, however the current code doesn't care so this is /// just a helper for /// 22296 No need to query for system ID when querying a TSR AIR device /// really a lot of devices probably belong in here, but I'm keeping the /// existing behavior for now... /// /// /// true if device supports QueryOneWireID, false otherwise public static bool SupportsQueryOneWireID(ICommunication comm) { switch (comm) { case EthernetTsrAir _: case WinUSBTsrAir _: return false; } return true; } private static EID[] Retrive_EIDs(ICommunication comm, byte DASChannelNumber, int idType) { if (!SupportsQueryOneWireID(comm)) { return null; } var idList = new List(); try { var IDQuery = new QueryOneWireID(comm); IDQuery.StackChannel = DASChannelNumber; IDQuery.IdType = (QueryOneWireID.IdTypes)idType; IDQuery.SyncExecute(); if (IDQuery.IDs.Count > 0) { for (int i = 0; i < IDQuery.IDs.Count; i++) { idList.Add(new EID(HexEncoding.ToString(IDQuery.IDs[i]))); } return idList.ToArray(); } } catch (Exception ex) { APILogger.Log("Failed to retrieve ids", comm.SerialNumber, ex); } // no id's return null; } private static EID[] Retrive_EIDs(ICommunication comm, int moduleIndex) { if (!SupportsQueryOneWireID(comm)) { return null; } var idList = new List(); Command.TDAS.ReadSensorIDs rsi = new Command.TDAS.ReadSensorIDs(comm); rsi.ModuleIndex = moduleIndex; rsi.SyncExecute(); if (rsi.IDs != null && rsi.IDs.Count > 0) { foreach (string id in rsi.IDs) { idList.Add(new EID(id)); } return idList.ToArray(); } else { return null; } } public static IEID[] RetriveEIDs(ICommunication comm, byte DASChannelNumber, int idType) { //don't return empty set as calstation indexes it if it's not null :/ if (!SupportsQueryOneWireID(comm)) { return null; } //I have to return null here rather than empty set as there's a consumer that automatically index 0 if it's not null //and it's in calstation code, so I want to avoid breaking that if (RunTestVariables.BypassEIDRead) { return null; } for (int RetryCounter = 0; RetryCounter < 3; RetryCounter++) { var ids = Retrive_EIDs(comm, DASChannelNumber, idType); if (ids != null && ids.Length > 0) { bool bValid = true; foreach (var id in ids) { if (!id.IsValid()) { bValid = false; string target = ""; if (comm is IDASCommunication) { target = (comm as IDASCommunication).ToString(); } APILogger.LogString(string.Format("EIDReader.RetriveEIDs: Retry for {0} on channel {1}, ID({2})", target, DASChannelNumber, id.ID)); break; } } // if we made it here, they're all valid if (bValid) { return ids; } } } return null; } /// /// blank EID returned by some command queries (TDAS IDX for example) /// public const string BLANK_ID = "0000000000000000"; /// /// returns true if the EID is blank /// /// /// true if is blank, false otherwise public static bool IsBlankID(string id) { return id.Equals(BLANK_ID); } public static EID[][] RetrieveEIDsG5(ICommunication comm) { if (RunTestVariables.BypassEIDRead) { return new EID[0][]; } var eids = new List(); try { var idx = new Command.TDAS.RackIDX(comm); idx.SyncExecute(); foreach (var channelIDs in idx.IDs) { var ids = new List(); foreach (var channelID in channelIDs) { if (IsBlankID(channelID)) { ids.Add(new EID()); } else { ids.Add(new EID(channelID)); } } eids.Add(ids.ToArray()); } } catch (Exception ex) { APILogger.Log("RetrieveEIDsG5 failed", ex); } return eids.ToArray(); } public static EID[] RetriveEIDs(ICommunication comm, int moduleIndex) { if (!SupportsQueryOneWireID(comm)) { return null; } for (int RetryCounter = 0; RetryCounter < 3; RetryCounter++) { var ids = Retrive_EIDs(comm, moduleIndex); if (null != ids && ids.Length > 0) { for (int i = 0; i < ids.Length; i++) { if (!ids[i].IsValid()) { string target = ""; if (comm is IDASCommunication) { target = (comm as IDASCommunication).ToString(); } APILogger.LogString(string.Format("EIDReader.RetreiveEIDS: ID invalid for {0} on module {1}, ID({2})", target, moduleIndex, ids[i].ID)); break; ; } } return ids; } } return null; } } }