-
Notifications
You must be signed in to change notification settings - Fork 2
/
remote_control_database_history.sql
293 lines (261 loc) · 9 KB
/
remote_control_database_history.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
CREATE TABLE weather_data(
weather_data_id INT AUTO_INCREMENT PRIMARY KEY,
location_id INT NULL,
recorded DATETIME,
temperature DECIMAL(6,3) NULL,
pressure DECIMAL(9,4) NULL,
humidity DECIMAL(6,3) NULL,
wind_direction INT NULL,
precipitation INT NULL,
wind_speed DECIMAL(8,3) NULL,
wind_increment INT NULL,
gas_metric DECIMAL(15,4) NULL,
sensor_id INT NULL,
device_feature_id INT NULL
);
CREATE TABLE reboot_log(
reboot_log_id INT AUTO_INCREMENT PRIMARY KEY,
location_id INT,
recorded DATETIME
);
CREATE TABLE location(
location_id INT AUTO_INCREMENT PRIMARY KEY,
device_type_feature_id INT NULL,
name VARCHAR(100) NULL,
description VARCHAR(2000) NULL,
device_id INT NULL,
i2c_address INT NULL,
connecting_device_id INT NULL,
tenant_id INT NULL,
created DATETIME
);
CREATE TABLE device_type(
device_type_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NULL,
description VARCHAR(2000) NULL,
architecture VARCHAR(100) NULL,
power_voltage DECIMAL(9,3) NULL,
tenant_id INT NULL,
created DATETIME
);
CREATE TABLE device(
device_id INT AUTO_INCREMENT PRIMARY KEY,
device_type_id INT,
name VARCHAR(100) NULL,
description VARCHAR(2000) NULL,
tenant_id INT NULL,
sensor_id INT NULL,
latitude DECIMAL(8,5) NULL,
longitude DECIMAL(8,5) NULL,
created DATETIME
);
CREATE TABLE feature_type(
feature_type_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NULL,
description VARCHAR(2000) NULL,
tenant_id INT NULL,
created DATETIME
);
CREATE TABLE device_type_feature(
device_type_feature_id INT AUTO_INCREMENT PRIMARY KEY,
feature_type_id INT,
can_be_input TINYINT,
can_be_output TINYINT,
can_be_analog TINYINT,
pin_number INT,
via_i2c_address INT,
sensor_type INT DEFAULT NULL,
sensor_sub_type INT DEFAULT NULL,
power_pin INT NULL,
name VARCHAR(100) NULL,
description VARCHAR(2000) NULL,
tenant_id INT NULL,
created DATETIME
);
CREATE TABLE device_feature(
device_feature_id INT AUTO_INCREMENT PRIMARY KEY,
device_type_feature_id INT,
device_type_id INT,
value INT DEFAULT NULL,
name VARCHAR(100) NULL,
description VARCHAR(2000) NULL,
enabled TINYINT DEFAULT 0,
tenant_id INT NULL,
created DATETIME,
modified DATETIME,
last_known_device_value INT NULL,
last_known_device_modified DATETIME,
allow_automatic_management TINYINT DEFAULT 1
restore_automation_after INT NULL,
automation_disabled_when DATETIME NULL,
user_id INT NULL
);
CREATE TABLE device_feature_management_rule(
device_feature_id INT,
management_rule_id INT,
tenant_id INT,
management_priority INT DEFAULT 1,
created DATETIME
)
CREATE TABLE device_feature_log(
device_feature_log_id INT AUTO_INCREMENT PRIMARY KEY,
device_feature_id INT,
tenant_id INT,
recorded DATETIME,
beginning_state INT,
end_state INT,
management_rule_id INT NULL,
mechanism VARCHAR(20) NULL,
user_id INT NULL
)
CREATE TABLE management_rule(
management_rule_id INT AUTO_INCREMENT PRIMARY KEY,
tenant_id INT,
name VARCHAR(50),
result_value INT DEFAULT 1,
description VARCHAR(2000),
time_valid_start TIME NULL,
time_valid_end TIME NULL,
conditions TEXT,
created DATETIME
)
CREATE TABLE inverter_log(
inverter_log_id INT AUTO_INCREMENT PRIMARY KEY,
tenant_id INT,
recorded DATETIME,
solar_power INT NULL,
load_power INT NULL,
grid_power INT NULL,
battery_percentage INT NULL,
battery_power INT NULL,
battery_voltage DECIMAL(8,5) NULL,
solar_potential INT NULL
mystery_value1 INT NULL,
mystery_value2 INT NULL,
changer1 INT NULL,
changer2 INT NULL,
changer3 INT NULL,
changer4 INT NULL,
changer5 INT NULL,
changer6 INT NULL,
changer7 INT NULL,
weather VARCHAR(50) NULL
)
CREATE TABLE report(
report_id INT AUTO_INCREMENT PRIMARY KEY,
tenant_id INT,
name VARCHAR(100),
created DATETIME,
modified DATETIME,
form TEXT NULL,
`sql` TEXT NULL,
role VARCHAR(50) NULL,
templateable TINYINT DEFAULT 0
);
CREATE TABLE report_log(
report_log_id INT AUTO_INCREMENT PRIMARY KEY,
report_id INT,
tenant_id INT,
run DATETIME,
data TEXT NULL,
`sql` TEXT NULL,
records_returned int,
runtime int,
user_id INT NULL
);
CREATE TABLE tenant(
tenant_id INT AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(100),
`about` VARCHAR(300),
expired DATETIME NULL,
created DATETIME,
preferences TEXT NULL,
storage_password VARCHAR(100) NULL,
energy_api_username VARCHAR(100) NULL,
energy_api_password VARCHAR(100) NULL,
energy_api_plant_id INT NULL,
open_weather_api_key VARCHAR(100) NULL,
latitude DECIMAL(8,5) NULL,
longitude DECIMAL(8,5) NULL
);
CREATE TABLE user(
user_id INT AUTO_INCREMENT PRIMARY KEY,
role VARCHAR(50) DEFAULT 'normal',
expired DATETIME NULL,
created DATETIME,
preferences TEXT NULL
);
CREATE TABLE tenant_user(
user_id INT,
tenant_id INT,
role VARCHAR(50) DEFAULT 'normal',
expired DATETIME NULL,
created DATETIME
);
--fixes for older versions of the schema:
--ALTER TABLE device_feature ADD enabled TINYINT DEFAULT 0;
--ALTER TABLE location ADD device_id INT NULL;
--ALTER TABLE location ADD device_type_feature_id INT NULL;
--ALTER TABLE location ADD i2c_address INT NULL;
--ALTER TABLE location ADD connecting_device_id INT NULL;
--ALTER TABLE location ADD tenant_id INT NULL;
--ALTER TABLE device_type ADD tenant_id INT NULL;
--ALTER TABLE device ADD tenant_id INT NULL;
--ALTER TABLE feature_type ADD tenant_id INT NULL;
--ALTER TABLE device_type_feature ADD tenant_id INT NULL;
--ALTER TABLE device_feature ADD tenant_id INT NULL;
--ALTER TABLE device_feature ADD last_known_device_value INT NULL;
--ALTER TABLE device_feature ADD last_known_device_modified DATETIME;
--ALTER TABLE user ADD preferences TEXT NULL;
--ALTER TABLE user ADD storage_password VARCHAR(100) NULL;
--ALTER TABLE device_type_feature ADD via_i2c_address INT NULL;
--ALTER TABLE user ADD energy_api_username VARCHAR(100) NULL;
--ALTER TABLE user ADD energy_api_password VARCHAR(100) NULL;
--ALTER TABLE user ADD energy_api_plant_id INT NULL;
--ALTER TABLE weather_data ADD sensor_id INT NULL;
--ALTER TABLE device_feature ADD allow_automatic_management TINYINT DEFAULT 1;
--ALTER TABLE device_feature ADD management_priority INT NULL;
--ALTER TABLE device ADD sensor_id INT NULL;
--ALTER TABLE management_rule ADD result_value INT NULL;
--ALTER TABLE management_rule RENAME COLUMN management_script TO conditions;
--ALTER TABLE device_feature_management_rule ADD management_priority INT DEFAULT 1;
--ALTER TABLE device_type_feature RENAME COLUMN device_type_id TO feature_type_id;
--ALTER TABLE device_type_feature ADD sensor_type INT DEFAULT NULL;
--ALTER TABLE device_type_feature ADD sensor_sub_type INT DEFAULT NULL;
--ALTER TABLE device_type_feature ADD power_pin INT DEFAULT NULL;
--ALTER TABLE device_feature MODIFY value INT DEFAULT NULL;
--ALTER TABLE device ADD latitude DECIMAL(8,5) NULL;
--ALTER TABLE device ADD longitude DECIMAL(8,5) NULL;
--ALTER TABLE user ADD open_weather_api_key VARCHAR(100) NULL;
--ALTER TABLE weather_data ADD device_feature_id INT NULL;
--ALTER TABLE inverter_log ADD battery_voltage DECIMAL(8,5) NULL;
--ALTER TABLE inverter_log ADD solar_potential INT NULL;
--ALTER TABLE report_log ADD sql TEXT NULL;
--ALTER TABLE inverter_log ADD mystery_value1 INT NULL;
--ALTER TABLE inverter_log ADD mystery_value2 INT NULL;
--ALTER TABLE inverter_log ADD changer1 INT NULL;
--ALTER TABLE inverter_log ADD changer2 INT NULL;
--ALTER TABLE inverter_log ADD changer3 INT NULL;
--ALTER TABLE inverter_log ADD changer4 INT NULL;
--ALTER TABLE inverter_log ADD changer5 INT NULL;
--ALTER TABLE inverter_log ADD changer6 INT NULL;
--ALTER TABLE inverter_log ADD changer7 INT NULL;
--ALTER TABLE report ADD role VARCHAR(50) NULL;
--ALTER TABLE report_log ADD user_id INT NULL;
--ALTER TABLE tenant ADD about TEXT NULL;
--ALTER TABLE report ADD templateable TINYINT NULL;
--ALTER TABLE device_feature ADD restore_automation_after INT NULL;
--ALTER TABLE device_feature ADD automation_disabled_when DATETIME NULL;
--ALTER TABLE inverter_log ADD weather VARCHAR(50) NULL;
--ALTER TABLE tenant ADD latitude DECIMAL(8,5) NULL;
--ALTER TABLE tenant ADD longitude DECIMAL(8,5) NULL;
--ALTER TABLE device_feature ADD user_id INT NULL;
--ALTER TABLE device_feature_log ADD user_id INT NULL;
INSERT INTO device_type (name, architecture, power_voltage, created) VALUES ('NodeMCU', 'ESP8266', 3.3, NOW());
INSERT INTO device (name, device_type_id, created) VALUES ('Hotspot Watchdog', 1, NOW());
INSERT INTO location (location_id, name, device_id, created) VALUES (1, 'outside cabin', 1, NOW());
INSERT INTO location (location_id, name, device_id, created) VALUES (2, 'downstairs cabin', 1, NOW());
INSERT INTO location (location_id, name, device_id, created) VALUES (3, 'upstairs cabin', 1, NOW());
INSERT INTO feature_type (name, created) VALUES ('digital output', NOW());
INSERT INTO device_type_feature (name, created, can_be_input, can_be_output, can_be_analog, pin_number) VALUES ('D14', NOW(), 1, 1, 0, 13);
INSERT INTO device_feature (name, created, enabled) VALUES ('Moxee Power Switch', NOW(), 1);