Mini Kabibi Habibi

Current Path : C:/Program Files/Smart Professional Surveillance System/PC-NVR/Common/Lua/
Upload File :
Current File : C:/Program Files/Smart Professional Surveillance System/PC-NVR/Common/Lua/PTZCtrl.lua

--   "$Id: init.lua 2416 2006-05-09 14:43:40Z yuansy $"
--   (c) Copyright 1992-2005, ZheJiang Dahua Information Technology Stock CO.LTD.
--                            All Rights Reserved
--
--	�� �� ��� PTZCtrl.lua
--	��    �:  ��̨���ƽű�
--	�޸ļ�¼�� 2006-5-22 ����� <wang_hengwen@dahuatech.com> ��Ԭʿ�´���Ļ�����������
--   
local AllPTZProtocol = {}; 		-- ������кźͶ�Ӧ���ļ��
local SelectedPTZ = {}; 		-- ���潫Ҫ�����Э��
local CamAddr = nil; 			-- ����Э�����̨��ַ
local MonAddr = nil; 			-- ������ӵ�ַ
local MatrixAddr = nil;		-- ��������ַ

local PTZCtrl = {};
PTZCtrl.PathSet = {};

local Utils = require("utils");   -- ����Utils

-- ������е���̨����Э��
local function buildPtzList(PathSet)
	local PTZProtocols = {};

	-- ���ڼ��ص�������̨����Э���ļ�
	local function loadPtzFile(filename)
		local f,err = loadfile(filename);
		if f then
			local ret, protocol;
			ret, protocol = pcall(f);
			if protocol.Attr == nil then
			   err = filename
			else
				if( ret ) then
					PTZProtocols[protocol.Attr.Name] = protocol;
				else 
					err = protocol;
				end
		    end
		end
		
		if err then
			print(
				string.format("Error while loading PTZ protocol:%s",err)
			);
		end;
	end

	-- ���ڼ���ָ��Ŀ¼�µ��ļ�
	local function LoadPtzProtocol(ptzPath)
		local ret, iter = pcall(lfs.dir, ptzPath);
		if ret then
			for filename in iter do
				if string.find(filename, ".lua") then
					loadPtzFile(ptzPath .. '/' .. filename);
				end;
			end;
		end;
	end	
	
	-- ����·�������µ�����ļ�
	for _, path in pairs(PathSet) do 
		LoadPtzProtocol(path);
	end

	-- ������̨����Э�����ƽ�������
	local t1 = {};
	for k,_ in pairs(PTZProtocols) do 
		table.insert(t1, k);
	end
	
	table.sort(t1);
	
	-- �Ѱ���ĸ�������̨����Э��ŵ�AllPTZProtocol����ӡЭ���嵥
	
	local ptzList = '';
	for k, v in pairs(t1) do 
		AllPTZProtocol[k] = PTZProtocols[v];
		if(ptzList ~= '') then
			ptzList = ptzList .. ',';
		end
		ptzList = ptzList .. v ;
	end
	print(string.format("The following PTZ protocols have been loaded:\n\t%s", ptzList));
	
	-- �����ܵ���̨����Э���
	PTZCtrl.ProtocolCount = table.getn(AllPTZProtocol);
end


--[[
local function printstr(str)
		-- ��ӡ����
	local printstr = "";
	for i = 1, string.len(str) do
		printstr = printstr .. string.format("0x%02X ",string.byte(str,i));
	end;
	print(printstr);
end;

local function printtable(tab)
	local printtab = "";
	for i = 1, table.getn(tab) do
		printtab = printtab .. string.format("0x%02x ",tab[i]);
	end;
	print(printtab);
end;
--]]

-- ��̨֧�ֵ�ȫ�����
local SupportedCommand = 
{
		--��׼���
		"Direction", "Zoom", "Focus", "Iris",
		
		--��չ���	
		-- ��ת
		"AlarmSearch",
		
		-- �ƹ�
		"Light",
		
		-- Ԥ�õ��������ã�����ת��)
		"SetPreset", "ClearPreset", "GoToPreset",
		
		-- ˮƽ�Զ�
		"AutoPanOn", "AutoPanOff",
		 
		-- �Զ�ɨ�裬��Ԥ�����õı߽��м�ת��
		"SetLimit","AutoScanOn","AutoScanOff",		
			
		-- �Զ�Ѳ����һ��ָ��Ԥ�õ�֮��Ѳ��
		"AddTour", "DeleteTour", "StartTour", "StopTour", "ClearTour",
			
		-- �켣Ѳ��, һ��ָģʽ(���ÿ�ʼ�����ý�������У�ֹͣ����ģʽ
		"SetPattern", "StartPattern", "StopPattern", "ClearPattern",
		
		-- ���ٶ�λ����
		"Position",	
		
		-- �������
		"Aux",
			
		-- �˵���ز��
		"Menu", "MenuExit", "MenuEnter", "MenuEsc", "MenuUpDown", "MenuLeftRight",		
		
		-- �����л�
		"MatrixSwitch",	
		
		-- ��ͷ��ת����̨��λ
		"Flip", "Reset",

}

local PTZStandard =
{

    "TileUp",
	"TileDown",
	"PanLeft",
	"PanRight",
	"ZoomWide",
	"ZoomTele",
	"FocusFar",
	"FocusNear",
	"IrisLarge",
	"IrisSmall",  	 
}


local PTZOperateCommand =
{
	"LeftUp", "TileUp", "RightUp", "PanLeft", "PanRight", "LeftDown", "TileDown", "RightDown",
	"ZoomWide", "ZoomTele", "FocusFar", "FocusNear", "IrisLarge", "IrisSmall", "AlarmSearch", "LightOn", "LightOff",
	"SetPreset", "ClearPreset", "GoToPreset", "AutoPanOn", "AutoPanOff",
	"SetLeftLimit", "SetRightLimit", "AutoScanOn", "AutoScanOff", 
	"AddTour", "DeleteTour", "StartTour", "StopTour", "ClearTour",
	"SetPatternStart", "SetPatternStop", "StartPattern", "StopPattern", "ClearPattern",
	"Position",
	"AuxOn", "AuxOff",
	"Menu", "MenuExit", "MenuEnter", "MenuEsc", "MenuUp", "MenuDown", "MenuLeft", "MenuRight",
	"MatrixSwitch",
	"Flip", "Reset",
	"MATRIX_SWITCH","LIGHT_CONTROLLER","SETPRESETNAME","ALARMPTZ", 
	"STANDARD",
}

-- ȡ��SupportedCommand���ñ�
local RevCommand = {};
for i,v in ipairs(SupportedCommand) do
	RevCommand[v] = i;
end; 

local RevOperateCommand ={};
for i,v in ipairs(PTZOperateCommand) do
	RevOperateCommand[v] = i;
end; 

-- �õ�֧�ֵ�Э���
local function GetProtocolNum()
	return table.getn(AllPTZProtocol);
end;

--[[
�õ�ָ��Э�������
param:
	index:Э����������±�1��ʼ
--]]
local function GetProtocolAttr(index)
	local tmpPTZ = {};
	local Attr = {}; 
	if (index > 0) and (index <= table.getn(AllPTZProtocol)) then
		tmpPTZ = AllPTZProtocol[index];
		Attr = tmpPTZ.Attr;
	end;
	
	--[[ �������C����ݽṹȡֵʱ�ã��������������
	local RetSeq = {"HighMask", "LowMask", "Name",  "CamAddrMin", "CamAddrMax", 
		"MonAddrMin", "MonAddrMax", 	"PresetMin", "PresetMax", "TourMin", "TourMax", "PatternMin", "PatternMax",
		"TileSpeedMin", "TileSpeedMax", "PanSpeedMin", "PanSpeedMax",
		"AuxMin","AuxMax", "Internal", "Type", "AlarmLen"};		
	--]]
		
	-- ˳�򲻸���
	local ptztype = {"PTZ","MATRIX"};
	local revtype ={};
	for k, v in pairs(ptztype) do
		revtype[v] = k;
	end;
	
	local RetAttr = {};	

	RetAttr["Name"] 			= string.sub(Attr.Name, 1, 15);
	RetAttr["Type"] 			= revtype[Attr.Type];
	RetAttr["Internal"]			= Attr.Internal;
	RetAttr["CamAddrMin"] 		= Attr.CamAddrRange[1];
	RetAttr["CamAddrMax"] 		= Attr.CamAddrRange[2];
	RetAttr["MonAddrMin"] 		= Attr.MonAddrRange[1];
	RetAttr["MonAddrMax"] 		= Attr.MonAddrRange[2];
	RetAttr["PresetMin"]	 	= Attr.PresetRange[1];
	RetAttr["PresetMax"]	 	= Attr.PresetRange[2];
	RetAttr["TourMin"] 			= Attr.TourRange[1];
	RetAttr["TourMax"]			= Attr.TourRange[2];
	RetAttr["PatternMin"]		= Attr.PatternRange[1];
	RetAttr["PatternMax"]		= Attr.PatternRange[2];
	RetAttr["TileSpeedMin"]		= Attr.TileSpeedRange[1];
	RetAttr["TileSpeedMax"]		= Attr.TileSpeedRange[2];
	RetAttr["PanSpeedMin"] 		= Attr.PanSpeedRange[1];
	RetAttr["PanSpeedMax"] 		= Attr.PanSpeedRange[2];
	RetAttr["AuxMin"] 			= Attr.AuxRange[1];
	RetAttr["AuxMax"] 			= Attr.AuxRange[2];
	RetAttr["AlarmLen"]     = Attr.AlarmLen or 0;

	-- ���������ͨ��̨������룬����ǰ��4���DZ�׼��һ��֧��
	local highmask = 0;
	local lowmask = 0xf;
	local hexbit = 0x8;
	local operatemask = lowmask;
	for i = 5, table.getn(SupportedCommand) do	
		hexbit = hexbit * 2;
		if i == 33 then 
			hexbit = 1;	
			operatemask = 0;		
		end;
		local tmpTable = tmpPTZ.Command.Start;
		if i == RevCommand["Light"] then
			if tmpTable["LightOn"] or tmpTable["LightOff"] then
				operatemask = operatemask + hexbit;
			end;
		elseif i == RevCommand["SetLimit"] then
			if tmpTable["SetLeftLimit"] or tmpTable["SetRightLimit"] then
				operatemask = operatemask + hexbit;
			end;
		elseif i == RevCommand["SetPattern"] then
			if tmpTable["SetPatternStart"] or tmpTable["SetPatternStop"] then
				operatemask = operatemask + hexbit;
			end;
		elseif i == RevCommand["Aux"] then
			if tmpTable["AuxOn"] or tmpTable["AuxOff"] then
				operatemask = operatemask + hexbit;
			end;
		elseif i == RevCommand["MenuUpDown"] then
			if tmpTable["MenuUp"] or tmpTable["MenuDown"] then
				operatemask = operatemask + hexbit;
			end;	
		elseif i == RevCommand["MenuLeftRight"] then
			if tmpTable["MenuLeft"]	or tmpTable["MenuRight"] then
				operatemask = operatemask + hexbit;
			end;	
		elseif i == RevCommand["Flip"] then
			if  tmpTable["Flip"] then
				operatemask = operatemask + hexbit;
			end;
		elseif i == RevCommand["Reset"] then
			if tmpTable["Reset"] then
				operatemask = operatemask + hexbit;
			end;
		else
				if tmpTable[SupportedCommand[i]] then
					operatemask = operatemask + hexbit;
				end;
			end;
		if i <= 32 then
			lowmask = operatemask;
			--print(string.format("for lowmask = %x", lowmask));
		else
			highmask = operatemask;
			--print("highmask = ", highmask);
		end;
	end
	--print(string.format("supported Operate %x",operatemask));	
	RetAttr["HighMask"] 					= highmask;
	RetAttr["LowMask"]						= lowmask;
	-- print(string.format("lowmask = %x", lowmask));
	-- print(string.format("lowmask = %x", highmask));
	
	
	--�˴����֧�ֵ�ֱ�۵ĸ�����������,��ʱ֧��64��������
		local Auxmask0 = 0;--�������ĸ�λ����
		local Auxmask1 = 0;--�������ĵ�λ����
		local hexbit = 0x8;
		local opermask = 0;
		local auxtable = tmpPTZ.AuxCommand;
		
		if auxtable ~= nil then
			local len = table.getn(auxtable);
			for i = 1 , len do
				hexbit = hexbit * 2;
				if i == 33 then 
					hexbit = 1;	
					opermask = 0;		
				end;	
				opermask = opermask + 2^(auxtable[i] - 1);
				if i <= 32 then
					Auxmask1 = opermask;
				else
					Auxmask0 = opermask;
				end;
			end;
		end;
	RetAttr["HighAuxMask"] = Auxmask0;
	RetAttr["LowAuxMask"] = Auxmask1;
		
	return RetAttr;
end;

--[[
�����ַ��Ϣ
��ִ�������ַ����û�еĻ�����ͨ�÷�ʽ����
--]]
local function CamAddrProcess(opttable, addr)
	if not opttable then
		print("opttable is nill");
	end;
	-- �ȳ������⴦��
	if SelectedPTZ.CamAddrProcess then
		return SelectedPTZ.CamAddrProcess(opttable, addr);
	else
		-- ��ʼͨ������
		local addr = math.mod(addr,256);
		opttable[SelectedPTZ.CommandAttr.AddrPos] = addr;
		return opttable;
	end;
end;

--[[
����������ַ�������⴦��Ŀǰ��û��ͨ�ð취
--]]
local function MonAddrProcess(opttable,addr)
	if not opttable then
		print("opttable is nil");
	end;
	if SelectedPTZ.MonAddrProcess then
		return SelectedPTZ.MonAddrProcess(opttable, addr);
	else
		return opttable;
	end;
end;

--[[
��������ַ�������⴦��Ŀǰ��û��ͨ�ð취
--]]
local function MatrixAddrProcess(opttable, addr)
	if not opttable then
		print("opttable is nil");
	end;

	if SelectedPTZ.MatrixAddrProcess then
		return SelectedPTZ.MatrixAddrProcess(opttable, addr);
	else
		return opttable;
	end;
end;

--[[
����Э����Ϣ������Ӧ��Э������
param:
	index��ָ���ĸ�Э�飬���±�1��ʼ
	camaddr:	���õ���̨��ַ����,ֱ����16����ֵ
	monaddr:	���õļ������ַ
	matrixaddr: ���õľ����ַ
--]]
local function SetProtocol(index, camaddr, monaddr, matrixaddr)
	-- �����
	if (index <= 0) or (index > table.getn(AllPTZProtocol)) or not camaddr then
		print("the Procotol isn't exist or the Camera's addr isn't exist");
		SelectedPTZ = nil;
		return;
	end;
	
	-- �õ������	
	SelectedPTZ = AllPTZProtocol[index];

	CamAddr = math.abs(camaddr);
	if monaddr then
		MonAddr = math.abs(monaddr);
	end;
	
	if matrixaddr then
		MatrixAddr = math.abs(matrixaddr);
	end;

end;

--[[
���������ֵ
--]]
local function GetCMDTable(cmd)
	local RetTable = {};
	--print(cmd);
	if type(cmd) == "string" then
		RetTable = Utils.str2table(cmd);
	elseif type(cmd) == "table" then
		RetTable = cmd;
	else
		return nil;
	end;
	
	-- ������̨��ַ��Ϣ
	RetTable = CamAddrProcess(RetTable, CamAddr);

	-- ����������ַ
	if MonAddr then
		RetTable = MonAddrProcess(RetTable, MonAddr);
	end;
	
	-- ��������ַ��Ϣ
	if MatrixAddr then
		RetTable = MatrixAddrProcess(RetTable, MatrixAddr);
	end;

	return RetTable;
	
end;

--[[
�����ٶ�,�����⴦���ʹ�����⴦��û�еĻ�ʹ��ͨ�ô���
arg1: ��ֱ�����ٶ�
arg2: ˮƽ�����ٶ�
--]]
local function SpeedProcess(opttable, arg1, arg2)
	if not opttable then
		print("opttable is nil");
		return nil;
	end;
	local res = SelectedPTZ.SpeedProcess;
	if res then
		return SelectedPTZ.SpeedProcess(opttable, arg1, arg2);
	else
		opttable[SelectedPTZ.CommandAttr.TileSpeedPos] = math.abs(arg1);
		opttable[SelectedPTZ.CommandAttr.PanSpeedPos] = math.abs(arg2);
		return opttable;
	end;
end;
--[[
�������Ŀǰ֧�ֵIJ��࣬������⴦��
--]]
local function MultipleProcess(opttable, multiple)
	if not opttable then
		print("opttable is nil");
		return nil;
	end;
	if SelectedPTZ.MultipleProcess then
		return SelectedPTZ.MultipleProcess(opttable, multiple);
	else
		return opttable;
	end;
end;


--[[
����Ԥ�õ㣬�����⴦���ʹ�����⴦��û�еĻ�ʹ��ͨ�ô���
param
	arg2:��ʱ����
--]]
local function PresetProcess(opttable, arg1)
	if not opttable then
		print("opttable is nil");
		return nil;
	end;
	local res = SelectedPTZ.PresetProcess;
	if res then
		return SelectedPTZ.PresetProcess(opttable, arg1);
	else
		opttable[SelectedPTZ.CommandAttr.PresetPos] = math.abs(arg1);
		return opttable;
	end;
end;

local function SetTourProcess(opttable, tour, preset)
	if not opttable then
		print("opttable is nil");
		return nil;
	end;
	if SelectedPTZ.SetTourProcess then
		return SelectedPTZ.SetTourProcess(opttable, tour, preset);
	else
		return opttable;
	end;
end;
--[[
�����Զ�Ѳ��·��
--]]
local function TourProcess(opttable, tour)
	if not opttable then
		print("opttable is nill");
		return nil;
	end;
	if SelectedPTZ.TourProcess then
		return SelectedPTZ.TourProcess(opttable, tour);
	else
		return opttable;
	end;
end;

--[[
����켣
--]]
local function PatternProcess(opttable, num)
	if not opttable then
		print("opttable is nil");
		return nil;
	end;
	if SelectedPTZ.PatternProcess then
		return SelectedPTZ.PatternProcess(opttable, num);
	else
		return opttable;
	end;
end;

--[[
������ٶ�λ
--]]
local function PositionProcess(opttable, hor, ver, zoom)
	if not opttable then
		print("opttable is nil");
		return nil;
	end;
	if SelectedPTZ.PositionProcess then
		return SelectedPTZ.PositionProcess(opttable, hor, ver, zoom);
	else
		return opttable;
	end;
end;
 
--[[
������ش���
--]]
local function AuxProcess(opttable, num)
	if not opttable then
		print("opttable is nil");
		return nil;
	end;
	if SelectedPTZ.AuxProcess then
		return SelectedPTZ.AuxProcess(opttable, num);
	else
		opttable[SelectedPTZ.CommandAttr.AuxPos] = num;
		return opttable;
	end;
end;

--[[
���������
arg1: �������ַ
arg2: ��̨��ַ
--]]
local function SwitchProcess(opttable, MonAddr, CamAddr)
	local rettable = {};
	rettable = MonAddrProcess(opttable, MonAddr);
	rettable = CamAddrProcess(rettable, CamAddr);
	return rettable;
end;

--[[
��ѯ���
arg:��ѯ���
searchtype:��ѯ����
--]]
local function SearchProcess(opttable, arg, searchtype)
	if not opttable then
		print("opttable is nil");
		return nil;
	end;
	
	if SelectedPTZ.SearchProcess then
		return SelectedPTZ.SearchProcess(opttable, arg);
	else
		return opttable;
	end;
end;


--[[
�ϲ�����
table1:���������ı�
table2:һ����
--]]
local function MergeTables(table1, table2)
	for i = 1, table.getn(table2) do
	    table.insert(table1, table2[i]);
	end;
end;


--[[
Э���´�����
 param:
	opttable:�������
	cmd:	ָ��������±�
	arg1:
	arg2:���ĵ�
	arg3:���������Ŀǰֻ����PTZStandard���е����������Ч
--]]
local function StandardProcess(opttable,arg1,arg2,arg3)
	if not opttable then
	   print("opttable is nil");
	   return nil;
	end;
	if SelectedPTZ.StandardProcess then
		return SelectedPTZ.StandardProcess(opttable,arg1,arg2,arg3);
	else
		for i,v in ipairs(PTZStandard) do
		  	if(bits.band(arg3,bits.lshift(1, i - 1))) == (bits.lshift(1, i - 1)) then
		  		local a = SelectedPTZ.CommandAttr[v].bytePos;
		  		local b = SelectedPTZ.CommandAttr[v].bitPos;
		  		opttable[a] = bits.bxor(opttable[a],bits.lshift(1, b));
		  		
		  		if(v == "TileUp") or (v == "TileDown") then
		  			local c = SelectedPTZ.CommandAttr.TileSpeedPos;
		  			opttable[c] = math.abs(arg1);
	            end;
	            
		  		if(v == "PanLeft") or (v == "PanRight") then
		  			local c = SelectedPTZ.CommandAttr.PanSpeedPos;
		  			opttable[c] = math.abs(arg1);
		  		end;
		  		
		    end;
		  
	    end;	
	    return opttable;   
     
    end;
end;


--[[
�����������ҳ���Ӧ�����������ò��
param:
	OpeTable:�������
	cmd:	ָ��������±�
	arg1:
	arg2:���ĵ�


--]]
local function Parse(opttable, cmd, arg1, arg2, arg3)
    PTZCommand = GetCMDTable(opttable[PTZOperateCommand[cmd]]);
    
    if not PTZCommand then
    	return nil;
    end
    
	if cmd >= RevOperateCommand["LeftUp"] and cmd <= RevOperateCommand["RightDown"] then
		if cmd == RevOperateCommand["TileUp"] or cmd == RevOperateCommand["TileDown"] then
			PTZCommand = SpeedProcess(PTZCommand, arg1, 0);
		elseif cmd == RevOperateCommand["PanLeft"] or cmd == RevOperateCommand["PanRight"] then
			PTZCommand = SpeedProcess(PTZCommand, 0, arg1);
		else
			PTZCommand = SpeedProcess(PTZCommand, arg1, arg2);
		end;
	-- �����
	elseif cmd >= RevOperateCommand["ZoomWide"] and cmd <= RevOperateCommand["IrisSmall"] then
			PTZCommand = MultipleProcess(PTZCommand, arg1);
	-- �������ã�����ת�Ԥ�õ�
	elseif cmd >= RevOperateCommand["SetPreset"] and cmd <= RevOperateCommand["GoToPreset"] then
		PTZCommand = PresetProcess(PTZCommand, arg1);
	-- �������Ԥ�õ㵽Ѳ������
	elseif cmd == RevOperateCommand["AddTour"] or cmd == RevOperateCommand["DeleteTour"] then
		PTZCommand = SetTourProcess(PTZCommand, arg1, arg2);
	elseif cmd == RevOperateCommand["StartTour"] or cmd == RevOperateCommand["ClearTour"] then
		PTZCommand = TourProcess(PTZCommand, arg1);
	-- ��������ģʽ
	elseif cmd >= RevOperateCommand["SetPatternStart"] and cmd <= RevOperateCommand["ClearPattern"] then
		PTZCommand = PatternProcess(PTZCommand, arg1);
	-- ������ٶ�λ
	elseif cmd == RevOperateCommand["Position"] then
		PTZCommand = PositionProcess(PTZCommand, arg1, arg2, arg3);
	-- ���������
	elseif cmd == RevOperateCommand["AuxOn"] or cmd == RevOperateCommand["AuxOff"] then
		PTZCommand = AuxProcess(PTZCommand, arg1);
	-- ��������л� 
	elseif cmd == RevOperateCommand["MatrixSwitch"] then
		PTZCommand = SwitchProcess(PTZCommand, arg1, arg2);
	elseif cmd == RevOperateCommand["AlarmSearch"] then
		PTZCommand = SearchProcess(PTZCommand, arg1, arg2);
	elseif cmd == RevOperateCommand["STANDARD"] then
		if(opttable == SelectedPTZ.Command.Start) then
		PTZCommand = StandardProcess(PTZCommand, arg1, arg2, arg3);
		end;
	end;
	
	if PTZCommand then
		if SelectedPTZ.SpecialProcess then
			local cmd = SelectedPTZ.SpecialProcess(PTZCommand, arg1, arg2, arg3);
			if  cmd then
				return cmd;
			end;
		end;
			return SelectedPTZ.Checksum(PTZCommand);		
	end;
end;

--[[
�����������ҳ���Ӧ�����������ò��
param:
	OpeTable:�������
	cmd:	ָ��������±�
	arg1:
	arg2:���ĵ�
	
˵���lenΪ���ȱ�����������ij���
--]]
local function AnalyseCommand(opttable, cmd, arg1, arg2, arg3)
	local PTZCommand = nil;
	local allCMD = {};
	local lenTable = {};
	if (cmd <= 0) or (cmd > table.getn(PTZOperateCommand)) then
		print("out of command\n");
		return nil;
	end;

	--print(PTZOperateCommand[cmd]);
		if not opttable[PTZOperateCommand[cmd]] then
			if (cmd ~= RevOperateCommand["STANDARD"])  then
		  		return nil;
		  	else                --�������������STANDARD����Э���ֲ�֧��STANDARD��ʱ��
		  		local CMD = {}; --�����е��������±�ı�
		  		if (arg3 == 5) then
		  		   CMD["LeftUp"] = RevOperateCommand["LeftUp"];
		  		elseif (arg3 == 6) then
		  		   CMD["LeftDown"] = RevOperateCommand["LeftDown"];
		  		elseif (arg3 == 9) then
		  		   CMD["RightUp"] = RevOperateCommand["RightUp"];
		  		elseif (arg3 == 10) then
		  		   CMD["RightDown"] = RevOperateCommand["RightDown"]
		  		else
		  			for i,v in ipairs(PTZStandard) do
		  			    if((bits.band(arg3,bits.lshift(1,i - 1))) == (bits.lshift(1, i - 1))) then
		  				CMD[v] = RevOperateCommand[v];
		  				end;
		  			end;
		  		end;
                for k in pairs(CMD) do --����CMDȡ����е��õ��������ϲ���һ����
                	PTZCommand = Parse(opttable, CMD[k], arg1, arg2, arg3);
                	if not PTZCommand then
                		return nil;
                	end
                    table.insert(lenTable ,(table.getn(PTZCommand)));
		            MergeTables(allCMD,PTZCommand);
		        end;
		        PTZCommand = allCMD; 
		    end;    
	   	else
	   		PTZCommand = Parse(opttable, cmd, arg1, arg2, arg3);
	   		if not PTZCommand then
				return nil;
			end
	   		table.insert(lenTable ,(table.getn(PTZCommand)));
	   	end;
	   	return 	PTZCommand,lenTable ;
		end;

--[[
��̨���ָ��
param:
	cmd:��̨���,SupportedCommand���±�
	arg1:���1
	arg2:���2������ľ��庬���ĵ�
 arg3:���������Ŀǰֻ����PTZStandard���е����������Ч
--]]
local function StartPTZ(cmd, arg1, arg2, arg3)
	local PTZCommand,lenTable  = AnalyseCommand(SelectedPTZ.Command.Start, cmd, arg1, arg2, arg3);
	if PTZCommand then
		--printtable(PTZCommand);
		return lenTable,table.getn(lenTable),PTZCommand,table.getn(PTZCommand);
	end;

end;

local function StopPTZ(cmd, arg1, arg2, arg3)  
                                      
	local PTZCommand,lenTable = AnalyseCommand(SelectedPTZ.Command.Stop, cmd, arg1, arg2, arg3);
	if PTZCommand then
		--printtable(PTZCommand);
		return  lenTable,table.getn(lenTable),PTZCommand,table.getn(PTZCommand);
	end;
end;

local function test()
print("Protocol Num = " .. GetProtocolNum());
for i = 1, GetProtocolNum() do
	local attr = GetProtocolAttr(i);
	SetProtocol(i,1);

	for j=1, table.getn(SupportedCommand) do
		if bits.band(attr.LowMask, bits.lshift(1, j-1)) == bits.lshift(1,j-1) then
		StartPTZ(j, 31, 0, 1);
	end;
--	StopPTZ(j, -63,63,1);
	end;
end;
end;

local function LoadProtocols()
	buildPtzList(PTZCtrl.PathSet);
end

PTZCtrl.LoadProtocols   = LoadProtocols;
PTZCtrl.GetProtocolNum  = GetProtocolNum;
PTZCtrl.GetProtocolAttr = GetProtocolAttr;
PTZCtrl.SetProtocol     = SetProtocol;
PTZCtrl.StartPTZ        = StartPTZ;
PTZCtrl.StopPTZ         = StopPTZ;
PTZCtrl.PTZProtocol     = AllPTZProtocol;
PTZCtrl.buildPtzList    = buildPtzList;

return PTZCtrl;

--
-- "$Id: init.lua 2416 2006-05-09 14:43:40Z yuansy $"
--