-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppWPF.xaml.cs
277 lines (249 loc) · 12.3 KB
/
AppWPF.xaml.cs
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Resources;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows.Markup;
using System.Collections.ObjectModel;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Text;
using System.Net.Sockets;
using System.IO.Ports;
using Npgsql;
using System.Security.Cryptography;
namespace SCADA
{
/// <summary>
/// Логика взаимодействия для App.xaml
/// </summary>
public class FileAssociation
{
// Associate file extension with progID, description, icon and application
public static void Associate(string extension,
string progID, string description, string icon, string application)
{
Registry.ClassesRoot.CreateSubKey(extension).SetValue("", progID);
if (progID != null && progID.Length > 0)
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(progID))
{
if (description != null)
key.SetValue("", description);
if (icon != null)
key.CreateSubKey("DefaultIcon").SetValue("", ToShortPathName(icon));
if (application != null)
key.CreateSubKey(@"Shell\Open\Command").SetValue("",
ToShortPathName(application) + " \"%1\"");
}
}
// Return true if extension already associated in registry
public static bool IsAssociated(string extension)
{
return (Registry.ClassesRoot.OpenSubKey(extension, false) != null);
}
[DllImport("Kernel32.dll")]
private static extern uint GetShortPathName(string lpszLongPath,
[Out] StringBuilder lpszShortPath, uint cchBuffer);
// Return short path format of a file name
private static string ToShortPathName(string longName)
{
StringBuilder s = new StringBuilder(1000);
uint iSize = (uint)s.Capacity;
uint iRet = GetShortPathName(longName, s, iSize);
return s.ToString();
}
}
public partial class AppWPF : Application
{
public Random GenerateTextName = new Random((int)DateTime.Now.Ticks);
public SerializationSetting ConfigProgramBin;
//Если запускаем через файл проекта нужен первый путь, иначе он изменится
public string StartPath;
private Dictionary<string, Page> collectionPage = new Dictionary<string,Page>();
public Dictionary<string, Page> CollectionPage
{
get { return collectionPage; }
}
private ObservableCollection<EthernetSer> collectionEthernetSers = new ObservableCollection<EthernetSer>();
public ObservableCollection<EthernetSer> CollectionEthernetSers
{
get { return collectionEthernetSers; }
}
private ObservableCollection<ModbusSer> collectionModbusSers = new ObservableCollection<ModbusSer>();
public ObservableCollection<ModbusSer> CollectionModbusSers
{
get { return collectionModbusSers; }
}
private ObservableCollection<ComSer> collectionComSers = new ObservableCollection<ComSer>();
public ObservableCollection<ComSer> CollectionComSers
{
get { return collectionComSers; }
}
private List<TabItemParent> collectionSaveTabItem = new List<TabItemParent>();
public List<TabItemParent> CollectionSaveTabItem
{
get { return collectionSaveTabItem; }
}
private Dictionary<string, TabItemParent> collectionTabItemParent = new Dictionary<string,TabItemParent>();
public Dictionary<string, TabItemParent> CollectionTabItemParent
{
get { return collectionTabItemParent; }
}
public bool IsSaveProject { get; set; }
public Cursor CursorPipe;
public Cursor CursorPipeInvalid;
public Cursor CursorPipe90;
public Cursor CursorPipe90Invalid;
public Cursor CursorText;
public Cursor CursorTextInvalid;
public Cursor CursorEthernet;
public Cursor CursorEthernetInvalid;
public Cursor CursorCom;
public Cursor CursorComInvalid;
public Cursor CursorDisplay;
public Cursor CursorDisplayInvalid;
public Cursor CursorImage;
public Cursor CursorImageInvalid;
public Cursor CursorModbus;
public Cursor CursorModbusInvalid;
public void SaveTabItem(TabItemParent tabItemParent)
{
if (!tabItemParent.isSave)
{
StackPanel panel = (StackPanel)tabItemParent.Header;
Label l = (Label)panel.Children[0];
l.Content = tabItemParent.IS.Name + "*";
tabItemParent.isSave = true;
((AppWPF)Application.Current).CollectionSaveTabItem.Add(tabItemParent);
}
}
public string GenerateID()
{
byte[] bytes = new Byte[16];
RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();
rand.GetBytes(bytes);
Guid myGuid = new Guid(bytes);
return myGuid.ToString().Replace("-", "").Trim();
}
private void StartUp(object sender, StartupEventArgs e)
{
if (!FileAssociation.IsAssociated(".proj"))
{
var location = System.Reflection.Assembly.GetEntryAssembly().Location; // Полный путь с исполняемым екзешником
FileAssociation.Associate(".proj", "ClassID.ProgID", "Проект PrimSCADA", "YourIcon.ico", location);
}
StreamResourceInfo sriPipe = Application.GetResourceStream(new Uri("Images/Pipe.ico", UriKind.Relative));
CursorPipe = new Cursor(sriPipe.Stream);
StreamResourceInfo sriPipeInvalid = Application.GetResourceStream(new Uri("Images/PipeDragInvalid.ico", UriKind.Relative));
CursorPipeInvalid = new Cursor(sriPipeInvalid.Stream);
StreamResourceInfo sriPipe90 = Application.GetResourceStream(new Uri("Images/Pipe90Drag.ico", UriKind.Relative));
CursorPipe90 = new Cursor(sriPipe90.Stream);
StreamResourceInfo sriPipe90Invalid = Application.GetResourceStream(new Uri("Images/Pipe90DragInvalid.ico", UriKind.Relative));
CursorPipe90Invalid = new Cursor(sriPipe90Invalid.Stream);
StreamResourceInfo sriText = Application.GetResourceStream(new Uri("Images/Text.ico", UriKind.Relative));
CursorText = new Cursor(sriText.Stream);
StreamResourceInfo sriTextInvalid = Application.GetResourceStream(new Uri("Images/TextInvalid.ico", UriKind.Relative));
CursorTextInvalid = new Cursor(sriTextInvalid.Stream);
StreamResourceInfo sriEthernet = Application.GetResourceStream(new Uri("Images/Ethernet.ico", UriKind.Relative));
CursorEthernet = new Cursor(sriEthernet.Stream);
StreamResourceInfo sriEthernetInvalid = Application.GetResourceStream(new Uri("Images/EthernetInvalid.ico", UriKind.Relative));
CursorEthernetInvalid = new Cursor(sriEthernetInvalid.Stream);
StreamResourceInfo sriCom = Application.GetResourceStream(new Uri("Images/Com.ico", UriKind.Relative));
CursorCom = new Cursor(sriCom.Stream);
StreamResourceInfo sriComInvalid = Application.GetResourceStream(new Uri("Images/ComInvalid.ico", UriKind.Relative));
CursorComInvalid = new Cursor(sriComInvalid.Stream);
StreamResourceInfo sriDisplay = Application.GetResourceStream(new Uri("Images/Display.ico", UriKind.Relative));
CursorDisplay = new Cursor(sriDisplay.Stream);
StreamResourceInfo sriDisplayInvalid = Application.GetResourceStream(new Uri("Images/DisplayInvalid.ico", UriKind.Relative));
CursorDisplayInvalid = new Cursor(sriDisplayInvalid.Stream);
StreamResourceInfo sriImage = Application.GetResourceStream(new Uri("Images/Image.ico", UriKind.Relative));
CursorImage = new Cursor(sriImage.Stream);
StreamResourceInfo sriImageInvalid = Application.GetResourceStream(new Uri("Images/ImageInvalid.ico", UriKind.Relative));
CursorImageInvalid = new Cursor(sriImageInvalid.Stream);
StreamResourceInfo sriModbus = Application.GetResourceStream(new Uri("Images/Modbus.ico", UriKind.Relative));
CursorModbus = new Cursor(sriModbus.Stream);
StreamResourceInfo sriModbusInvalid = Application.GetResourceStream(new Uri("Images/ModbusInvalid.ico", UriKind.Relative));
CursorModbusInvalid = new Cursor(sriModbusInvalid.Stream);
StartPath = Environment.CurrentDirectory + "\\Config.bin";
if (File.Exists(Environment.CurrentDirectory + "\\Config.bin"))
{
Stream FileStream = File.OpenRead(Environment.CurrentDirectory + "\\Config.bin");
BinaryFormatter deserializer = new BinaryFormatter();
ConfigProgramBin = (SerializationSetting)deserializer.Deserialize(FileStream);
FileStream.Close();
if (string.IsNullOrEmpty(ConfigProgramBin.PathBrowseProject))
{
ConfigProgramBin.PathBrowseProject = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\SCADA\\Projects";
}
}
else
{
Stream FileStream = File.Create(Environment.CurrentDirectory + "\\Config.bin");
BinaryFormatter serializer = new BinaryFormatter();
ConfigProgramBin = new SerializationSetting();
ConfigProgramBin.PathBrowseProject = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\SCADA\\Projects";
serializer.Serialize(FileStream, ConfigProgramBin);
FileStream.Close();
}
}
private void Application_Exit(object sender, ExitEventArgs e)
{
if(((MainWindow)this.MainWindow != null))
{
if(((MainWindow)this.MainWindow).CollectionTCPEthernetObject != null)
{
foreach (EthernetObject client in ((MainWindow)this.MainWindow).CollectionTCPEthernetObject)
{
if (client.TcpClient != null)
{
client.TcpClient.Close();
}
}
}
if (((MainWindow)this.MainWindow).CollectionUDPEthernetObject != null)
{
foreach (EthernetObject client in ((MainWindow)this.MainWindow).CollectionUDPEthernetObject)
{
if (client.UdpClient != null)
{
client.UdpClient.Close();
}
}
}
if (((MainWindow)this.MainWindow).CollectionSerialPortThread != null)
{
foreach (SerialPort serialPort in ((MainWindow)this.MainWindow).CollectionSerialPortThread)
{
if (serialPort != null)
{
serialPort.Close();
}
}
}
if (((MainWindow)this.MainWindow).CollectionSQLObject != null)
{
foreach (SQLObject connection in ((MainWindow)this.MainWindow).CollectionSQLObject)
{
if (connection.SQL != null)
{
connection.SQL.Close();
connection.SQL.Dispose();
}
}
}
}
}
}
}