Program.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using Microsoft.Extensions.Caching.Memory;
  2. using Microsoft.Extensions.Options;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5. using Polly;
  6. using Polly.Timeout;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.IO;
  11. using System.IO.Compression;
  12. using System.Linq;
  13. using System.Net;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. namespace Decompress
  17. {
  18. class Program
  19. {
  20. public enum ResponseState
  21. {
  22. Success = 0,
  23. Error = 1,
  24. Timeout = 2,
  25. }
  26. public class MyClass
  27. {
  28. public string Name;
  29. public string Description;
  30. }
  31. private static string _path = "./appsettings.json";
  32. public struct IecServerData
  33. {
  34. /// <summary>
  35. /// 数据类型 1遥测 2遥信
  36. /// </summary>
  37. public int dataType { get; set; }
  38. /// <summary>
  39. /// 装置地址
  40. /// </summary>
  41. public byte devAddr { get; set; }
  42. /// <summary>
  43. /// cpu扇区号
  44. /// </summary>
  45. public byte devCpu { get; set; }
  46. /// <summary>
  47. /// 信息体地址
  48. /// </summary>
  49. public int inf { get; set; }
  50. /// <summary>
  51. /// 遥测值
  52. /// </summary>
  53. public float ycValue { get; set; }
  54. /// <summary>
  55. /// 遥信值
  56. /// </summary>
  57. public int yxValue { get; set; }
  58. /// <summary>
  59. /// 时间
  60. /// </summary>
  61. public DateTime dateTime { get; set; }
  62. /// <summary>
  63. /// 装置名称
  64. /// </summary>
  65. public string devName { get; set; }
  66. }
  67. static async Task Main(string[] args)
  68. {
  69. DateTime time = default;
  70. var timeStr = string.Format("{0:d2}:{1:d2}:{2:d2}.{3:d3}", 1, 1, 1, 1);
  71. if (!DateTime.TryParse(timeStr, out time))
  72. {
  73. time = DateTime.Now;
  74. }
  75. await Console.Out.WriteLineAsync();
  76. List<IecServerData> list = new List<IecServerData>();
  77. var test = list.FirstOrDefault();
  78. Console.WriteLine();
  79. bool bFlag = true;
  80. var test100 = bFlag.ToString().ToLower();
  81. Console.WriteLine(bFlag.ToString());
  82. MemoryCache cache = new MemoryCache(Options.Create<MemoryCacheOptions>(new MemoryCacheOptions()));
  83. cache.Set("test", new MyClass() { Name = "guor", Description = "test" }, TimeSpan.FromSeconds(15));
  84. Task.Delay(2000).Wait();
  85. var obj1 = cache.Get("test");
  86. cache.Remove("test");
  87. Console.WriteLine(obj1);
  88. var x1 = DateTime.Now - TimeSpan.FromSeconds(6);
  89. if (x1 > DateTime.Now)
  90. {
  91. Console.WriteLine("cao");
  92. }
  93. var str = "D:\\ISAS\\Data\\SysAttachment\\Robot\\Robot01\\2021\\11\\18\\6df578e3a90b4a42ba331addc3c7b1c3\\CCD\\21_Robot01_20211118140205.jpg";
  94. var parh = Path.GetDirectoryName(str);
  95. Console.WriteLine(parh);
  96. return;
  97. DateTime dt = DateTime.ParseExact("202109", "yyyyMM", null, DateTimeStyles.AssumeLocal);
  98. Console.WriteLine(dt);
  99. using (StreamReader reader = File.OpenText(_path))
  100. {
  101. JObject obj = (JObject)JToken.ReadFrom(new JsonTextReader(reader));
  102. //var token = obj.SelectToken(key);
  103. //token.
  104. //obj.SelectToken(key) = value;
  105. obj["ServiceConfig"]["SubstationName"] = "test";
  106. Console.WriteLine();
  107. }
  108. return;
  109. var policy = Policy<HttpWebResponse>.Handle<Exception>()
  110. .FallbackAsync(c =>
  111. {
  112. ////熔断后再来个通知
  113. //Console.WriteLine("熔断完成,通知一下");
  114. return null;
  115. });
  116. //设置一个超时时间,里面加个回调函数给个提示
  117. var pTimeout = Policy.TimeoutAsync(5, TimeoutStrategy.Pessimistic,
  118. async (context, timespan, task) =>
  119. {
  120. Console.WriteLine("Timeout!");
  121. //throw new Exception("Timeout!");
  122. });
  123. var excPolicy = policy.WrapAsync(pTimeout);
  124. var response = await excPolicy.ExecuteAsync(async () =>
  125. {
  126. HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://localhost:4431/swagger/index.html");
  127. var result = await request.GetResponseAsync();
  128. return (HttpWebResponse)result;
  129. });
  130. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  131. {
  132. string rstStr = reader.ReadToEnd();
  133. try
  134. {
  135. //rstObj = JsonConvert.DeserializeObject<T>(rstStr);
  136. Console.WriteLine(rstStr);
  137. }
  138. catch
  139. {
  140. throw new Exception("请检查主站url是否正确!");
  141. }
  142. reader.Close();
  143. reader.Dispose();
  144. }
  145. Console.WriteLine("hello");
  146. Console.ReadLine();
  147. }
  148. public static void DecompressFileWithBrotli(string fileName)
  149. {
  150. FileStream inputStream = File.OpenRead(fileName);
  151. using (MemoryStream outputStream = new MemoryStream())
  152. using (var compressStream = new BrotliStream(inputStream, CompressionMode.Decompress))
  153. {
  154. compressStream.CopyTo(outputStream);
  155. outputStream.Seek(0, SeekOrigin.Begin);
  156. inputStream.Dispose();
  157. string filenameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
  158. FileStream fileStream = new FileStream(filenameWithoutExtension + ".xlsx", FileMode.OpenOrCreate);
  159. var bytes = new byte[outputStream.Length];
  160. outputStream.Read(bytes);
  161. fileStream.Write(bytes);
  162. }
  163. }
  164. }
  165. }