<commit_before>﻿namespace KitKare.Server.Controllers
{
    using System;
    using System.IO;
    using System.Linq;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Web.Http;

    using KitKare.Data.Models;
    using KitKare.Data.Repositories;
    using KitKare.Server.Common.Streaming;

   // [Authorize]
    [RoutePrefix("api/Device")]
    public class DeviceController : ApiController
    {
        private const string TeleduinoKey = "FFCC09A911CC4D6B8AF8E8A0941E1F87";

        private IRepository<Video> videos;
        private IRepository<User> users;
        private IRepository<Feeding> feedings;

        private string userId;

        public DeviceController(IRepository<Video> videos, IRepository<User> users, IRepository<Feeding> feedings)
        {
            this.users = users;
            this.videos = videos;
            this.feedings = feedings;

            this.userId = this.users
                .All()
                .Where(x => x.UserName == this.User.Identity.Name)
                .FirstOrDefault()
                ?.Id;
        }
        
        [Route("GiveFood")]
        [HttpGet]
        public IHttpActionResult GiveFood()
        {
            // TODO: make api call to teleduino service
            var feeding = new Feeding
            {
                Quantity = 200,
                Time = DateTime.Now,
                UserId = this.userId
            };

            this.feedings.Add(feeding);
            this.feedings.SaveChanges();

            return this.Ok();
        }

        [Route("GiveWater")]
        [HttpGet]
        public IHttpActionResult GiveWater()
        {
            // TODO: make api call to teleduino service

            return this.Ok();
        }

        [Route("TurnLightsOn")]
        [HttpGet]
        public IHttpActionResult TurnLightsOn()
        {
            // TODO: make api call to teleduino service

            return this.Ok();
        }

        [Route("TurnLightsOff")]
        [HttpGet]
        public IHttpActionResult TurnLightsOff()
        {
            // TODO: make api call to teleduino service

            return this.Ok();
        }
        
        [Route("TurnCameraOn")]
        [HttpGet]
        public HttpResponseMessage TurnCameraOn()
        {
            try
            {
                var video = this.videos.All().FirstOrDefault();

                var videoStream = new VideoStream(video.Data);

                var response = Request.CreateResponse();
                var header = new MediaTypeHeaderValue("video/avi");
                var streamContent = new PushStreamContent((Action<Stream, HttpContent, System.Net.TransportContext>)videoStream.WriteToStream, header);
                response.Content = streamContent;

                return response;
            }
            catch (Exception e)
            {
                return Request.CreateResponse(System.Net.HttpStatusCode.BadRequest, e.Message);

            }

        }

        [Route("TurnCameraOff")]
        [HttpGet]
        public IHttpActionResult TurnCameraOff()
        {

            return this.Ok();
        }
    }
}<commit_msg>Api call to Teleduino service added to GiveFood in DeviceController<commit_after>﻿namespace KitKare.Server.Controllers
{
    using System;
    using System.IO;
    using System.Linq;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Web.Http;

    using KitKare.Data.Models;
    using KitKare.Data.Repositories;
    using KitKare.Server.Common.Streaming;
    using System.Net;
    // [Authorize]
    [RoutePrefix("api/Device")]
    public class DeviceController : ApiController
    {
        private const int ServoPin = 9;
        private const string TeleduinoKey = "2BAAB610021E3E7DFAC34C532F12A540";

        private IRepository<Video> videos;
        private IRepository<User> users;
        private IRepository<Feeding> feedings;

        private string userId;

        public DeviceController(IRepository<Video> videos, IRepository<User> users, IRepository<Feeding> feedings)
        {
            this.users = users;
            this.videos = videos;
            this.feedings = feedings;

            this.userId = this.users
                .All()
                .Where(x => x.UserName == this.User.Identity.Name)
                .FirstOrDefault()
                ?.Id;
        }
        
        [Route("GiveFood")]
        [HttpGet]
        public IHttpActionResult GiveFood()
        {
            var webClient = new WebClient();

            // Define servo
            webClient.DownloadString(string.Format(
                "https://us01.proxy.teleduino.org/api/1.0/328.php?k={0}&r=defineServo&servo=0&pin={1}", TeleduinoKey, ServoPin));

            // Set initial servo position
            webClient.DownloadString(
                string.Format("https://us01.proxy.teleduino.org/api/1.0/328.php?k={0}&r=setServo&servo=0&position=0", TeleduinoKey));

            // Open door
            webClient.DownloadString(
                string.Format("https://us01.proxy.teleduino.org/api/1.0/328.php?k={0}&r=setServo&servo=0&position=90", TeleduinoKey));

            // Close door
            webClient.DownloadString(
                string.Format("https://us01.proxy.teleduino.org/api/1.0/328.php?k={0}&r=setServo&servo=0&position=0", TeleduinoKey));
            
            var feeding = new Feeding
            {
                Quantity = 200,
                Time = DateTime.Now,
                UserId = this.userId
            };

            this.feedings.Add(feeding);

            try
            {
                this.feedings.SaveChanges();

                return this.Ok();
            }
            catch (Exception e)
            {
                return this.BadRequest(e.Message);
            }
        }

        [Route("GiveWater")]
        [HttpGet]
        public IHttpActionResult GiveWater()
        {
            // TODO: make api call to teleduino service

            return this.Ok();
        }

        [Route("TurnLightsOn")]
        [HttpGet]
        public IHttpActionResult TurnLightsOn()
        {
            // TODO: make api call to teleduino service

            return this.Ok();
        }

        [Route("TurnLightsOff")]
        [HttpGet]
        public IHttpActionResult TurnLightsOff()
        {
            // TODO: make api call to teleduino service

            return this.Ok();
        }
        
        [Route("TurnCameraOn")]
        [HttpGet]
        public HttpResponseMessage TurnCameraOn()
        {
            try
            {
                var video = this.videos.All().FirstOrDefault();

                var videoStream = new VideoStream(video.Data);

                var response = Request.CreateResponse();
                var header = new MediaTypeHeaderValue("video/avi");
                var streamContent = new PushStreamContent((Action<Stream, HttpContent, System.Net.TransportContext>)videoStream.WriteToStream, header);
                response.Content = streamContent;

                return response;
            }
            catch (Exception e)
            {
                return Request.CreateResponse(System.Net.HttpStatusCode.BadRequest, e.Message);

            }

        }

        [Route("TurnCameraOff")]
        [HttpGet]
        public IHttpActionResult TurnCameraOff()
        {

            return this.Ok();
        }
    }
}
{'fraction_non_alphanumeric': 0.09816859682197684, 'fraction_numerical': 0.011311607864260706, 'mean_word_length': 1.6271666077113547, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3217131', 'n_tokens_mistral': 2092, 'n_tokens_neox': 1960, 'n_words': 394}