Merged expression table into virtual meter table in database

pull/80/head
13621160019@163.com 2021-11-14 16:40:58 +08:00
parent 6865fba169
commit 6856423ed1
11 changed files with 164 additions and 172 deletions

View File

@ -16,6 +16,35 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Removed ### Removed
- None. - None.
## [v1.4.0] - 2021-11-14
### Added
- added installation on docker to myems-modbus-tcp.
### Changed
- Merged expression table into virtual meter table in database.
NOTE: THIS CHANGE MAY BREAK YOUR SYSTEM.
Upgrade procedure for this change:
1. Stop the myems-normalization service or container.
2. Backup your database.
3. Upgrade myems/admin, myems/myems-api, and myems/myems-normalization source code,
and keep previous .env files unchanged.
4. Run database/upgrade/upgrade1.4.0.sql to merge expression into virtual meter
5. Check the virtual meters in Admin UI.
6. Start the myems-normalization service or container.
- updated virtual meter model view in admin ui
- updated docker hub address in README
- updated excel exporters to make them print friendly
- added access control to actions of user in api
### Fixed
- fixed issue in on_delete of gateway in API
- upgraded falcon framework to v3.0.1 in API to fix warnings
### Removed
- None.
## [v1.3.4] - 2021-11-06 ## [v1.3.4] - 2021-11-06
### Added ### Added
- added notification drop down list and notification page. - added notification drop down list and notification page.

View File

@ -374,18 +374,6 @@ VALUES
COMMIT; COMMIT;
-- ---------------------------------------------------------------------------------------------------------------------
-- Demo Data for table `myems_system_db`.`tbl_expressions`
-- ---------------------------------------------------------------------------------------------------------------------
START TRANSACTION;
USE `myems_system_db`;
INSERT INTO `myems_system_db`.`tbl_expressions`
(`id`, `uuid`, `virtual_meter_id`, `equation`)
VALUES
(1, '3fff2cfb-f755-44c8-a919-6135205a8573', 1, 'x1+x2-x3');
COMMIT;
-- --------------------------------------------------------------------------------------------------------------------- -- ---------------------------------------------------------------------------------------------------------------------
-- Demo Data for table `myems_system_db`.`tbl_gateways` -- Demo Data for table `myems_system_db`.`tbl_gateways`
-- --------------------------------------------------------------------------------------------------------------------- -- ---------------------------------------------------------------------------------------------------------------------
@ -1114,9 +1102,9 @@ START TRANSACTION;
USE `myems_system_db`; USE `myems_system_db`;
INSERT INTO `myems_system_db`.`tbl_virtual_meters` INSERT INTO `myems_system_db`.`tbl_virtual_meters`
(`id`, `name`, `uuid`, `energy_category_id`, `is_counted`, `cost_center_id`, `energy_item_id`, `description`) (`id`, `name`, `uuid`, `equation`, `energy_category_id`, `is_counted`, `cost_center_id`, `energy_item_id`, `description`)
VALUES VALUES
(1, '虚拟表1', '3fff2cfb-f755-44c8-a919-6135205a8573', 1, true, 1, 1, 'virtual description'); (1, '虚拟表1', '3fff2cfb-f755-44c8-a919-6135205a8573', 'x1+x2+x3', 1, true, 1, 1, 'virtual description');
COMMIT; COMMIT;
@ -1127,7 +1115,7 @@ START TRANSACTION;
USE `myems_system_db`; USE `myems_system_db`;
-- meter_type = {'meter', 'virtual_meter', 'offline_meter'} -- meter_type = {'meter', 'virtual_meter', 'offline_meter'}
INSERT INTO `myems_system_db`.`tbl_variables` INSERT INTO `myems_system_db`.`tbl_variables`
(`id`, `name`, `expression_id`, `meter_type`, `meter_id`) (`id`, `name`, `virtual_meter_id`, `meter_type`, `meter_id`)
VALUES VALUES
(1, 'x1', 1, 'meter', 1), (1, 'x1', 1, 'meter', 1),
(2, 'x2', 1, 'meter', 2), (2, 'x2', 1, 'meter', 2),

View File

@ -375,18 +375,6 @@ VALUES
COMMIT; COMMIT;
-- ---------------------------------------------------------------------------------------------------------------------
-- Demo Data for table `myems_system_db`.`tbl_expressions`
-- ---------------------------------------------------------------------------------------------------------------------
START TRANSACTION;
USE `myems_system_db`;
INSERT INTO `myems_system_db`.`tbl_expressions`
(`id`, `uuid`, `virtual_meter_id`, `equation`)
VALUES
(1, '3fff2cfb-f755-44c8-a919-6135205a8573', 1, 'x1+x2-x3');
COMMIT;
-- --------------------------------------------------------------------------------------------------------------------- -- ---------------------------------------------------------------------------------------------------------------------
-- Demo Data for table `myems_system_db`.`tbl_gateways` -- Demo Data for table `myems_system_db`.`tbl_gateways`
-- --------------------------------------------------------------------------------------------------------------------- -- ---------------------------------------------------------------------------------------------------------------------
@ -1127,9 +1115,9 @@ START TRANSACTION;
USE `myems_system_db`; USE `myems_system_db`;
INSERT INTO `myems_system_db`.`tbl_virtual_meters` INSERT INTO `myems_system_db`.`tbl_virtual_meters`
(`id`, `name`, `uuid`, `energy_category_id`, `is_counted`, `cost_center_id`, `energy_item_id`, `description`) (`id`, `name`, `uuid`, `equation`, `energy_category_id`, `is_counted`, `cost_center_id`, `energy_item_id`, `description`)
VALUES VALUES
(1, 'Beispiel Virtueller Meter', '3fff2cfb-f755-44c8-a919-6135205a8573', 1, true, 1, 1, 'virtual description'); (1, 'Beispiel Virtueller Meter', '3fff2cfb-f755-44c8-a919-6135205a8573', 'x1+x2+x3', 1, true, 1, 1, 'virtual description');
COMMIT; COMMIT;
@ -1140,7 +1128,7 @@ START TRANSACTION;
USE `myems_system_db`; USE `myems_system_db`;
-- meter_type = {'meter', 'virtual_meter', 'offline_meter'} -- meter_type = {'meter', 'virtual_meter', 'offline_meter'}
INSERT INTO `myems_system_db`.`tbl_variables` INSERT INTO `myems_system_db`.`tbl_variables`
(`id`, `name`, `expression_id`, `meter_type`, `meter_id`) (`id`, `name`, `virtual_meter_id`, `meter_type`, `meter_id`)
VALUES VALUES
(1, 'x1', 1, 'meter', 1), (1, 'x1', 1, 'meter', 1),
(2, 'x2', 1, 'meter', 2), (2, 'x2', 1, 'meter', 2),

View File

@ -375,18 +375,6 @@ VALUES
COMMIT; COMMIT;
-- ---------------------------------------------------------------------------------------------------------------------
-- Demo Data for table `myems_system_db`.`tbl_expressions`
-- ---------------------------------------------------------------------------------------------------------------------
START TRANSACTION;
USE `myems_system_db`;
INSERT INTO `myems_system_db`.`tbl_expressions`
(`id`, `uuid`, `virtual_meter_id`, `equation`)
VALUES
(1, '3fff2cfb-f755-44c8-a919-6135205a8573', 1, 'x1+x2-x3');
COMMIT;
-- --------------------------------------------------------------------------------------------------------------------- -- ---------------------------------------------------------------------------------------------------------------------
-- Demo Data for table `myems_system_db`.`tbl_gateways` -- Demo Data for table `myems_system_db`.`tbl_gateways`
-- --------------------------------------------------------------------------------------------------------------------- -- ---------------------------------------------------------------------------------------------------------------------
@ -1126,9 +1114,9 @@ START TRANSACTION;
USE `myems_system_db`; USE `myems_system_db`;
INSERT INTO `myems_system_db`.`tbl_virtual_meters` INSERT INTO `myems_system_db`.`tbl_virtual_meters`
(`id`, `name`, `uuid`, `energy_category_id`, `is_counted`, `cost_center_id`, `energy_item_id`, `description`) (`id`, `name`, `uuid`, `equation`, `energy_category_id`, `is_counted`, `cost_center_id`, `energy_item_id`, `description`)
VALUES VALUES
(1, 'Example Virtual Meter', '3fff2cfb-f755-44c8-a919-6135205a8573', 1, true, 1, 1, 'virtual description'); (1, 'Example Virtual Meter', '3fff2cfb-f755-44c8-a919-6135205a8573', 'x1+x2+x3', 1, true, 1, 1, 'virtual description');
COMMIT; COMMIT;
@ -1139,7 +1127,7 @@ START TRANSACTION;
USE `myems_system_db`; USE `myems_system_db`;
-- meter_type = {'meter', 'virtual_meter', 'offline_meter'} -- meter_type = {'meter', 'virtual_meter', 'offline_meter'}
INSERT INTO `myems_system_db`.`tbl_variables` INSERT INTO `myems_system_db`.`tbl_variables`
(`id`, `name`, `expression_id`, `meter_type`, `meter_id`) (`id`, `name`, `virtual_meter_id`, `meter_type`, `meter_id`)
VALUES VALUES
(1, 'x1', 1, 'meter', 1), (1, 'x1', 1, 'meter', 1),
(2, 'x2', 1, 'meter', 2), (2, 'x2', 1, 'meter', 2),

View File

@ -336,19 +336,6 @@ CREATE TABLE IF NOT EXISTS `myems_system_db`.`tbl_equipments_virtual_meters` (
PRIMARY KEY (`id`)); PRIMARY KEY (`id`));
CREATE INDEX `tbl_equipments_virtual_meters_index_1` ON `myems_system_db`.`tbl_equipments_virtual_meters` (`equipment_id`); CREATE INDEX `tbl_equipments_virtual_meters_index_1` ON `myems_system_db`.`tbl_equipments_virtual_meters` (`equipment_id`);
-- ---------------------------------------------------------------------------------------------------------------------
-- Table `myems_system_db`.`tbl_expressions`
-- ---------------------------------------------------------------------------------------------------------------------
DROP TABLE IF EXISTS `myems_system_db`.`tbl_expressions` ;
CREATE TABLE IF NOT EXISTS `myems_system_db`.`tbl_expressions` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`uuid` CHAR(36) NOT NULL,
`virtual_meter_id` BIGINT NOT NULL,
`equation` LONGTEXT NOT NULL,
PRIMARY KEY (`id`));
CREATE INDEX `tbl_expressions_index_1` ON `myems_system_db`.`tbl_expressions` (`virtual_meter_id`);
-- --------------------------------------------------------------------------------------------------------------------- -- ---------------------------------------------------------------------------------------------------------------------
-- Table `myems_system_db`.`tbl_gateways` -- Table `myems_system_db`.`tbl_gateways`
-- --------------------------------------------------------------------------------------------------------------------- -- ---------------------------------------------------------------------------------------------------------------------
@ -1213,6 +1200,7 @@ CREATE TABLE IF NOT EXISTS `myems_system_db`.`tbl_virtual_meters` (
`id` BIGINT NOT NULL AUTO_INCREMENT, `id` BIGINT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL, `name` VARCHAR(255) NOT NULL,
`uuid` CHAR(36) NOT NULL, `uuid` CHAR(36) NOT NULL,
`equation` LONGTEXT NOT NULL,
`energy_category_id` BIGINT NOT NULL, `energy_category_id` BIGINT NOT NULL,
`is_counted` BOOL NOT NULL, `is_counted` BOOL NOT NULL,
`cost_center_id` BIGINT NOT NULL, `cost_center_id` BIGINT NOT NULL,
@ -1231,12 +1219,12 @@ DROP TABLE IF EXISTS `myems_system_db`.`tbl_variables` ;
CREATE TABLE IF NOT EXISTS `myems_system_db`.`tbl_variables` ( CREATE TABLE IF NOT EXISTS `myems_system_db`.`tbl_variables` (
`id` BIGINT NOT NULL AUTO_INCREMENT, `id` BIGINT NOT NULL AUTO_INCREMENT,
`name` CHAR(36) NOT NULL, `name` CHAR(36) NOT NULL,
`expression_id` BIGINT NOT NULL, `virtual_meter_id` BIGINT NOT NULL,
`meter_type` VARCHAR(32) NOT NULL COMMENT 'meter, virtual_meter, offline_meter', `meter_type` VARCHAR(32) NOT NULL COMMENT 'meter, virtual_meter, offline_meter',
`meter_id` BIGINT NOT NULL, `meter_id` BIGINT NOT NULL,
PRIMARY KEY (`id`)); PRIMARY KEY (`id`));
CREATE INDEX `tbl_variables_index_1` ON `myems_system_db`.`tbl_variables` (`expression_id`); CREATE INDEX `tbl_variables_index_1` ON `myems_system_db`.`tbl_variables` (`virtual_meter_id`);
CREATE INDEX `tbl_variables_index_2` ON `myems_system_db`.`tbl_variables` (`meter_id`, `meter_type`, `expression_id`); CREATE INDEX `tbl_variables_index_2` ON `myems_system_db`.`tbl_variables` (`meter_id`, `meter_type`, `virtual_meter_id`);
-- --------------------------------------------------------------------------------------------------------------------- -- ---------------------------------------------------------------------------------------------------------------------
-- Table `myems_system_db`.`tbl_versions` -- Table `myems_system_db`.`tbl_versions`
@ -1258,6 +1246,6 @@ USE `myems_system_db`;
INSERT INTO `myems_system_db`.`tbl_versions` INSERT INTO `myems_system_db`.`tbl_versions`
(`id`, `version`, `release_date`) (`id`, `version`, `release_date`)
VALUES VALUES
(1, '1.3.3', '2021-10-30'); (1, '1.4.0', '2021-11-13');
COMMIT; COMMIT;

View File

@ -0,0 +1,60 @@
-- NOTE: BACKUP YOUR DATABASE AND SOURCE CODE FIRST
-- THIS UPGRADE MAY CAUSE DATA LOSS AND BREAK YOUR MYEMS SYSTEM
-- THIS UPGRADE SCRIPT WILL MERGE EXPRESSION TABLE INTO VIRTUAL METER TABLE
-- STOP NORMALIZATION SERVICE BEFORE RUNNING THIS SCRIPT:
-- $ sudo systemctl stop myems-normalization.service
-- AFTER RUNNING THIS UPGRADE SCRIPT,
-- UPGRADE ADMIN UI, API, AND NORMALIZATION SERVICE
-- START NORMALIZATION SERVICE
-- $ sudo systemctl start myems-normalization.service
START TRANSACTION;
CREATE TABLE IF NOT EXISTS `myems_system_db`.`tbl_virtual_meters_new` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`uuid` CHAR(36) NOT NULL,
`equation` LONGTEXT NOT NULL,
`energy_category_id` BIGINT NOT NULL,
`is_counted` BOOL NOT NULL,
`cost_center_id` BIGINT NOT NULL,
`energy_item_id` BIGINT,
`description` VARCHAR(255),
PRIMARY KEY (`id`));
CREATE TABLE IF NOT EXISTS `myems_system_db`.`tbl_variables_new` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`name` CHAR(36) NOT NULL,
`virtual_meter_id` BIGINT NOT NULL,
`meter_type` VARCHAR(32) NOT NULL COMMENT 'meter, virtual_meter, offline_meter',
`meter_id` BIGINT NOT NULL,
PRIMARY KEY (`id`));
INSERT INTO myems_system_db.tbl_virtual_meters_new
(id, name, uuid, equation, energy_category_id, is_counted, cost_center_id, energy_item_id, description)
SELECT tvm.id, tvm.name, tvm.uuid, te.equation, tvm.energy_category_id, tvm.is_counted, tvm.cost_center_id,
tvm.energy_item_id, tvm.description
FROM myems_system_db.tbl_virtual_meters tvm, myems_system_db.tbl_expressions te
WHERE tvm.id = te.virtual_meter_id ;
INSERT INTO myems_system_db.tbl_variables_new
(id, name, virtual_meter_id, meter_type, meter_id)
SELECT tv.id, tv.name, te.virtual_meter_id, tv.meter_type, tv.meter_id
FROM myems_system_db.tbl_variables tv, myems_system_db.tbl_expressions te
WHERE tv.expression_id = te.id ;
DROP TABLE IF EXISTS myems_system_db.tbl_variables;
DROP TABLE IF EXISTS myems_system_db.tbl_virtual_meters;
ALTER TABLE myems_system_db.tbl_variables_new RENAME myems_system_db.tbl_variables;
ALTER TABLE myems_system_db.tbl_virtual_meters_new RENAME myems_system_db.tbl_virtual_meters;
CREATE INDEX `tbl_variables_index_1` ON `myems_system_db`.`tbl_variables` (`virtual_meter_id`);
CREATE INDEX `tbl_variables_index_2` ON `myems_system_db`.`tbl_variables` (`meter_id`, `meter_type`, `virtual_meter_id`);
CREATE INDEX `tbl_virtual_meters_index_1` ON `myems_system_db`.`tbl_virtual_meters` (`name`);
CREATE INDEX `tbl_virtual_meters_index_2` ON `myems_system_db`.`tbl_virtual_meters` (`energy_category_id`);
CREATE INDEX `tbl_virtual_meters_index_3` ON `myems_system_db`.`tbl_virtual_meters` (`energy_item_id`);
-- UPDATE VERSION NUMBER
UPDATE myems_system_db.tbl_versions SET version='1.4.0', release_date='2021-11-13' WHERE id=1;
COMMIT;

View File

@ -394,9 +394,8 @@ class MeterItem:
# check if this meter is being used by virtual meters # check if this meter is being used by virtual meters
cursor.execute(" SELECT vm.name " cursor.execute(" SELECT vm.name "
" FROM tbl_variables va, tbl_expressions ex, tbl_virtual_meters vm " " FROM tbl_variables va, tbl_virtual_meters vm "
" WHERE va.meter_id = %s AND va.meter_type = 'meter' AND va.expression_id = ex.id " " WHERE va.meter_id = %s AND va.meter_type = 'meter' AND va.virtual_meter_id = vm.id ",
" AND ex.virtual_meter_id = vm.id ",
(id_,)) (id_,))
row_virtual_meter = cursor.fetchone() row_virtual_meter = cursor.fetchone()
if row_virtual_meter is not None: if row_virtual_meter is not None:

View File

@ -339,9 +339,8 @@ class OfflineMeterItem:
# check if this offline meter is being used by virtual meters # check if this offline meter is being used by virtual meters
cursor.execute(" SELECT vm.name " cursor.execute(" SELECT vm.name "
" FROM tbl_variables va, tbl_expressions ex, tbl_virtual_meters vm " " FROM tbl_variables va, tbl_virtual_meters vm "
" WHERE va.meter_id = %s AND va.meter_type = 'offline_meter' AND va.expression_id = ex.id " " WHERE va.meter_id = %s AND va.meter_type = 'offline_meter' AND va.virtual_meter_id = vm.id ",
" AND ex.virtual_meter_id = vm.id ",
(id_,)) (id_,))
row_virtual_meter = cursor.fetchone() row_virtual_meter = cursor.fetchone()
if row_virtual_meter is not None: if row_virtual_meter is not None:

View File

@ -57,8 +57,8 @@ class VirtualMeterCollection:
"name": row['name'], "name": row['name'],
"uuid": row['uuid']} "uuid": row['uuid']}
query = (" SELECT id, name, uuid, energy_category_id, is_counted, " query = (" SELECT id, name, uuid, equation, energy_category_id, is_counted, cost_center_id, "
" energy_item_id, cost_center_id, description " " energy_item_id, description "
" FROM tbl_virtual_meters " " FROM tbl_virtual_meters "
" ORDER BY id ") " ORDER BY id ")
cursor.execute(query) cursor.execute(query)
@ -68,36 +68,29 @@ class VirtualMeterCollection:
if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
for row in rows_virtual_meters: for row in rows_virtual_meters:
energy_category = energy_category_dict.get(row['energy_category_id'], None) energy_category = energy_category_dict.get(row['energy_category_id'], None)
energy_item = energy_item_dict.get(row['energy_item_id'], None)
cost_center = cost_center_dict.get(row['cost_center_id'], None) cost_center = cost_center_dict.get(row['cost_center_id'], None)
energy_item = energy_item_dict.get(row['energy_item_id'], None)
meta_result = {"id": row['id'], meta_result = {"id": row['id'],
"name": row['name'], "name": row['name'],
"uuid": row['uuid'], "uuid": row['uuid'],
"equation": row['equation'],
"energy_category": energy_category, "energy_category": energy_category,
"is_counted": True if row['is_counted'] else False, "is_counted": True if row['is_counted'] else False,
"energy_item": energy_item,
"cost_center": cost_center, "cost_center": cost_center,
"energy_item": energy_item,
"description": row['description'], "description": row['description'],
"expression": {}} "expression": {}}
expression = dict() expression = dict()
query_expression = (" SELECT e.id, e.uuid, e.equation "
" FROM tbl_expressions e "
" WHERE e.virtual_meter_id = %s ")
cursor.execute(query_expression, (row['id'],))
row_expression = cursor.fetchone()
if row_expression is not None: if meta_result["equation"] is not None:
expression = {'id': row_expression['id'], expression = {'equation': meta_result["equation"], 'variables': []}
'uuid': row_expression['uuid'],
'equation': row_expression['equation'],
'variables': []}
query_variables = (" SELECT v.id, v.name, v.meter_type, v.meter_id " query_variables = (" SELECT v.id, v.name, v.meter_type, v.meter_id "
" FROM tbl_expressions e, tbl_variables v " " FROM tbl_virtual_meters vm, tbl_variables v "
" WHERE e.id = %s AND v.expression_id = e.id " " WHERE vm.id = %s AND v.virtual_meter_id = vm.id "
" ORDER BY v.name ") " ORDER BY v.name ")
cursor.execute(query_variables, (row_expression['id'],)) cursor.execute(query_variables, (meta_result['id'],))
rows_variables = cursor.fetchall() rows_variables = cursor.fetchall()
if rows_variables is not None: if rows_variables is not None:
for row_variable in rows_variables: for row_variable in rows_variables:
@ -312,11 +305,12 @@ class VirtualMeterCollection:
description='API.VIRTUAL_METER_OF_VARIABLE_NOT_FOUND') description='API.VIRTUAL_METER_OF_VARIABLE_NOT_FOUND')
add_values = (" INSERT INTO tbl_virtual_meters " add_values = (" INSERT INTO tbl_virtual_meters "
" (name, uuid, energy_category_id, is_counted, " " (name, uuid, equation, energy_category_id, is_counted, "
" cost_center_id, energy_item_id, description) " " cost_center_id, energy_item_id, description) "
" VALUES (%s, %s, %s, %s, %s, %s, %s) ") " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ")
cursor.execute(add_values, (name, cursor.execute(add_values, (name,
str(uuid.uuid4()), str(uuid.uuid4()),
new_values['data']['expression']['equation'].lower(),
energy_category_id, energy_category_id,
is_counted, is_counted,
cost_center_id, cost_center_id,
@ -325,32 +319,12 @@ class VirtualMeterCollection:
new_id = cursor.lastrowid new_id = cursor.lastrowid
cnx.commit() cnx.commit()
cursor.execute(" SELECT id "
" FROM tbl_expressions "
" WHERE virtual_meter_id = %s ", (new_id,))
row_expression = cursor.fetchone()
if row_expression is not None:
# delete variables
cursor.execute(" DELETE FROM tbl_variables WHERE expression_id = %s ", (row_expression[0],))
# delete expression
cursor.execute(" DELETE FROM tbl_expressions WHERE id = %s ", (row_expression[0],))
cnx.commit()
# add expression
add_values = (" INSERT INTO tbl_expressions (uuid, virtual_meter_id, equation) "
" VALUES (%s, %s, %s) ")
cursor.execute(add_values, (str(uuid.uuid4()),
new_id,
new_values['data']['expression']['equation'].lower()))
new_expression_id = cursor.lastrowid
cnx.commit()
# add variables # add variables
for variable in new_values['data']['expression']['variables']: for variable in new_values['data']['expression']['variables']:
add_values = (" INSERT INTO tbl_variables (name, expression_id, meter_type, meter_id) " add_values = (" INSERT INTO tbl_variables (name, virtual_meter_id, meter_type, meter_id) "
" VALUES (%s, %s, %s, %s) ") " VALUES (%s, %s, %s, %s) ")
cursor.execute(add_values, (variable['name'].lower(), cursor.execute(add_values, (variable['name'].lower(),
new_expression_id, new_id,
variable['meter_type'], variable['meter_type'],
variable['meter_id'],)) variable['meter_id'],))
cnx.commit() cnx.commit()
@ -417,8 +391,8 @@ class VirtualMeterItem:
"name": row['name'], "name": row['name'],
"uuid": row['uuid']} "uuid": row['uuid']}
query = (" SELECT id, name, uuid, energy_category_id, is_counted, " query = (" SELECT id, name, uuid, equation, energy_category_id, is_counted, cost_center_id, "
" energy_item_id, cost_center_id, description " " energy_item_id, description "
" FROM tbl_virtual_meters " " FROM tbl_virtual_meters "
" WHERE id = %s ") " WHERE id = %s ")
cursor.execute(query, (id_,)) cursor.execute(query, (id_,))
@ -428,36 +402,29 @@ class VirtualMeterItem:
description='API.VIRTUAL_METER_NOT_FOUND') description='API.VIRTUAL_METER_NOT_FOUND')
else: else:
energy_category = energy_category_dict.get(row['energy_category_id'], None) energy_category = energy_category_dict.get(row['energy_category_id'], None)
energy_item = energy_item_dict.get(row['energy_item_id'], None)
cost_center = cost_center_dict.get(row['cost_center_id'], None) cost_center = cost_center_dict.get(row['cost_center_id'], None)
energy_item = energy_item_dict.get(row['energy_item_id'], None)
meta_result = {"id": row['id'], meta_result = {"id": row['id'],
"name": row['name'], "name": row['name'],
"uuid": row['uuid'], "uuid": row['uuid'],
"equation": row['equation'],
"energy_category": energy_category, "energy_category": energy_category,
"is_counted": True if row['is_counted'] else False, "is_counted": True if row['is_counted'] else False,
"energy_item": energy_item,
"cost_center": cost_center, "cost_center": cost_center,
"energy_item": energy_item,
"description": row['description'], "description": row['description'],
"expression": {}} "expression": {}}
expression = dict() expression = dict()
query_expression = (" SELECT e.id, e.uuid, e.equation "
" FROM tbl_expressions e "
" WHERE e.virtual_meter_id = %s ")
cursor.execute(query_expression, (id_,))
row_expression = cursor.fetchone()
if row_expression is not None: if meta_result["equation"] is not None:
expression = {'id': row_expression['id'], expression = {'equation': meta_result["equation"], 'variables': []}
'uuid': row_expression['uuid'],
'equation': row_expression['equation'],
'variables': []}
query_variables = (" SELECT v.id, v.name, v.meter_type, v.meter_id " query_variables = (" SELECT v.id, v.name, v.meter_type, v.meter_id "
" FROM tbl_expressions e, tbl_variables v " " FROM tbl_virtual_meters vm, tbl_variables v "
" WHERE e.id = %s AND v.expression_id = e.id " " WHERE vm.id = %s AND v.virtual_meter_id = vm.id "
" ORDER BY v.name ") " ORDER BY v.name ")
cursor.execute(query_variables, (row_expression['id'],)) cursor.execute(query_variables, (meta_result["id"],))
rows_variables = cursor.fetchall() rows_variables = cursor.fetchall()
if rows_variables is not None: if rows_variables is not None:
for row_variable in rows_variables: for row_variable in rows_variables:
@ -528,9 +495,8 @@ class VirtualMeterItem:
# check relations with other virtual meters # check relations with other virtual meters
cursor.execute(" SELECT vm.name " cursor.execute(" SELECT vm.name "
" FROM tbl_variables va, tbl_expressions ex, tbl_virtual_meters vm " " FROM tbl_variables va, tbl_virtual_meters vm "
" WHERE va.meter_id = %s AND va.meter_type = 'virtual_meter' AND va.expression_id = ex.id " " WHERE va.meter_id = %s AND va.meter_type = 'virtual_meter' AND va.virtual_meter_id = vm.id ",
" AND ex.virtual_meter_id = vm.id ",
(id_,)) (id_,))
row_virtual_meter = cursor.fetchone() row_virtual_meter = cursor.fetchone()
if row_virtual_meter is not None: if row_virtual_meter is not None:
@ -639,17 +605,6 @@ class VirtualMeterItem:
title='API.BAD_REQUEST', title='API.BAD_REQUEST',
description='API.THERE_IS_RELATION_WITH_SHOPFLOORS') description='API.THERE_IS_RELATION_WITH_SHOPFLOORS')
cursor.execute(" SELECT id "
" FROM tbl_expressions "
" WHERE virtual_meter_id = %s ", (id_,))
row_expression = cursor.fetchone()
if row_expression is not None:
# delete variables
cursor.execute(" DELETE FROM tbl_variables WHERE expression_id = %s ", (row_expression[0],))
# delete expression
cursor.execute(" DELETE FROM tbl_expressions WHERE id = %s ", (row_expression[0],))
cnx.commit()
# check relation with energy flow diagram links # check relation with energy flow diagram links
cursor.execute(" SELECT id " cursor.execute(" SELECT id "
" FROM tbl_energy_flow_diagrams_links " " FROM tbl_energy_flow_diagrams_links "
@ -662,6 +617,15 @@ class VirtualMeterItem:
title='API.BAD_REQUEST', title='API.BAD_REQUEST',
description='API.THERE_IS_RELATION_WITH_ENERGY_FLOW_DIAGRAM_LINKS') description='API.THERE_IS_RELATION_WITH_ENERGY_FLOW_DIAGRAM_LINKS')
cursor.execute(" SELECT id "
" FROM tbl_variables "
" WHERE virtual_meter_id = %s ", (id_,))
row_variables = cursor.fetchall()
if row_variables is not None and len(row_variables) > 0:
# delete variables
cursor.execute(" DELETE FROM tbl_variables WHERE virtual_meter_id = %s ", (id_,))
cnx.commit()
cursor.execute(" DELETE FROM tbl_virtual_meters WHERE id = %s ", (id_,)) cursor.execute(" DELETE FROM tbl_virtual_meters WHERE id = %s ", (id_,))
cnx.commit() cnx.commit()
@ -850,10 +814,11 @@ class VirtualMeterItem:
description='API.VIRTUAL_METER_OF_VARIABLE_NOT_FOUND') description='API.VIRTUAL_METER_OF_VARIABLE_NOT_FOUND')
update_row = (" UPDATE tbl_virtual_meters " update_row = (" UPDATE tbl_virtual_meters "
" SET name = %s, energy_category_id = %s, is_counted = %s, " " SET name = %s, equation = %s, energy_category_id = %s, is_counted = %s, "
" cost_center_id = %s, energy_item_id = %s, description = %s " " cost_center_id = %s, energy_item_id = %s, description = %s "
" WHERE id = %s ") " WHERE id = %s ")
cursor.execute(update_row, (name, cursor.execute(update_row, (name,
new_values['data']['expression']['equation'].lower(),
energy_category_id, energy_category_id,
is_counted, is_counted,
cost_center_id, cost_center_id,
@ -863,31 +828,20 @@ class VirtualMeterItem:
cnx.commit() cnx.commit()
cursor.execute(" SELECT id " cursor.execute(" SELECT id "
" FROM tbl_expressions " " FROM tbl_variables "
" WHERE virtual_meter_id = %s ", (id_,)) " WHERE virtual_meter_id = %s ", (id_,))
row_expression = cursor.fetchone() row_variables = cursor.fetchall()
if row_expression is not None: if row_variables is not None and len(row_variables) > 0:
# delete variables # delete variables
cursor.execute(" DELETE FROM tbl_variables WHERE expression_id = %s ", (row_expression[0],)) cursor.execute(" DELETE FROM tbl_variables WHERE virtual_meter_id = %s ", (id_,))
# delete expression
cursor.execute(" DELETE FROM tbl_expressions WHERE id = %s ", (row_expression[0],))
cnx.commit()
# add expression
add_values = (" INSERT INTO tbl_expressions (uuid, virtual_meter_id, equation) "
" VALUES (%s, %s, %s) ")
cursor.execute(add_values, (str(uuid.uuid4()),
id_,
new_values['data']['expression']['equation'].lower()))
new_expression_id = cursor.lastrowid
cnx.commit() cnx.commit()
# add variables # add variables
for variable in new_values['data']['expression']['variables']: for variable in new_values['data']['expression']['variables']:
add_values = (" INSERT INTO tbl_variables (name, expression_id, meter_type, meter_id) " add_values = (" INSERT INTO tbl_variables (name, virtual_meter_id, meter_type, meter_id) "
" VALUES (%s, %s, %s, %s) ") " VALUES (%s, %s, %s, %s) ")
cursor.execute(add_values, (variable['name'].lower(), cursor.execute(add_values, (variable['name'].lower(),
new_expression_id, id_,
variable['meter_type'], variable['meter_type'],
variable['meter_id'],)) variable['meter_id'],))
cnx.commit() cnx.commit()

View File

@ -25,11 +25,11 @@ def main():
logger.addHandler(fh) logger.addHandler(fh)
# calculate energy consumption in hourly period # calculate energy consumption in hourly period
Process(target=meter.calculate_hourly, args=(logger,)).start() # Process(target=meter.calculate_hourly, args=(logger,)).start()
Process(target=offlinemeter.calculate_hourly, args=(logger,)).start() # Process(target=offlinemeter.calculate_hourly, args=(logger,)).start()
Process(target=virtualmeter.calculate_hourly, args=(logger,)).start() Process(target=virtualmeter.calculate_hourly, args=(logger,)).start()
# calculate virtual point value # calculate virtual point value
Process(target=virtualpoint.calculate, args=(logger,)).start() # Process(target=virtualpoint.calculate, args=(logger,)).start()
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -36,10 +36,9 @@ def calculate_hourly(logger):
virtual_meter_list = list() virtual_meter_list = list()
try: try:
cursor_system_db.execute(" SELECT m.id, m.name, e.equation, e.id as expression_id " cursor_system_db.execute(" SELECT id, name, equation "
" FROM tbl_virtual_meters m, tbl_expressions e " " FROM tbl_virtual_meters "
" WHERE m.id = e.virtual_meter_id " " ORDER BY id ")
" ORDER BY m.id ")
rows_virtual_meters = cursor_system_db.fetchall() rows_virtual_meters = cursor_system_db.fetchall()
if rows_virtual_meters is None or len(rows_virtual_meters) == 0: if rows_virtual_meters is None or len(rows_virtual_meters) == 0:
@ -48,7 +47,7 @@ def calculate_hourly(logger):
continue continue
for row in rows_virtual_meters: for row in rows_virtual_meters:
meta_result = {"id": row[0], "name": row[1], "equation": row[2], "expression_id": row[3]} meta_result = {"id": row[0], "name": row[1], "equation": row[2]}
virtual_meter_list.append(meta_result) virtual_meter_list.append(meta_result)
except Exception as e: except Exception as e:
@ -196,8 +195,8 @@ def worker(virtual_meter):
" FROM tbl_meters m, tbl_variables v " " FROM tbl_meters m, tbl_variables v "
" WHERE m.id = v.meter_id " " WHERE m.id = v.meter_id "
" AND v.meter_type = 'meter' " " AND v.meter_type = 'meter' "
" AND v.expression_id = %s ", " AND v.virtual_meter_id = %s ",
(virtual_meter['expression_id'], )) (virtual_meter['id'], ))
rows = cursor_system_db.fetchall() rows = cursor_system_db.fetchall()
if rows is not None and len(rows) > 0: if rows is not None and len(rows) > 0:
for row in rows: for row in rows:
@ -211,8 +210,8 @@ def worker(virtual_meter):
" FROM tbl_virtual_meters m, tbl_variables v " " FROM tbl_virtual_meters m, tbl_variables v "
" WHERE m.id = v.meter_id " " WHERE m.id = v.meter_id "
" AND v.meter_type = 'virtual_meter' " " AND v.meter_type = 'virtual_meter' "
" AND v.expression_id = %s ", " AND v.virtual_meter_id = %s ",
(virtual_meter['expression_id'],)) (virtual_meter['id'],))
rows = cursor_system_db.fetchall() rows = cursor_system_db.fetchall()
if rows is not None and len(rows) > 0: if rows is not None and len(rows) > 0:
for row in rows: for row in rows:
@ -227,8 +226,8 @@ def worker(virtual_meter):
" FROM tbl_offline_meters m, tbl_variables v " " FROM tbl_offline_meters m, tbl_variables v "
" WHERE m.id = v.meter_id " " WHERE m.id = v.meter_id "
" AND v.meter_type = 'offline_meter' " " AND v.meter_type = 'offline_meter' "
" AND v.expression_id = %s ", " AND v.virtual_meter_id = %s ",
(virtual_meter['expression_id'],)) (virtual_meter['id'],))
rows = cursor_system_db.fetchall() rows = cursor_system_db.fetchall()
if rows is not None and len(rows) > 0: if rows is not None and len(rows) > 0:
for row in rows: for row in rows: