Repository navigation
Expand file tree
/
Copy pathMatplotlibCS.cs
More file actions
201 lines (170 loc) · 7 KB
/
Copy pathMatplotlibCS.cs
File metadata and controls
201 lines (170 loc) · 7 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using NLog;
namespace MatplotlibCS
{
/// <summary>
/// Обёртка над питоновским скриптом построения графиков
/// </summary>
// ReSharper disable once InconsistentNaming
public class MatplotlibCS
{
#region Fields
private ILogger _log;
/// <summary>
/// Пусть к интерпрететору питона
/// </summary>
private string _pythonExePath;
/// <summary>
/// Путь к скрипту dasPlot.py
/// </summary>
private string _dasPlotPyPath;
/// <summary>
/// Путь директории, в которой хранятся временные json-файлы, через которые передаются параметры задачи
/// </summary>
private string _jsonTempPath;
/// <summary>
/// Python web service URL
/// </summary>
private string _serviceUrlCheckAliveMethod = "http://127.0.0.1:57123/";
/// <summary>
/// Python web service URL
/// </summary>
private string _serviceUrlPlotMethod = "http://127.0.0.1:57123/plot";
/// <summary>
/// Kill web service url
/// </summary>
private string _serviceUrlKillMethod = "http://127.0.0.1:57123/kill";
#endregion
#region .ctor
/// <summary>
/// Обёртка над python скриптом, строящим matplotlib графики
/// </summary>
/// <param name="pythonExePath">Путь python.exe</param>
/// <param name="dasPlotPyPath">Путь dasPlot.py</param>
/// <param name="jsonTempPath">Опциональный путь директории, в которой хранятся временные json файлы, через которые передаются данные</param>
public MatplotlibCS(string pythonExePath, string dasPlotPyPath, string jsonTempPath = "c:\\MatplotlibCS")
{
_pythonExePath = pythonExePath;
_dasPlotPyPath = dasPlotPyPath;
_jsonTempPath = jsonTempPath;
_log = LogManager.GetLogger(typeof(MatplotlibCS).Name);
if (!Directory.Exists(_jsonTempPath))
Directory.CreateDirectory(_jsonTempPath);
}
#endregion
#region Public methods
/// <summary>
/// Выполняет задачу построения графиков
/// </summary>
/// <param name="task">Описание задачи</param>
public async Task BuildFigure(Figure task)
{
task.HealthCheck();
try
{
LaunchPythonWebService();
if (!Path.IsPathRooted(task.FileName))
task.FileName = Path.Combine(_jsonTempPath, task.FileName);
JsonConvert.DefaultSettings = (() =>
{
var settings = new JsonSerializerSettings();
settings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
return settings;
});
var serializer = new JsonSerializer() { StringEscapeHandling = StringEscapeHandling.EscapeHtml };
var sb = new StringBuilder();
using (var writer = new StringWriter(sb))
{
serializer.Serialize(writer, task);
}
var json = sb.ToString();
using (var client = new HttpClient())
{
var content = new StringContent(JsonConvert.SerializeObject(json), Encoding.UTF8, "application/json");
var response = await client.PostAsync(_serviceUrlPlotMethod, content);
var responseString = await response.Content.ReadAsStringAsync();
}
}
catch (Exception ex)
{
_log.Fatal($"Error while building figure: {ex.Message}\n{ex.StackTrace}");
}
}
#endregion
#region Private methods
/// <summary>
/// Check if python web service is alive and if no, launches it
/// </summary>
private void LaunchPythonWebService()
{
if (!CheckIfWebServiceIsUpAndRunning())
{
var psi = new ProcessStartInfo(_pythonExePath, _dasPlotPyPath)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
};
_log.Info($"Starting python process {_pythonExePath}, {_dasPlotPyPath}");
var pythonProcess = Process.Start(psi);
// when starting python process, it's better to wait for some time to ensure, that
// web service started
Thread.Sleep(2000);
}
}
/// <summary>
/// Check if python web service is alive
/// </summary>
/// <returns>true if service is up and running</returns>
private bool CheckIfWebServiceIsUpAndRunning()
{
try
{
_log.Info("Check if python web-service is already running");
//Creating the HttpWebRequest
var request = WebRequest.Create(_serviceUrlCheckAliveMethod) as HttpWebRequest;
//Setting the Request method HEAD, you can also use GET too.
request.Method = "GET";
//Getting the Web Response.
var response = request.GetResponse() as HttpWebResponse;
//Returns TRUE if the Status code == 200
response.Close();
_log.Info($"Service response status is {response.StatusCode}");
return (response.StatusCode == HttpStatusCode.OK);
}
catch (Exception ex)
{
_log.Info("Python web-service wasn't found");
//Any exception will returns false.
return false;
}
}
/// <summary>
/// Возвращает новый путь, по которому можно сохранить json задачи
/// </summary>
/// <returns></returns>
private string GetNewJsonPath()
{
if (!Directory.Exists(_jsonTempPath))
{
Directory.CreateDirectory(_jsonTempPath);
}
return Path.Combine(_jsonTempPath, $"task-{DateTime.Now:HHmmssfff}.json");
}
#endregion
}
}