Example #1
0
        public bool TakePhoto(WebServer server, HttpListenerContext context)
        {
            try
            {
                if (IsRunningOnMono())
                {
                    var process = Process.Start("raspistill",
                        "-o /home/pi/target/ui/" + DateTime.Now.ToString("yyyMMdd-HHmm") + ".jpg");

                    process.WaitForExit();
                }
                else
                {
                    var filename = Path.Combine(photodir, DateTime.Now.ToString("yyyMMdd-HHmm") + ".jpg");

                    (new WebClient()).DownloadFile("http://unosquare.github.io/assets/embedio.png", filename);
                }

                var dir = new DirectoryInfo(photodir);
                var files = dir.GetFiles("*.jpg").Select(x => x.Name).Take(10);

                return context.JsonResponse(files);
            }
            catch (Exception ex)
            {
                return HandleError(context, ex);
            }
        }
Example #2
0
            protected bool HandleError(HttpListenerContext context, Exception ex, int statusCode = 500) {
                var errorResponse = new {
                    Title = "Unexpected Error",
                    ErrorCode = ex.GetType().Name,
                    Description = ex.ExceptionMessage(),
                };

                context.Response.StatusCode = statusCode;
                return context.JsonResponse(errorResponse);
            }
Example #3
0
        public bool GetHosts(WebServer server, HttpListenerContext context)
        {
            try
            {
                var data = LocalCdn.HostEntries;

                return context.JsonResponse(data);
            }
            catch (Exception ex)
            {
                return HandleError(context, ex);
            }
        }
Example #4
0
        public bool Get(WebServer server, HttpListenerContext context)
        {
            try
            {
                var data = EntryRepository.GetAll();

                var lastSegment = context.Request.Url.Segments.Last();

                if (lastSegment.EndsWith("/"))
                    return context.JsonResponse(data.Select(x => x.Name));

                if (data.Any(p => p.Name == lastSegment))
                {
                    return context.JsonResponse(data.FirstOrDefault(p => p.Name == lastSegment));
                }

                throw new KeyNotFoundException("Key Not Found: " + lastSegment);
            }
            catch (Exception ex)
            {
                return HandleError(context, ex);
            }
        }
Example #5
0
        public bool GetPhotos(WebServer server, HttpListenerContext context)
        {
            try
            {
                var dir = new DirectoryInfo(photodir);
                var files = dir.GetFiles("*.jpg").Select(x => x.Name).Take(10);

                return context.JsonResponse(files);
            }
            catch (Exception ex)
            {
                return HandleError(context, ex);
            }
        }
Example #6
0
        public bool GetPeople(WebServer server, HttpListenerContext context)
        {
            try
            {
                var model = context.ParseJson<GridDataRequest>();

                return context.JsonResponse(model.CreateGridDataResponse(People.AsQueryable()));
            }
            catch (Exception ex)
            {
                // here the error handler will respond with a generic 500 HTTP code a JSON-encoded object
                // with error info. You will need to handle HTTP status codes correctly depending on the situation.
                // For example, for keys that are not found, ou will need to respond with a 404 status code.
                return HandleError(context, ex);
            }
        }
Example #7
0
            public bool GetPeople(WebServer server, HttpListenerContext context)
            {
                try
                {
                    // read the last segment
                    var lastSegment = context.Request.Url.Segments.Last();

                    // if it ends with a / means we need to list people
                    if (lastSegment.EndsWith("/"))
                        return context.JsonResponse(RestApiSample.People);

                    // otherwise, we need to parse the key and respond with the entity accordingly
                    int key = 0;
                    if (int.TryParse(lastSegment, out key) && People.Any(p => p.Key == key))
                    {
                        return context.JsonResponse(People.FirstOrDefault(p => p.Key == key));
                    }

                    throw new KeyNotFoundException("Key Not Found: " + lastSegment);
                }
                catch (Exception ex)
                {
                    // here the error handler will respond with a generic 500 HTTP code a JSON-encoded object
                    // with error info. You will need to handle HTTP status codes correctly depending on the situation.
                    // For example, for keys that are not found, ou will need to respond with a 404 status code.
                    return HandleError(context, ex, (int) HttpStatusCode.InternalServerError);
                }
            }
Example #8
0
        public async Task<bool> ButtonIo(WebServer server, HttpListenerContext context)
        {
            var lastSegment = context.Request.Url.Segments.Last();
            var bcmPinNumber = int.Parse(lastSegment);

            return context.JsonResponse(new
            {
                count = ButtonCheck[bcmPinNumber]
            });
        }