コード例 #1
0
        // Unsubscribe to the relay on the server control system
        public void Unsubscribe()
        {
            try
            {
                if (!subscribed)
                {
                    CrestronConsole.PrintLine("Already unsubscribed to relay on " + serverAPIUrl);
                    return;
                }
                // send a DELETE request to the subscription URL
                HttpClientRequest req = new HttpClientRequest();

                req.Url         = subscriptionUrl;
                req.RequestType = RequestType.Delete;

                HttpClientResponse res = client.Dispatch(req);

                if (res.Code == 204) // Expecting a "Not Found" response to show that the DELETE succeeded
                {
                    CrestronConsole.PrintLine("Deleted subscription to relay " + rlyID);
                    subscribed = false;
                }
                else
                {
                    CrestronConsole.PrintLine("Server was unable to delete subscription to relay " + rlyID);
                }
                res.Dispose();
            }
            catch (Exception e)
            {
                CrestronConsole.PrintLine("Error in Unsubscribe: " + e.Message);
            }
        }
コード例 #2
0
        private string SendToClient(string url)
        {
            try
            {
                var body = string.Empty;
                using (HttpClient client = new HttpClient())
                {
                    HttpClientRequest request = new HttpClientRequest();

                    client.TimeoutEnabled = true;
                    client.Timeout        = 25;
                    client.Port           = p;
                    request.Url.Parse(url);
                    request.Header.AddHeader(new HttpHeader("X-Plex-Client-Identifier", identifier));
                    request.Header.AddHeader(new HttpHeader("X-Plex-Target-Client-Identifier", identifier));
                    request.Header.AddHeader(new HttpHeader("X-Plex-Device-Name", "Crestron"));
                    request.Header.AddHeader(new HttpHeader("Access-Control-Expose-Headers", "X-Plex-Client-Identifier"));

                    HttpClientResponse response = client.Dispatch(request);
                    body = response.ContentString;

                    request = null;
                    response.Dispose();
                }
                return(body);
            }
            catch (Exception e)
            {
                e.ToString();
                return(string.Empty);
            }
        }
コード例 #3
0
            // Notify() sends a POST request to the Subscription's callback URL.
            // DispatchAsync could be used to keep this method from blocking if the server has many subscriptions
            public void Notify()
            {
                try
                {
                    HttpClientRequest req = new HttpClientRequest();
                    req.RequestType = RequestType.Post;

                    // First, resolve the hostname using the ResolveHost method
                    var    parsedUrl        = new UrlParser(SubscriberCallbackUrl);
                    string resolvedHostname = ResolveHost(parsedUrl.Hostname);

                    // Swap in the resolved hostname when setting the target URL of the notification
                    string resolvedUrl = parsedUrl.ToString().Replace(parsedUrl.Hostname, resolvedHostname);
                    req.Url = new UrlParser(resolvedUrl);
                    HttpHeaders headers = new HttpHeaders();
                    headers.SetHeaderValue("Notification-Type", NotificationType);
                    headers.SetHeaderValue("Link", "<" + ResourceUrl + ">, rel=\"Resource\"");
                    req.Header = headers;
                    req.FinalizeHeader();
                    HttpClientResponse res = notifier.Dispatch(req);

                    if (res.Code != 200)
                    {
                        CrestronConsole.PrintLine("Notification to " + resolvedUrl + " not acknowledged");
                    }

                    // Must call Dispose on the HttpClientResponse object to end the http session
                    res.Dispose();
                }
                catch (Exception e)
                {
                    CrestronConsole.PrintLine(e.ToString());
                }
            }
コード例 #4
0
        public static void DeleteSongFromPlaylist(LibraryItem song, string playlistName, string name)
        {
            try
            {
                LibraryItem playlist;
                if ((playlist = Playlists.Find(x => x.Name == playlistName)) != null)
                {
                    using (HttpClient client = new HttpClient())
                    {
                        client.Port           = p;
                        client.TimeoutEnabled = true;
                        client.Timeout        = 25;

                        HttpClientRequest request = new HttpClientRequest();

                        request.Url.Parse(string.Format("http://{0}{1}/{2}?uri=server://{3}/com.plexapp.plugins.library{4}", adr, playlist.Key, song.PlaylistItemID, MachineIdentifier, song.Key));
                        request.Header.AddHeader(new HttpHeader("Accept", "application/json"));
                        request.Header.AddHeader(new HttpHeader("X-Plex-Token", token));
                        request.RequestType = RequestType.Delete;


                        HttpClientResponse response = client.Dispatch(request);

                        var body = response.ContentString;
                        response.Dispose();
                        GetPlaylist(playlist.Key, name);
                    }
                }
            }
            catch (Exception e)
            {
                ErrorLog.Exception("Error dispatching deleting song to playlist delete", e);
            }
        }
コード例 #5
0
        public static void CreatePlaylist(string tag, string name)
        {
            try
            {
                LibraryItem playlist;
                if ((playlist = Playlists.Find(x => x.Name == tag)) == null)
                {
                    using (HttpClient client = new HttpClient())
                    {
                        client.Port           = p;
                        client.TimeoutEnabled = true;
                        client.Timeout        = 25;

                        HttpClientRequest request = new HttpClientRequest();

                        request.Url.Parse(string.Format("http://{0}/playlists/?uri=server://{1}/com.plexapp.plugins.library/library/&title={2}&smart=0&type=audio", adr, MachineIdentifier, tag));
                        request.Header.AddHeader(new HttpHeader("Accept", "application/json"));
                        request.Header.AddHeader(new HttpHeader("X-Plex-Token", token));
                        request.RequestType = RequestType.Post;


                        HttpClientResponse response = client.Dispatch(request);

                        var body = response.ContentString;
                        response.Dispose();
                        GetPlaylists(name);
                    }
                }
            }
            catch (Exception e)
            {
                ErrorLog.Exception("Error dispatching a create playlist post", e);
            }
        }
コード例 #6
0
        private static string Dispatch(string path, bool reqMID, RequestType type)
        {
            try
            {
                string body = string.Empty;

                using (HttpClient client = new HttpClient())
                {
                    client.Port           = p;
                    client.TimeoutEnabled = true;
                    client.Timeout        = 25;

                    HttpClientRequest request = new HttpClientRequest();

                    if (reqMID)
                    {
                        request.Url.Parse(string.Format("http://{0}{1}?uri=server://{2}/com.plexapp.pligins.library/{1}", adr, path, MachineIdentifier, path));
                    }
                    else
                    {
                        request.Url.Parse(string.Format("http://{0}{1}", adr, path));
                    }
                    request.Header.AddHeader(new HttpHeader("Accept", "application/json"));
                    request.Header.AddHeader(new HttpHeader("X-Plex-Token", token));
                    request.RequestType = type;


                    HttpClientResponse response = client.Dispatch(request);

                    body = response.ContentString;
                    response.Dispose();
                }

                return(body);
            }
            catch (HttpException h)
            {
                ErrorLog.Error("Error dispatching request: {0}", h.Message);
                return(string.Empty);
            }
            catch (Exception e)
            {
                ErrorLog.Error("Error dispatching request: {0}", e.Message);
                return(string.Empty);
            }
        }
コード例 #7
0
        // Subscribe to a random relay port on the server control system. The RelayMonitor will print notifications
        // to the console when this relay changes state
        public void Subscribe(string serverHostname)
        {
            try
            {
                if (subscribed)
                {
                    CrestronConsole.PrintLine("The monitor can only subscribe to one relay at a time. Unsubscribe first");
                    return;
                }

                // point serverAPIUrl at the root of the remote CWS server
                serverAPIUrl = new UrlParser("http://" + serverHostname + "/cws/api/");

                // GET the relay collection and derive the number of relays
                // available on the control system from the response
                HttpClientRequest req           = new HttpClientRequest();
                UrlParser         collectionUrl = new UrlParser(serverAPIUrl, "relays"); // complete URL = baseUrl + relativeUrl

                req.Url         = collectionUrl;
                req.RequestType = RequestType.Get;
                HttpHeader acceptHeader = new HttpHeader("Accept", "application/vnd.collection+json");
                req.Header.AddHeader(acceptHeader);
                req.FinalizeHeader();
                HttpClientResponse res = client.Dispatch(req);

                if (res.Code == 200)
                {
                    CrestronConsole.PrintLine("Received GET response for the relay collection");

                    string  json       = res.ContentString;
                    JObject collection = JsonConvert.DeserializeObject <JObject>(json);
                    JArray  list       = (JArray)collection["collection"]["items"];
                    int     relayCount = list.Count;
                    Random  rnd        = new Random();
                    rlyID = rnd.Next(1, relayCount + 1);

                    CrestronConsole.PrintLine("Server control system has " + relayCount + " relays. Subscribing to relay #" + rlyID + "...");

                    // Subscribe the the web-hook for the Relay #rlyID
                    req = new HttpClientRequest();
                    UrlParser webhookUrl = new UrlParser(serverAPIUrl, "relays/" + rlyID + "/web-hooks");
                    req.Url         = webhookUrl;
                    req.RequestType = RequestType.Post;
                    // add the Content-Type and Notification-Type headers as required by the RESTful WebHooks standard
                    HttpHeaders headers = new HttpHeaders();
                    headers.SetHeaderValue("Content-Type", "text/uri-list"); // webhook expects POST body to contain a single URL
                    headers.SetHeaderValue("Notification-Type", "UPDATED");  // monitor wants to know when relay state is changed
                    req.Header = headers;
                    req.FinalizeHeader();
                    req.ContentString = listenUrl.ToString();
                    res = client.Dispatch(req);

                    if (res.Code == 201)                                               // successful POST, subscription resource has been created
                    {
                        subscriptionUrl = new UrlParser(res.Header["Location"].Value); // save the obscure URL to the new subscription resource
                        subscribed      = true;
                        CrestronConsole.PrintLine("Subscribed to Relay #" + rlyID);
                        CrestronConsole.PrintLine("Subscription resource URL: " + subscriptionUrl);
                    }
                    else
                    {
                        CrestronConsole.PrintLine("Failed to subscribe to " + rlyID);
                    }
                    // Must call Dispose on the HttpClientResponse object to end this HTTP session
                    res.Dispose();
                }
                else
                {
                    CrestronConsole.PrintLine("Failed to get relay collection");
                    return;
                }
            }
            catch (Exception e)
            {
                CrestronConsole.PrintLine("Error in Subscribe(): " + e.Message);
            }
            finally
            {
            }
        }