This is the forum archive of Homey. For more information about Homey, visit the Official Homey website.

The Homey Community has been moved to https://community.athom.com.

This forum is now read-only for archive purposes.
Closed

failed http-requests hanging apps (or Homey)

bvdbosbvdbos Member
I get the feeling quite some errors with hanging apps (and perhaps Homey) are caused by failed and/or refused http-requests. Like this error (not on my Homey):
Error: connect ECONNREFUSED 192.168.1.61:80
   at Object.exports._errnoException (util.js:870:11)
   at exports._exceptionWithHostPort (util.js:893:20)
   at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1061:14)
Error: socket hang up
   at createHangUpError (_http_client.js:200:15)
   at Socket.socketCloseListener (_http_client.js:232:23)
   at emitOne (events.js:82:20)
   at Socket.emit (events.js:169:7)
   at TCP._onclose (net.js:477:12)

--- INFO: com.sony.androidtv has been killed ---
Disconnected from Homey, automatically reconnecting...
Trying to reconnect...
Debug session expired, exiting...
A connection refused or socket hang up shouldn't crash the app or is this an error in the app not providing some fallback for functions returning an error?

Comments

  • Yes, the connection refused error seems quite familiar. This is the error I get when Homey crashes but is still connected to my LAN.
  • MarcoFMarcoF Member
    edited May 2016
    @BasVanDenBosch ;;
    Error came from me and error I posted. 

    It's looks like a homey issue. 
    I moved exact the same code to a separate nodejs file and it's runs over and over and over.... Again without a hitch.... 

    Running the code on homey results (almost) every time in a error and killed a app. 

    Tried a timeout of 2secs and that doesn't help. 
  • bvdbosbvdbos Member
    @MarcoF : just to be sure : even with the ip 192.168.1.61:80 being not reachable so throwing the same kind of error but handling the error more gracefully?
  • jordenjorden Member
    I think Emile is trying to make sure we take care of error handling in the request. You can catch this error without a problem so your app doesn't crash. It's probably best to do that as soon as possible. 

    I also got some work to do in this case with my apps ;)
  • MarcoFMarcoF Member
    The strange thing is that when I run the standalone code directly in a node box, the TV instantly response on both on and of commands.
    I did some testing;
    1. external nodeJS file which send a ON signal to the Television
    2. external nodeJS file which send a OFF signal to the Television
    3. running a flow with a TV ON action card which sends a ON signal to the Television

    15:05:20; external nodeJS sends ON signal with OK result
    15:05:25; external nodeJS sends OFF signal with OK result
    15:05:30; run Homey flow sends OFF signal and App killed and TV not reacting
    15:05:35; external nodeJS sends ON signal with OK result

    This is the code of the external nodeJS file;
    "use strict";
    
    var request = require("request");
    //var sendcode = 'AAAAAQAAAAEAAAAVAw=='; //power
    var sendcode = 'AAAAAQAAAAEAAAAuAw=='; //power on
    //var sendcode = 'AAAAAQAAAAEAAAAvAw=='; //power off
    function SendXMLToTelevision(xml) {
        request.post({
            method: 'POST',
            uri: 'http://192.168.1.61/sony/IRCC',
            headers: {
                'SOAPACTION': '"urn:schemas-sony-com:service:IRCC:1#X_SendIRCC"',
                'X-Auth-PSK': '5555'
            },
            body: xml
        });
    } SendXMLToTelevision('<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:X_SendIRCC xmlns:u="urn:schemas-sony-com:service:IRCC:1"><IRCCCode>'+sendcode+'</IRCCCode></u:X_SendIRCC></s:Body></s:Envelope>');

    Here the "failing" app code:
    Homey.manager('flow').on('action.powerOn', function (callback, args) {
        for (var i = 0; i < remoteControllerCodes.length; i++) {
            if (remoteControllerCodes[i]['name'] == 'WakeUp') {
                var sendcode = remoteControllerCodes[i]['value'];
            }
        }
        sendCommand(sendcode, devices[args.device.id].settings.tvip, devices[args.device.id].settings.tvpsk);
    });
    function sendCommand(code, tvip, psk) {    
        var envelope = xmlEnvelope.replace("%code%", code);
        request.post({
            method: 'POST',
            timeout: 3000,
            uri: 'http://' + tvip + '/sony/IRCC',
            headers: {
                'SOAPACTION': '"urn:schemas-sony-com:service:IRCC:1#X_SendIRCC"',
                'X-Auth-PSK': psk
            },
            body: envelope
        });
    }

    The above code results in:

    Error: connect ECONNREFUSED 192.168.1.61:80
        at Object.exports._errnoException (util.js:870:11)
        at exports._exceptionWithHostPort (util.js:893:20)
        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1061:14)
    Error: socket hang up
        at createHangUpError (_http_client.js:200:15)
        at Socket.socketCloseListener (_http_client.js:232:23)
        at emitOne (events.js:82:20)
        at Socket.emit (events.js:169:7)
        at TCP._onclose (net.js:477:12)
    --- INFO: com.sony.androidtv has been killed ---

    So at all time;

    Homey and the TV are reachable.

    Homey app get killed and TV doesn't respond

    External nodeJS file works and TV does respond



    I have no idea whats going wrong and/or how to solve it.

  • jordenjorden Member
    At least try to catch the error:

    request.post({
            method: 'POST',
            headers: {
                'SOAPACTION': '"urn:schemas-sony-com:service:IRCC:1#X_SendIRCC"',
                'X-Auth-PSK': '5555'
            },
            body: xml
        }, function (error, response, body) {
    if (error) Homey.log('error: ' + error);
    }


    Next, could it be possible that the TV somehow links the 5555 pincode to the device that requests it? Meaning that you cannot send the wakeup command to the TV from Homey because the pincode you requested was generated based on your browsers REST client.
    (Just guessing here!)
  • MarcoFMarcoF Member
    edited May 2016
    The PSK is device independent and a great "open" solution to communicate with the TV. What I found on the internet, the PSK gives devices authentication and device independent control on the TV. The PSK is set in the TV and not requested from the TV. With that PSK every client/application/etc can control the TV.

    I had a callback for error "handling", but removed it while testing.
    Added your suggestion and this is the result;
    error: Error: connect ECONNREFUSED 192.168.1.61:80
    error: Error: connect ECONNREFUSED 192.168.1.61:80

    Also changed the PSK key for testing, so if it was device was linked to it, the fresh key would accept Homey, but that also didn't help.


  • jordenjorden Member
    edited May 2016
    That is kinda strange. Could it be that the TV only allows 1 device to connect and blocks all others? In other words, just allows 1 connection at a time.

    Would surprise me though.

    By the way, I also noticed yesterday that my TV didn't respond to WOL when it was off for a few hours. I couldn't ping the device either.
  • bvdbosbvdbos Member
    MarcoF said:
    The PSK is device independent and a great "open" solution to communicate with the TV. What I found on the internet, the PSK gives devices authentication and device independent control on the TV. The PSK is set in the TV and not requested from the TV. With that PSK every client/application/etc can control the TV.

    I had a callback for error "handling", but removed it while testing.
    Added your suggestion and this is the result;
    error: Error: connect ECONNREFUSED 192.168.1.61:80
    error: Error: connect ECONNREFUSED 192.168.1.61:80

    Also changed the PSK key for testing, so if it was device was linked to it, the fresh key would accept Homey, but that also didn't help.


    But the app doesn´t crash? That's one thing already...
    I'm no programmer so forgive me for dumb questions: There is a difference in body:xml and body:envelope isn't there? Also, did you enter the 5555 in your app instead of using a variable for it?
  • jordenjorden Member
    For testing purposes I guess ;) 
  • The connection issue aside, it the callback to the flow manager will never be called, so the flow will never finish. 
This discussion has been closed.