more G-Labs products

Author Topic: send Light status via tcpclient to 3rd party tcpserver  (Read 2039 times)

January 24, 2015, 09:29:39 PM
Read 2039 times

kobi.beja

  • *
  • Information
  • Newbie
  • Posts: 21
Hi all
I'm looking for help, To send via tcpclient the real status of light/sensor, For example "lights:02:on", "lights:03:0ff" to 3rd party server.
Right now i know how to open the tcpclient and send only one light status.
hope to some help.
Kobi

January 25, 2015, 10:04:19 AM
Reply #1

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Code: [Select]
var remoteServer = "192.168.123.45";
var remotePort = 12345;

Action<string> HandleStringReceived = (string message) => {
  // this will be called every time a message is received from the tcp channel
  Program.Notify("Server String", message);
};

Action<byte[]> HandleMessageReceived = (byte[] message) => {
  // this will be called every time a message is received from the tcp channel
  Program.Notify("Server Bytes", BitConverter.ToString(message));
};

Action<bool> HandleStatusChanged = (bool connected) => {
  // this will be called whenever the tcp connection status has changed
  Program.Notify("TCP Client", "Connected = " + connected);
};

Program.Notify("TCP Client", "Starting");
// open the TCPIP port channel and register handlers

//TcpClient.EndOfLine = "\r\n"; // default is "\n"
TcpClient
  .OnStringReceived( HandleStringReceived )
  .OnMessageReceived( HandleMessageReceived )
  .OnStatusChanged( HandleStatusChanged )
  .Service( remoteServer )
  .Connect( remotePort );

Program.Notify("TCP Client", "Started");

When.ModuleParameterChanged((module, parameter) => {
 
  if (module.IsOfDeviceType("Light,Dimmer") && parameter.Is("Status.Level"))
  if (TcpClient.IsConnected)
  {
    var message = String.Format("{0}:{1}:{2}", module.Instance.Domain, module.Instance.Address, parameter.Value);
    TcpClient.SendMessage(message);
  }
  else
  {
    Program.Notify("TCP client disconnected", "Unable to deliver message.");
  }
 
  return true;
});

Program.GoBackground();

g.
« Last Edit: January 25, 2015, 10:07:22 AM by Gene »

January 25, 2015, 10:51:59 AM
Reply #2

kobi.beja

  • *
  • Information
  • Newbie
  • Posts: 21
Hi Gene,

Thanks a lot! it's working perfect. ;D ;D
in this case it is connect only to one server, But if I want to make the HG to the server and one or more clients will be able to speak with him it is possible?
Thx
Kobi.

January 25, 2015, 10:59:21 AM
Reply #3

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
in this case it is connect only to one server, But if I want to make the HG to the server and one or more clients will be able to speak with him it is possible?

please explain better. Do you need HG to connect to more than just one server?

g.

January 25, 2015, 11:25:52 AM
Reply #4

kobi.beja

  • *
  • Information
  • Newbie
  • Posts: 21
Ok,

The HG is the TCPClient and he speak only with one TCPServer 192.xxx.xxx.xxx,
It's possible to make the HG to the TCPServer and other TCP clients speak with him?
Otherwise if it's not possible to open on HG TCPServer there is any way to convert the TCPClient to Serial Port via USB?

Thanks.
Kobi

January 26, 2015, 11:08:45 AM
Reply #5

kobi.beja

  • *
  • Information
  • Newbie
  • Posts: 21
Hi Gene,

In the same code that you sent to me, There is possibility to add alarm system and send ARMED or DISARMED status?

Thanks
Kobi

January 26, 2015, 11:36:01 AM
Reply #6

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
change the line
Code: [Select]
if (module.IsOfDeviceType("Light,Dimmer") && parameter.Is("Status.Level"))
with
Code: [Select]
if (module.Is("Security Alarm System") || (module.IsOfDeviceType("Light,Dimmer") && parameter.Is("Status.Level")))

g.

January 26, 2015, 11:51:53 AM
Reply #7

kobi.beja

  • *
  • Information
  • Newbie
  • Posts: 21