using System.Text; namespace RuleEngine.Common { public class StaticResources { public static void Log2File(string filePath, string data) { try { FileInfo info = new FileInfo(filePath); try { if (info.Exists && info.Length > 10000000) // delete the file first if 10 MB { File.Delete(filePath); } } catch(Exception ex) { Console.WriteLine(ex.Message); } using var fs = File.Open(filePath, FileMode.Append); using var sw = new StreamWriter(fs); sw.WriteLine(data); } catch (Exception ex) { Console.WriteLine(ex.Message); } } public static void Log2FileByMonth(string folderPath,string fileName, string data,bool IsAddTime=true) { try { DateTime dt = DateTime.UtcNow; TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("SE Asia Standard Time"); DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(dt, cstZone); var folderNameByMonth = localTime.Year.ToString() + "-" + localTime.Month.ToString(); folderPath = Path.Combine(folderPath, folderNameByMonth); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } var filePath = Path.Combine(folderPath, fileName); if(IsAddTime) { data = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss ") + data; } Log2File(filePath, data); } catch (Exception ex) { Console.WriteLine(ex.Message); } } public static void LogException2File(string folderPath, string fileName,string exception) { try { var tmpTime = DateTime.UtcNow.AddHours(7); folderPath += tmpTime.Year.ToString() + "-" + tmpTime.Month.ToString(); string filePath = folderPath + "\\" + fileName + ".txt"; try { FileInfo info = new FileInfo(filePath); if (info.Exists && info.Length > 10000000) // delete the file first if 10 MB { File.Delete(filePath); } } catch(Exception ex) { Console.WriteLine(ex.Message); } using var fs = File.Open(filePath, FileMode.Append); using var sw = new StreamWriter(fs); sw.WriteLine(exception); } catch(Exception ex) { Console.WriteLine(ex.Message); } } public static double DistanceGpsCalculate(double lat1, double lon1, double lat2, double lon2, char unit) { try { double theta = lon1 - lon2; double Dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta)); Dist = Math.Acos(Dist); Dist = rad2deg(Dist); Dist = Dist * 60 * 1.1515; if (unit == 'K') { Dist = Dist * 1.609344; } else if (unit == 'N') { Dist = Dist * 0.8684; } if (double.IsNaN(Dist)) return 0; return Dist; } catch { return 0; } } public static double deg2rad(double deg) { return (deg * Math.PI / 180.0); } public static double rad2deg(double rad) { return (rad / Math.PI * 180.0); } public static string ConvertToHexaString(string strRFID) { string strResult = ""; int number = 0; int.TryParse(strRFID, out number); if (number != 0) { string hexaStr = number.ToString("X6"); string strNum1 = hexaStr.Substring(0, 2); string strNum2 = hexaStr.Substring(2, 2); string strNum3 = hexaStr.Substring(4, 2); try { int num1 = int.Parse(strNum1, System.Globalization.NumberStyles.HexNumber); int num2 = int.Parse(strNum2, System.Globalization.NumberStyles.HexNumber); int num3 = int.Parse(strNum3, System.Globalization.NumberStyles.HexNumber); int num4 = num1 ^ num2 ^ num3; strResult = hexaStr; // +num4.ToString("X2"); //bo thong tin checksum } catch (Exception) { strResult = ""; } } return strResult; } public static List ConvertRFIDStringToList(string RFIDString) { // checksum to get rfid var rfidList = new List(); int checksum = 0; for (int k = 0; k < RFIDString.Length - 7; k++) { string strNum1 = RFIDString.Substring(k, 2); string strNum2 = RFIDString.Substring(k + 2, 2); string strNum3 = RFIDString.Substring(k + 4, 2); string strNum4 = RFIDString.Substring(k + 6, 2); // loai bo cac RFID ffffffff vĂ  ffffff01 if (strNum1 == "ff" && strNum2 == "ff") { continue; } try { int num1 = int.Parse(strNum1, System.Globalization.NumberStyles.HexNumber); int num2 = int.Parse(strNum2, System.Globalization.NumberStyles.HexNumber); int num3 = int.Parse(strNum3, System.Globalization.NumberStyles.HexNumber); checksum = num1 ^ num2 ^ num3; } catch (Exception) { } int num4 = int.Parse(strNum4, System.Globalization.NumberStyles.HexNumber); if (checksum == num4) { string rfid_hex = strNum1 + strNum2 + strNum3; rfidList.Add(rfid_hex); } } return rfidList; } public static string GetString(byte[] buffer, int index, int count) { try { var enc = new UTF8Encoding(); if (index + count > buffer.Length) return ""; var i = 0; var temp = ""; while (i < count) { if (buffer[index + i] == '\0') break; temp += enc.GetString(buffer, index + i, 1); i++; } return temp.ToLower(); } catch { return ""; } } } }