MainForm.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using Hik.Api;
  2. using Newtonsoft.Json.Linq;
  3. using S7.Net;
  4. using System.Threading.Channels;
  5. using System.Windows.Forms;
  6. namespace xicheji
  7. {
  8. public partial class MainForm : Form
  9. {
  10. private readonly Config config;
  11. private readonly List<WashRecord> washRecords = new();
  12. private DateTime lastReceiveTime = DateTime.MinValue;
  13. private Plc? plc = null;
  14. private HikApi? hikApi = null;
  15. public MainForm()
  16. {
  17. InitializeComponent();
  18. config = ConfigHelper.GetConfig();
  19. }
  20. private void MainForm_Load(object sender, EventArgs e)
  21. {
  22. try
  23. {
  24. //测试数据
  25. washRecords.Add(new WashRecord()
  26. {
  27. CarNum = "京123456",
  28. StartTime = new DateTime(2024, 6, 1, 12, 00, 00),
  29. EndTime = new DateTime(2024, 6, 1, 12, 30, 00),
  30. WashTime = 15,
  31. DryTime = 15,
  32. TotalElectricity = 10,
  33. TotalWater = 2,
  34. WaterPressure = 6,
  35. });
  36. dataGridView1.DataSource = washRecords;
  37. ConnectPLC();
  38. //ConnectCamera();
  39. receivePLCTimer.Enabled = true;
  40. uploadTimer.Enabled = true;
  41. }
  42. catch (Exception ex)
  43. {
  44. MessageBox.Show(ex.Message + ex.StackTrace);
  45. }
  46. }
  47. private void btnConnectCamera_Click(object sender, EventArgs e)
  48. {
  49. try
  50. {
  51. ConnectCamera();
  52. }
  53. catch (Exception ex)
  54. {
  55. MessageBox.Show(ex.Message + ex.StackTrace);
  56. }
  57. }
  58. private void nowTimer_Tick(object sender, EventArgs e)
  59. {
  60. try
  61. {
  62. var now = DateTime.Now;
  63. lblTime.Text = $"{now.Month}月 {now.Day}日 {now.Hour}:{now:mm}:{now:ss}";
  64. }
  65. catch { }
  66. }
  67. private void receivePLCTimer_Tick(object sender, EventArgs e)
  68. {
  69. try
  70. {
  71. if (plc == null || !plc.IsConnected)
  72. {
  73. receivePLCTimer.Enabled = false;
  74. return;
  75. }
  76. ReadPLCData();
  77. }
  78. catch (Exception ex)
  79. {
  80. MessageBox.Show(ex.Message + ex.StackTrace);
  81. }
  82. }
  83. private void ReadPLCData()
  84. {
  85. lblWashCount.Text = ReadPLCString("洗车次数");
  86. return;
  87. var time = new DateTime(
  88. DateTime.Now.Year,
  89. ReadPLCInt("当前月"),
  90. ReadPLCInt("当前日"),
  91. ReadPLCInt("当前时"),
  92. ReadPLCInt("当前分"),
  93. ReadPLCInt("当前秒"));
  94. if (time <= lastReceiveTime)
  95. {
  96. //已经读过这个时间的了
  97. return;
  98. }
  99. lastReceiveTime = time;
  100. ReadError();
  101. ReadStatus();
  102. lblWashCount.Text = ReadPLCString("洗车次数");
  103. lblTotalWater.Text = ReadPLCString("累计用水量");
  104. lblTotalElectricity.Text = ReadPLCString("累计用电量");
  105. //.Text = ReadPLCStringValue("当前流量");
  106. lblCurrentElectricity.Text = ReadPLCString("当前功率");
  107. lblCurrentWaterP.Text = ReadPLCString("当前压力");
  108. }
  109. private void ReadStatus()
  110. {
  111. lblStatus.Text = string.Empty;
  112. var statuses = new List<string>() {
  113. "洗车中", "清洗中", "沥水中",
  114. "出车中", "风干中"};
  115. foreach (var status in statuses)
  116. {
  117. if (ReadPLCBool(status))
  118. {
  119. lblStatus.Text = status;
  120. break;
  121. }
  122. }
  123. }
  124. private void ReadError()
  125. {
  126. lblError.Text = string.Empty;
  127. var errors = new List<string>() {
  128. "急停", "冲洗泵故障", "反洗泵故障",
  129. "清淤泵故障", "伴热故障", "风刀故障" };
  130. foreach (var error in errors)
  131. {
  132. if (ReadPLCBool(error))
  133. {
  134. lblError.Text = error;
  135. break;
  136. }
  137. }
  138. }
  139. private string ReadPLCString(string name)
  140. {
  141. var variable = ConfigHelper.FindVariable(name);
  142. if (plc == null || variable == null)
  143. return string.Empty;
  144. var value = plc.Read(variable.Key);
  145. return $"{value} {variable.Desc}";
  146. }
  147. private int ReadPLCInt(string name)
  148. {
  149. var variable = ConfigHelper.FindVariable(name);
  150. if (plc == null || variable == null)
  151. return 0;
  152. var value = plc.Read(variable.Key);
  153. return int.Parse(value?.ToString() ?? "0");
  154. }
  155. private bool ReadPLCBool(string name)
  156. {
  157. var variable = ConfigHelper.FindVariable(name);
  158. if (plc == null || variable == null)
  159. return false;
  160. var value = plc.Read(variable.Key);
  161. return value?.ToString()?.ToLower() == "true";
  162. }
  163. private void uploadTimer_Tick(object sender, EventArgs e)
  164. {
  165. }
  166. private void btnSetting_Click(object sender, EventArgs e)
  167. {
  168. try
  169. {
  170. var settingForm = new SettingForm();
  171. if (settingForm.ShowDialog() == DialogResult.OK)
  172. {
  173. ConnectPLC();
  174. ConnectCamera();
  175. receivePLCTimer.Enabled = true;
  176. uploadTimer.Enabled = true;
  177. }
  178. }
  179. catch { }
  180. }
  181. private void ConnectPLC()
  182. {
  183. plc = new Plc(config.PLCType, config.PLCIP, int.Parse(config.PLCPort), 0, 1);
  184. plc.Open();
  185. if (plc == null || !plc.IsConnected)
  186. {
  187. MessageBox.Show("PLC连接失败!");
  188. return;
  189. }
  190. }
  191. private void ConnectCamera()
  192. {
  193. try
  194. {
  195. hikApi = HikApi.Login(config.CameraIP, int.Parse(config.CameraPort),
  196. config.CameraUserName, config.CameraPassword) as HikApi;
  197. if (hikApi == null || !hikApi.Connected)
  198. {
  199. MessageBox.Show("摄像头连接失败!");
  200. return;
  201. }
  202. hikApi.PlaybackService.StartPlayBack(hikApi.DefaultIpChannel, pictureBox1.Handle);
  203. }
  204. catch (Exception ex)
  205. {
  206. MessageBox.Show(ex.Message + ex.StackTrace);
  207. }
  208. }
  209. public void UpdateCarInfo(DateTime now)
  210. {
  211. this.Invoke(() =>
  212. {
  213. this.Text = now.ToString();
  214. });
  215. }
  216. }
  217. }