StationManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using NEIntelligentControl2.Models.Messages;
  2. using NEIntelligentControl2.Models.Station;
  3. using SVGViewer.Models.Info;
  4. using SVGViewer.Models.Svg;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Threading;
  12. namespace NEIntelligentControl2.Service.Station
  13. {
  14. /// <summary>
  15. /// 场站信息维护
  16. /// </summary>
  17. public class StationManager
  18. {
  19. private List<StationInfo> _StationInfos; // 场站信息
  20. private WEBHelper _WEBHelper;
  21. private string _Url;
  22. private Dictionary<string, StationInfo> _Stations;
  23. private List<StationInfo> _BoostStationInfos;
  24. private List<ShapeInfo> _BoostStationShapes;
  25. private Dictionary<string, ShapeInfo> _CustomizeShapesDic;
  26. public StationManager(WEBHelper web)
  27. {
  28. _WEBHelper = web;
  29. try
  30. {
  31. #if (DEBUG)
  32. _Url = ConfigurationManager.AppSettings["ServicePathDebug"];
  33. #else
  34. _Url = ConfigurationManager.AppSettings["ServicePath"];
  35. #endif
  36. }
  37. catch (Exception ex)
  38. {
  39. Console.WriteLine("读取配置文件[ServicePath]出错!", ex);
  40. }
  41. }
  42. /// <summary>
  43. /// 获取场站信息
  44. /// </summary>
  45. /// <returns>场站信息</returns>
  46. public List<StationInfo> GetStationInfos()
  47. {
  48. try
  49. {
  50. if (_StationInfos == null || _StationInfos.Count <= 0)
  51. {
  52. _StationInfos = _WEBHelper.HttpGetJSON<List<StationInfo>>($"{_Url}/api/station/info");
  53. }
  54. }
  55. catch (Exception ex)
  56. {
  57. Console.WriteLine("读取场站信息出错!", ex);
  58. }
  59. return _StationInfos;
  60. }
  61. /// <summary>
  62. /// 获取单场站信息
  63. /// </summary>
  64. /// <param name="stationId">场站ID</param>
  65. /// <returns></returns>
  66. internal StationInfo GetStatonInfo(string stationId)
  67. {
  68. try
  69. {
  70. if (_Stations == null)
  71. {
  72. _Stations = _StationInfos?.ToDictionary(s => s.Id, s => s);
  73. }
  74. }
  75. catch { }
  76. if (_Stations == null || !_Stations.ContainsKey(stationId)) return null;
  77. return _Stations[stationId];
  78. }
  79. /// <summary>
  80. /// 获取升压站图形信息
  81. /// </summary>
  82. internal List<ShapeInfo> GetBoostStationShapes()
  83. {
  84. if (_BoostStationShapes == null)
  85. {
  86. InitBoostStationInfos();
  87. }
  88. return _BoostStationShapes;
  89. }
  90. /// <summary>
  91. /// 获取升压站场站信息
  92. /// </summary>
  93. /// <returns></returns>
  94. internal List<StationInfo> GetBoostStationInfos()
  95. {
  96. if (_BoostStationInfos == null)
  97. {
  98. InitBoostStationInfos();
  99. }
  100. return _BoostStationInfos;
  101. }
  102. /// <summary>
  103. /// 移除升压站
  104. /// </summary>
  105. /// <exception cref="NotImplementedException"></exception>
  106. internal string RemoveBoostStation(ShapeInfo shape)
  107. {
  108. try
  109. {
  110. var v = _WEBHelper.HttpGetString($"{_Url}/api/boost_station/remove?id={shape.Id}");
  111. return v;
  112. }
  113. catch(Exception e)
  114. {
  115. return e.Message;
  116. }
  117. }
  118. /// <summary>
  119. /// 移除二次图
  120. /// </summary>
  121. /// <param name="arg1">二次图信息</param>
  122. internal string RemoveSubStation(ShapeInfo arg1)
  123. {
  124. try
  125. {
  126. var v = _WEBHelper.HttpGetString($"{_Url}/api/boost_station/substation/remove?id={arg1.Id}");
  127. return v;
  128. }
  129. catch (Exception e)
  130. {
  131. return e.Message;
  132. }
  133. }
  134. private void InitBoostStationInfos()
  135. {
  136. try
  137. {
  138. var ls = _WEBHelper.HttpGetJSON<List<ShapeInfo>>($"{_Url}/api/boost_station/");
  139. _BoostStationShapes = ls;
  140. var sis = from shape in ls select new StationInfo() { Name = shape.Name, Id = shape.Id };
  141. _BoostStationInfos = sis.ToList();
  142. _BoostStationInfos.Insert(0, new StationInfo() { Name = "全部", Id = "ALL" });
  143. }
  144. catch
  145. {
  146. _BoostStationInfos = new List<StationInfo>();
  147. _BoostStationShapes = new List<ShapeInfo>();
  148. }
  149. }
  150. /// <summary>
  151. /// 根据ID获取升压站信息
  152. /// </summary>
  153. /// <exception cref="NotImplementedException"></exception>
  154. internal async Task<ShapeInfo> GetNewStationInfo(string id)
  155. {
  156. return await Task.Run(getShapeInfo);
  157. ShapeInfo getShapeInfo()
  158. {
  159. try
  160. {
  161. var si = _WEBHelper.HttpGetJSON<ShapeInfo>($"{_Url}/api/boost_station/booststation?id={id}");
  162. return si;
  163. }
  164. catch { }
  165. return null;
  166. }
  167. }
  168. /// <summary>
  169. /// 获取二次图信息
  170. /// </summary>
  171. /// <param name="subId">二次图ID</param>
  172. /// <returns></returns>
  173. internal async Task<ShapeInfo> GetSubBoostStation(string subId)
  174. {
  175. return await Task.Run(getShapeInfo);
  176. ShapeInfo getShapeInfo()
  177. {
  178. try
  179. {
  180. var si = _WEBHelper.HttpGetJSON<ShapeInfo>($"{_Url}/api/boost_station/substation?id={subId}");
  181. return si;
  182. }
  183. catch { }
  184. return null;
  185. }
  186. }
  187. /// <summary>
  188. /// 获取所有二次图
  189. /// </summary>
  190. /// <returns></returns>
  191. internal async Task<List<ShapeInfo>> GetSubStations()
  192. {
  193. return await Task.Run(getShapeInfo);
  194. List<ShapeInfo> getShapeInfo()
  195. {
  196. try
  197. {
  198. var si = _WEBHelper.HttpGetJSON<List<ShapeInfo>>($"{_Url}/api/boost_station/substation/all");
  199. return si;
  200. }
  201. catch { }
  202. return null;
  203. }
  204. }
  205. /// <summary>
  206. /// 获取自定义控件
  207. /// </summary>
  208. internal List<ShapeInfo> GetCustomizeShapes()
  209. {
  210. try
  211. {
  212. var ls = _WEBHelper.HttpGetJSON<List<ShapeInfo>>($"{_Url}/api/boost_station/customize");
  213. _CustomizeShapesDic = ls?.ToDictionary(c => c.Id, c => c);
  214. return ls;
  215. }
  216. catch { }
  217. return new List<ShapeInfo>();
  218. }
  219. internal Dictionary<string,ShapeInfo> GetCustomizeShapesDic()
  220. {
  221. if (_CustomizeShapesDic == null)
  222. {
  223. _CustomizeShapesDic = GetCustomizeShapes()?.ToDictionary(c => c.Id, c => c);
  224. }
  225. return _CustomizeShapesDic;
  226. }
  227. /// <summary>
  228. /// 添加升压站
  229. /// </summary>
  230. /// <param name="info">图形信息</param>
  231. internal string AddOrUpdateStation(ShapeInfo info, Windows.BoostStation.BoostStationEditType editType)
  232. {
  233. try
  234. {
  235. switch (editType)
  236. {
  237. case Windows.BoostStation.BoostStationEditType.BoostStationEdit: // 升压站编辑
  238. return _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/update", info);
  239. case Windows.BoostStation.BoostStationEditType.BoostStationAdd: // 升压站添加
  240. return _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/add", info);
  241. case Windows.BoostStation.BoostStationEditType.SubStationAdd: // 二次图添加
  242. return _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/substation/add", info);
  243. case Windows.BoostStation.BoostStationEditType.SubStationEdit: // 二次图编辑
  244. return _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/substation/update", info);
  245. default:return "操作类型错误!";
  246. }
  247. }
  248. catch (Exception e)
  249. {
  250. return e.Message;
  251. }
  252. }
  253. /// <summary>
  254. /// 添加或移除自定义控件
  255. /// </summary>
  256. /// <param name="info">图形信息</param>
  257. /// <param name="isadd">是否添加</param>
  258. internal string AddOrRemoveCustomizeShape(ShapeInfo info, string type)
  259. {
  260. try
  261. {
  262. string v = "";
  263. switch (type)
  264. {
  265. case "add":
  266. v = _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/customize/add", info);
  267. break;
  268. case "remove":
  269. v = _WEBHelper.HttpGetString($"{_Url}/api/boost_station/customize/remove?id={info.Id}");
  270. break;
  271. case "edit":
  272. v = _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/customize/update", info);
  273. break;
  274. default:return v;
  275. }
  276. return v;
  277. }
  278. catch (Exception e)
  279. {
  280. return e.Message;
  281. }
  282. }
  283. /// <summary>
  284. /// 将导出的svg发送到服务器
  285. /// </summary>
  286. /// <param name="svg">svg字符串</param>
  287. /// <param name="info">svg信息</param>
  288. /// <returns></returns>
  289. /// <exception cref="NotImplementedException"></exception>
  290. internal string OutputSvgInfo(object svg, SvgInfo info)
  291. {
  292. try
  293. {
  294. var s = _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/svg", svg);
  295. if (s != "success")
  296. {
  297. return s;
  298. }
  299. s = _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/svginfo", info);
  300. return s;
  301. }
  302. catch (Exception e)
  303. {
  304. return $"导出svg出现错误:{e.Message}";
  305. }
  306. }
  307. }
  308. }