HubSender.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. namespace MinicsUI.Connector;
  2. public class HubSender(HubReceiver receiver, Hardwares hardwares) : SenderBase, IEnableDeviceNotify
  3. {
  4. public bool Initialize(string ip, int port, string hub, int retry = 40)
  5. {
  6. StartConsole();
  7. if (_hubConnection is not null)
  8. return false;
  9. HubConnection temp = new HubConnectionBuilder()
  10. .WithUrl($"http://{ip}:{port}/{hub}")
  11. .WithAutomaticReconnect()
  12. .Build();
  13. temp.On<byte, Mini8Data>("UpdateMini8", UpdateMini8);
  14. temp.On<byte, byte, ChannelData>("UpdateSingleChannel", receiver.UpdateSingleChannel);
  15. temp.On<byte, List<ChannelData>>("UpdateChannel", receiver.UpdateChannels);
  16. temp.On<TemperatureConfig>("UpdateFiles", receiver.UpdateFiles);
  17. temp.On("ClearFiles", receiver.ClearFiles);
  18. temp.On<byte, Mini8Address>("UpdateAddress", receiver.UpdateAddress);
  19. temp.On<TemperatureConfig>("CurrentFile", receiver.CurrentFile);
  20. temp.On<byte, byte, float>("AlarmNotify", receiver.AlarmNotify);
  21. temp.On<byte, byte>("AlarmTcBrockenNotify", receiver.AlarmTcBrockenNotify);
  22. temp.On<byte, bool>("Mini8Connect", receiver.Mini8Connect);
  23. temp.On<int, DateTime>("UpdateDataBaseInfo", receiver.UpdateDataBaseInfo);
  24. temp.On<byte, byte, ChannelData>("ChannelDataUpdate", receiver.ChannelDataUpdate);
  25. temp.ServerTimeout = TimeSpan.FromSeconds(5.0);
  26. for (int i = 1; i <= retry; i++)
  27. {
  28. try
  29. {
  30. temp.StartAsync().Wait();
  31. _hubConnection = temp;
  32. break;
  33. }
  34. catch
  35. {
  36. if (i == retry)
  37. return false;
  38. Thread.Sleep(1000);
  39. }
  40. }
  41. return true;
  42. }
  43. public bool SwitchDataBase(out string? dbName)
  44. {
  45. if (!Invoke("SwitchDataBase", out dbName) || dbName is null)
  46. return false;
  47. return true;
  48. }
  49. public async Task<bool> ClearDataBase()
  50. {
  51. return await Send("ClearDataBase");
  52. }
  53. public async Task<bool> UpdateDataBaseRange(int range)
  54. {
  55. return await Send("UpdateDataBaseRange", range);
  56. }
  57. public async Task<bool> RequestHardwares()
  58. {
  59. hardwares.Mini8s.Clear();
  60. hardwares.Mini8Channels.Clear();
  61. return await Send("QueryHardwares");
  62. }
  63. public bool FullyReset()
  64. {
  65. return Send("FullyReset").Result;
  66. }
  67. public async Task<bool> RequestConfigFiles()
  68. {
  69. return await Send("QueryConfigFiles");
  70. }
  71. public bool SaveSetValue(Channel channelSet)
  72. {
  73. ChannelData channelData = new();
  74. channelSet.Adapt(channelData);
  75. if (!Invoke("SaveSetValue", channelData, out bool result))
  76. return false;
  77. return result;
  78. }
  79. public bool EnableChannel(byte mini8Index, byte channelIndex, Inhibit inhibit)
  80. {
  81. if (!Invoke("EnableChannel", mini8Index, channelIndex, inhibit, out bool result))
  82. return false;
  83. return result;
  84. }
  85. public bool SwitchChannelMode(byte mini8Index, byte channelIndex, ChannelMode channelMode)
  86. {
  87. if (!Invoke("SwitchChannelMode", mini8Index, channelIndex, channelMode, out bool result))
  88. return false;
  89. return result;
  90. }
  91. public bool ReconnectMini8(byte mini8Index)
  92. {
  93. if (!Invoke("ReconnectMini8", mini8Index, out bool result))
  94. return false;
  95. return result;
  96. }
  97. public bool SelectPID(byte mini8Index, byte channelIndex, ActiveTuneSet activeTuneSet)
  98. {
  99. if (!Invoke("SelectPID", mini8Index, channelIndex, activeTuneSet, out bool result))
  100. return false;
  101. return result;
  102. }
  103. public bool EnableAT(byte mini8Index, byte channelIndex, bool isEnable)
  104. {
  105. if (!Invoke("EnableAT", mini8Index, channelIndex, isEnable, out bool result))
  106. return false;
  107. return result;
  108. }
  109. public bool SendSetValue(Channel channelSet)
  110. {
  111. ChannelData channelData = new();
  112. channelSet.Adapt(channelData);
  113. if (!Invoke("SendSetValue", channelData, out bool success))
  114. return false;
  115. return success;
  116. }
  117. public bool SetConfigFile(string fileName)
  118. {
  119. if (!Invoke("SetConfigFile", fileName, out bool success))
  120. return false;
  121. return success;
  122. }
  123. public bool QueryMini8Data(out TempConfig? config)
  124. {
  125. config = null;
  126. if (!Invoke("QueryMini8Data", out TemperatureConfig? temp) || temp is null)
  127. return false;
  128. config = new();
  129. temp.Adapt(config);
  130. return true;
  131. }
  132. public bool LoadNewFile(string filePath)
  133. {
  134. if (!Invoke("LoadNewFile", filePath, out bool success))
  135. return false;
  136. return success;
  137. }
  138. public bool RemoveFile(string fileName)
  139. {
  140. if (!Invoke("RemoveFile", fileName, out bool success))
  141. return false;
  142. return success;
  143. }
  144. public bool SaveConfig(string fileName, TempConfig tempConfig, bool saveAsNew)
  145. {
  146. TemperatureConfig temperatureConfig = new();
  147. tempConfig.Adapt(temperatureConfig);
  148. if (!Invoke("SaveConfig", fileName, temperatureConfig, saveAsNew, out bool success))
  149. return false;
  150. return success;
  151. }
  152. public bool RequestLogin(string userName, string password, out UserAuthority userAuthority)
  153. {
  154. if (Invoke("TryLogin", userName, password, out userAuthority))
  155. return true;
  156. userAuthority = UserAuthority.Deinded;
  157. return false;
  158. }
  159. public bool RequestShutdown()
  160. {
  161. Invoke("ShutDown", out bool success);
  162. return success;
  163. }
  164. private void UpdateMini8(byte key, Mini8Data value)
  165. {
  166. if (!hardwares.Mini8s.TryGetValue(key, out Mini8Info? mini8) || mini8 is null)
  167. {
  168. mini8 = new(this);
  169. hardwares.Mini8s[key] = mini8;
  170. }
  171. mini8.Status = value.Enable switch
  172. {
  173. true => Mini8Status.Normal,
  174. false => Mini8Status.Disabled
  175. };
  176. value.Adapt(mini8);
  177. }
  178. void IEnableDeviceNotify.EnableDevice(byte mini8Index, bool isEnable)
  179. {
  180. Send("EnableDisableDevice", mini8Index, isEnable).Wait();
  181. }
  182. }