Fixes for PointType; added distanceFrom(otherPoint) #3410
Conversation
@watou : yes, that is fine to me but are you sure I did not already put distanceFrom in the lib ? Sorry, I have headaches catching up with what I think, what I do, what I push to OH1, ESH... will check but nearly sure it already exists somewhere. |
@@ -113,6 +118,24 @@ public DecimalType getGravity() { | |||
} | |||
|
|||
/** | |||
* Return the distance in meters from otherPoint, ignoring altitude. This algorithm also |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you specify which algorithm that is being used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's based on the Haversine formula.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool, I haven't used that one before. Might be useful to update the javadoc with this info.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your question prompted me to do so. Thanks for suggesting!
@clinique I don't see a |
Thank you, @clinique -- I should have looked there as well. I did not expect to find it in an item class. Is there a way of thinking about where such a method should live? My thinking was it should live in |
Yes, because gravity only depends on the PointType itself |
I would argue that |
i would say: if the Item provides the method then it should also take another Item to calculate the distance which would then delegate the calculation to the PointType since it has the "knowledge" about the data. So i would opt to move distanceFrom to the PointType. What do you think @kaikreuzer? |
Yes, I can see @watou point here and would agree that this is probably the better place. |
@kaikreuzer , @watou , no issue to move it. I'm sure I had a very good reason to put it there when I did it, but as I can't put the finger on it anymore, the reason might not be so good :) |
Thank you, @clinique. I changed I can set up an ESH development environment and submit a PR there, or I could leave it someone else. Whatever you think. |
I've got an ESH dev env - no pb to push the PR |
Make PointType.valueOf a static method like in ESH Return parseable string from PointType.toString() just like ESH Moved implementation of LocationItem.distanceFrom to PointType.distanceFrom Changed LocationItem.distanceFrom(PointType) to LocationItem.distanceFrom(LocationItem) and delegate to PointType.distanceFrom Updated LocationItemTest to match above.
dca3dc1
to
538b607
Compare
I think this PR is ready for 1.8, so please push a matching PR in ESH and thank you again, @clinique . |
The ESH bug I opened is 482188. |
@watou once the ESH PR is merged please go ahead and merge this PR as well. Thanks, Thomas E.-E. |
OK, will do. Thanks. |
eclipse-archived/smarthome#520 has been merged. Thanks! |
Fixes for PointType; added distanceFrom(otherPoint)
Thanks @watou! |
Signed-off-by: lewie <helmut.lehmeyer@gmail.com>
Addition:
distanceFrom(PointType otherPoint)
returns distance in meters from this point to another point. Useful for geofencing and for other purposes in rules.Fixes:
toString()
method would return nicely formatted string version ofPointType
, but the formatted version was useless from the REST API, and therefore unusable by clients. Now returns 3 decimal numbers separated by commas without rounding or other formatting. @clinique, is this OK with you?String
constructor would take any non-empty string and construct the object regardless of the string contents if it had no commas in the string. Following the example of theHSBType
ComplexType
, the string is now sanity checked in the constructor, andIllegalArgumentException
s are thrown if the string is not in the valid "lat,long[,alt]" format.valueOf
method must be static, since it's expected to be called inorg.openhab.core.types.TypeParser
using reflection. (There is still an issue with actually usingTypeParser
, since it uses a list of Types and the first successful call toXXXType.valueOf
determines the type returned. For example, ifPointType
appeared beforeStringType
in the list (which it would have to be), then any string that happened to be of an acceptable format toPointType
would "win", even if aStringType
was preferred.)