Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

Fixes for PointType; added distanceFrom(otherPoint) #3410

Merged
merged 1 commit into from Nov 17, 2015

Conversation

watou
Copy link
Contributor

@watou watou commented Nov 12, 2015

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 of PointType, 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?
  • The 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 the HSBType ComplexType, the string is now sanity checked in the constructor, and IllegalArgumentExceptions are thrown if the string is not in the valid "lat,long[,alt]" format.
  • The valueOf method must be static, since it's expected to be called in org.openhab.core.types.TypeParser using reflection. (There is still an issue with actually using TypeParser, since it uses a list of Types and the first successful call to XXXType.valueOf determines the type returned. For example, if PointType appeared before StringType in the list (which it would have to be), then any string that happened to be of an acceptable format to PointType would "win", even if a StringType was preferred.)

@watou watou changed the title Fix PointType ctor and valueOf method. Fixes for PointType; added distanceFrom(otherPoint) Nov 13, 2015
@clinique
Copy link
Contributor

@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.
For toString(), very fine with this.

@@ -113,6 +118,24 @@ public DecimalType getGravity() {
}

/**
* Return the distance in meters from otherPoint, ignoring altitude. This algorithm also
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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!

@watou
Copy link
Contributor Author

watou commented Nov 14, 2015

@clinique I don't see a distanceFrom method in the ESH version, but I will add an issue to add it there. I did see that valueOf was fixed there already, and I am taking the toString() method from ESH back to OH1 so they are the same (optional output of altitude if it's non-zero).

@watou watou added the esh label Nov 14, 2015
@watou
Copy link
Contributor Author

watou commented Nov 14, 2015

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 PointType, but you may have more background knowledge about why having it as a method of the item would make more sense. Also, the PointType class has a getGravity method, so it seemed to be more natural to put distanceFrom there as well. Regards, John

@clinique
Copy link
Contributor

Yes, because gravity only depends on the PointType itself

@watou
Copy link
Contributor Author

watou commented Nov 14, 2015

I would argue that distanceFrom also only depends on the PointType itself (specifically, this and another instance of PointType only). The implementation also relies on the WGS84_a constant (equatorial radius) as does getGravity. So I would ask @kaikreuzer and @teichsta to tell me what is a good way to think about it. Sorry for duplicating your work, @clinique, but also the process will at least educate me about what the architects' thinking is as well. Regards, John

@teichsta teichsta added this to the 1.8.0 milestone Nov 14, 2015
@teichsta
Copy link
Member

So I would ask @kaikreuzer and @teichsta to tell me what is a good way to think about it.

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?

@kaikreuzer
Copy link
Member

Yes, I can see @watou point here and would agree that this is probably the better place.
I am not sure if anybody is using this method on the LocationItem in ESH yet - if so, moving the method would break this. But if we decide to move it, then it should be done rather sooner than later.
@clinique would you be ok with this as well or do you have any good arguments why it should stay on the item?

@clinique
Copy link
Contributor

@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 :)

@watou
Copy link
Contributor Author

watou commented Nov 15, 2015

Thank you, @clinique. I changed LocationItem.distanceFrom(PointType) to LocationItem.distanceFrom(LocationItem), and it delegates to PointType.distanceFrom(PointType). Also updated the LocationItemTest but didn't add PointTypeTest as it's redundant. This is a breaking change for any OH1.7 users who might be using it, but with an easy fix. Likewise for any ESH users when a matching code change is made there. This PR will also allow any REST API clients to use PointType states against 1.8, since it's toString() method now matches the ESH version.

I can set up an ESH development environment and submit a PR there, or I could leave it someone else. Whatever you think.

@clinique
Copy link
Contributor

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.
@watou
Copy link
Contributor Author

watou commented Nov 15, 2015

I think this PR is ready for 1.8, so please push a matching PR in ESH and thank you again, @clinique .

@watou
Copy link
Contributor Author

watou commented Nov 15, 2015

The ESH bug I opened is 482188.

@teichsta
Copy link
Member

@watou once the ESH PR is merged please go ahead and merge this PR as well. Thanks, Thomas E.-E.

@watou
Copy link
Contributor Author

watou commented Nov 16, 2015

OK, will do. Thanks.

@kaikreuzer
Copy link
Member

eclipse-archived/smarthome#520 has been merged. Thanks!

watou added a commit that referenced this pull request Nov 17, 2015
Fixes for PointType; added distanceFrom(otherPoint)
@watou watou merged commit 79faa5b into openhab:master Nov 17, 2015
@watou watou deleted the point-type-errors branch November 17, 2015 08:26
@watou watou self-assigned this Nov 17, 2015
@teichsta
Copy link
Member

Thanks @watou!

lewie added a commit to lewie/openhab that referenced this pull request Mar 28, 2018
lewie added a commit to lewie/openhab that referenced this pull request Mar 29, 2018
Signed-off-by: lewie <helmut.lehmeyer@gmail.com>
9037568 pushed a commit that referenced this pull request Mar 29, 2018
* mapping 9.007 to PercentType, sync for KNX2 PR #3410

Signed-off-by: lewie <helmut.lehmeyer@gmail.com>

* updated tests for #5277

added @author and deleted unneeded imports in MANIFEST.MF

Signed-off-by: lewie <helmut.lehmeyer@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants