Giter Site home page Giter Site logo

Comments (6)

sweetpi avatar sweetpi commented on July 23, 2024

The plugin looks great. The best would be to write a test case.

Take a look at the test for the LogActionHandler for an example: https://github.com/sweetpi/pimatic/blob/2c79db7a03ad4993ae042921bdc12a0c3f061409/test/lib-actions-test.coffee#L81

You can place your test file in pimatic-your-plugin/test/. It will be executed when you run grunt test in the pimatic directory.

I've seen you are struggling to call a function that takes a callback as argument in an action handler. You can use Q.ninvoke to convert the result of the function to a Promise: https://github.com/kriskowal/q/wiki/API-Reference#qninvokeobject-methodname-args

return Q.ninvoke(object, "functionName", argument).then(=> "function executed" )

from pimatic-plugin-template.

thexperiments avatar thexperiments commented on July 23, 2024

Thanks, but there is still quite much to do, have to wrap my head around this new programming style and languages. It is quite frustrating because most stuff won't work immediately. I will check out grunt as soon as possible.
Plugin now works for me, if you have pushover you can test it, if not it still would be great if you take a look at it and point out errors/weaknesses.
I for example was not possible to catch the error thrown when sending a notification with priority 2, I thought after ninvoking I could handle it with .fail() or .catch() but it still propagates up and crashes whole pimatic.

from pimatic-plugin-template.

sweetpi avatar sweetpi commented on July 23, 2024

I know its a steep learning curve. I've mainly done c++, Java and php development in the past. I used node.js and so on for pimatic because I always wanted to try it and I think it is very well fitted for the task. I think the most confusing thing are the promises. At least it took me a long time to get used to it.

Back to the plugin. Its looking really great, I will use it, too :)

The problem with the priority could be, that the pushover node module expects a number and not a string. So maybe use isNaN to check if priority_content is a number and if, then convert it using parseInt.

You have some unnecessary Q.fcalls:

return Q.fcall -> 
  msg =
    message: message_content
    title: title_content
    sound: default_sound
    priority: priority_content

  env.logger.debug "executeAction: we send the message"

  return Q.ninvoke(pushover_instance, "send", msg).then(->
    Q.fcall env.logger.info "pusover message sent successfully")

You just need the Q.fcall if you want to "convert a value to a promise". Since Q.ninvoke already returns a promise you can remove Q.fcall and the action handler should return a promise fullfilled with a message. So instead of logging the message your own. You can return the string and let pimatic log the message.

msg =
  message: message_content
  title: title_content
  sound: default_sound
  priority: priority_content

env.logger.debug "executeAction: we send the message"

return Q.ninvoke(pushover_instance, "send", msg).then( ->
  return "pusover message sent successfully"
)

I think I'm missing some error handling in pimatic for actions that are throwing errors. I will add it. It should be that pimatic crashed just because a action couldn't be executed.

But sureley you should be able to use .catch errors youself. I didn't test the following but I think it should work:

return Q.ninvoke(pushover_instance, "send", msg).then( ->
  return "pusover message sent successfully"
).catch( (e) ->
  env.logger.error "something went wrong: ", e
)

from pimatic-plugin-template.

thexperiments avatar thexperiments commented on July 23, 2024

Thanks for the tips. The problem with the priority is that priority 2
requires an additional timeout parameter which I currently don't send,
string/int should be no problem as it will be a string in the http request
anyway.

I think I now have understood the promise model, what I was struggling with
is the coffeescript syntax (indentions, no brackets, how to rewrite some
javascript example in coffeescript), node.js/javascript as a programming
language (how do objects, constructors etc. work) and the "framework". In
the framework I was often digging into pimatic code to find out what kind
of return value is actually expected and which function is called when.
Also regex is quite new to me but I wanted to learn it for some time anyway.

I tried the catch in a similar way but it didn't work, I think the log I
put there was logged but it still was crashing but I'm not 100% sure if I
had exactly the same syntax.

2014/1/20 sweetpi [email protected]

I know its a steep learning curve. I've mainly done c++, Java and php
development in the past. I used node.js and so on for pimatic because I
always wanted to try it and I think it is very well fitted for the task. I
think the most confusing thing are the promises. At least it took me a long
time to get used to it.

Back to the plugin. Its looking really great, I will use it, too :)

The problem with the priority could be, that the pushover node module
expects a number and not a string. So maybe use isNaN to check if
priority_content is a number and if, then convert it using parseInt.

You have some unnecessary Q.fcalls:

return Q.fcall ->
msg =
message: message_content
title: title_content
sound: default_sound
priority: priority_content

env.logger.debug "executeAction: we send the message"

return Q.ninvoke(pushover_instance, "send", msg).then(->
Q.fcall env.logger.info "pusover message sent successfully")

You just need the Q.fcall if you want to "convert a value to a promise".
Since Q.ninvoke already returns a promise you can remove Q.fcall and the
action handler should return a promise fullfilled with a message. So
instead of logging the message your own. You can return the string and let
pimatic log the message.

msg =
message: message_content
title: title_content
sound: default_sound
priority: priority_content

env.logger.debug "executeAction: we send the message"

return Q.ninvoke(pushover_instance, "send", msg).then( ->
return "pusover message sent successfully"
)

I think I'm missing some error handling in pimatic for actions that are
throwing errors. I will add it. It should be that pimatic crashed just
because a action couldn't be executed.

But sureley you should be able to use .catch errors youself. I didn't
test the following but I think it should work:

return Q.ninvoke(pushover_instance, "send", msg).then( ->
return "pusover message sent successfully"
).catch( (e) ->
env.logger.error "something went wrong: ", e
)


Reply to this email directly or view it on GitHubhttps://github.com/sweetpi/pimatic-plugin-template/issues/1#issuecomment-32739948
.

from pimatic-plugin-template.

sweetpi avatar sweetpi commented on July 23, 2024

Thanks for the tips. The problem with the priority is that priority 2
requires an additional timeout parameter which I currently don't send

Oh didn't know. But please check for NaN anyway.

I think I now have understood the promise model, what I was struggling with
is the coffeescript syntax (indentions, no brackets, how to rewrite some
javascript example in coffeescript)

Cool you are learning fast :) http://js2coffee.org/ is a good friend if you need to wrewrite node.js samples.

Also regex is quite new to me but I wanted to learn it for some time anyway.

Another great tool I can recommend: http://regexpal.com/

from pimatic-plugin-template.

thexperiments avatar thexperiments commented on July 23, 2024

For regex I found a great online tool too:
https://www.debuggex.com/
It really helps to see the regex visually to understand what is happening.

Just tried js2coffee on one thing I remembered from yesterday and ended up
with nearly exact the same string as in plain javascript, these optional
brackets confuse me, I'm thinking about always using brackets to make it
consistent because sometimes you seem to need them.

Q.ninvoke(someFunction(some_parameter)).then(doStuff).then(doMoreStuff)

2014/1/20 sweetpi [email protected]

Thanks for the tips. The problem with the priority is that priority 2
requires an additional timeout parameter which I currently don't send

Oh didn't know. But please check for NaN anyway.

I think I now have understood the promise model, what I was struggling with
is the coffeescript syntax (indentions, no brackets, how to rewrite some
javascript example in coffeescript)

Cool you are learning fast :) http://js2coffee.org/ is a good friend if
you need to wrewrite node.js samples.

Also regex is quite new to me but I wanted to learn it for some time
anyway.

Another great tool I can recommand: http://regexpal.com/


Reply to this email directly or view it on GitHubhttps://github.com/sweetpi/pimatic-plugin-template/issues/1#issuecomment-32742698
.

from pimatic-plugin-template.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.