Esempio n. 1
0
        private async Task ReadRefPersonStream()
        {
            Dictionary <string, Models.RefPerson> refPersons = new Dictionary <string, Models.RefPerson>();

            refPersons.Clear();
            dataGridDetectedPersons.ItemsSource = refPersons.Values;
            dataGridDetectedPersons.Items.Refresh();

            string streamArn = Models.MyAWSConfigs.DynamodbRefPersonTableStreamArn;

            //int maxItemCount = 100;

            try
            {
                AmazonDynamoDBStreamsClient streamsClient;
                using (streamsClient = new AmazonDynamoDBStreamsClient(Models.MyAWSConfigs.DynamodbRegion))
                {
                    String lastEvaluatedShardId = null;

                    do
                    {
                        DescribeStreamRequest describeStreamRequest = new DescribeStreamRequest()
                        {
                            StreamArn             = streamArn,
                            ExclusiveStartShardId = lastEvaluatedShardId,
                        };

                        DescribeStreamResponse describeStreamResponse = await streamsClient.DescribeStreamAsync(describeStreamRequest);

                        List <Shard> shards = describeStreamResponse.StreamDescription.Shards;

                        // Process each shard on this page

                        foreach (Shard shard in shards)
                        {
                            String shardId = shard.ShardId;

                            // Get an iterator for the current shard

                            GetShardIteratorRequest getShardIteratorRequest = new GetShardIteratorRequest()
                            {
                                StreamArn         = streamArn,
                                ShardId           = shardId,
                                ShardIteratorType = ShardIteratorType.LATEST,
                            };

                            GetShardIteratorResponse getShardIteratorResponse =
                                await streamsClient.GetShardIteratorAsync(getShardIteratorRequest);

                            String currentShardIter = getShardIteratorResponse.ShardIterator;

                            int processedRecordCount = 0;
                            //&& processedRecordCount < maxItemCount
                            while (currentShardIter != null)
                            {
                                // Use the shard iterator to read the stream records

                                GetRecordsRequest getRecordsRequest = new GetRecordsRequest()
                                {
                                    ShardIterator = currentShardIter
                                };

                                GetRecordsResponse getRecordsResponse = await streamsClient.GetRecordsAsync(getRecordsRequest);

                                List <Record> records = getRecordsResponse.Records;

                                foreach (Record record in records)
                                {
                                    foreach (KeyValuePair <string, AttributeValue> newImage in record.Dynamodb.NewImage)
                                    {
                                        string changedRefPersonId          = record.Dynamodb.NewImage["id"].S;
                                        string changedRefPersonStatus      = record.Dynamodb.NewImage["status"].N.ToString();
                                        string changedRefPersonName        = record.Dynamodb.NewImage["name"].S;
                                        string changedRefPersonDescription = record.Dynamodb.NewImage["description"].S;
                                        string changedRefPersonCamera      = record.Dynamodb.NewImage["camera"].S;

                                        Models.RefPerson refPerson = new Models.RefPerson();

                                        refPerson.id           = changedRefPersonId;
                                        refPerson.name         = changedRefPersonName;
                                        refPerson.status       = (changedRefPersonStatus == "1") ? true : false;
                                        refPerson.description  = changedRefPersonDescription;
                                        refPerson.camera       = changedRefPersonCamera;
                                        refPerson.lastLocation = cameras[changedRefPersonCamera].location;

                                        string directoryPath = "Resources/Images/";

                                        if (!File.Exists(directoryPath + refPerson.id))
                                        {
                                            Models.S3Bucket.DownloadFile(refPerson.id, Models.MyAWSConfigs.RefImagesBucketName);
                                        }

                                        string exeDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\";

                                        Uri fileUri = new Uri(exeDirectory + directoryPath + refPerson.id);

                                        refPerson.image = new BitmapImage(fileUri);

                                        if (refPerson.status)
                                        {
                                            if (!refPersons.ContainsKey(refPerson.id))
                                            {
                                                refPersons.Add(refPerson.id, refPerson);
                                            }
                                        }
                                        else
                                        {
                                            if (refPersons.ContainsKey(refPerson.id))
                                            {
                                                refPersons.Remove(refPerson.id);
                                            }
                                        }

                                        dataGridDetectedPersons.ItemsSource = refPersons.Values;
                                        dataGridDetectedPersons.Items.Refresh();
                                    }
                                }
                                processedRecordCount += records.Count;
                                currentShardIter      = getRecordsResponse.NextShardIterator;
                            }
                        }

                        // If LastEvaluatedShardId is set, then there is
                        // at least one more page of shard IDs to retrieve
                        lastEvaluatedShardId = describeStreamResponse.StreamDescription.LastEvaluatedShardId;
                    } while (lastEvaluatedShardId != null);
                }
            }
            catch (AmazonDynamoDBException e)
            {
                Console.WriteLine("AmazonDynamoDBException: " + e);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e);
            }
        }
Esempio n. 2
0
        private async Task ReadStream()
        {
            List <Models.RefPerson> refPersons = new List <Models.RefPerson>();

            dataGridDetectedPersons.ItemsSource = refPersons;
            dataGridDetectedPersons.Items.Refresh();

            string streamArn    = "arn:aws:dynamodb:ap-southeast-2:358403828169:table/ref_persons/stream/2019-11-18T05:31:40.045";
            int    maxItemCount = 100;

            try
            {
                AmazonDynamoDBStreamsClient streamsClient;
                using (streamsClient = new AmazonDynamoDBStreamsClient(Models.MyAWSConfigs.dynamodbRegion))
                {
                    String lastEvaluatedShardId = null;

                    do
                    {
                        DescribeStreamRequest describeStreamRequest = new DescribeStreamRequest()
                        {
                            StreamArn             = streamArn,
                            ExclusiveStartShardId = lastEvaluatedShardId,
                        };

                        DescribeStreamResponse describeStreamResponse = await streamsClient.DescribeStreamAsync(describeStreamRequest);

                        List <Shard> shards = describeStreamResponse.StreamDescription.Shards;

                        // Process each shard on this page

                        foreach (Shard shard in shards)
                        {
                            String shardId = shard.ShardId;

                            // Get an iterator for the current shard

                            GetShardIteratorRequest getShardIteratorRequest = new GetShardIteratorRequest()
                            {
                                StreamArn         = streamArn,
                                ShardId           = shardId,
                                ShardIteratorType = ShardIteratorType.LATEST,
                            };

                            GetShardIteratorResponse getShardIteratorResponse =
                                await streamsClient.GetShardIteratorAsync(getShardIteratorRequest);

                            String currentShardIter = getShardIteratorResponse.ShardIterator;

                            int processedRecordCount = 0;
                            while (currentShardIter != null && processedRecordCount < maxItemCount)
                            {
                                // Use the shard iterator to read the stream records

                                GetRecordsRequest getRecordsRequest = new GetRecordsRequest()
                                {
                                    ShardIterator = currentShardIter
                                };

                                GetRecordsResponse getRecordsResponse = await streamsClient.GetRecordsAsync(getRecordsRequest);

                                List <Record> records = getRecordsResponse.Records;

                                foreach (Record record in records)
                                {
                                    foreach (KeyValuePair <string, AttributeValue> newImage in record.Dynamodb.NewImage)
                                    {
                                        string changedRefPersonId          = record.Dynamodb.NewImage["id"].S;
                                        string changedRefPersonStatus      = record.Dynamodb.NewImage["status"].N.ToString();
                                        string changedRefPersonName        = record.Dynamodb.NewImage["name"].S;
                                        string changedRefPersonDescription = record.Dynamodb.NewImage["description"].S;

                                        //Console.WriteLine($"{changedRefPersonId}:{changedRefPersonStatus}:{changedRefPersonName}:{changedRefPersonDescription}");

                                        Models.RefPerson refPerson = new Models.RefPerson();

                                        refPerson.id          = changedRefPersonId;
                                        refPerson.name        = changedRefPersonName;
                                        refPerson.status      = (changedRefPersonStatus == "1")?true:false;
                                        refPerson.description = changedRefPersonDescription;
                                        refPerson.camera      = "mobile_stream_1";

                                        string directoryPath = "Resources/Images/";

                                        if (!File.Exists(directoryPath + refPerson.id))
                                        {
                                            Models.S3Bucket.DownloadFile(refPerson.id);
                                        }

                                        string exeDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\";

                                        Uri fileUri = new Uri(exeDirectory + directoryPath + refPerson.id);

                                        refPerson.image = new BitmapImage(fileUri);

                                        if (refPerson.status)
                                        {
                                            if (refPersons.FindAll(p => p.id == refPerson.id).Count == 0)
                                            {
                                                refPersons.Add(refPerson);
                                            }
                                        }
                                        else
                                        {
                                            refPersons.RemoveAll(p => p.id == refPerson.id);
                                        }

                                        dataGridDetectedPersons.ItemsSource = refPersons;
                                        dataGridDetectedPersons.Items.Refresh();
                                    }
                                }
                                processedRecordCount += records.Count;
                                currentShardIter      = getRecordsResponse.NextShardIterator;
                            }
                        }

                        // If LastEvaluatedShardId is set, then there is
                        // at least one more page of shard IDs to retrieve
                        lastEvaluatedShardId = describeStreamResponse.StreamDescription.LastEvaluatedShardId;
                    } while (lastEvaluatedShardId != null);
                }
            }
            catch (AmazonDynamoDBException e)
            {
                Console.WriteLine("AmazonDynamoDBException: " + e);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e);
            }
        }
Esempio n. 3
0
 private void DeleteButton_Click(object sender, RoutedEventArgs e)
 {
     Models.RefPerson p = (Models.RefPerson)dataGridAllRefPersons.SelectedItem;
     mv.DeleteRefPerson(p.id, this);
 }