Robot Not a Dev? Buy Now
Not a Dev?

Misty Community Forum

Questions About Javascript SDK

@piikoicoder I’m going to answer some of your questions here instead of your original post as others might find them useful!

Questions from Original Post:
This is from docs:

“Get” Data Callbacks

For “get” callback functions, the callback name is by default set to be _<COMMAND> (you can use your own callback name if desired). So, for example, the default callback function for GetDeviceInformation would be _GetDeviceInformation .

Then it shows this code snippit

StartMySkill();

function StartMySkill() {
misty.GetAudioList();
}

function _GetAudioList(callbackData) {
  var audioList = callbackData.Result;
if (audioList.length > 0) {
  var randomInt = Math.floor(Math.random() * audioList.length);
  var currentSound = audioList[randomInt].Name;
   misty.PlayAudio(currentSound);
  }
}

Note also that the docs acknowledge somewhere that Misty’s JavaScript SDK is different. And I can attest it is certainly alien to someone who’s accustomed to Java’s Event/Listener architecture.

So here’s the question I would have liked to share under the new category:

What the heck does (you can use your own callback name if desired). mean?

I assume the commands available in the form of Misty. are set by the API. So how, for example, would Misty.GetAudoData() know _WhatsUp() is the function to which the programmer expects it to provide with the callBackData object?

Or more basically, how do I mentally map Java’s Event handling (events fired by the system, or by external input like a button click, or fired by by the programmer) to Misty’s JavaScript architecture?

May I add that a list of all Get Commands would be nice? Actually a complete list of implemented commands would be nicer.

And maybe a simple mention at the start that Misty’s JavaScript SDK get commands do not return data. I mean if that’s what the example is supposed to convey. Because from the example I can only guess instead of returning data, Misty.getSomthing() commands callback their underscored namesakes and feed it a key/value data object. Yes? No?

Lots more areas where the docs could be made clearer … but all of them boil down to the fact that Misty’s JaveScript documentation is so submerged in ho to do “SKILLS” the drafters aren’t able to just focus on the SKD.

Or said another way, so documenters seem in so much of a hurry to get people “doing cool stuff” they aren’t able to focus documentation of the SDK o the SDK.

Something that would be infinitely more useful to people who are starting from scratch to lay down a serious foundation. Not just swap stuff that makes Misty do things … and then tinkering with code to tweak someone else’s code to sort of do what they want (picking up, and propagating, any flaws, and deviation from best practices.)

But maybe it’s only me who would like to master the SDK first.

But if the goal is to attract a mass of serious programmers from outside the robotic’s choir to pick up Misty and take it to levels as yet undreamed, I’m pretty sure the “just do this and Misty does this” approach is the wrong one.

To me that seems most attractive to making Misty an expensive toy, so I’d push harder to get Misty’s SDK more completely documented at a much higher than current level to make it easier for people from alien code to build a foundation in Misty.

Because just like a small vocabulary limits what a human can conceive of to small ideas, a limited knowledge of a language’s API limits a programmer to conceive of doing with it. At least me thinks.

I know that documentation is time-consuming, mind-numbing, difficult, but unsatisfying, which is why I assume so few people do it well. Like Oracle after it trashed Java. So I know that no matter what the argument, it won’t be done by those who develop things.

Answers!

There was a lot to unpack in there, but I broke out the ones that I can answer below!

What the heck does (you can use your own callback name if desired). mean?

Whenever you use a command that is intended to return data, Misty will by default send that data to the callback of the same name preceded by an underscore(eg misty.GetAudioLIst() by default looks for a callback in your code called _GetAudioList(data) ) . It’s important to note that if you do not have a relevant callback in your code, you won’t ever see the data being returned.

But your question is about using your own callback. That means If you wanted to have the requested data return to a method of your own, all you need to do is designate the name of your desired callback when you make the request. For example, let’s take the sample code you mentioned. If you wanted to handle the data you requested with the misty.GetAudioList(); with your own function foobar(data) instead of the default _GetAudioList(data), the only modification you would need to do is the following:

StartMySkill();

function StartMySkill() {
    misty.GetAudioList("foobar");
}

function foobar(data) {
    var audioList = data.Result;
    misty.Debug(JSON.stringify(data.Result));
    
    if (audioList.length > 0) {
        var randomInt = Math.floor(Math.random() * audioList.length);
        var currentSound = audioList[randomInt].Name;
        misty.PlayAudio(currentSound);
    }
}

The very first optional parameter is the name of your callback :slight_smile:

Your other question:
Or more basically, how do I mentally map Java’s Event handling (events fired by the system, or by external input like a button click, or fired by by the programmer) to Misty’s JavaScript architecture?

is harder for me to answer since I’m not familiar with Java’s Event handling architecture. But in essence, things that are one-off data requests (get a list of audio commands, get a picture, get running skills, etc) you always need to specify a callback to interact with the data being returned. Other things that you mention like ‘events fired by the system’ are handled by subscribing to the events you care about and creating a callback to parse data and take action (Here is the documentation on how to do this)

For example, if I wanted to know when Misty’s Bump sensors were pressed I would do the following

misty.AddReturnProperty("Bumped", "sensorName");
misty.AddReturnProperty("Bumped", "IsContacted");
misty.RegisterEvent("Bumped", "BumpSensor", 50 ,true);


function _Bumped(data) 
{
    var sensor = data.AdditionalResults[0];
    var isPressed = data.AdditionalResults[1];
	isPressed ? misty.Debug(sensor+" is Pressed") : misty.Debug(sensor+" is Released");
}

If you want more information, we have a bump sensor tutorial.

May I add that a list of all Get Commands would be nice? Actually a complete list of implemented commands would be nicer.

A list of all our implemented commands are available here, but I’m guessing you’re looking for just a single page with links to relevant documentation? Is that what you’re looking for?

Regarding your other question:

And maybe a simple mention at the start that Misty’s JavaScript SDK get commands do not return data. I mean if that’s what the example is supposed to convey. Because from the example I can only guess instead of returning data, Misty.getSomthing() commands callback their underscored namesakes and feed it a key/value data object. Yes? No?

I might be misunderstanding your question, but if you don’t specify where you want Misty to send the requested data, it will default to a callback named _<Command>(data). If your skill does not implement that callback, the skill doesn’t know what you want it to do with the data. For instance, if you want the base64 string for an image you would call misty.GetImage("Angry.png"); then implement a callback like this:

function _GetImage(data)
{
    misty.Debug(JSON.stringify(data));
}

This will just print out the base64 string of the requested image file, but if you don’t implement _GetImage() (or designate your own), nothing will happen.

Lots more areas where the docs could be make clearer … but all of them boil down to the fact that Misty’s JaveScript documentation is so submerged in ho to do “SKILLS” the drafters aren’t able to just focus on the SKD.

We would really love to hear your suggestions! Can you provide another example of how you like to dig into other SDK’s?

I hope this helps!

-Chris

1 Like

Hi Chris,

Yes your explanation helps. Thank you.

However, it raises as many questions as it answers. So I believe going forward my best strategy will be to post new topics/questions that are very narrowly focused, which I will do in the future.

However, in case anyone has the time and interest enough to keep reading, I think I’ll restate my wants. Grin … I learned long ago that just because I write it, it’s foolish to assume many/anyone will read it. But complaining is always fun anyway.

So just in case … It still seems to me that the current approach – making a single Devlopment category the repository for all questions related to Life the Universe and Everything is only great for users who can stay daily connected to the stream of consciousness … from the beginning.

But for someone who comes to Misty later, or someone who just has a specific interest … that’s not so great, because answers are submerged in a sort of Indian burial mound as fast as they are given.

Regardless … yes. I was looking for a single page list of “Events” triggered by Misty’s sensors. and separately a list of all the get commands.

The scheme seems to be that:

Events triggered by Misty’s sensors are fired whether anyone is Subscribe to?/ Register for? them or not. And each Event makes available a lot of information not all of which will be of interest.

Therefore, the programmer can not only Listen (the Java Equivalent of Subscribe) for the Event, but also select just those data items of interest. Then the selected data items are returned as a single property object … key/value pairs. Yes?

So my single page would list all Events automatically fired by Misty and for each a complete list of all data items produced by that Event from which to select. Instead of little bits and examples here … and some more over there … and some 15 topics back … etc.

Events triggered by a get command are fired when the programmer calls the command. And returned by calling a method whose name is the get command preceded by an underscore.

But my response is the same. Desire for a single page of all get commands. And for each one a complete list of all data items … i.e. all keys in the single data object it returns.

But … well that’s me … and these are not the only wishes our world has seen fit to ignore. So we go with what we have.

And again, thank you for your very helpful reply to my first post.

1 Like