Ejemplo n.º 1
0
        public async Task <InvokeResult> AddBinaryMessageAsync(byte[] buffer, DateTime startTimeStamp, String deviceId = "", String topic = "")
        {
            try
            {
                var message = new PipelineExecutionMessage()
                {
                    PayloadType       = MessagePayloadTypes.Binary,
                    BinaryPayload     = buffer,
                    CreationTimeStamp = startTimeStamp.ToJSONString()
                };

                Metrics.MessagesProcessed++;

                if (buffer != null)
                {
                    message.PayloadLength = buffer.Length;
                }

                Metrics.BytesProcessed = message.PayloadLength + (String.IsNullOrEmpty(topic) ? 0 : topic.Length);

                message.Envelope.DeviceId = deviceId;
                message.Envelope.Topic    = topic;

                var listenerInstruction = new PipelineExecutionInstruction()
                {
                    Name            = _listenerConfiguration.Name,
                    Type            = GetType().Name,
                    QueueId         = "N/A",
                    StartDateStamp  = startTimeStamp.ToJSONString(),
                    ProcessByHostId = PEMBus.Instance.PrimaryHost.Id,
                    ExecutionTimeMS = (DateTime.UtcNow - startTimeStamp).TotalMilliseconds,
                };

                message.Instructions.Add(listenerInstruction);

                var planner            = PEMBus.Instance.Solution.Value.Planner.Value;
                var plannerInstruction = new PipelineExecutionInstruction()
                {
                    Name    = "Planner",
                    Type    = "Planner",
                    QueueId = "N/A",
                };

                message.CurrentInstruction = plannerInstruction;
                message.Instructions.Add(plannerInstruction);

                var plannerQueue = PEMBus.Queues.Where(queue => queue.ForModuleType == PipelineModuleType.Planner).FirstOrDefault();
                await plannerQueue.EnqueueAsync(message);

                return(InvokeResult.Success);
            }
            catch (Exception ex)
            {
                PEMBus.InstanceLogger.AddException("ListenerModule_AddBinaryMessageAsync", ex);
                return(InvokeResult.FromException("ListenerModule_AddBinaryMessageAsync", ex));
            }
        }
Ejemplo n.º 2
0
        public async Task <InvokeResult> AddStringMessageAsync(string buffer, DateTime startTimeStamp, string path = "", string deviceId = "", string topic = "", Dictionary <string, string> headers = null)
        {
            try
            {
                if (!String.IsNullOrEmpty(topic))
                {
                    Console.WriteLine($"Received Message with topic [{topic}]");
                }

                if (!String.IsNullOrEmpty(topic) && topic.StartsWith("nuviot/srvr/dvcsrvc"))
                {
                    return(await HandleSystemMessageAsync(topic, buffer));
                }

                if (!String.IsNullOrEmpty(path) && path.StartsWith("/nuviot/srvr/dvcsrvc"))
                {
                    return(await HandleSystemMessageAsync(path.TrimStart('/'), buffer));
                }

                var message = new PipelineExecutionMessage()
                {
                    PayloadType       = MessagePayloadTypes.Text,
                    TextPayload       = buffer,
                    CreationTimeStamp = startTimeStamp.ToJSONString()
                };

                var headerLength = 0;

                if (headers != null)
                {
                    if (headers.ContainsKey("method"))
                    {
                        message.Envelope.Method = headers["method"];
                    }

                    if (headers.ContainsKey("topic"))
                    {
                        message.Envelope.Topic = headers["topic"];

                        foreach (var header in headers)
                        {
                            headerLength += header.Key.Length + (String.IsNullOrEmpty(header.Value) ? 0 : header.Value.Length);
                        }
                    }

                    if (headers != null)
                    {
                        foreach (var hdr in headers)
                        {
                            message.Envelope.Headers.Add(hdr.Key, hdr.Value);
                        }
                    }
                }

                message.PayloadLength = String.IsNullOrEmpty(buffer) ? 0 : buffer.Length;

                var bytesProcessed = message.PayloadLength + (String.IsNullOrEmpty(path) ? 0 : path.Length) + headerLength;

                Metrics.BytesProcessed += bytesProcessed;
                Metrics.MessagesProcessed++;

                var json = JsonConvert.SerializeObject(Metrics);

                /*
                 * Console.WriteLine("LISTENER => " + Id);
                 * Console.ForegroundColor = ConsoleColor.Yellow;
                 * Console.WriteLine(json);
                 * Console.WriteLine("----------------------------");
                 */

                message.Envelope.DeviceId = deviceId;
                message.Envelope.Path     = path;
                message.Envelope.Topic    = topic;

                var listenerInstruction = new PipelineExecutionInstruction()
                {
                    Name            = _listenerConfiguration.Name,
                    Type            = GetType().Name,
                    QueueId         = _listenerConfiguration.Id,
                    StartDateStamp  = startTimeStamp.ToJSONString(),
                    ProcessByHostId = PEMBus.Instance.PrimaryHost.Id,
                    Enqueued        = startTimeStamp.ToJSONString(),
                    ExecutionTimeMS = (DateTime.UtcNow - startTimeStamp).TotalMilliseconds,
                };

                message.Instructions.Add(listenerInstruction);

                var plannerQueue = PEMBus.Queues.Where(queue => queue.ForModuleType == PipelineModuleType.Planner).FirstOrDefault();
                if (plannerQueue == null)
                {
                    PEMBus.InstanceLogger.AddError("ListenerModule_AddStringMessageAsync", "Could not find planner queue.");
                    return(InvokeResult.FromError("Could not find planner queue."));
                }

                var planner            = PEMBus.Instance.Solution.Value.Planner.Value;
                var plannerInstruction = new PipelineExecutionInstruction()
                {
                    Name     = planner.Name,
                    Type     = "Planner",
                    QueueId  = plannerQueue.PipelineModuleId,
                    Enqueued = DateTime.UtcNow.ToJSONString()
                };

                message.CurrentInstruction = plannerInstruction;
                message.Instructions.Add(plannerInstruction);

                await plannerQueue.EnqueueAsync(message);

                return(InvokeResult.Success);
            }
            catch (Exception ex)
            {
                Metrics.DeadLetterCount++;
                Metrics.ErrorCount++;
                PEMBus.InstanceLogger.AddException("ListenerModule_AddStringMessageAsync", ex);
                Console.WriteLine(ex.StackTrace);
                return(InvokeResult.FromException("ListenerModule_AddStringMessageAsync", ex));
            }
        }
Ejemplo n.º 3
0
        public async Task <InvokeResult> AddMediaMessageAsync(Stream stream, string contentType, long contentLength, DateTime startTimeStamp, string path, String deviceId = "", String topic = "", Dictionary <string, string> headers = null)
        {
            try
            {
                var message = new PipelineExecutionMessage()
                {
                    PayloadType       = MessagePayloadTypes.Media,
                    CreationTimeStamp = startTimeStamp.ToJSONString()
                };

                Metrics.MessagesProcessed++;

                message.PayloadLength   = contentLength;
                Metrics.BytesProcessed += message.PayloadLength + (String.IsNullOrEmpty(topic) ? 0 : topic.Length);

                message.Envelope.DeviceId = deviceId;
                message.Envelope.Topic    = topic;
                message.Envelope.Path     = path;

                var headerLength = 0;

                if (headers != null)
                {
                    if (headers.ContainsKey("method"))
                    {
                        message.Envelope.Method = headers["method"];
                    }

                    if (headers.ContainsKey("topic"))
                    {
                        message.Envelope.Topic = headers["topic"];

                        foreach (var header in headers)
                        {
                            headerLength += header.Key.Length + (String.IsNullOrEmpty(header.Value) ? 0 : header.Value.Length);
                        }
                    }

                    foreach (var hdr in headers)
                    {
                        message.Envelope.Headers.Add(hdr.Key, hdr.Value);
                    }
                }

                var listenerInstruction = new PipelineExecutionInstruction()
                {
                    Name            = _listenerConfiguration.Name,
                    Type            = GetType().Name,
                    QueueId         = "N/A",
                    StartDateStamp  = startTimeStamp.ToJSONString(),
                    ProcessByHostId = PEMBus.Instance.PrimaryHost.Id,
                    ExecutionTimeMS = (DateTime.UtcNow - startTimeStamp).TotalMilliseconds,
                };

                message.Instructions.Add(listenerInstruction);
                var plannerQueue = PEMBus.Queues.Where(queue => queue.ForModuleType == PipelineModuleType.Planner).FirstOrDefault();

                var planner            = PEMBus.Instance.Solution.Value.Planner.Value;
                var plannerInstruction = new PipelineExecutionInstruction()
                {
                    Name     = "Planner",
                    Type     = "Planner",
                    QueueId  = plannerQueue.PipelineModuleId,
                    Enqueued = DateTime.UtcNow.ToJSONString()
                };

                message.CurrentInstruction = plannerInstruction;
                message.Instructions.Add(plannerInstruction);

                double?lat = null, lon = null;

                if (headers != null &&
                    headers.ContainsKey("x-latitude") && headers.ContainsKey("x-longitude"))
                {
                    if (double.TryParse(headers["x-latitude"], out double tmpLatitude) &&
                        double.TryParse(headers["x-longitude"], out double tmpLongitude))
                    {
                        lat = tmpLatitude;
                        lon = tmpLongitude;
                    }
                }

                var insertResult = await PEMBus.DeviceMediaStorage.StoreMediaItemAsync(stream, message.Id, contentType, contentLength, lat, lon);

                if (!insertResult.Successful)
                {
                    return(insertResult.ToInvokeResult());
                }

                message.MediaItemId = insertResult.Result;

                await plannerQueue.EnqueueAsync(message);

                return(InvokeResult.Success);
            }
            catch (Exception ex)
            {
                PEMBus.InstanceLogger.AddException("ListenerModule_AddBinaryMessageAsync", ex);
                return(InvokeResult.FromException("ListenerModule_AddBinaryMessageAsync", ex));
            }
        }
Ejemplo n.º 4
0
        public async Task <InvokeResult> AddStringMessageAsync(string buffer, DateTime startTimeStamp, string path = "", string deviceId = "", string topic = "", Dictionary <string, string> headers = null)
        {
            try
            {
                var message = new PipelineExecutionMessage()
                {
                    PayloadType       = MessagePayloadTypes.Text,
                    TextPayload       = buffer,
                    CreationTimeStamp = startTimeStamp.ToJSONString()
                };

                var headerLength = 0;

                if (headers != null)
                {
                    if (headers.ContainsKey("method"))
                    {
                        message.Envelope.Method = headers["method"];
                    }

                    if (headers.ContainsKey("topic"))
                    {
                        message.Envelope.Topic = headers["topic"];

                        foreach (var header in headers)
                        {
                            headerLength += header.Key.Length + (String.IsNullOrEmpty(header.Value) ? 0 : header.Value.Length);
                        }
                    }

                    if (headers != null)
                    {
                        foreach (var hdr in headers)
                        {
                            message.Envelope.Headers.Add(hdr.Key, hdr.Value);
                        }
                    }
                }

                message.PayloadLength = String.IsNullOrEmpty(buffer) ? 0 : buffer.Length;

                var bytesProcessed = message.PayloadLength + (String.IsNullOrEmpty(path) ? 0 : path.Length) + headerLength;

                Metrics.BytesProcessed += bytesProcessed;
                Metrics.MessagesProcessed++;

                var json = JsonConvert.SerializeObject(Metrics);

                /*
                 * Console.WriteLine("LISTENER => " + Id);
                 * Console.ForegroundColor = ConsoleColor.Yellow;
                 * Console.WriteLine(json);
                 * Console.WriteLine("----------------------------");
                 */

                message.Envelope.DeviceId = deviceId;
                message.Envelope.Path     = path;
                message.Envelope.Topic    = topic;

                var listenerInstruction = new PipelineExecutionInstruction()
                {
                    Name            = _listenerConfiguration.Name,
                    Type            = GetType().Name,
                    QueueId         = "N/A",
                    StartDateStamp  = startTimeStamp.ToJSONString(),
                    ProcessByHostId = ModuleHost.Id,
                    Enqueued        = startTimeStamp.ToJSONString(),
                    ExecutionTimeMS = (DateTime.UtcNow - startTimeStamp).TotalMilliseconds,
                };

                message.Instructions.Add(listenerInstruction);

                var planner            = PEMBus.Instance.Solution.Value.Planner.Value;
                var plannerInstruction = new PipelineExecutionInstruction()
                {
                    Name     = planner.Name,
                    Type     = "Planner",
                    QueueId  = _plannerQueue.InstanceId,
                    Enqueued = DateTime.UtcNow.ToJSONString()
                };

                message.CurrentInstruction = plannerInstruction;
                message.Instructions.Add(plannerInstruction);

                await _plannerQueue.EnqueueAsync(message);

                return(InvokeResult.Success);
            }
            catch (Exception ex)
            {
                PEMBus.InstanceLogger.AddException("ListenerModule_AddStringMessageAsync", ex);
                return(InvokeResult.FromException("ListenerModule_AddStringMessageAsync", ex));
            }
        }