LineForm.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using System.Windows.Forms.DataVisualization.Charting;
  13. using IntelligentControlForsx.Common;
  14. using WisdomClient;
  15. using WisdomClient.data;
  16. namespace IntelligentControlForsx.MyControls.agc
  17. {
  18. public partial class LineForm : Form
  19. {
  20. private string actualDataPoint;
  21. public string ActualDataPoint
  22. {
  23. set { actualDataPoint = value; }
  24. get { return actualDataPoint; }
  25. }
  26. private string setDataPoint;
  27. public string SetDataPoint
  28. {
  29. set { setDataPoint = value; }
  30. get { return setDataPoint; }
  31. }
  32. private string title;
  33. public string Title
  34. {
  35. set { title = value; }
  36. get { return title; }
  37. }
  38. private IList<TsData> actualDatalist = new List<TsData>();
  39. private IList<TsData> setDataList = new List<TsData>();
  40. private DateTime startTime;
  41. private DateTime endTime;
  42. private bool isFirstLoad = true;
  43. private int? interval = 300;
  44. /// <summary>
  45. /// 窗体初始化方法
  46. /// </summary>
  47. /// <param name="actualDataPoint">实发有功测点</param>
  48. /// <param name="setDataPoint">有功设定测点</param>
  49. /// <param name="title">标题</param>
  50. public LineForm(string actualDataPoint, string setDataPoint, string title)
  51. {
  52. InitializeComponent();
  53. this.ActualDataPoint = actualDataPoint;
  54. this.SetDataPoint = setDataPoint;
  55. this.title = title;
  56. lblTitle.Text = title;
  57. startTime = DateTime.Now.AddHours(-2);
  58. endTime = DateTime.Now;
  59. this.dtStartTime.CustomFormat = "yyyy-MM-dd HH:mm:ss";
  60. this.dtStartTime.Format = DateTimePickerFormat.Custom;
  61. this.dtEndTime.CustomFormat = "yyyy-MM-dd HH:mm:ss";
  62. this.dtEndTime.Format = DateTimePickerFormat.Custom;
  63. this.dtStartTime.Value = startTime;
  64. this.dtEndTime.Value = endTime;
  65. isFirstLoad = false;
  66. ckbFive.Checked = true;
  67. }
  68. private void LineForm_Load(object sender, EventArgs e)
  69. {
  70. chartData.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm";
  71. chartData.Series[0].XValueType = ChartValueType.DateTime;
  72. Thread t = new Thread(BindFormThread);
  73. t.IsBackground = true;
  74. t.Start();
  75. }
  76. public void BindFormThread()
  77. {
  78. this.chartData.BeginInvoke(
  79. (Action)delegate
  80. {
  81. BindDataToForm();
  82. });
  83. }
  84. private void chartData_GetToolTipText(object sender, ToolTipEventArgs e)
  85. {
  86. }
  87. private void BindDataToForm()
  88. {
  89. this.chartData.Series[0].Points.Clear();
  90. this.chartData.Series[1].Points.Clear();
  91. GetData(startTime, endTime);
  92. foreach (var td in actualDatalist)
  93. {
  94. DateTime dt = CommonMethod.ConvertIntDateTime(td.ts);
  95. this.chartData.Series[0].Points.AddXY(dt, td.doubleValue.Value);
  96. }
  97. foreach (var td in setDataList)
  98. {
  99. DateTime dt = CommonMethod.ConvertIntDateTime(td.ts);
  100. this.chartData.Series[1].Points.AddXY(dt, td.doubleValue.Value);
  101. }
  102. }
  103. private void GetData(DateTime startTime, DateTime endTime)
  104. {
  105. DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  106. TimeSpan sTime = startTime.Subtract(dtStart);
  107. TimeSpan eTime = endTime.Subtract(dtStart);
  108. long startTimeLong = long.Parse(sTime.Ticks.ToString().Substring(0, sTime.Ticks.ToString().Length - 4));
  109. long endTimeLong = long.Parse(eTime.Ticks.ToString().Substring(0, eTime.Ticks.ToString().Length - 4));
  110. if (interval == null)
  111. {
  112. List<TsData> actualList = RestfulClient.findHistoryRawByTagName(actualDataPoint, startTimeLong, endTimeLong);
  113. List<TsData> setList = RestfulClient.findHistoryRawByTagName(setDataPoint, startTimeLong, endTimeLong);
  114. actualDatalist = actualList;
  115. setDataList = setList;
  116. }
  117. else
  118. {
  119. List<TsData> actualList = RestfulClient.findHistorySnapByTagName(actualDataPoint, startTimeLong, endTimeLong, interval.Value);
  120. List<TsData> setList = RestfulClient.findHistorySnapByTagName(setDataPoint, startTimeLong, endTimeLong, interval.Value);
  121. actualDatalist = actualList;
  122. setDataList = setList;
  123. }
  124. }
  125. private void picClose_Click(object sender, EventArgs e)
  126. {
  127. this.Close();
  128. }
  129. private void ckbOne_Click(object sender, EventArgs e)
  130. {
  131. bool oneMinute = ckbOne.Checked;
  132. ckbFive.Checked = false;
  133. ckbTen.Checked = false;
  134. ckbAll.Checked = false;
  135. ckbOne.Checked = true;
  136. if (ckbOne.Checked)
  137. {
  138. ckbOne.Enabled = false;
  139. ckbFive.Enabled = true;
  140. ckbTen.Enabled = true;
  141. ckbAll.Enabled = true;
  142. }
  143. interval = 60;
  144. if (!isFirstLoad)
  145. {
  146. DateTime sTime = dtStartTime.Value;
  147. DateTime eTime = dtEndTime.Value;
  148. startTime = sTime;
  149. endTime = eTime;
  150. Thread t = new Thread(BindFormThread);
  151. t.IsBackground = true;
  152. t.Start();
  153. }
  154. }
  155. private void ckbFive_Click(object sender, EventArgs e)
  156. {
  157. bool fiveMinute = ckbFive.Checked;
  158. ckbOne.Checked = false;
  159. ckbTen.Checked = false;
  160. ckbAll.Checked = false;
  161. ckbFive.Checked = true;
  162. if (ckbFive.Checked)
  163. {
  164. ckbOne.Enabled = true;
  165. ckbFive.Enabled = false;
  166. ckbTen.Enabled = true;
  167. ckbAll.Enabled = true;
  168. }
  169. interval = 300;
  170. if (!isFirstLoad)
  171. {
  172. DateTime sTime = dtStartTime.Value;
  173. DateTime eTime = dtEndTime.Value;
  174. startTime = sTime;
  175. endTime = eTime;
  176. Thread t = new Thread(BindFormThread);
  177. t.IsBackground = true;
  178. t.Start();
  179. }
  180. }
  181. private void ckbTen_Click(object sender, EventArgs e)
  182. {
  183. bool tenMinute = ckbTen.Checked;
  184. ckbOne.Checked = false;
  185. ckbFive.Checked = false;
  186. ckbAll.Checked = false;
  187. ckbTen.Checked = true;
  188. if (ckbTen.Checked)
  189. {
  190. ckbOne.Enabled = true;
  191. ckbFive.Enabled = true;
  192. ckbTen.Enabled = false;
  193. ckbAll.Enabled = true;
  194. }
  195. interval = 600;
  196. if (!isFirstLoad)
  197. {
  198. DateTime sTime = dtStartTime.Value;
  199. DateTime eTime = dtEndTime.Value;
  200. startTime = sTime;
  201. endTime = eTime;
  202. Thread t = new Thread(BindFormThread);
  203. t.IsBackground = true;
  204. t.Start();
  205. }
  206. }
  207. private void ckbAll_Click(object sender, EventArgs e)
  208. {
  209. bool all = ckbAll.Checked;
  210. ckbOne.Checked = false;
  211. ckbFive.Checked = false;
  212. ckbAll.Checked = true;
  213. ckbTen.Checked = false;
  214. if (ckbAll.Checked)
  215. {
  216. ckbOne.Enabled = true;
  217. ckbFive.Enabled = true;
  218. ckbTen.Enabled = true;
  219. ckbAll.Enabled = false;
  220. }
  221. interval = null;
  222. if (!isFirstLoad)
  223. {
  224. DateTime sTime = dtStartTime.Value;
  225. DateTime eTime = dtEndTime.Value;
  226. startTime = sTime;
  227. endTime = eTime;
  228. Thread t = new Thread(BindFormThread);
  229. t.IsBackground = true;
  230. t.Start();
  231. }
  232. }
  233. private void dtStartTime_ValueChanged(object sender, EventArgs e)
  234. {
  235. if (!isFirstLoad)
  236. {
  237. DateTime sTime = dtStartTime.Value;
  238. DateTime eTime = dtEndTime.Value;
  239. startTime = sTime;
  240. endTime = eTime;
  241. Thread t = new Thread(BindFormThread);
  242. t.IsBackground = true;
  243. t.Start();
  244. }
  245. }
  246. private void dtEndTime_ValueChanged(object sender, EventArgs e)
  247. {
  248. if (!isFirstLoad)
  249. {
  250. DateTime sTime = dtStartTime.Value;
  251. DateTime eTime = dtEndTime.Value;
  252. startTime = sTime;
  253. endTime = eTime;
  254. Thread t = new Thread(BindFormThread);
  255. t.IsBackground = true;
  256. t.Start();
  257. }
  258. }
  259. private void btnExport_Click(object sender, EventArgs e)
  260. {
  261. string path = SelectPath();
  262. ExportToCSV(path, Title, actualDatalist, setDataList);
  263. MessageBox.Show("导出成功");
  264. }
  265. private string SelectPath() //弹出一个选择目录的对话框
  266. {
  267. FolderBrowserDialog path = new FolderBrowserDialog();
  268. path.ShowDialog();
  269. return path.SelectedPath;
  270. }
  271. private void ExportToCSV(string filePath, string fileName, IList<TsData> actualList, IList<TsData> setList) //filePath为保存到本地磁盘的位置
  272. {
  273. string filePathOne = System.IO.Path.Combine(filePath, fileName + ".csv");
  274. FileStream fs = System.IO.File.Create(filePathOne);
  275. StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
  276. string col_txt = "";
  277. string row_txt = "";
  278. col_txt = "实发有功,时间戳,有功设定,时间戳";
  279. sw.WriteLine(col_txt);//写入更改
  280. int index = 0;
  281. if (actualList.Count > setList.Count)
  282. index = actualList.Count;
  283. else
  284. index = setList.Count;
  285. for (int i = 0; i < index; i++)
  286. {
  287. row_txt = "";//容易漏写,造成数据的重复写入
  288. if (actualList.Count > i)
  289. {
  290. DateTime dt = CommonMethod.ConvertIntDateTime(actualList[i].ts);
  291. row_txt = row_txt + actualList[i].getValue() + "," + dt.ToString("yyyy-MM-dd HH:mm:ss") + ",";
  292. }
  293. else
  294. {
  295. row_txt =row_txt + ", ,";
  296. }
  297. if (setList.Count > i)
  298. {
  299. DateTime dt = CommonMethod.ConvertIntDateTime(setList[i].ts);
  300. row_txt = row_txt + setList[i].getValue() + "," + dt.ToString("yyyy-MM-dd HH:mm:ss") + ",";
  301. }
  302. else
  303. {
  304. row_txt = row_txt +", ,";
  305. }
  306. sw.WriteLine(row_txt);//写入更改
  307. }
  308. sw.Flush(); //此处必须有此操作
  309. }
  310. }
  311. }