Exemplo n.º 1
0
        public void Process(InvokeMethodArgs args)
        {
            Assert.ArgumentNotNull(args, nameof(args));
            Assert.ArgumentNotNull(args.Device, nameof(args.Device));
            Assert.ArgumentNotNull(args.Method, nameof(args.Method));

            // No two way - call the method directly
            if (!args.Method.TwoWay)
            {
                args.Response = InvokeMethod(args.Device, args.Method, args.Payload);
                return;
            }

            // Two way - gets from cache
            var resultFromCache = _methodCacheManager.GetResponseFromCache(args.Device, args.Method, args.Payload);

            if (!string.IsNullOrEmpty(resultFromCache))
            {
                args.Response = DeserializeAndParse(args.Method, resultFromCache);
                return;
            }

            // Or the method itself
            args.Response = InvokeMethod(args.Device, args.Method, args.Payload);

            // If taken from Itself then needs to save back to cache if TwoWay
            if (args.Response != null && !string.IsNullOrEmpty(args.Response.RawMessage))
            {
                _methodCacheManager.SaveResponseToCache(args.Device, args.Method, args.Payload, args.Response.RawMessage);
            }
        }
Exemplo n.º 2
0
        public void Process(MessageReceivedArgs args)
        {
            Assert.ArgumentNotNull(args, nameof(args));
            Assert.ArgumentNotNull(args.EventData, nameof(args.EventData));
            Assert.ArgumentNotNull(args.Database, nameof(args.Database));

            var eventData = args.EventData;

            if (eventData?.Body.Array == null)
            {
                return;
            }

            var data = Encoding.UTF8.GetString(eventData.Body.Array);

            Log.Info($"Message received on partition {args.Partition}: {data}", args.EventData);

            // Find the key that brings the method reference - won't handle the message if this is not found
            var methodKey = eventData.Properties.Keys.FirstOrDefault(p => p.ToLower() == MethodProperyName.ToLower());

            if (string.IsNullOrEmpty(methodKey))
            {
                return;
            }
            var methodNameOrPath = eventData.Properties[methodKey].ToString();
            var method           = _hubRepository.GetMethodByName(methodNameOrPath, args.Database);
            var device           = _hubRepository.GetDeviceByName(methodNameOrPath, args.Database);

            if (method == null || device == null)
            {
                return;
            }

            // Get payload from device (if any)
            var payloadKey = eventData.Properties.Keys.FirstOrDefault(p => p.ToLower() == PayloadProperyName.ToLower());
            var payload    = string.IsNullOrEmpty(payloadKey) ? string.Empty : eventData.Properties[payloadKey].ToString();

            // Store results on cache
            _methodCacheManager.SaveResponseToCache(device, method, payload, data);
        }