123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- 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<TsData> list = new List<TsData>();
- 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<TsData> dataList = RestfulClient.findHistoryRawByTagName(tagName, startTimeLong, endTimeLong);
- list = dataList;
- }
- else
- {
- List<TsData> 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<TsData> 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(); //此处必须有此操作
- }
- }
- }
|