PointLineForm.cs 11 KB

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