public override void Handle(T808ProtocolPackage package)
        {
            package.Datas = new List<EQueueData>();

            using (var reader = new BytesReader(package.RawData))
            {
                //移动索引到消息体
                reader.MoveIndex(package.BodyIndex);
                //数据项个数
                var count = reader.ReadInt16();
                //位置数据类型
                var type = reader.Read();
                for (var i = 0; i < count; i++)
                {
                    var data = new EQueueData();

                    //跳过报警数据
                    reader.MoveIndex(4);

                    var gpsStatus = (EnumGpsStatus)reader.ReadInt32();

                    if (gpsStatus.HasFlag(EnumGpsStatus.AccIsOn))
                    {
                        data.IsAccOn = true;
                    }
                    if (gpsStatus.HasFlag(EnumGpsStatus.GpsIsLocated))
                    {
                        data.IsLocated = true;
                    }
                    if (gpsStatus.HasFlag(EnumGpsStatus.SLat))
                    {
                        data.IsSLat = true;
                    }

                    if (gpsStatus.HasFlag(EnumGpsStatus.WLon))
                    {
                        data.IsWLon = true;
                    }
                    if (gpsStatus.HasFlag(EnumGpsStatus.OilBreak) || gpsStatus.HasFlag(EnumGpsStatus.PowerBreak))
                    {
                        data.IsOilOrPowerBreak = true;
                    }

                    if (gpsStatus.HasFlag(EnumGpsStatus.Encrypted))
                    {
                        data.IsSos = true;
                    }
                    data.Latitude = reader.ReadInt32() / 1000000d;
                    data.Longitude = reader.ReadInt32() / 1000000d;
                    data.Height = reader.ReadInt16();
                    data.Speed = reader.ReadInt16() / 10.00;
                    data.Direction = reader.ReadInt16();
                    data.ReportTime = reader.ReadDateTimeByBCD();
                    package.Datas.Add(data);
                }
            }
            SendCommonResponse(package);
        }
Exemple #2
0
 public void Send(EQueueData data)
 {
     try
     {
         if (!_cts.IsCancellationRequested)
         {
             _buffer.Add(data);
         }
     }
     catch (Exception ex)
     {
         _logger.Error(ex);
     }
 }
        protected override void Handle(EQueueData data)
        {
            if (data.ReportTime < DateTime.Now.AddMinutes(-5))
            {
                return;
            }

            var device = GetDeviceByDeviceGpsCodeFromCache(data.DeviceGpsCode);
            if (device == null)
            {
                _logger.WarnFormat("处理报警[{0}]时,未找到当前GpsCode[{1}]所关联的设备!", Name, data.DeviceGpsCode);
                return;
            }

            //当前配置
            var setting = GetAlarmSettingFromCache<EAlarmSettingTired>(device.Id);

            if (setting == null || !setting.IsEnable)
            {
                return;
            }

            //获取当前状态
            var current = GetVDeviceCurrentDataStatusDetailFromCache(device.Id);

            if (current == null)
            {
                return;
            }

            if (data.Speed > 0)
            {

                if (!current.IsTiredAlarm)
                {
                    //行驶中
                    if (!current.IsTiredAlarmDetailObject.StartTime.HasValue)
                    {
                        //开始计时
                        current.IsTiredAlarmDetailObject.StartTime = DateTime.Now;
                        current.IsTiredAlarmDetailObject.StartRestTime = null;
                        return;
                    }

                    if (current.IsTiredAlarmDetailObject.StartRestTime.HasValue)
                    {
                        //如果是中途休息 时间不够 的时候 又开始 驾车 那么把 开始休息时间点 重置 让他再开始休息
                        current.IsTiredAlarmDetailObject.StartRestTime = null;
                        return;
                    }

                    if (current.IsTiredAlarmDetailObject.StartTime > DateTime.Now.AddMinutes(-setting.Duration))
                    {
                        //未达到持续时间
                        return;
                    }

                    //设置当前报警状态 为疲劳驾驶
                    current.IsTiredAlarm = true;
                    //记录本次报警详细信息
                    current.IsTiredAlarmDetail = current.IsTiredAlarmDetailObject.ToJson();
                    //保存疲劳驾驶记录当前数据表
                    _rep.Save(new EDeviceCurrentData()
                    {
                        IsTiredAlarm = current.IsTiredAlarm,
                        IsTiredAlarmDetail = current.IsTiredAlarmDetail
                    }, p => p.DeviceId == device.Id, p => new Columns(p.IsTiredAlarm, p.IsTiredAlarmDetail));

                    //获取报警地址
                    var location = GetLocationByWgs84(new EMapPoint(data.Latitude, data.Longitude));
                    var alarmReport = new EAlarmReportTired()
                    {
                        DeviceId = device.Id,
                        ReportTime = data.ReportTime,
                        ReceiveTime = data.ReceiveTime,
                        AlarmAddress = location.Address,
                        StartTime = current.IsTiredAlarmDetailObject.StartTime,
                        EndTime = DateTime.Now
                    };
                    //添加疲劳驾驶报警记录
                    _rep.Add(alarmReport);
                    var message = string.Format("车辆[{0}][{1}]在{2},疲劳驾驶!时间段[{3},{4}]!", device.DeviceName, alarmReport.ReportTime, alarmReport.AlarmAddress, alarmReport.StartTime, alarmReport.EndTime);
                    //触发邮件、短信、站内信提醒
                    RiseAlarm(device.UserId, "疲劳驾驶", message, setting);
                }
            }
            else
            {
                if (!current.IsTiredAlarmDetailObject.StartRestTime.HasValue)
                {
                    current.IsTiredAlarmDetailObject.StartRestTime = DateTime.Now;
                    return;
                }

                if (current.IsTiredAlarmDetailObject.StartRestTime > DateTime.Now.AddMinutes(-setting.RestTimeDuration))
                {
                    //未达到持续时间
                    return;
                }

                if (current.IsTiredAlarm)
                {
                    current.IsTiredAlarm = false;
                    current.IsTiredAlarmDetailObject.StartTime = null;
                    current.IsTiredAlarmDetailObject.StartRestTime = null;
                    current.IsTiredAlarmDetail = current.IsTiredAlarmDetailObject.ToJson();
                    _rep.Save(new EDeviceCurrentData()
                    {
                        IsTiredAlarm = current.IsTiredAlarm,
                        IsTiredAlarmDetail = current.IsTiredAlarmDetail
                    }, p => p.DeviceId == device.Id, p => new Columns(p.IsTiredAlarm, p.IsTiredAlarmDetail));
                }
                else
                {
                    current.IsTiredAlarmDetailObject.StartTime = null;
                    current.IsTiredAlarmDetailObject.StartRestTime = null;
                }
            }
        }
        protected override void Handle(EQueueData data)
        {
            if (data.ReportTime < DateTime.Now.AddMinutes(-5))
            {
                return;
            }

            var device = GetDeviceByDeviceGpsCodeFromCache(data.DeviceGpsCode);
            if (device == null)
            {
                _logger.WarnFormat("处理报警[{0}]时,未找到当前GpsCode[{1}]所关联的设备!", Name, data.DeviceGpsCode);
                return;
            }

            //当前配置
            var setting = GetAlarmSettingFromCache<EAlarmSettingParkingNotStalled>(device.Id);

            if (setting == null || !setting.IsEnable)
            {
                return;
            }

            //获取当前状态
            var current = GetVDeviceCurrentDataStatusDetailFromCache(device.Id);

            if (current == null)
            {
                return;
            }

            if (data.IsAccOn && data.Speed == 0)
            {
                //停车未熄火 之前没有该报警
                if (!current.IsParkingNotStalledAlarm)
                {
                    if (!current.IsParkingNotStalledAlarmDetailObject.StartTime.HasValue)
                    {
                        //开始计时
                        current.IsParkingNotStalledAlarmDetailObject.StartTime = DateTime.Now;
                        return;
                    }

                    if (current.IsParkingNotStalledAlarmDetailObject.StartTime > DateTime.Now.AddMinutes(-setting.Duration))
                    {
                        //未达到持续时间
                        return;
                    }

                    //设置当前报警状态 为停车未熄火
                    current.IsParkingNotStalledAlarm = true;
                    //记录本次报警详细信息
                    current.IsParkingNotStalledAlarmDetail = current.IsParkingNotStalledAlarmDetailObject.ToJson();
                    //保存停车未熄火记录当前数据表
                    _rep.Save(new EDeviceCurrentData()
                    {
                        IsParkingNotStalledAlarm = current.IsParkingNotStalledAlarm,
                        IsParkingNotStalledAlarmDetail = current.IsParkingNotStalledAlarmDetail
                    }, p => p.DeviceId == device.Id,p => new Columns(p.IsParkingNotStalledAlarm, p.IsParkingNotStalledAlarmDetail));

                    //获取报警地址
                    var location = GetLocationByWgs84(new EMapPoint(data.Latitude, data.Longitude));
                    var alarmReport = new EAlarmReportParkingNotStalled
                    {
                        DeviceId = device.Id,
                        ReportTime = data.ReportTime,
                        ReceiveTime = data.ReceiveTime,
                        AlarmAddress = location.Address,
                        StartTime = current.IsParkingNotStalledAlarmDetailObject.StartTime
                    };
                    //添加停车未熄火报警记录
                    _rep.Add(alarmReport);
                    //记录本次报表的Id 用于统计停车未熄火 结束时间
                    current.IsParkingNotStalledAlarmDetailObject.ParkingNotStalledAlarmReportId = alarmReport.Id;
                    var message = string.Format("车辆[{0}][{1}]在{2},停车未熄火!", device.DeviceName, alarmReport.ReportTime,alarmReport.AlarmAddress);
                    //触发邮件、短信、站内信提醒
                    RiseAlarm(device.UserId, "停车未熄火", message, setting);
                }
            }
            else
            {
                //恢复正常
                if (current.IsParkingNotStalledAlarm)
                {
                    //记录持续时间
                    if (current.IsParkingNotStalledAlarmDetailObject.ParkingNotStalledAlarmReportId.HasValue)
                    {
                        var report = _rep.Get<EAlarmReportParkingNotStalled>(p => p.Id == current.IsParkingNotStalledAlarmDetailObject.ParkingNotStalledAlarmReportId);
                        if (report != null && report.StartTime.HasValue && report.StartTime > DateTime.Now.AddHours(-3))//3个小时内有效
                        {
                            report.EndTime = DateTime.Now;
                            _rep.Save(report, p => p.Id == report.Id, p => new Columns(p.EndTime));
                        }
                    }
                    //清除报警数据
                    current.IsParkingNotStalledAlarmDetailObject.ParkingNotStalledAlarmReportId = null;
                    current.IsParkingNotStalledAlarmDetailObject.StartTime = null;
                    current.IsParkingNotStalledAlarmDetail = current.IsParkingNotStalledAlarmDetailObject.ToJson();
                    current.IsParkingNotStalledAlarm = false;
                    _rep.Save(new EDeviceCurrentData()
                    {
                        IsParkingNotStalledAlarm = current.IsParkingNotStalledAlarm,
                        IsParkingNotStalledAlarmDetail = current.IsParkingNotStalledAlarmDetail
                    }, p => p.DeviceId == device.Id,p => new Columns(p.IsParkingNotStalledAlarm, p.IsParkingNotStalledAlarmDetail));
                }
                else
                {
                    current.IsParkingNotStalledAlarmDetailObject.ParkingNotStalledAlarmReportId = null;
                    current.IsParkingNotStalledAlarmDetailObject.StartTime = null;
                    current.IsParkingNotStalledAlarm = false;
                }
            }
        }
        protected override void Handle(EQueueData data)
        {
            var device = GetDeviceByDeviceGpsCodeFromCache(data.DeviceGpsCode);
            if (device == null)
            {
                _logger.WarnFormat("处理报警[{0}]时,未找到当前GpsCode[{1}]所关联的设备!", Name, data.DeviceGpsCode);
                return;
            }

            //当前配置
            var setting = GetAlarmSettingFromCache<EAlarmSettingMainPowerBreak>(device.Id);

            if (setting == null || !setting.IsEnable)
            {
                return;
            }

            //获取当前状态
            var current = GetVDeviceCurrentDataStatusDetailFromCache(device.Id);

            if (current == null)
            {
                return;
            }


            if (data.IsMainPowerBreak)
            {
                if (!current.IsMainPowerBreakDetailObject.StartTime.HasValue || current.IsMainPowerBreakDetailObject.StartTime < DateTime.Now.AddMinutes(-15))
                {
                    current.IsMainPowerBreak = true;
                    current.IsMainPowerBreakDetailObject.StartTime = DateTime.Now;
                    _rep.Save(new EDeviceCurrentData()
                    {
                        IsMainPowerBreak = current.IsMainPowerBreak,
                        IsMainPowerBreakDetail = current.IsMainPowerBreakDetailObject.ToJson()
                    }, p => p.DeviceId == device.Id, p => new Columns(p.IsMainPowerBreak, p.IsMainPowerBreakDetail));

                    //获取报警地址
                    var location = GetLocationByWgs84(new EMapPoint(data.Latitude, data.Longitude));
                    var alarmReport = new EAlarmReportMainPowerBreak()
                    {
                        DeviceId = device.Id,
                        ReportTime = data.ReportTime,
                        ReceiveTime = data.ReceiveTime,
                        AlarmAddress = location.Address,
                    };
                    //添加超速报警记录
                    _rep.Add(alarmReport);
                    var message = string.Format("车辆[{0}][{1}]在{2},主电源断开!", device.DeviceName, alarmReport.ReportTime, alarmReport.AlarmAddress);
                    //触发邮件、短信、站内信提醒
                    RiseAlarm(device.UserId, "主电源断开报警", message, setting);


                }
            }
            else
            {
                if (current.IsMainPowerBreak)
                {
                    current.IsMainPowerBreak = false;
                    current.IsMainPowerBreakDetailObject.StartTime = null;
                    _rep.Save(new EDeviceCurrentData()
                    {
                        IsMainPowerBreak = current.IsMainPowerBreak,
                        IsMainPowerBreakDetail = current.IsMainPowerBreakDetailObject.ToJson()
                    }, p => p.DeviceId == device.Id, p => new Columns(p.IsMainPowerBreak, p.IsMainPowerBreakDetail));
                }
            }

        }
        protected override void Handle(EQueueData data)
        {
            var device = GetDeviceByDeviceGpsCodeFromCache(data.DeviceGpsCode);
            if (device == null)
            {
                _logger.WarnFormat("处理报警[{0}]时,未找到当前GpsCode[{1}]所关联的设备!", Name, data.DeviceGpsCode);
                return;
            }

            //获取当前状态
            var current = GetVDeviceCurrentDataStatusDetailFromCache(device.Id);

            if (current == null)
            {
                return;
            }

            if (data.IsSos)
            {
                //已报警 之前没有报警
                //if (!current.IsSosAlarm || (current.IsSosAlarmDetail.HasValue() && current.IsSosAlarmDetail.ToDataTime() < DateTime.Now.AddMinutes(-5)))

                if (!current.IsSosAlarm)
                {
                    //设置当前报警状态 为已报警
                    current.IsSosAlarm = true;
                    //记录本次报警详细信息
                    current.IsSosAlarmDetail = DateTime.Now.ToString();
                    //保存超速记录当前数据表
                    _rep.Save(new EDeviceCurrentData()
                    {
                        IsSosAlarm = current.IsSosAlarm,
                        IsSosAlarmDetail = current.IsSosAlarmDetail
                    }, p => p.DeviceId == device.Id, p => new Columns(p.IsSosAlarm, p.IsSosAlarmDetail));


                    //获取报警地址
                    var location = GetLocationByWgs84(new EMapPoint(data.Latitude, data.Longitude));
                    var alarmReport = new EAlarmReportSos()
                    {
                        DeviceId = device.Id,
                        ReportTime = data.ReportTime,
                        ReceiveTime = data.ReceiveTime,
                        AlarmAddress = location.Address
                    };
                    //添加紧急报警记录
                    _rep.Add(alarmReport);
                    var message = string.Format("车辆[{0}][{1}]在{2},紧急报警[设备脱落或者用户按了SOS]!", device.DeviceName, alarmReport.ReportTime, alarmReport.AlarmAddress);
                    //触发邮件、短信、站内信提醒
                    RiseAlarm(device.UserId, "紧急报警", message, null);
                }
            }
            else
            {
                if (current.IsSosAlarm)
                {
                    //清除报警数据
                    current.IsSosAlarm = false;
                    //清除报警时间
                    current.IsSosAlarmDetail = string.Empty;
                    _rep.Save(new EDeviceCurrentData()
                    {
                        IsSosAlarm = current.IsSosAlarm,
                        IsSosAlarmDetail = current.IsSosAlarmDetail
                    }, p => p.DeviceId == device.Id, p => new Columns(p.IsSosAlarm, p.IsSosAlarmDetail));
                }
            }
        }
        protected override void Handle(EQueueData data)
        {

            var device = GetDeviceByDeviceGpsCodeFromCache(data.DeviceGpsCode);
            if (device == null)
            {
                _logger.WarnFormat("处理报警[{0}]时,未找到当前GpsCode[{1}]所关联的设备!", Name, data.DeviceGpsCode);
                return;
            }

            //当前配置
            var setting = GetAlarmSettingFromCache<EAlarmSettingInOutArea>(device.Id);

            if (setting == null || !setting.IsEnable || setting.ExtInOutAreaSetting.Count == 0)
            {
                return;
            }

            //获取当前状态
            var current = GetVDeviceCurrentDataStatusDetailFromCache(device.Id);

            if (current == null)
            {
                return;
            }

            foreach (var areaSettingItem in setting.ExtInOutAreaSetting)
            {
                var area = GetAreaByIdFromCache(areaSettingItem.Id);
                if (area == null || area.Area == null)
                {
                    //无效区域
                    continue;
                }
                var last = current.InOutAreaDetailObject.FirstOrDefault(p => p.Id == area.Id);
                var isIn = _mapService.PointIsInPolygon(area.Area.ToArray(), new EMapPoint(data.Latitude, data.Longitude));
                if (isIn)
                {
                    //如果现在这个区域
                    if (last == null && areaSettingItem.IsIn)
                    {
                        //如果之前不在这个区域 触发进入区域提示
                        
                        //在当前状态中添加进入区域信息
                        current.InOutAreaDetailObject.Add(new VDeviceCurrentDataStatusDetail.AreaItem
                        {
                            Id = area.Id,
                            Name = area.AreaName,
                            ReportTime = DateTime.Now
                        });
                        //记录本次报警详细信息
                        current.InOutAreaDetail = current.InOutAreaDetailObject.ToJson();
                        //保存区域记录当前数据表
                        _rep.Save(new EDeviceCurrentData()
                        {
                            InOutAreaDetail = current.InOutAreaDetail
                        }, p => p.DeviceId == device.Id, p => new Columns(p.InOutAreaDetail));


                        //获取报警地址
                        var location = GetLocationByWgs84(new EMapPoint(data.Latitude, data.Longitude));
                        var alarmReport = new EAlarmReportInOutArea()
                        {
                            DeviceId = device.Id,
                            ReportTime = data.ReportTime,
                            ReceiveTime = data.ReceiveTime,
                            AlarmAddress = location.Address,
                            AreaId = area.Id,
                            AreaName = area.AreaName,
                            IsInArea = true
                        };
                        //添加区域报警记录
                        _rep.Add(alarmReport);
                        var message = string.Format("车辆[{0}][{1}]在{2},进入区域[{3}]!", device.DeviceName, alarmReport.ReportTime, alarmReport.AlarmAddress, area.AreaName);
                        //触发邮件、短信、站内信提醒
                        RiseAlarm(device.UserId, "进入区域[" + area.AreaName + "]", message, setting);
                    }
                }
                else
                {
                    //如果现在不在这个区域
                    if (last != null && !areaSettingItem.IsIn)
                    {
                        //如果之前在这个区域 触发离开区域提示
                        //在当前状态中移除出区域信息
                        current.InOutAreaDetailObject.Remove(last);
                        //记录本次报警详细信息
                        current.InOutAreaDetail = current.InOutAreaDetailObject.ToJson();
                        //保存区域记录当前数据表
                        _rep.Save(new EDeviceCurrentData()
                        {
                            InOutAreaDetail = current.InOutAreaDetail
                        }, p => p.DeviceId == device.Id, p => new Columns(p.InOutAreaDetail));


                        //获取报警地址
                        var location = GetLocationByWgs84(new EMapPoint(data.Latitude, data.Longitude));
                        var alarmReport = new EAlarmReportInOutArea()
                        {
                            DeviceId = device.Id,
                            ReportTime = data.ReportTime,
                            ReceiveTime = data.ReceiveTime,
                            AlarmAddress = location.Address,
                            AreaId = area.Id,
                            AreaName = area.AreaName,
                            IsInArea = false
                        };
                        //添加区域报警记录
                        _rep.Add(alarmReport);
                        var message = string.Format("车辆[{0}][{1}]在{2},离开区域[{3}]!", device.DeviceName, alarmReport.ReportTime, alarmReport.AlarmAddress, area.AreaName);
                        //触发邮件、短信、站内信提醒
                        RiseAlarm(device.UserId, "离开区域[" + area.AreaName + "]", message, setting);
                    }
                }
            }
        }
        protected override void Handle(EQueueData data)
        {
            var device = GetDeviceByDeviceGpsCodeFromCache(data.DeviceGpsCode);
            if (device == null)
            {
                _logger.WarnFormat("处理报警[{0}]时,未找到当前GpsCode[{1}]所关联的设备!", Name, data.DeviceGpsCode);
                return;
            }

            //当前配置
            var setting = GetAlarmSettingFromCache<EAlarmSettingAutoFortify>(device.Id);

            if (setting == null || !setting.IsEnable)
            {
                return;
            }

            //获取当前状态
            var current = GetVDeviceCurrentDataStatusDetailFromCache(device.Id);

            if (current == null)
            {
                return;
            }

            if (data.Speed > 0)
            {
                //车辆移动
                if (current.IsAutoFortify)
                {
                    var isOut = !_mapService.PointIsInCircle(
                        new EMapPoint(current.IsAutoFortifyAlarmDetailObject.Latitude,
                            current.IsAutoFortifyAlarmDetailObject.Longitude),
                        setting.Radius,
                        new EMapPoint(data.Latitude, data.Longitude));

                    if (isOut)
                    {

                        //设置自动设防 为取消
                        current.IsAutoFortifyAlarmDetailObject.StartTime = null;
                        current.IsAutoFortifyAlarmDetailObject.Latitude = 0;
                        current.IsAutoFortifyAlarmDetailObject.Longitude = 0;

                        //设置自动设防
                        current.IsAutoFortify = false;
                        //设置自动设防详细信息
                        current.IsAutoFortifyAlarmDetail = current.IsAutoFortifyAlarmDetailObject.ToJson();
                        //保存自动设防记录当前数据表
                        _rep.Save(new EDeviceCurrentData()
                        {
                            IsAutoFortify = current.IsAutoFortify,
                            IsAutoFortifyAlarmDetail = current.IsAutoFortifyAlarmDetail
                        }, p => p.DeviceId == device.Id, p => new Columns(p.IsAutoFortify, p.IsAutoFortifyAlarmDetail));
                        var location = GetLocationByWgs84(new EMapPoint(data.Latitude, data.Longitude));
                        var message = string.Format("车辆[{0}][{1}],离开设防区域{2}!", device.DeviceName, DateTime.Now,
                            location.Address);
                        //触发邮件、短信、站内信提醒
                        RiseAlarm(device.UserId, "设防报警", message, setting);

                    }

                }
                else
                {
                    current.IsAutoFortifyAlarmDetailObject.StartTime = null;
                    current.IsAutoFortifyAlarmDetailObject.Latitude = 0;
                    current.IsAutoFortifyAlarmDetailObject.Longitude = 0;
                }
            }
            else
            {
                //车辆静止
                if (current.IsAutoFortify)
                {
                    //如果已经是设防状态 那么无需再次设防
                    return;
                }
                if (!current.IsAutoFortifyAlarmDetailObject.StartTime.HasValue)
                {
                    //记录开始设防开始信息
                    current.IsAutoFortifyAlarmDetailObject.StartTime = DateTime.Now;
                    current.IsAutoFortifyAlarmDetailObject.Latitude = data.Latitude;
                    current.IsAutoFortifyAlarmDetailObject.Longitude = data.Longitude;
                    return;
                }

                if (current.IsAutoFortifyAlarmDetailObject.StartTime < DateTime.Now.AddMinutes(-setting.ActivationTime))
                {
                    //激活自动设防
                    //设置自动设防 为设防中
                    current.IsAutoFortify = true;
                    //设置自动设防详细信息
                    current.IsAutoFortifyAlarmDetail = current.IsAutoFortifyAlarmDetailObject.ToJson();
                    //保存自动设防记录当前数据表
                    _rep.Save(new EDeviceCurrentData()
                    {
                        IsAutoFortify = current.IsAutoFortify,
                        IsAutoFortifyAlarmDetail = current.IsAutoFortifyAlarmDetail
                    }, p => p.DeviceId == device.Id, p => new Columns(p.IsAutoFortify, p.IsAutoFortifyAlarmDetail));
                    var location = GetLocationByWgs84(new EMapPoint(data.Latitude, data.Longitude));
                    var message = string.Format("车辆[{0}][{1}],已经自动设防,设防半径{2}米 地址:{3}!", device.DeviceName, DateTime.Now, setting.Radius, location.Address);

                    if (current.IsAutoFortifyAlarmDetailObject.StartTime >
                        DateTime.Now.AddMinutes(-60))
                    {
                        //防止车辆进入停车场或者其他没有信号的地方时 然后过了很久又有了信号误报 这个时候仅仅设防 但是不提示设防信息。
                        //触发邮件、短信、站内信提醒
                        RiseAlarm(device.UserId, "自动设防", message, setting);
                    }
                }
            }
        }
        protected override void Handle(EQueueData data)
        {
            var device = GetDeviceByDeviceGpsCodeFromCache(data.DeviceGpsCode);
            if (device == null)
            {
                _logger.WarnFormat("处理报警[{0}]时,未找到当前GpsCode[{1}]所关联的设备!", Name, data.DeviceGpsCode);
                return;
            }

            //处理静止时长

            if (data.Speed > 5)
            {
                //保存超速记录当前数据表
                _rep.Save(new EDeviceCurrentData()
                {
                    LastHaveSpeedTime = data.ReportTime,
                }, p => p.DeviceId == device.Id, p => new Columns(p.LastHaveSpeedTime));
            }

            //当前配置
            var setting = GetAlarmSettingFromCache<EAlarmSettingOverspeed>(device.Id);

            if (setting == null || !setting.IsEnable)
            {
                return;
            }

            //获取当前状态
            var current = GetVDeviceCurrentDataStatusDetailFromCache(device.Id);

            if (current == null)
            {
                return;
            }

            if (data.Speed >= setting.LimitSpeed)
            {
                //已超速 之前没有超速
                if (!current.IsOverSpeedAlarm)
                {
                    //设置当前报警状态 为超速
                    current.IsOverSpeedAlarm = true;
                    //记录本次报警详细信息
                    current.IsOverSpeedAlarmDetail = DateTime.Now.ToString();
                    //保存超速记录当前数据表
                    _rep.Save(new EDeviceCurrentData()
                    {
                        IsOverSpeedAlarm = current.IsOverSpeedAlarm,
                        IsOverSpeedAlarmDetail = current.IsOverSpeedAlarmDetail
                    }, p => p.DeviceId == device.Id, p => new Columns(p.IsOverSpeedAlarm,p.IsOverSpeedAlarmDetail));


                    //获取报警地址
                    var location = GetLocationByWgs84(new EMapPoint(data.Latitude, data.Longitude));
                    var alarmReport = new EAlarmReportOverspeed()
                    {
                        DeviceId = device.Id,
                        ReportTime = data.ReportTime,
                        ReceiveTime = data.ReceiveTime,
                        Speed = data.Speed,
                        LimitSpeed = setting.LimitSpeed,
                        AlarmAddress = location.Address,
                        IsOverspeed = true
                    };
                    //添加超速报警记录
                    _rep.Add(alarmReport);
                    var message = string.Format("车辆[{0}][{1}]在{2},超速行驶!当前限速{3}km/h, 现在时速:{4}km/h", device.DeviceName, alarmReport.ReportTime, alarmReport.AlarmAddress, setting.LimitSpeed, data.Speed);
                    //触发邮件、短信、站内信提醒
                    RiseAlarm(device.UserId, "超速报警", message, setting);
                }
            }
            else
            {
                if (current.IsOverSpeedAlarm)
                {
                    //清除报警数据
                    current.IsOverSpeedAlarm = false;
                    //清除报警时间
                    current.IsOverSpeedAlarmDetail = null;
                    _rep.Save(new EDeviceCurrentData()
                    {
                        IsOverSpeedAlarm = current.IsOverSpeedAlarm,
                        IsOverSpeedAlarmDetail = current.IsOverSpeedAlarmDetail
                    }, p => p.DeviceId == device.Id, p => new Columns(p.IsOverSpeedAlarm, p.IsOverSpeedAlarmDetail));


                    //获取报警地址
                    var location = GetLocationByWgs84(new EMapPoint(data.Latitude, data.Longitude));
                    var alarmReport = new EAlarmReportOverspeed()
                    {
                        DeviceId = device.Id,
                        ReportTime = data.ReportTime,
                        ReceiveTime = data.ReceiveTime,
                        Speed = data.Speed,
                        LimitSpeed = setting.LimitSpeed,
                        AlarmAddress = location.Address,
                        IsOverspeed = false
                    };
                    //添加超速报警记录
                    _rep.Add(alarmReport);
                    var message = string.Format("车辆[{0}][{1}]在{2},恢复速度行驶!当前限速{3}km/h, 现在时速:{4}km/h", device.DeviceName, alarmReport.ReportTime, alarmReport.AlarmAddress, setting.LimitSpeed, data.Speed);
                    //触发邮件、短信、站内信提醒
                    RiseAlarm(device.UserId, "恢复速度", message, setting);
                }
            }
        }