使用Gobot.io和Go的Sleepy RESTful框架执行SparkCore函数。

huangapple go评论86阅读模式
英文:

Execute SparkCore function using Gobot.io and sleepy RESTful Framework for Go

问题

我有一段代码,其中我使用了Go语言的RESTful框架sleepy

我可以成功启动服务:http://localhost:3000,但是当我尝试访问http://localhost:3000/temperature时,我期望我的SparkCore函数dht被执行。

我使用Gobot.io Spark平台来执行这个函数,基于这个示例,我已经在我的代码中实现了它。

问题是代码无法通过Get()函数中的gobot.Start()方法,所以我无法返回result数据。

我设置了data的值,希望我可以这样做:

return 200, data, http.Header{"Content-type": {"application/json"}}

但是由于gobot.Start()的原因,它从未被调用。

我对Go语言非常陌生,所以非常感谢任何帮助。

package main

import (
    "net/url"
    "net/http"
    "fmt"
    "github.com/dougblack/sleepy"
    "github.com/hybridgroup/gobot"
    "github.com/hybridgroup/gobot/platforms/spark"
)

var gbot = gobot.NewGobot()
var sparkCore = spark.NewSparkCoreAdaptor("spark", "device_id", "auth_token")

type Temperature struct {}
func (temperature Temperature) Get(values url.Values, headers http.Header) (int, interface{}, http.Header) {
    work := func() {
        if result, err := sparkCore.Function("dht", ""); err != nil {
            fmt.Println(err)
        } else {
            data := map[string]string{"Temperature": result}
            fmt.Println("result from \"dht\":", result)
        }
    }

    robot := gobot.NewRobot("spark",
        []gobot.Connection{sparkCore},
        work,
    )

    gbot.AddRobot(robot)
    gbot.Start()

    return 200, data, http.Header{"Content-type": {"application/json"}}
}

func main() {
    api := sleepy.NewAPI()

    temperatureResource := new(Temperature)
    api.AddResource(temperatureResource, "/temperature")

    fmt.Println("Listening on http://localhost:3000/")
    api.Start(3000)
}
英文:

I have the following bit of code where I'm using the RESTful framework for Go called sleepy.

I can successfully start the service at: http://localhost:3000, however when I try to access http://localhost:3000/temperature I'm expecting my SparkCore function dht to execute.

I'm using the Gobot.io Spark platform to execute this function based on this example, which I've implemented in my own code.

The problem is that the code doesn't get past the gobot.Start() method inside the Get() function so I can't actually return the result data.

I'm setting the data value hoping that I can do the:

return 200, data, http.Header{"Content-type": {"application/json"}}

But it never gets called becuase of the gobot.Start().

I'm very new to Go so any help would be greatly appreciated.

package main

import (
    "net/url"
    "net/http"
    "fmt"
    "github.com/dougblack/sleepy"
    "github.com/hybridgroup/gobot"
    "github.com/hybridgroup/gobot/platforms/spark"
)

var gbot = gobot.NewGobot()
var sparkCore = spark.NewSparkCoreAdaptor("spark", "device_id", "auth_token")

type Temperature struct {}
func (temperature Temperature) Get(values url.Values, headers http.Header) (int, interface{}, http.Header) {
    work := func() {
        if result, err := sparkCore.Function("dht", ""); err != nil {
            fmt.Println(err)
        } else {
            data := map[string]string{"Temperature": result}
            fmt.Println("result from \"dht\":", result)
        }
    }

    robot := gobot.NewRobot("spark",
        []gobot.Connection{sparkCore},
        work,
    )

    gbot.AddRobot(robot)
    gbot.Start()

    return 200, data, http.Header{"Content-type": {"application/json"}}
}

func main() {
    api := sleepy.NewAPI()

    temperatureResource := new(Temperature)
    api.AddResource(temperatureResource, "/temperature")

    fmt.Println("Listening on http://localhost:3000/")
    api.Start(3000)
}

答案1

得分: 2

gbot.Start() 是一个阻塞调用。

在这个上下文中,你应该这样调用它:

go gbot.Start()

这将在一个 goroutine(类似于线程)中启动它,然后让你的应用程序继续执行。

当你查看gobot示例应用程序时,它们不在后台运行,因为它是主函数。如果主函数在后台运行并且不等待任何东西,应用程序会立即退出,没有明显的效果。

英文:

gbot.Start() is a blocking call.

In this context, you are expected to call it as:

go gbot.Start() 

This will launch it in a goroutine (think thread) and then let your app continue.

When you look at the gobot example app, they don't run in the background since it is the main function. If main runs everything in the background and doesn't wait for anything, the app exits immediately with no apparent effect.

huangapple
  • 本文由 发表于 2015年3月25日 12:06:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/29247254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定