예제 #1
0
 public async Task <JObject> SetLightsAsync([FromBody] Light light)
 {
     try
     {
         if (light.Controller)
         {
             var context = new DatabaseContext();
             context.Lights.Add(light);
             context.SaveChanges();
         }
         else
         {
             var      service  = new MqttService();
             GetLight newLight = new GetLight()
             {
                 Animation = light.Animation, Brightness = light.Brightness, Color = light.Color
             };
             await service.SendMessageAsync("boominator", JObject.FromObject(newLight).ToString());
         }
         var value = "{ \"response\": \"success\" }";
         return(JObject.Parse(value));
     }
     catch (Exception e)
     {
         var value = "{ \"response\": \"error " + e.Message + "\" }";
         return(JObject.Parse(value));
     }
 }
예제 #2
0
 public JObject GetLights()
 {
     try
     {
         var      context = new DatabaseContext();
         var      count   = context.Lights.Count();
         Light    light   = context.Lights.OrderByDescending(x => x.Id).FirstOrDefault();
         GetLight response;
         if (light == null)
         {
             response = new GetLight {
                 Animation = null, Brightness = 0, Color = new Color {
                     R = 0, G = 0, B = 0
                 }
             };
         }
         else
         {
             response = new GetLight {
                 Animation = light.Animation, Brightness = light.Brightness, Color = light.Color
             };
         }
         return(JObject.FromObject(response));
     }
     catch (Exception e)
     {
         var value = "{ \"response\": \"error " + e.Message + "\" }";
         return(JObject.Parse(value));
     }
 }