Projet de remplacement du "RPiPasserelle" d'Otec.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pyheatpump.test.sql 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. CREATE TABLE IF NOT EXISTS var_type (
  2. slabel CHAR(1) UNIQUE PRIMARY KEY,
  3. label VARCHAR(10) UNIQUE,
  4. type VARCHAR(10) NOT NULL,
  5. start_address INT NOT NULL DEFAULT 0,
  6. end_address INT NOT NULL DEFAULT 250
  7. );
  8. CREATE TABLE IF NOT EXISTS variable (
  9. type CHAR(1) NOT NULL,
  10. address INT NOT NULL,
  11. unit VARCHAR(5) NULL,
  12. last_update INT NULL,
  13. FOREIGN KEY (type) REFERENCES var_type(slabel)
  14. ON DELETE CASCADE,
  15. PRIMARY KEY(type, address)
  16. );
  17. CREATE TABLE IF NOT EXISTS var_value (
  18. type CHAR(1) NOT NULL,
  19. address INT NOT NULL,
  20. time INT DEFAULT CURRENT_TIMESTAMP,
  21. value INT NOT NULL,
  22. FOREIGN KEY (type) REFERENCES variable(type)
  23. ON DELETE CASCADE,
  24. FOREIGN KEY (address) REFERENCES variable(address)
  25. ON DELETE CASCADE,
  26. PRIMARY KEY(type, address, time)
  27. );
  28. INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
  29. 'A', 'Analog', 'float', 1, 125);
  30. INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
  31. 'I', 'Integer', 'int', 5002, 5002 + 125);
  32. INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
  33. 'D', 'Digital', 'bool', 1, 125);
  34. CREATE TRIGGER variable_insert BEFORE INSERT ON var_value
  35. FOR EACH ROW
  36. WHEN
  37. NOT EXISTS (
  38. SELECT MAX(time) FROM var_value
  39. WHERE
  40. NEW.type = type
  41. AND
  42. NEW.address = address
  43. AND
  44. NEW.value = value
  45. )
  46. BEGIN
  47. INSERT INTO var_value
  48. (type, address, value)
  49. VALUES (NEW.type, NEW.address, NEW.value);
  50. END;
  51. CREATE TRIGGER variable_last_update AFTER INSERT ON var_value
  52. FOR EACH ROW
  53. BEGIN
  54. UPDATE variable
  55. SET last_update = NEW.time
  56. WHERE
  57. type = NEW.type
  58. AND
  59. address = NEW.address;
  60. END;