using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Documents; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; using EntityDataSet; using IntelligentControlForsx.Common; using WisdomClient; using WisdomClient.data; namespace IntelligentControlForsx.ChildForms { public partial class PointLineForm : Form { private DateTime startTime; private DateTime endTime; public string tagNameString = ""; private string formTitle = ""; private bool isFirstLoad = true; private int? interval = 300; private IList list = new List(); public PointLineForm(string thingType, string thingId, string uniformCode, string title) { InitializeComponent(); formTitle = title; using (wisdom_cs_entity ctx = new wisdom_cs_entity()) { view_tspoint vPoint = ctx.view_tspoint.Where( s => s.thing_id == thingId && s.thing_type == thingType & s.uniform_code == uniformCode) .FirstOrDefault(); if (vPoint != null) tagNameString = vPoint.id; } startTime = DateTime.Now.AddHours(-2); endTime = DateTime.Now; this.dtStartTime.CustomFormat = "yyyy-MM-dd HH:mm:ss"; this.dtStartTime.Format = DateTimePickerFormat.Custom; this.dtEndTime.CustomFormat = "yyyy-MM-dd HH:mm:ss"; this.dtEndTime.Format = DateTimePickerFormat.Custom; this.dtStartTime.Value = startTime; this.dtEndTime.Value = endTime; if (!string.IsNullOrEmpty(formTitle)) lblTitle.Text = formTitle; else lblTitle.Text = tagNameString; isFirstLoad = false; ckbFive.Checked = true; } public PointLineForm(string pointIdString, string title) { InitializeComponent(); formTitle = title; startTime = DateTime.Now.AddHours(-2); endTime = DateTime.Now; tagNameString = pointIdString; this.dtStartTime.CustomFormat = "yyyy-MM-dd HH:mm:ss"; this.dtStartTime.Format = DateTimePickerFormat.Custom; this.dtEndTime.CustomFormat = "yyyy-MM-dd HH:mm:ss"; this.dtEndTime.Format = DateTimePickerFormat.Custom; this.dtStartTime.Value = startTime; this.dtEndTime.Value = endTime; if (!string.IsNullOrEmpty(formTitle)) lblTitle.Text = formTitle; else lblTitle.Text = tagNameString; isFirstLoad = false; } private void GetData(string tagName, DateTime startTime, DateTime endTime) { DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); TimeSpan sTime = startTime.Subtract(dtStart); TimeSpan eTime = endTime.Subtract(dtStart); long startTimeLong = long.Parse(sTime.Ticks.ToString().Substring(0, sTime.Ticks.ToString().Length - 4)); long endTimeLong = long.Parse(eTime.Ticks.ToString().Substring(0, eTime.Ticks.ToString().Length - 4)); if (interval == null) { List dataList = RestfulClient.findHistoryRawByTagName(tagName, startTimeLong, endTimeLong); list = dataList; } else { List dataList = RestfulClient.findHistorySnapByTagName(tagNameString, startTimeLong, endTimeLong, interval.Value); list = dataList; } } private void BindDataToForm() { this.chartData.Series[0].Points.Clear(); GetData(tagNameString, startTime, endTime); foreach (var td in list) { DateTime dt = CommonMethod.ConvertIntDateTime(td.ts); this.chartData.Series[0].Points.AddXY(dt, td.doubleValue.Value); } } private void dtStartTime_ValueChanged(object sender, EventArgs e) { if (!isFirstLoad) { DateTime sTime = dtStartTime.Value; DateTime eTime = dtEndTime.Value; startTime = sTime; endTime = eTime; Thread t = new Thread(BindFormThread); t.IsBackground = true; t.Start(); } } private void dtEndTime_ValueChanged(object sender, EventArgs e) { if (!isFirstLoad) { DateTime sTime = dtStartTime.Value; DateTime eTime = dtEndTime.Value; startTime = sTime; endTime = eTime; Thread t = new Thread(BindFormThread); t.IsBackground = true; t.Start(); } } private void picClose_Click(object sender, EventArgs e) { this.Close(); } private delegate void FormBindelegate(); public void BindFormThread() { this.chartData.BeginInvoke( (Action)delegate { BindDataToForm(); }); } private void PointLineForm_Load(object sender, EventArgs e) { chartData.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm"; chartData.Series[0].XValueType = ChartValueType.DateTime; chartData.ChartAreas[0].AxisY.IsStartedFromZero = false; Thread t = new Thread(BindFormThread); t.IsBackground = true; t.Start(); } private void ckbOne_Click(object sender, EventArgs e) { bool oneMinute = ckbOne.Checked; ckbFive.Checked = false; ckbTen.Checked = false; ckbAll.Checked = false; ckbOne.Checked = true; if (ckbOne.Checked) { ckbOne.Enabled = false; ckbFive.Enabled = true; ckbTen.Enabled = true; ckbAll.Enabled = true; } interval = 60; if (!isFirstLoad) { DateTime sTime = dtStartTime.Value; DateTime eTime = dtEndTime.Value; startTime = sTime; endTime = eTime; Thread t = new Thread(BindFormThread); t.IsBackground = true; t.Start(); } } private void ckbFive_Click(object sender, EventArgs e) { bool fiveMinute = ckbFive.Checked; ckbOne.Checked = false; ckbTen.Checked = false; ckbAll.Checked = false; ckbFive.Checked = true; if (ckbFive.Checked) { ckbOne.Enabled = true; ckbFive.Enabled = false; ckbTen.Enabled = true; ckbAll.Enabled = true; } interval = 300; if (!isFirstLoad) { DateTime sTime = dtStartTime.Value; DateTime eTime = dtEndTime.Value; startTime = sTime; endTime = eTime; Thread t = new Thread(BindFormThread); t.IsBackground = true; t.Start(); } } private void ckbTen_Click(object sender, EventArgs e) { bool tenMinute = ckbTen.Checked; ckbOne.Checked = false; ckbFive.Checked = false; ckbAll.Checked = false; ckbTen.Checked = true; if (ckbTen.Checked) { ckbOne.Enabled = true; ckbFive.Enabled = true; ckbTen.Enabled = false; ckbAll.Enabled = true; } interval = 600; if (!isFirstLoad) { DateTime sTime = dtStartTime.Value; DateTime eTime = dtEndTime.Value; startTime = sTime; endTime = eTime; Thread t = new Thread(BindFormThread); t.IsBackground = true; t.Start(); } } private void ckbAll_Click(object sender, EventArgs e) { bool all = ckbAll.Checked; ckbOne.Checked = false; ckbFive.Checked = false; ckbAll.Checked = true; ckbTen.Checked = false; if (ckbAll.Checked) { ckbOne.Enabled = true; ckbFive.Enabled = true; ckbTen.Enabled = true; ckbAll.Enabled = false; } interval = null; if (!isFirstLoad) { DateTime sTime = dtStartTime.Value; DateTime eTime = dtEndTime.Value; startTime = sTime; endTime = eTime; Thread t = new Thread(BindFormThread); t.IsBackground = true; t.Start(); } } private void btnExport_Click(object sender, EventArgs e) { string path = SelectPath(); ExportToCSV(path, formTitle, list); MessageBox.Show("导出成功"); } private string SelectPath() //弹出一个选择目录的对话框 { FolderBrowserDialog path = new FolderBrowserDialog(); path.ShowDialog(); return path.SelectedPath; } private void ExportToCSV(string filePath, string fileName, IList dataList) //filePath为保存到本地磁盘的位置 { string filePathOne = System.IO.Path.Combine(filePath, fileName + ".csv"); FileStream fs = System.IO.File.Create(filePathOne); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8); string col_txt = ""; string row_txt = ""; col_txt = "实发有功,时间戳"; sw.WriteLine(col_txt);//写入更改 for (int i = 0; i < dataList.Count; i++) { row_txt = "";//容易漏写,造成数据的重复写入 DateTime dt = CommonMethod.ConvertIntDateTime(dataList[i].ts); row_txt = row_txt + dataList[i].getValue() + "," + dt.ToString("yyyy-MM-dd HH:mm:ss") ; sw.WriteLine(row_txt);//写入更改 } sw.Flush(); //此处必须有此操作 } } }