123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557 |
- 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);
- }
- }
- }
- }
|