msloth / lgtv.js

Control LG WebOS TV using node.js
MIT License
249 stars 119 forks source link

Allow Hostname to be Set #1

Closed maddox closed 8 years ago

maddox commented 9 years ago

Second, you should enable the TV to broadcast itself as lgsmarttv.lan in the local network. This setting is, IIRC, under Network/LG Connect Apps. This is necessary in order for this module to find the TV on the network.

Any thoughts on allowing users to pass in the hostname of the tv on init?

lgtv.connect('living-room', function(err, response){
  if (!err) {
    lgtv.show_float("It works!", function(err, response){
      if (!err) {
        lgtv.disconnect();
      }
    }); // show float
  }
}); // connect
msloth commented 8 years ago

Sounds like a great addition! I'd be happy for a PR as I unfortunately haven't worked anymore on this for a looooong time (and sorry for the very late response btw).

gaving commented 8 years ago

:+1:

msloth commented 8 years ago

I found a way to autodetect it through sending SSDP discover datagrams; have the POC in python, gonna port it back to this js. If anyone would like to go ahead before me, here's how: send this as an UDP to 239.255.255.250:1900. Check responses for the string "LG Smart TV".

  ssdp_msg = 'M-SEARCH * HTTP/1.1\r\n'
  ssdp_msg += 'HOST: 239.255.255.250:1900\r\n'
  ssdp_msg += 'MAN: "ssdp:discover"\r\n'
  ssdp_msg += 'MX: 5\r\n'
  ssdp_msg += "ST: urn:dial-multiscreen-org:service:dial:1\r\n"
  ssdp_msg += "USER-AGENT: iOS/5.0 UDAP/2.0 iPhone/4\r\n\r\n"

Example response (it actually sends two identical just after eachother),

HTTP/1.1 200 OK
CACHE-CONTROL: max-age=1800
DATE: Thu, 29 Oct 2015 21:43:41 GMT
EXT:
LOCATION: http://192.168.1.86:1900/description.xml
SERVER: Linux/3.10.19-22 UPnP/1.0 LG Smart TV/1.0
ST: urn:dial-multiscreen-org:service:dial:1
USN: uuid:dea2b14d-f326-4638-ad40-0392b189c220::urn:dial-multiscreen-org:service:dial:1
msloth commented 8 years ago

...and what I mean by that is that setting a hostname would be unnecessary at the cost of the UDP exchange at the start.

avenantsoftware commented 8 years ago

kewl, can't wait

msloth commented 8 years ago

No need, done. Solved with commit 31bf1f9b636dc62a805e0e127ec57f66ef9a87f6 Now you can either specify a hostname, use the default, or chain a autodiscover before a connect. The discover adds an additional 3-4 seconds though.

avenantsoftware commented 8 years ago

I get an error:

/Users/htpc/node_modules/lgtv/index.js:295 if (hoststr.indexOf("ws://") !== 0) { ^

TypeError: hoststr.indexOf is not a function at _check_host_string (/Users/htpc/node_modules/lgtv/index.js:295:17) at Object.connect (/Users/htpc/node_modules/lgtv/index.js:313:10) at run_test (/Users/htpc/Source/lgtv.js/examples/show-float.js:4:8) at Object. (/Users/htpc/Source/lgtv.js/examples/show-float.js:15:1) at Module._compile (module.js:435:26) at Object.Module._extensions..js (module.js:442:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:311:12) at Function.Module.runMain (module.js:467:10) at startup (node.js:134:18)

msloth commented 8 years ago

Check my answer in #2 .

avenantsoftware commented 8 years ago

Do I need to alter your code? I just don't seem to get it? I am trying your example show-float.js and that's when i get the error, also the other examples also give the error.

I want on my android phone, with the app tasker and the tasker addon autovoice, to control the tv with voice commands from my android phone and android wear watch and also create that when I receive a pushover message from my arduino on my phone, wich controls all my home automation, to display the pushover messages as float message on my tv and also control the tv from kodi with the xbmc.callbacks2 addon wich alows to run scripts on certain kodi actions and also to be able to start netflix with a script, or trough voice commands from tasker and autovoice and also trough geofencing put the tv on standby when I leave my home, etc.. etc..

avenantsoftware commented 8 years ago

nevermind figured out how it works, the example files needed to be changed, now it works, verry cool btw, thanx for your help and for updating the code!

for other users:

for example to make the show-float.js work

change: "lgtv.connect(function(err, response)"

to: lgtv.connect("xxx.xxx.xxx.xxx", function(err, response)

where the x's are the ip address of the tv in your lan

avenantsoftware commented 8 years ago

I used your code to modify the app pullover wich can display pushover messages on osx/windows/linux to also display the received messages on a lg tv.

This is the pullover app: https://github.com/cgrossde/Pullover

If anyone is interested howto modify it ask...

msloth commented 8 years ago

Cool! Thanks, the reason was that the examples were not updated so they passed the callback as first parameter and my catch-it code didn't do anything :sake: . Now it sets a default, but I updated the examples and added a new one to set input through the command line like node input-cli.js HDMI_1.

It would be great to read of your project on a blog, or if you made a repo for it!

avenantsoftware commented 8 years ago

Once I figure this whole git thing out I will clone the pullover repository and add my modified version to github.

btw, when I run npm install lgtv I get your old code? does the node package manager get your code from some other source? or does it take time before it is synced with your changes or something?

btw all the other stuff I wanted to do can be done from Tasker on android, but then I need to install Node on android,but in order to do that I need to compile node myself with the android NDK, but all tutorials are for 32 bit arm android and my phone is 64 bit arm lol, no luck so far compiling for arm64, there is also jxcore wich is like node their download page says it has binaries for android also 64 bit, but if you download it, it's a zip file with no executable file in it and no explanation what to do with it lol and then there is still the problem if you can use npm on android to install your code. the android port of node is pretty basic.

I also tried using your code in my os x app written in objective-c, also some problems there running console commands from objective-c to execute node with js files doesn't work lol.

I also tried to avoid using node on android and trigger from tasker a python launcher I created https://github.com/spambus/Launcher wich runs on my mac wich can execute programs and run console commands on my mac, but I ran into the same problems as from objective-c, unable to run node with a js file from python executing a console command, I did however figure out how to parse a variable from outside to your show-float.js by altering it, so you can send for example different messages everytime you run show-float.js

if you change in show-float.js:

"It works!"

to: process.argv.slice(2)

you can run from console: node show-float.js "your message here" (including the "") and change the message to whatever you want whenever you run the command, you can also use this in your other examples to switch inputs or start different apps on the tv etc.. so you don't need to create multiple versions of the same code.

msloth commented 8 years ago

Nice; sorry to hear that it's messy with getting node on android/ios (I didn't even know there was a shot for it). However, if you want to rewrite this to work on android/ios on swift/java/obj-c then "just" look in the index.js file for the commands that are sent. It is basically two parts: UDP send and recieve for autodetect the TV, and websockets for the commands.

I have to run npm publish to push the current version to npm, so I guess it keeps version<->commit, but life got in the way before. Doing it now.

avenantsoftware commented 8 years ago

here is the fork of pullover with your code: https://github.com/spambus/Pullover

it only requires 2 small modifications

https://github.com/spambus/Pullover/commit/277faf82a854006e945524f134d5189f9a9813f1 https://github.com/spambus/Pullover/commit/b112dd89dfdebe1156347d57a3bea5542455b8e6

somehow with your auto connect code it doesn't work, only when you give the ip address in the code

it's verry kewl to receive pushover messages directly on the tv :)