Page 1 of 1

HTTP connection with LG 2011 TV

Posted: Sunday 02 July 2017 21:24
by elgringo
I am trying to create a plugin for the LG 2012 series smart TV

Protocol is described here: https://www.domoticz.com/wiki/LG_2012_smart_tv however the 2011 tv must use 'hdcp' iso 'roap' See: https://community.home-assistant.io/t/w ... ote/188/10

I create a connection:

Code: Select all

self.connection = Domoticz.Connection(Name="LG_TCP", Transport="TCP/IP", Protocol="HTTP", Address=Parameters["Address"], Port=Parameters["Port"])
and send the paring key command:

Code: Select all

self.headers = {"Content-Type": "application/atom+xml"}
reqKey = "<auth><type>AuthKeyReq</type></auth>"
self.connection.Send(Message=reqKey, URL="/hdcp/api/auth", Verb="POST", Headers=self.headers)
This result in a intenal server error (500)

When using a script (python 2.7) with following code:

Code: Select all

headers = {"Content-Type": "application/atom+xml"}
conn = httplib.HTTPConnection( "192.168.13.15", port=8080)
reqKey = "<auth><type>AuthKeyReq</type></auth>"
conn.request("POST", "/hdcp/api/auth", reqKey, headers=headers)
httpResponse = conn.getresponse()
if httpResponse.reason != "OK" :
  print 'Failed'


It shows the pairing key. I don't see any differences between the two. What am I doing wrong?

When starting it in debug mode logging report:

Code: Select all

2017-07-02 21:55:56.874 Error: CPlugin:CConnection_init, unable to find module for current interpreter.
2017-07-02 21:55:56.874 Error: CConnection_connect:, illegal operation, Plugin has not started yet. 

Re: HTTP connection with LG 2011 TV1

Posted: Monday 03 July 2017 2:15
by Dnpwwo
@elgringo,

I'm still new to HTTP and I'm not familiar with the HttpLib functionality but I'm prepared to guess :)

I suspect that the httplib.HTTPConnection is enhancing the request while I know that the current Domoticz HTTP protocol support does not (maybe I should add it?). I would recommend having a look at the DLink example plugin because it does something similar to what you are doing (https://github.com/domoticz/domoticz/bl ... SP-W215.py).

Although it seems to vary from web server to web server there are some headers that seem to be required, specifically:
  • Content-Length
  • Host
I would try adding headers from the example progressively until the web server is happy.

Re: HTTP connection with LG 2011 TV1

Posted: Monday 03 July 2017 20:02
by elgringo
Thanks dnpwwo!

The LG required data when a connection was established. After a single command was send the connection was closed at once (by the TV). So I only must establish one when I need to send data.

Complete plugin is stored at: https://github.com/ericstaal/domoticz/t ... /plugin/LG

Re: HTTP connection with LG 2011 TV

Posted: Monday 17 July 2017 19:55
by elgringo
I noticed it works the first time. Domoticz started for the first time, TV still off. When I turn on the TV a connection get established. After the TV is turn off and on again Domoticz cannot connect. It will return a HTTP connection refused.

It looks like the connection is not cleaned up because after a restart of Domoticz, with the tv still on, it does connect. What could be the problem?

Re: HTTP connection with LG 2011 TV

Posted: Wednesday 19 July 2017 13:00
by Dnpwwo
@elgringo,

I would suggest you turn debugging on and submit the log to the form.

From looking at github I do have some feedback:
  • The Connection objects know if they are connected or not and you can ask them via .Connected() and .Connecting(). Keeping a separate variable is not required and not optimal.
  • You can reconnect existing Connections so you don't need to set them back to None and then re-create them
  • It is interesting that you are pinging the TV during the heartbeat. Why is that required? Does the LG close the connection after a time? or does it not get closed when the TV is turned off?

Re: HTTP connection with LG 2011 TV

Posted: Thursday 20 July 2017 16:18
by elgringo
Dnpwwo wrote:@elgringo,

I would suggest you turn debugging on and submit the log to the form.

From looking at github I do have some feedback:
  • The Connection objects know if they are connected or not and you can ask them via .Connected() and .Connecting(). Keeping a separate variable is not required and not optimal.
  • You can reconnect existing Connections so you don't need to set them back to None and then re-create them
  • It is interesting that you are pinging the TV during the heartbeat. Why is that required? Does the LG close the connection after a time? or does it not get closed when the TV is turned off?
The function connecting was not available, so I created them. What happens if I call connect whne the satte is either connecting or connected?

The ping is to check if the Tv is 'on', if somebody switches it off via remote control or so. The TV does not support WOL either so I have to remove this part as wel. The TV closes the connection after each command.

There might be a problem with threadsafety issues. Do the onConnect, onHeartbeat, onMessage, onCommand and on Disconnect function called from the same thread? Is not what is the relation between the threadsand functions

Re: HTTP connection with LG 2011 TV

Posted: Friday 21 July 2017 12:08
by Dnpwwo
@elgringo,
What happens if I call connect whne the satte is either connecting or connected?
The connection request is ignored and an error is logged telling you why.
There might be a problem with threadsafety issues. Do the onConnect, onHeartbeat, onMessage, onCommand and on Disconnect function called from the same thread?
All the callbacks are called by the same thread so there can never be any thread safety issues.

I'm not surprised that the TV closes the connection after each command, it seems that many devices exhibit similar behavior. Others close it after 10 seconds or a couple of minutes. Resorting to pinging is still back though for two reasons:
  • It is slow and will stop all your plugins for up to a second during every heart beat
  • Makes the plugin platform specific because ping has different parameters for Windows and Linux
An approach I've used in the past in situations like this is to initiate a connection in the onHeartbeat even if you don't have a command to send. If the connect succeeds you know the device is on otherwise you should get a socket timeout error in onConnect so you know it is not.

Re: HTTP connection with LG 2011 TV

Posted: Wednesday 02 August 2017 18:24
by elgringo
After a lot of try and error I found out the LG sometimes does not start its webserver. Doing a factory reset made it better, however not always. So it problem i proberbly the upnp was disabled in the router. Turning it on solved the problem....

My ping does not to the plugin. It is start on an separate task and the output is stored on the /tmp dir (= ramdrive). Next tick it reads the file to check if it is online. It is not a neat solution, but it work (feature request: sending ICMP messages via the plugin framework :) )

Re: HTTP connection with LG 2011 TV

Posted: Tuesday 08 August 2017 11:45
by Dnpwwo
@elgringo,
(feature request: sending ICMP messages via the plugin framework :) )
I just found out that the boost libraries support ICMP (http://www.boost.org/doc/libs/1_64_0/do ... p/ping.cpp) so when I get UDP/IP sorted I'll add this

Re: HTTP connection with LG 2011 TV

Posted: Tuesday 08 August 2017 14:21
by elgringo
Dnpwwo wrote:@elgringo,
(feature request: sending ICMP messages via the plugin framework :) )
I just found out that the boost libraries support ICMP (http://www.boost.org/doc/libs/1_64_0/do ... p/ping.cpp) so when I get UDP/IP sorted I'll add this
Oh I really like it :)