Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Net.Http.Headers;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10.  
  11. namespace HttpLightTest
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         int LightBulpID;
  16.         int Saturation;
  17.         int Brightnes;
  18.         bool ON;
  19.         string BridgeIP;
  20.         string UserID;
  21.  
  22.         public Form1()
  23.         {
  24.             InitializeComponent();
  25.  
  26.             tbUserID.Text = "XBI0FIpPgB4AN9TRuuxMFzkRcjCzNiXUvLGm0U12";
  27.             tBIp.Text = "192.168.1.188";
  28.             rBOn.Checked = true;
  29.  
  30.             LightBulpID = 4;
  31.             Saturation = 254;
  32.             Brightnes = 50;
  33.         }
  34.  
  35.         private void bttnSend_Click(object sender, EventArgs e)
  36.         {
  37.             ON = rBOn.Checked;
  38.             if (tBIp.Text != "")
  39.             {
  40.                 if (tbUserID.Text != "")
  41.                 {
  42.                     UserID = tbUserID.Text;
  43.                     if (tBHue.Text != "")
  44.                     {
  45.                         if (int.TryParse(tBHue.Text, out int Hue))
  46.                         {
  47.  
  48.                             string IP = tBIp.Text; ;
  49.                             BridgeIP = tBIp.Text;
  50.  
  51.                             //var response = Send(Hue);
  52.                             //rTBresulst.Text = response;
  53.  
  54.  
  55.                             RunAll();
  56.                         }
  57.                         else
  58.                         {
  59.                             rTBresulst.Text = "Hue must be a number";
  60.                         }
  61.                     }
  62.                     else
  63.                     {
  64.                         rTBresulst.Text = "Hue must be filled";
  65.                     }
  66.                 }
  67.                 else
  68.                 {
  69.                     rTBresulst.Text = "User ID must be filled";
  70.                 }
  71.             }
  72.             else
  73.             {
  74.                 rTBresulst.Text = "Bridge IP must be filled";
  75.             }
  76.         }
  77.         public void RunAll()
  78.         {
  79.             while (ON)
  80.             {
  81.                 int Hue = 0;
  82.                 int b = 1;
  83.                 while (ON)
  84.                 {
  85.                     Thread.Sleep(1);
  86.                     if (Hue < 65535 && b == 0)
  87.                     {
  88.                         Hue -= 50;
  89.                         if (Hue == 0)
  90.                         {
  91.                             b = 1;
  92.                         }
  93.                     }
  94.                     else if (Hue >= 0 && b == 1)
  95.                     {
  96.                         Hue += 50;
  97.                         if (Hue == 60000)
  98.                         {
  99.                             b = 0;
  100.                         }
  101.                     }
  102.                     string IP = tBIp.Text; ;
  103.                     BridgeIP = tBIp.Text;
  104.  
  105.                     var response = Send(Hue);
  106.  
  107.                     rTBresulst.Text = response;
  108.                 }
  109.             }
  110.         }
  111.         //private string Send(string BridgeIP, string UserID, int LightBulpID, bool ON, int Saturation, int Brightnes, int Hue)
  112.         private string Send(int Hue)
  113.         {
  114.             string responce;
  115.  
  116.  
  117.             Hue hue = new Hue();
  118.             hue.on = ON;
  119.             hue.sat = Saturation;
  120.             hue.bri = Brightnes;
  121.             hue.hue = Hue;
  122.  
  123.  
  124.             string json = JsonConvert.SerializeObject(hue);
  125.  
  126.  
  127.             string Address = "http://" + BridgeIP + "/api/" + UserID + "/lights/" + LightBulpID + "/state/";
  128.             using (WebClient client = new WebClient())
  129.             {
  130.                 responce = client.UploadString(Address, "PUT", json);
  131.             }
  132.             return responce;
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement