I wanted to be able to turn my Sony TV off from HomeSeer, so first I did it the easy way by executing a curl command in an event:
That worked great. In fact, it worked so well that I wanted to try to do it in a C# script. The odd thing is that sometimes the script works, but other times it times out with two error messages:
I can find no rhyme or reason as to when it works vs. timing out. Here is my logic, which, I believe, equates to the above curl command:
I'm running HS4 on a HomeSeer Raspberry PI.
/usr/bin/curl --header "Content-Type: application/json; charset=UTF-8" --header "X-Auth-PSK:9999" --request POST --data '{"method":"setPowerStatus","version":"1.0","id":1,"params ":[{"status":false}]}' http://192.168.4.53/sony/system
That worked great. In fact, it worked so well that I wanted to try to do it in a C# script. The odd thing is that sometimes the script works, but other times it times out with two error messages:
Running script SonyTV_Off.cs :Exception has been thrown by the target of an invocation.
Runing script more info: The operation has timed out.
Runing script more info: The operation has timed out.
I can find no rhyme or reason as to when it works vs. timing out. Here is my logic, which, I believe, equates to the above curl command:
public object Main(object[] Parms)
{
var url = "http://192.168.4.53/sony/system";
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url) ;
request.Method = "POST";
request.ContentType = "application/json; charset=UTF-8";
request.Headers.Add("X-Auth-PSK", "9999");
System.IO.Stream dataStream = request.GetRequestStream();
string data = "{"method":"setPowerStatus","version":"1.0","id":1 ,"params":[{"status":false}]}";
System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();
byte[] bytes = utf8.GetBytes(data);
dataStream.Write(bytes, 0, bytes.Length);
dataStream.Close();
System.Net.WebResponse response = request.GetResponse();
hs.WriteLog("CSHARP", "SonyTV_Off.cs is done!");
return null;
}
{
var url = "http://192.168.4.53/sony/system";
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url) ;
request.Method = "POST";
request.ContentType = "application/json; charset=UTF-8";
request.Headers.Add("X-Auth-PSK", "9999");
System.IO.Stream dataStream = request.GetRequestStream();
string data = "{"method":"setPowerStatus","version":"1.0","id":1 ,"params":[{"status":false}]}";
System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();
byte[] bytes = utf8.GetBytes(data);
dataStream.Write(bytes, 0, bytes.Length);
dataStream.Close();
System.Net.WebResponse response = request.GetResponse();
hs.WriteLog("CSHARP", "SonyTV_Off.cs is done!");
return null;
}
I'm running HS4 on a HomeSeer Raspberry PI.