-- phpMyAdmin SQL Dump
-- version 4.7.0
-- https://www.phpmyadmin.net/
--
-- Generation Time: Sep 18, 2017 at 10:05 PM
-- Server version: 5.5.57-0+deb8u1-log
-- PHP Version: 5.6.30

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `homecontrol`
--

-- --------------------------------------------------------

--
-- Table structure for table `commands`
--

CREATE TABLE `commands` (
  `deviceId` int(50) NOT NULL,
  `id` int(11) NOT NULL,
  `name` varchar(50) NOT NULL,
  `carrierType` int(11) NOT NULL,
  `signalName` varchar(50) NOT NULL,
  `commandType` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `deviceGroups`
--

CREATE TABLE `deviceGroups` (
  `id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `deviceGroupsMembers`
--

CREATE TABLE `deviceGroupsMembers` (
  `deviceGroupId` int(11) NOT NULL,
  `deviceId` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `devices`
--

CREATE TABLE `devices` (
  `roomid` int(11) NOT NULL,
  `id` int(11) NOT NULL,
  `name` varchar(50) NOT NULL,
  `housecode` varchar(5) NOT NULL,
  `unitcode` varchar(1) NOT NULL,
  `state` tinyint(2) NOT NULL,
  `protectedStatus` tinyint(2) NOT NULL DEFAULT '0',
  `carrierCapabilities` varchar(50) NOT NULL COMMENT 'carrierTypeRadio433 = 0, carrierTypeInfrared = 1, carrierTypeOrviboS20 = 2, carrierTypeAnelPowerline = 3',
  `macAddress` varchar(50) DEFAULT NULL,
  `ipAddress` varchar(200) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `environment`
--

CREATE TABLE `environment` (
  `sensorid` int(11) NOT NULL,
  `timestamp` bigint(11) NOT NULL,
  `temperature` double DEFAULT NULL,
  `humidity` double DEFAULT NULL,
  `brightness` double DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `nodes`
--

CREATE TABLE `nodes` (
  `id` int(11) NOT NULL,
  `name` varchar(20) NOT NULL,
  `networkNameOrAddress` varchar(50) NOT NULL,
  `relayPort` int(11) NOT NULL,
  `roomid` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `rooms`
--

CREATE TABLE `rooms` (
  `id` int(11) NOT NULL,
  `name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `lightLevel` int(11) DEFAULT NULL,
  `lightLevelLastUpdate` bigint(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

-- --------------------------------------------------------

--
-- Table structure for table `rule-actions`
--

CREATE TABLE `rule-actions` (
  `ruleId` int(11) NOT NULL,
  `executionOrder` int(11) NOT NULL,
  `objectType` enum('device','deviceGroup','command','switchRoom','switchAll','temperature','blinds','wait') NOT NULL,
  `object` varchar(50) DEFAULT NULL,
  `value` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `rule-triggers`
--

CREATE TABLE `rule-triggers` (
  `ruleId` int(11) NOT NULL,
  `objectType` enum('brightness','humidity','temperature','users','motion') NOT NULL,
  `object` varchar(250) NOT NULL,
  `direction` enum('eq','lt','ht','le','he','ne') NOT NULL,
  `value` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `rules`
--

CREATE TABLE `rules` (
  `id` int(11) NOT NULL,
  `name` varchar(150) NOT NULL,
  `activated` tinyint(2) NOT NULL DEFAULT '1',
  `comment` varchar(200) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `schedules`
--

CREATE TABLE `schedules` (
  `id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL,
  `objectType` enum('device','deviceGroup','command') NOT NULL,
  `objectToUse` int(11) NOT NULL,
  `desiredState` tinyint(2) NOT NULL,
  `schedulingType` enum('once','reoccuring') NOT NULL,
  `timestamp` int(11) NOT NULL,
  `days` varchar(11) NOT NULL,
  `hour` int(11) NOT NULL,
  `minute` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `sensors`
--

CREATE TABLE `sensors` (
  `id` int(11) NOT NULL,
  `name` varchar(200) NOT NULL,
  `active` tinyint(2) NOT NULL DEFAULT '1',
  `roomid` int(11) NOT NULL,
  `responsiblenode` int(11) NOT NULL,
  `sensortype` int(11) NOT NULL COMMENT '1 = motion, 2 = temperature/humidity, 3 = brightness',
  `modelString` varchar(20) DEFAULT NULL COMMENT 'dht11 or dht22 for temperature and humidity, otherwise null',
  `frequency` int(11) DEFAULT NULL,
  `pinOrAddress` varchar(50) NOT NULL,
  `direction` varchar(10) DEFAULT NULL,
  `lastActivity` bigint(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `settings`
--

CREATE TABLE `settings` (
  `settingName` varchar(50) NOT NULL,
  `settingValue` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `username` varchar(20) NOT NULL,
  `password` varchar(200) NOT NULL,
  `emailAddress` varchar(80) NOT NULL,
  `useridentifier` varchar(50) NOT NULL,
  `home` tinyint(2) NOT NULL DEFAULT '0',
  `lastUpdate` bigint(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `commands`
--
ALTER TABLE `commands`
  ADD PRIMARY KEY (`id`),
  ADD KEY `deviceId` (`deviceId`);

--
-- Indexes for table `deviceGroups`
--
ALTER TABLE `deviceGroups`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `deviceGroupsMembers`
--
ALTER TABLE `deviceGroupsMembers`
  ADD PRIMARY KEY (`deviceGroupId`,`deviceId`),
  ADD KEY `deviceId` (`deviceId`);

--
-- Indexes for table `devices`
--
ALTER TABLE `devices`
  ADD PRIMARY KEY (`id`),
  ADD KEY `roomid` (`roomid`);

--
-- Indexes for table `environment`
--
ALTER TABLE `environment`
  ADD PRIMARY KEY (`sensorid`,`timestamp`);

--
-- Indexes for table `nodes`
--
ALTER TABLE `nodes`
  ADD PRIMARY KEY (`id`),
  ADD KEY `roomid` (`roomid`);

--
-- Indexes for table `rooms`
--
ALTER TABLE `rooms`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `name` (`name`);

--
-- Indexes for table `rule-actions`
--
ALTER TABLE `rule-actions`
  ADD PRIMARY KEY (`ruleId`,`executionOrder`);

--
-- Indexes for table `rule-triggers`
--
ALTER TABLE `rule-triggers`
  ADD PRIMARY KEY (`ruleId`,`objectType`,`object`);

--
-- Indexes for table `rules`
--
ALTER TABLE `rules`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `schedules`
--
ALTER TABLE `schedules`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `sensors`
--
ALTER TABLE `sensors`
  ADD PRIMARY KEY (`id`),
  ADD KEY `responsiblenode` (`responsiblenode`),
  ADD KEY `roomid` (`roomid`);

--
-- Indexes for table `settings`
--
ALTER TABLE `settings`
  ADD PRIMARY KEY (`settingName`);

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `useridentifier` (`useridentifier`),
  ADD UNIQUE KEY `username` (`username`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `commands`
--
ALTER TABLE `commands`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;
--
-- AUTO_INCREMENT for table `deviceGroups`
--
ALTER TABLE `deviceGroups`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
--
-- AUTO_INCREMENT for table `devices`
--
ALTER TABLE `devices`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=33;
--
-- AUTO_INCREMENT for table `nodes`
--
ALTER TABLE `nodes`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
--
-- AUTO_INCREMENT for table `rooms`
--
ALTER TABLE `rooms`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;
--
-- AUTO_INCREMENT for table `rules`
--
ALTER TABLE `rules`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
--
-- AUTO_INCREMENT for table `schedules`
--
ALTER TABLE `schedules`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `sensors`
--
ALTER TABLE `sensors`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
--
-- Constraints for dumped tables
--

--
-- Constraints for table `commands`
--
ALTER TABLE `commands`
  ADD CONSTRAINT `commands_ibfk_2` FOREIGN KEY (`deviceId`) REFERENCES `devices` (`id`);

--
-- Constraints for table `deviceGroupsMembers`
--
ALTER TABLE `deviceGroupsMembers`
  ADD CONSTRAINT `deviceGroupsMembers_ibfk_1` FOREIGN KEY (`deviceGroupId`) REFERENCES `deviceGroups` (`id`),
  ADD CONSTRAINT `deviceGroupsMembers_ibfk_2` FOREIGN KEY (`deviceId`) REFERENCES `devices` (`id`);

--
-- Constraints for table `devices`
--
ALTER TABLE `devices`
  ADD CONSTRAINT `devices_ibfk_1` FOREIGN KEY (`roomid`) REFERENCES `rooms` (`id`);

--
-- Constraints for table `environment`
--
ALTER TABLE `environment`
  ADD CONSTRAINT `environment_ibfk_1` FOREIGN KEY (`sensorid`) REFERENCES `sensors` (`id`);

--
-- Constraints for table `nodes`
--
ALTER TABLE `nodes`
  ADD CONSTRAINT `nodes_ibfk_1` FOREIGN KEY (`roomid`) REFERENCES `rooms` (`id`);

--
-- Constraints for table `rule-actions`
--
ALTER TABLE `rule-actions`
  ADD CONSTRAINT `rulefk` FOREIGN KEY (`ruleId`) REFERENCES `rules` (`id`);

--
-- Constraints for table `rule-triggers`
--
ALTER TABLE `rule-triggers`
  ADD CONSTRAINT `ruleidfk` FOREIGN KEY (`ruleId`) REFERENCES `rules` (`id`);

--
-- Constraints for table `sensors`
--
ALTER TABLE `sensors`
  ADD CONSTRAINT `sensors_ibfk_1` FOREIGN KEY (`roomid`) REFERENCES `rooms` (`id`),
  ADD CONSTRAINT `sensors_ibfk_2` FOREIGN KEY (`responsiblenode`) REFERENCES `nodes` (`id`);
COMMIT;


INSERT INTO `settings` (`settingName`, `settingValue`) VALUES
('authenticationMode', 'ldap'),
('databaseVersion', '2'),
('dateFormat', 'dd.MM.yyyy HH:mm:ss:SSSS'),
('ldapBasePath', 'CN=Users,DC=some,DC=domain,DC=name'),
('ldapCacheRefresh', '60'),
('ldapDomain', 'SOMEDOMAIN'),
('ldapHosts', 'dc1.some.domain.name:636;dc2.dc1.some.domain.name:636'),
('ldapMailAttribute', 'mail'),
('ldapMemberGroup', 'CN=HomeControl,CN=Users,DC=some,DC=domain,DC=name'),
('ldapUserAttribute', 'samAccountName'),
('ldapUseSsl', 'true'),
('logLevel', '5'),
('logMaxSize', '5'),
('maximumLoggedInUsers', '20'),
('notificationAuthenticate', 'false'),
('notificationEncryptionType', 'NONE'),
('notificationFromAddress', 'administrator@some.domain.name'),
('notificationMailServer', 'mailgate.some.domain.name'),
('notificationMailServerPort', '25'),
('notificationPassword', ''),
('notificationsEnabled', '0'),
('notificationToAddress', 'administrator@some.domain.name'),
('notificationUsername', ''),
('sensorUdpWatchPort', '36739'),
('serverRelay', 'true'),
('serverRelayAutoDisconnectTime', '300000'),
('serverRelayClientTimeout', '5000'),
('serverRelayPsk', 'somekey'),
('serverRelayUseSsl', 'true'),
('switchRoomDelay', '0'),
('temperatureUnit', 'C');


/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
