コード例 #1
0
        /// <summary>
        /// This program is an example of a very simple ItemSense Coordinator
        /// Connector. After starting an ItemSense job via a POST request,
        /// the system periodically performs a GET request of the Items API
        /// endpoint and presents to the useronly item data that has been
        /// updated since the start of the job.
        /// </summary>
        /// <param name="args">
        /// Command line parameters ignored.
        /// </param>
        static void Main(string[] args)
        {
            int      itemUpdateIntervalInSeconds = 20;
            int      jobDurationInSeconds        = 60;
            DateTime jobCreationDateTime         = DateTime.Now;
            Dictionary <string, ItemSense.Item> itemsFromCurrentJob = new Dictionary <string, ItemSense.Item>();

            try
            {
                // Create and configure a ReaderDefinition object
                ItemSense.Job newJobDefinition =
                    new ItemSense.Job()
                {
                    RecipeName                  = "IMPINJ_BasicLocation",
                    DurationSeconds             = jobDurationInSeconds,
                    StartDelay                  = 0,
                    ReportToDatabaseEnabled     = true,
                    ReportToMessageQueueEnabled = true,
                    Facility = ""
                };

                // Create a string-based JSON object of the object
                string objectAsJson = JsonConvert.SerializeObject(newJobDefinition);
                // Now translate the JSON into bytes
                byte[] objectAsBytes = Encoding.UTF8.GetBytes(objectAsJson);

                // Create the full path to the configure zoneTransitions
                // Message Queu endpoint from default ItemSense URI
                string ReaderDefinitionPostApiEndpoint =
                    SolutionConstants.ItemSenseUri +
                    "/control/v1/jobs/start";

                // Create a WebRequest, identifying it as a POST request
                // with a JSON payload, and assign it the specified
                // credentials.
                WebRequest ItemSensePostRequest =
                    WebRequest.Create(ReaderDefinitionPostApiEndpoint);
                ItemSensePostRequest.Credentials =
                    new System.Net.NetworkCredential(
                        SolutionConstants.ItemSenseUsername,
                        SolutionConstants.ItemSensePassword
                        );
                ItemSensePostRequest.Proxy         = null;
                ItemSensePostRequest.Method        = "POST";
                ItemSensePostRequest.ContentType   = "application/json";
                ItemSensePostRequest.ContentLength = objectAsBytes.Length;

                // Create an output data stream representation of the
                // POST WebRequest to output the data
                Stream dataStream = ItemSensePostRequest.GetRequestStream();
                dataStream.Write(objectAsBytes, 0, objectAsBytes.Length);
                dataStream.Close();

                // Execute the POST request and retain the response.
                using (HttpWebResponse ItemSenseResponse = (HttpWebResponse)ItemSensePostRequest.GetResponse())
                {
                    // The response StatusCode is a .NET Enum, so convert it to
                    // an ItemSense response code
                    ItemSense.ResponseCode ResponseCode =
                        (ItemSense.ResponseCode)ItemSenseResponse.StatusCode;

                    // In this instance, we are only interested in whether
                    // the ItemSense response to the POST request was a "Success".
                    if (ItemSense.ResponseCode.Success == ResponseCode)
                    {
                        // Open a stream to access the response data which
                        // contains the Job identifier
                        Stream ItemSenseDataStream = ItemSenseResponse.GetResponseStream();

                        // Only continue if an actual response data stream exists
                        if (null != ItemSenseDataStream)
                        {
                            // Create a StreamReader to access the resulting data
                            StreamReader objReader = new StreamReader(ItemSenseDataStream);

                            // Read the complete POST request results as a raw string
                            string itemSenseData = objReader.ReadToEnd();

                            // Now convert the raw JSON into an Items class
                            // representation
                            ItemSense.JobResponse PostResponseData =
                                JsonConvert.DeserializeObject <ItemSense.JobResponse>(
                                    itemSenseData
                                    );

                            string jobCreationTime = PostResponseData.CreationTime.Replace("Z[Etc/UTC]", string.Empty);

                            Console.WriteLine("Job started; ID: {0}",
                                              PostResponseData.Id
                                              );

                            jobCreationDateTime = DateTime.Parse(
                                jobCreationTime,
                                null,
                                System.Globalization.DateTimeStyles.RoundtripKind
                                );

                            Console.WriteLine("Job created at time: {0} [Etc/UTC]",
                                              jobCreationDateTime
                                              );

                            // Close the data stream. If we have got here,
                            // everything has gone well and there are no
                            // errors.
                            ItemSenseDataStream.Close();
                        }
                        else
                        {
                            Console.WriteLine("null ItemSense data stream.");
                        }
                    }
                    else
                    {
                        throw (new Exception(string.Format(
                                                 "ItemSense POST Response returned status of {0}",
                                                 ItemSenseResponse.StatusCode
                                                 )));
                    }
                }

                // Periodically poll the Items endpoint for data
                for (int i = 0; i < jobDurationInSeconds; i += itemUpdateIntervalInSeconds)
                {
                    Console.Write("Getting Items data update ... ");
                    // Request all of the Items data from the Items endpoint
                    List <ItemSense.Item> allItems = GetItems();

                    // Now iterate through the returned list and only add those
                    // items identified during this job to the final list
                    foreach (ItemSense.Item currentItem in allItems)
                    {
                        // Convert the current item time into a value that can be
                        // compared
                        DateTime itemTime = DateTime.Parse(
                            currentItem.LastModifiedTime,
                            null,
                            System.Globalization.DateTimeStyles.RoundtripKind
                            );

                        // Write the item data to AllItemData only if the time
                        // filter requiremente are met
                        if (itemTime >= jobCreationDateTime)
                        {
                            ItemSense.Item itemFromDictionary;

                            if (false == itemsFromCurrentJob.TryGetValue(currentItem.Epc, out itemFromDictionary))
                            {
                                itemsFromCurrentJob.Add(currentItem.Epc, currentItem);
                            }
                            else
                            {
                                itemsFromCurrentJob[currentItem.Epc] = currentItem;
                            }
                        }
                    }
                    Console.WriteLine("Done.");

                    Thread.Sleep(itemUpdateIntervalInSeconds * 1000);
                }

                // Print out the results
                Console.WriteLine("Items data:");
                foreach (KeyValuePair <string, ItemSense.Item> kvp in itemsFromCurrentJob)
                {
                    Console.WriteLine(kvp.Value.itemToCsvString());
                }
            }
            catch (WebException wex)
            {
                if (wex.Status == WebExceptionStatus.ProtocolError)
                {
                    StreamReader reader = new StreamReader(wex.Response.GetResponseStream());
                    Console.WriteLine("Detail: {0}", reader.ReadToEnd());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0} ({1}){2}",
                                  ex.Message,
                                  ex.GetType(),
                                  (null == ex.InnerException) ? string.Empty : Environment.NewLine + ex.InnerException.Message
                                  );
            }

            // Hang on here until user presses Enter
            Console.WriteLine(" Press <Enter> to exit.");
            Console.ReadLine();
        }
コード例 #2
0
        /// <summary>
        /// This program is an example of a very simple ItemSense Coordinator
        /// Connector. After starting an ItemSense job via a POST request,
        /// the system periodically performs a GET request of the Items API
        /// endpoint and presents to the useronly item data that has been
        /// updated since the start of the job.
        /// </summary>
        /// <param name="args">
        /// Command line parameters ignored.
        /// </param>
        static void Main(string[] args)
        {
            int itemUpdateIntervalInSeconds = 20;
            int jobDurationInSeconds = 60;
            DateTime jobCreationDateTime = DateTime.Now;
            Dictionary<string, ItemSense.Item> itemsFromCurrentJob = new Dictionary<string, ItemSense.Item>();

            try
            {
                // Create and configure a ReaderDefinition object
                ItemSense.Job newJobDefinition =
                    new ItemSense.Job()
                {
                    RecipeName = "IMPINJ_BasicLocation",
                    DurationSeconds = jobDurationInSeconds,
                    StartDelay = 0,
                    ReportToDatabaseEnabled = true,
                    ReportToMessageQueueEnabled = true,
                    Facility = ""
                };

                // Create a string-based JSON object of the object
                string objectAsJson = JsonConvert.SerializeObject(newJobDefinition);
                // Now translate the JSON into bytes
                byte[] objectAsBytes = Encoding.UTF8.GetBytes(objectAsJson);

                // Create the full path to the configure zoneTransitions
                // Message Queu endpoint from default ItemSense URI
                string ReaderDefinitionPostApiEndpoint =
                    SolutionConstants.ItemSenseUri +
                    "/control/v1/jobs/start";

                // Create a WebRequest, identifying it as a POST request
                // with a JSON payload, and assign it the specified
                // credentials.
                WebRequest ItemSensePostRequest =
                    WebRequest.Create(ReaderDefinitionPostApiEndpoint);
                ItemSensePostRequest.Credentials =
                    new System.Net.NetworkCredential(
                        SolutionConstants.ItemSenseUsername,
                        SolutionConstants.ItemSensePassword
                        );
                ItemSensePostRequest.Proxy = null;
                ItemSensePostRequest.Method = "POST";
                ItemSensePostRequest.ContentType = "application/json";
                ItemSensePostRequest.ContentLength = objectAsBytes.Length;

                // Create an output data stream representation of the
                // POST WebRequest to output the data
                Stream dataStream = ItemSensePostRequest.GetRequestStream();
                dataStream.Write(objectAsBytes, 0, objectAsBytes.Length);
                dataStream.Close();

                // Execute the POST request and retain the response.
                using (HttpWebResponse ItemSenseResponse = (HttpWebResponse)ItemSensePostRequest.GetResponse())
                {
                    // The response StatusCode is a .NET Enum, so convert it to
                    // an ItemSense response code
                    ItemSense.ResponseCode ResponseCode =
                        (ItemSense.ResponseCode)ItemSenseResponse.StatusCode;

                    // In this instance, we are only interested in whether
                    // the ItemSense response to the POST request was a "Success".
                    if (ItemSense.ResponseCode.Success == ResponseCode)
                    {
                        // Open a stream to access the response data which
                        // contains the Job identifier
                        Stream ItemSenseDataStream = ItemSenseResponse.GetResponseStream();

                        // Only continue if an actual response data stream exists
                        if (null != ItemSenseDataStream)
                        {
                            // Create a StreamReader to access the resulting data
                            StreamReader objReader = new StreamReader(ItemSenseDataStream);

                            // Read the complete POST request results as a raw string
                            string itemSenseData = objReader.ReadToEnd();

                            // Now convert the raw JSON into an Items class
                            // representation
                            ItemSense.JobResponse PostResponseData =
                                JsonConvert.DeserializeObject<ItemSense.JobResponse>(
                                itemSenseData
                                );

                            string jobCreationTime = PostResponseData.CreationTime.Replace("Z[Etc/UTC]", string.Empty);

                            Console.WriteLine("Job started; ID: {0}",
                                PostResponseData.Id
                                );

                            jobCreationDateTime = DateTime.Parse(
                                jobCreationTime,
                                null,
                                System.Globalization.DateTimeStyles.RoundtripKind
                                );

                            Console.WriteLine("Job created at time: {0} [Etc/UTC]",
                                jobCreationDateTime
                                );

                            // Close the data stream. If we have got here,
                            // everything has gone well and there are no
                            // errors.
                            ItemSenseDataStream.Close();
                        }
                        else
                        {
                            Console.WriteLine("null ItemSense data stream.");
                        }
                    }
                    else
                    {
                        throw (new Exception(string.Format(
                            "ItemSense POST Response returned status of {0}",
                            ItemSenseResponse.StatusCode
                            )));
                    }
                }

                // Periodically poll the Items endpoint for data
                for (int i = 0; i < jobDurationInSeconds; i += itemUpdateIntervalInSeconds)
                {
                    Console.Write("Getting Items data update ... ");
                    // Request all of the Items data from the Items endpoint
                    List<ItemSense.Item> allItems = GetItems();

                    // Now iterate through the returned list and only add those
                    // items identified during this job to the final list
                    foreach(ItemSense.Item currentItem in allItems)
                    {
                        // Convert the current item time into a value that can be
                        // compared
                        DateTime itemTime = DateTime.Parse(
                            currentItem.LastModifiedTime,
                            null,
                            System.Globalization.DateTimeStyles.RoundtripKind
                            );

                        // Write the item data to AllItemData only if the time
                        // filter requiremente are met
                        if (itemTime >= jobCreationDateTime)
                        {
                            ItemSense.Item itemFromDictionary;

                            if (false == itemsFromCurrentJob.TryGetValue(currentItem.Epc, out itemFromDictionary))
                            {
                                itemsFromCurrentJob.Add(currentItem.Epc, currentItem);
                            }
                            else
                            {
                                itemsFromCurrentJob[currentItem.Epc] = currentItem;
                            }
                        }
                    }
                    Console.WriteLine("Done.");

                    Thread.Sleep(itemUpdateIntervalInSeconds * 1000);
                }

                // Print out the results
                Console.WriteLine("Items data:");
                foreach (KeyValuePair<string, ItemSense.Item> kvp in itemsFromCurrentJob)
                {
                    Console.WriteLine(kvp.Value.itemToCsvString());
                }
            }
            catch (WebException wex)
            {
                if (wex.Status == WebExceptionStatus.ProtocolError)
                {
                    StreamReader reader = new StreamReader(wex.Response.GetResponseStream());
                    Console.WriteLine("Detail: {0}", reader.ReadToEnd());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0} ({1}){2}",
                    ex.Message,
                    ex.GetType(),
                    (null == ex.InnerException) ? string.Empty : Environment.NewLine + ex.InnerException.Message
                    );
            }

            // Hang on here until user presses Enter
            Console.WriteLine(" Press <Enter> to exit.");
            Console.ReadLine();
        }