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.