2021-05-28 20:53:14 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using NLog;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using DataClient.Struct;
|
|
|
|
|
using System.Net;
|
2021-07-07 16:36:28 +05:00
|
|
|
|
using System.Threading.Tasks;
|
2021-05-28 20:53:14 +05:00
|
|
|
|
|
|
|
|
|
namespace DataClient
|
|
|
|
|
{
|
2021-07-08 16:16:35 +05:00
|
|
|
|
/// <summary>Класс для связи с СТП. Содержит все инструменты, для получения данных.</summary>
|
2021-05-28 20:53:14 +05:00
|
|
|
|
public class STPClient
|
|
|
|
|
{
|
2021-07-08 16:16:35 +05:00
|
|
|
|
//ЛОГИРОВАНИЕ
|
|
|
|
|
private Logger log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
|
|
//КОНФИГУРАЦИЯ
|
|
|
|
|
private string confDir = Path.Combine(Directory.GetCurrentDirectory(), "Config");
|
|
|
|
|
private Dictionary<int, ConfAnalog[]> confAnalog = new Dictionary<int, ConfAnalog[]>();
|
2021-07-07 16:36:28 +05:00
|
|
|
|
private List<(int num, string name)> vdpList = new List<(int num, string name)>();
|
2021-07-08 16:16:35 +05:00
|
|
|
|
/// <summary>Чтение конфигурационнного файла и установка параметров по умолчанию.</summary>
|
|
|
|
|
private void Config()
|
|
|
|
|
{
|
|
|
|
|
string jsonString;
|
|
|
|
|
var confFile = Path.Combine(confDir, "config.json");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
jsonString = File.ReadAllText(confFile);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
log.Warn(e, "Can't read config file from " + confDir + ".");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var conf = (JObject)JsonConvert.DeserializeObject(jsonString);
|
|
|
|
|
if (!conf.HasValues && !conf["servers"].HasValues && conf["servers"].Type != JTokenType.Array)
|
|
|
|
|
{
|
|
|
|
|
log.Warn("Doesn't exist \"servers\" object in config file.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var count = -1;
|
|
|
|
|
foreach (var s in conf["servers"])
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
var name = (s["name"].Type == JTokenType.String) ? (string)s["name"] : "default";
|
|
|
|
|
var ip = (s["ip"].Type == JTokenType.String) ? (string)s["ip"] : "";
|
|
|
|
|
var port = (s["port"].Type == JTokenType.Integer) ? (int)s["port"] : -1;
|
|
|
|
|
var dir = (s["dir"].Type == JTokenType.String) ? (string)s["dir"] : "";
|
|
|
|
|
var res = false;
|
|
|
|
|
var idx = _servers.FindIndex(x => x.name == name);
|
|
|
|
|
if (idx == -1)
|
|
|
|
|
res = AddServer(name, ip, port, dir);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_servers.RemoveAt(idx);
|
|
|
|
|
res = AddServer(name, ip, port, dir);
|
|
|
|
|
}
|
|
|
|
|
if (!res)
|
|
|
|
|
log.Trace("Can't add or change server #" + count + " from conf file.");
|
|
|
|
|
}
|
|
|
|
|
//init vdp
|
|
|
|
|
for (var i = 0; i < 49; i++)
|
|
|
|
|
vdpList.Add(new(i, i == 0 ? "Общие" : "Печь " + i.ToString("D2")));
|
|
|
|
|
for (var i = 91; i < 95; i++)
|
|
|
|
|
vdpList.Add((i, "Печь " + i.ToString("D2")));
|
|
|
|
|
//init analog
|
|
|
|
|
var tmpvdp = new List<int>();
|
|
|
|
|
foreach (var v in vdpList) tmpvdp.Add(v.num);
|
|
|
|
|
confAnalog = Configuration.InitAnalogList(Path.Combine(confDir, "analog"), tmpvdp.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//РАБОТА С СЕРВЕРОМ
|
|
|
|
|
private List<server> _servers = new List<server>();
|
|
|
|
|
private int _useServer = -1;
|
2021-05-28 20:53:14 +05:00
|
|
|
|
/// <summary>Номер сервера, используемого по умолчанию.</summary>
|
|
|
|
|
public int UseServer
|
2021-07-08 16:16:35 +05:00
|
|
|
|
{
|
|
|
|
|
get { return _useServer; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_useServer =
|
|
|
|
|
(_servers.Count == 0) ? -1 :
|
2021-05-28 20:53:14 +05:00
|
|
|
|
(value < 0) ? 0 :
|
2021-07-08 16:16:35 +05:00
|
|
|
|
(value >= _servers.Count) ? _useServer = _servers.Count - 1 :
|
2021-05-28 20:53:14 +05:00
|
|
|
|
value;
|
|
|
|
|
}
|
2021-07-08 16:16:35 +05:00
|
|
|
|
}
|
2021-05-28 20:53:14 +05:00
|
|
|
|
/// <summary>Массив серверов, инициализированных в экземпляре класса.</summary>
|
2021-07-08 16:16:35 +05:00
|
|
|
|
public server[] ListServers
|
2021-05-28 20:53:14 +05:00
|
|
|
|
{
|
2021-07-08 16:16:35 +05:00
|
|
|
|
get { return _servers.ToArray(); }
|
2021-05-28 20:53:14 +05:00
|
|
|
|
}
|
2021-07-08 16:16:35 +05:00
|
|
|
|
/// <summary> Текущий рабочий сервер </summary>
|
|
|
|
|
public server? Server
|
|
|
|
|
{
|
|
|
|
|
get { return (_useServer == -1) ? null : _servers[_useServer]; }
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Проверка параметров сервера.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ip">Ip адрес сервера СТП.</param>
|
|
|
|
|
/// <param name="port">Порт сервера СТП.</param>
|
|
|
|
|
/// <param name="dir">Путь к архивным данным.</param>
|
|
|
|
|
/// <returns>Результат проверки.</returns>
|
2021-05-28 20:53:14 +05:00
|
|
|
|
private bool CheckDataServer(string ip, int port, string dir)
|
|
|
|
|
{
|
|
|
|
|
if (!IPAddress.TryParse(ip, out _))
|
|
|
|
|
{
|
|
|
|
|
log.Warn("Ip address incorrect: " + ip + " .");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (port < 1 && port > 65535)
|
|
|
|
|
{
|
|
|
|
|
log.Warn("Port incorrect: " + port.ToString() + ".");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!Directory.Exists(dir))
|
|
|
|
|
{
|
|
|
|
|
log.Warn("Directory incorrect or not exist: " + dir + " .");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>Добавление сервера в список серверов.</summary>
|
|
|
|
|
/// <param name="name">Название сервера</param>
|
|
|
|
|
/// <param name="ip">IP адресс сервера.</param>
|
|
|
|
|
/// <param name="port">Порт сервера.</param>
|
|
|
|
|
/// <param name="dir">Путь к архиву.</param>
|
|
|
|
|
/// <returns>Результат выполнения функции.</returns>
|
|
|
|
|
public bool AddServer(string name, string ip, int port, string dir)
|
|
|
|
|
{
|
|
|
|
|
if (!CheckDataServer(ip, port, dir))
|
|
|
|
|
{
|
|
|
|
|
log.Warn("Server not added: Data incorrect.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(name))
|
|
|
|
|
{
|
|
|
|
|
log.Warn("Server not added: Incorrect name server.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-07-08 16:16:35 +05:00
|
|
|
|
if (_servers.FindIndex(x => x.name == name) != -1)
|
2021-05-28 20:53:14 +05:00
|
|
|
|
{
|
|
|
|
|
log.Warn("Server not added: Server with this name exist.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-07-08 16:16:35 +05:00
|
|
|
|
_servers.Add(new server(name, ip, port, dir));
|
|
|
|
|
_useServer = 0;
|
2021-05-28 20:53:14 +05:00
|
|
|
|
log.Trace("Server (name=" + name + ";ip=" + ip + ";port=" + port + ";dir=" + dir + ") added.");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>Удаление сервера из списка серверов по его номеру.</summary>
|
|
|
|
|
/// <param name="id">Номер сервера в списке серверов.</param>
|
|
|
|
|
/// <returns>Результат выполнения функции.</returns>
|
2021-07-08 16:16:35 +05:00
|
|
|
|
public bool RemoveServer(int id)
|
|
|
|
|
{
|
|
|
|
|
if (_servers.Count == 0 || id >= _servers.Count || id < 0)
|
2021-05-28 20:53:14 +05:00
|
|
|
|
{
|
|
|
|
|
log.Warn("Server not removed: Id incorrect or list server are empty.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-07-08 16:16:35 +05:00
|
|
|
|
log.Trace("Server id=" + id.ToString() + ";name=" + _servers[id].name + ";ip=" + _servers[id].ip + ";port=" + _servers[id].port.ToString() + ";dir=" + _servers[id].dir + "; removed.");
|
|
|
|
|
_servers.RemoveAt(id);
|
2021-05-28 20:53:14 +05:00
|
|
|
|
UseServer = 0;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-07-08 16:16:35 +05:00
|
|
|
|
//#################
|
|
|
|
|
|
|
|
|
|
//Переменные
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Геттеры и сеттеры
|
|
|
|
|
|
|
|
|
|
//Функции
|
|
|
|
|
|
|
|
|
|
|
2021-05-28 20:53:14 +05:00
|
|
|
|
/// <summary>Создание экземпляра класса.</summary>
|
|
|
|
|
public STPClient()
|
|
|
|
|
{
|
|
|
|
|
log.Trace("Create instance class.");
|
|
|
|
|
Config();
|
|
|
|
|
}
|
|
|
|
|
/// <summary>Создание экземпляра класса с указанием конфигурационного файла.</summary>
|
|
|
|
|
/// <param name="confDir">Путь к файлу конфигурации.</param>
|
|
|
|
|
public STPClient(string confDir)
|
|
|
|
|
{
|
|
|
|
|
log.Trace("Create instance class with dir:" + confDir + ".");
|
|
|
|
|
this.confDir = confDir;
|
|
|
|
|
Config();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-08 16:16:35 +05:00
|
|
|
|
public TechCycle[] GetTechCyclesList(int vdp, DateTime tS, DateTime tE)
|
2021-06-10 16:56:05 +05:00
|
|
|
|
{
|
|
|
|
|
//Use File with ID = 3
|
|
|
|
|
var res = new List<TechCycle>();
|
|
|
|
|
return res.ToArray();
|
|
|
|
|
}
|
2021-07-07 16:36:28 +05:00
|
|
|
|
|
2021-07-08 16:16:35 +05:00
|
|
|
|
public (ConfAnalog conf, Analog[] list)[] GetAnalogsList(int vdp, DateTime tS, DateTime tE, int[] idA = null)
|
|
|
|
|
{
|
|
|
|
|
return Array.Empty<(ConfAnalog conf, Analog[] list)>();
|
2021-07-07 16:36:28 +05:00
|
|
|
|
}
|
2021-05-28 20:53:14 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|