1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using LB_VisionControl;
using LB_VisionProcesses.Cameras;
using OpenCvSharp.Flann;
using System;
using System.Collections.Concurrent;
using System.Windows.Forms;
using System.Xml.Linq;
 
namespace LB_SmartVision.Forms.Pages.CameraPage
{
    public partial class CamerasEditPage : UserControl
    {
        public Action<string, LogInfoType> LogInfo;
 
 
        public CamerasEditPage()
        {
            InitializeComponent();
 
            Name = "CamerasEditPage";
            Text = "相机设置";
            //this.Font = new Font("Microsoft YaHei UI", 16F, FontStyle.Regular, GraphicsUnit.Point, 0);
        }
 
        private void CamerasEditPage_Paint(object sender, PaintEventArgs e)
        {
            uiFlowLayoutPanel1.Controls.Clear();
 
            if (GlobalVar.dicCameras.Count <= 0)
                return;
 
            foreach (var item in GlobalVar.dicCameras)
            {
                string CameraSN = item.Key;
                string CameraBrand = item.Value.Brand.ToString();
 
                if (string.IsNullOrEmpty(CameraSN) || string.IsNullOrEmpty(CameraBrand))
                    return;
 
                UserItem flow = new UserItem(new string[] { "测试", "刷新", "移除" });
                flow.Name = CameraSN;
                flow.Text = $"[{CameraBrand}]";
                if (item.Value.isGrabbing)
                    flow.state = State.Pass;
                else
                    flow.state |= State.Error;
                LoadFlowEvent(flow);
 
                uiFlowLayoutPanel1.Controls.Add(flow);
            }
        }
 
 
        private void uiButton1_Click(object sender, System.EventArgs e)
        {
            CreateCameraForm createCameraForm = new CreateCameraForm();
            createCameraForm.ShowDialog();
 
            if (createCameraForm.bCreate)
            {
                BaseCamera camera = createCameraForm.camera;
                if (GlobalVar.dicCameras.ContainsKey(camera.SN))
                {
                    MessageBox.Show($"相机[{camera.SN}]已存在!", "异常");
                    return;
                }
 
                string CameraSN = camera.SN;//"127.0.0.1"
                string CameraBrand = camera.Brand.ToString();//"1111"
 
                if (string.IsNullOrEmpty(CameraSN) || string.IsNullOrEmpty(CameraBrand))
                    return;
 
                GlobalVar.dicCameras.TryAdd(CameraSN, camera);
                LogInfo?.Invoke(string.Format("添加相机[{0}][{1}]成功", CameraBrand, CameraSN), LogInfoType.PASS);
            }
            this.Invalidate();
        }
 
        private void uiButton2_Click(object sender, System.EventArgs e)
        {
            //清除用Clear方法
            uiFlowLayoutPanel1.Controls.Clear();
            foreach (var camera in GlobalVar.dicCameras.Values)
            {
                camera.CloseDevice();
            }
            GlobalVar.dicCameras.Clear();
        }
 
        private void LoadFlowEvent(UserItem flow)
        {
            //按键1为测试
            flow.MenuItem1ClickedEvent += TestEvent;
            //按键2为刷新
            flow.MenuItem2ClickedEvent += ReconnectEvent;
            //按键3为移除
            flow.MenuItem3ClickedEvent += DeleteEvent;
        }
 
        private void RemoveFlowEvent(UserItem flow)
        {
            //按键1为测试
            flow.MenuItem1ClickedEvent -= TestEvent;
            //按键2为刷新
            flow.MenuItem2ClickedEvent -= ReconnectEvent;
            //按键3为移除
            flow.MenuItem3ClickedEvent += DeleteEvent;
        }
 
        // 测试相机
        private void TestEvent(string Name, string Text)
        {
            for (int i = 0; i < uiFlowLayoutPanel1.Controls.Count; i++)
            {
                UserItem flow = (UserItem)uiFlowLayoutPanel1.Controls[i];
                string name = flow.Name;
                if (name != Name)
                    continue;
 
                if (GlobalVar.dicCameras.ContainsKey(name))
                {
                    CameraForm cameraForm = new CameraForm(GlobalVar.dicCameras[name]);
                    cameraForm.Show();
                }
            }
        }
 
        // 移除相机
        private void DeleteEvent(string Name, string Text)
        {
            for (int i = 0; i < uiFlowLayoutPanel1.Controls.Count; i++)
            {
                UserItem flow = (UserItem)uiFlowLayoutPanel1.Controls[i];
                string name = flow.Name;
                if (name != Name)
                    continue;
 
                RemoveFlowEvent(flow);
                uiFlowLayoutPanel1.Controls.Remove(uiFlowLayoutPanel1.Controls[i]);
                if (GlobalVar.dicCameras.TryRemove(name, out BaseCamera BaseCamera))
                {
                    BaseCamera.Dispose();
                    LogInfo?.Invoke(string.Format("移除相机[{0}]", name), LogInfoType.INFO);
                }
                else
                    LogInfo?.Invoke(string.Format("移除相机[{0}]失败", name), LogInfoType.ERROR);
 
            }
        }
 
        // 重连相机
        private void ReconnectEvent(string Name, string Text)
        {
            for (int i = 0; i < uiFlowLayoutPanel1.Controls.Count; i++)
            {
                UserItem flow = (UserItem)uiFlowLayoutPanel1.Controls[i];
                string name = flow.Name;
                if (name != Name)
                    continue;
 
                GlobalVar.dicCameras[name].CloseDevice();
                Thread.Sleep(50);
                GlobalVar.dicCameras[name].InitDevice(name, this.Handle);
                if (GlobalVar.dicCameras[name].isGrabbing)
                {
                    flow.state = State.Pass;
                    LogInfo?.Invoke(string.Format("重连相机[{0}]成功", name), LogInfoType.PASS);
                }
                else
                {
                    flow.state = State.Error;
                    LogInfo?.Invoke(string.Format("重连相机[{0}]失败", name), LogInfoType.ERROR);
                }
                flow.Refresh();
            }
        }
    }
}