I have started learning Java a while back and looking at doing my first advanced (for me) project. I am wanting to create a program that uses the OpenWeatherMap API to show the weather for a particular city.
User inputs name of city
City stored as variable
Call API using city variable
Store results in two varibles (City & temp)
Display results using System.out.Println(City, Temp)
I have done some reading before starting and I am seeing people saying that storing api results in variables is bad practice? Just checking I am going down the right route before starting?
Below is an example of the API result
{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":300,"main":"Drizzle","description":"light intensity drizzle","icon":"09d"}],"base":"stations","main":{"temp":280.32,"pressure":1012,"humidity":81,"temp_min":279.15,"temp_max":281.15},"visibility":10000,"wind":{"speed":4.1,"deg":80},"clouds":{"all":90},"dt":1485789600,"sys":{"type":1,"id":5091,"message":0.0103,"country":"GB","sunrise":1485762037,"sunset":1485794875},"id":2643743,"name":"London","cod":200}
Not sure why storing data in variables is bad practice. The alternative is to chain methods. e.g. instead of this:
String cityResult = callApi(city);
JSON jsonCityResult = JSON.parse(cityResult);
print(jsonCityResult.temp);
it would be:
print(JSON.parse(callApi(city)).temp);
which is difficult to read, difficult to maintain and difficult to work out what went wrong. Was it the api call or the JSON parse?
As long as the code is 'self documenting', i.e. cityResult instead of cr I think that is perfectable acceptable programming.
Related
We are using Vorto now mainly as a normalized format and are starting to look into using the mapping engine for mapping different payload formats to Vorto model as well. I more or less understand how to map functionblock properties from JSON or binary payload using xpath and the conversion functions. However, I'm not clear how to support parsing of non-fixed format binary payload using this method.
For instance we have an off the shelf LoRaWAN sensor which transmits in the following format:
<length><frame type>[<sensor-id><sensor-value>] where length is the total frame length and sensor-id (for eg temperature, humidity, battery, ...) describes how to parse the sensor-value (ie length, datatype). In one frame multiple of these readings may be present in random order.
Parsing this can be done easily in for instance loraserver.io using a small javascript function which iterates over all the bytes en returns the parsed properties. The same way will work in the Ditto payload mapping engine afaik.
However, currently I don't see how to do something similar in Vorto mapping. This is just one specific sensor example of course, but more examples exist on the market using similar dynamic payload format. I know there is already an open issue (#1535) to improve the documentation, but it would already be helpful to know if such flexible parsing would be possible using the mapping DSL.
I tried passing the raw payload as bytearray to the javascript function. In order to test this I duplicated the org.eclipse.vorto.mapping.engine.converter.binary.BinaryMappingTest#testMappingBinaryContaining2DataPoints and adapted the model to use a custom javascript function like this
evaluator.addScriptFunction(new ScriptClassFunction("extractTemperature",
"function extractTemperature(value) { " +
" print(\"parameter of type \" + typeof value + \", value = \" + value);" +
" print(value[1]);" +
"}"));
The output of this function is
parameter of type number, value = 1
undefined
Where the value 1 is the first element of the bytearray used.
So the function does not seem to receive the parameter as bytarray.
The model is configured with .withXPathStereotype("custom:extractTemperature(data)", "demo") so the payload is passed (as BinaryData) in the same way as in the testMappingBinaryContaining2DataPoints test (.withXPathStereotype("custom:convert(vorto_conversion1:byteArrayToInt(data,0,0,0,2))", "demo")). The only difference I see now is that in the testMappingBinaryContaining2DataPoints test is that the byetarray parameter is passed to a Java function instead of a javascript function. Or am I missing something?
Also, I noticed that loop keywords like for and while are not allowed in the javascript code. So even if I can access the bytearray parameter in the javascript function I see no way for now how to iterate over this.
On gitter I received following reply (together with the suggestion to move discussion to SO)
You are right. We restricted the Javascript function usage to very rudimentary set of language keywords excluding for loops as nasty stuff can be implemented there. What you could do Instead is to register a java function In your own namespace to the mapping engine. That function can hold a byte array. Later this function can be contributed to the mapping engine as a standard function to extract a certain value out for other developers to reuse.
I don't think this is solution to the problem however. As mentioned above this is just one example of an off the shelf sensor payload format, and I don't see how this can be generalized enough to include as a generic function in the mapping engine. And I don't think it should be required to implement a sensor specific conversion in Java, since (as an end-user of an IoT platform wanting to deploy a new sensor type) this is more complex to develop and deploy than a little javascript function which can be altered at runtime in the mapping spec. I see a lot of value in being able to do simple mappings in javascript, just like this can be done in for example loraserver.io and Eclipse Ditto.
I think being able to pass a byte array to javascript is a first step. Also I wonder where exactly the risk is in allowing loops in the javascript? For example Ditto also has some restrictions in the javascript sandbox (see here) but this allows loops and only prevents endless looping and recursion.
They state the following:
Using Rhino instead of Nashorn, the newer JavaScript engine shipped with Java, has the benefit that sandboxing can be applied in a better way.
Sandboxing of different payload scripts is required as Ditto is intended to be run as cloud service where multiple connections to different endpoints are managed for different tenants at the same time. This requires the isolation of each single script to avoid interference with other scripts and to protect the JVM executing the script against harmful code execution.
Would using Rhino in Vorto as well allow to control the risks you see and allow loop construct in Vorto mapping?
PS: can someone with enough SO reputation points add the tag eclipse-vorto please?
I created an issue for you request to support this in the Javascript converters: https://github.com/eclipse/vorto/issues/2029
As stated in the issue, as a current workaround, you can register your own custom converter function with Java and re-use this function across your mappings. In these java converter functions, you have all the power of the java language to convert to extract the right property from the arbitrary list.
In order to find out how to implement your own custom converter function with Java, take a look here: https://github.com/eclipse/vorto/tree/master/mapping-engine#Advanced-Usage
Since Eclipse Vorto 0.12.3 release, a fix for your request is available. With this it is possible to pass array object to javascript Converter as well as use for loops inside javascript functions. You might wanna give it a try.
See release notes https://github.com/eclipse/vorto/blob/master/docs/release-notes.md
I have a found a free API to get some data I need for my app. The thing is the values that I get are in English.
I wanted to know if there is some way to translate this strings in my language before showing them to the users.
You can translate text but it will require another API call. Not only that, but you will have to create an appropriate request object and parse a response object from your chosen API.
You have various API providers to choose from, the top Google hits being:
Google: https://cloud.google.com/translate/
Yandex: https://tech.yandex.com/translate/
But beware! Machine translation is patchy at best. The likelihood of getting odd sounding or outright wrong results, particularly for anything other than simple text, is very high.
I am new to android development but familiar with java. I want to send a request (containing coordinates of locations) to google maps api for distance matrix and later calculate the sum of distances for each point. The response from api is either in javascript or xml (i dont really know which one). How do i send the query in java and how do i extract the distances from the response?
Try using seperate classes to send and recieve data through the Google maps API. try using JSON to send aaplication data.
In order to use any thing like this (or that is owned by google etc.. ) You first need to setup a developer account at https://play.google.com/apps/publish/signup/
It does cost 25 dollars but in all honesty it's worth it as you get access to a lot of different things including the google maps api, if you're not interested in spending the $25 I would suggest looking into an alternative mapping api.
If on the other hand you will spend the money they've got some great tutorials laid out in the documentation!
A Google Maps Distance Matrix API request takes the following form:
https://maps.googleapis.com/maps/api/distancematrix/output?parameters
If you need more information, Refer to "Distance Matrix Requests".
https://developers.google.com/maps/documentation/distance-matrix/intro#DistanceMatrixRequests
You can also use the "getDistanceMatrix" that issues a distance matrix request.
https://developers.google.com/maps/documentation/javascript/3.exp/reference#DistanceMatrixService
Let's say I want to calculate the cumulative estimate of my defects. I do
double estimate = 0.0;
Double tEstimate = 0.0;
Collection<Defect> defects = project.getDefects(null);
for(Defect d : defects){
tEstimate = d.getEstimate();
if(tEstimate != null){
estimate += tEstimate;
}
}
Here each call to d.getEstimate() does a callback to the server, meaning this code runs extremely slowly. I would like to take the one-time performance hit up front and download all the info along with the Defect object, probably including getting some information I won't use, but avoid hitting the latency of a server callback during each iteration of the loop.
You are using the VersionOne Object model SDK. It does lack robustness because of the very thing you are complaining about. One of the inefficiencies is how it knows that you are requesting a list of assets but first gets all of the assets with a predetermined set of attributes such as AssetState and checks to see if it is dead asset. After this, it makes another call to get the same list of assets again but with your specified attributes. This could be remedied by applying a greedy algorithm that could grab a set a of attributes such that each member of this set is returned regardless of which attributes are requested in your .get_() method. Why? This already (sort of) happens in the Rest based VersionOne API as it stands. If the query returned all attributes, it would probably a little wasteful especially for humongous backlogs.
Anyway, the VersionOne will be deprecating the Object Model in the near future so if you plan on a lot of coding using the OM, consider this.
Here are some ways to circumvent this problem
1) Rewrite your code to use the VersionOne APIClient SDK. It has XML plumbing so that you will save you a lot of time writing your own. This is a little bit more verbose but it is more powerful, fast and efficient. The Object model is actually built upon the APIClient.
2) Rewrite your code using Java and the raw VersionOne Rest API - The requires that you understand http and the VersionOne Rest API.
3) If you cannot change from the Object model, you can mix the 2 sdks. When you need to read large amounts to data, just use APIClient code to manage that segment of the code. Kind of pointless when you can just learn the APIclient and use exclusively unless you have a huge investment in using the Object model and you can't change. The code gets mucky real fast. Not recommended.
The rest-1.v1 API endpoint exposes operations for assets, including DeepCopy. There is no client code that enumerates all of the operations, so you must first explore the asset using the meta.v1 API endpoint. Using the API Client backdoor from the Object Model, you can get to the classes that will allow you to call an operation once you know the name.
I want to create a United Kingdom(London focused) post code based application. For instance, one of the basic queries would be:
User comes and types their post code, determine the n most nearby objects to the post code they typed in.
I am using noSQL(MongoDB) and I benefit from DB native geo-spatial objects.
How do I link geo-spatial coordinates to UK postcodes?
Is this long-lat to postcode conversion the right approach?
Is there a service available to cover that part?
How do you convert from London post code to latitude and longitude
so that you can measure distance?
The mapping between UK postcodes and latitude/longtitude is accessible online on this site in form of CSV so it should be easily parsed. In case it wouldn't be available latter for any reasons I've mirrored it here.
As for your first three questions, as written in this post you can store entries like case class Zip(code, location) and perform lookup with $near operator on the second field.