Beispiel #1
0
func UpdateGadget(w http.ResponseWriter, req *http.Request) {
	args := GetArgs(req)
	var g quimby.Gadget
	dec := json.NewDecoder(req.Body)
	err := dec.Decode(&g)
	if err != nil {
		context.Set(req, "error", err)
		return // err
	}

	g.DB = args.DB
	g.Id = args.Vars["id"]
	err = g.Save()
	if err != nil {
		context.Set(req, "error", err)
		return // err
	}

	u, err := url.Parse(fmt.Sprintf("/api/gadgets/%s", g.Id))
	if err != nil {
		context.Set(req, "error", err)
		return //err
	}
	w.Header().Set("Location", u.String())
}
Beispiel #2
0
func AddGadget(g *quimby.Gadget) {
	if g.Name == "" {
		fmt.Print("name: ")
		fmt.Scanf("%s\n", &g.Name)
		fmt.Print("host: ")
		fmt.Scanf("%s\n", &g.Host)
		fmt.Print(fmt.Sprintf("really save gadget (name: %s, host: %s)? (Y/n) ", g.Name, g.Host))
		var save string
		fmt.Scanf("%s\n", &save)
		if save == "y" || save == "Y" || save == "" {
			fmt.Println(g.Save())
		} else {
			fmt.Println("not saving")
		}
	} else {
		err := g.Save()
		if err != nil {
			log.Fatal(err)
		}
	}
	fmt.Println(g.Id)
}
Beispiel #3
0
func getOnTime(name string, period time.Duration, g *quimby.Gadget) string {
	e := time.Now()
	s := e.Add(period)
	data, err := g.GetDataPoints(name, s, e, 0, true)
	if err != nil {
		return "0hr"
	}

	var d time.Duration
	var prev quimby.DataPoint
	end := len(data) - 1
	for i, p := range data {
		if p.Value > 0.5 && prev.Value < 0.5 {
			prev = p
		} else if p.Value < 0.5 && prev.Value > 0.5 {
			d += p.Time.Sub(prev.Time)
			prev = p
		} else if i == end && p.Value > 0.5 && prev.Value > 0.5 {
			d += p.Time.Sub(prev.Time)
		}
	}
	return parseDuration(d)
}
Beispiel #4
0
	"time"

	"github.com/boltdb/bolt"
	"github.com/cswank/gogadgets"
	"github.com/cswank/quimby"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Gadgets", func() {
	var (
		g       *quimby.Gadget
		dir     string
		pth     string
		db      *bolt.DB
		ts      *httptest.Server
		msgs    []gogadgets.Message
		clients []map[string]string
	)

	BeforeEach(func() {
		msgs = []gogadgets.Message{}
		clients = []map[string]string{}

		ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			if r.Method == "GET" {
				fmt.Fprintln(
					w,
					`{
  "back garden": {