Ejemplo n.º 1
0
        public static Payload GetPayload(byte[] serverPayload)
        {
            /*string bin_strng = "1100110001";
             * int number = 0;
             *
             * number = Convert.ToInt32(bin_strng, 2);
             * Console.WriteLine("Number value of binary \"{0}\" is = {1}",
             *  bin_strng, number);
             *
             * bin_strng = "1111100000110001";
             * number = Convert.ToInt32(bin_strng, 2);
             * Console.WriteLine("Number value of binary \"{0}\" is = {1}",
             *  bin_strng, number);
             */

            var payload  = new Payload();
            var bitArray = new BitArray(serverPayload);

            payload.Maneuver              = (Maneuver)Convert.ToInt32(FunctionHelpers.ToBitString(bitArray, 0, 3), 2);
            payload.PlatoonGap            = Convert.ToInt32(FunctionHelpers.ToBitString(bitArray, 3, 11), 2);
            payload.PlatoonOverrideStatus = Convert.ToInt32(FunctionHelpers.ToBitString(bitArray, 11, 12), 2) != 0;
            payload.VehicleRank           = Convert.ToInt32(FunctionHelpers.ToBitString(bitArray, 12, 16), 2);
            payload.BreakPedal            = Convert.ToInt32(FunctionHelpers.ToBitString(bitArray, 16, 23), 2);
            payload.PlatoonDissolveStatus = Convert.ToInt32(FunctionHelpers.ToBitString(bitArray, 23, 24), 2) != 0;
            payload.StationId             = Convert.ToInt32(FunctionHelpers.ToBitString(bitArray, 24, 56), 2);
            payload.StreamingRequests     = Convert.ToInt32(FunctionHelpers.ToBitString(bitArray, 56, 58), 2);
            payload.V2HealthStatus        = Convert.ToInt32(FunctionHelpers.ToBitString(bitArray, 58, 59), 2) != 0;
            payload.TruckRoutingStaus     = Convert.ToInt32(FunctionHelpers.ToBitString(bitArray, 59, 61), 2);
            payload.RealPayload           = Encoding.ASCII.GetString(serverPayload);

            return(payload);
        }
Ejemplo n.º 2
0
        private static void MqttConsumer_DataReceived(object sender, byte[] e)
        {
            var bitarry = new BitArray(e);

            Console.WriteLine("Data received ===>" + FunctionHelpers.ToBitString(bitarry, 0, bitarry.Length));
        }
Ejemplo n.º 3
0
        public async Task StartConsume()
        {
            try
            {
                await Server.StartAsync(OptionsBuilder);
            }
            catch (Exception ex)
            {
                StopConsume();
                Console.WriteLine(ex.Message);
                throw;
            }
            Server.StartedHandler = new MqttServerStartedHandlerDelegate(e =>
            {
                Console.WriteLine("Mqtt Broker start");
            });
            Server.StoppedHandler = new MqttServerStoppedHandlerDelegate(e =>
            {
                Console.WriteLine("Mqtt Broker stop");
            });
            Server.ClientSubscribedTopicHandler = new MqttServerClientSubscribedHandlerDelegate(e =>
            {
                var vehicleId = e.TopicFilter.Topic
                                .Replace("platooning/", "").Replace("/#", "");

                Console.WriteLine("Client subscribed " + e.ClientId + " topic " + e.TopicFilter.Topic + "Vehicle Id " +
                                  vehicleId);
                using var context = new MqttBrokerDbContext();
                try
                {
                    var audit = new Audit
                    {
                        ClientId = e.ClientId,
                        Type     = "Sub",
                        Topic    = e.TopicFilter.Topic,
                        Payload  = JsonConvert.SerializeObject(e.TopicFilter, Formatting.Indented)
                    };
                    context.Audit.AddAsync(audit);
                    var subs = context.Subscribe.AsQueryable()
                               .FirstOrDefault(s => s.Topic == e.TopicFilter.Topic &&
                                               s.ClientId == e.ClientId &&
                                               s.QoS == e.TopicFilter.QualityOfServiceLevel.ToString());
                    if (subs != null)
                    {
                        return;
                    }


                    var subClient = new Subscribe
                    {
                        Topic    = e.TopicFilter.Topic,
                        Enable   = true,
                        ClientId = e.ClientId,
                        QoS      = e.TopicFilter.QualityOfServiceLevel.ToString()
                    };
                    context.Subscribe.AddAsync(subClient);
                    context.SaveChanges();
                }
                catch (Exception exception)
                {
                    var log = new Log
                    {
                        Exception = exception.StackTrace
                    };
                    context.Log.AddAsync(log);
                    context.SaveChanges();
                    Console.WriteLine(exception);
                }
            });

            Server.ClientUnsubscribedTopicHandler = new MqttServerClientUnsubscribedTopicHandlerDelegate(args =>
            {
                try
                {
                    var clientId    = args.ClientId;
                    var topicFilter = args.TopicFilter;

                    Console.WriteLine($"[{DateTime.Now}] Client '{clientId}' un-subscribed to {topicFilter}.");
                    using var context = new MqttBrokerDbContext();
                    try
                    {
                        var sub = context.Subscribe.AsQueryable()
                                  .FirstOrDefault(a => a.Topic == args.TopicFilter && a.ClientId == clientId);
                        if (sub == null)
                        {
                            return;
                        }
                        context.Subscribe.Update(sub !);
                        context.SaveChanges();
                        sub.Enable = false;
                    }
                    catch (Exception exception)
                    {
                        var log = new Log
                        {
                            Exception = exception.StackTrace
                        };
                        context.Log.AddAsync(log);
                        context.SaveChanges();
                        Console.WriteLine(exception);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"[{DateTime.Now}] Client get error " + ex.Message);
                }
            });

            Server.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(e =>
            {
                Console.WriteLine("Client Connected " + e.ClientId);
            });
            Server.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
            {
                using var context = new MqttBrokerDbContext();
                try
                {
                    var payload = FunctionHelpers.GetPayload(e.ApplicationMessage.Payload);
                    var audit   = new Audit
                    {
                        ClientId = e.ClientId,
                        Type     = "Pub",
                        Topic    = e.ApplicationMessage.Topic,
                        Payload  = JsonConvert.SerializeObject(payload, Formatting.Indented)
                    };
                    context.Audit.AddAsync(audit);
                    if (e.ClientId == null)
                    {
                        var log = new Log
                        {
                            Exception = new string("Broker publish message itself " +
                                                   JsonConvert.SerializeObject(payload, Formatting.Indented) + " " +
                                                   e.ClientId)
                        };
                        context.Log.AddAsync(log);
                        context.SaveChanges();
                        return;
                    }

                    if (payload.Maneuver == Maneuver.CreatePlatoon)
                    {
                        var vehPla    = e.ApplicationMessage.Topic.Replace("platooning/message/", "");
                        var platoonId = vehPla.Split("/").Last();
                        var pla       = context.Platoon.AsQueryable()
                                        .FirstOrDefault(f => f.Enable && f.PlatoonRealId == platoonId);
                        if (pla == null)
                        {
                            var platoon = new Platoon()
                            {
                                Enable        = true,
                                ClientId      = e.ClientId,
                                IsLead        = true,
                                VechicleId    = vehPla.Split("/").First(),
                                PlatoonRealId = vehPla.Split("/").Last()
                            };
                            context.Platoon.AddAsync(platoon);
                            Console.WriteLine($"[{DateTime.Now}] Creating new Platoon Client Id " + e.ClientId +
                                              " platooning Id" + platoon.PlatoonRealId + " payload " + audit.Payload);
                        }
                        else
                        {
                            Console.WriteLine($"[{DateTime.Now}] Platoon is already created Client Id " + e.ClientId +
                                              " platooning Id" + platoonId + " payload " + audit.Payload);
                        }
                    }
                    else if (payload.Maneuver == Maneuver.JoinRequest)
                    {
                        var followingVec = e.ApplicationMessage.Topic.Replace("platooning/message/", "");
                        var isFollowing  = context.Platoon.FirstOrDefault(f => f.IsFollower && f.VechicleId == followingVec && f.Enable);
                        if (isFollowing != null)
                        {
                            return;
                        }
                        var platoonLead = context.Platoon.AsQueryable().FirstOrDefault(f => f.IsLead && f.Enable);
                        if (platoonLead != null)
                        {
                            var platoon = new Platoon()
                            {
                                Enable        = false,
                                ClientId      = e.ClientId,
                                IsLead        = false,
                                IsFollower    = true,
                                VechicleId    = followingVec,
                                PlatoonRealId = platoonLead.PlatoonRealId
                            };
                            context.Platoon.AddAsync(platoon);
                            Console.WriteLine($"[{DateTime.Now}] Join Platoon Client Id " + e.ClientId +
                                              " platooning Id" + platoon.PlatoonRealId + " payload " + audit.Payload);
                            var message = new BitArray(_dataLenght);
                            message.Set(0, false);
                            message.Set(1, false);
                            message.Set(2, true);

                            Server.PublishAsync("platooning/" + platoonLead.ClientId + "/" + followingVec,
                                                Encoding.ASCII.GetString(FunctionHelpers.BitArrayToByteArray(message)));
                        }
                    }
                    else if (payload.Maneuver == Maneuver.JoinAccepted)
                    {
                        Console.WriteLine($"[{DateTime.Now}] Join accepted Client Id " + e.ClientId + " payload " +
                                          audit.Payload);
                        var splitTopic      = e.ApplicationMessage.Topic.Split("/");
                        var followvehicleId = splitTopic[1];
                        var leadVehicle     = splitTopic[2];
                        var plattonId       = splitTopic[3];
                        var platoonfollow   = context.Platoon.AsQueryable()
                                              .FirstOrDefault(f => f.IsFollower && f.ClientId == followvehicleId);

                        if (platoonfollow != null)
                        {
                            platoonfollow.Enable        = true;
                            platoonfollow.PlatoonRealId = plattonId;
                            context.Platoon.Update(platoonfollow);
                        }
                        else
                        {
                            var platoonlead = context.Platoon.AsQueryable()
                                              .FirstOrDefault(f => f.IsLead && f.Enable && f.ClientId == leadVehicle);
                            if (platoonlead != null)
                            {
                                var platoon = new Platoon()
                                {
                                    Enable        = true,
                                    ClientId      = e.ClientId,
                                    IsLead        = false,
                                    IsFollower    = true,
                                    VechicleId    = followvehicleId,
                                    PlatoonRealId = platoonlead.PlatoonRealId
                                };
                                context.Platoon.AddAsync(platoon);
                            }
                        }
                    }
                    else
                    {
                        var log = new Log
                        {
                            Exception = new string("Unknown Maneuver " +
                                                   JsonConvert.SerializeObject(payload, Formatting.Indented) + " " +
                                                   e.ClientId)
                        };
                        context.Log.AddAsync(log);
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                    var log = new Log
                    {
                        Exception = exception.StackTrace
                    };
                    context.Log.AddAsync(log);
                    context.SaveChanges();
                }

                context.SaveChanges();
                OnDataReceived(e.ApplicationMessage.Payload);
                //Console.WriteLine("Message Received");
                //Console.WriteLine(e.ClientId + " " + e.ApplicationMessage.Topic);
                //Console.WriteLine(e.ClientId + " " + e.ApplicationMessage.ConvertPayloadToString());
            });
        }