TdevWebScadaDevSpotController.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Logging;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using Org.BouncyCastle.Crypto;
  7. using Ropin.Core.Common;
  8. using Ropin.Core.Extensions.Redis;
  9. using Ropin.Inspection.Api.Common;
  10. using Ropin.Inspection.Api.Controllers;
  11. using Ropin.Inspection.Common.Helper;
  12. using Ropin.Inspection.Model;
  13. using Ropin.Inspection.Model.Entities;
  14. using Ropin.Inspection.Model.SearchModel.DEV;
  15. using Ropin.Inspection.Model.ViewModel.DEV;
  16. using Ropin.Inspection.Service;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Threading.Tasks;
  21. namespace Ropin.Inspection.Api
  22. {
  23. public class TdevWebScadaDevSpotController : BaseController
  24. {
  25. public ILogger<TdevWebScadaDevSpotController> _logger { get; }
  26. private readonly ITdevWebScadaDevSpotService _TdevWebScadaDevSpotService;
  27. private readonly IPushMsgService _pushMsgService;
  28. /// <summary>
  29. /// 构造函数
  30. /// </summary>
  31. /// <param name="TdevWebScadaDevSpotService"></param>
  32. /// <param name="pushMsgService"></param>
  33. /// <param name="logger"></param>
  34. public TdevWebScadaDevSpotController(ITdevWebScadaDevSpotService TdevWebScadaDevSpotService, IPushMsgService pushMsgService, ILogger<TdevWebScadaDevSpotController> logger)
  35. {
  36. _TdevWebScadaDevSpotService = TdevWebScadaDevSpotService;
  37. _logger = logger;
  38. _pushMsgService = pushMsgService;
  39. }
  40. /// <summary>
  41. /// 通过id获取云组态设备点信息
  42. /// </summary>
  43. /// <param name="id"></param>
  44. /// <returns></returns>
  45. [HttpGet("GetWebScadaDevSpotAsync/{id}")]
  46. [AllowAnonymous]
  47. public async Task<ApiResult> GetWebScadaDevSpotAsync(string id)
  48. {
  49. if (string.IsNullOrEmpty(id))
  50. {
  51. return new ApiResult(ReturnCode.GeneralError);
  52. }
  53. try
  54. {
  55. var content = await _TdevWebScadaDevSpotService.GetConditionAsync(new TdevWebScadaDevSpotSearchModel { C_ID = id });
  56. return new ApiResult<TdevWebScadaDevSpotViewModel>(content.FirstOrDefault());
  57. }
  58. catch (Exception ex)
  59. {
  60. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  61. }
  62. }
  63. /// <summary>
  64. /// 通过devId获取云组态信息
  65. /// </summary>
  66. /// <param name="devId"></param>
  67. /// <returns></returns>
  68. [HttpGet("GetWebScadaByAsync/{devId}")]
  69. [AllowAnonymous]
  70. public async Task<ApiResult> GetWebScadaByAsync(string devId)
  71. {
  72. if (string.IsNullOrEmpty(devId))
  73. {
  74. return new ApiResult(ReturnCode.GeneralError);
  75. }
  76. try
  77. {
  78. var content = await _TdevWebScadaDevSpotService.GetWebScadaByAsync(devId);
  79. return new ApiResult<string>(content);
  80. }
  81. catch (Exception ex)
  82. {
  83. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  84. }
  85. }
  86. /// <summary>
  87. /// 通过devId获取云组态设备点值
  88. /// </summary>
  89. /// <param name="devId"></param>
  90. /// <returns></returns>
  91. [HttpGet("GetWebScadaDevSpotValue/{devId}")]
  92. [AllowAnonymous]
  93. public async Task<ApiResult> GetWebScadaDevSpotValue(string devId)
  94. {
  95. if (string.IsNullOrEmpty(devId))
  96. {
  97. return new ApiResult(ReturnCode.GeneralError);
  98. }
  99. try
  100. {
  101. var content = await _TdevWebScadaDevSpotService.GetWebScadaDevSpotValue(devId);
  102. Dictionary<string, DeviceVlue> msgAlarmDic = FanyiHelper.msgAlarmDic;
  103. foreach (KeyValuePair<string, DeviceVlue> keyValuePair in msgAlarmDic)
  104. {
  105. Console.WriteLine("key:{0}\tvalue:{1}", keyValuePair.Key, keyValuePair.Value);
  106. await _pushMsgService.PushAlarmMsgAsync(new TpushMsgModel
  107. {
  108. C_DevStoreCode = keyValuePair.Value.storeCode,
  109. C_MsgTypeCode = "MSG_TYPE_001",
  110. Msg = "设备点异常" + keyValuePair.Value.name + " boxId:" + keyValuePair.Value.boxId + " " + keyValuePair.Key,
  111. Subject = "运维通知",
  112. DevNumber = "boxId:" + keyValuePair.Value.boxId + " "+ keyValuePair.Key,
  113. DevName = keyValuePair.Value.name,
  114. GenerationType = 2,
  115. msgStatus = 0,
  116. }, "运维通知");
  117. }
  118. FanyiHelper.msgAlarmDic.Clear();
  119. return new ApiResult<string>(content);
  120. }
  121. catch (Exception ex)
  122. {
  123. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  124. }
  125. }
  126. /// <summary>
  127. /// 测试Redis消息队列
  128. /// </summary>
  129. /// <param name="_redisBasketRepository"></param>
  130. /// <param name="devId"></param>
  131. /// <returns></returns>
  132. [HttpGet("GetWebScadaDevSpotValueByRedis/{devId}")]
  133. [AllowAnonymous]
  134. public async Task<ApiResult> GetWebScadaDevSpotValueByRedis([FromServices] IRedisBasketRepository _redisBasketRepository, string devId)
  135. {
  136. if (string.IsNullOrEmpty(devId))
  137. {
  138. return new ApiResult(ReturnCode.GeneralError);
  139. }
  140. try
  141. {
  142. var content = await _redisBasketRepository.GetValue(RedisKey.Fanyibox_DevStore_ + devId);
  143. //var v = JsonConvert.DeserializeObject<JObject>(content);
  144. return new ApiResult<string>(content);
  145. }
  146. catch (Exception ex)
  147. {
  148. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  149. }
  150. }
  151. /// <summary>
  152. /// 取设备值
  153. /// </summary>
  154. /// <param name="_redisBasketRepository"></param>
  155. /// <param name="devId"></param>
  156. /// <returns></returns>
  157. [HttpGet("GetWebScadaDevSpotsByRedis/{devId}")]
  158. [AllowAnonymous]
  159. public async Task<ApiResult> GetWebScadaDevSpotsByRedis([FromServices] IRedisBasketRepository _redisBasketRepository, string devId)
  160. {
  161. if (string.IsNullOrEmpty(devId))
  162. {
  163. return new ApiResult(ReturnCode.GeneralError);
  164. }
  165. try
  166. {
  167. var content = await _redisBasketRepository.GetValue(RedisKey.Fanyibox_DevStore_ + devId);
  168. //var jo = JsonConvert.DeserializeObject<JObject>(content);
  169. //var list = jo["device"]?.ToObject<IEnumerable<DeviceSpot>>();
  170. var jo = JsonConvert.DeserializeObject<JObject>(content);
  171. var list = jo?.ToObject<DeviceSpotData>();
  172. return new ApiResult<DeviceSpotData>(list);
  173. }
  174. catch (Exception ex)
  175. {
  176. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  177. }
  178. }
  179. /// <summary>
  180. /// 取设备值,直接返回设备值
  181. /// </summary>
  182. /// <param name="_redisBasketRepository"></param>
  183. /// <param name="devId"></param>
  184. /// <returns></returns>
  185. [HttpGet("GetWebScadaDevValueByRedis/{devId}")]
  186. [AllowAnonymous]
  187. public async Task<DeviceSpotData> GetWebScadaDevValueByRedis([FromServices] IRedisBasketRepository _redisBasketRepository, string devId)
  188. {
  189. if (string.IsNullOrEmpty(devId))
  190. {
  191. return null;
  192. }
  193. try
  194. {
  195. var content = await _redisBasketRepository.GetValue(RedisKey.Fanyibox_DevStore_ + devId);
  196. //var jo = JsonConvert.DeserializeObject<JObject>(content);
  197. //var list = jo["device"]?.ToObject<IEnumerable<DeviceSpot>>();
  198. var jo = JsonConvert.DeserializeObject<JObject>(content);
  199. var list = jo?.ToObject<DeviceSpotData>();
  200. return list;
  201. }
  202. catch (Exception ex)
  203. {
  204. return null;
  205. }
  206. }
  207. /// <summary>
  208. /// 大屏设备点数据
  209. /// </summary>
  210. /// <param name="_redisBasketRepository"></param>
  211. /// <param name="devId"></param>
  212. /// <returns></returns>
  213. [HttpGet("GetWebScadaDevSpotValueForLargeScreenByRedis/{devId}")]
  214. [AllowAnonymous]
  215. public async Task<string> GetWebScadaDevSpotValueForLargeScreenByRedis([FromServices] IRedisBasketRepository _redisBasketRepository, string devId)
  216. {
  217. if (string.IsNullOrEmpty(devId))
  218. {
  219. return "请输入参数";
  220. }
  221. try
  222. {
  223. var content = await _redisBasketRepository.GetValue(RedisKey.Fanyibox_DevStore_ + devId);
  224. if(content == null)
  225. return "[]";
  226. JObject jObjectContent = JsonConvert.DeserializeObject<JObject>(content);
  227. string strContent = JsonConvert.SerializeObject(jObjectContent["device"]);
  228. if (string.IsNullOrEmpty(strContent))
  229. return "[]";
  230. else
  231. {
  232. JArray ja = JsonConvert.DeserializeObject<JArray>(strContent);
  233. for (int i = 0; i < ja.Count; i++)
  234. {
  235. JObject jo = JObject.Parse(ja[i].ToString());
  236. string uName = jo["name"].ToString() + "("+ jo["unitName"].ToString()+")";
  237. ja[i]["name"] = uName;
  238. }
  239. return JsonConvert.SerializeObject(ja);
  240. }
  241. }
  242. catch (Exception ex)
  243. {
  244. return "[]";
  245. //return ex.Message;
  246. }
  247. }
  248. /// <summary>
  249. /// 保存灯设备点数据
  250. /// </summary>
  251. /// <param name="_redisBasketRepository"></param>
  252. /// <param name="spotModel"></param>
  253. /// <returns></returns>
  254. [HttpPost("SaveDevSpotValueByRedis")]
  255. [AllowAnonymous]
  256. public async Task<ApiResult> SaveLightDevSpotValueByRedis([FromServices] IRedisBasketRepository _redisBasketRepository, LightDevSpotList spotModel)
  257. {
  258. if (spotModel == null || string.IsNullOrWhiteSpace(spotModel.ModuleId))
  259. {
  260. return new ApiResult(ReturnCode.GeneralError);
  261. }
  262. try
  263. {
  264. await _redisBasketRepository.Set(RedisKey.Light_Dev_ + spotModel.ModuleId, spotModel, new TimeSpan(4, 20, 33));
  265. //var content = await _redisBasketRepository.GetValue(RedisKey.Light_Dev_ + spotModel.ModuleId);
  266. //if (content == null)
  267. // content = await _redisBasketRepository.Set(RedisKey.Light_Dev_ + spotModel.ModuleId, spotModel,new TimeSpan(360000));
  268. //else
  269. //{
  270. // LightDevSpotList lightDevSpotList = JsonConvert.DeserializeObject<LightDevSpotList>(content);
  271. // LightDevSpot lightDevSpot = lightDevSpotList.LightDevSpots.Where(x=>x.Name == spotModel.Name).FirstOrDefault();
  272. // if (lightDevSpot != null)
  273. // {
  274. // lightDevSpotList.LightDevSpots.Remove(lightDevSpot);
  275. // }
  276. // lightDevSpotList.LightDevSpots.Add(spotModel);
  277. //}
  278. return new ApiResult(ReturnCode.Success);
  279. }
  280. catch (Exception ex)
  281. {
  282. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  283. }
  284. }
  285. /// <summary>
  286. /// 取灯设备点数据
  287. /// </summary>
  288. /// <param name="_redisBasketRepository"></param>
  289. /// <param name="moduleId">模块ID</param>
  290. /// <returns></returns>
  291. [HttpGet("GetLightDevSpotValueByRedis/{moduleId}")]
  292. [AllowAnonymous]
  293. public async Task<ApiResult> GetLightDevSpotValueByRedis([FromServices] IRedisBasketRepository _redisBasketRepository, string moduleId)
  294. {
  295. if (string.IsNullOrEmpty(moduleId))
  296. {
  297. return new ApiResult(ReturnCode.GeneralError);
  298. }
  299. try
  300. {
  301. var content = await _redisBasketRepository.GetValue(RedisKey.Light_Dev_ + moduleId);
  302. //var v = JsonConvert.DeserializeObject<JObject>(content);
  303. return new ApiResult<string>(content);
  304. }
  305. catch (Exception ex)
  306. {
  307. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  308. }
  309. }
  310. /// <summary>
  311. /// 通过devId获取云组态设备点历史值
  312. /// </summary>
  313. /// <param name="devId"></param>
  314. /// <returns></returns>
  315. [HttpGet("GetWebScadaDevSpotHisData/{devId}")]
  316. [AllowAnonymous]
  317. public async Task<ApiResult> GetWebScadaDevSpotHisData(string devId)
  318. {
  319. if (string.IsNullOrEmpty(devId))
  320. {
  321. return new ApiResult(ReturnCode.GeneralError);
  322. }
  323. try
  324. {
  325. var content = await _TdevWebScadaDevSpotService.GetWebScadaDevSpotHisData(devId);
  326. return new ApiResult<string>(content);
  327. }
  328. catch (Exception ex)
  329. {
  330. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  331. }
  332. }
  333. /// <summary>
  334. /// 获取所有云组态设备点
  335. /// </summary>
  336. /// <returns></returns>
  337. [HttpGet("GetWebScadaDevSpotsAsync")]
  338. public async Task<ApiResult> GetWebScadaDevSpotsAsync()
  339. {
  340. try
  341. {
  342. var contentList = await _TdevWebScadaDevSpotService.GetAllAsync();
  343. return new ApiResult<IEnumerable<TdevWebScadaDevSpotViewModel>>(contentList);
  344. }
  345. catch (Exception ex)
  346. {
  347. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  348. }
  349. }
  350. /// <summary>
  351. /// 通过云组态设备点名称条件查询
  352. /// </summary>
  353. /// <param name="searchModel"></param>
  354. /// <returns></returns>
  355. [HttpPost("GetWebScadaDevSpotsByAsync")]
  356. public async Task<ApiResult> GetWebScadaDevSpotsByAsync(TdevWebScadaDevSpotSearchModel searchModel)
  357. {
  358. if (searchModel == null)
  359. {
  360. return new ApiResult(ReturnCode.ArgsError);
  361. }
  362. searchModel.IsPagination = false;
  363. try
  364. {
  365. var contentList = await _TdevWebScadaDevSpotService.GetConditionAsync(searchModel);
  366. return new ApiResult<PagesModel<TdevWebScadaDevSpotViewModel>>(new PagesModel<TdevWebScadaDevSpotViewModel>(contentList, searchModel));
  367. }
  368. catch (Exception ex)
  369. {
  370. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  371. }
  372. }
  373. /// <summary>
  374. /// 创建云组态设备点
  375. /// </summary>
  376. /// <param name="content"></param>
  377. /// <returns></returns>
  378. [HttpPost("CreateWebScadaDevSpotAsync")]
  379. public async Task<ApiResult> CreateWebScadaDevSpotAsync(TdevWebScadaDevSpotCreateModel content)
  380. {
  381. if (content == null)
  382. {
  383. return new ApiResult(ReturnCode.ArgsError);
  384. }
  385. try
  386. {
  387. await _TdevWebScadaDevSpotService.CreateAsync(content);
  388. }
  389. catch (Exception ex)
  390. {
  391. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  392. }
  393. return new ApiResult(ReturnCode.Success);
  394. }
  395. /// <summary>
  396. /// 删除云组态设备点
  397. /// </summary>
  398. /// <param name="id"></param>
  399. /// <returns></returns>
  400. [HttpDelete("DeleteWebScadaDevSpotAsync/{id}")]
  401. public async Task<ApiResult> DeleteWebScadaDevSpotAsync(string id)
  402. {
  403. if (string.IsNullOrEmpty(id))
  404. {
  405. return new ApiResult(ReturnCode.GeneralError);
  406. }
  407. try
  408. {
  409. await _TdevWebScadaDevSpotService.DeleteAsync(id);
  410. }
  411. catch (Exception ex)
  412. {
  413. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  414. }
  415. return new ApiResult(ReturnCode.Success);
  416. }
  417. /// <summary>
  418. /// 删除云组态设备点【真删除】
  419. /// </summary>
  420. /// <param name="id"></param>
  421. /// <returns></returns>
  422. [HttpDelete("DeleteWebScadaDevSpotDataAsync/{id}")]
  423. public async Task<ApiResult> DeleteWebScadaDevSpotDataAsync(string id)
  424. {
  425. if (string.IsNullOrEmpty(id))
  426. {
  427. return new ApiResult(ReturnCode.GeneralError);
  428. }
  429. try
  430. {
  431. await _TdevWebScadaDevSpotService.DeleteDataAsync(id);
  432. }
  433. catch (Exception ex)
  434. {
  435. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  436. }
  437. return new ApiResult(ReturnCode.Success);
  438. }
  439. /// <summary>
  440. /// 更新云组态设备点
  441. /// </summary>
  442. /// <param name="id"></param>
  443. /// <param name="updateModel"></param>
  444. /// <returns></returns>
  445. [HttpPut("UpdateWebScadaDevSpotAsync/{id}")]
  446. public async Task<ApiResult> UpdateWebScadaDevSpotAsync(string id, TdevWebScadaDevSpotUpdateModel updateModel)
  447. {
  448. if (string.IsNullOrEmpty(id))
  449. {
  450. return new ApiResult(ReturnCode.GeneralError);
  451. }
  452. try
  453. {
  454. await _TdevWebScadaDevSpotService.UpdateAsync(id, updateModel);
  455. }
  456. catch (Exception ex)
  457. {
  458. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  459. }
  460. return new ApiResult(ReturnCode.Success);
  461. }
  462. /// <summary>
  463. /// 更新云组态设备点顺序
  464. /// </summary>
  465. /// <param name="ids"></param>
  466. /// <returns></returns>
  467. [HttpPut("UpdateSortAsync")]
  468. public async Task<ApiResult> UpdateSortAsync(IEnumerable<string> ids)
  469. {
  470. if (null == ids)
  471. {
  472. return new ApiResult(ReturnCode.GeneralError);
  473. }
  474. try
  475. {
  476. await _TdevWebScadaDevSpotService.UpdateSortAsync(ids);
  477. }
  478. catch (Exception ex)
  479. {
  480. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  481. }
  482. return new ApiResult(ReturnCode.Success);
  483. }
  484. /// <summary>
  485. /// 判断设备是否已经存在
  486. /// </summary>
  487. /// <param name="model"></param>
  488. /// <returns></returns>
  489. [HttpPost("devBoxIsExist")]
  490. public async Task<ApiResult> devBoxIsExist(TdevWebScadaDevSpotIsExist model)
  491. {
  492. var data = false;
  493. if (null == model)
  494. {
  495. return new ApiResult(ReturnCode.GeneralError);
  496. }
  497. try
  498. {
  499. data=await _TdevWebScadaDevSpotService.devBoxIsExist(model);
  500. }
  501. catch (Exception ex)
  502. {
  503. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  504. }
  505. return new ApiResult<object>(data);
  506. }
  507. /// <summary>
  508. /// 获取设备网关关联列表
  509. /// </summary>
  510. /// <param name="id"></param>
  511. /// <returns></returns>
  512. [HttpGet("GetdevDevBoxConditionAsync/{id}")]
  513. [AllowAnonymous]
  514. public async Task<ApiResult> GetdevDevBoxConditionAsync(string id)
  515. {
  516. if (string.IsNullOrEmpty(id))
  517. {
  518. return new ApiResult(ReturnCode.GeneralError);
  519. }
  520. try
  521. {
  522. DevBoxSearchModel searchModel = new DevBoxSearchModel();
  523. searchModel.C_DevStoreCode = id;
  524. var content = await _TdevWebScadaDevSpotService.GetdevDevBoxConditionAsync(searchModel);
  525. return new ApiResult<IEnumerable<DevBoxViewModel>>(content);
  526. }
  527. catch (Exception ex)
  528. {
  529. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  530. }
  531. }
  532. }
  533. }