AndonUpMom.cs 5.34 KB
using Hh.Mes.Common.config;
using Hh.Mes.Common.log;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.ApiEntity;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Response;
using Newtonsoft.Json;
using NPOI.POIFS.FileSystem;
using SqlSugar;
using System;


namespace Hh.Mes.Service.ApiService
{
    public partial class UpstreamService
    {
        /// <summary>
        /// ANDON信息反馈
        /// </summary>
        /// <remarks>
        /// <para><a href="https://kdocs.cn/l/caBR6DBb2LpP">MOM-产线中控接口文档</a></para>
        /// <para><a href="https://www.bejson.com/">Josn在线解析工具</a></para>
        /// </remarks>
        /// <returns><see cref="Response"/></returns>
        public Response AndonUpMom(AndonUpMomEntity andonUpMomEntity)
        {
            var response = new Response();

            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var resultToken = GetMomToken();
                if (!resultToken.Item1) return response.ResponseError(resultToken.Item2);

                var urlDic = "UrlAndonUpMom" + ConfigRead.GetInstance.GetAppsetConnection().AppCustomExtend1;
                var url = GetDictionaryDictValue(urlDic, "GetUrl");
                //var url = "http://10.77.101.247:51001/mom/intf/intfMbosLineTaskAndon/receive";

                #region 测试数据
                //var sendData = new AndonUpMomEntity();
                //var item = new AndonUpMomDetail()
                //{
                //    id = "01",
                //    line_code = "TZTTHW",
                //    device_information = "xxx",
                //    device_fault_type = "xxx",
                //    device_fault_description = "xxx",
                //    device_status = "1",
                //    start_time = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd HH:mm:ss"),
                //    end_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                //    ylzd1 = "1",
                //    type = EnumAction.I.ToString()
                //};
                //sendData.andon_list.Add(item);
                #endregion

                var json = JsonConvert.SerializeObject(andonUpMomEntity);
                //var json = "{\"andon_list\":[{\"id\":\"01\",\"line_code\":\"zjqzzz\",\"device_information\":\"xxx\",\"device_fault_type\":\"xxx\",\"device_fault_description\":\"xxx\",\"device_status\":\"1\",\"start_time\":\"2008-02-21 12:00:00\",\"end_time\":\"2014-02-21 12:00:00\",\"ylzd1\":\"1\",\"type\":\"i\"}]}";

                response = HttpMan(url, json, EnumLog.ANDON报警.ToString(), token: resultToken.Item2);
                if (response.Code == 200)
                {
                    return response.ResponseSuccess();
                }
                //Log4NetHelper.Instance.Error($"【MOM返回结果】" + response.Result);
                MOMResult momResult = JsonConvert.DeserializeObject<MOMResult>(response.Result);
                response.Message = $"【MOM】PLMEID:{momResult.PLMEID},错误原因:{momResult.MSG},STATUS:{momResult.STATUS}";
                response.Result = "";
                return response;
            });
        }


        /// <summary>
        /// ANDON信息反馈
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <returns><see cref="Response"/></returns>
        public Response AndonUpMomNew()
        {
            var response = new Response();

            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var url = GetDictionaryDictValue("UrlAndonUpMom_off", "GetUrl");

                #region 数据

                // 获取当前时间并计算1小时之后的时间点
                DateTime nowTime = DateTime.Now;
                DateTime endTime = nowTime.AddHours(-1);

                var equipmentalarmList= Context.Queryable<wcs_equipmentalarm>().Where(it => it.createTime >= endTime && it.createTime <= nowTime).ToList();
                if (equipmentalarmList.Count==0)
                    return response.ResponseError($"没有可以上报报警信息!");

                var sendData = new AndonUpMomEntity();
                foreach (var item in equipmentalarmList)
                {
                    var andonUpMomDetail = new AndonUpMomDetail()
                    {
                        plmeid = item.id.ToString(),
                        line_code = "Line4",
                        device_information_name = item.equipmentName,
                        device_information_code = item.equipmentCode,
                        device_fault_type = item.equipmentPropCode,
                        device_fault_description = item.equipmentPropName,
                        device_status = item.isEnd ? 1 : 0,
                        start_time = item.createTime.ToString(),
                        end_time = item.updateTime.ToString(),
                        type = EnumAction.I.ToString()
                    };
                    sendData.andon_list.Add(andonUpMomDetail);
                }

                #endregion
                //var json = JsonConvert.SerializeObject(sendData);
                response = HttpMan(url, sendData, EnumLog.ANDON报警.ToString(), method: "post");
                if (response.Code == 200) {
                    response.Message = "上传设备报警信息成功!";
                }

                return response;
            });
        }

    }
}