using NEIntelligentControl2.Models.BoostStation;
using NEIntelligentControl2.Models.Messages;
using NEIntelligentControl2.Models.Pages;
using NEIntelligentControl2.Models.Station;
using NEIntelligentControl2.Service.WebSocket;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace NEIntelligentControl2.Pages.BoostStation
{
///
/// 升压站页面
///
public partial class PageBoostStation : Page
{
private Dictionary _Stations;
private Dictionary _BoostStationInfos;
///
/// 当前 选择的场站
///
private string _CurrentStationId;
///
/// 服务地址
///
private string _Url;
private WEBHelper _WEBHelper;
private IPageAction _IPageAction;
private TagManager _TagManager;
private List _StationInfos;
private UrlManager _UrlManager;
public PageBoostStation(WEBHelper web, IPageAction pa, TagManager tm, UrlManager um)
{
InitializeComponent();
_WEBHelper = web;
_IPageAction = pa;
_TagManager = tm;
_UrlManager = um;
_Url = _UrlManager.ServicePath;
InitBoostatationInfos();
}
private void InitBoostatationInfos()
{
_BoostStationInfos = new Dictionary();
_Stations = new Dictionary();
try
{
var infos = _WEBHelper.HttpGetJSON>($"{_Url}/api/station/boost_station/all");
_BoostStationInfos = infos;
var sis = from i in infos
orderby i.Value.Index
select new StationInfo()
{
Name = i.Value.TagName,
Id = i.Value.Id
};
_StationInfos = sis.ToList();
}
catch (Exception ex)
{
Console.WriteLine($"升压站信息加载出现错误:{ex.Message}");
}
}
///
/// 场站选择改变
///
/// 场站ID
public void StationChanged(string stationId)
{
if (_CurrentStationId == stationId) return;
if (!_Stations.ContainsKey(stationId))
{
BoostStationInfo bsi = new BoostStationInfo() { Id = stationId };
if (_BoostStationInfos.ContainsKey(stationId))
{
bsi = _BoostStationInfos[stationId];
}
Page sp = _IPageAction["BoostStation.PageStation"];
(sp as PageStation).StationInfo = bsi;
(sp as PageStation).SaveAction = SaveBoostStationInfos;
_Stations.Add(stationId, sp as PageStation);
}
_CurrentStationId = stationId;
_MainFrame.Navigate(_Stations[stationId]);
if (!_MainFrame.NavigationService.CanGoBack) return;
var en = _MainFrame.NavigationService.RemoveBackEntry();
while (en != null)
{
en = _MainFrame.NavigationService.RemoveBackEntry();
}
}
///
/// 保存升压站信息
///
private void SaveBoostStationInfos(BoostStationInfo obj)
{
if (_BoostStationInfos.ContainsKey(obj.Id))
{
_BoostStationInfos[obj.Id] = obj;
}
else
{
_BoostStationInfos.Add(obj.Id, obj);
}
try
{
_WEBHelper.HttpPostBodyString($"{_Url}/api/station/boost_station", obj);
}
catch (Exception ex)
{
Console.WriteLine($"保存升压站信息出现错误:{ex.Message}");
}
try
{
string s = _WEBHelper.Serialize(_BoostStationInfos);
File.WriteAllText(".\\BoostStationInfos.json", s);
}
catch { }
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
_TagManager.TagSwitched = TagSwitched;
_TagManager.TagChanged?.Invoke(_StationInfos);
_CurrentStationId = null;
}
private void TagSwitched(StationInfo obj)
{
StationChanged(obj.Id);
}
private void Page_Unloaded(object sender, RoutedEventArgs e)
{
_CurrentStationId = null;
}
}
}