示例#1
0
        private void GetRequest(string uri, HTTPClientResponseCallback callback)
        {
            var request = new ServerRequest(IpAddress, uri, callback);
            var auth    = Convert.ToBase64String(Encoding.ASCII.GetBytes(_username + ":" + _password));

            request.Header.AddHeader(new HttpHeader("Authorization", "Basic " + auth));
            _server.QueueRequest(request);
        }
示例#2
0
        internal void QueueRequest(ServerRequest request)
        {
            RequestQueue.Enqueue(request);

            if (_dispatchThread != null && _dispatchThread.ThreadState == Thread.eThreadStates.ThreadRunning)
            {
                return;
            }

            _dispatchThread = new Thread(specific =>
            {
#if true
                Debug.WriteSuccess("AvediaServer", "Launching {0}.DispacthThread, Request Count = {1}", GetType().Name,
                                   RequestQueue.Count);
                Debug.WriteInfo("AvediaServer", "HttpClient Timeout = {0}, TimeoutEnabled = {1}", HttpClient.Timeout,
                                HttpClient.TimeoutEnabled);
#endif

                while (true)
                {
                    var r = RequestQueue.Dequeue();
                    if (request == null)
                    {
                        CloudLog.Info("Exiting {0}", Thread.CurrentThread.Name);
                        return(null);
                    }
#if true
                    CrestronConsole.PrintLine("{0} {1}", r.RequestType.ToString(), r.Url);
                    if (r.RequestType == RequestType.Post)
                    {
                        CrestronConsole.PrintLine(r.ContentString);
                    }
#endif
                    try
                    {
                        var response = HttpClient.Dispatch(r);

                        try
                        {
                            r.Callback(response, HTTP_CALLBACK_ERROR.COMPLETED);
                        }
                        catch (Exception e)
                        {
                            CloudLog.Exception(e);
                        }
                    }
                    catch
                    {
                        r.Callback(null, HTTP_CALLBACK_ERROR.UNKNOWN_ERROR);
                    }

                    CrestronEnvironment.AllowOtherAppsToRun();
                }
            }, null)
            {
                Name = "Avedia HTTP dispatch process"
            };
        }
示例#3
0
        public void StopRecording(int contentId, string name)
        {
            var requestData = new
            {
                name,
                schedule = new
                {
                    @stop = DateTime.Now,
                }
            };
            var json    = JToken.FromObject(requestData);
            var request = new ServerRequest(_server.HostNameOrIpAddress, "/api/content/recording/scheduled/" + contentId,
                                            json.ToString(), RequestType.Put,
                                            (response, error) =>
            {
                if (error != HTTP_CALLBACK_ERROR.COMPLETED)
                {
                    CloudLog.Warn("Cannot communicate with AvediaServer to get recordings");
                    OnRecordingsUpdated(new RecordingsUpdatedEventArgs
                    {
                        EventType     = RecordingUpdatedEventType.StopRecording,
                        RequestFailed = true,
                        FailReason    = "Could not get response from server"
                    });
                    return;
                }

                if (response.Code != 200)
                {
                    CloudLog.Error("{0} HttpResponse = {1}", GetType().Name, response.Code);
                    OnRecordingsUpdated(new RecordingsUpdatedEventArgs
                    {
                        EventType     = RecordingUpdatedEventType.StopRecording,
                        RequestFailed = true,
                        FailReason    = "Server responded with " + response.Code + " error",
                    });
                    return;
                }

                var data = JToken.Parse(response.ContentString).First;

                var id = data["Id"].Value <int>();

                CloudLog.Error("{0} HttpResponse = {1}", GetType().Name, response.Code);
                OnRecordingsUpdated(new RecordingsUpdatedEventArgs
                {
                    EventType     = RecordingUpdatedEventType.StopRecording,
                    RequestFailed = false,
                    RecordingId   = id
                });
            });

            _server.QueueRequest(request);
        }
示例#4
0
        private void PostRequest(string uri, string data, HTTPClientResponseCallback callback)
        {
            var request = new ServerRequest(IpAddress, uri, callback)
            {
                RequestType = RequestType.Post
            };
            var auth = Convert.ToBase64String(Encoding.ASCII.GetBytes(_username + ":" + _password));

            request.Header.AddHeader(new HttpHeader("Authorization", "Basic " + auth));
            request.Header.ContentType = "application/json";
            request.ContentString      = data;
            _server.QueueRequest(request);
        }
示例#5
0
        public void StartRecording(string name, string description, DateTime startTime, DateTime endTime, string channelName, string channelIp, int channelPort)
        {
            var requestData = new
            {
                name,
                description,
                bitrate = 20000000,
                channel = new
                {
                    @channel = channelName,
                    address  = new
                    {
                        @ip   = channelIp,
                        @port = channelPort
                    }
                },
                schedule = new
                {
                    @start = startTime,
                    @stop  = endTime,
                }
            };
            var json    = JToken.FromObject(requestData);
            var request = new ServerRequest(_server.HostNameOrIpAddress, "/api/content/recording/scheduled",
                                            json.ToString(),
                                            (response, error) =>
            {
                if (error != HTTP_CALLBACK_ERROR.COMPLETED)
                {
                    CloudLog.Warn("Cannot communicate with AvediaServer to get recordings");
                    OnRecordingsUpdated(new RecordingsUpdatedEventArgs
                    {
                        EventType     = RecordingUpdatedEventType.StartRecording,
                        RequestFailed = true,
                        FailReason    = "Could not get response from server"
                    });
                    return;
                }

                if (response.Code != 201)
                {
                    CloudLog.Error("{0} HttpResponse = {1}", GetType().Name, response.Code);
                    OnRecordingsUpdated(new RecordingsUpdatedEventArgs
                    {
                        EventType     = RecordingUpdatedEventType.StartRecording,
                        RequestFailed = true,
                        FailReason    = "Server responded with " + response.Code + " error",
                    });
                    return;
                }

                var data = JToken.Parse(response.ContentString).First;

                var id = data["Id"].Value <int>();

                CloudLog.Error("{0} HttpResponse = {1}", GetType().Name, response.Code);
                OnRecordingsUpdated(new RecordingsUpdatedEventArgs
                {
                    EventType     = RecordingUpdatedEventType.StartRecording,
                    RequestFailed = false,
                    RecordingId   = id
                });
            });

            _server.QueueRequest(request);
        }
示例#6
0
        public void GetRecordings()
        {
            var request = new ServerRequest(_server.HostNameOrIpAddress, "/api/content/recording/scheduled", (response, error) =>
            {
                var recordings = new List <Recording>();

                if (error != HTTP_CALLBACK_ERROR.COMPLETED)
                {
                    CloudLog.Warn("Cannot communicate with AvediaServer to get recordings");
                    OnRecordingsUpdated(new RecordingsUpdatedEventArgs
                    {
                        EventType     = RecordingUpdatedEventType.GetRecordings,
                        RequestFailed = true,
                        FailReason    = "Could not get response from server",
                        Recordings    = new ReadOnlyCollection <Recording>(recordings)
                    });
                    return;
                }

                if (response.Code != 200)
                {
                    CloudLog.Error("{0} HttpResponse = {1}", GetType().Name, response.Code);
                    OnRecordingsUpdated(new RecordingsUpdatedEventArgs
                    {
                        EventType     = RecordingUpdatedEventType.GetRecordings,
                        RequestFailed = true,
                        FailReason    = "Server responded with " + response.Code + " error",
                        Recordings    = new ReadOnlyCollection <Recording>(recordings)
                    });
                    return;
                }

                try
                {
                    var data = JToken.Parse(response.ContentString)["content"];
#if true
                    Debug.WriteNormal(Debug.AnsiPurple + data.ToString(Formatting.Indented) + Debug.AnsiReset);
#endif
                    foreach (var recordingData in data.Where(r => r["status"].Value <string>() == "RECORDING"))
                    {
                        try
                        {
                            recordings.Add(new Recording(recordingData));
                        }
                        catch (Exception e)
                        {
                            CloudLog.Error("Error parsing content data, {0}", e.Message);
                        }
                    }
#if true
                    foreach (var recording in recordings)
                    {
                        Debug.WriteSuccess("Recording " + recording.Id,
                                           "{0} - {1}\r\nDuration: {2}\r\n Channel IP: {3} Port: {4}, Name: {5}", recording.Name,
                                           recording.Description, recording.Duration, recording.ChannelIp,
                                           recording.ChannelPort, recording.ChannelName);
                    }
#endif
                    OnRecordingsUpdated(new RecordingsUpdatedEventArgs
                    {
                        EventType     = RecordingUpdatedEventType.GetRecordings,
                        RequestFailed = false,
                        Recordings    = new ReadOnlyCollection <Recording>(recordings)
                    });
                }
                catch (Exception e)
                {
                    CloudLog.Exception(e);

                    OnRecordingsUpdated(new RecordingsUpdatedEventArgs
                    {
                        EventType     = RecordingUpdatedEventType.GetRecordings,
                        RequestFailed = true,
                        FailReason    = "Server responded but data could not be parsed, " + e.Message,
                        Recordings    = new ReadOnlyCollection <Recording>(recordings)
                    });
                }
            });

            _server.QueueRequest(request);
        }