/// <summary>
        /// Initializes an instance of this class and creates all <see cref="Led"/> resources in the non-persistent <see cref="ILedStorage"/>.
        /// </summary>
        /// <param name="ledStorage">reference to the <see cref="ILedStorage"/>  where all groups are stored</param>
        /// <param name="controllerStorage">reference to the <see cref="IGroupStorage"/> where all controllers are stored</param>
        public LedHandler(ILedStorage ledStorage, IControllerStorage controllerStorage)
        {
            _ledStorage = ledStorage;

            foreach (IControllerDataSet cds in controllerStorage.GetAllControllers())
            {
                for (int i = 0; i < cds.LedCount; i++)
                {
                    _ledStorage.AddLed(cds.Id, i);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Returns an instance of <see cref="Controllers"/> which contains all <see cref="Controller"/>s matching the passed query. The following query parameters can be applied:
        /// <list type="number">
        ///     <item>query parameter <code>id</code>: returns only the <see cref="Controller"/> having the passed <see cref="Controller.Id"/></item>
        ///     <item>query parameter <code>name</code>: returns all <see cref="Controller"/> whose <see cref="Controller.Name"/> contains the passed value (case invariant).</item>
        ///     <item>query parameter <code>device_name</code>: returns all <see cref="Controller"/> whose device name contains the passed value (case invariant). Note that the device name is only visible if the respective <see cref="Controller"/> is online.</item>
        ///     <item>query parameter <code>firmware_id</code>: returns all <see cref="Controller"/> whose firmware has the passed firmware ID. Note that the firmware ID is only visible if the respective <see cref="Controller"/> is online.</item>
        ///     <item>query parameter <code>network_state</code>: returns all <see cref="Controller"/> whose <see cref="Controller.NetworkState"/> has the passed value (case invariant).</item>
        /// </list>
        /// If multiple query parameters are applied, the respective <see cref="Controller"/> must match all query criterion to be in the list of <see cref="Controllers"/>.
        /// If the query is 'null' or empty (i.e. query.Count == 0), all <see cref="Controller"/>s handled by this class are returned.
        /// Unknown query parameters will be ignored.
        /// </summary>
        /// <param name="query">query (can be null or empty)</param>
        /// <returns>instance of <see cref="Controllers"/> containg all <see cref="Controller"/>s matching the passed query</returns>
        public Controllers GetControllers(IDictionary <string, string> query)
        {
            Controllers controllers = new Controllers();

            //return all controllers, if no query parameters are specified
            if (query == null || query.Count == 0)
            {
                foreach (IControllerDataSet cds in _controllerStorage.GetAllControllers())
                {
                    //create controller entity object
                    Controller controller = new Controller();
                    controller.Id       = cds.Id;
                    controller.Name     = cds.FriendlyName;
                    controller.LedCount = cds.LedCount;
                    controller.State    = CalculateState(cds.Timestamp);

                    controllers.ControllerList.Add(controller);
                }
            }
            //filter controllers based on the given query parameters
            else
            {
                foreach (IControllerDataSet cds in _controllerStorage.GetAllControllers())
                {
                    if (query.ContainsKey("id"))
                    {
                        if (cds.Id != ApiBase.ParseId(query["id"]))
                        {
                            continue;
                        }
                    }
                    if (query.ContainsKey("name"))
                    {
                        if (String.IsNullOrWhiteSpace(cds.FriendlyName) || !cds.FriendlyName.ToLower().Contains(query["name"].ToLower()))
                        {
                            continue;
                        }
                    }
                    if (query.ContainsKey("device_name"))
                    {
                        if (String.IsNullOrWhiteSpace(cds.DeviceName) || !cds.DeviceName.ToLower().Contains(query["device_name"].ToLower()))
                        {
                            continue;
                        }
                    }
                    if (query.ContainsKey("firmware_id"))
                    {
                        if (String.IsNullOrWhiteSpace(cds.FirmwareId) || cds.FirmwareId.CompareTo(query["firmware_id"]) != 0)
                        {
                            continue;
                        }
                    }
                    if (query.ContainsKey("uuid"))
                    {
                        if (String.IsNullOrWhiteSpace(cds.UuId) || cds.UuId.CompareTo(query["uuid"]) != 0)
                        {
                            continue;
                        }
                    }
                    if (query.ContainsKey("network_state"))
                    {
                        NetworkState networkState = CalculateState(cds.Timestamp);
                        if (networkState != NetworkState.connection_lost && query["network_state"].ToLower().CompareTo("connection_lost") == 0)
                        {
                            continue;
                        }
                        else if (networkState != NetworkState.online && query["network_state"].ToLower().CompareTo("online") == 0)
                        {
                            continue;
                        }
                        else if (networkState != NetworkState.offline && query["network_state"].ToLower().CompareTo("offline") == 0)
                        {
                            continue;
                        }
                        else if (query["network_state"].ToLower().CompareTo("offline") != 0 ||
                                 query["network_state"].ToLower().CompareTo("connection_lost") != 0 ||
                                 query["network_state"].ToLower().CompareTo("online") != 0)
                        {
                            continue;
                        }
                    }

                    Controller controller = new Controller();
                    controller.Id       = cds.Id;
                    controller.Name     = cds.FriendlyName;
                    controller.LedCount = cds.LedCount;
                    controller.State    = CalculateState(cds.Timestamp);

                    controllers.ControllerList.Add(controller);
                }
            }
            return(controllers);
        }