Пример #1
0
        private void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Received = " + Encoding.UTF8.GetString(e.Message) + " on topic " + e.Topic);
            var bodyObj    = JsonSerializer.Deserialize <DetectionModel>(Encoding.UTF8.GetString(e.Message));
            var topicStr   = e.Topic;
            var splitTopic = topicStr.Split("/");

            var detection = new DetectionConfiguration
            {
                ScooterId           = Convert.ToInt32(splitTopic[1]),
                SensorId            = Convert.ToInt32(splitTopic[2]),
                SensorType          = splitTopic[3],
                SensorValue         = bodyObj.SensorValue,
                SensorDetectionDate = bodyObj.SensorDetectionDate
            };

            try
            {
                _detectionRepoitory.insertData(detection, _connectionString);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Detection inserted");
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
            }
        }
Пример #2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var factory = new ConnectionFactory()
            {
                HostName = brokerUrl
            };

            connection = factory.CreateConnection();
            channel    = connection.CreateModel();
            channel.ExchangeDeclare(exchange: exchange, type: ExchangeType.Direct);
            channel.QueueDeclare(queue: queue,
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);
            channel.QueueBind(queue, exchange, "Position_Sensor");

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (model, ea) =>
            {
                var body    = ea.Body.ToArray();
                var message = Encoding.UTF8.GetString(body);
                var obj     = JsonSerializer.Deserialize <DetectionConfiguration>(message);
                _detectionRepoitory.insertData(obj, _connectionString);
                Console.WriteLine(" [x] Received {0}", message);
            };
            channel.BasicConsume(queue: queue,
                                 autoAck: true,
                                 consumer: consumer);
        }
 public ResponseAPI Post([FromBody] DetectionConfiguration configuration)
 {
     try
     {
         _detectionRepository.insertData(configuration, _connectionStringSQL);
         return(new ResponseAPI {
             StatusCode = 200, Message = "Detection inserted"
         });
     }
     catch (Exception e)
     {
         return(new ResponseAPI {
             StatusCode = 500, Message = e.Message
         });
     }
 }