RemoteToLocal.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. using Dm.util;
  2. using SqlSugar;
  3. using SqlSugarORM;
  4. using System.Reflection;
  5. using Universal;
  6. namespace DB_Proxima;
  7. public class RemoteToLocal
  8. {
  9. private SqlSugarCustom? _orm;
  10. public bool Initialize(string dbName, string password, string host, string username)
  11. {
  12. if (this._orm is not null)
  13. return false;
  14. _orm = new SqlSugarCustom();
  15. _orm.CreateDataBase(dbName);
  16. _orm.Initialize(null);
  17. string dbString = $"Database={dbName};Password={password};Host={host};Username={username};Persist Security Info=True";
  18. return _orm.Open(dbString, DbType.PostgreSQL, true);
  19. }
  20. public bool CreateTablePM(IDictionary<string, object> data)
  21. {
  22. if (this._orm is null)
  23. return false;
  24. Dictionary<string, object> ValueSensor = [];
  25. Dictionary<string, object> StatusSensor = [];
  26. Dictionary<string, object> leakCheck = [];
  27. Dictionary<string, object> recipe = [];
  28. Dictionary<string, object> aoValue = [];
  29. Dictionary<string, object> ffu = [];
  30. Dictionary<string, object> mfc = [];
  31. Dictionary<string, object> gaslineHeater = [];
  32. Dictionary<string, object> bufferFoup = [];
  33. Dictionary<string, object> avValue = [];
  34. foreach (var item in data)
  35. {
  36. if (item.Value is not IDictionary<string, object> values)
  37. {
  38. switch (item.Key)
  39. {
  40. case string s when s.EndsWith("Enable"):
  41. continue;
  42. case string s when s.StartsWith("LeakCheck"):
  43. leakCheck.Add(item.Key, item.Value);
  44. continue;
  45. default:
  46. recipe.Add(item.Key, item.Value);
  47. continue;
  48. }
  49. }
  50. switch (item.Key)
  51. {
  52. case "APC":
  53. case "APCVATGV":
  54. case "BoatElevatorServo":
  55. case "BoatRotationServo":
  56. case "BufferServo":
  57. case "Shutter":
  58. CreateTable(item.Key, values);
  59. continue;
  60. case string s when s.startsWith("Trig"):
  61. if (item.Value is IDictionary<string, object> value)
  62. aoValue.Add(item.Key, value["AOValue"]);
  63. continue;
  64. case string s when s.startsWith("FS"):
  65. case string s1 when s1.startsWith("PG"):
  66. case string s2 when s2.startsWith("PS"):
  67. case string s3 when s3.startsWith("VG"):
  68. if (item.Value is IDictionary<string, object> vss)
  69. ValueSensor.Add(item.Key, vss["Value"]);
  70. continue;
  71. case string s when s.StartsWith("Sensor"):
  72. if (item.Value is IDictionary<string, object> status)
  73. StatusSensor.Add(item.Key, status["Value"]);
  74. continue;
  75. case string s when s.StartsWith("MFC"):
  76. if (item.Value is IDictionary<string, object> mfcs)
  77. mfcs.Foreach(t => mfc.Add($"{item.Key}_{t.Key}", t.Value));
  78. continue;
  79. case string s when s.StartsWith("FFU"):
  80. if (item.Value is IDictionary<string, object> ffus)
  81. ffus.Foreach(t => ffu.Add($"{item.Key}_{t.Key}", t.Value));
  82. continue;
  83. case string s when s.StartsWith("GaselineHeater"):
  84. if (item.Value is IDictionary<string, object> gaslines)
  85. gaslines.Foreach(t => gaslineHeater.Add($"{item.Key}_{t.Key}", t.Value));
  86. continue;
  87. case string s when s.StartsWith("Valve"):
  88. if (item.Value is IDictionary<string, object> valves)
  89. valves.Foreach(t => avValue.Add($"{item.Key}_{t.Key}", t.Value));
  90. continue;
  91. default:
  92. continue;
  93. }
  94. }
  95. CreateTable("MFC", mfc);
  96. CreateTable("FFU", ffu);
  97. CreateTable("Valve", avValue);
  98. CreateTable("GaselineHeater", gaslineHeater);
  99. CreateTable("ValueSensor", ValueSensor);
  100. CreateTable("StatusSensor", StatusSensor);
  101. CreateTable("LeakCheck", leakCheck);
  102. CreateTable("AoValue", aoValue);
  103. CreateTable("Recipe", recipe);
  104. return true;
  105. }
  106. public void CreateTableSystem(Dictionary<string, object> data)
  107. {
  108. if (_orm is null)
  109. return;
  110. Dictionary<string, object> systemCollection = [];
  111. Dictionary<string, object> alarmCollection = [];
  112. _orm.CreateTable<Heater>("Heater");
  113. _orm.CreateTable<Stocker>("Stocker");
  114. _orm.CreateTable<LoadPort>("LoadPort");
  115. _orm.CreateTable<FIMS>("FIMS");
  116. foreach (var item in data)
  117. {
  118. if (item.Value is not IDictionary<string, object> values)
  119. continue;
  120. switch (item.Key)
  121. {
  122. case "Boat":
  123. case "CarrierRobot":
  124. case "Scheduler":
  125. case "WaferRobot":
  126. CreateTable(item.Key, values);
  127. continue;
  128. case "System":
  129. if (values is not IDictionary<string, object> systems)
  130. continue;
  131. foreach (var system in systems)
  132. {
  133. switch (system.Key)
  134. {
  135. case string s when s.StartsWith("Heater"):
  136. continue;
  137. case string s when s.StartsWith("AlarmSignalHeater"):
  138. alarmCollection.Add(system.Key, ((IDictionary<string, object>)system.Value)["Value"] ??= false);
  139. continue;
  140. default:
  141. systemCollection.Add(system.Key, system.Value);
  142. break;
  143. }
  144. }
  145. continue;
  146. default:
  147. break;
  148. }
  149. CreateTable("System", systemCollection);
  150. CreateTable("AlarmSignalHeater", alarmCollection);
  151. }
  152. }
  153. public bool InsertDataPM(IDictionary<string, object> data, Guid guid, DateTime time)
  154. {
  155. if (this._orm is null)
  156. return false;
  157. Dictionary<string, object> ValueSensor = [];
  158. Dictionary<string, object> StatusSensor = [];
  159. Dictionary<string, object> leakCheck = [];
  160. Dictionary<string, object> recipe = [];
  161. Dictionary<string, object> aoValue = [];
  162. Dictionary<string, object> mfc = [];
  163. Dictionary<string, object> ffu = [];
  164. Dictionary<string, object> gaslineHeater = [];
  165. Dictionary<string, object> bufferFoup = [];
  166. Dictionary<string, object> avValue = [];
  167. foreach (var item in data)
  168. {
  169. if (item.Value is not IDictionary<string, object> values)
  170. {
  171. switch (item.Key)
  172. {
  173. case string s when s.EndsWith("Enable"):
  174. continue;
  175. case string s when s.StartsWith("LeakCheck"):
  176. leakCheck.Add(item.Key, item.Value);
  177. continue;
  178. default:
  179. recipe.Add(item.Key, item.Value);
  180. continue;
  181. }
  182. }
  183. switch (item.Key)
  184. {
  185. case "APC":
  186. case "APCVATGV":
  187. case "BoatElevatorServo":
  188. case "BoatRotationServo":
  189. case "BufferServo":
  190. case "Shutter":
  191. InsertData(item.Key, time, guid, values);
  192. continue;
  193. case string s when s.startsWith("Trig"):
  194. if (item.Value is IDictionary<string, object> value)
  195. aoValue.Add(item.Key, value["AOValue"]);
  196. continue;
  197. case string s when s.startsWith("FS"):
  198. case string s1 when s1.startsWith("PG"):
  199. case string s2 when s2.startsWith("PS"):
  200. case string s3 when s3.startsWith("VG"):
  201. if (item.Value is IDictionary<string, object> vss)
  202. ValueSensor.Add(item.Key, vss["Value"]);
  203. continue;
  204. //case string s when s.StartsWith("ValveAV"):
  205. // _orm.Insert<AvValue>("AvValue", CreateData<AvValue>(guid, time, item));
  206. // continue;
  207. case string s when s.StartsWith("Sensor"):
  208. if (item.Value is IDictionary<string, object> status)
  209. StatusSensor.Add(item.Key, status["Value"]);
  210. continue;
  211. //case string s when s.StartsWith("FFU"):
  212. // _orm.Insert<FFU>("FFU", CreateData<FFU>(guid, time, item));
  213. // continue;
  214. //case string s when s.StartsWith("GaslineHeater"):
  215. // _orm.Insert<GaslineHeater>("GaslineHeater", CreateData<GaslineHeater>(guid, time, item));
  216. // continue;
  217. case string s when s.StartsWith("MFC"):
  218. if (item.Value is IDictionary<string, object> mfcs)
  219. mfcs.Foreach(t => mfc.Add($"{item.Key}_{t.Key}", t.Value));
  220. //_orm.Insert<MFC>("MFC", CreateData<MFC>(guid, time, item));
  221. continue;
  222. case string s when s.StartsWith("FFU"):
  223. if (item.Value is IDictionary<string, object> ffus)
  224. ffus.Foreach(t => ffu.Add($"{item.Key}_{t.Key}", t.Value));
  225. continue;
  226. case string s when s.StartsWith("GaselineHeater"):
  227. if (item.Value is IDictionary<string, object> gaslines)
  228. gaslines.Foreach(t => gaslineHeater.Add($"{item.Key}_{t.Key}", t.Value));
  229. continue;
  230. case string s when s.StartsWith("Valve"):
  231. if (item.Value is IDictionary<string, object> valves)
  232. valves.Foreach(t => avValue.Add($"{item.Key}_{t.Key}", t.Value));
  233. continue;
  234. default:
  235. continue;
  236. }
  237. }
  238. InsertData("StatusSensor", time, guid, StatusSensor);
  239. InsertData("Recipe", time, guid, recipe);
  240. InsertData("LeakCheck", time, guid, leakCheck);
  241. InsertData("AoValue", time, guid, aoValue);
  242. InsertData("MFC", time, guid, mfc);
  243. InsertData("FFU", time, guid, ffu);
  244. InsertData("Valve", time, guid, avValue);
  245. InsertData("GaselineHeater", time, guid, gaslineHeater);
  246. return true;
  247. }
  248. public void InsertDataSystem(Dictionary<string, object> data, Guid guid, DateTime time)
  249. {
  250. if (_orm is null)
  251. return;
  252. Dictionary<string, object> systemCollection = [];
  253. Dictionary<string, object> alarmCollection = [];
  254. foreach (var item in data)
  255. {
  256. if (item.Value is not IDictionary<string, object> values)
  257. continue;
  258. switch (item.Key)
  259. {
  260. case "Boat":
  261. case "CarrierRobot":
  262. case "Scheduler":
  263. case "WaferRobot":
  264. InsertData(item.Key, time, guid, values);
  265. continue;
  266. case "System":
  267. if (values is not IDictionary<string, object> systems)
  268. continue;
  269. foreach (var system in systems)
  270. {
  271. switch (system.Key)
  272. {
  273. case string s when s.StartsWith("Heater"):
  274. _orm.Insert("Heater", CreateData<Heater>(guid, time, item));
  275. continue;
  276. case string s when s.StartsWith("AlarmSignalHeater"):
  277. alarmCollection.Add(system.Key, ((IDictionary<string, object>)system.Value)["Value"] ??= false);
  278. continue;
  279. default:
  280. systemCollection.Add(system.Key, system.Value);
  281. break;
  282. }
  283. }
  284. continue;
  285. case string s when s.StartsWith("Stocker"):
  286. _orm.Insert("Stocker", CreateData<Stocker>(guid, time, item));
  287. continue;
  288. case string s when s.StartsWith("LP"):
  289. continue;
  290. case string s when s.StartsWith("FIMS"):
  291. continue;
  292. default:
  293. break;
  294. }
  295. }
  296. InsertData("System", time, guid, systemCollection);
  297. InsertData("AlarmSignalHeater", time, guid, alarmCollection);
  298. }
  299. private static T? CreateData<T>(Guid guid, DateTime time, KeyValuePair<string, object> input) where T : class, new()
  300. {
  301. if (input.Value is not IDictionary<string, object> values)
  302. return null;
  303. T source = new();
  304. if (source is not IBasicInfo basicInfo)
  305. return null;
  306. basicInfo.UID = guid;
  307. basicInfo.Time = time;
  308. basicInfo.Name = input.Key;
  309. DicToClass(ref source, values);
  310. return source;
  311. }
  312. private static void DicToClass<T>(ref T input, IDictionary<string, object> source)
  313. {
  314. if (input is null)
  315. return;
  316. foreach (PropertyInfo property in input.GetType().GetProperties())
  317. {
  318. if (!source.TryGetValue(property.Name, out object? value))
  319. continue;
  320. property.SetValue(input, value);
  321. }
  322. }
  323. private void CreateTable(string tableName, IDictionary<string, object> source)
  324. {
  325. if (_orm is null || _orm.Client is null || source is null)
  326. return;
  327. DynamicProperyBuilder builder = _orm.Client.DynamicBuilder().CreateClass(tableName, new SugarTable());
  328. builder.CreateProperty("UID", typeof(Guid), new SugarColumn() { });
  329. builder.CreateProperty("time", typeof(DateTime), new SugarColumn() { });
  330. source.Foreach(t => builder.CreateProperty(t.Key.Replace('.', '_'), t.Value?.GetType(), new SugarColumn() { IsNullable = true }));
  331. _orm.Client.CodeFirst.InitTables(builder.BuilderType());
  332. }
  333. public void CreateTableRaw(string tableName, IDictionary<string, object> source)
  334. {
  335. if (_orm is null || _orm.Client is null || source is null)
  336. return;
  337. DynamicProperyBuilder builder = _orm.Client.DynamicBuilder().CreateClass(tableName, new SugarTable());
  338. //builder.CreateProperty("UID", typeof(Guid), new SugarColumn() { });
  339. //builder.CreateProperty("time", typeof(DateTime), new SugarColumn() { });
  340. source.Foreach(t => builder.CreateProperty(t.Key.Replace('.', '_'), t.Value?.GetType(), new SugarColumn() { IsNullable = true }));
  341. _orm.Client.CodeFirst.InitTables(builder.BuilderType());
  342. }
  343. public void CreateTableRaw(string tableName, IList<DbColumnInfo> source)
  344. {
  345. if (_orm is null || _orm.Client is null || source is null)
  346. return;
  347. DynamicProperyBuilder builder = _orm.Client.DynamicBuilder().CreateClass(tableName, new SugarTable());
  348. foreach (var t in source)
  349. {
  350. Type type = t.DataType switch
  351. {
  352. "float4" => typeof(float),
  353. "float8"=>typeof(double),
  354. "bool" => typeof(bool),
  355. "int4" => typeof(int),
  356. "int8" => typeof(long),
  357. "text" => typeof(char[]),
  358. "varchar"=> typeof(char[]),
  359. "timestamp" => typeof(DateTime),
  360. "time" => typeof(TimeSpan),
  361. _ => typeof(object),
  362. };
  363. if (t.DataType == "text"||t.DataType== "varchar")
  364. builder.CreateProperty(t.DbColumnName.Replace('.', '_'), type, new SugarColumn() { IsNullable = true, Length = 1000 });
  365. else
  366. builder.CreateProperty(t.DbColumnName.Replace('.', '_'), type, new SugarColumn() { IsNullable = true });
  367. //$"\"{t.DbColumnName.Replace('.', '_')}\""
  368. }
  369. //source.Foreach(t => builder.CreateProperty(t.DbColumnName.Replace('.', '_'), t.PropertyType, new SugarColumn() { IsNullable = true }));
  370. _orm.Client.CodeFirst.InitTables(builder.BuilderType());
  371. }
  372. private int InsertData(string tableName, DateTime dateTime, Guid guid, IDictionary<string, object> source)
  373. {
  374. if (_orm is null || _orm.Client is null)
  375. return 0;
  376. Dictionary<string, object> column = new() { ["UID"] = guid, ["time"] = dateTime, };
  377. source.Foreach(t => column.Add(t.Key, t.Value));
  378. return _orm.Client.Insertable(column).AS(tableName).ExecuteCommand();
  379. }
  380. public int InsertDataRaw(string tableName, IDictionary<string, object> source)
  381. {
  382. if (_orm is null || _orm.Client is null)
  383. return 0;
  384. _orm.Client.CurrentConnectionConfig.MoreSettings ??= new();
  385. _orm.Client.CurrentConnectionConfig.MoreSettings.IsCorrectErrorSqlParameterName = false;
  386. Dictionary<string, object> column = [];
  387. //source.Foreach(t => column.Add(t.Key.Replace('.', '_'), t.Value));
  388. foreach (var t in source)
  389. {
  390. if (t.Value is null)
  391. {
  392. continue;
  393. }
  394. if (t.Key.contains("-"))
  395. _orm.Client.CurrentConnectionConfig.MoreSettings.IsCorrectErrorSqlParameterName = true;
  396. column.Add(t.Key.Replace('.', '_'), t.Value);
  397. }
  398. return _orm.Client.Insertable(column).AS(tableName).ExecuteCommand();
  399. }
  400. }