HubSender.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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> LockMini8(byte mini8Index, bool isLock)
  54. {
  55. return await Send("LockMini8", mini8Index, isLock);
  56. }
  57. public async Task<bool> UpdateDataBaseRange(int range)
  58. {
  59. return await Send("UpdateDataBaseRange", range);
  60. }
  61. public async Task<bool> RequestHardwares()
  62. {
  63. hardwares.Mini8s.Clear();
  64. hardwares.Mini8Channels.Clear();
  65. return await Send("QueryHardwares");
  66. }
  67. public bool FullyReset()
  68. {
  69. return Send("FullyReset").Result;
  70. }
  71. public async Task<bool> RequestConfigFiles()
  72. {
  73. return await Send("QueryConfigFiles");
  74. }
  75. public bool SaveSetValue(Channel channelSet)
  76. {
  77. ChannelData channelData = new();
  78. channelSet.Adapt(channelData);
  79. if (!Invoke("SaveSetValue", channelData, out bool result))
  80. return false;
  81. return result;
  82. }
  83. public bool EnableChannel(byte mini8Index, byte channelIndex, Inhibit inhibit)
  84. {
  85. if (!Invoke("EnableChannel", mini8Index, channelIndex, inhibit, out bool result))
  86. return false;
  87. return result;
  88. }
  89. public bool SwitchChannelMode(byte mini8Index, byte channelIndex, ChannelMode channelMode)
  90. {
  91. if (!Invoke("SwitchChannelMode", mini8Index, channelIndex, channelMode, out bool result))
  92. return false;
  93. return result;
  94. }
  95. public bool ReconnectMini8(byte mini8Index)
  96. {
  97. if (!Invoke("ReconnectMini8", mini8Index, out bool result))
  98. return false;
  99. return result;
  100. }
  101. public bool SelectPID(byte mini8Index, byte channelIndex, ActiveTuneSet activeTuneSet)
  102. {
  103. if (!Invoke("SelectPID", mini8Index, channelIndex, activeTuneSet, out bool result))
  104. return false;
  105. return result;
  106. }
  107. public bool EnableAT(byte mini8Index, byte channelIndex, bool isEnable)
  108. {
  109. if (!Invoke("EnableAT", mini8Index, channelIndex, isEnable, out bool result))
  110. return false;
  111. return result;
  112. }
  113. public bool SendSetValue(Channel channelSet)
  114. {
  115. ChannelData channelData = new();
  116. channelSet.Adapt(channelData);
  117. if (!Invoke("SendSetValue", channelData, out bool success))
  118. return false;
  119. return success;
  120. }
  121. public bool SetConfigFile(string fileName)
  122. {
  123. if (!Invoke("SetConfigFile", fileName, out bool success))
  124. return false;
  125. return success;
  126. }
  127. public bool QueryMini8Data(out TempConfig? config)
  128. {
  129. config = null;
  130. if (!Invoke("QueryMini8Data", out TemperatureConfig? temp) || temp is null)
  131. return false;
  132. config = new();
  133. temp.Adapt(config);
  134. return true;
  135. }
  136. public bool LoadNewFile(string filePath)
  137. {
  138. if (!Invoke("LoadNewFile", filePath, out bool success))
  139. return false;
  140. return success;
  141. }
  142. public bool RemoveFile(string fileName)
  143. {
  144. if (!Invoke("RemoveFile", fileName, out bool success))
  145. return false;
  146. return success;
  147. }
  148. public bool SaveConfig(string fileName, TempConfig tempConfig, bool saveAsNew)
  149. {
  150. TemperatureConfig temperatureConfig = new();
  151. tempConfig.Adapt(temperatureConfig);
  152. if (!Invoke("SaveConfig", fileName, temperatureConfig, saveAsNew, out bool success))
  153. return false;
  154. return success;
  155. }
  156. public bool RequestLogin(string userName, string password, out UserAuthority userAuthority)
  157. {
  158. if (Invoke("TryLogin", userName, password, out userAuthority))
  159. return true;
  160. userAuthority = UserAuthority.Deinded;
  161. return false;
  162. }
  163. public bool RequestShutdown()
  164. {
  165. Invoke("ShutDown", out bool success);
  166. return success;
  167. }
  168. private void UpdateMini8(byte key, Mini8Data value)
  169. {
  170. if (!hardwares.Mini8s.TryGetValue(key, out Mini8Info? mini8) || mini8 is null)
  171. {
  172. mini8 = new(this);
  173. hardwares.Mini8s[key] = mini8;
  174. }
  175. mini8.Status = value.Enable switch
  176. {
  177. true => Mini8Status.Normal,
  178. false => Mini8Status.Disabled
  179. };
  180. value.Adapt(mini8);
  181. }
  182. void IEnableDeviceNotify.EnableDevice(byte mini8Index, bool isEnable)
  183. {
  184. Send("EnableDisableDevice", mini8Index, isEnable).Wait();
  185. }
  186. }