using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; using laserPTZ.insertForm.service; using laserPTZ.insertForm.service.impl; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using SuperWebSocket; using WebSocketSharp; using System.Web; using laserPTZ.insertForm; using laserPTZ.AI; using System.Media; namespace laserPTZ { public partial class FormMain : Form { private Point mouseOff; // 鼠标移动位置变量 private bool leftFlag; // 标签是否为左键 public static IInsertService insertService = new InsertImpl(); public static FormMain formMain; public static int iActulaWidth; public static int iActulaHeight; public static WebSocketServer ws = null; public static Dictionary<String, Queue<GasCollect>> gasDictionary = new Dictionary<String, Queue<GasCollect>>(); static Dictionary<string, WebSocketSession> sessionDics = new Dictionary<string, WebSocketSession>(); System.Threading.Timer timer1 = null; public FormMain() { InitializeComponent(); } ArrayList m_lDownHandle = new ArrayList(); // 录像 public void take_Record(int index, int m_lUserID, string devName, string monitorId, string alarmTime) { new Thread(new ThreadStart(() => { try { if (!radioMap.ContainsKey(monitorId)) // 第一次录 { radioMap.Add(monitorId, true); } else { if (radioMap[monitorId] == true) return; //该设备正在录,不录了 else radioMap[monitorId] = true; // 开始录 } if (!System.IO.Directory.Exists(ConfigHelper.GetAppConfig("saveImagePath").Trim() + devName)) { System.IO.Directory.CreateDirectory(ConfigHelper.GetAppConfig("saveImagePath").Trim() + devName); } string sVideoFileName = ConfigHelper.GetAppConfig("saveImagePath").Trim() + devName + "\\" + alarmTime.Replace(" ", "-").Replace(":", "-") + ".mp4"; int lChannel = 1; CHCNetSDK.NET_DVR_MakeKeyFrame(m_lUserID, lChannel); Int32 m_lRealHandle = -1; if (CtrlHome.activeIndex_1 == index) { m_lRealHandle = CtrlHome.m_lRealHandle1; } else if (CtrlHome.activeIndex_2 == index) { m_lRealHandle = CtrlHome.m_lRealHandle2; } else if (CtrlHome.activeIndex_3 == index) { m_lRealHandle = CtrlHome.m_lRealHandle3; } else if (CtrlHome.activeIndex_4 == index) { m_lRealHandle = CtrlHome.m_lRealHandle4; } // 四个框里没有,登录 if (m_lRealHandle == -1) { if (CtrlHome.m_lRealHandle5 != -1) { CHCNetSDK.NET_DVR_StopRealPlay(CtrlHome.m_lRealHandle5); } m_lRealHandle = CtrlHome.ctrlHome.preview(CtrlHome.ctrlHome.pictureBox5.Handle, m_lUserID, index); CtrlHome.m_lRealHandle5 = m_lRealHandle; if (m_lRealHandle == -1) { radioMap[monitorId] = false; //解锁 return; } if (!CHCNetSDK.NET_DVR_SaveRealData(m_lRealHandle, sVideoFileName)) { //MessageBox.Show("NET_DVR_SaveRealData 开始录制失败, error code= " + CHCNetSDK.NET_DVR_GetLastError()); radioMap[monitorId] = false; //解锁 CHCNetSDK.NET_DVR_StopRealPlay(CtrlHome.m_lRealHandle5); return; } Thread.Sleep(120000); if (!CHCNetSDK.NET_DVR_StopSaveRealData(m_lRealHandle)) { //MessageBox.Show("NET_DVR_StopSaveRealData 停止录制失败, error code= " + CHCNetSDK.NET_DVR_GetLastError()); radioMap[monitorId] = false; //解锁 return; } CHCNetSDK.NET_DVR_StopRealPlay(CtrlHome.m_lRealHandle5); radioMap[monitorId] = false; //解锁 } else { // 四个框里有 if (!CHCNetSDK.NET_DVR_SaveRealData(m_lRealHandle, sVideoFileName)) { //MessageBox.Show("NET_DVR_SaveRealData failed, error code= " + CHCNetSDK.NET_DVR_GetLastError()); radioMap[monitorId] = false; //解锁 return; } Thread.Sleep(120000); if (!CHCNetSDK.NET_DVR_StopSaveRealData(m_lRealHandle)) { //MessageBox.Show("NET_DVR_StopSaveRealData failed, error code= " + CHCNetSDK.NET_DVR_GetLastError()); radioMap[monitorId] = false; //解锁 return; } radioMap[monitorId] = false; //解锁 } } catch (Exception ex) { } })).Start(); } // 拍照 public void take_Photo(int m_lUserID, string gasValue) { new Thread(new ThreadStart(() => { int lChannel = 1; //通道号 Channel number CHCNetSDK.NET_DVR_JPEGPARA lpJpegPara = new CHCNetSDK.NET_DVR_JPEGPARA(); lpJpegPara.wPicQuality = 0; //图像质量 Image quality lpJpegPara.wPicSize = 0xff; //抓图分辨率 Picture size: 0xff-Auto(使用当前码流分辨率) //抓图分辨率需要设备支持,更多取值请参考SDK文档 //JPEG抓图保存成文件 Capture a JPEG picture string path = ConfigHelper.GetAppConfig("saveImagePath").Trim(); string sJpegPicFileName; sJpegPicFileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".jpg";//图片保存路径和文件名 the path and file name to save if (!CHCNetSDK.NET_DVR_CaptureJPEGPicture(m_lUserID, lChannel, ref lpJpegPara, sJpegPicFileName)) { string str = "NET_DVR_CaptureJPEGPicture failed, error code= "; //MessageBox.Show(str); return; } else { FileStream fs = new FileStream(Application.StartupPath + "\\" + sJpegPicFileName, FileMode.Open); byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); MemoryStream ms = new MemoryStream(bytes); Bitmap bitmap = new Bitmap(ms); Graphics gh = Graphics.FromImage(bitmap); SolidBrush a = new SolidBrush(Color.Red); Font font = new Font("微软雅黑", 20f, FontStyle.Bold); gh.DrawString("CH4: " + gasValue + "ppm·m", font, a, 40, 80); bitmap.Save(ConfigHelper.GetAppConfig("saveImagePath").Trim() + sJpegPicFileName, System.Drawing.Imaging.ImageFormat.Jpeg); bitmap.Dispose(); bitmap = null; File.Delete(Application.StartupPath + "\\" + sJpegPicFileName); } })).Start(); } public static Dictionary<string, bool> radioMap = new Dictionary<string, bool>(); private void FormMain_Shown(object sender, EventArgs e) { var ws = new WebSocket(ConfigHelper.GetAppConfig("websocket").ToString().Trim()); int count = 0; ws.OnMessage += (sender1, e1) => { try { JObject json = (JObject)JsonConvert.DeserializeObject(e1.Data);//或者JObject jo = JObject.Parse(jsonText); // 巡检中到达巡航点 if (e1.Data.Contains("type") && json["type"].ToString() == "presetIndex") { if (json["deviceIp"].ToString() == CtrlRealTime.ctrlRealTime.ip) { CtrlRealTime.ctrlRealTime.txt_preset.Text = json["name"].ToString(); } } else if (e1.Data.Contains("type") && json["type"].ToString() == "cancelAlarm") { for (int j = 0; j < CtrlHome.moniterArr.Count; j++) { string id = CtrlHome.moniterArr[j]["monitorPointId"].ToString(); if(id == json["monitorId"].ToString()) { // 取消红框 if (j == CtrlHome.activeIndex_1) { if (CtrlHome.ctrlHome.panel_video1.BackgroundImage == Properties.Resources.小监控窗口红色) { CtrlHome.ctrlHome.panel_video1.BackgroundImage = Properties.Resources.小监控窗口紫色; } } else if (j == CtrlHome.activeIndex_2) { if (CtrlHome.ctrlHome.panel_video2.BackgroundImage == Properties.Resources.小监控窗口红色) { CtrlHome.ctrlHome.panel_video2.BackgroundImage = Properties.Resources.小监控窗口紫色; } } else if (j == CtrlHome.activeIndex_3) { if (CtrlHome.ctrlHome.panel_video3.BackgroundImage == Properties.Resources.小监控窗口红色) { CtrlHome.ctrlHome.panel_video3.BackgroundImage = Properties.Resources.小监控窗口紫色; } } else if (j == CtrlHome.activeIndex_4) { if (CtrlHome.ctrlHome.panel_video4.BackgroundImage == Properties.Resources.小监控窗口红色) { CtrlHome.ctrlHome.panel_video4.BackgroundImage = Properties.Resources.小监控窗口紫色; } } break; } } try { if (CtrlRealTime.ctrlRealTime.monitorId == json["monitorId"].ToString()) { CtrlRealTime.ctrlRealTime.getDataGridViewData(); } } catch { } } else if (e1.Data.Contains("type") && json["type"].ToString() == "gasAlarm") { //刷新列表 try { if (CtrlRealTime.ctrlRealTime.monitorId == json["monitorId"].ToString()) { CtrlRealTime.ctrlRealTime.getDataGridViewData(); } } finally { //本地报警 if (isSound) { new Thread(new ThreadStart(() => { SoundPlayer play = new SoundPlayer("alarm.wav"); play.PlayLooping(); Thread.Sleep(5000); play.Stop(); })).Start(); } //WavPlayer.Play("C:/Users/wangxitong/Desktop/laserPTZ/Resources/alarm.mp3"); //axWindowsMediaPlayer1.URL = "alarm.mp3"; //axWindowsMediaPlayer1.Ctlcontrols.play(); //拍照 + 录像 if (CtrlHome.userIdMap.ContainsKey(json["monitorId"].ToString())) { for (int i = 0; i < CtrlHome.moniterArr.Count; i++) { if (CtrlHome.moniterArr[i]["monitorPointId"].ToString() == json["monitorId"].ToString()) { CtrlHome.ctrlHome.alarmVideo(i); // 录像(不配备NVR) if (ConfigHelper.GetAppConfig("hasNVR") == "0") { take_Record(i, CtrlHome.userIdMap[json["monitorId"].ToString()], json["monitorName"].ToString(), json["monitorId"].ToString(), json["alarmTime"].ToString()); } } } //take_Photo(CtrlHome.userIdMap[json["monitorId"].ToString()], json["alarmValue"].ToString());// 拍照 } } } else if (e1.Data.Contains("type") && json["type"].ToString() == "strengthAlarm") { //刷新列表 try { if (CtrlRealTime.ctrlRealTime.monitorId == json["monitorId"].ToString()) { CtrlRealTime.ctrlRealTime.getDataGridViewData(); } } finally { //拍照 + 录像 if (CtrlHome.userIdMap.ContainsKey(json["monitorId"].ToString())) { for (int i = 0; i < CtrlHome.moniterArr.Count; i++) { if (CtrlHome.moniterArr[i]["monitorPointId"].ToString() == json["monitorId"].ToString()) { CtrlHome.ctrlHome.alarmVideo(i); // 录像 if (ConfigHelper.GetAppConfig("hasNVR") == "0") { take_Record(i, CtrlHome.userIdMap[json["monitorId"].ToString()], json["monitorName"].ToString(), json["monitorId"].ToString(), json["alarmTime"].ToString()); } } } } } } else { string deviceIp = json["deviceIp"].ToString(); string userId = json["userId"].ToString(); string gasData = json["gasData"].ToString(); string verticalAngle = json["verticalAngle"].ToString(); string horizontalAngle = json["horizontal"].ToString(); string time = json["time"].ToString(); GasCollect gasCollect = new GasCollect(); gasCollect.GasData = gasData; gasCollect.Time = time; addQueue(deviceIp, gasCollect); if (CtrlRealTime.ctrlRealTime == null || CtrlRealTime.ctrlRealTime.ip != deviceIp) return; else { CtrlRealTime.ctrlRealTime.label_gasValue.Text = gasData; CtrlRealTime.ctrlRealTime.halfDashboardUc1.ChangeValue = (float)Convert.ToDouble(gasData); CtrlRealTime.ctrlRealTime.halfDashboardUc1.Refresh(); changeRealTimeChart(gasDictionary[deviceIp]); count++; // 水平角度 CtrlRealTime.ctrlRealTime.coordinateUc1.Value = (float)Convert.ToDouble(horizontalAngle); CtrlRealTime.ctrlRealTime.label_angleD.Text = horizontalAngle; CtrlRealTime.ctrlRealTime.coordinateUc1.Refresh(); // 垂直角度 CtrlRealTime.ctrlRealTime.halfCoordinateUc1.value = (float)Convert.ToDouble(verticalAngle); CtrlRealTime.ctrlRealTime.label_angleP.Text = verticalAngle; CtrlRealTime.ctrlRealTime.halfCoordinateUc1.Refresh(); /*if (count == 2) { count = 0; if (CtrlRealTime.ctrlRealTime.presetAdding) { CtrlRealTime.ctrlRealTime.presetAdding = false; string conStr = "http://" + ConfigHelper.GetAppConfig("server").Trim() + "/HCNet/preset" + "?userId=" + CtrlRealTime.ctrlRealTime.m_lUserID + "&realHandle=" + CtrlRealTime.ctrlRealTime.m_lRealHandle + "&deviceIp=" + CtrlRealTime.ctrlRealTime.ip + "&command=presetAdd&" + "&cruiseRoute=" + CtrlRealTime.ctrlRealTime.txt_lineNo.Text + "&presetIndex=" + CtrlRealTime.ctrlRealTime.txt_presetNo.Text + "&speed=" + CtrlRealTime.ctrlRealTime.txt_speed.Text + "&stopTime=" + CtrlRealTime.ctrlRealTime.txt_presetStop.Text + "&direction=" + CtrlRealTime.ctrlRealTime.label_angleD.Text + "&pitch=" + CtrlRealTime.ctrlRealTime.label_angleP.Text; string result = Http.http(conStr, "GET", null); if (result == "" || ((JObject)JsonConvert.DeserializeObject(result))["success"].ToString() == "False") { MessageBox.Show("添加失败"); } else { MessageBox.Show("添加成功"); } CtrlRealTime.ctrlRealTime.getLineList(); } }*/ } } } catch (Exception ex) { //MessageBox.Show("编辑失败:" + ex, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }; ws.ConnectAsync(); } private void Form1_Load(object sender, EventArgs e) { formMain = this; iActulaWidth = Screen.PrimaryScreen.Bounds.Width; iActulaHeight = Screen.PrimaryScreen.Bounds.Height; //if (iActulaHeight < 768) iActulaHeight = 768; //if (iActulaWidth < 1366) iActulaWidth = 1366; //if (iActulaHeight > 1080) iActulaHeight = 1080; //if (iActulaWidth > 1920) iActulaWidth = 1920; //iActulaWidth = 1366; //iActulaHeight = 768; this.Width = iActulaWidth; this.Height = iActulaHeight; panel_title.Width = iActulaWidth; panel_insert.Width = iActulaWidth; panel_insert.Height = iActulaHeight - 63; panel_insert.Location = new Point(0, 57); this.Invoke(new EventHandler(delegate { panel_home.Visible = false; new Thread(new ThreadStart(() => { insertService.setToFirst(panel_insert); insertService.SetPanelControl(panel_insert, "home", "", -1); //AutoResetEvent autoEvent = new AutoResetEvent(false); //TimerCallback timer1Delegate = new TimerCallback(timerProgress_Tick); //timer1 = new System.Threading.Timer(timer1Delegate, autoEvent, 10000, 10000); })).Start(); })); } private void btn_close_Click(object sender, EventArgs e) { DialogResult dr = MessageBox.Show("确定要退出系统?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (dr == DialogResult.OK) { CHCNetSDK.NET_DVR_Cleanup(); // 新增:释放SDK资源 Close(); //ScoketClient.close(); Environment.Exit(0); } } private void btn_home_Click(object sender, EventArgs e) { btn_home.Visible = false; try { if (CtrlRealTime.rt_lRealHandle >= 0) { CHCNetSDK.NET_DVR_StopRealPlay(CtrlRealTime.rt_lRealHandle); CtrlRealTime.rt_lRealHandle = -1; } } catch { } CtrlRealTime.ctrlRealTime._tranForm.Hide(); insertService.setToFirst(panel_insert); insertService.SetPanelControl(panel_insert, "home", "", -1); CtrlHome.ctrlHome.rePreviewNow(); ScoketClient.sendMsg("close"); ScoketClient.isConnect = false; } private void panel_title_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mouseOff = new Point(-e.X, -e.Y); //得到变量的值 leftFlag = true; //点击左键按下时标注为true; } } private void panel_title_MouseMove(object sender, MouseEventArgs e) { if (leftFlag) { Point mouseSet = Control.MousePosition; mouseSet.Offset(mouseOff.X, mouseOff.Y); //设置移动后的位置 Location = mouseSet; } } private void panel_title_MouseUp(object sender, MouseEventArgs e) { if (leftFlag) { leftFlag = false;//释放鼠标后标注为false; } } private void addQueue(String deviceIp, GasCollect gasCollect) { if (gasDictionary.Keys.Contains(deviceIp)) { gasDictionary[deviceIp].Enqueue(gasCollect); if (gasDictionary[deviceIp].Count > 10) gasDictionary[deviceIp].Dequeue(); } else { Queue<GasCollect> gasQueue = new Queue<GasCollect>(); gasQueue.Enqueue(gasCollect); gasDictionary.Add(deviceIp, gasQueue); } } public static bool rtChart = false; public void changeRealTimeChart(Queue<GasCollect> gasQueue) { new Thread(new ThreadStart(() => { try { if (rtChart) return; else rtChart = true; DataTable tblDatas = new DataTable(); tblDatas.Columns.Add("ID", Type.GetType("System.Int32")); tblDatas.Columns[0].AutoIncrement = true; tblDatas.Columns[0].AutoIncrementSeed = 1; tblDatas.Columns[0].AutoIncrementStep = 1; tblDatas.Columns.Add("concentration", Type.GetType("System.String")); tblDatas.Columns.Add("logTime", Type.GetType("System.String")); foreach (var gasCollect in gasQueue) { tblDatas.Rows.Add(new object[] { null, gasCollect.GasData, gasCollect.Time.Substring(14, 5) }); } /*Series Series1 = new Series(); if (CtrlRealTime.ctrlRealTime.chart1.Series.Count > 7) { CtrlRealTime.ctrlRealTime.chart1.Series.RemoveAt(7); } CtrlRealTime.ctrlRealTime.chart1.Series.Add(Series1); //加入你的chart1*/ CtrlRealTime.ctrlRealTime.chart1.Series["Series1"].Points.Clear();//清除之前的图 Series series = CtrlRealTime.ctrlRealTime.chart1.Series["Series1"]; CtrlRealTime.ctrlRealTime.chart1.DataSource = tblDatas; CtrlRealTime.ctrlRealTime.chart1.Series["Series1"].XValueMember = "logTime"; CtrlRealTime.ctrlRealTime.chart1.Series["Series1"].YValueMembers = "concentration"; rtChart = false; } catch { rtChart = false; } })).Start(); } // 全局声音控制 public bool isSound = false; private void btn_sound_Click(object sender, EventArgs e) { new Thread(new ThreadStart(() => { isSound = !isSound; if (isSound) { // 更换按钮照片 btn_sound.BackgroundImage = Properties.Resources.on; string conStr = "http://" + ConfigHelper.GetAppConfig("server").Trim() + "/HCNet/alarmOperate?command=1"; string result = Http.http(conStr, "POST", null); if (result == "") { } else { MessageBox.Show("已开启声光报警器以及本地报警声音"); } // 写本地文件 ConfigHelper.SetAppConfig("sound", "1"); } else { // 更换按钮照片 btn_sound.BackgroundImage = Properties.Resources.off; // 关闭声光报警器(特殊定制指令) string conStr = "http://" + ConfigHelper.GetAppConfig("server").Trim() + "/HCNet/alarmOperate?command=0"; string result = Http.http(conStr, "POST", null); if (result == "") { } else { MessageBox.Show("已关闭声光报警器以及本地报警声音"); } // 写本地文件 ConfigHelper.SetAppConfig("sound", "0"); } })).Start(); } } public class DataGridViewNew : DataGridView { protected override void OnPaint(PaintEventArgs e) { try { base.OnPaint(e); } catch(Exception ex) { Invalidate(); CtrlRealTime.ctrlRealTime.dataGridView1.DataSource = null; CtrlRealTime.ctrlRealTime.dataGridView1.Invalidate(); CtrlRealTime.ctrlRealTime.getDataGridViewData(); /*if(CtrlRealTime.ctrlRealTime.therod_checked) { CtrlRealTime.ctrlRealTime.list_allPoint.DataSource = null; CtrlRealTime.ctrlRealTime.list_allPoint.Invalidate(); CtrlRealTime.ctrlRealTime.getPointsViewData(); }*/ } } } public class ChartNew : Chart { protected override void OnPaint(PaintEventArgs e) { try { base.OnPaint(e); } catch { Invalidate(); CtrlRealTime.ctrlRealTime.initChart(); } } } }