Open HomeOfSourceCode opened 6 years ago
Looks like the PersistenceExtensions always takes the "last persisted state" into account - which most likely will reflect the "current" state of the item. Clearly not what you would have expected from the method name...
To me this looks like a bug and the "most recent" entry should be discarded for that matter - @kaikreuzer please object if you remember differently.
I agree, looks like a bug.
The root cause here is the non deterministic behaviour of the rule engine and the persistence manager: Both RuleEngineImpl and PersistenceManagerImpl implement the StateChangeListener interface and thus will be informed about item changes (the RuleEngine triggering rules for this item, the PersistenceManager persisting the current item state).
The order in which both components get called is not deterministic though. When the script item.previousState
is executed the persistence may or may not have happened.
In order to resolve this bug there are two possible resolutions:
previousState
this timestamp can be used to filter out the current state (if it is already persisted, the timestamps on item and 1st result from the persistence service match).The second option looks more robust to me.
Plus: it would allow any item to directly answer the question of when it was last updated itself, without the need to query (or use) a persistence service. Nice side-effect, IMHO.
As I understood, item.previousState(false) should reflect the state before current state of an item regarding to persistance database. It is clear, that this dos not work with
But should work with any SQL-Database if persistance is configured as strategy = everyChange, everyUpdate
I made some tests. It is working as expected with item.previousState(true) but not working with item.previousState(false)
My test szenario:
My rules:
If I change swSwitch10 from OFF to ON:
This is wrong: item.previousState(false) should be swSwitch10 OFF with timestamp Thu Jan 25 11:59:16 CET 2018. But is the same as current state of item.
If I update swSwitch10 from ON to ON:
This is wrong too.
For me it looks like item.previousState(false) is item.state on every time?