using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using LFP_Manager.DataStructure; namespace LFP_Manager.Utils { static class csUtils { public static int GetModuleQty(CommConfig aConfig) { int mQty = 1; switch (aConfig.CommType) { case csConstData.CommType.COMM_SNMP: switch (aConfig.SnmpModelIndex) { case 6: // LFPR-481000H mQty = aConfig.SnmpMdQty; break; case 5: // LFPR-48900 mQty = 3; break; case 4: // LFPR-48600 mQty = 2; break; default: mQty = 1; break; } break; case csConstData.CommType.COMM_UARTCAN: mQty = 1; break; case csConstData.CommType.COMM_USBCAN: mQty = 1; break; } return mQty; } public static void TypingOnlyNumber(object sender, KeyPressEventArgs e, bool includePoint, bool includeMinus) { bool isValidInput = false; if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) { if (includePoint == true) { if (e.KeyChar == '.') isValidInput = true; } if (includeMinus == true) { if (e.KeyChar == '-') isValidInput = true; } if (isValidInput == false) e.Handled = true; } if (includePoint == true) { if (e.KeyChar == '.' && (string.IsNullOrEmpty((sender as DevExpress.XtraEditors.TextEdit).Text.Trim()) || (sender as DevExpress.XtraEditors.TextEdit).Text.IndexOf('.') > -1)) e.Handled = true; } if (includeMinus == true) { if (e.KeyChar == '-' && (!string.IsNullOrEmpty((sender as DevExpress.XtraEditors.TextEdit).Text.Trim()) || (sender as DevExpress.XtraEditors.TextEdit).Text.IndexOf('-') > -1)) e.Handled = true; } } public static bool[] Int16ToBitArray(Int16 data) { bool[] result = new bool[16]; for (int i = 0; i < 16; i++) { result[i] = (((data >> i) & 0x0001) == 0x0001) ? true : false; } return result; } public static byte[] GetCRC(byte[] pby, UInt16 nSize) { byte[] result = new byte[2]; UInt16 uIndex, i; UInt16 crc; byte uchCRCHi = 0xff; byte uchCRCLo = 0xff; for (i = 0; i < nSize; i++) { uIndex = (UInt16)(uchCRCHi ^ pby[i]); uchCRCHi = (byte)(uchCRCLo ^ csConstData.CRC.AuchCRCHi[uIndex]); uchCRCLo = csConstData.CRC.AuchCRCLo[uIndex]; } crc = (UInt16)(((UInt16)uchCRCHi << 8) | uchCRCLo); result[1] = (byte)(crc / 256); result[0] = (byte)(crc % 256); return result; } public static byte StrByte2toByte(byte a, byte b) { byte result = 0; byte a1 = 0, b1 = 0; if ((a >= (byte)'0')&&(a <= (byte)'9')) { a1 = (byte)(a - (byte)'0'); } else if ((a >= (byte)'a')&&(a <= (byte)'f')) { a1 = (byte)(a - (byte)'a'); } else if ((a >= (byte)'A')&&(a <= (byte)'F')) { a1 = (byte)((a - (byte)'A') + 0x0A); } if ((b >= (byte)'0') && (b <= (byte)'9')) { b1 = (byte)(b - (byte)'0'); } else if ((b >= (byte)'a') && (b <= (byte)'f')) { b1 = (byte)(b - (byte)'a'); } else if ((b >= (byte)'A') && (b <= (byte)'F')) { b1 = (byte)((b - (byte)'A') + 0x0A); } result = (byte)((b1 << 4) | a1); return result; } public static byte[] StrToByteArray(byte[] rData, int offset, int rlen) { int len = (rlen - 2) / 2; byte[] result = new byte[len]; for (int i = 0; i < len; i++) { result[i] = StrByte2toByte(rData[(i * 2) + offset + 2], rData[(i * 2) + offset + 1]); } return result; } public static byte[] StringToByte(string str) { byte[] StrByte = Encoding.UTF8.GetBytes(str); return StrByte; } public static long UnixTimeNow() { var timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)); return (long)timeSpan.TotalSeconds; } #region OPERATING WARNING FUCTION public static bool BitCheck(short rData, int pos) { if (((rData >> pos) & 0x0001) == 0x0001) return true; else return false; } #endregion public static UInt32 CalcKKTimeStamp(DateTime tDate) { //Time of test and calibration. Local time in seconds since 2000. //Ex: //Epoch time offset (1 January 2000 00:00:00) = 946684800 //Current epoch time (1 October 2019 12:00:00) = 1569931200 //Timestamp = 1569931200 - 946684800 = 623246400 //(Used 'https://www.epochconverter.com/' for conversion) DateTime baseDate = new DateTime(2000, 1, 1, 0, 0, 0); int bDate = ConvertToUnixTimestamp(baseDate); int nDate = ConvertToUnixTimestamp(tDate); return (UInt32)(nDate - bDate); } public static DateTime ConvertTimeStampToDateTime(UInt32 tStamp) { DateTime result = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); result = result.AddSeconds(tStamp); // DateTimeOffset.Now.ToUnixTimeSeconds() // (.NET Framework 4.6 +/.NET Core), // older versions: //var epoch = (DateTime.UtcNow - new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; return result; } public static int ConvertToUnixTimestamp(DateTime date) { DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0); TimeSpan diff = date - origin; return (int)Math.Floor(diff.TotalSeconds); } } }