using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Org.BouncyCastle.Crypto; using Ropin.Core.Common; using Ropin.Core.Extensions.Redis; using Ropin.Inspection.Api.Common; using Ropin.Inspection.Api.Controllers; using Ropin.Inspection.Common.Helper; using Ropin.Inspection.Model; using Ropin.Inspection.Model.Entities; using Ropin.Inspection.Model.SearchModel.DEV; using Ropin.Inspection.Model.ViewModel.DEV; using Ropin.Inspection.Service; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Ropin.Inspection.Api { public class TdevWebScadaDevSpotController : BaseController { public ILogger<TdevWebScadaDevSpotController> _logger { get; } private readonly ITdevWebScadaDevSpotService _TdevWebScadaDevSpotService; private readonly IPushMsgService _pushMsgService; /// <summary> /// 构造函数 /// </summary> /// <param name="TdevWebScadaDevSpotService"></param> /// <param name="pushMsgService"></param> /// <param name="logger"></param> public TdevWebScadaDevSpotController(ITdevWebScadaDevSpotService TdevWebScadaDevSpotService, IPushMsgService pushMsgService, ILogger<TdevWebScadaDevSpotController> logger) { _TdevWebScadaDevSpotService = TdevWebScadaDevSpotService; _logger = logger; _pushMsgService = pushMsgService; } /// <summary> /// 通过id获取云组态设备点信息 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpGet("GetWebScadaDevSpotAsync/{id}")] [AllowAnonymous] public async Task<ApiResult> GetWebScadaDevSpotAsync(string id) { if (string.IsNullOrEmpty(id)) { return new ApiResult(ReturnCode.GeneralError); } try { var content = await _TdevWebScadaDevSpotService.GetConditionAsync(new TdevWebScadaDevSpotSearchModel { C_ID = id }); return new ApiResult<TdevWebScadaDevSpotViewModel>(content.FirstOrDefault()); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 通过devId获取云组态信息 /// </summary> /// <param name="devId"></param> /// <returns></returns> [HttpGet("GetWebScadaByAsync/{devId}")] [AllowAnonymous] public async Task<ApiResult> GetWebScadaByAsync(string devId) { if (string.IsNullOrEmpty(devId)) { return new ApiResult(ReturnCode.GeneralError); } try { var content = await _TdevWebScadaDevSpotService.GetWebScadaByAsync(devId); return new ApiResult<string>(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 通过devId获取云组态设备点值 /// </summary> /// <param name="devId"></param> /// <returns></returns> [HttpGet("GetWebScadaDevSpotValue/{devId}")] [AllowAnonymous] public async Task<ApiResult> GetWebScadaDevSpotValue(string devId) { if (string.IsNullOrEmpty(devId)) { return new ApiResult(ReturnCode.GeneralError); } try { var content = await _TdevWebScadaDevSpotService.GetWebScadaDevSpotValue(devId); Dictionary<string, DeviceVlue> msgAlarmDic = FanyiHelper.msgAlarmDic; foreach (KeyValuePair<string, DeviceVlue> keyValuePair in msgAlarmDic) { Console.WriteLine("key:{0}\tvalue:{1}", keyValuePair.Key, keyValuePair.Value); await _pushMsgService.PushAlarmMsgAsync(new TpushMsgModel { C_DevStoreCode = keyValuePair.Value.storeCode, C_MsgTypeCode = "MSG_TYPE_001", Msg = "设备点异常" + keyValuePair.Value.name + " boxId:" + keyValuePair.Value.boxId + " " + keyValuePair.Key, Subject = "运维通知", DevNumber = "boxId:" + keyValuePair.Value.boxId + " "+ keyValuePair.Key, DevName = keyValuePair.Value.name, GenerationType = 2, msgStatus = 0, }, "运维通知"); } FanyiHelper.msgAlarmDic.Clear(); return new ApiResult<string>(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 测试Redis消息队列 /// </summary> /// <param name="_redisBasketRepository"></param> /// <param name="devId"></param> /// <returns></returns> [HttpGet("GetWebScadaDevSpotValueByRedis/{devId}")] [AllowAnonymous] public async Task<ApiResult> GetWebScadaDevSpotValueByRedis([FromServices] IRedisBasketRepository _redisBasketRepository, string devId) { if (string.IsNullOrEmpty(devId)) { return new ApiResult(ReturnCode.GeneralError); } try { var content = await _redisBasketRepository.GetValue(RedisKey.Fanyibox_DevStore_ + devId); //var v = JsonConvert.DeserializeObject<JObject>(content); return new ApiResult<string>(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 取设备值 /// </summary> /// <param name="_redisBasketRepository"></param> /// <param name="devId"></param> /// <returns></returns> [HttpGet("GetWebScadaDevSpotsByRedis/{devId}")] [AllowAnonymous] public async Task<ApiResult> GetWebScadaDevSpotsByRedis([FromServices] IRedisBasketRepository _redisBasketRepository, string devId) { if (string.IsNullOrEmpty(devId)) { return new ApiResult(ReturnCode.GeneralError); } try { var content = await _redisBasketRepository.GetValue(RedisKey.Fanyibox_DevStore_ + devId); //var jo = JsonConvert.DeserializeObject<JObject>(content); //var list = jo["device"]?.ToObject<IEnumerable<DeviceSpot>>(); var jo = JsonConvert.DeserializeObject<JObject>(content); var list = jo?.ToObject<DeviceSpotData>(); return new ApiResult<DeviceSpotData>(list); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 取设备值,直接返回设备值 /// </summary> /// <param name="_redisBasketRepository"></param> /// <param name="devId"></param> /// <returns></returns> [HttpGet("GetWebScadaDevValueByRedis/{devId}")] [AllowAnonymous] public async Task<DeviceSpotData> GetWebScadaDevValueByRedis([FromServices] IRedisBasketRepository _redisBasketRepository, string devId) { if (string.IsNullOrEmpty(devId)) { return null; } try { var content = await _redisBasketRepository.GetValue(RedisKey.Fanyibox_DevStore_ + devId); //var jo = JsonConvert.DeserializeObject<JObject>(content); //var list = jo["device"]?.ToObject<IEnumerable<DeviceSpot>>(); var jo = JsonConvert.DeserializeObject<JObject>(content); var list = jo?.ToObject<DeviceSpotData>(); return list; } catch (Exception ex) { return null; } } /// <summary> /// 大屏设备点数据 /// </summary> /// <param name="_redisBasketRepository"></param> /// <param name="devId"></param> /// <returns></returns> [HttpGet("GetWebScadaDevSpotValueForLargeScreenByRedis/{devId}")] [AllowAnonymous] public async Task<string> GetWebScadaDevSpotValueForLargeScreenByRedis([FromServices] IRedisBasketRepository _redisBasketRepository, string devId) { if (string.IsNullOrEmpty(devId)) { return "请输入参数"; } try { var content = await _redisBasketRepository.GetValue(RedisKey.Fanyibox_DevStore_ + devId); if(content == null) return "[]"; JObject jObjectContent = JsonConvert.DeserializeObject<JObject>(content); string strContent = JsonConvert.SerializeObject(jObjectContent["device"]); if (string.IsNullOrEmpty(strContent)) return "[]"; else { JArray ja = JsonConvert.DeserializeObject<JArray>(strContent); for (int i = 0; i < ja.Count; i++) { JObject jo = JObject.Parse(ja[i].ToString()); string uName = jo["name"].ToString() + "("+ jo["unitName"].ToString()+")"; ja[i]["name"] = uName; } return JsonConvert.SerializeObject(ja); } } catch (Exception ex) { return "[]"; //return ex.Message; } } /// <summary> /// 保存灯设备点数据 /// </summary> /// <param name="_redisBasketRepository"></param> /// <param name="spotModel"></param> /// <returns></returns> [HttpPost("SaveDevSpotValueByRedis")] [AllowAnonymous] public async Task<ApiResult> SaveLightDevSpotValueByRedis([FromServices] IRedisBasketRepository _redisBasketRepository, LightDevSpotList spotModel) { if (spotModel == null || string.IsNullOrWhiteSpace(spotModel.ModuleId)) { return new ApiResult(ReturnCode.GeneralError); } try { await _redisBasketRepository.Set(RedisKey.Light_Dev_ + spotModel.ModuleId, spotModel, new TimeSpan(4, 20, 33)); //var content = await _redisBasketRepository.GetValue(RedisKey.Light_Dev_ + spotModel.ModuleId); //if (content == null) // content = await _redisBasketRepository.Set(RedisKey.Light_Dev_ + spotModel.ModuleId, spotModel,new TimeSpan(360000)); //else //{ // LightDevSpotList lightDevSpotList = JsonConvert.DeserializeObject<LightDevSpotList>(content); // LightDevSpot lightDevSpot = lightDevSpotList.LightDevSpots.Where(x=>x.Name == spotModel.Name).FirstOrDefault(); // if (lightDevSpot != null) // { // lightDevSpotList.LightDevSpots.Remove(lightDevSpot); // } // lightDevSpotList.LightDevSpots.Add(spotModel); //} return new ApiResult(ReturnCode.Success); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 取灯设备点数据 /// </summary> /// <param name="_redisBasketRepository"></param> /// <param name="moduleId">模块ID</param> /// <returns></returns> [HttpGet("GetLightDevSpotValueByRedis/{moduleId}")] [AllowAnonymous] public async Task<ApiResult> GetLightDevSpotValueByRedis([FromServices] IRedisBasketRepository _redisBasketRepository, string moduleId) { if (string.IsNullOrEmpty(moduleId)) { return new ApiResult(ReturnCode.GeneralError); } try { var content = await _redisBasketRepository.GetValue(RedisKey.Light_Dev_ + moduleId); //var v = JsonConvert.DeserializeObject<JObject>(content); return new ApiResult<string>(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 通过devId获取云组态设备点历史值 /// </summary> /// <param name="devId"></param> /// <returns></returns> [HttpGet("GetWebScadaDevSpotHisData/{devId}")] [AllowAnonymous] public async Task<ApiResult> GetWebScadaDevSpotHisData(string devId) { if (string.IsNullOrEmpty(devId)) { return new ApiResult(ReturnCode.GeneralError); } try { var content = await _TdevWebScadaDevSpotService.GetWebScadaDevSpotHisData(devId); return new ApiResult<string>(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 获取所有云组态设备点 /// </summary> /// <returns></returns> [HttpGet("GetWebScadaDevSpotsAsync")] public async Task<ApiResult> GetWebScadaDevSpotsAsync() { try { var contentList = await _TdevWebScadaDevSpotService.GetAllAsync(); return new ApiResult<IEnumerable<TdevWebScadaDevSpotViewModel>>(contentList); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 通过云组态设备点名称条件查询 /// </summary> /// <param name="searchModel"></param> /// <returns></returns> [HttpPost("GetWebScadaDevSpotsByAsync")] public async Task<ApiResult> GetWebScadaDevSpotsByAsync(TdevWebScadaDevSpotSearchModel searchModel) { if (searchModel == null) { return new ApiResult(ReturnCode.ArgsError); } searchModel.IsPagination = false; try { var contentList = await _TdevWebScadaDevSpotService.GetConditionAsync(searchModel); return new ApiResult<PagesModel<TdevWebScadaDevSpotViewModel>>(new PagesModel<TdevWebScadaDevSpotViewModel>(contentList, searchModel)); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 创建云组态设备点 /// </summary> /// <param name="content"></param> /// <returns></returns> [HttpPost("CreateWebScadaDevSpotAsync")] public async Task<ApiResult> CreateWebScadaDevSpotAsync(TdevWebScadaDevSpotCreateModel content) { if (content == null) { return new ApiResult(ReturnCode.ArgsError); } try { await _TdevWebScadaDevSpotService.CreateAsync(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// <summary> /// 删除云组态设备点 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpDelete("DeleteWebScadaDevSpotAsync/{id}")] public async Task<ApiResult> DeleteWebScadaDevSpotAsync(string id) { if (string.IsNullOrEmpty(id)) { return new ApiResult(ReturnCode.GeneralError); } try { await _TdevWebScadaDevSpotService.DeleteAsync(id); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// <summary> /// 删除云组态设备点【真删除】 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpDelete("DeleteWebScadaDevSpotDataAsync/{id}")] public async Task<ApiResult> DeleteWebScadaDevSpotDataAsync(string id) { if (string.IsNullOrEmpty(id)) { return new ApiResult(ReturnCode.GeneralError); } try { await _TdevWebScadaDevSpotService.DeleteDataAsync(id); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// <summary> /// 更新云组态设备点 /// </summary> /// <param name="id"></param> /// <param name="updateModel"></param> /// <returns></returns> [HttpPut("UpdateWebScadaDevSpotAsync/{id}")] public async Task<ApiResult> UpdateWebScadaDevSpotAsync(string id, TdevWebScadaDevSpotUpdateModel updateModel) { if (string.IsNullOrEmpty(id)) { return new ApiResult(ReturnCode.GeneralError); } try { await _TdevWebScadaDevSpotService.UpdateAsync(id, updateModel); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// <summary> /// 更新云组态设备点顺序 /// </summary> /// <param name="ids"></param> /// <returns></returns> [HttpPut("UpdateSortAsync")] public async Task<ApiResult> UpdateSortAsync(IEnumerable<string> ids) { if (null == ids) { return new ApiResult(ReturnCode.GeneralError); } try { await _TdevWebScadaDevSpotService.UpdateSortAsync(ids); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// <summary> /// 判断设备是否已经存在 /// </summary> /// <param name="model"></param> /// <returns></returns> [HttpPost("devBoxIsExist")] public async Task<ApiResult> devBoxIsExist(TdevWebScadaDevSpotIsExist model) { var data = false; if (null == model) { return new ApiResult(ReturnCode.GeneralError); } try { data=await _TdevWebScadaDevSpotService.devBoxIsExist(model); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult<object>(data); } /// <summary> /// 获取设备网关关联列表 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpGet("GetdevDevBoxConditionAsync/{id}")] [AllowAnonymous] public async Task<ApiResult> GetdevDevBoxConditionAsync(string id) { if (string.IsNullOrEmpty(id)) { return new ApiResult(ReturnCode.GeneralError); } try { DevBoxSearchModel searchModel = new DevBoxSearchModel(); searchModel.C_DevStoreCode = id; var content = await _TdevWebScadaDevSpotService.GetdevDevBoxConditionAsync(searchModel); return new ApiResult<IEnumerable<DevBoxViewModel>>(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } } }