-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeMoSwitch.cs
49 lines (42 loc) · 1.23 KB
/
WeMoSwitch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System.ServiceModel;
using Karpach.Wemo.Switcher.WemoService;
namespace Karpach.Wemo.Switcher
{
public class WeMoSwitch
{
public static WeMoSwitch ConnectTo(string deviceName)
{
var device = new WeMoFinder().Find(deviceName);
return device != null ? new WeMoSwitch(device.IpAddress, device.Port) : null;
}
private readonly BasicServicePortTypeClient _client;
public WeMoSwitch(string ip, int port)
{
_client = new BasicServicePortTypeClient();
_client.Endpoint.Address = new EndpointAddress($"http://{ip}:{port}/upnp/control/basicevent1");
}
public bool IsOff()
{
var state = _client.GetBinaryState(new GetBinaryState());
return state.BinaryState != "1";
}
public void TurnOn()
{
if (IsOff())
{
SetState("1");
}
}
public void TurnOff()
{
if (!IsOff())
{
SetState("0");
}
}
private void SetState(string binaryState)
{
_client.SetBinaryState(new SetBinaryState { BinaryState = binaryState });
}
}
}