using System; using Newtonsoft.Json; using Xylem.Common.CommonCore.Configuration; using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisCore; using Xylem.Common.Hardware.WaterMeter.Genesis.Registers; using Xylem.Common.Logic.SoftwareAccessHelper; namespace Xylem.Common.Hardware.WaterMeter.Genesis.GenesisStatus { /// /// Handler for the production status check. This will be feed by the DB. /// - Calculation of drained battery, /// - Assembly capability check, /// - Shipping capability check. /// public static class GenesisStatusHandler { /// /// Get all production infos for skip process checks or limits for production like: /// - Battery drained percentage upper limit, /// - Production due date for a special register, /// - Required life time for assembly line, /// - Required life time for shipping line. /// /// /// /// production status /// true if successfully executed /// /// - Initial. /// public static Boolean GetProductionSkipAndLifeTimeInfoFromDb(String pcbId, Int32 orderNumber, out GenesisStatus status) { if (string.IsNullOrEmpty(pcbId)) { status = null; return false; } try { var url = ServiceUrls.GetProductionSkipAndLifeTimeInfoServiceUrl(); url += $"{pcbId}&orderNumber={orderNumber}"; var response = LocalWebRequest.GetRequest(url, 2000); status = JsonConvert.DeserializeObject(response); } catch (Exception ) { status = null; return false; } return true; } /// /// Read and calculate life time information. /// /// /// /// /// /// - Initial. /// public static Boolean BuildLifeTimeInformation(GenesisMeter currentGenesis, GenesisStatus status) { Boolean retVal; if (currentGenesis == null || status == null) return false; // read information from genesis try { if (!currentGenesis.IsLoggedOn) currentGenesis.Login(); status.ExceededLifeTime_s = RegisterConverter.ByteArrayToValue( currentGenesis.ReadRegister(Register.Powermon.BatteryExceededSeconds)); status.DrainedBatteryLoad_uAs = RegisterConverter.ByteArrayToValue( currentGenesis.ReadRegister(Register.Powermon.BatteryDrainedLoad, 8)); status.BatteryQuantity = RegisterConverter.ByteArrayToValue( currentGenesis.ReadRegister(Register.Powermon.BatteryQuantity)); status.InitialBatteryLoad_mAh = RegisterConverter.ByteArrayToValue( currentGenesis.ReadRegister(Register.Powermon.BatteryInitialLoad)); currentGenesis.Logout(); retVal = CalculateLifeTime(status); } catch (Exception) { retVal = false; } return retVal; } /// /// Calculation of life time values. /// /// /// true if all calculations could be executed /// /// - Initial. /// public static Boolean CalculateLifeTime(GenesisStatus status) { var retVal = true; // Calculate the initial overall battery load of all batteries in uAs as the drained load uses this unit. var initialBatteryLoad_uAs = 1000 * 3600 * status.InitialBatteryLoad_mAh * (UInt64)status.BatteryQuantity; // Relation of drained to initial load, set the battery to fully unloaded for failed check to force a recalculation var drainedBatteryLoad_rel = 1.0; if (initialBatteryLoad_uAs != 0) { drainedBatteryLoad_rel = (Double)status.DrainedBatteryLoad_uAs / initialBatteryLoad_uAs; } else { retVal = false; } status.DrainedBatteryLoadPercent = drainedBatteryLoad_rel * 100; // The estimated life time uses the drained load over the actual exceeded life time and assumes, that further on // an identical load will be consumed over the remaining life cycle. The initial load will be multiplied with the // drained load relation. Set the estimated time to the exceeded time to force a recalculation on failed input. var estimatedLifeTime_s = (Double)status.ExceededLifeTime_s; if (drainedBatteryLoad_rel != 0.0) { estimatedLifeTime_s = status.ExceededLifeTime_s / drainedBatteryLoad_rel; } else { retVal = false; } var remainingLifetime_s = estimatedLifeTime_s - status.ExceededLifeTime_s; // convert the seconds to years status.RemainingLifeTimeYears = remainingLifetime_s / 3600 / 24 / 365.25; status.UNTLimit = 1368000000 + (status.ExceededLifeTime_s * 77.7); status.UNTStatus = status.UNTLimit >= status.DrainedBatteryLoad_uAs; return retVal; } } }