Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/656 device model initialization in cpp #681

Merged
merged 36 commits into from
Jul 8, 2024

Conversation

maaikez
Copy link
Contributor

@maaikez maaikez commented Jun 20, 2024

Describe your changes

Python script to initialize and insert device model database is now moved to C++. This also allows is to add more Connectors and EVSE's and change Variables of Connectors and EVSE's.

Not everything is done yet:

  • There are some todo's left (if you have an opinion on them, please add to the draft as well)
  • Master needs to be merged in.

Issue ticket number and link

#656

Checklist before requesting a review

maaikez added 12 commits June 5, 2024 18:01
Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
… (install directory) so it can be copied to the target. Move sql script to separate 'migration' folder.

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
…raints, change foreign key of variable characteristics so we can add an ON DELETE constraint, which prevents dangling characteristics. Get components from db and check if they are in the schema, remove if they are not in the schema files but are existing in the database (only for Connectors and EVSE's).

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
…eck if anything changed with components and variables and update variables.

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
…ributes, delete variables.

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
… call so it also works with in memory databases.

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
…commented code.

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
… so the value source is / can be set when an attribute value is changed. Add test for insert config and default values and fix some bugs.

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
@hikinggrass hikinggrass self-assigned this Jun 24, 2024
@maaikez maaikez force-pushed the feature/656-device-model-initialization-in-cpp branch from 8637579 to d9b5b0e Compare June 28, 2024 12:21
maaikez added 2 commits June 28, 2024 16:38
…me functions to not return a boolean, because they just throw if something is going wrong. Add more verbose information to exception.

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
@maaikez maaikez force-pushed the feature/656-device-model-initialization-in-cpp branch from 6b3e007 to dbe1291 Compare June 28, 2024 16:53
maaikez added 4 commits June 29, 2024 18:07
…d new tests. Fix almost all tests.

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
…ning when default value is an array or object).

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
@maaikez maaikez force-pushed the feature/656-device-model-initialization-in-cpp branch from f228821 to e5a500c Compare July 1, 2024 13:33
maaikez added 4 commits July 1, 2024 16:16
Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
…ing foreign key pointing to it.

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
… does not work anymore.

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
return config_values;
}

bool InitDeviceModelDb::check_config_integrity(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a check for the source of the value as well and add a log message if it is not 'default' (so the value is not changed)? And then maybe remove it from the list so we don't get more warnings about it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds like a good idea

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm because it is now a vector, if there is any error in the vector it will throw. So maybe it is not the best idea after all :). I think I keep it like this.

@maaikez maaikez force-pushed the feature/656-device-model-initialization-in-cpp branch from 52809d4 to 1fcf825 Compare July 3, 2024 09:51
Comment on lines 152 to 153
EVLOG_error << "Could not remove database " << database_path.u8string();
throw InitDeviceModelDbError("Could not remove database " + database_path.u8string());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EVLOG_AND_THROW

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it (and all the others). Shouldn't we use EVLOG_AND_THROW always instead of only throw?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say if you want to log an error that the user should see then yes, but if it's just an internal exception that can be handled without the user needed to know it's perfectly fine to just throw

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the "errors" output variable should be handled in a different way (preferable with a return value)

Copy link
Contributor Author

@maaikez maaikez Jul 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole string as return value you mean?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, or a struct/vector with the individual errors might also be an idea

…port migrations yet. Fix some config / schema inconsistencies.

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
@maaikez maaikez force-pushed the feature/656-device-model-initialization-in-cpp branch from a0216cd to 9ea7186 Compare July 3, 2024 15:31
Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
@maaikez maaikez force-pushed the feature/656-device-model-initialization-in-cpp branch from 9ea7186 to 654a121 Compare July 3, 2024 15:42
maaikez added 3 commits July 3, 2024 17:45
…fault config value could not be set because it has already changed by another source.

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
…components / variables / attributes that are not consistent with the database.

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
… at the check integrity function. Put inserting in database inside transactions, otherwise it is very slow. Add test for the default device model schemas / config.

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
InitDeviceModelDbError("Config not consistent with device model component schema's: \n" + output));
}

std::unique_ptr<common::DatabaseTransactionInterface> transaction = database->begin_transaction();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Starting a transaction here and ending it at the end of the function. If anything throws in between, transaction will not be committed. I think that's the idea but I can of course also add a try/catch there. What do you think?

Copy link
Contributor

@Pietfried Pietfried left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good and works well! Added a few remarks

MUTABILITY_ID INTEGER,
PERSISTENT INTEGER,
CONSTANT INTEGER,
TYPE_ID INTEGER,
VALUE_SOURCE TEXT DEFAULT "default",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to leave this empty and use config for the initialization (by the config file) because its more verbose. There might be variables in the database without a value for the variable attribute

Comment on lines +24 to +32
if (init_db) {
if (db_path.empty() || migration_files_path.empty() || schemas_path.empty() || config_path.empty()) {
EVLOG_AND_THROW(
DeviceModelStorageError("Can not initialize device model storage: one of the paths is empty."));
}
InitDeviceModelDb init_device_model_db(db_path, migration_files_path);
init_device_model_db.initialize_database(schemas_path, false);
init_device_model_db.insert_config_and_default_values(schemas_path, config_path);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also remove this constructor and move the content inside the ChargePoint constructor and leave the other DeviceModelStorageSqlite constructor as is.

I personally dont like a constructor that relies only on a certain combination of arguments to be valid. This would also avoid this odd initialzation L45

Copy link
Contributor Author

@maaikez maaikez Jul 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that constructor thing is a bit strange indeed
Problem is that I have to do the device model initialization before creating DeviceModelStorageSqlite.

I'll think about it (suggestions welcome)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the odd looking constructor and added default parameters to the remaining constructor. Because we do so much code in the constructors, I don't know how else to do it, because like I said the code has to be called before the code in the chargepoint constructor is called, which creates a device model instance. But all the migration and inserting stuff should have been done at that point already.

/// \param r Second componentKey to compare with the other ComponentKey
/// \return True if 'l' is 'smaller' than r (should be placed before 'r').
///
friend bool operator<(const ComponentKey& l, const ComponentKey& r);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like using unordered_map(s) could be more suitable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I tried to change it but for an unordered_map you need a hash function.

maaikez added 2 commits July 4, 2024 17:51
Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
…e and use default values.

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
@maaikez maaikez marked this pull request as ready for review July 5, 2024 09:53
@maaikez maaikez requested a review from marcemmers as a code owner July 5, 2024 09:53
@maaikez
Copy link
Contributor Author

maaikez commented Jul 5, 2024

Looks good and works well! Added a few remarks

They should be fixed now. If possible.

maaikez added 2 commits July 5, 2024 12:46
Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
@@ -52,12 +52,6 @@
"Actual": true
}
},
"EVSEPower": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering why these variables were removed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They have been removed because they are not configuration but telemetry variables, so there is no reason to have them in the config.

…oes otherwise not work.

Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
@maaikez maaikez merged commit 981925c into main Jul 8, 2024
3 of 4 checks passed
@maaikez maaikez deleted the feature/656-device-model-initialization-in-cpp branch February 17, 2025 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants