tbf/TracingDB/BatteryInfo.cs
2018-08-28 09:44:39 +02:00

116 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
using log4net;
using TracingDB.Entities;
namespace TracingDB
{
public class BatteryInfo
{
static readonly ILog log = LogManager.GetLogger(typeof(BatteryInfo));
/// 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 PrintedBatteryInfo; /// 'T', 'Vi' or '-'
public DateTime TimeStamp; /// Record creation time
bool complete { get { return (PcbNumber != null) && (BattSapPartNr != null) && (BattBatchNr != 0) && (BattMMYY != null) && (BattSupplier != null); } }
public BatteryInfo()
{
TimeStamp = DateTime.Now;
PrintedBatteryInfo = "-";
}
/// <summary>
/// Search MySQL DB and get battery info, add it into the record.
/// </summary>
/// <returns>true when successful</returns>
public static BatteryInfo GetBatteryInfo(string pcbNumber, IList<Record> records)
{
if (string.IsNullOrEmpty(pcbNumber))
{
log.ErrorFormat("Invalid PcbBr, no battery info.");
return null;
}
BatteryInfo info = new BatteryInfo() { PcbNumber = pcbNumber };
try
{
bool error = false;
foreach (var rec in records)
{
if (rec.Part.CodeLocation == CodeLocation.OnPallet)
{
if (rec.Part.Name == VitzrocellBatteryPartNr)
{
if (info.BattSapPartNr != null) { error = true; break; }
info.BattSapPartNr = VitzrocellBatteryPartNr;
int battBatchNr;
info.BattBatchNr = int.TryParse(rec.Code, out battBatchNr) ? battBatchNr : 1;
info.BattSupplier = "VITZROCELL";
}
else if (rec.Part.Name == TadiranBatteryPartNr)
{
if (info.BattSapPartNr != null) { error = true; break; }
info.BattSapPartNr = TadiranBatteryPartNr;
int battBatchNr;
info.BattBatchNr = int.TryParse(rec.Code, out battBatchNr) ? battBatchNr : 1;
info.BattSupplier = "TADIRAN";
}
}
if (rec.Part.CodeType == CodeType.DateMMYY)
{
if (info.BattMMYY != null) { error = true; break; }
info.BattMMYY = rec.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.PrintedBatteryInfo = "Vi";
}
else if (info.BattSupplier == "TADIRAN")
{
info.PrintedBatteryInfo = "T";
}
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}, Time={4}", PcbNumber, BattBatchNr, BattMMYY, BattSupplier, TimeStamp);
}
}
}