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 _logger { get; } private readonly ITdevWebScadaDevSpotService _TdevWebScadaDevSpotService; private readonly IPushMsgService _pushMsgService; /// /// 构造函数 /// /// /// /// public TdevWebScadaDevSpotController(ITdevWebScadaDevSpotService TdevWebScadaDevSpotService, IPushMsgService pushMsgService, ILogger logger) { _TdevWebScadaDevSpotService = TdevWebScadaDevSpotService; _logger = logger; _pushMsgService = pushMsgService; } /// /// 通过id获取云组态设备点信息 /// /// /// [HttpGet("GetWebScadaDevSpotAsync/{id}")] [AllowAnonymous] public async Task 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(content.FirstOrDefault()); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 通过devId获取云组态信息 /// /// /// [HttpGet("GetWebScadaByAsync/{devId}")] [AllowAnonymous] public async Task GetWebScadaByAsync(string devId) { if (string.IsNullOrEmpty(devId)) { return new ApiResult(ReturnCode.GeneralError); } try { var content = await _TdevWebScadaDevSpotService.GetWebScadaByAsync(devId); return new ApiResult(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 通过devId获取云组态设备点值 /// /// /// [HttpGet("GetWebScadaDevSpotValue/{devId}")] [AllowAnonymous] public async Task GetWebScadaDevSpotValue(string devId) { if (string.IsNullOrEmpty(devId)) { return new ApiResult(ReturnCode.GeneralError); } try { var content = await _TdevWebScadaDevSpotService.GetWebScadaDevSpotValue(devId); Dictionary msgAlarmDic = FanyiHelper.msgAlarmDic; foreach (KeyValuePair 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(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 测试Redis消息队列 /// /// /// /// [HttpGet("GetWebScadaDevSpotValueByRedis/{devId}")] [AllowAnonymous] public async Task 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(content); return new ApiResult(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 取设备值 /// /// /// /// [HttpGet("GetWebScadaDevSpotsByRedis/{devId}")] [AllowAnonymous] public async Task 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(content); //var list = jo["device"]?.ToObject>(); var jo = JsonConvert.DeserializeObject(content); var list = jo?.ToObject(); return new ApiResult(list); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 取设备值,直接返回设备值 /// /// /// /// [HttpGet("GetWebScadaDevValueByRedis/{devId}")] [AllowAnonymous] public async Task 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(content); //var list = jo["device"]?.ToObject>(); var jo = JsonConvert.DeserializeObject(content); var list = jo?.ToObject(); return list; } catch (Exception ex) { return null; } } /// /// 大屏设备点数据 /// /// /// /// [HttpGet("GetWebScadaDevSpotValueForLargeScreenByRedis/{devId}")] [AllowAnonymous] public async Task 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(content); string strContent = JsonConvert.SerializeObject(jObjectContent["device"]); if (string.IsNullOrEmpty(strContent)) return "[]"; else { JArray ja = JsonConvert.DeserializeObject(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; } } /// /// 保存灯设备点数据 /// /// /// /// [HttpPost("SaveDevSpotValueByRedis")] [AllowAnonymous] public async Task 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(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); } } /// /// 取灯设备点数据 /// /// /// 模块ID /// [HttpGet("GetLightDevSpotValueByRedis/{moduleId}")] [AllowAnonymous] public async Task 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(content); return new ApiResult(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 通过devId获取云组态设备点历史值 /// /// /// [HttpGet("GetWebScadaDevSpotHisData/{devId}")] [AllowAnonymous] public async Task GetWebScadaDevSpotHisData(string devId) { if (string.IsNullOrEmpty(devId)) { return new ApiResult(ReturnCode.GeneralError); } try { var content = await _TdevWebScadaDevSpotService.GetWebScadaDevSpotHisData(devId); return new ApiResult(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 获取所有云组态设备点 /// /// [HttpGet("GetWebScadaDevSpotsAsync")] public async Task GetWebScadaDevSpotsAsync() { try { var contentList = await _TdevWebScadaDevSpotService.GetAllAsync(); return new ApiResult>(contentList); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 通过云组态设备点名称条件查询 /// /// /// [HttpPost("GetWebScadaDevSpotsByAsync")] public async Task GetWebScadaDevSpotsByAsync(TdevWebScadaDevSpotSearchModel searchModel) { if (searchModel == null) { return new ApiResult(ReturnCode.ArgsError); } searchModel.IsPagination = false; try { var contentList = await _TdevWebScadaDevSpotService.GetConditionAsync(searchModel); return new ApiResult>(new PagesModel(contentList, searchModel)); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 创建云组态设备点 /// /// /// [HttpPost("CreateWebScadaDevSpotAsync")] public async Task 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); } /// /// 删除云组态设备点 /// /// /// [HttpDelete("DeleteWebScadaDevSpotAsync/{id}")] public async Task 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); } /// /// 删除云组态设备点【真删除】 /// /// /// [HttpDelete("DeleteWebScadaDevSpotDataAsync/{id}")] public async Task 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); } /// /// 更新云组态设备点 /// /// /// /// [HttpPut("UpdateWebScadaDevSpotAsync/{id}")] public async Task 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); } /// /// 更新云组态设备点顺序 /// /// /// [HttpPut("UpdateSortAsync")] public async Task UpdateSortAsync(IEnumerable 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); } /// /// 判断设备是否已经存在 /// /// /// [HttpPost("devBoxIsExist")] public async Task 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(data); } /// /// 获取设备网关关联列表 /// /// /// [HttpGet("GetdevDevBoxConditionAsync/{id}")] [AllowAnonymous] public async Task 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>(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } } }