HTTP connection with LG 2011 TV

Python and python framework
Post Reply
elgringo
Posts: 94
Joined: Thursday 18 May 2017 8:08
Target OS: Raspberry Pi
Domoticz version: Left
Contact:

HTTP connection with LG 2011 TV

Post by elgringo » Sunday 02 July 2017 21:24

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. 
Last edited by elgringo on Monday 03 July 2017 20:34, edited 1 time in total.

User avatar
Dnpwwo
Posts: 625
Joined: Sunday 23 March 2014 10:00
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: HTTP connection with LG 2011 TV1

Post by Dnpwwo » Monday 03 July 2017 2:15

@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.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw

elgringo
Posts: 94
Joined: Thursday 18 May 2017 8:08
Target OS: Raspberry Pi
Domoticz version: Left
Contact:

Re: HTTP connection with LG 2011 TV1

Post by elgringo » Monday 03 July 2017 20:02

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

elgringo
Posts: 94
Joined: Thursday 18 May 2017 8:08
Target OS: Raspberry Pi
Domoticz version: Left
Contact:

Re: HTTP connection with LG 2011 TV

Post by elgringo » Monday 17 July 2017 19:55

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?

User avatar
Dnpwwo
Posts: 625
Joined: Sunday 23 March 2014 10:00
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: HTTP connection with LG 2011 TV

Post by Dnpwwo » Wednesday 19 July 2017 13:00

@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 reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw

elgringo
Posts: 94
Joined: Thursday 18 May 2017 8:08
Target OS: Raspberry Pi
Domoticz version: Left
Contact:

Re: HTTP connection with LG 2011 TV

Post by elgringo » Thursday 20 July 2017 16:18

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

User avatar
Dnpwwo
Posts: 625
Joined: Sunday 23 March 2014 10:00
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: HTTP connection with LG 2011 TV

Post by Dnpwwo » Friday 21 July 2017 12:08

@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.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw

elgringo
Posts: 94
Joined: Thursday 18 May 2017 8:08
Target OS: Raspberry Pi
Domoticz version: Left
Contact:

Re: HTTP connection with LG 2011 TV

Post by elgringo » Wednesday 02 August 2017 18:24

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 :) )

User avatar
Dnpwwo
Posts: 625
Joined: Sunday 23 March 2014 10:00
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: HTTP connection with LG 2011 TV

Post by Dnpwwo » Tuesday 08 August 2017 11:45

@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
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw

elgringo
Posts: 94
Joined: Thursday 18 May 2017 8:08
Target OS: Raspberry Pi
Domoticz version: Left
Contact:

Re: HTTP connection with LG 2011 TV

Post by elgringo » Tuesday 08 August 2017 14:21

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 :)

Post Reply

Who is online

Users browsing this forum: kofec and 5 guests