--[[

  SVSi Decoder Q-Sys Plugin
  Author: Michael Goodyear
  Email: michael@locimation.com
  Version: 1.4
  
  Copyright 2020 Locimation Pty Ltd

  Permission is hereby granted, free of charge,
  to any person obtaining a copy of this software
  and associated documentation files (the "Software"),
  to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify,
  merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom
  the Software is furnished to do so, subject to the
  following conditions:

  The above copyright notice and this permission
  notice shall be included in all copies or substantial
  portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY
  OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
  LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
  EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.

]]

PluginInfo = {
  Name = "SVSi Decoder v1.4",
  Version = "1.4",
  Id = "9d3fa871-0c9e-4bf3-85bc-f70656f4b5c4",
  Description = "Update to match new firmware",
  ShowDebug = false
}

function GetColor()
  return {0,0,0}; -- black
end;

function GetProperties()
  return {};
end;

function GetControls(props)
  local controls = {

    {
      Name = "Video Mute",
      ControlType = "Button",
      ButtonType = "Toggle",
      UserPin = true,
      PinStyle = 'Both'
    },
    {
      Name = "Audio Mute",
      ControlType = "Button",
      ButtonType = "Toggle",
      UserPin = true,
      PinStyle = 'Both'
    },
    {
      Name = "Video Stream",
      ControlType = "Text",
      UserPin = true,
      PinStyle = 'Both'
    },
    {
      Name = "Audio Stream",
      ControlType = "Text",
      UserPin = true,
      PinStyle = 'Both'
    },
    {
      Name = "Audio Follow",
      ControlType = "Button",
      ButtonType = "Toggle",
      UserPin = true,
      PinStyle = 'Both'
    },
    {
      Name = "Left Gain",
      ControlType = "Knob",
      ControlUnit = "Integer",
      Min = 0,
      Max = 100,
      UserPin = true,
      PinStyle = 'Both'
    },
    {
      Name = "Right Gain",
      ControlType = "Knob",
      ControlUnit = "Integer",
      Min = 0,
      Max = 100,
      UserPin = true,
      PinStyle = 'Both'
    },
    {
      Name = "Local Playback",
      ControlType = "Button",
      ButtonType = "Toggle",
      UserPin = true,
      PinStyle = 'Both'
    },
    {
      Name = "Local Playlist",
      ControlType = "Text",
      UserPin = true,
      PinStyle = 'Both'
    },
    {
      Name = "Unit Type",
      ControlType = "Indicator",
      IndicatorType = "Text",
      UserPin = true,
      PinStyle = 'Output'
    },
    {
      Name = "Serial",
      ControlType = "Indicator",
      IndicatorType = "Text",
      UserPin = true,
      PinStyle = 'Output'
    },
    {
      Name = "Name",
      ControlType = "Indicator",
      IndicatorType = "Text",
      UserPin = true,
      PinStyle = 'Output'
    },
    {
      Name = "SW Version",
      ControlType = "Indicator",
      IndicatorType = "Text",
      UserPin = true,
      PinStyle = 'Output'
    },
    {
      Name = "Video Muted",
      ControlType = "Indicator",
      IndicatorType = "Led",
      UserPin = true,
      PinStyle = 'Output'
    },
    {
      Name = "Current Video Source",
      ControlType = "Indicator",
      IndicatorType = "Text",
      UserPin = true,
      PinStyle = 'Output'
    },
    {
      Name = "Current Audio Source",
      ControlType = "Indicator",
      IndicatorType = "Text",
      UserPin = true,
      PinStyle = 'Output'
    },
    {
      Name = "Display Connected",
      ControlType = "Indicator",
      IndicatorType = "Led",
      UserPin = true,
      PinStyle = 'Output'
    },
    {
      Name = "Current Resolution",
      ControlType = "Indicator",
      IndicatorType = "Text",
      UserPin = true,
      PinStyle = 'Output'
    },
    {
      Name = "Audio Muted",
      ControlType = "Indicator",
      IndicatorType = "Led",
      UserPin = true,
      PinStyle = 'Output'
    },
    {
      Name = "IP",
      ControlType = "Text",
      UserPin = true,
      PinStyle = 'Both'
    },
    {
      Name = "Status",
      ControlType = "Indicator",
      IndicatorType = "Status",
      UserPin = true,
      PinStyle = 'Output'
    },
    {
      Name = "IR Code",
      ControlType = "Indicator",
      IndicatorType = "Text",
      UserPin = true,
      PinStyle = 'Both'
    },
    {
      Name = "IR Send",
      ControlType = "Button",
      ButtonType = "Trigger",
      UserPin = true,
      PinStyle = 'Both'
    }

  }

  return controls;

end;

function GetControlLayout(props)

  local controls = {};

  -- tuck away IR
  controls['IR Code'] = {
    Position = {123,89},
    Size = {2,2},
    PrettyName = "Infrared~Code (Presto)"
  }
  controls['IR Send'] = {
    Position = {123,89},
    Size = {2,2},
    PrettyName = "Infrared~Send"
  }


  controls['Video Stream'] = {
    Position = {123,89},
    Size = {123,16},
    PrettyName = "Stream~Video"
  }
  controls['Audio Stream'] = {
    Position = {123,105},
    Size = {61,16},
    PrettyName = "Stream~Audio"
  }
  controls['Audio Follow'] = {
    Position = {184,105},
    Size = {61,16},
    Margin = 1,
    Legend = 'Follow',
    PrettyName = "Stream~Audio Follow Video"
  }

  controls['Local Playback'] = {
    Position = {123,155},
    Size = {123,16},
    Margin = 1,
    Legend = 'Enable',
    PrettyName = "LocalPlay~Enable"
  }
  controls['Local Playlist'] = {
    Position = {123,171},
    Size = {123,16},
    Style = 'ComboBox',
    PrettyName = "LocalPlay~Playlist"
  }

  controls['Current Video Source'] = {
    Position = {123,223},
    Size = {123,16},
    PrettyName = "Stream~Video Selected"
  }
  controls['Current Audio Source'] = {
    Position = {123,239},
    Size = {123,16},
    PrettyName = "Stream~Audio Selected"
  }

  controls['Video Mute'] = {
    Position = {397,88},
    Size = {107,16},
    Legend = 'Mute',
    Color = {255,0,0},
    PrettyName = "Mute~Mute Video"
  }
  controls['Audio Mute'] = {
    Position = {397,104},
    Size = {107,16},
    Legend = 'Mute',
    Color = {255,0,0},
    PrettyName = "Mute~Mute Audio"
  }
  controls['Video Muted'] = {
    Position = {504,88},
    Size = {16,16},
    Color = {255,0,0},
    PrettyName = "Mute~Video Muted"
  }
  controls['Audio Muted'] = {
    Position = {504,104},
    Size = {16,16},
    Color = {255,0,0},
    PrettyName = "Mute~Audio Muted"
  }

  controls['Left Gain'] = {
    Position = {397,154},
    Size = {123,16},
    Color = {153, 153,153},
    Style = 'Text',
    TextBoxStyle = 'Meter',
    PrettyName = "Gain~Left"
  }
  controls['Right Gain'] = {
    Position = {397,170},
    Size = {123,16},
    Color = {153, 153,153},
    Style = 'Text',
    TextBoxStyle = 'Meter',
    PrettyName = "Gain~Right"
  }

  controls['Display Connected'] = {
    Position = {397,222},
    Style = 'Button',
    Size = {123,16},
    Color = {255,255,255},
    Legend = 'Unknown',
    Margin = 2,
    PrettyName = "Display~Connected"
  }
  controls['Current Resolution'] = {
    Position = {397,238},
    Size = {123,16},
    PrettyName = "Display~Resolution"
  }

  controls['IP'] = {
    Position = {671,122},
    Size = {123,16},
    PrettyName = "Device~IP"
  }
  controls['Status'] = {
    Position = {671,138},
    Size = {123,16},
    PrettyName = "Device~Status"
  }

  controls['Unit Type'] = {
    Position = {671,188},
    Size = {123,16},
    PrettyName = "Device~Type"
  }
  controls['Serial'] = {
    Position = {671,204},
    Size = {123,16},
    PrettyName = "Device~Serial"
  }
  controls['Name'] = {
    Position = {671,220},
    Size = {123,16},
    PrettyName = "Device~Name"
  }
  controls['SW Version'] = {
    Position = {671,236},
    Size = {123,16},
    PrettyName = "Device~Software Version"
  }



  local graphics={



    {
      Type = 'GroupBox',
      Fill = {0,0,0},
      StrokeWidth = 0,
      Position = {0,0},
      Size = {844,299},
      CornerRadius = 0
    },

    {
      Type = 'GroupBox',
      Fill = {51,51,51},
      StrokeWidth = 1,
      StrokeColor = {0,0,0},
      Position = {21,56},
      Size = {255,219},
      CornerRadius = 8
    },
    {
      Type = 'GroupBox',
      Fill = {51,51,51},
      StrokeWidth = 1,
      StrokeColor = {0,0,0},
      Position = {294,56},
      Size = {255,219},
      CornerRadius = 8
    },
    {
      Type = 'GroupBox',
      Fill = {51,51,51},
      StrokeWidth = 1,
      StrokeColor = {0,0,0},
      Position = {568,88},
      Size = {255,187},
      CornerRadius = 8
    },

    {
      Type = 'Label',
      Text = 'SVSi Decoder',
      TextSize = 36,
      Position = {568,15},
      Size = {255,41},
      IsBold = true,
      HTextAlign = 'Right',
      Color = {255,255,255}
    },

    {
      Type = 'Label',
      Text = 'Input',
      TextSize = 12,
      Position = {34,37},
      Size = {242,19},
      IsBold = true,
      HTextAlign = 'Left',
      Color = {255,255,255}
    },
    {
      Type = 'Label',
      Text = 'Input',
      TextSize = 12,
      Position = {34,37},
      Size = {242,19},
      IsBold = true,
      HTextAlign = 'Left',
      Color = {255,255,255}
    },
    {
      Type = 'Label',
      Text = 'Output',
      TextSize = 12,
      Position = {307,37},
      Size = {242,19},
      IsBold = true,
      HTextAlign = 'Left',
      Color = {255,255,255}
    },
    {
      Type = 'Label',
      Text = 'Device',
      TextSize = 12,
      Position = {581,69},
      Size = {242,19},
      IsBold = true,
      HTextAlign = 'Left',
      Color = {255,255,255}
    },

    {
      Type = 'Label',
      Text = 'Video Stream',
      TextSize = 12,
      Position = {46,89},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },
    {
      Type = 'Label',
      Text = 'Audio Stream',
      TextSize = 12,
      Position = {46,105},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },
    {
      Type = 'Label',
      Text = 'Live / Local',
      TextSize = 12,
      Position = {46,155},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },
    {
      Type = 'Label',
      Text = 'Local Playlist',
      TextSize = 12,
      Position = {46,171},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },
    {
      Type = 'Label',
      Text = 'Video Source',
      TextSize = 12,
      Position = {46,223},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },
    {
      Type = 'Label',
      Text = 'Audio Source',
      TextSize = 12,
      Position = {46,239},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },

    {
      Type = 'Label',
      Text = 'Video Mute',
      TextSize = 12,
      Position = {320,88},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },
    {
      Type = 'Label',
      Text = 'Audio Mute',
      TextSize = 12,
      Position = {320,104},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },
    {
      Type = 'Label',
      Text = 'Left Gain',
      TextSize = 12,
      Position = {320,154},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },
    {
      Type = 'Label',
      Text = 'Right Gain',
      TextSize = 12,
      Position = {320,170},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },
    {
      Type = 'Label',
      Text = 'Display',
      TextSize = 12,
      Position = {320,222},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },
    {
      Type = 'Label',
      Text = 'Resolution',
      TextSize = 12,
      Position = {320,238},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },

    {
      Type = 'Label',
      Text = 'IP',
      TextSize = 12,
      Position = {594,122},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },
    {
      Type = 'Label',
      Text = 'Status',
      TextSize = 12,
      Position = {594,138},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },

    {
      Type = 'Label',
      Text = 'Unit Type',
      TextSize = 12,
      Position = {594,188},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },
    {
      Type = 'Label',
      Text = 'Serial',
      TextSize = 12,
      Position = {594,204},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },
    {
      Type = 'Label',
      Text = 'Name',
      TextSize = 12,
      Position = {594,220},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },
    {
      Type = 'Label',
      Text = 'SW Version',
      TextSize = 12,
      Position = {594,236},
      Size = {77,16},
      HTextAlign = 'Left',
      Color = {204,204,204}
    },

    {
      Type = 'Header',
      Text = 'Network',
      TextSize = 12,
      Position = {46,72},
      Size = {200,16},
      HTextAlign = 'Center',
      Color = {255,255,255}
    },
    {
      Type = 'Header',
      Text = 'Local',
      TextSize = 12,
      Position = {46,138},
      Size = {200,16},
      HTextAlign = 'Center',
      Color = {255,255,255}
    },
    {
      Type = 'Header',
      Text = 'Information',
      TextSize = 12,
      Position = {46,206},
      Size = {200,16},
      HTextAlign = 'Center',
      Color = {255,255,255}
    },

    {
      Type = 'Header',
      Text = 'Mutes',
      TextSize = 12,
      Position = {320,72},
      Size = {200,16},
      HTextAlign = 'Center',
      Color = {255,255,255}
    },
    {
      Type = 'Audio Gain',
      Text = 'Local',
      TextSize = 12,
      Position = {320,138},
      Size = {200,16},
      HTextAlign = 'Center',
      Color = {255,255,255}
    },
    {
      Type = 'Header',
      Text = 'Status',
      TextSize = 12,
      Position = {320,206},
      Size = {200,16},
      HTextAlign = 'Center',
      Color = {255,255,255}
    },

    {
      Type = 'Communication',
      Text = 'Local',
      TextSize = 12,
      Position = {594,105},
      Size = {200,16},
      HTextAlign = 'Center',
      Color = {255,255,255}
    },
    {
      Type = 'Header',
      Text = 'Information',
      TextSize = 12,
      Position = {594,171},
      Size = {200,16},
      HTextAlign = 'Center',
      Color = {255,255,255}
    },
    {
      Type = 'Image',
      Image = 'iVBORw0KGgoAAAANSUhEUgAAA4QAAAAoCAYAAACmeDQzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAG8WlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNi4wLWMwMDIgNzkuMTY0MzUyLCAyMDIwLzAxLzMwLTE1OjUwOjM4ICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTk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DAYDAaDlWX4hhBACBu+5W9i2rgUMBgMBoPBYDAAIYAQNrMsHwZQw6WAwWAwGAwGgwEIAYSwmWOuhZlhMBgMBoPBYDAAIYAQNkGmfpQcuoYUDAaDwWAwGAwGIAQQwsYYCDuAQRgMBoPBYDAYgHBCgBCCIAiCIAiCIAgCEEIQBEEQBEEQBEEAQgiCIAiCIAiCIMil/wPhbnawL98O3AAAAABJRU5ErkJggg==',
      Size = { 450, 20 },
      Position = { 422-225, 310 }
    }

  };

  return controls,graphics;

end;

if(not Controls) then return; end;

SVSiSocket = TcpSocket.New();

DECODER_STATUS = {};

function updateStatus(code, str)
  Controls['Status'].Value = code;
  Controls['Status'].String = str;
end;

SVSiSocket.ReadTimeout = 2
SVSiSocket.WriteTimeout = 0
SVSiSocket.ReconnectTimeout = 3


SVSiSocket.EventHandler = function(sock, evt)
  if(evt == TcpSocket.Events.Connected) then
    -- connected
  elseif(evt == TcpSocket.Events.Data) then
    local line = sock:ReadLine(TcpSocket.EOL.Any);
    while(line ~= nil) do
      handleFeedback(line);
      line = sock:ReadLine(TcpSocket.EOL.Any);
    end;
    updateStatus(0,'');
    updateDisplay();
    doDiff();
  elseif evt == TcpSocket.Events.Reconnect then
    updateStatus(5, 'Connecting');
  elseif evt == TcpSocket.Events.Closed then
    updateStatus(2, 'Closed by remote');
  elseif evt == TcpSocket.Events.Error then
    updateStatus(2, 'Socket error');
  elseif evt == TcpSocket.Events.Timeout then
    updateStatus(2, 'Timeout');
  end

end;

PollTimer = Timer.New();
PollTimer.EventHandler = function()
  if(not SVSiSocket.IsConnected) then return; end;
  SVSiSocket:Write('?;\r\n');
end;
PollTimer:Start(0.1);

function handleFeedback(line)
  -- print('> ' .. line);
  local prop,val = line:match('(.*):(.*)');
  DECODER_STATUS[prop] = val;
end;

function updateDisplay()  
  Controls['Video Muted'].Boolean = DECODER_STATUS['DVIOFF']~='off';
  Controls['Audio Muted'].Boolean = DECODER_STATUS['MUTE']~='0';
  
  local displayConnected = DECODER_STATUS['DVISTATUS']~='disconnected';
  Controls['Display Connected'].Legend = displayConnected and 'Connected' or 'Disconnected';
  Controls['Display Connected'].Color = displayConnected and 'green' or 'darkred';
  Controls['Display Connected'].Boolean = displayConnected;
  
  Controls['Current Resolution'].String = DECODER_STATUS['INPUTRES'] or DECODER_STATUS['MODE'] or '';
  
  Controls['Unit Type'].String = DECODER_STATUS['SVSI_RXGEN2']:sub(1,5);
  Controls['Serial'].String = DECODER_STATUS['SVSI_RXGEN2'];
  Controls['Name'].String = DECODER_STATUS['NAME'] and DECODER_STATUS['NAME'] or '';
  Controls['SW Version'].String = DECODER_STATUS['SWVER'];
  
  local isLocalMode = DECODER_STATUS['PLAYMODE']~='live';
  local videoSource =
    isLocalMode and
      'Local Playlist ' .. DECODER_STATUS['PLAYLIST']
    or
      'Network Stream ' .. DECODER_STATUS['STREAM'];
  Controls['Current Video Source'].String = videoSource;
  local audioFollowsVideo = DECODER_STATUS['STREAMAUDIO']=='0';
  Controls['Current Audio Source'].String =
    audioFollowsVideo and videoSource
    or (isLocalMode and 'Local Playlist ' .. DECODER_STATUS['PLAYLIST']
      or 'Network Stream ' .. DECODER_STATUS['STREAMAUDIO']);
  
end;

--[[ Control Handlers ]]
function toggleAudioOption()
  Controls['Audio Stream'].IsDisabled = Controls['Audio Follow'].Boolean;
end;
Controls['Audio Follow'].EventHandler = toggleAudioOption;
toggleAudioOption();

Controls['Local Playlist'].Choices = { '1','2','3','4','5','6','7','8' };

function doDiff()
  
  -- Video stream
  local desiredVideoStream = Controls['Video Stream'].String;
  if(desiredVideoStream~='' and tonumber(desiredVideoStream)~=nil and tonumber(DECODER_STATUS['STREAM'])~=nil) then
    if(tonumber(DECODER_STATUS['STREAM']) ~= tonumber(desiredVideoStream)) then
      SVSiSocket:Write('set:' .. desiredVideoStream .. ';\r\n');
      return;
    end;
  end;
  
  -- Audio stream + follow
  local desiredAudioStream = Controls['Audio Follow'].Boolean and '0' or Controls['Audio Stream'].String;
  if(desiredAudioStream~='' and tonumber(desiredAudioStream)~=nil and tonumber(DECODER_STATUS['STREAMAUDIO'])~=nil) then
    if(tonumber(DECODER_STATUS['STREAMAUDIO']) ~= tonumber(desiredAudioStream)) then
      SVSiSocket:Write('seta:' .. desiredAudioStream .. ';\r\n');
      return;
    end;
  end;    
  
  -- Local playback
  local isLocalMode = DECODER_STATUS['PLAYMODE']~='live';
  if(isLocalMode ~= Controls['Local Playback'].Boolean or (isLocalMode and Controls['Local Playlist'].String ~= DECODER_STATUS['PLAYLIST'])) then
    if(Controls['Local Playback'].Boolean) then
      SVSiSocket:Write('local:' .. Controls['Local Playlist'].String .. ';\r\n');
    else
      SVSiSocket:Write('live;\r\n');
    end;
    return;
  end;
  
  -- Video Mute
  if(Controls['Video Mute'].Boolean ~= (DECODER_STATUS['DVIOFF']~='off')) then
    if(Controls['Video Mute'].Boolean) then
      SVSiSocket:Write('dviOff;\r\n');
    else
      SVSiSocket:Write('dviOn;\r\n');
    end;
    return;
  end;
  
  -- Audio Mute
  if(Controls['Audio Mute'].Boolean ~= (DECODER_STATUS['MUTE']~='0')) then
    if(Controls['Audio Mute'].Boolean) then
      SVSiSocket:Write('mute;\r\n');
    else
      SVSiSocket:Write('unmute;\r\n');
    end;
    return;
  end;
  
  -- Left Gain
  if(DECODER_STATUS['LINEOUTVOL_L']~=Controls['Left Gain'].String) then
    SVSiSocket:Write('lovolleft:'..(Controls['Left Gain'].String)..';\r\n');
    return;
  end;
  
  -- Right Gain
  if(DECODER_STATUS['LINEOUTVOL_R']~=Controls['Right Gain'].String) then
    SVSiSocket:Write('lovolright:'..(Controls['Right Gain'].String)..';\r\n');
    return;
  end;
  
end;

-- IR functionality
Controls['IR Send'].EventHandler = function()
  if(not SVSiSocket.IsConnected) then return; end;
  local code = Controls['IR Code'].String:gsub("[^0-9A-Fa-f]", " "):gsub(" +", " ");
  SVSiSocket:Write('sendirraw: '..code..';\r\n');
end;

function updateIP()
  if(SVSiSocket.IsConnected) then SVSiSocket:Disconnect(); end;
  SVSiSocket:Connect(Controls['IP'].String, 50002);
  updateStatus(5, 'Connecting');
end;
updateIP();
Controls['IP'].EventHandler = updateIP;