TdevWebScadaDevSpotController.cs 21 KB

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