using Logic.ProductionToProductMapper.Cordonel; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Drawing; using System.Globalization; using System.IO; using System.Reflection; using System.Text; using System.Threading; using System.Windows.Forms; using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisFile; using Xylem.Common.Logic.ProductionOrderCore.OrderData; using Xylem.Common.Logic.SoftwareAccessHelper; using Xylem.Common.Utils.Crc16Ccitt; namespace Xylem.Common.Ui.GenesisToolBox { /// /// FW update form /// public partial class FrmLutManagement : Form { #region Variables private MeterLutUpdate _meterLutUpdate; private String _lastFwUpdateState; private Int32 _lastSingleProgress; private DateTimeOffset _startTime; private Boolean _resetTimeMeasurement; private readonly Boolean _updateGui = true; private Boolean _lutFileValid; private Boolean _tryConnectPcb; private Boolean _lutFileSuccessfulWritten; private const String StrUpdateErrorMessage = "METROLOGY LOOKUP TABLE UPDATE FAILED!"; private const String StrUpdateSuccessMessage = "Metrology lookup table successfulley written!"; private const String StrLutFileInvalid = "LUT FILE INVALID"; private const String StrLutFileValid = "LUT file loaded"; private const String StrLutFileId = "GEN_LUT\0"; private const Int32 IndexLutFileId = 0; private const Int32 LengthLutFileId = 8; // data length, calculated from version field to the end of the data section private const Int32 IndexDataLength = 8; private const Int32 LengthDataLength = 4; // CCITT-CRC-16 , calculated from version field to the end of the data section private const Int32 IndexCrc = 12; private const Int32 LengthCrc = 2; // version private const Int32 IndexVersion = 14; private const Int32 LengthVersion = 2; // meter size as enumeration private const Int32 IndexMeterSize = 16; // header size of file private const Int32 LutFileHeaderLength = LengthLutFileId + LengthDataLength + LengthCrc + LengthVersion; // data size of file private const Int32 LutFileDataLength = 730; // LUT file entire file size private const Int32 SizeLutFile = LutFileHeaderLength + LutFileDataLength; // external update remarks form private readonly FrmHistory _frmHistory = new FrmHistory(); private readonly Version _version; // status information private static readonly Color ColorDefault = Color.Black; private static readonly Color ColorSuccess = Color.Green; private static readonly Color ColorProcessFailed = Color.Red; private const String StrSeparator = "-----------------------------------------------------" + "-----------------------------------------------------"; #endregion /// /// Ctor FW update /// public FrmLutManagement() { InitializeComponent(); var cultureInfo = new CultureInfo("en-GB"); Thread.CurrentThread.CurrentUICulture = cultureInfo; Thread.CurrentThread.CurrentCulture = cultureInfo; _version = Assembly.GetExecutingAssembly().GetName().Version; } String _StrLutRegion; String _StrLutVersion; MeterSize _MeterSize; UInt16 _FileCrc; UInt16 _Version; UInt32 _DataLength; String lutFilePathName; String lutFileName; private void btnOpenLutFile_Click(Object sender, EventArgs e) { if (dlgOpenLuFile.ShowDialog() != DialogResult.OK) { return; } var ErrorMessage = ""; lutFilePathName = dlgOpenLuFile.FileName; lutFileName = Path.GetFileNameWithoutExtension(lutFilePathName); var BinData = new List(File.ReadAllBytes(lutFilePathName)); // check if LUT file contains data if (BinData == null) { ErrorMessage = "File size mismatch!"; } btnStore.Enabled = false; var IsValid = false; var IndexLutFileId = 0; var LengthLutFileId = 8; var IndexDataLength = 8; var LengthDataLength = 4; // extract the identifier and check, if this is a LUT file var StrLutFileId = Encoding.UTF8.GetString(BinData.ToArray(), IndexLutFileId, IndexDataLength); _StrLutRegion = lutFileName.Split('_')[3]; _StrLutVersion = lutFileName.Split('_')[2]; _MeterSize = (MeterSize)BinData[IndexMeterSize]; _FileCrc = (UInt16)(BinData[IndexCrc] + (BinData[IndexCrc + 1] << 8)); _Version = (UInt16)(BinData[IndexVersion] + (BinData[IndexVersion + 1] << 8)); _DataLength = (UInt32)(BinData[IndexDataLength] + (BinData[IndexDataLength + 1] << 8) + (BinData[IndexDataLength + 2] << 16) + (BinData[IndexDataLength + 3] << 24)); // calculate CRC and compare this with the file CRC var entireFile = new List(); entireFile.AddRange(BinData); var payload = new List(); payload.AddRange(entireFile.GetRange(IndexVersion, LutFileDataLength + LengthVersion)); var calculatedCrc = Crc16Ccitt.CalculateMsb1021(payload.ToArray()); if (calculatedCrc != _FileCrc) { ErrorMessage = "CRC mismatch!"; } var CalculatedCrc = calculatedCrc; IsValid = true; btnStore.Enabled = IsValid; tbxLutFilePathName.Text = lutFilePathName; tbxMeterLutCrc.Text = _FileCrc.ToString("X4"); tbxMeterMeterSize.Text = _MeterSize.ToString(); tbxRegionRadio.Text = _StrLutRegion; tbxVersion.Text = _StrLutVersion; tbxDataLengt.Text = _DataLength.ToString(); //lutFilePathName. ////mettbl_202203101419_v0.04_EMEA_DN50_v1.00_0xC50F } /// /// Clear data table and set genesis to not connected /// private void Init() { } private void label6_Click(Object sender, EventArgs e) { } private void button1_Click(Object sender, EventArgs e) { var FileList = new Dictionary(); var fileConfig = new FileInfo(tbxLutFilePathName.Text); FileList = new Dictionary(); FileList.Add(fileConfig.Name, fileConfig.FullName); StringBuilder sbUrl = new StringBuilder(); sbUrl.Append("http://10.49.40.25/MeterProcessState/api/FileUpToDate/PostLutFile"); //sbUrl.Append("http://localhost:56011//api/FileUpToDate/PostLutFile"); // sbUrl.Append($"?DataLength={_DataLength}"); sbUrl.Append($"&version={_Version}"); sbUrl.Append($"&crc={_FileCrc}"); sbUrl.Append($"&Metersize={_MeterSize.GetHashCode()}"); sbUrl.Append($"&Region={_StrLutRegion}"); var result = LocalWebRequest.PostBinaryFileRequestAsync(sbUrl.ToString(), lutFileName, File.ReadAllBytes(lutFilePathName), 2800); } private void label4_Click(Object sender, EventArgs e) { } private void btnLoad_Click(Object sender, EventArgs e) { StringBuilder sbUrl = new StringBuilder(); sbUrl.Append("http://10.49.40.25/MeterProcessState/api/FileUpToDate/GetLutFiles"); //sbUrl.Append("http://localhost:56011/api/FileUpToDate/GetLutFiles"); //sbUrl.Append($"?DataLength={_DataLength}"); //sbUrl.Append($"&version={_Version}"); //sbUrl.Append($"&crc={_FileCrc}"); //sbUrl.Append($"&Metersize={_MeterSize.GetHashCode()}"); //sbUrl.Append($"&Region={_StrLutRegion}"); try { var ret = LocalWebRequest.GetRequest(sbUrl.ToString(), 80000); var a = JsonConvert.DeserializeObject>(ret); dgvLuts.DataSource = a; } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }