/// <summary>
            /// Creates a new FNV hash initialized to <see cref="Offset"/>.
            /// </summary>
            public static FnvHash Create()
            {
                var result = new FnvHash();

                result.HashCode = Offset;
                return(result);
            }
        public async Task <IActionResult> Get(string distName)
        {
            ConfigurationSettings settings = FabricRuntime.GetActivationContext().GetConfigurationPackageObject("Config").Settings;
            KeyedCollection <string, ConfigurationProperty> serviceParameters = settings.Sections["MyConfigSection"].Parameters;
            string reverseProxyPort = serviceParameters["ReverseProxyPort"].Value;
            string dataServiceName  = serviceParameters["CityServiceName"].Value;
            string serviceUri       = string.Format($"{FabricRuntime.GetActivationContext().ApplicationName}/{dataServiceName}");
            long   partitionKey     = FnvHash.Hash(distName);
            string proxyUrl         =
                $"http://localhost:{reverseProxyPort}/{serviceUri.Replace("fabric:/", "")}/api/citydata/{distName}?PartitionKind=Int64Range&PartitionKey={partitionKey}";

            ServiceEventSource.Current.Message(proxyUrl);

            HttpResponseMessage response = await new HttpClient().GetAsync(proxyUrl);

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                return(this.StatusCode((int)response.StatusCode));
            }

            string distData = await response.Content.ReadAsStringAsync();

            var districtReport = JsonConvert.DeserializeObject <DistrictReport>(distData);

            return(this.Json(districtReport));
        }
Exemple #3
0
        private async Task SendDistrictData()
        {
            ConfigurationSettings configurationSettings = FabricRuntime.GetActivationContext().GetConfigurationPackageObject("Config").Settings;
            KeyedCollection <string, ConfigurationProperty> serviceParameters = configurationSettings.Sections["DistrictsActorServiceReplicatorConfig"].Parameters;
            string cityServiceName  = serviceParameters["CityServiceName"].Value;
            string applicationName  = FabricRuntime.GetActivationContext().ApplicationName;
            string reverseProxyPort = serviceParameters["ReverseProxyPort"].Value;
            string serviceUri       = string.Format("{0}/{1}", applicationName, cityServiceName);
            string distName         = await this.StateManager.GetStateAsync <string>(DistrictName);

            List <string> tempList = await this.StateManager.GetStateAsync <List <string> >(TempRecords);

            long partitionKey = FnvHash.Hash(distName);
            //long partitionKey = -9223372036854775808;
            double averageTempData = GetAverageTempData(tempList);
            //ServicePartitionList partitions = await new FabricClient().QueryManager.GetPartitionListAsync(new Uri(serviceUri));

            string proxyUrl =
                $"http://localhost:{reverseProxyPort}/{serviceUri.Replace("fabric:/", "")}/api/citydata/{distName}?PartitionKind=Int64Range&PartitionKey={partitionKey}";

            ActorEventSource.Current.Message(proxyUrl);
            string        payload     = $"{{ 'AverageTemp' : '{averageTempData}' }}";
            StringContent postContent = new StringContent(payload, Encoding.UTF8, "application/json");

            postContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            HttpResponseMessage response = await new HttpClient().PostAsync(proxyUrl, postContent);

            ActorEventSource.Current.Message(response.ToString());
            ActorEventSource.Current.Message(string.Format("In District Actor id: {0}. District Name: {1}. Temp List: {2}.",
                                                           this.Id,
                                                           distName,
                                                           tempList.ToString()));
        }
Exemple #4
0
        public async Task <ActionResult> Devices()
        {
            // Manage session and Context
            HttpServiceUriBuilder contextUri = new HttpServiceUriBuilder().SetServiceName(this.context.ServiceName);

            if (HTTPHelper.IsSessionExpired(HttpContext, this))
            {
                return(Redirect(contextUri.GetServiceNameSiteHomePath()));
            }
            else
            {
                this.ViewData["TargetSite"]  = contextUri.GetServiceNameSite();
                this.ViewData["PageTitle"]   = "Devices";
                this.ViewData["HeaderTitle"] = "Devices Dashboard";

                string reportUniqueId = FnvHash.GetUniqueId();
                string reportName     = "PSG-VibrationDeviceReport-02"; // the dashboard

                EmbedConfig task = await ReportsHandler.GetEmbedReportConfigData(ClientId, GroupId, Username, Password, AuthorityUrl, ResourceUrl, ApiUrl, reportUniqueId, reportName, this.context, ServiceEventSource.Current);

                this.ViewData["EmbedToken"]     = task.EmbedToken.Token;
                this.ViewData["EmbedURL"]       = task.EmbedUrl;
                this.ViewData["EmbedId"]        = task.Id;
                this.ViewData["ReportUniqueId"] = "";

                return(this.View());
            }
        }
        public static async Task <object> ExecuteFabricGETForEntity(Type targetObjectType, string targetServiceType, string servicePathAndQuery, string entityName, ServiceContext serviceContext, HttpClient httpClient, CancellationToken cancellationToken, IServiceEventSource serviceEventSource = null)
        {
            object objRet = null;
            ServiceEventSourceHelper serviceEventSourceHelper = new ServiceEventSourceHelper(serviceEventSource);

            ServiceUriBuilder uriBuilder = new ServiceUriBuilder(targetServiceType);
            Uri  serviceUri = uriBuilder.Build();
            long targetSiteServicePartitionKey = FnvHash.Hash(entityName);
            Uri  getUrl = new HttpServiceUriBuilder()
                          .SetServiceName(serviceUri)
                          .SetPartitionKey(targetSiteServicePartitionKey)
                          .SetServicePathAndQuery(servicePathAndQuery)
                          .Build();

            HttpResponseMessage response = await httpClient.GetAsync(getUrl, cancellationToken);

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                serviceEventSourceHelper.ServiceMessage(serviceContext, $"On Execute Fabric GET - Service returned result[{response.StatusCode}] for entity[{entityName}] request[{servicePathAndQuery}]");
                objRet = response;
            }
            else
            {
                JsonSerializer serializer = new JsonSerializer();
                using (StreamReader streamReader = new StreamReader(await response.Content.ReadAsStreamAsync()))
                {
                    using (JsonTextReader jsonReader = new JsonTextReader(streamReader))
                    {
                        objRet = serializer.Deserialize(jsonReader, targetObjectType);
                    }
                }
            }

            return(objRet);
        }
Exemple #6
0
        private void CB_Location_SelectedIndexChanged(object sender, EventArgs e)
        {
            SaveEntry(entry);
            entry = CB_Location.SelectedIndex;
            var id    = Tables.EncounterTables[entry].ZoneID;
            var iname = LocIDTable.First(z => FnvHash.HashFnv1a_64(z.Key) == id).Key;

            L_Hash.Text = Tables.EncounterTables[entry].ZoneID.ToString("X16") + " " + iname;
            LoadEntry(entry);
        }
Exemple #7
0
    private static Dictionary <ulong, string> GenerateTableMap()
    {
        var result = new Dictionary <ulong, string>
        {
            [0xCBF29CE484222645] = "",
        };

        var prefixes = new[] { "eve", "fly", "gmk", "lnd", "mas", "oyb", "swm", "whl" };
        var kinds    = new[] { "ex", "no", "ra" };

        foreach (var prefix in prefixes)
        {
            foreach (var kind in kinds)
            {
                for (var i = 0; i < 150; i++)
                {
                    var name = $"{prefix}_{kind}_{i:00}";
                    var hash = FnvHash.HashFnv1a_64(name);
                    result[hash] = name;
                }
            }
        }

        for (var area = 0; area < 6; area++)
        {
            for (var i = 0; i < 10; i++)
            {
                result[FnvHash.HashFnv1a_64($"sky_area{area}_{i:00}")] = $"sky_area{area}_{i:00}";
            }
        }

        result[FnvHash.HashFnv1a_64("eve_ex_16_b")] = "eve_ex_16_b";
        result[FnvHash.HashFnv1a_64("eve_ex_17_b")] = "eve_ex_17_b";
        result[FnvHash.HashFnv1a_64("eve_ex_18_b")] = "eve_ex_18_b";

        var gimmicks = new[] { "no", "tree", "rock", "crystal", "snow", "box" };

        foreach (var gimmick in gimmicks)
        {
            for (var i = 0; i < 100; i++)
            {
                result[FnvHash.HashFnv1a_64($"gmk_{gimmick}_{i:00}")] = $"gmk_{gimmick}_{i:00}";
                for (var j = 0; j < 3; j++)
                {
                    result[FnvHash.HashFnv1a_64($"gmk_{gimmick}_{i:00}_{j:00}")] = $"gmk_{gimmick}_{i:00}_{j:00}";
                    for (var k = 0; k < 3; k++)
                    {
                        result[FnvHash.HashFnv1a_64($"gmk_{gimmick}_{i:00}{j:00}_{k:00}")] = $"gmk_{gimmick}_{i:00}{j:00}_{k:00}";
                    }
                }
            }
        }

        return(result);
    }
    private static IReadOnlyDictionary <ulong, string> GenerateSpawnerNameMap()
    {
        var result = new Dictionary <ulong, string>();

        result[FnvHash.HashFnv1a_64("hoge")] = "hoge";

        var gimmicks = new[] { "no", "tree", "rock", "crystal", "snow", "box", "leaves_r", "leaves_g", "yachi" };

        foreach (var gimmick in gimmicks)
        {
            result[FnvHash.HashFnv1a_64($"{gimmick}")] = $"{gimmick}";
            for (var which = 0; which < 20; which++)
            {
                result[FnvHash.HashFnv1a_64($"{gimmick}{which:00}")]  = $"{gimmick}{which:00}";
                result[FnvHash.HashFnv1a_64($"{gimmick}_{which:00}")] = $"{gimmick}_{which:00}";
                for (var i = 0; i < 100; i++)
                {
                    result[FnvHash.HashFnv1a_64($"{gimmick}_{which:00}_{i:00}")]   = $"{gimmick}_{which:00}_{i:00}";
                    result[FnvHash.HashFnv1a_64($"{gimmick}_{which:00}_ex{i:00}")] = $"{gimmick}_{which:00}_ex{i:00}";
                    for (var j = 0; j < 3; j++)
                    {
                        result[FnvHash.HashFnv1a_64($"{gimmick}_{which:00}_{i:00}_{j:00}")]   = $"{gimmick}_{which:00}_{i:00}_{j:00}";
                        result[FnvHash.HashFnv1a_64($"{gimmick}_{which:00}_ex{i:00}_{j:00}")] = $"{gimmick}_{which:00}_ex{i:00}_{j:00}";
                        for (var k = 0; k < 3; k++)
                        {
                            result[FnvHash.HashFnv1a_64($"{gimmick}_{which:00}_{i:00}{j:00}_{k:00}")] = $"{gimmick}_{which:00}_{i:00}{j:00}_{k:00}";
                        }
                    }
                }
            }

            for (var w1 = 0; w1 < 5; w1++)
            {
                for (var w2 = 0; w2 < 5; w2++)
                {
                    result[FnvHash.HashFnv1a_64($"{gimmick}_{w1:00}_{w2:00}")] = $"{gimmick}_{w1:00}_{w2:00}";
                    for (var i = 0; i < 100; i++)
                    {
                        result[FnvHash.HashFnv1a_64($"{gimmick}_{w1:00}_{w2:00}_{i:00}")] = $"{gimmick}_{w1:00}_{w2:00}_{i:00}";
                        for (var j = 0; j < 3; j++)
                        {
                            result[FnvHash.HashFnv1a_64($"{gimmick}_{w1:00}_{w2:00}_{i:00}_{j:00}")] = $"{gimmick}_{w1:00}_{w2:00}_{i:00}_{j:00}";
                            for (var k = 0; k < 3; k++)
                            {
                                result[FnvHash.HashFnv1a_64($"{gimmick}_{w1:00}_{w2:00}_{i:00}{j:00}_{k:00}")] = $"{gimmick}_{w1:00}_{w2:00}_{i:00}{j:00}_{k:00}";
                            }
                        }
                    }
                }
            }
        }

        return(result);
    }
            public int GetHashCode(byte[] obj)
            {
                var hash = FnvHash.Create();

                foreach (var t in obj)
                {
                    hash.Combine(t);
                }

                return(hash.HashCode);
            }
        public async Task Post(string jobName, string parameters)
        {
            FnvHash fnv          = new FnvHash();
            long    partitionKey = (long)fnv.Hash(Encoding.UTF8.GetBytes(jobName));

            HttpClient client = new HttpClient(new HttpServiceClientHandler());

            Uri serviceUri = new HttpServiceUriBuilder()
                             .SetServiceName(new ServiceUriBuilder("WorkService").Build())
                             .SetPartitionKey(partitionKey)
                             .SetServicePathAndQuery($"api/work/{jobName}/{parameters}")
                             .Build();

            await client.PostAsync(serviceUri, new StringContent(String.Empty));
        }
Exemple #11
0
        public long FindPartitionKey(string id)
        {
            long partition;

            if (_dictionary.ContainsKey(id))
            {
                partition = _dictionary[id];
            }
            else
            {
                partition = FnvHash.Hash(id);
                _dictionary.Add(id, partition);
            }

            return(partition);
        }
Exemple #12
0
        /// <summary>
        /// Verifica se uma imagem foi gerada pelo sistema
        /// A verificação é salva no arquivo de Log
        /// </summary>
        /// <param name="args"></param>
        private void ImageIsUdyat(string[] args)
        {
            if (Path.GetExtension(args[2]).ToUpper() != ".PNG")
            {
                Log.AddLog("-VI" + args[2], "Imagem não verificada. Somente arquivos PNG.");
                Util.ShowConsoleMessage("Somente imagens PNG podem ser verificadas.", showConsoleMessages);
                Environment.Exit(0);
            }
            if ((args.Length >= 3) && (args[2].Length > 0) && (File.Exists(args[2])))
            {
                string auxFileName = args[2];
                Bitmap teste       = new Bitmap(auxFileName);

                string msg = SteganographyHelper.extractText(teste);
                if (msg.Length > UDYATSIGN.Length)
                {
                    string UdyatSign = MsgValue(msg, out msg);
                    if (UdyatSign == UDYATSIGN)
                    {
                        string   customerIdent  = MsgValue(msg, out msg);
                        string   customerMacNum = MsgValue(msg, out msg);
                        string   customerHash   = MsgValue(msg, out msg);
                        string   machineUID     = MsgValue(msg, out msg);
                        string   embedfileName  = msg.Substring(0, msg.Length);
                        string   strUserName    = MsgValue(msg, out msg);
                        string   macAddress     = MsgValue(msg, out msg);
                        string   macIP          = MsgValue(msg, out msg);
                        string   imgSeqMonitor  = msg.Substring(22, msg.Length - 22).Substring(0, msg.Substring(22, msg.Length - 22).IndexOf("."));
                        string   prtDateTime    = msg.Substring(0, 21);
                        DateTime outputDateTimeValue;
                        string   strDateTime;
                        if (DateTime.TryParseExact(prtDateTime, "yyyyMMdd_HH_mm_ss_fff", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out outputDateTimeValue))
                        {
                            strDateTime = outputDateTimeValue.ToString("dd/MM/yyyy HH:mm:ss FFF");
                        }
                        string customerWord = sysConfig.Data.CustomerWord;
                        string wordClienth  = FnvHash.GetHash(customerWord, 120).ToHexString();
                        bool   testSecurity = ((wordClienth == customerHash) && (embedfileName == auxFileName));
                        Log.AddLog("-VI " + args[2], "Imagem válida");
                        return;
                    }
                }
                Log.AddLog("-VI" + args[2], "Imagem inválida");
                return;
            }
            Log.AddLog("-VI", "Parâmetros inválidos");
        }
        public static async Task <bool> ExecuteFabricPOSTForEntity(Type targetObjectType, string targetServiceType, string servicePathAndQuery, string entityName, object bodyObject, ServiceContext serviceContext, HttpClient httpClient, CancellationToken cancellationToken, IServiceEventSource serviceEventSource = null)
        {
            bool bRet = false;
            ServiceEventSourceHelper serviceEventSourceHelper = new ServiceEventSourceHelper(serviceEventSource);

            ServiceUriBuilder uriBuilder = new ServiceUriBuilder(targetServiceType);
            Uri  serviceUri = uriBuilder.Build();
            long targetSiteServicePartitionKey = FnvHash.Hash(entityName);

            Uri postUrl = new HttpServiceUriBuilder()
                          .SetServiceName(serviceUri)
                          .SetPartitionKey(targetSiteServicePartitionKey)
                          .SetServicePathAndQuery(servicePathAndQuery)
                          .Build();

            string       jsonStr = JsonConvert.SerializeObject(bodyObject);
            MemoryStream mStrm   = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr));

            using (StreamContent postContent = new StreamContent(mStrm))
            {
                postContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                HttpResponseMessage response = await httpClient.PostAsync(postUrl, postContent, cancellationToken);

                if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
                {
                    // This service expects the receiving target site service to return HTTP 400 if the device message was malformed.
                    // In this example, the message is simply logged.
                    // Your application should handle all possible error status codes from the receiving service
                    // and treat the message as a "poison" message.
                    // Message processing should be allowed to continue after a poison message is detected.

                    string responseContent = await response.Content.ReadAsStringAsync();

                    serviceEventSourceHelper.ServiceMessage(serviceContext, $"On Execute Fabric POST - Service returned BAD REQUEST for entity[{entityName}] request[{servicePathAndQuery}] result=[{responseContent} requestBody[{await postContent.ReadAsStringAsync()}]");
                }
                else
                {
                    bRet = true;
                }
            }

            return(bRet);
        }
        /// <summary>
        /// This is the main entry point for your service replica.
        /// This method executes when this replica of your service becomes primary and has write status.
        /// </summary>
        /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service replica.</param>
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            // Get the IoT Hub connection string from the Settings.xml config file
            // from a configuration package named "Config"
            string iotHubConnectionString =
                this.Context.CodePackageActivationContext
                .GetConfigurationPackageObject("Config")
                .Settings
                .Sections["IoTHubConfigInformation"]
                .Parameters["ConnectionString"]
                .Value;
            string iotHubProcessOnlyFutureEvents =
                this.Context.CodePackageActivationContext
                .GetConfigurationPackageObject("Config")
                .Settings
                .Sections["IoTHubConfigInformation"]
                .Parameters["ProcessOnlyFutureEvents"]
                .Value.ToLower();

            ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - Starting service  - Process Only Future Events[{iotHubProcessOnlyFutureEvents}] - IoTHub Connection String[{iotHubConnectionString}]");

            // These Reliable Dictionaries are used to keep track of our position in IoT Hub.
            // If this service fails over, this will allow it to pick up where it left off in the event stream.
            IReliableDictionary <string, string> offsetDictionary =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <string, string> >(OffsetDictionaryName);

            IReliableDictionary <string, long> epochDictionary =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <string, long> >(EpochDictionaryName);

            // Each partition of this service corresponds to a partition in IoT Hub.
            // IoT Hub partitions are numbered 0..n-1, up to n = 32.
            // This service needs to use an identical partitioning scheme.
            // The low key of every partition corresponds to an IoT Hub partition.
            Int64RangePartitionInformation partitionInfo = (Int64RangePartitionInformation)this.Partition.PartitionInfo;
            long servicePartitionKey = partitionInfo.LowKey;

            EventHubReceiver eventHubReceiver = null;
            MessagingFactory messagingFactory = null;

            try
            {
                // HttpClient is designed as a shared object.
                // A single instance should be used throughout the lifetime of RunAsync.
                using (HttpClient httpClient = new HttpClient(new HttpServiceClientHandler()))
                {
                    int  offsetIteration = 0;
                    bool IsConnected     = false;

                    while (true)
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        if (!IsConnected)
                        {
                            // Get an EventHubReceiver and the MessagingFactory used to create it.
                            // The EventHubReceiver is used to get events from IoT Hub.
                            // The MessagingFactory is just saved for later so it can be closed before RunAsync exits.
                            Tuple <EventHubReceiver, MessagingFactory> iotHubInfo = await this.ConnectToIoTHubAsync(iotHubConnectionString, servicePartitionKey, epochDictionary, offsetDictionary, iotHubProcessOnlyFutureEvents);

                            eventHubReceiver = iotHubInfo.Item1;
                            messagingFactory = iotHubInfo.Item2;

                            IsConnected = true;
                        }

                        Uri postUrl = null;

                        try
                        {
                            // It's important to set a low wait time here in lieu of a cancellation token
                            // so that this doesn't block RunAsync from exiting when Service Fabric needs it to complete.
                            // ReceiveAsync is a long-poll operation, so the timeout should not be too low,
                            // yet not too high to block RunAsync from exiting within a few seconds.
                            using (EventData eventData = await eventHubReceiver.ReceiveAsync(TimeSpan.FromSeconds(5)))
                            {
                                if (eventData == null)
                                {
                                    ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - No event data available on hub '{eventHubReceiver.Name}'");
                                    await Task.Delay(global::Iot.Common.Names.IoTHubRetryWaitIntervalsInMills);

                                    continue;
                                }
                                else
                                {
                                    ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - Received event data from hub '{eventHubReceiver.Name}' - Enqueued Time[{eventData.EnqueuedTimeUtc}] - Partition '{eventData.PartitionKey}' Sequence # '{eventData.SequenceNumber}'");
                                }

                                string targetSite = (string)eventData.Properties[global::Iot.Common.Names.EventKeyFieldTargetSite];
                                string deviceId   = (string)eventData.Properties[global::Iot.Common.Names.EventKeyFieldDeviceId];

                                // This is the named service instance of the target site data service that the event should be sent to.
                                // The targetSite id is part of the named service instance name.
                                // The incoming device data stream specifie which target site the data belongs to.
                                string prefix                        = global::Iot.Common.Names.InsightApplicationNamePrefix;
                                string serviceName                   = global::Iot.Common.Names.InsightDataServiceName;
                                Uri    targetSiteServiceName         = new Uri($"{prefix}/{targetSite}/{serviceName}");
                                long   targetSiteServicePartitionKey = FnvHash.Hash(deviceId);

                                ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - About to post data to Insight Data Service from device '{deviceId}' to target site '{targetSite}' - partitionKey '{targetSiteServicePartitionKey}' - Target Service Name '{targetSiteServiceName}'");

                                // The target site data service exposes an HTTP API.
                                // For incoming device events, the URL is /api/events/{deviceId}
                                // This sets up a URL and sends a POST request with the device JSON payload.
                                postUrl = new HttpServiceUriBuilder()
                                          .SetServiceName(targetSiteServiceName)
                                          .SetPartitionKey(targetSiteServicePartitionKey)
                                          .SetServicePathAndQuery($"/api/events/{deviceId}")
                                          .Build();

                                ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - Ready to post data to Insight Data Service from device '{deviceId}' to taget site '{targetSite}' - partitionKey '{targetSiteServicePartitionKey}' - Target Service Name '{targetSiteServiceName}' - url '{postUrl.PathAndQuery}'");

                                // The device stream payload isn't deserialized and buffered in memory here.
                                // Instead, we just can just hook the incoming stream from Iot Hub right into the HTTP request stream.
                                using (Stream eventStream = eventData.GetBodyStream())
                                {
                                    using (StreamContent postContent = new StreamContent(eventStream))
                                    {
                                        postContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                                        HttpResponseMessage response = await httpClient.PostAsync(postUrl, postContent, cancellationToken);

                                        if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
                                        {
                                            // This service expects the receiving target site service to return HTTP 400 if the device message was malformed.
                                            // In this example, the message is simply logged.
                                            // Your application should handle all possible error status codes from the receiving service
                                            // and treat the message as a "poison" message.
                                            // Message processing should be allowed to continue after a poison message is detected.

                                            string responseContent = await response.Content.ReadAsStringAsync();

                                            ServiceEventSource.Current.ServiceMessage(
                                                this.Context,
                                                $"RouterService - {ServiceUniqueId} - RunAsync - Insight service '{targetSiteServiceName}' returned HTTP 400 due to a bad device message from device '{deviceId}'. Error message: '{responseContent}'");
                                        }

                                        ServiceEventSource.Current.ServiceMessage(
                                            this.Context,
                                            $"RouterService - {ServiceUniqueId} - RunAsync - Sent event data to Insight service '{targetSiteServiceName}' with partition key '{targetSiteServicePartitionKey}'. Result: {response.StatusCode.ToString()}");
                                    }
                                }

                                // Save the current Iot Hub data stream offset.
                                // This will allow the service to pick up from its current location if it fails over.
                                // Duplicate device messages may still be sent to the the target site service
                                // if this service fails over after the message is sent but before the offset is saved.
                                if (++offsetIteration % OffsetInterval == 0)
                                {
                                    ServiceEventSource.Current.ServiceMessage(
                                        this.Context,
                                        $"RouterService - {ServiceUniqueId} - RunAsync - Saving offset {eventData.Offset}");

                                    using (ITransaction tx = this.StateManager.CreateTransaction())
                                    {
                                        await offsetDictionary.SetAsync(tx, "offset", eventData.Offset);

                                        await tx.CommitAsync();
                                    }

                                    offsetIteration = 0;
                                }
                            }
                        }
                        catch (Microsoft.ServiceBus.Messaging.ReceiverDisconnectedException rde)
                        {
                            // transient error. Retry.
                            ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - Receiver Disconnected Exception in RunAsync: {rde.ToString()}");

                            IsConnected = false;
                        }
                        catch (TimeoutException te)
                        {
                            // transient error. Retry.
                            ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - TimeoutException in RunAsync: {te.ToString()}");
                        }
                        catch (FabricTransientException fte)
                        {
                            // transient error. Retry.
                            ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - FabricTransientException in RunAsync: {fte.ToString()}");
                        }
                        catch (FabricNotPrimaryException fnpe)
                        {
                            ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - FabricNotPrimaryException Exception - Message=[{fnpe}]");

                            // not primary any more, time to quit.
                            return;
                        }
                        catch (Exception ex)
                        {
                            IsConnected = false;
                            string url = postUrl == null ? "Url undefined" : postUrl.ToString();
                            //ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - General Exception Url=[{url}]- Message=[{ex}] - Inner Exception=[{ex.InnerException.Message ?? "ex.InnerException is null"}] Call Stack=[{ex.StackTrace ?? "ex.StackTrace is null"}] - Stack trace of inner exception=[{ex.InnerException.StackTrace ?? "ex.InnerException.StackTrace is null"}]");

                            ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - General Exception Message[{ex.Message}] for url[{url}]");
                        }
                    }
                }
            }
            finally
            {
                if (messagingFactory != null)
                {
                    await messagingFactory.CloseAsync();
                }
            }
        }
Exemple #15
0
        public async Task <IActionResult> EmbedReport(string reportName, string reportParm = null, string reportParmStart = null, string reportParmEnd = null, int numberOfObservations = (-1), int minMagnitudeAllowed = 1)
        {
            // Manage session and Context
            HttpServiceUriBuilder contextUri = new HttpServiceUriBuilder().SetServiceName(this.context.ServiceName);

            ViewBag.RedirectURL = "";

            if (HTTPHelper.IsSessionExpired(HttpContext, this))
            {
                return(Ok(contextUri.GetServiceNameSiteHomePath()));
            }
            else
            {
                this.ViewData["TargetSite"]  = contextUri.GetServiceNameSite();
                this.ViewData["PageTitle"]   = "Report";
                this.ViewData["HeaderTitle"] = "Last Posted Events";

                string reportUniqueId = FnvHash.GetUniqueId();

                // Now it is time to refresh the data set
                List <DeviceViewModelList> deviceViewModelList = null;
                int  resampleSetsLimit = 0;
                var  refreshDataresult = false;
                bool publishReportData = true;

                if (reportName.Equals("PSG-VibrationDeviceReport-02"))
                {
                    refreshDataresult = true;
                    publishReportData = false;
                }
                else if (reportName.Equals("PSG-VibrationDeviceReport-01") && reportParm != null)
                {
                    deviceViewModelList = await DevicesController.GetDevicesDataAsync(reportParm, httpClient, fabricClient, appLifetime);
                }
                else
                {
                    resampleSetsLimit = 1;

                    deviceViewModelList = new List <DeviceViewModelList>();
                    ServiceUriBuilder uriBuilder = new ServiceUriBuilder(Names.InsightDataServiceName);
                    Uri serviceUri = uriBuilder.Build();

                    // service may be partitioned.
                    // this will aggregate device IDs from all partitions
                    ServicePartitionList partitions = await fabricClient.QueryManager.GetPartitionListAsync(serviceUri);

                    foreach (Partition partition in partitions)
                    {
                        string pathAndQuery      = null;
                        int    index             = 0;
                        float  indexInterval     = 1F;
                        bool   keepLooping       = true;
                        int    observationsCount = 0;
                        int    batchIndex        = 0;
                        int    batchSize         = 10000;


                        while (keepLooping)
                        {
                            if (reportParmEnd == null)
                            {
                                pathAndQuery = $"/api/devices/history/byKey/{reportParmStart}";
                                keepLooping  = false;
                            }
                            else if (numberOfObservations != (-1))
                            {
                                pathAndQuery = $"/api/devices/history/byKeyRange/{reportParmStart}/{reportParmEnd}/{batchIndex}/{batchSize}";

                                if (index == 0)
                                {
                                    string getCountPathAndQuery = $"/api/devices/history/count/interval/{reportParmStart}/{reportParmEnd}";
                                    Uri    getCountUrl          = new HttpServiceUriBuilder()
                                                                  .SetServiceName(serviceUri)
                                                                  .SetPartitionKey(((Int64RangePartitionInformation)partition.PartitionInformation).LowKey)
                                                                  .SetServicePathAndQuery(getCountPathAndQuery)
                                                                  .Build();

                                    HttpResponseMessage localResponse = await httpClient.GetAsync(getCountUrl, appLifetime.ApplicationStopping);

                                    if (localResponse.StatusCode == System.Net.HttpStatusCode.OK)
                                    {
                                        string localResult = await localResponse.Content.ReadAsStringAsync();

                                        long count = Int64.Parse(localResult);

                                        indexInterval = count / numberOfObservations;

                                        if (indexInterval < 1)
                                        {
                                            indexInterval = 1;
                                        }
                                    }
                                }
                            }
                            else if (reportParmEnd != null)
                            {
                                pathAndQuery = $"/api/devices/history/byKeyRange/{reportParmStart}/{reportParmEnd}";
                                keepLooping  = false;
                            }

                            Uri getUrl = new HttpServiceUriBuilder()
                                         .SetServiceName(serviceUri)
                                         .SetPartitionKey(((Int64RangePartitionInformation)partition.PartitionInformation).LowKey)
                                         .SetServicePathAndQuery(pathAndQuery)
                                         .Build();

                            HttpResponseMessage response = await httpClient.GetAsync(getUrl, appLifetime.ApplicationStopping);

                            if (response.StatusCode == System.Net.HttpStatusCode.OK)
                            {
                                JsonSerializer serializer = new JsonSerializer();
                                using (StreamReader streamReader = new StreamReader(await response.Content.ReadAsStreamAsync()))
                                {
                                    using (JsonTextReader jsonReader = new JsonTextReader(streamReader))
                                    {
                                        List <DeviceViewModelList> localResult = serializer.Deserialize <List <DeviceViewModelList> >(jsonReader);

                                        if (localResult != null)
                                        {
                                            if (localResult.Count != 0)
                                            {
                                                foreach (DeviceViewModelList device in localResult)
                                                {
                                                    if (index >= (observationsCount * indexInterval))
                                                    {
                                                        deviceViewModelList.Add(device);
                                                        observationsCount++;
                                                    }
                                                    index++;

                                                    if (numberOfObservations != (-1))
                                                    {
                                                        if (observationsCount == numberOfObservations)
                                                        {
                                                            keepLooping = false;
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                keepLooping = false;
                                            }
                                        }
                                        else
                                        {
                                            keepLooping = false;
                                        }
                                    }
                                }
                            }
                            batchIndex += batchSize;
                        }
                    }
                }

                if (publishReportData)
                {
                    refreshDataresult = await ReportsHandler.PublishReportDataFor(reportUniqueId, DevicesDataStream01URL, deviceViewModelList, context, httpClient, appLifetime.ApplicationStopping, ServiceEventSource.Current, resampleSetsLimit, minMagnitudeAllowed);
                }

                if (reportName.Equals("PSG-VibrationDeviceReport-02"))
                {
                    reportUniqueId = "";
                }

                EmbedConfig task = await ReportsHandler.GetEmbedReportConfigData(ClientId, GroupId, Username, Password, AuthorityUrl, ResourceUrl, ApiUrl, reportUniqueId, reportName, this.context, ServiceEventSource.Current);

                this.ViewData["EmbedToken"]     = task.EmbedToken.Token;
                this.ViewData["EmbedURL"]       = task.EmbedUrl;
                this.ViewData["EmbedId"]        = task.Id;
                this.ViewData["ReportUniqueId"] = reportUniqueId;

                return(this.View());
            }
        }
Exemple #16
0
 public bool IsMatch(string fileName) => FnvHash.HashFnv1a_64(fileName) == HashFnv1aPathFileName;
        public async Task <IActionResult> CreateEntity(string name, string key, [FromBody] UserProfile userProfile)
        {
            bool bRet = false;

            if (String.IsNullOrEmpty(name) || String.IsNullOrEmpty(key))
            {
                return(this.BadRequest());
            }

            if (userProfile != null)
            {
                Debug.WriteLine("On CreateEntity postContent=[" + userProfile.ToString() + "]");
            }
            else
            {
                Debug.WriteLine("On CreateEntity postContent=[ userProfile is null ]");
            }

            if (userProfile == null)
            {
                return(this.BadRequest());
            }

            string id   = FnvHash.GetUniqueId();
            User   user = new User();

            user.Id              = id;
            user.Username        = userProfile.UserName;
            user.FirstName       = userProfile.FirstName;
            user.LastName        = userProfile.LastName;
            user.Password        = userProfile.Password;
            user.PasswordCreated = true;

            IReliableDictionary <string, string> identitiesDictionary = await this.stateManager.GetOrAddAsync <IReliableDictionary <string, string> >(Names.IdentitiesDictionaryName);

            IReliableDictionary <string, User> entitiesDictionary = await this.stateManager.GetOrAddAsync <IReliableDictionary <string, User> >(Names.EntitiesDictionaryName);

            using (ITransaction tx = this.stateManager.CreateTransaction())
            {
                int retryCount = 1;

                while (retryCount > 0)
                {
                    try
                    {
                        await identitiesDictionary.AddAsync(tx, userProfile.UserName, id);

                        await entitiesDictionary.AddAsync(tx, id, user);

                        // Commit
                        await tx.CommitAsync();

                        retryCount = 0;
                    }
                    catch (TimeoutException te)
                    {
                        // transient error. Could Retry if one desires .
                        ServiceEventSource.Current.ServiceMessage(this.context, $"DataService - CreateEntity(Save) - TimeoutException : Retry Count#{retryCount}: Message=[{te.ToString()}]");

                        if (global::Iot.Common.Names.TransactionsRetryCount > retryCount)
                        {
                            retryCount = 0;
                        }
                        else
                        {
                            retryCount++;

                            await Task.Delay(global::Iot.Common.Names.TransactionRetryWaitIntervalInMills *(int)Math.Pow(2, retryCount));
                        }
                    }
                    catch (Exception ex)
                    {
                        ServiceEventSource.Current.ServiceMessage(this.context, $"DataService - CreateEntity(Save) - General Exception - Message=[{0}]", ex);
                        retryCount = 0;
                        tx.Abort();
                    }
                }
            }

            // now let's check if the commits have finished
            using (ITransaction tx = this.stateManager.CreateTransaction())
            {
                try
                {
                    bool keepReading = true;
                    while (keepReading)
                    {
                        var result = await identitiesDictionary.TryGetValueAsync(tx, userProfile.UserName);

                        if (result.Value.Equals(id))
                        {
                            await tx.CommitAsync();

                            bRet = true;
                            break;
                        }
                        Thread.Sleep(1000);
                    }
                }
                catch (TimeoutException te)
                {
                    // transient error. Could Retry if one desires .
                    ServiceEventSource.Current.ServiceMessage(this.context, $"DataService - CreateEntity(Wait Save) - TimeoutException : Message=[{te.ToString()}]");
                }
                catch (Exception ex)
                {
                    ServiceEventSource.Current.ServiceMessage(this.context, $"DataService - CreateEntity(Wait Save) - General Exception - Message=[{0}]", ex);
                    tx.Abort();
                }
            }

            return(this.Ok(bRet));
        }
Exemple #18
0
 private static uint Hash(ReadOnlySpan <byte> name) => (uint)FnvHash.HashFnv1a_64(name);
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            // Get the IoT Hub connection string from the Settings.xml config file
            // from a configuration package named "Config"
            string PublishDataServiceURLs =
                this.Context.CodePackageActivationContext
                .GetConfigurationPackageObject("Config")
                .Settings
                .Sections["ExtenderServiceConfigInformation"]
                .Parameters["PublishDataServiceURLs"]
                .Value.Trim('/');

            ServiceEventSource.Current.ServiceMessage(this.Context, $"ExtenderService - {ServiceUniqueId} - RunAsync - Starting service  - Data Service URLs[{PublishDataServiceURLs}]");

            DateTimeOffset currentSearchStartingTime = DateTimeOffset.UtcNow.AddHours(-1);

            if (PublishDataServiceURLs != null && PublishDataServiceURLs.Length > 0)
            {
                string[] routingparts = PublishDataServiceURLs.Split(';');
                int      currentValueForIntervalEnd = global::Iot.Common.Names.ExtenderStandardRetryWaitIntervalsInMills;

                using (HttpClient httpClient = new HttpClient(new HttpServiceClientHandler()))
                {
                    while (true)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string reportUniqueId = FnvHash.GetUniqueId();
                        int    messageCount   = 1;

                        while (messageCount > 0)
                        {
                            try
                            {
                                DateTimeOffset startTime           = currentSearchStartingTime;
                                long           searchIntervalStart = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - currentSearchStartingTime.ToUnixTimeMilliseconds() - 500; // this last adjusment is only to compensate for latency around the calls
                                long           searchIntervalEnd   = searchIntervalStart - currentValueForIntervalEnd;

                                if (searchIntervalEnd < 0)
                                {
                                    searchIntervalEnd          = 0;
                                    currentValueForIntervalEnd = global::Iot.Common.Names.ExtenderStandardRetryWaitIntervalsInMills;
                                }

                                DateTimeOffset endTime = DateTimeOffset.UtcNow.AddMilliseconds(searchIntervalEnd * (-1));

                                string servicePathAndQuery = $"/api/devices/history/interval/{searchIntervalStart}/{searchIntervalEnd}";

                                ServiceUriBuilder uriBuilder = new ServiceUriBuilder(routingparts[0], global::Iot.Common.Names.InsightDataServiceName);
                                Uri serviceUri = uriBuilder.Build();

                                ServiceEventSource.Current.ServiceMessage(this.Context, $"ExtenderService - {ServiceUniqueId} - RunAsync - About to call URL[{serviceUri}] to collect completed messages - Search[{servicePathAndQuery}] Time Start[{startTime}] End[{endTime}]");

                                // service may be partitioned.
                                // this will aggregate the queue lengths from each partition
                                System.Fabric.Query.ServicePartitionList partitions = await this.fabricClient.QueryManager.GetPartitionListAsync(serviceUri);

                                foreach (System.Fabric.Query.Partition partition in partitions)
                                {
                                    List <DeviceViewModelList> deviceViewModelList = new List <DeviceViewModelList>();
                                    Uri getUrl = new HttpServiceUriBuilder()
                                                 .SetServiceName(serviceUri)
                                                 .SetPartitionKey(((Int64RangePartitionInformation)partition.PartitionInformation).LowKey)
                                                 .SetServicePathAndQuery(servicePathAndQuery)
                                                 .Build();

                                    HttpResponseMessage response = await httpClient.GetAsync(getUrl, cancellationToken);

                                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                                    {
                                        JsonSerializer serializer = new JsonSerializer();
                                        using (StreamReader streamReader = new StreamReader(await response.Content.ReadAsStreamAsync()))
                                        {
                                            using (JsonTextReader jsonReader = new JsonTextReader(streamReader))
                                            {
                                                List <DeviceViewModelList> resultList = serializer.Deserialize <List <DeviceViewModelList> >(jsonReader);

                                                deviceViewModelList.AddRange(resultList);
                                            }
                                        }

                                        if (deviceViewModelList.Count > 0)
                                        {
                                            DeviceViewModelList lastItem = deviceViewModelList.ElementAt(deviceViewModelList.Count() - 1);

                                            messageCount = deviceViewModelList.Count;
                                            await ReportsHandler.PublishReportDataFor(reportUniqueId, routingparts[1], deviceViewModelList, this.Context, httpClient, cancellationToken, ServiceEventSource.Current, 1);

                                            ServiceEventSource.Current.ServiceMessage(this.Context, $"ExtenderService - {ServiceUniqueId} - RunAsync - Finished posting messages to report stream - Published total number of collected messages[{messageCount}]");
                                            currentSearchStartingTime  = endTime;
                                            currentValueForIntervalEnd = global::Iot.Common.Names.ExtenderStandardRetryWaitIntervalsInMills;
                                        }
                                        else
                                        {
                                            messageCount = 0;
                                            ServiceEventSource.Current.ServiceMessage(this.Context, $"ExtenderService - {ServiceUniqueId} - RunAsync - Could not find any messages in the interval from [{startTime}] to [{endTime}]");
                                        }
                                    }
                                    else
                                    {
                                        messageCount = 0;
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                ServiceEventSource.Current.ServiceMessage(this.Context, $"ExtenderService - {ServiceUniqueId} - RunAsync - Severe error when reading or sending messages to report stream - Exception[{ex}] - Inner Exception[{ex.InnerException}] StackTrace[{ex.StackTrace}]");
                            }
                        }

                        currentValueForIntervalEnd += global::Iot.Common.Names.ExtenderStandardRetryWaitIntervalsInMills;

                        DateTimeOffset boundaryTime = currentSearchStartingTime.AddMilliseconds(currentValueForIntervalEnd);

                        if (boundaryTime.CompareTo(DateTimeOffset.UtcNow) > 0)
                        {
                            await Task.Delay(global::Iot.Common.Names.ExtenderStandardRetryWaitIntervalsInMills);
                        }
                    }
                }
            }
            else
            {
                ServiceEventSource.Current.ServiceMessage(this.Context, $"ExtenderService - {ServiceUniqueId} - RunAsync - Starting service  - Data Service URLs[{PublishDataServiceURLs}]");
            }
        }
        /// <summary>
        /// This is the main entry point for your service replica.
        /// This method executes when this replica of your service becomes primary and has write status.
        /// </summary>
        /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service replica.</param>
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            // Get the IoT Hub connection string from the Settings.xml config file
            // from a configuration package named "Config"
            string iotHubConnectionString =
                this.Context.CodePackageActivationContext
                .GetConfigurationPackageObject("Config")
                .Settings
                .Sections["IoTHubConfigInformation"]
                .Parameters["ConnectionString"]
                .Value;

            // These Reliable Dictionaries are used to keep track of our position in IoT Hub.
            // If this service fails over, this will allow it to pick up where it left off in the event stream.
            IReliableDictionary <string, string> offsetDictionary =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <string, string> >(OffsetDictionaryName);

            IReliableDictionary <string, long> epochDictionary =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <string, long> >(EpochDictionaryName);

            // Each partition of this service corresponds to a partition in IoT Hub.
            // IoT Hub partitions are numbered 0..n-1, up to n = 32.
            // This service needs to use an identical partitioning scheme.
            // The low key of every partition corresponds to an IoT Hub partition.
            Int64RangePartitionInformation partitionInfo = (Int64RangePartitionInformation)this.Partition.PartitionInfo;
            long servicePartitionKey = partitionInfo.LowKey;

            EventHubReceiver eventHubReceiver = null;
            MessagingFactory messagingFactory = null;

            try
            {
                // Get an EventHubReceiver and the MessagingFactory used to create it.
                // The EventHubReceiver is used to get events from IoT Hub.
                // The MessagingFactory is just saved for later so it can be closed before RunAsync exits.
                Tuple <EventHubReceiver, MessagingFactory> iotHubInfo =
                    await this.ConnectToIoTHubAsync(iotHubConnectionString, servicePartitionKey, epochDictionary, offsetDictionary);

                eventHubReceiver = iotHubInfo.Item1;
                messagingFactory = iotHubInfo.Item2;

                // HttpClient is designed as a shared object.
                // A single instance should be used throughout the lifetime of RunAsync.
                using (HttpClient httpClient = new HttpClient(new HttpServiceClientHandler()))
                {
                    int offsetIteration = 0;

                    while (true)
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        try
                        {
                            // It's important to set a low wait time here in lieu of a cancellation token
                            // so that this doesn't block RunAsync from exiting when Service Fabric needs it to complete.
                            // ReceiveAsync is a long-poll operation, so the timeout should not be too low,
                            // yet not too high to block RunAsync from exiting within a few seconds.
                            using (EventData eventData = await eventHubReceiver.ReceiveAsync(TimeSpan.FromSeconds(5)))
                            {
                                if (eventData == null)
                                {
                                    continue;
                                }

                                string tenantId = (string)eventData.Properties["TenantID"];
                                string deviceId = (string)eventData.Properties["DeviceID"];

                                // This is the named service instance of the tenant data service that the event should be sent to.
                                // The tenant ID is part of the named service instance name.
                                // The incoming device data stream specifie which tenant the data belongs to.
                                Uri  tenantServiceName         = new Uri($"{Names.InsightApplicationNamePrefix}/{tenantId}/{Names.InsightDataServiceName}");
                                long tenantServicePartitionKey = FnvHash.Hash(deviceId);

                                // The tenant data service exposes an HTTP API.
                                // For incoming device events, the URL is /api/events/{deviceId}
                                // This sets up a URL and sends a POST request with the device JSON payload.
                                Uri postUrl = new HttpServiceUriBuilder()
                                              .SetServiceName(tenantServiceName)
                                              .SetPartitionKey(tenantServicePartitionKey)
                                              .SetServicePathAndQuery($"/api/events/{deviceId}")
                                              .Build();

                                // The device stream payload isn't deserialized and buffered in memory here.
                                // Instead, we just can just hook the incoming stream from Iot Hub right into the HTTP request stream.
                                using (Stream eventStream = eventData.GetBodyStream())
                                {
                                    using (StreamContent postContent = new StreamContent(eventStream))
                                    {
                                        postContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                                        HttpResponseMessage response = await httpClient.PostAsync(postUrl, postContent, cancellationToken);

                                        ServiceEventSource.Current.ServiceMessage(
                                            this.Context,
                                            "Sent event data to insight service '{0}' with partition key '{1}'. Result: {2}",
                                            tenantServiceName,
                                            tenantServicePartitionKey,
                                            response.StatusCode.ToString());

                                        if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
                                        {
                                            // This service expects the receiving tenant service to return HTTP 400 if the device message was malformed.
                                            // In this example, the message is simply logged.
                                            // Your application should handle all possible error status codes from the receiving service
                                            // and treat the message as a "poison" message.
                                            // Message processing should be allowed to continue after a poison message is detected.

                                            string responseContent = await response.Content.ReadAsStringAsync();

                                            ServiceEventSource.Current.ServiceMessage(
                                                this.Context,
                                                "Insight service '{0}' returned HTTP 400 due to a bad device message from device '{1}'. Error message: '{2}'",
                                                tenantServiceName,
                                                deviceId,
                                                responseContent);
                                        }
                                    }
                                }

                                // Save the current Iot Hub data stream offset.
                                // This will allow the service to pick up from its current location if it fails over.
                                // Duplicate device messages may still be sent to the the tenant service
                                // if this service fails over after the message is sent but before the offset is saved.
                                if (++offsetIteration % OffsetInterval == 0)
                                {
                                    ServiceEventSource.Current.ServiceMessage(
                                        this.Context,
                                        "Saving offset {0}",
                                        eventData.Offset);

                                    using (ITransaction tx = this.StateManager.CreateTransaction())
                                    {
                                        await offsetDictionary.SetAsync(tx, "offset", eventData.Offset);

                                        await tx.CommitAsync();
                                    }

                                    offsetIteration = 0;
                                }
                            }
                        }
                        catch (TimeoutException te)
                        {
                            // transient error. Retry.
                            ServiceEventSource.Current.ServiceMessage(this.Context, $"TimeoutException in RunAsync: {te.ToString()}");
                        }
                        catch (FabricTransientException fte)
                        {
                            // transient error. Retry.
                            ServiceEventSource.Current.ServiceMessage(this.Context, $"FabricTransientException in RunAsync: {fte.ToString()}");
                        }
                        catch (FabricNotPrimaryException)
                        {
                            // not primary any more, time to quit.
                            return;
                        }
                        catch (Exception ex)
                        {
                            ServiceEventSource.Current.ServiceMessage(this.Context, ex.ToString());

                            throw;
                        }
                    }
                }
            }
            finally
            {
                if (messagingFactory != null)
                {
                    await messagingFactory.CloseAsync();
                }
            }
        }
        public async Task <IActionResult> Post(string deviceId)
        {
            IActionResult resultRet       = this.Ok();
            DateTime      durationCounter = DateTime.UtcNow;
            TimeSpan      duration;
            string        traceId = FnvHash.GetUniqueId();

            Stream req         = Request.Body;
            string eventsArray = new StreamReader(req).ReadToEnd();

            if (String.IsNullOrEmpty(deviceId))
            {
                ServiceEventSource.Current.ServiceMessage(
                    this.context,
                    "Data Service - Received a Really Bad Request - device id not defined");
                return(this.BadRequest());
            }

            if (eventsArray == null)
            {
                ServiceEventSource.Current.ServiceMessage(this.context, $"Data Service - Received Bad Request from device {deviceId}");

                return(this.BadRequest());
            }

            DeviceMessage deviceMessage = EventRegistry.DeserializeEvents(deviceId, eventsArray, this.context, ServiceEventSource.Current);

            if (deviceMessage == null)
            {
                ServiceEventSource.Current.ServiceMessage(this.context, $"Data Service - Received Bad Request from device {deviceId} - Error parsing message body [{eventsArray}]");

                return(this.BadRequest());
            }

            ServiceEventSource.Current.ServiceMessage(
                this.context,
                $"Data Service - Received event from device {deviceId} for message type [{deviceMessage.MessageType}] timestamp [{deviceMessage.Timestamp}]- Traceid[{traceId}]");

            IReliableDictionary <string, DeviceMessage> storeLatestMessage = await this.stateManager.GetOrAddAsync <IReliableDictionary <string, DeviceMessage> >(TargetSolution.Names.EventLatestDictionaryName);

            IReliableDictionary <DateTimeOffset, DeviceMessage> storeCompletedMessages = await this.stateManager.GetOrAddAsync <IReliableDictionary <DateTimeOffset, DeviceMessage> >(TargetSolution.Names.EventHistoryDictionaryName);

            string               transactionType      = "";
            DeviceMessage        completedMessage     = null;
            DateTimeOffset       messageTimestamp     = DateTimeOffset.UtcNow;
            int                  retryCounter         = 1;
            MessageConfiguration messageConfiguration = EventRegistry.GetMessageConfiguration(deviceMessage.MessageType);

            try
            {
                while (retryCounter > 0)
                {
                    transactionType = "";
                    using (ITransaction tx = this.stateManager.CreateTransaction())
                    {
                        try
                        {
                            transactionType = "In Progress Message";

                            await storeLatestMessage.AddOrUpdateAsync(
                                tx,
                                deviceId,
                                deviceMessage,
                                (key, currentValue) =>
                            {
                                return(messageConfiguration.ManageDeviceEventSeriesContent(currentValue, deviceMessage, out completedMessage));
                            });

                            duration = DateTime.UtcNow.Subtract(durationCounter);
                            ServiceEventSource.Current.ServiceMessage(
                                this.context,
                                $"Data Service Received event from device {deviceId} - Finished [{transactionType}] - Duration [{duration.TotalMilliseconds}] mills - Traceid[{traceId}]");

                            await tx.CommitAsync();

                            retryCounter = 0;
                            duration     = DateTime.UtcNow.Subtract(durationCounter);
                            ServiceEventSource.Current.ServiceMessage(
                                this.context,
                                $"Data Service - Finish commits to message with timestamp [{completedMessage.Timestamp.ToString()}] from device {deviceId} - Duration [{duration.TotalMilliseconds}] mills - Traceid[{traceId}]");
                        }
                        catch (TimeoutException tex)
                        {
                            if (global::Iot.Common.Names.TransactionsRetryCount > retryCounter)
                            {
                                ServiceEventSource.Current.ServiceMessage(
                                    this.context,
                                    $"Data Service Timeout Exception when saving [{transactionType}] data from device {deviceId} - Iteration #{retryCounter} - Message-[{tex}] - Traceid[{traceId}]");

                                await Task.Delay(global::Iot.Common.Names.TransactionRetryWaitIntervalInMills *(int)Math.Pow(2, retryCounter));

                                retryCounter++;
                            }
                            else
                            {
                                ServiceEventSource.Current.ServiceMessage(
                                    this.context,
                                    $"Data Service Timeout Exception when saving [{transactionType}] data from device {deviceId} - Iteration #{retryCounter} - Transaction Aborted - Message-[{tex}] - Traceid[{traceId}]");

                                resultRet    = this.BadRequest();
                                retryCounter = 0;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.ServiceMessage(
                    this.context,
                    $"Data Service Exception when saving [{transactionType}] data from device {deviceId} - Message-[{ex}] - - Traceid[{traceId}]");
            }

            if (completedMessage != null)
            {
                transactionType = "Check Message Timestamp";
                retryCounter    = 1;
                while (retryCounter > 0)
                {
                    try
                    {
                        using (ITransaction tx = this.stateManager.CreateTransaction())
                        {
                            bool tryAgain = true;
                            while (tryAgain)
                            {
                                ConditionalValue <DeviceMessage> storedCompletedMessageValue = await storeCompletedMessages.TryGetValueAsync(tx, messageTimestamp, LockMode.Default);

                                duration = DateTime.UtcNow.Subtract(durationCounter);
                                ServiceEventSource.Current.ServiceMessage(
                                    this.context,
                                    $"Message Completed (Look for duplication - result [{storedCompletedMessageValue.HasValue}] from device {deviceId} - Starting [{transactionType}] - Duration [{duration.TotalMilliseconds}] mills - Traceid[{traceId}]");

                                if (storedCompletedMessageValue.HasValue)
                                {
                                    DeviceMessage storedCompletedMessage = storedCompletedMessageValue.Value;

                                    if (completedMessage.DeviceId.Equals(storedCompletedMessage.DeviceId))
                                    {
                                        tryAgain = false; // this means this record was already saved before - no duplication necessary
                                        ServiceEventSource.Current.ServiceMessage(
                                            this.context,
                                            $"Data Service - Message with timestamp {completedMessage.Timestamp.ToString()} from device {deviceId} already present in the store - (Ignore this duplicated record) - Traceid[{traceId}]");
                                        completedMessage = null;
                                    }
                                    else
                                    {
                                        // this is a true collision between information from different devices
                                        messageTimestamp = messageTimestamp.AddMilliseconds(10);
                                        ServiceEventSource.Current.ServiceMessage(
                                            this.context,
                                            $"Data Service - Message with timestamp {completedMessage.Timestamp.ToString()} from device {deviceId} already present in the store - (Adjusted the timestamp) - Traceid[{traceId}]");
                                    }
                                }
                                else
                                {
                                    tryAgain = false;
                                }
                            }
                            await tx.CommitAsync();

                            retryCounter = 0;
                        }
                    }
                    catch (TimeoutException tex)
                    {
                        if (global::Iot.Common.Names.TransactionsRetryCount > retryCounter)
                        {
                            ServiceEventSource.Current.ServiceMessage(
                                this.context,
                                $"Data Service Timeout Exception when saving [{transactionType}] data from device {deviceId} - Iteration #{retryCounter} - Message-[{tex}] - Traceid[{traceId}]");

                            await Task.Delay(global::Iot.Common.Names.TransactionRetryWaitIntervalInMills *(int)Math.Pow(2, retryCounter));

                            retryCounter++;
                        }
                        else
                        {
                            ServiceEventSource.Current.ServiceMessage(
                                this.context,
                                $"Data Service Timeout Exception when saving [{transactionType}] data from device {deviceId} - Iteration #{retryCounter} - Transaction Aborted - Message-[{tex}] - Traceid[{traceId}]");

                            resultRet    = this.BadRequest();
                            retryCounter = 0;
                        }
                    }
                }

                completedMessage.Timestamp = messageTimestamp;
                transactionType            = "Save Completed Message";
                retryCounter = 1;
                while (retryCounter > 0)
                {
                    try
                    {
                        using (ITransaction tx = this.stateManager.CreateTransaction())
                        {
                            await storeCompletedMessages.AddOrUpdateAsync(
                                tx,
                                completedMessage.Timestamp,
                                completedMessage,
                                (key, currentValue) =>
                            {
                                return(completedMessage);
                            }
                                );

                            duration = DateTime.UtcNow.Subtract(durationCounter);
                            ServiceEventSource.Current.ServiceMessage(
                                this.context,
                                $"Completed message saved message to Completed Messages Store - Duration [{duration.TotalMilliseconds}] mills - Traceid[{traceId}]");
                            await tx.CommitAsync();

                            retryCounter = 0;
                        }
                    }
                    catch (TimeoutException tex)
                    {
                        if (global::Iot.Common.Names.TransactionsRetryCount > retryCounter)
                        {
                            ServiceEventSource.Current.ServiceMessage(
                                this.context,
                                $"Data Service Timeout Exception when saving [{transactionType}] data from device {deviceId} - Iteration #{retryCounter} - Message-[{tex}] - Traceid[{traceId}]");

                            await Task.Delay(global::Iot.Common.Names.TransactionRetryWaitIntervalInMills *(int)Math.Pow(2, retryCounter));

                            retryCounter++;
                        }
                        else
                        {
                            ServiceEventSource.Current.ServiceMessage(
                                this.context,
                                $"Data Service Timeout Exception when saving [{transactionType}] data from device {deviceId} - Iteration #{retryCounter} - Transaction Aborted - Message-[{tex}] - Traceid[{traceId}]");

                            resultRet    = this.BadRequest();
                            retryCounter = 0;
                        }
                    }
                }

                duration = DateTime.UtcNow.Subtract(durationCounter);
                ServiceEventSource.Current.ServiceMessage(
                    this.context,
                    $"Data Service - Saved Message to Complete Message Store with timestamp [{completedMessage.Timestamp.ToString()}] indexed by timestamp[{messageTimestamp}] from device {deviceId} - Duration [{duration.TotalMilliseconds}] mills - Traceid[{traceId}]");
            }

            duration = DateTime.UtcNow.Subtract(durationCounter);
            ServiceEventSource.Current.ServiceMessage(
                this.context,
                $"Data Service Received event from device {deviceId} - Message completed Duration [{duration.TotalMilliseconds}] mills - Traceid[{traceId}]");

            return(resultRet);
        }
Exemple #22
0
 /// <summary>
 /// Método responsável pela captura das imagens
 /// O nome de imagem capturada é adiciona numa lista que, posteriormente, é percorrida para enviar as imagens para a pasta destino
 /// </summary>
 private void CreatePrintScreen()
 {
     Seq += 1;
     // Sequência
     ScreenCapturing.LegendData.FrameID = Seq;
     // Captura a tela
     Image[] imgList;
     if (sysConfig.Data.OneImagePerScreen)
     {
         imgList = ScreenCapturing.GetDesktopWindowCaptureAsBitmaps();
     }
     else
     {
         // Captura uma única imagem para todos monitores
         imgList    = new Image[1];
         imgList[0] = ScreenCapturing.GetDesktopWindowCaptureAsBitmap();
     }
     for (int i = 0; i < imgList.Length; i++)
     {
         Image tempImg = imgList[i];
         try
         {
             // Nome do arquivo
             string fileToSave = Environment.UserName + DATA_DELIMITER +
                                 MACAddress + DATA_DELIMITER +
                                 Util.GetLocalIpAddress() + DATA_DELIMITER +
                                 string.Format("{0:yyyyMMdd_HH_mm_ss_fff}", internalClock) + DATA_DELIMITER +
                                 i.ToString() +
                                 sysConfig.Data.ExtImgFormat;
             // No do arquivo temporário (com path)
             string tempFullFileToSave = sysConfig.TempPath + fileToSave;
             // Adiciona assinatura com dados de segurança na imagem quando a imagem for PNG
             if ((sysConfig.Data.ImgFormat == ImageFormat.Png) && (sysConfig.Data.UseSignature))
             {
                 // Pabavra chave de segurança e identificação que é embutida na imagem
                 string wordSecurity = UDYATSIGN + DATA_DELIMITER +
                                       sysConfig.Data.CustomerID.ToString() + DATA_DELIMITER +
                                       sysConfig.Data.CustomerMacNumber.ToString() + DATA_DELIMITER +
                                       FnvHash.GetHash(sysConfig.Data.CustomerWord, 120).ToHexString() + DATA_DELIMITER +
                                       sysConfig.MachineUniqueID + DATA_DELIMITER +
                                       fileToSave;
                 // Esconde o texto na imagem (esteganografia)
                 SteganographyHelper.embedText(wordSecurity, (Bitmap)tempImg);
             }
             // Salva o arquivo no Temp
             if (sysConfig.Data.ImgFormat == ImageFormat.Jpeg)
             {
                 tempImg.Save(tempFullFileToSave, sysConfig.Data.ImageCodec, sysConfig.Data.EncoderParams);
             }
             else
             {
                 tempImg.Save(tempFullFileToSave, sysConfig.Data.ImgFormat);
             }
             // Adiciona o arquivo na lista de arquivos que devem ser movidos
             if (sysConfig.Data.MoveImagesToTarget)
             {
                 lock (filesToSend.SyncRoot)
                 {
                     filesToSend.Add(fileToSave);
                 }
             }
         }
         catch (Exception ex)
         {
             Log.AddLog("Captura", ex.Message);
         }
     }
 }
        public async Task <IActionResult> GetDevicesHistoryByInterval(int startHours, int endHours, string deviceId = null, int limit = Int32.MaxValue, int minMagnitudeAllowed = 1)
        {
            // Manage session and Context
            HttpServiceUriBuilder contextUri = new HttpServiceUriBuilder().SetServiceName(this.context.ServiceName);
            string reportsSecretKey          = HTTPHelper.GetQueryParameterValueFor(HttpContext, Names.REPORTS_SECRET_KEY_NAME);
            List <DeviceHistoricalReportModel> deviceHistoricalReportModelList = new List <DeviceHistoricalReportModel>();

            long searchIntervalStart = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - DateTimeOffset.UtcNow.AddHours(startHours * (-1)).ToUnixTimeMilliseconds();
            long searchIntervalEnd   = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - DateTimeOffset.UtcNow.AddHours(endHours * (-1)).ToUnixTimeMilliseconds();

            ServiceUriBuilder uriBuilder = new ServiceUriBuilder(Names.InsightDataServiceName);
            Uri serviceUri = uriBuilder.Build();

            // service may be partitioned.
            // this will aggregate device IDs from all partitions
            ServicePartitionList partitions = await fabricClient.QueryManager.GetPartitionListAsync(serviceUri);

            foreach (Partition partition in partitions)
            {
                String pathAndQuery = $"/api/devices/history/interval/{searchIntervalStart}/{searchIntervalEnd}/limit/{limit}";

                if (deviceId != null && deviceId.Length > 0)
                {
                    pathAndQuery = $"/api/devices/history/{deviceId}/interval/{searchIntervalStart}/{searchIntervalEnd}/limit/{limit}";
                }

                Uri getUrl = new HttpServiceUriBuilder()
                             .SetServiceName(serviceUri)
                             .SetPartitionKey(((Int64RangePartitionInformation)partition.PartitionInformation).LowKey)
                             .SetServicePathAndQuery(pathAndQuery)
                             .Build();

                HttpResponseMessage response = await httpClient.GetAsync(getUrl, appLifetime.ApplicationStopping);

                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    JsonSerializer serializer = new JsonSerializer();
                    using (StreamReader streamReader = new StreamReader(await response.Content.ReadAsStreamAsync()))
                    {
                        using (JsonTextReader jsonReader = new JsonTextReader(streamReader))
                        {
                            List <DeviceViewModelList> deviceViewModelListResult = serializer.Deserialize <List <DeviceViewModelList> >(jsonReader);

                            string                uniqueId      = FnvHash.GetUniqueId();
                            List <string>         deviceList    = new List <string>();
                            List <DateTimeOffset> timestampList = new List <DateTimeOffset>();
                            foreach (DeviceViewModelList deviceViewModelList in deviceViewModelListResult)
                            {
                                int deviceIdIndex = 0;

                                if (deviceList.Contains(deviceViewModelList.DeviceId))
                                {
                                    deviceIdIndex = deviceList.IndexOf(deviceViewModelList.DeviceId);
                                }
                                else
                                {
                                    deviceList.Add(deviceViewModelList.DeviceId);
                                    deviceIdIndex = deviceList.IndexOf(deviceViewModelList.DeviceId);
                                }

                                int timesampIndex = 0;

                                if (timestampList.Contains(deviceViewModelList.Events.ElementAt(0).Timestamp))
                                {
                                    timesampIndex = timestampList.IndexOf(deviceViewModelList.Events.ElementAt(0).Timestamp);
                                }
                                else
                                {
                                    timestampList.Add(deviceViewModelList.Events.ElementAt(0).Timestamp);
                                    timesampIndex = timestampList.IndexOf(deviceViewModelList.Events.ElementAt(0).Timestamp);
                                }

                                int batteryVoltage    = deviceViewModelList.Events.ElementAt(0).BatteryLevel / 1000;
                                int batteryPercentage = 0;

                                if (deviceViewModelList.Events.ElementAt(0).BatteryLevel < 2800)
                                {
                                    batteryPercentage = 0;
                                }
                                else if (deviceViewModelList.Events.ElementAt(0).BatteryLevel > 3600)
                                {
                                    batteryPercentage = 100;
                                }
                                else
                                {
                                    batteryPercentage = (deviceViewModelList.Events.ElementAt(0).BatteryLevel - 2800) / 10;
                                }

                                int  minAllowedFrequency = 0;
                                bool needReferencEntry   = true;
                                foreach (DeviceViewModel evnt in deviceViewModelList.Events)
                                {
                                    for (int index = 0; index < evnt.DataPointsCount; index++)
                                    {
                                        if (evnt.Magnitude[index] >= minMagnitudeAllowed)
                                        {
                                            needReferencEntry = false;
                                            DeviceHistoricalReportModel message = new DeviceHistoricalReportModel(
                                                uniqueId,
                                                evnt.Timestamp,
                                                timesampIndex,
                                                evnt.DeviceId,
                                                deviceIdIndex,
                                                evnt.BatteryLevel,
                                                batteryVoltage,
                                                batteryPercentage,
                                                evnt.TempExternal,
                                                evnt.TempInternal,
                                                evnt.DataPointsCount,
                                                evnt.MeasurementType,
                                                evnt.SensorIndex,
                                                evnt.Frequency[index],
                                                evnt.Magnitude[index]);
                                            deviceHistoricalReportModelList.Add(message);

                                            if (minAllowedFrequency == 0)
                                            {
                                                minAllowedFrequency = evnt.Frequency[index];
                                            }
                                        }
                                    }
                                }

                                if (needReferencEntry)
                                {
                                    DeviceHistoricalReportModel message = new DeviceHistoricalReportModel(
                                        uniqueId,
                                        deviceViewModelList.Events.ElementAt(0).Timestamp,
                                        timesampIndex,
                                        deviceViewModelList.Events.ElementAt(0).DeviceId,
                                        deviceIdIndex,
                                        deviceViewModelList.Events.ElementAt(0).BatteryLevel,
                                        batteryVoltage,
                                        batteryPercentage,
                                        deviceViewModelList.Events.ElementAt(0).TempExternal,
                                        deviceViewModelList.Events.ElementAt(0).TempInternal,
                                        deviceViewModelList.Events.ElementAt(0).DataPointsCount,
                                        deviceViewModelList.Events.ElementAt(0).MeasurementType,
                                        deviceViewModelList.Events.ElementAt(0).SensorIndex,
                                        minAllowedFrequency,
                                        minMagnitudeAllowed);
                                    deviceHistoricalReportModelList.Add(message);
                                }
                            }
                        }
                    }
                }
            }

            return(this.Ok(deviceHistoricalReportModelList));
        }