Jump to content
Guides for the Forum Read more... ×
Poradniki na Forum Read more... ×
FIBARO Home Center App 1.0.0 - release Read more... ×
FIBARO Home Center App 1.0.0 - wydanie Read more... ×
  • 0
samuelboerhoop

When to use "tcpSocket:disconnect();" ?

Question

When do I use the following code in lua:?

 

tcpSocket:disconnect();

 

i some sample code it has been added to the sample in others not.

 

example:

 

-- Volume 40dB --
tcpSocket:write("MV40".."\r")
fibaro:sleep(500);
result, err = tcpSocket:read();
tcpSocket:disconnect();
if (err == 0) then
  fibaro:log("transfer OK");
  fibaro:debug("transfer OK");
else
  fibaro:log("transfer failed");
  fibaro:debug("transfer failed");
end
 
------------------------------------
 
 
-- HERE its not been used --
 
tcpSocket:write(string.char(0x53, 0x49, 0x49, 0x52, 0x41, 0x44, 0x49, 0x4f, 0x0d));
fibaro:sleep(1000);
result, err = tcpSocket:read();
if (err == 0) then
  fibaro:log("Transfer OK: Internet Radio");
else
  fibaro:log("Transfer failed: extra check");
  -- check source: needs 2 times... otherwise still failed...
  tcpSocket:write(string.char(0x53, 0x49, 0x3f, 0x0d));
  fibaro:sleep(500);
  result, err = tcpSocket:read();
  tcpSocket:write(string.char(0x53, 0x49, 0x3f, 0x0d));
  fibaro:sleep(500);
  result, err = tcpSocket:read();
  if (err == 0) then
    if ( trim(string.sub(result, 1, string.len(result)-9)) == 'SIIRADIO' ) then
      fibaro:log("Transfer OK: Internet Radio");
    else
      fibaro:log("Transfer failed");
    end
  else
    fibaro:log("Transfer failed");
  end
end
 
 
 
 

Share this post


Link to post
Share on other sites

6 answers to this question

Recommended Posts

  • 1

Where can I find a reference manual/page about this TCPsocket? I've searched the web, but all I can find is examples of one type of application (to see if a device is present on the network or not). I would like to be able to see what all of its variables do. I noticed that the readTimeout does not work as expected; the socket takes ages (>20 seconds) if no device is available, i.e. write returns an error. I would like to shorten this time-out, but changing readTimeout doesn't do the trick.

Share this post


Link to post
Share on other sites
  • 1

Would be nice if the Fibaro adinistrator not only moves the subject, but also anwers the question about where to find documentaiton of TCPsocket

Share this post


Link to post
Share on other sites
  • 0

It is of course used to close the connection and dispose the socket. It is more of a good practice question. You should close things like that.

Share this post


Link to post
Share on other sites
  • 0
  • Inquirer
  • Ok If you look at the last example below where should i place the sentence: tcpSocket:disconnect(); ?

     

    After every "result, err = tcpSocket:read();" OR at the end of the last sentence of the code "result, err = tcpSocket:read();" ?

    Because in the example the "result, err = tcpSocket:read();" appears 3x times ?

     

    ----- Example ----

    tcpSocket:write(string.char(0x53, 0x49, 0x49, 0x52, 0x41, 0x44, 0x49, 0x4f, 0x0d));
    fibaro:sleep(1000);
    result, err = tcpSocket:read();
    if (err == 0) then
      fibaro:log("Transfer OK: Internet Radio");
    else
      fibaro:log("Transfer failed: extra check");
      -- check source: needs 2 times... otherwise still failed...
      tcpSocket:write(string.char(0x53, 0x49, 0x3f, 0x0d));
      fibaro:sleep(500);
      result, err = tcpSocket:read();
      tcpSocket:write(string.char(0x53, 0x49, 0x3f, 0x0d));
      fibaro:sleep(500);
      result, err = tcpSocket:read();
      if (err == 0) then
        if ( trim(string.sub(result, 1, string.len(result)-9)) == 'SIIRADIO' ) then
          fibaro:log("Transfer OK: Internet Radio");
        else
          fibaro:log("Transfer failed");
        end
      else
        fibaro:log("Transfer failed");
      end
    end

    Share this post


    Link to post
    Share on other sites
    • 0

    The topic has been moved from "

    Please login or register to see this link.

    " to "

    Please login or register to see this link.

    ".

     

    Temat został przeniesiony z "

    Please login or register to see this link.

    " do "

    Please login or register to see this link.

    ".

    Share this post


    Link to post
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    ×