Exemplo n.º 1
0
        public static bool Update(UserConfiguration userConfig, List <DeviceInfo> deviceInfos)
        {
            var json = Data.DeviceInfo.ListToJson(deviceInfos);

            if (!string.IsNullOrEmpty(json))
            {
                Uri apiHost = ApiConfiguration.DataApiHost;

                string url = new Uri(apiHost, "data/update/index.php").ToString();

                var postDatas = new NameValueCollection();
                postDatas["token"]     = userConfig.SessionToken;
                postDatas["sender_id"] = UserManagement.SenderId.Get();
                postDatas["devices"]   = json;

                string response = HTTP.POST(url, postDatas);
                if (response != null)
                {
                    return(ApiError.ProcessResponse(response, "Update Device Data"));
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Remove Specified Devices from User
        /// </summary>
        /// <param name="userConfig">UserConfiguration object for the current user</param>
        /// <param name="deviceUniqueId">The Unique ID of the device to return</param>
        /// <returns></returns>
        public static bool Remove(UserConfiguration userConfig, string[] deviceUniqueIds)
        {
            if (userConfig != null && deviceUniqueIds != null && deviceUniqueIds.Length > 0)
            {
                Uri apiHost = ApiConfiguration.AuthenticationApiHost;

                string url = new Uri(apiHost, "devices/remove/index.php").ToString();

                var postDatas = new NameValueCollection();
                postDatas["token"]     = userConfig.SessionToken;
                postDatas["sender_id"] = UserManagement.SenderId.Get();

                var devices = RemoveDeviceInfo.ArrayToJSON(deviceUniqueIds);
                postDatas["devices"] = devices;

                string response = HTTP.POST(url, postDatas);
                if (response != null)
                {
                    return(ApiError.ProcessResponse(response, "Remove Devices"));
                }
            }

            return(false);
        }
Exemplo n.º 3
0
        public static bool Send(UserConfiguration userConfig, List <MessageInfo> messageInfos)
        {
            string json = JSON.FromList <MessageInfo>(messageInfos);

            if (!string.IsNullOrEmpty(json))
            {
                Uri apiHost = ApiConfiguration.AuthenticationApiHost;

                string url = new Uri(apiHost, "messages/send/index.php").ToString();

                var postDatas = new NameValueCollection();
                postDatas["token"]     = userConfig.SessionToken;
                postDatas["sender_id"] = UserManagement.SenderId.Get();
                postDatas["messages"]  = json;

                string response = HTTP.POST(url, postDatas);
                if (response != null)
                {
                    return(ApiError.ProcessResponse(response, "Send Messages"));
                }
            }

            return(false);
        }
Exemplo n.º 4
0
        public static Image DownloadImagev1(UserConfiguration userConfig, string fileId, bool useCache)
        {
            Image result = null;

            if (useCache)
            {
                if (cachedImages == null)
                {
                    LoadCachedImages();
                }

                var cachedImage = cachedImages.Find(o => o.Id == fileId);
                if (cachedImage != null)
                {
                    result = cachedImage.Image;
                }
            }

            if (result == null && userConfig != null)
            {
                Uri apiHost = ApiConfiguration.AuthenticationApiHost;

                string url = new Uri(apiHost, "files/download/index.php").ToString();

                string format = "{0}?token={1}&sender_id={2}&file_id={3}";

                string token    = userConfig.SessionToken;
                string senderId = UserManagement.SenderId.Get();

                url = string.Format(format, url, token, senderId, fileId);

                var response = HTTP.GET(url, true);
                if (response != null && response.Body != null && response.Headers != null)
                {
                    bool success = false;

                    string dummy = System.Text.Encoding.ASCII.GetString(response.Body);

                    // Takes forever to process an image
                    if (response.Body.Length < 500)
                    {
                        success = ApiError.ProcessResponse(response, "Download File");
                    }
                    else
                    {
                        success = true;
                    }

                    if (success)
                    {
                        try
                        {
                            using (var memStream = new MemoryStream())
                            {
                                memStream.Write(response.Body, 0, response.Body.Length);
                                result = Image.FromStream(memStream);
                                Logger.Log("Download File Successful", LogLineType.Notification);
                            }
                        }
                        catch (Exception ex) { Logger.Log("Response Not an Image : Exception : " + ex.Message); }
                    }

                    // Add Image to Cache
                    if (useCache && result != null)
                    {
                        AddImageToCache(new CachedImage(fileId, result));
                    }
                }
            }

            return(result);
        }
Exemplo n.º 5
0
        public static List <DeviceInfo> Get(UserConfiguration userConfig, List <string> uniqueIds, DateTime from, DateTime to, int timeout, string command)
        {
            Uri apiHost = ApiConfiguration.DataApiHost;

            string url = new Uri(apiHost, "data/get/index.php").ToString();

            string times = "&from=" + from.ToString("o") + "&to=" + to.ToString("o");

            string devices = "";

            // List Devices to Get data for
            // If no devices are listed then ALL devices are retrieved
            if (uniqueIds != null && uniqueIds.Count > 0)
            {
                var deviceItems = new List <DeviceListItem>();

                foreach (var uniqueId in uniqueIds)
                {
                    deviceItems.Add(new DeviceListItem(uniqueId));
                }

                string json = JSON.FromList <DeviceListItem>(deviceItems);
                if (!string.IsNullOrEmpty(json))
                {
                    devices = "&devices=" + json;
                }
            }

            string cmd = "";

            if (command != null)
            {
                cmd = "&command=" + command;
            }

            // Create GET request parameters
            if (userConfig != null)
            {
                string format = "{0}?token={1}&sender_id={2}{3}{4}{5}";

                string token    = userConfig.SessionToken;
                string senderId = UserManagement.SenderId.Get();

                url = string.Format(format, url, token, senderId, devices, cmd, times);
            }
            else
            {
                string format = "{0}?{1}{2}{3}";
                url = string.Format(format, url, devices, cmd, times);
            }

            // Setup HTTP Information for GET request
            var httpInfo = new HTTP.HTTPInfo();

            httpInfo.Url         = url;
            httpInfo.Timeout     = timeout;
            httpInfo.MaxAttempts = 1;


            string response = HTTP.GET(httpInfo);

            if (response != null)
            {
                bool success = ApiError.ProcessResponse(response, "Get Device Data");
                if (success)
                {
                    var deviceInfos = JSON.ToType <List <DeviceInfo> >(response);
                    if (deviceInfos != null)
                    {
                        return(deviceInfos);
                    }
                }
            }

            return(null);
        }