Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Threading;
- namespace HttpLightTest
- {
- public partial class Form1 : Form
- {
- int LightBulpID;
- int Saturation;
- int Brightnes;
- bool ON;
- string BridgeIP;
- string UserID;
- public Form1()
- {
- InitializeComponent();
- tbUserID.Text = "XBI0FIpPgB4AN9TRuuxMFzkRcjCzNiXUvLGm0U12";
- tBIp.Text = "192.168.1.188";
- rBOn.Checked = true;
- LightBulpID = 4;
- Saturation = 254;
- Brightnes = 50;
- }
- private void bttnSend_Click(object sender, EventArgs e)
- {
- ON = rBOn.Checked;
- if (tBIp.Text != "")
- {
- if (tbUserID.Text != "")
- {
- UserID = tbUserID.Text;
- if (tBHue.Text != "")
- {
- if (int.TryParse(tBHue.Text, out int Hue))
- {
- string IP = tBIp.Text; ;
- BridgeIP = tBIp.Text;
- //var response = Send(Hue);
- //rTBresulst.Text = response;
- RunAll();
- }
- else
- {
- rTBresulst.Text = "Hue must be a number";
- }
- }
- else
- {
- rTBresulst.Text = "Hue must be filled";
- }
- }
- else
- {
- rTBresulst.Text = "User ID must be filled";
- }
- }
- else
- {
- rTBresulst.Text = "Bridge IP must be filled";
- }
- }
- public void RunAll()
- {
- while (ON)
- {
- int Hue = 0;
- int b = 1;
- while (ON)
- {
- Thread.Sleep(1);
- if (Hue < 65535 && b == 0)
- {
- Hue -= 50;
- if (Hue == 0)
- {
- b = 1;
- }
- }
- else if (Hue >= 0 && b == 1)
- {
- Hue += 50;
- if (Hue == 60000)
- {
- b = 0;
- }
- }
- string IP = tBIp.Text; ;
- BridgeIP = tBIp.Text;
- var response = Send(Hue);
- rTBresulst.Text = response;
- }
- }
- }
- //private string Send(string BridgeIP, string UserID, int LightBulpID, bool ON, int Saturation, int Brightnes, int Hue)
- private string Send(int Hue)
- {
- string responce;
- Hue hue = new Hue();
- hue.on = ON;
- hue.sat = Saturation;
- hue.bri = Brightnes;
- hue.hue = Hue;
- string json = JsonConvert.SerializeObject(hue);
- string Address = "http://" + BridgeIP + "/api/" + UserID + "/lights/" + LightBulpID + "/state/";
- using (WebClient client = new WebClient())
- {
- responce = client.UploadString(Address, "PUT", json);
- }
- return responce;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement