コード例 #1
0
        public bool AddRSU(RSU rsu)
        {
            if (_managerContext.RSUs.Any(r => r.Id == rsu.Id))
            {
                return(false);
            }

            _managerContext.Add(rsu);
            _managerContext.SaveChanges();

            return(true);
        }
コード例 #2
0
        public bool UpdateRSU(RSU rsu)
        {
            var rsu_mod = GetRSU(rsu.Id);

            if (rsu_mod == null)
            {
                return(false);
            }

            _managerContext.Update(rsu);
            _managerContext.SaveChanges();
            return(true);
        }
コード例 #3
0
        public async Task <IActionResult> OnPostAsync(int?id, int?managerId)
        {
            if (!id.HasValue)
            {
                return(NotFound());
            }

            var user = _applicationDbContext.Users
                       .Include(u => u.UserManagerUsers)
                       .FirstOrDefault(u => u.UserName == HttpContext.User.Identity.Name);

            if (user == null)
            {
                // TODO finish
            }

            if (!managerId.HasValue)
            {
                return(NotFound());
            }

            var manager = _applicationDbContext.Managers
                          .Include(m => m.Users)
                          .FirstOrDefault(m => m.Id == managerId.Value);

            if (manager == null)
            {
                return(NotFound());
            }

            var managerUser = user.UserManagerUsers.FirstOrDefault(umu => umu.ManagerUserManagerId == managerId)?.ManagerUser;

            if (managerUser == null)
            {
                return(NotFound($"There's no Manager User assigned to this User, with {RSU.Manager.Name} Manager"));
            }

            RSU = await _rsuService.GetAsync(managerUser, id.Value);

            if (RSU == null)
            {
                return(NotFound());
            }
            else
            {
                await _rsuService.DeleteAsync(managerUser, id.Value);
            }


            return(RedirectToPage("./Index"));
        }
コード例 #4
0
ファイル: RSUService.cs プロジェクト: sturmp/RoadSideUnit_DMS
        public async Task <bool> UpdateAsync(ManagerUser managerUser, RSU rsu)
        {
            var host = GetHost(managerUser);

            try
            {
                var result = await _httpClinet.PutAsJsonAsync($"http://{host}:{managerUser.Manager.Port}/{controller}/{managerUser.Name}/{managerUser.Token}", rsu.ConvertToRSUDto());

                return(true);
            }
            catch (HttpRequestException ex)
            {
                return(false);
            }
        }
コード例 #5
0
ファイル: RSUService.cs プロジェクト: sturmp/RoadSideUnit_DMS
        public async Task <RSU> GetAsync(ManagerUser managerUser, int id)
        {
            var host = GetHost(managerUser);

            try
            {
                var result = await _httpClinet.GetStringAsync($"http://{host}:{managerUser.Manager.Port}/{controller}/{managerUser.Name}/{managerUser.Token}/{id}");

                var rsu = RsuDto.FromJson(result);

                return(RSU.Parse(rsu, managerUser.Manager));
            }
            catch (HttpRequestException ex)
            {
                return(null);
            }
        }
コード例 #6
0
ファイル: RSUService.cs プロジェクト: sturmp/RoadSideUnit_DMS
        public async Task <IEnumerable <RSU> > GetAsync(ManagerUser managerUser)
        {
            var host = GetHost(managerUser);

            try
            {
                var result = await _httpClinet.GetStringAsync($"http://{host}:{managerUser.Manager.Port}/{controller}/{managerUser.Name}/{managerUser.Token}");

                var rsus = RsuDto.FromJsonCollection(result);


                return(rsus.Select(r => RSU.Parse(r, managerUser.Manager)).ToList());
            }
            catch (HttpRequestException ex)
            {
                return(null);
            }
        }
コード例 #7
0
        public bool Set(RSU rsu, Core.Entities.User user, string OID, SnmpType type, string value)
        {
            IPEndPoint receiver = new IPEndPoint(rsu.IP, rsu.Port);
            int        timeout  = _managerSettings.Timeout;

            Discovery     discovery = Messenger.GetNextDiscovery(SnmpType.GetRequestPdu);
            ReportMessage report    = discovery.GetResponse(timeout, receiver);

            var auth = new SHA1AuthenticationProvider(new Lextm.SharpSnmpLib.OctetString(user.SNMPv3Auth));
            var priv = new DESPrivacyProvider(new Lextm.SharpSnmpLib.OctetString(user.SNMPv3Priv), auth);

            ISnmpData data;

            try
            {
                data = ConvertStringValue2SnmpData(type, value);
            }
            catch (InvalidDataType invalidDataType) { throw invalidDataType; }
            catch (FormatException formatException) { throw formatException; }

            List <Variable> variables = new List <Variable>()
            {
                new Variable(new ObjectIdentifier(OID), data)
            };

            SetRequestMessage request = new SetRequestMessage(VersionCode.V3
                                                              , Messenger.NextMessageId
                                                              , Messenger.NextRequestId
                                                              , new OctetString(user.UserName)
                                                              , new OctetString(String.Empty)
                                                              , variables
                                                              , priv
                                                              , Messenger.MaxMessageSize
                                                              , report);

            ISnmpMessage reply = request.GetResponse(timeout, receiver);

            // Need to send again (RFC 3414)
            if (reply is ReportMessage)
            {
                //throw new ReplyIsReportMessage();
                request = new SetRequestMessage(VersionCode.V3
                                                , Messenger.NextMessageId
                                                , Messenger.NextRequestId
                                                , new OctetString(user.UserName)
                                                , new OctetString(String.Empty)
                                                , variables
                                                , priv
                                                , Messenger.MaxMessageSize
                                                , reply);

                reply = request.GetResponse(timeout, receiver);
                if (reply.Pdu().ErrorStatus.ToInt32() != 0)
                {
                    throw new InvalidDataType();
                }
            }
            else if (reply.Pdu().ErrorStatus.ToInt32() != 0)
            {
                throw new InvalidDataType();
            }

            return(true);
        }
コード例 #8
0
        public List <MIBObject> Get(RSU rsu, Core.Entities.User user, string OID)
        {
            List <MIBObject> mibObjects;

            try
            {
                IPEndPoint receiver = new IPEndPoint(rsu.IP, rsu.Port);
                int        timeout  = _managerSettings.Timeout;

                Discovery     discovery = Messenger.GetNextDiscovery(SnmpType.GetRequestPdu);
                ReportMessage report    = discovery.GetResponse(timeout, receiver);

                var auth = new SHA1AuthenticationProvider(new Lextm.SharpSnmpLib.OctetString(user.SNMPv3Auth));
                var priv = new DESPrivacyProvider(new Lextm.SharpSnmpLib.OctetString(user.SNMPv3Priv), auth);

                GetRequestMessage request = new GetRequestMessage(VersionCode.V3
                                                                  , Messenger.NextMessageId
                                                                  , Messenger.NextRequestId
                                                                  , new OctetString(user.UserName)
                                                                  , new OctetString(String.Empty)
                                                                  , new List <Variable> {
                    new Variable(new ObjectIdentifier(OID))
                }
                                                                  , priv
                                                                  , Messenger.MaxMessageSize
                                                                  , report);

                ISnmpMessage reply = request.GetResponse(timeout, receiver);

                // Need to send again (RFC 3414)???
                if (reply is ReportMessage)
                {
                    //throw new ReplyIsReportMessage();
                    request = new GetRequestMessage(VersionCode.V3
                                                    , Messenger.NextMessageId
                                                    , Messenger.NextRequestId
                                                    , new OctetString(user.UserName)
                                                    , new OctetString(String.Empty)
                                                    , new List <Variable> {
                        new Variable(new ObjectIdentifier(OID))
                    }
                                                    , priv
                                                    , Messenger.MaxMessageSize
                                                    , reply);

                    reply = request.GetResponse(timeout, receiver);
                    if (reply.Pdu().ErrorStatus.ToInt32() != 0)
                    {
                        throw new SnmpGetError();
                    }
                }
                else if (reply.Pdu().ErrorStatus.ToInt32() != 0)
                {
                    throw new SnmpGetError();
                }

                mibObjects = SNMPVariables2MIBObjects(reply.Pdu().Variables);

                return(mibObjects);
            }
            catch (Lextm.SharpSnmpLib.Messaging.TimeoutException ex)
            {
                mibObjects = new List <MIBObject>();
                mibObjects.Add(new MIBObject("0", SnmpType.OctetString, "Timeout"));
                return(mibObjects);
            }
        }
コード例 #9
0
 public void SetTrapListener(RSU rsu, IPAddress listenerIP, int listenerPort)
 {
     throw new NotImplementedException();
 }
コード例 #10
0
        private void MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var contextService = scope.ServiceProvider.GetRequiredService <IContextService>();

                RSU rsu = contextService.GetRSU().SingleOrDefault(r => r.IP.ToString() == e.Sender.Address.ToString() &&
                                                                  r.Port == e.Sender.Port);

                // Registrate new RSU +
                if (rsu == null)
                {
                    bool success = contextService.AddRSU(new RSU
                    {
                        Name                = "Unnamed",
                        IP                  = e.Sender.Address,
                        Port                = e.Sender.Port,
                        Active              = true,
                        MIBVersion          = "unknown",
                        FirmwareVersion     = "unknown",
                        LocationDescription = "unknown",
                        Manufacturer        = "unknown",
                        NotificationIP      = ownIP,
                        NotificationPort    = listenerPort
                    });
                    if (success)
                    {
                        rsu = contextService.GetRSU().Single(r => r.IP == e.Sender.Address && r.Port == e.Sender.Port);
                        // Get the unknow rsu latitude and longitude values and assign it to the rsu
                        var snmpService = scope.ServiceProvider.GetRequiredService <ISNMPManagerService>();
                        snmpService.Configure(contextService.GetManagerSettings());

                        var    mibo = snmpService.Get(rsu, contextService.GetUser("admin"), "0.1.15628.4.1.8.6").FirstOrDefault();
                        double lat  = (double.TryParse(mibo.Value, out lat)) ? lat / 1000000 : 0.0;

                        mibo = snmpService.Get(rsu, contextService.GetUser("admin"), "0.1.15628.4.1.8.7").FirstOrDefault();
                        double lon = (double.TryParse(mibo.Value, out lon)) ? lon / 1000000 : 0.0;

                        rsu.Latitude  = lat;
                        rsu.Longitude = lon;
                        contextService.UpdateRSU(rsu);

                        contextService.AddManagerLog(new ManagerLog(DateTime.Now, LogType.DB, $"Registered new RSU with id: {rsu.Id}"));
                    }
                    else
                    {
                        contextService.AddManagerLog(new ManagerLog(DateTime.Now, LogType.DB, $"Registering new RSU({e.Sender.Address.ToString()}/{e.Sender.Port}) failed"));
                        return;
                    }
                }
                // Registrate new RSU -
                else
                {
                    if (!rsu.Active)
                    {
                        rsu.Active = true;
                        contextService.UpdateRSU(rsu);
                    }
                }

                if (e.Message.Parameters.IsInvalid)
                {
                    contextService.AddTrapLog(new TrapLog(DateTime.Now, LogType.SNMP, rsu.Id, "Invalid Trap mesage!"));
                }

                try
                {
                    if (e.Message.Scope.Pdu.Variables.Count == 0)
                    {
                        contextService.AddTrapLog(new TrapLog(DateTime.Now, LogType.SNMP, rsu.Id, "Heartbeat ping"));
                    }

                    /*if (e.Message.Scope.Pdu.TypeCode == SnmpType.TrapV2Pdu)
                     * {
                     *  TrapV2Pdu v2trap = (TrapV2Pdu) e.Message.Scope.Pdu;
                     *  var variables = v2trap.Variables.ToList();
                     *  //contextService.AddTrapLog(new TrapLog(DateTime.Now, LogType.SNMP, rsu.GetValueOrDefault(), v2trap.Variables..ToString()));
                     * }*/
                }
                catch (Exception ex)
                {
                    contextService.AddTrapLog(new TrapLog(DateTime.Now, LogType.SNMP, rsu.Id, $"Error processing Trap message! {ex.Message.Take(250)}"));
                }
            }
        }