% modified from MatlabNetComClient example script (MatlabNetComClientExample.m)
% see MatlabNetComClientExample.m to check more details for Matlab-Cheetah connection


%Connects to a server, streams some data into plots, then exits.
%
%Note: If you have any questions about any of the functions in the
%MatlabNetComClient package, you can type help <function_name> at the
%Matlab command prompt.
%
%Load NetCom into Matlab, and connect to the NetCom server
%If you are running Matlab on a PC other than the one with the NetCom
%server, you will need to change the server name to the name of the server
%PC.
vt_acq_ent='VT1';
serverName = '129.116.157.231'; %the server we are going to connect with to get streaming data
% serverName = '192.168.0.1'; %the server we are going to connect with to get streaming data
%Replace this IP with the IP of the computer that is running Cheetah
% vtXResolution = 720; %number of pixels of horizontal resolution for the video tracker
% vtYResolution = 480; %number of pixels of vertical resolution for the video tracker


succeeded = NlxConnectToServer(serverName);
if succeeded ~= 1
    disp(sprintf('FAILED connect to %s. Exiting script.', serverName));
    return;
else
    disp(sprintf('Connect successful.'));
end

serverIP = NlxGetServerIPAddress();
disp(sprintf('Connected to IP address: %s', serverIP));

serverPCName = NlxGetServerPCName();
disp(sprintf('Connected to PC named: %s', serverPCName));

serverApplicationName = NlxGetServerApplicationName();
disp(sprintf('Connected to the NetCom server application: %s', serverApplicationName));

%Identify this program to the server we're connected to.
succeeded = NlxSetApplicationName('Tracking rats in Matlab');
% if succeeded ~= 1
%     disp 'FAILED set the application name'
% else
%     disp 'PASSED set the application name'
% end


%open up a stream for VT1
succeeded = NlxOpenStream(vt_acq_ent);
if succeeded == 1
    disp(sprintf('PASSED to open stream for %s', char(vt_acq_ent)));
elseif succeeded == 0
    disp(sprintf('FAILED to open stream for %s', char(vt_acq_ent)));
end

%This loop is run to get new data from NetCom.  All open streams must be
%servised regularly, or there will be dropped records.
numberOfPasses = 2;
for pass = 1:numberOfPasses
    objectToRetrieve = char(vt_acq_ent);
    %determine the type of acquisition entity we are currently indexed
    %to and call the appropriate function for that type
    
    [succeeded,  timeStampArray, extractedLocationArray, extractedAngleArray, numRecordsReturned, numRecordsDropped ] = NlxGetNewVTData(objectToRetrieve);
    if succeeded == 0
        disp(sprintf('FAILED to get new data for VT stream %s on pass %d', objectToRetrieve, pass));
        break;
    else
        disp(sprintf('Retrieved %d VT records for %s with %d dropped.', numRecordsReturned, objectToRetrieve, numRecordsDropped));
%         plot(extractedLocationArray);
    end
    pause(1)
end