Example #1
0
 public HardwareType Resolve(Hardware source, HardwareDto destination, HardwareType destMember, ResolutionContext context)
 {
     return(source switch
     {
         Cabinet e => HardwareType.Cabinet,
         ARM e => HardwareType.ARM,
         Flowmeter e => HardwareType.Flowmeter,
         Pressure e => HardwareType.Pressure,
         DiffPressure e => HardwareType.DiffPressure,
         Temperature e => HardwareType.Temperature,
         GasAnalyzer e => HardwareType.GasAnalyzer,
         FireSensor e => HardwareType.FireSensor,
         FlowComputer e => HardwareType.FlowComputer,
         PLC e => HardwareType.PLC,
         APC e => HardwareType.APC,
         NetworkHardware e => HardwareType.Network,
         Valve e => HardwareType.Valve,
         InformPanel e => HardwareType.InformPanel,
         FireModule e => HardwareType.FireModule,
         _ => HardwareType.Valve,
     });
        public IEnumerable <object> Get(string key, string VisitPage, string IpAddress, string Address)
        {
            try
            {
                WebInfo web = work.CreateRepository <WebInfo>().GetFirst(m => m.WebKey == key);
                if (web != null)
                {
                    #region 获取访客信息&添加访客信息
                    //搜索引擎来源
                    var se = System.Web.HttpContext.Current.Request.UserAgent;
                    //获取浏览器信息
                    //火狐浏览器
                    if (se.Contains("Firefox"))
                    {
                        se = "火狐";
                    }
                    //谷歌浏览器
                    else if (se.Contains("Chrome"))
                    {
                        se = "谷歌";
                    }
                    //Safari浏览器(苹果浏览器)
                    else if (se.Contains("Version"))
                    {
                        se = "苹果";
                    }
                    //Opera浏览器
                    else if (se.Contains("Opera"))
                    {
                        se = "Opera";
                    }
                    //LBBROWSER浏览器(猎豹)
                    else if (se.Contains("LBBROWSER"))
                    {
                        se = "猎豹";
                    }
                    //sougou浏览器(sougou)
                    else if (se.Contains("MetaSr"))
                    {
                        se = "搜狗";
                    }
                    //Maxthon浏览器(傲游)
                    else if (se.Contains("Maxthon"))
                    {
                        se = "傲游";
                    }
                    //因特网浏览器
                    else
                    {
                        se = "未知";
                    }
                    VisitorInfo vist = new VisitorInfo();
                    vist.AccessTime    = DateTime.Now;
                    vist.VisitPage     = VisitPage;
                    vist.IpAddress     = IpAddress;
                    vist.VisitSE       = se;
                    vist.WebInfo       = web;
                    vist.Address       = Address;
                    vist.Age           = 0;
                    vist.AccessEndTime = DateTime.Now;

                    //判断用户是否访问一个就退出
                    if (IsPostUrl == string.Empty)
                    {
                        IsPostUrl = VisitPage;
                    }
                    if (IsPostUrl != VisitPage)
                    {
                        vist.PageNumber = 1;
                    }
                    //随机生成标识码(identification code) 32位字符
                    string IC = Guid.NewGuid().ToString("N");
                    var    bo = work.CreateRepository <VisitorInfo>().GetList(m => m.IC == IC);
                    if (bo.Count() > 0)
                    {
                        IC = Guid.NewGuid().ToString("N");
                    }
                    vist.IC = IC;

                    //判断用户是否相同用户
                    List <VisitorInfo> alikeCount = work.CreateRepository <VisitorInfo>().GetList(
                        m => m.IpAddress == vist.IpAddress
                        ).ToList();
                    //通过ip地址保证访客数计算
                    if (alikeCount.Count() == 0)
                    {
                        work.CreateRepository <VisitorInfo>().Insert(vist);
                        work.Save();
                        List <VisitorInfo> list = work.CreateRepository <VisitorInfo>().GetList().ToList();
                        var model = list.Where(p => p.IC == IC).FirstOrDefault();
                        int id    = 0; id = model.Id;
                        HttpContext.Current.Session["id"] = id;
                    }
                    else
                    {
                        foreach (var item in alikeCount)
                        {
                            TimeSpan span = DateTime.Now - item.AccessEndTime;
                            //判断该ip地址访客访问时间是否超过24小时
                            int temp = Convert.ToInt32(span.TotalHours);
                            if (temp >= 24 && item.Lock == 0)
                            {
                                item.Lock = 1;
                                work.CreateRepository <VisitorInfo>().Update(item);
                                work.Save();
                                work.CreateRepository <VisitorInfo>().Insert(vist);
                                work.Save();
                                List <VisitorInfo> list = work.CreateRepository <VisitorInfo>().GetList().ToList();
                                var model = list.Where(p => p.IC == IC).FirstOrDefault();
                                int id    = 0; id = model.Id;
                                HttpContext.Current.Session["id"] = id;
                            }
                        }
                    }

                    #endregion

                    #region 获取PV信息&添加PV信息
                    FlowComputer flow = new FlowComputer();
                    flow.VisitPage = VisitPage;
                    flow.WebHost   = System.Web.HttpContext.Current.Request.Url.Host.ToString();
                    string str = HttpContext.Current.Request.UrlReferrer.ToString();
                    //判断是否搜索引擎链接
                    if (searchkey.IsSearchEnginesGet(str))
                    {
                        //取得搜索关键字
                        flow.SearchTerms = searchkey.SearchKey(str);
                        //取得搜索引擎名称
                        flow.VisitSE = searchkey.EngineName;
                    }
                    else
                    {
                        flow.SearchTerms = "其他";
                        flow.VisitSE     = vist.VisitSE;
                    }
                    flow.CurrentTime = DateTime.Now;
                    flow.WebInfo     = web;
                    lock (flow)
                    {
                        work.CreateRepository <FlowComputer>().Insert(flow);
                        work.Save();
                    }
                    #endregion

                    //web = work.CreateRepository<WebInfo>().GetFirst(m => m.WebKey == key);//获取pv
                    web.WebPv = web.WebPv + 1;
                    web.WebUv = work.CreateRepository <VisitorInfo>().GetCount(m => m.WebInfo.Id == web.Id);
                    int     rate       = work.CreateRepository <VisitorInfo>().GetCount(m => m.PageNumber == 0);
                    decimal rateResult = Math.Round((decimal)rate / web.WebPv, 4);
                    if (rateResult >= 1)
                    {
                        rateResult = 1;
                    }
                    web.BounceRate = (rateResult * 100).ToString().Length >= 5 ? (rateResult * 100).ToString().Substring(0, 4) + "%" : (rateResult * 100).ToString() + "%";
                    List <VisitorInfo> webuv = work.CreateRepository <VisitorInfo>().GetList().ToList();//获取uv
                    for (int i = 0; i < webuv.Count(); i++)
                    {
                        for (int j = webuv.Count() - 1; j > i; j--)
                        {
                            if (webuv[i].IpAddress == webuv[j].IpAddress)
                            {
                                webuv.RemoveAt(j);
                            }
                        }
                    }
                    web.IpCount = webuv.Count;//获取ip数 去重
                    lock (web)
                    {
                        work.CreateRepository <WebInfo>().Update(web);
                        work.Save();
                    }
                }
                else
                {
                    //var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                    //{
                    //    Content = new StringContent(string.Format("没有找到Key={0}的密钥", key)),
                    //    ReasonPhrase = "object is not found"
                    //};
                    //throw new HttpResponseException(resp);
                }
            }
            catch (Exception ex)
            {
                //var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                //{
                //    Content = new StringContent(string.Format("没有找到Key={0}的密钥", key)),
                //    ReasonPhrase = "object is not found"
                //};
                //throw new HttpResponseException(resp);
                return(new object[] { "" + ex });

                throw ex;
            }
            return(new object[] { "持行成功" });
        }
        public async Task <int> Handle(CreateHardwareCommand request, CancellationToken cancellationToken)
        {
            var contract = await _context.Contracts.FirstOrDefaultAsync(c => c.Id == request.ContractId);

            if (contract == null)
            {
                throw new NullReferenceException("Договор с таким Id не найден.");
            }

            Hardware entity;

            switch (request.HardwareType)
            {
            case HardwareType.Cabinet:
                entity = new Cabinet
                {
                    Position      = request.Position,
                    SerialNumber  = request.SerialNumber,
                    Constructed   = (DateTime)request.Constructed,
                    ConstructedBy = request.ConstructedBy
                };
                break;

            case HardwareType.FlowComputer:
                entity = new FlowComputer
                {
                    Position        = request.Position,
                    SerialNumber    = request.SerialNumber,
                    DeviceModel     = request.DeviceModel,
                    IP              = request.IPAddress,
                    AssemblyVersion = request.AssemblyVersion,
                    CRC32           = request.CRC32,
                    LastConfigDate  = request.LastConfigDate
                };
                break;

            case HardwareType.Flowmeter:
                entity = new Flowmeter
                {
                    Position     = request.Position,
                    SerialNumber = request.SerialNumber,
                    DeviceModel  = request.DeviceModel,
                    DeviceType   = request.DeviceType,
                    MinValue     = request.MinValue,
                    MaxValue     = request.MaxValue,
                    EU           = request.EU,
                    Kfactor      = request.KFactor,
                    SignalType   = request.SignalType,
                    Settings     = new ModbusSettings
                    {
                        Address  = request.ModbusSettings.Address,
                        BoudRate = request.ModbusSettings.BoudRate,
                        DataBits = request.ModbusSettings.DataBits,
                        Parity   = Enum.GetName(typeof(Parity), request.ModbusSettings.Parity),
                        StopBit  = request.ModbusSettings.StopBit
                    }
                };
                break;

            case HardwareType.Network:
                entity = new NetworkHardware
                {
                    Position     = request.Position,
                    SerialNumber = request.SerialNumber,
                    DeviceType   = request.DeviceType,
                    DeviceModel  = request.DeviceModel,
                    Mask         = request.Mask
                };
                foreach (var item in request.NetworkDevices)
                {
                    ((NetworkHardware)entity).NetworkDevices.Add(new NetworkDevice
                    {
                        IP         = item.IP,
                        MacAddress = item.MacAddress,
                        Name       = item.Name
                    });
                }
                break;

            case HardwareType.PLC:
                entity = new PLC
                {
                    Position        = request.Position,
                    SerialNumber    = request.SerialNumber,
                    DeviceModel     = request.DeviceModel,
                    IP              = request.IPAddress,
                    AssemblyVersion = request.AssemblyVersion
                };
                break;

            case HardwareType.Pressure:
                entity = new Pressure
                {
                    Position     = request.Position,
                    SerialNumber = request.SerialNumber,
                    DeviceModel  = request.DeviceModel,
                    DeviceType   = request.DeviceType,
                    MinValue     = request.MinValue,
                    MaxValue     = request.MaxValue,
                    EU           = request.EU,
                    SignalType   = request.SignalType
                };
                break;

            case HardwareType.Temperature:
                entity = new Temperature
                {
                    Position     = request.Position,
                    SerialNumber = request.SerialNumber,
                    DeviceModel  = request.DeviceModel,
                    DeviceType   = request.DeviceType,
                    MinValue     = request.MinValue,
                    MaxValue     = request.MaxValue,
                    EU           = request.EU,
                    SignalType   = request.SignalType
                };
                break;

            case HardwareType.DiffPressure:
                entity = new DiffPressure
                {
                    Position     = request.Position,
                    SerialNumber = request.SerialNumber,
                    DeviceModel  = request.DeviceModel,
                    DeviceType   = request.DeviceType,
                    MinValue     = request.MinValue,
                    MaxValue     = request.MaxValue,
                    EU           = request.EU,
                    SignalType   = request.SignalType
                };
                break;

            case HardwareType.GasAnalyzer:
                entity = new GasAnalyzer
                {
                    Position     = request.Position,
                    SerialNumber = request.SerialNumber,
                    DeviceModel  = request.DeviceModel,
                    DeviceType   = request.DeviceType,
                    MinValue     = request.MinValue,
                    MaxValue     = request.MaxValue,
                    EU           = request.EU,
                    SignalType   = request.SignalType
                };
                break;

            case HardwareType.InformPanel:
                entity = new InformPanel
                {
                    Position     = request.Position,
                    SerialNumber = request.SerialNumber,
                    DeviceModel  = request.DeviceModel,
                    DeviceType   = request.DeviceType,
                    PanelType    = request.PanelType
                };
                break;

            case HardwareType.FireSensor:
                entity = new FireSensor
                {
                    Position     = request.Position,
                    SerialNumber = request.SerialNumber,
                    DeviceModel  = request.DeviceModel,
                    DeviceType   = request.DeviceType
                };
                break;

            case HardwareType.FireModule:
                entity = new FireModule
                {
                    Position     = request.Position,
                    SerialNumber = request.SerialNumber,
                    DeviceType   = request.DeviceType
                };
                break;

            case HardwareType.Valve:
                entity = new Valve
                {
                    Position     = request.Position,
                    SerialNumber = request.SerialNumber,
                    DeviceType   = request.DeviceType,
                    DeviceModel  = request.DeviceModel,
                    SignalType   = request.SignalType,
                    Settings     = new ModbusSettings
                    {
                        Address  = request.ModbusSettings.Address,
                        BoudRate = request.ModbusSettings.BoudRate,
                        DataBits = request.ModbusSettings.DataBits,
                        Parity   = Enum.GetName(typeof(Parity), request.ModbusSettings.Parity),
                        StopBit  = request.ModbusSettings.StopBit
                    }
                };
                break;

            case HardwareType.ARM:
                entity = new ARM
                {
                    Position     = request.Position,
                    Name         = request.ArmName,
                    SerialNumber = request.SerialNumber,
                    Monitor      = request.Monitor,
                    MonitorSN    = request.MonitorSN,
                    Keyboard     = request.Keyboard,
                    KeyboardSN   = request.KeyboardSN,
                    Mouse        = request.Mouse,
                    MouseSN      = request.MouseSN,
                    OS           = request.OS,
                    ProductKeyOS = request.ProductKeyOS,
                    HasRAID      = request.HasRAID
                };
                foreach (var item in request.NetworkAdapters)
                {
                    ((ARM)entity).NetworkAdapters.Add(new NetworkAdapter
                    {
                        IP         = item.IP,
                        MacAddress = item.MacAddress
                    });
                }
                break;

            case HardwareType.APC:
                entity = new APC
                {
                    Position     = request.Position,
                    SerialNumber = request.SerialNumber,
                    DeviceModel  = request.DeviceModel,
                    DeviceType   = request.DeviceType
                };
                break;

            default:
                entity = null;
                break;
            }

            contract.HardwareList.Add(entity);
            contract.HasProtocol = false;
            _context.Update(contract);

            try
            {
                await _context.SaveChangesAsync(cancellationToken);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(entity.Id);
        }