﻿DROP PROCEDURE IF EXISTS `scada`.`InitServer`;

CREATE DEFINER=`root`@`localhost` PROCEDURE `InitServer`(IN pTYPE int)
BEGIN
	IF pTYPE<>1 THEN
	 SELECT M.DRIVERID,DRIVERNAME,SERVER,TIMEOUT,R.AssemblyName,R.ClassFullName,Spare1,Spare2 FROM META_DRIVER M INNER JOIN RegisterModule R ON M.DRIVERTYPE=R.DriverID;
	END IF;
	SELECT COUNT(*) FROM META_TAG;
	SELECT TAGID,GROUPID,RTRIM(TAGNAME),ADDRESS,DATATYPE,DATASIZE,ARCHIVE,MAXIMUM,MINIMUM,CYCLE FROM META_TAG WHERE ISACTIVE=1;
	IF pTYPE<>1 THEN
		SELECT DRIVERID,GROUPNAME,GROUPID,UPDATERATE,DEADBAND,ISACTIVE FROM META_GROUP ;
	END IF;
	IF pTYPE=0 THEN 
		SELECT SOURCE FROM META_Condition WHERE EVENTTYPE=2;
	END IF;
	IF pTYPE<>2 THEN 
		SELECT TYPEID,SOURCE,ALARMTYPE,A.ISENABLED,CONDITIONTYPE,PARA,IFNULL(COMMENT,''),DEADBAND,DELAY,SUBALARMTYPE,Threshold,SEVERITY,
		IFNULL(MESSAGE,''),B.ISENABLE FROM META_Condition a LEFT OUTER JOIN META_SUBCONDITION b ON a.TypeID=b.ConditionID WHERE EVENTTYPE<>2;
	END IF;
	-- LEFT OUTER JOIN META_TAG c ON a.SOURCEID=c.TAGID 
	SELECT SCALEID,SCALETYPE,EUHI,EULO,RAWHI,RAWLO FROM META_SCALE;
END