Beispiel #1
0
        public static async Task Run(GeotabDataOnlyPlanAPI api)
        {
            ConsoleUtility.LogExampleStarted(typeof(GetControllersAsyncExample).Name);

            IList <Controller> controllers;
            Controller         controller;

            try
            {
                // Example: Get all controllers:
                controllers = await api.GetControllersAsync();

                // Example: Search for a controller based on id:
                string controllerId = KnownId.ControllerObdPowertrainId.ToString();
                controllers = await api.GetControllersAsync(controllerId);

                controller = controllers.FirstOrDefault();

                // Example: Search for controllers based on name:
                string controllerName = "Body (B)";
                controllers = await api.GetControllersAsync("", controllerName);

                // Example: Search for controllers based on sourceId:
                string controllerSourceId = KnownId.SourceObdId.ToString();
                controllers = await api.GetControllersAsync("", "", controllerSourceId);
            }
            catch (Exception ex)
            {
                ConsoleUtility.LogError(ex);
            }

            ConsoleUtility.LogExampleFinished(typeof(GetControllersAsyncExample).Name);
        }
        /// <summary>
        /// Returns a fully-populated <see cref="Controller"/> from the cache if it exists, otherwise gets it from the server and adds it to the cache before returning it.
        /// </summary>
        /// <param name="controller">The <see cref="Controller"/> to populate.</param>
        /// <returns>A fully-populated <see cref="Controller"/>.</returns>
        async Task <Controller> GetControllerAsync(Controller controller)
        {
            if (controller == null || controller is NoController)
            {
                return(NoController.Value);
            }
            Id id = controller.Id;

            if (controllerCache.TryGetValue(id, out controller))
            {
                return(controller);
            }
            IList <Controller> returnedControllers = await api.GetControllersAsync(id.ToString());

            if (!returnedControllers.Any())
            {
                return(null);
            }
            controller = returnedControllers.First();
            controllerCache.Add(id, controller);
            return(controller);
        }