using NEIntelligentControl2.Models.Messages;
using NEIntelligentControl2.Models.Station;
using SVGViewer.Models.Info;
using SVGViewer.Models.Svg;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
namespace NEIntelligentControl2.Service.Station
{
///
/// 场站信息维护
///
public class StationManager
{
private List _StationInfos; // 场站信息
private WEBHelper _WEBHelper;
private string _Url;
private Dictionary _Stations;
private List _BoostStationInfos;
private List _BoostStationShapes;
private Dictionary _CustomizeShapesDic;
public StationManager(WEBHelper web)
{
_WEBHelper = web;
try
{
#if (DEBUG)
_Url = ConfigurationManager.AppSettings["ServicePathDebug"];
#else
_Url = ConfigurationManager.AppSettings["ServicePath"];
#endif
}
catch (Exception ex)
{
Console.WriteLine("读取配置文件[ServicePath]出错!", ex);
}
}
///
/// 获取场站信息
///
/// 场站信息
public List GetStationInfos()
{
try
{
if (_StationInfos == null || _StationInfos.Count <= 0)
{
_StationInfos = _WEBHelper.HttpGetJSON>($"{_Url}/api/station/info");
}
}
catch (Exception ex)
{
Console.WriteLine("读取场站信息出错!", ex);
}
return _StationInfos;
}
///
/// 获取单场站信息
///
/// 场站ID
///
internal StationInfo GetStatonInfo(string stationId)
{
try
{
if (_Stations == null)
{
_Stations = _StationInfos?.ToDictionary(s => s.Id, s => s);
}
}
catch { }
if (_Stations == null || !_Stations.ContainsKey(stationId)) return null;
return _Stations[stationId];
}
///
/// 获取升压站图形信息
///
internal List GetBoostStationShapes()
{
if (_BoostStationShapes == null)
{
InitBoostStationInfos();
}
return _BoostStationShapes;
}
///
/// 获取升压站场站信息
///
///
internal List GetBoostStationInfos()
{
if (_BoostStationInfos == null)
{
InitBoostStationInfos();
}
return _BoostStationInfos;
}
///
/// 移除升压站
///
///
internal string RemoveBoostStation(ShapeInfo shape)
{
try
{
var v = _WEBHelper.HttpGetString($"{_Url}/api/boost_station/remove?id={shape.Id}");
return v;
}
catch(Exception e)
{
return e.Message;
}
}
///
/// 移除二次图
///
/// 二次图信息
internal string RemoveSubStation(ShapeInfo arg1)
{
try
{
var v = _WEBHelper.HttpGetString($"{_Url}/api/boost_station/substation/remove?id={arg1.Id}");
return v;
}
catch (Exception e)
{
return e.Message;
}
}
private void InitBoostStationInfos()
{
try
{
var ls = _WEBHelper.HttpGetJSON>($"{_Url}/api/boost_station/");
_BoostStationShapes = ls;
var sis = from shape in ls select new StationInfo() { Name = shape.Name, Id = shape.Id };
_BoostStationInfos = sis.ToList();
_BoostStationInfos.Insert(0, new StationInfo() { Name = "全部", Id = "ALL" });
}
catch
{
_BoostStationInfos = new List();
_BoostStationShapes = new List();
}
}
///
/// 根据ID获取升压站信息
///
///
internal async Task GetNewStationInfo(string id)
{
return await Task.Run(getShapeInfo);
ShapeInfo getShapeInfo()
{
try
{
var si = _WEBHelper.HttpGetJSON($"{_Url}/api/boost_station/booststation?id={id}");
return si;
}
catch { }
return null;
}
}
///
/// 获取二次图信息
///
/// 二次图ID
///
internal async Task GetSubBoostStation(string subId)
{
return await Task.Run(getShapeInfo);
ShapeInfo getShapeInfo()
{
try
{
var si = _WEBHelper.HttpGetJSON($"{_Url}/api/boost_station/substation?id={subId}");
return si;
}
catch { }
return null;
}
}
///
/// 获取所有二次图
///
///
internal async Task> GetSubStations()
{
return await Task.Run(getShapeInfo);
List getShapeInfo()
{
try
{
var si = _WEBHelper.HttpGetJSON>($"{_Url}/api/boost_station/substation/all");
return si;
}
catch { }
return null;
}
}
///
/// 获取自定义控件
///
internal List GetCustomizeShapes()
{
try
{
var ls = _WEBHelper.HttpGetJSON>($"{_Url}/api/boost_station/customize");
_CustomizeShapesDic = ls?.ToDictionary(c => c.Id, c => c);
return ls;
}
catch { }
return new List();
}
internal Dictionary GetCustomizeShapesDic()
{
if (_CustomizeShapesDic == null)
{
_CustomizeShapesDic = GetCustomizeShapes()?.ToDictionary(c => c.Id, c => c);
}
return _CustomizeShapesDic;
}
///
/// 添加升压站
///
/// 图形信息
internal string AddOrUpdateStation(ShapeInfo info, Windows.BoostStation.BoostStationEditType editType)
{
try
{
switch (editType)
{
case Windows.BoostStation.BoostStationEditType.BoostStationEdit: // 升压站编辑
return _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/update", info);
case Windows.BoostStation.BoostStationEditType.BoostStationAdd: // 升压站添加
return _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/add", info);
case Windows.BoostStation.BoostStationEditType.SubStationAdd: // 二次图添加
return _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/substation/add", info);
case Windows.BoostStation.BoostStationEditType.SubStationEdit: // 二次图编辑
return _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/substation/update", info);
default:return "操作类型错误!";
}
}
catch (Exception e)
{
return e.Message;
}
}
///
/// 添加或移除自定义控件
///
/// 图形信息
/// 是否添加
internal string AddOrRemoveCustomizeShape(ShapeInfo info, string type)
{
try
{
string v = "";
switch (type)
{
case "add":
v = _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/customize/add", info);
break;
case "remove":
v = _WEBHelper.HttpGetString($"{_Url}/api/boost_station/customize/remove?id={info.Id}");
break;
case "edit":
v = _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/customize/update", info);
break;
default:return v;
}
return v;
}
catch (Exception e)
{
return e.Message;
}
}
///
/// 将导出的svg发送到服务器
///
/// svg字符串
/// svg信息
///
///
internal string OutputSvgInfo(object svg, SvgInfo info)
{
try
{
var s = _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/svg", svg);
if (s != "success")
{
return s;
}
s = _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/svginfo", info);
return s;
}
catch (Exception e)
{
return $"导出svg出现错误:{e.Message}";
}
}
}
}