PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE DeviceList (
DeviceSerial INTEGER NOT NULL DEFAULT 0,
DeviceLabel TEXT NOT NULL DEFAULT "Not Specified",
DeviceStatus INTEGER NOT NULL DEFAULT 0,
DeviceOpState INTEGER NOT NULL DEFAULT 0,
DeviceType INTEGER NOT NULL DEFAULT 0,
DiscreteInCount INTEGER NOT NULL DEFAULT 0,
DiscreteOutCount INTEGER NOT NULL DEFAULT 0,
AnalogInCount INTEGER NOT NULL DEFAULT 0,
AnalogOutCount INTEGER NOT NULL DEFAULT 0,
Version INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (DeviceSerial)
);
CREATE TABLE PortList (
PortID INTEGER NOT NULL DEFAULT 0,
DeviceSerial INTEGER NOT NULL DEFAULT 0,
PortType INTEGER NOT NULL DEFAULT 0,
PortLabel TEXT NOT NULL DEFAULT "Not Specified",
PortStatus INTEGER NOT NULL DEFAULT 0,
PortLastToggled INTEGER NOT NULL DEFAULT 0,
PortOpState INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (DeviceSerial) REFERENCES DeviceList (DeviceSerial) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, 
PRIMARY KEY (PortID, DeviceSerial, PortType)
);
CREATE TABLE PortListDiscreteIn (
PortID INTEGER NOT NULL DEFAULT 0,
DeviceSerial INTEGER NOT NULL DEFAULT 0,
PortType INTEGER NOT NULL DEFAULT 0,
DefaultValue INTEGER NOT NULL DEFAULT 0,
CurrentValue INTEGER NOT NULL DEFAULT 0,
InputType INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (PortID, DeviceSerial, PortType) REFERENCES PortList (PortID, DeviceSerial, PortType) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, 
PRIMARY KEY (PortID, DeviceSerial)
);
CREATE TABLE PortListDiscreteOut (
PortID INTEGER NOT NULL DEFAULT 0,
DeviceSerial INTEGER NOT NULL DEFAULT 0,
PortType INTEGER NOT NULL DEFAULT 0,
DefaultOutputStatus INTEGER NOT NULL DEFAULT 0,
OutputStatus INTEGER NOT NULL DEFAULT 0,
OutputType INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (PortID, DeviceSerial, PortType) REFERENCES PortList (PortID, DeviceSerial, PortType) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, 
PRIMARY KEY (PortID, DeviceSerial)
);
CREATE TABLE PortListAnalogIn (
PortID INTEGER NOT NULL DEFAULT 0,
DeviceSerial INTEGER NOT NULL DEFAULT 0,
PortType INTEGER NOT NULL DEFAULT 0,
SampledValue FLOAT DEFAULT NULL,
Offset FLOAT DEFAULT NULL,
Gain FLOAT DEFAULT NULL,
CalculatedValue FLOAT DEFAULT NULL,
LowWarnValue FLOAT DEFAULT NULL,
HighWarnValue FLOAT DEFAULT NULL,
LowErrorValue FLOAT DEFAULT NULL,
HighErrorValue FLOAT DEFAULT NULL,
TimeOfLastReport INTEGER NOT NULL DEFAULT 0,
LastReportedValue FLOAT DEFAULT NULL,
SampleAccumulator FLOAT DEFAULT NULL,
AcceptableNoise FLOAT DEFAULT NULL,
NoiseThreshold FLOAT DEFAULT NULL,
SampleRate INTEGER NOT NULL DEFAULT 0,
ReportRate INTEGER NOT NULL DEFAULT 0,
DisplayUnit TEXT NOT NULL DEFAULT "Not Specified",
AnalogInputType INTEGER NOT NULL DEFAULT 0,
LastSampleSeq INTEGER NOT NULL DEFAULT 0,
SampleCounter INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (PortID, DeviceSerial, PortType) REFERENCES PortList (PortID, DeviceSerial, PortType) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, 
PRIMARY KEY (PortID, DeviceSerial)
);
CREATE TABLE PortListAnalogOut (
DisplayUnit TEXT NOT NULL DEFAULT "Not Specified",
HighErrorValue FLOAT DEFAULT NULL,
LowErrorValue FLOAT DEFAULT NULL,
HighWarnValue FLOAT DEFAULT NULL,
LowWarnValue FLOAT DEFAULT NULL,
OutputValue FLOAT DEFAULT NULL,
Gain FLOAT DEFAULT NULL,
Offset FLOAT DEFAULT NULL,
MinimumValue FLOAT DEFAULT NULL,
MaximumValue FLOAT DEFAULT NULL,
SampledValue FLOAT DEFAULT NULL,
PortType INTEGER NOT NULL DEFAULT 0,
DeviceSerial INTEGER NOT NULL DEFAULT 0,
PortID INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (PortID, DeviceSerial, PortType) REFERENCES PortList (PortID, DeviceSerial, PortType) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, 
PRIMARY KEY (DeviceSerial, PortID)
);
CREATE TABLE EventList (
SeqNum INTEGER NOT NULL DEFAULT 0,
EventType INTEGER NOT NULL DEFAULT 0,
TimeStamp INTEGER NOT NULL DEFAULT 0,
DeviceSerial INTEGER NOT NULL DEFAULT 0,
PortID INTEGER NOT NULL DEFAULT 0,
Status INTEGER NOT NULL DEFAULT 0,
OpState INTEGER NOT NULL DEFAULT 0,
ObjectType INTEGER NOT NULL DEFAULT 0,
DerivedObjectType INTEGER NOT NULL DEFAULT 0,
EventOp INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (SeqNum)
);
CREATE TABLE TrendList (
SeqNum INTEGER NOT NULL DEFAULT 0,
TimeStamp INTEGER NOT NULL DEFAULT 0,
DeviceSerial INTEGER NOT NULL DEFAULT 0,
PortID INTEGER NOT NULL DEFAULT 0,
ObjectType INTEGER NOT NULL DEFAULT 0,
TrendValue FLOAT DEFAULT NULL,
PRIMARY KEY (SeqNum)
);
CREATE TABLE Capabilities (
Value TEXT NOT NULL DEFAULT "Not Specified",
Label TEXT NOT NULL DEFAULT "Not Specified",
Num INTEGER NOT NULL DEFAULT 0,
DeviceSerial INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (DeviceSerial) REFERENCES DeviceList (DeviceSerial) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, 
PRIMARY KEY (Num, DeviceSerial)
);
CREATE TABLE AdhocEventList (
Value TEXT NOT NULL DEFAULT "Not Specified",
Label TEXT NOT NULL DEFAULT "Not Specified",
PortNum INTEGER NOT NULL DEFAULT 0,
DeviceSerial INTEGER NOT NULL DEFAULT 0,
TimeStamp INTEGER NOT NULL DEFAULT 0,
SeqNum INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (SeqNum)
);
CREATE TABLE "SiteConfig" (
	`ConfigNum`	INTEGER NOT NULL DEFAULT 0,
	`SiteID`	TEXT NOT NULL DEFAULT "Not Specified",
	`DeviceStatus`	INTEGER NOT NULL DEFAULT 0,
	`SiteOpState`	INTEGER NOT NULL DEFAULT 0,
	`CurrentSeq`	INTEGER NOT NULL DEFAULT 0,
	`CurrentTrendSeq`	INTEGER NOT NULL DEFAULT 0,
	`LastSeq`	INTEGER NOT NULL DEFAULT 0,
	`LastTrendSeq`	INTEGER NOT NULL DEFAULT 0,
	`SiteLabel`	TEXT NOT NULL DEFAULT "Not Specified",
	`NMSAddress`	TEXT NOT NULL DEFAULT "Not Specified",
	`Version`	INTEGER NOT NULL DEFAULT 0,
	`CurrentAdhocSeq`	INTEGER NOT NULL DEFAULT 0,
	`LastAdhocSeq`	INTEGER NOT NULL DEFAULT 0,
	`SVPAddress`	TEXT,
	`SVPPort`	INTEGER,
	PRIMARY KEY(ConfigNum)
);
INSERT INTO "SiteConfig" VALUES(1,'',0,0,0,0,0,0,'Not Specified','Not Specified',6,0,0,NULL,NULL);
COMMIT;
