using System; using System.Collections.Generic; using log4net; using TracingDB.Entities; namespace TracingDB { public class BatteryAndFlowtubeInfo { static readonly ILog log = LogManager.GetLogger(typeof(BatteryAndFlowtubeInfo)); /// Constants public const string VitzrocellBatteryPartNr = "68118221"; public const string TadiranBatteryPartNr = "68117469"; public string PcbNumber; /// Unique PCB number of the water meter public string BattSapPartNr; /// Battery SAP part number public int BattBatchNr; /// Batch number (purchase order) public string BattMMYY; /// Production date public string BattSupplier; /// Supplier public string FlowtubeSapPartNr; /// Flowtube SAP part number public bool IsPorexFlowtube; /// public string PrintedInfo; /// 'T', 'Vi', '-', 'T/P', 'Vi/P' or '-/P' public DateTime TimeStamp; /// Record creation time bool complete { get { return (PcbNumber != null) && (BattSapPartNr != null) && (BattBatchNr != 0) && (BattMMYY != null) && (BattSupplier != null); } } public BatteryAndFlowtubeInfo() { TimeStamp = DateTime.Now; PrintedInfo = "-"; } /// /// Search MySQL DB and get battery info, add it into the record. /// /// true when successful public static BatteryAndFlowtubeInfo GetBatteryAndFlowtubeInfo(string pcbNumber, IList parts, string porexSapNumbers) { if (string.IsNullOrEmpty(pcbNumber)) { log.ErrorFormat("Invalid PcbBr, no battery info."); return null; } BatteryAndFlowtubeInfo info = new BatteryAndFlowtubeInfo() { PcbNumber = pcbNumber }; try { bool error = false; foreach (var part in parts) { if (part.CodeLocation == CodeLocation.OnPart) { if (porexSapNumbers.Contains(part.Name)) { info.IsPorexFlowtube = true; info.FlowtubeSapPartNr = part.Name; } } if (!string.IsNullOrEmpty(part.OraDBType)) { string[] oraItems = part.OraDBType.Split(new char[] { ' ' }); string oraType = oraItems[0]; string oraDescr = (oraItems.Length > 1) ? oraItems[1] : string.Empty; if ((oraType == "Batt") || (oraType == "Batt1") || (oraType == "Batt2")) { if (info.BattSapPartNr != null) { error = true; break; } info.BattSapPartNr = part.Name; int battBatchNr; info.BattBatchNr = int.TryParse(part.Code, out battBatchNr) ? battBatchNr : 1; info.BattSupplier = oraDescr; /// Should be "TADIRAN" or "VITZROCELL" } } if (part.CodeType == CodeType.DateMMYY) { if (info.BattMMYY != null) { error = true; break; } info.BattMMYY = part.Code; } } if (error || !info.complete) { log.ErrorFormat("Battery info. for PcbNr={0} wasn't found in MySQL database", info.PcbNumber); return null; } /// /// Set battery information printed on labels /// if (info.BattSupplier == "VITZROCELL") { info.PrintedInfo = "Vi"; } else if (info.BattSupplier == "TADIRAN") { info.PrintedInfo = "T"; } /// /// Append Porex flowtube info in case of a Porex flowtube /// if (info.IsPorexFlowtube) { info.PrintedInfo += "/P"; } return info; } catch (Exception exc) { log.ErrorFormat("Battery info. for PcbNr={0} wasn't found in MySQL database: {1}", info.PcbNumber, exc.Message); return null; } } public override string ToString() { return string.Format("PcbNr={0}, Battery={1}/{2}/{3} {4}, Time={5}", PcbNumber, BattBatchNr, BattMMYY, BattSupplier, (IsPorexFlowtube ? "porex" : ""), TimeStamp); } } }