Exemplo n.º 1
0
func ChartSetupPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)

	inputs := map[string]chartInput{}
	for _, name := range args.Gadget.GetDataPointSources() {
		inputs[name] = chartInput{
			Value: fmt.Sprintf("/api/gadgets/%s/sources/%s", args.Gadget.Id, name),
			Setup: fmt.Sprintf("/gadgets/%s/chart-setup/%s", args.Gadget.Id, name),
			Key:   fmt.Sprintf("%s %s", args.Gadget.Id, name),
		}
	}
	p := chartSetupPage{
		gadgetPage: gadgetPage{
			userPage: userPage{
				User:  args.User.Username,
				Admin: handlers.Admin(args),
				Links: []link{
					{"quimby", "/"},
					{args.Gadget.Name, fmt.Sprintf("/gadgets/%s", args.Gadget.Id)},
					{"chart-setup", fmt.Sprintf("/gadgets/%s/chart-setup.html", args.Gadget.Id)},
				},
			},
			Gadget: args.Gadget,
		},
		Inputs: inputs,
		Spans:  []string{"hour", "day", "week", "month"},
		Action: fmt.Sprintf("/gadgets/%s/chart.html", args.Gadget.Id),
	}
	templates["chart-setup.html"].template.ExecuteTemplate(w, "base", p)
}
Exemplo n.º 2
0
func UserChangePasswordPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)
	u := quimby.NewUser(args.Vars["username"], quimby.UserDB(args.DB))
	if err := u.Fetch(); err != nil {
		context.Set(req, "error", err)
		return
	}
	if err := req.ParseForm(); err != nil {
		context.Set(req, "error", err)
		return
	}

	u.Password = req.PostFormValue("password")
	pw := req.PostFormValue("password_confirm")
	if pw != u.Password {
		context.Set(req, "error", ErrPasswordsDoNotMatch)
		return
	}

	if _, err := u.Save(); err != nil {
		context.Set(req, "error", ErrPasswordsDoNotMatch)
		return
	}

	w.Header().Set("Location", "/admin.html")
	w.WriteHeader(http.StatusFound)
}
Exemplo n.º 3
0
func ChartPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)
	span := args.Args.Get("span")
	if span == "" {
		span = "day"
	}
	summarize := args.Args.Get("summarize")
	if summarize == "" {
		summarize = "0"
	}
	links := []link{
		{"quimby", "/"},
		{args.Gadget.Name, fmt.Sprintf("/gadgets/%s", args.Gadget.Id)},
		{"chart", fmt.Sprintf("/gadgets/%s/chart.html", args.Gadget.Id)},
	}
	if args.Args.Get("from-setup") == "true" {
		links = append(links[:2], link{"chart-setup", fmt.Sprintf("/gadgets/%s/chart-setup.html", args.Gadget.Id)}, links[2])
	}
	sources := args.Args["source"]
	p := chartPage{
		gadgetPage: gadgetPage{
			userPage: userPage{
				User:  args.User.Username,
				Admin: handlers.Admin(args),
				Links: links,
				CSS:   []string{"/css/nv.d3.css"},
			},
			Gadget: args.Gadget,
		},
		Span:      span,
		Sources:   sources,
		Summarize: summarize,
	}
	templates["chart.html"].template.ExecuteTemplate(w, "base", p)
}
Exemplo n.º 4
0
func UserTFAPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)

	u := quimby.NewUser(args.Vars["username"], quimby.UserDB(args.DB), quimby.UserTFA(handlers.TFA))
	if err := u.Fetch(); err != nil {
		context.Set(req, "error", err)
		return
	}

	qrData, err := u.UpdateTFA()
	if err != nil {
		context.Set(req, "error", err)
		return
	}

	if _, err := u.Save(); err != nil {
		context.Set(req, "error", err)
		return
	}

	qr := qrPage{
		userPage: userPage{
			User:  args.User.Username,
			Admin: handlers.Admin(args),
			Links: []link{
				{"quimby", "/"},
				{"admin", "/admin.html"},
			},
		},
		QR: template.HTMLAttr(base64.StdEncoding.EncodeToString(qrData)),
	}
	templates["qr-code.html"].template.ExecuteTemplate(w, "base", qr)
}
Exemplo n.º 5
0
func DeleteGadgetPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)
	g := &quimby.Gadget{Id: args.Vars["gadgetid"], DB: args.DB}
	if err := g.Delete(); err != nil {
		context.Set(req, "error", err)
		return
	}
	w.Header().Set("Location", "/admin.html")
	w.WriteHeader(http.StatusFound)
}
Exemplo n.º 6
0
func DeleteUserPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)
	u := quimby.NewUser(args.Vars["username"], quimby.UserDB(args.DB))
	if err := u.Delete(); err != nil {
		context.Set(req, "error", err)
		return
	}
	w.Header().Set("Location", "/admin.html")
	w.WriteHeader(http.StatusFound)
}
Exemplo n.º 7
0
func UserEditPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)
	username := args.Vars["username"]
	page := editUserPage{
		userPage: userPage{
			User:  args.User.Username,
			Admin: handlers.Admin(args),
		},
		Permissions: []string{"read", "write", "admin"},
	}
	if username == "new-user" {
		page.Links = []link{
			{"quimby", "/"},
			{"admin", "/admin.html"},
			{"new user", "/admin/users/new-user"},
		}
		page.Actions = []action{
			{Name: "cancel", URI: template.URL("/admin.html"), Method: "get"},
		}
		page.End = 0
	} else {
		u := quimby.NewUser(username, quimby.UserDB(args.DB))
		if err := u.Fetch(); err != nil {
			context.Set(req, "error", err)
			return
		}

		q := url.Values{}
		q.Add("resource", fmt.Sprintf("/admin/users/%s", username))
		q.Add("name", username)
		page.EditUser = u
		page.Links = []link{
			{"quimby", "/"},
			{"admin", "/admin.html"},
			{u.Username, fmt.Sprintf("/admin/users/%s", u.Username)},
		}
		page.Actions = []action{
			{Name: "cancel", URI: template.URL("/admin.html"), Method: "get"},
			{Name: "delete", URI: template.URL(fmt.Sprintf("/admin/confirmation?%s", q.Encode())), Method: "get"},
			{Name: "update-password", URI: template.URL(fmt.Sprintf("/admin/users/%s/password", username)), Method: "get"},
			{Name: "update-tfa", URI: template.URL(fmt.Sprintf("/admin/users/%s/tfa", username)), Method: "post"},
		}

		page.End = 3
	}

	if username == "new-user" {
		templates["new-user.html"].template.ExecuteTemplate(w, "base", page)
	} else {
		templates["edit-user.html"].template.ExecuteTemplate(w, "base", page)
	}
}
Exemplo n.º 8
0
func LinksPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)
	i := indexPage{
		userPage: userPage{
			User:  args.User.Username,
			Admin: handlers.Admin(args),
			Links: []link{
				{"quimby", "/"},
			},
		},
	}
	templates["links.html"].template.ExecuteTemplate(w, "base", i)
}
Exemplo n.º 9
0
func UserForm(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)

	err := req.ParseForm()
	if err != nil {
		context.Set(req, "error", err)
		return
	}

	username := args.Vars["username"]
	var u *quimby.User
	if username == "new-user" {
		u = quimby.NewUser(req.PostFormValue("username"), quimby.UserDB(args.DB), quimby.UserTFA(handlers.TFA))
		u.Password = req.PostFormValue("password")
		pw := req.PostFormValue("password_confirm")
		if pw != u.Password {
			context.Set(req, "error", ErrPasswordsDoNotMatch)
			return
		}
	} else {
		u = quimby.NewUser(username, quimby.UserDB(args.DB), quimby.UserTFA(handlers.TFA))
		if err := u.Fetch(); err != nil {
			context.Set(req, "error", ErrPasswordsDoNotMatch)
			return
		}
	}
	u.Permission = req.PostFormValue("permission")
	qrData, err := u.Save()
	if err != nil {
		context.Set(req, "error", err)
		return
	}
	if username == "new-user" {
		qr := qrPage{
			userPage: userPage{
				User:  args.User.Username,
				Admin: handlers.Admin(args),
				Links: []link{
					{"quimby", "/"},
					{"admin", "/admin.html"},
				},
			},
			QR: template.HTMLAttr(base64.StdEncoding.EncodeToString(qrData)),
		}
		templates["qr-code.html"].template.ExecuteTemplate(w, "base", qr)
	} else {
		w.Header().Set("Location", "/admin.html")
		w.WriteHeader(http.StatusFound)
	}
}
Exemplo n.º 10
0
func LogoutPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)
	p := editUserPage{
		userPage: userPage{
			User:  args.User.Username,
			Admin: handlers.Admin(args),
			Links: []link{
				{"quimby", "/"},
			},
		},
		Actions: []action{
			{Name: "cancel", URI: template.URL("/"), Method: "get"},
		},
	}
	templates["logout.html"].template.ExecuteTemplate(w, "base", p)
}
Exemplo n.º 11
0
func EditMethodPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)
	p := gadgetPage{
		userPage: userPage{
			User:  args.User.Username,
			Admin: handlers.Admin(args),
			Links: []link{
				{"quimby", "/"},
				{args.Gadget.Name, fmt.Sprintf("/gadgets/%s", args.Gadget.Id)},
				{"method", fmt.Sprintf("/gadgets/%s/method.html", args.Gadget.Id)},
			},
		},
		Gadget: args.Gadget,
		URI:    fmt.Sprintf("/gadgets/%s", args.Gadget.Id),
	}
	templates["edit-method.html"].template.ExecuteTemplate(w, "base", p)
}
Exemplo n.º 12
0
func IndexPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)
	g, err := quimby.GetGadgets(args.DB)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	i := indexPage{
		Gadgets: g,
		userPage: userPage{
			User:  args.User.Username,
			Admin: handlers.Admin(args),
			Links: []link{
				{"quimby", "/"},
			},
		},
	}
	templates["index.html"].template.ExecuteTemplate(w, "base", i)
}
Exemplo n.º 13
0
func DeleteConfirmPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)
	page := confirmPage{
		userPage: userPage{
			User:  args.User.Username,
			Admin: handlers.Admin(args),
			Links: []link{
				{"quimby", "/"},
				{"admin", "/admin.html"},
				{"new user", "/admin/users/new-user"},
			},
		},
		Actions: []action{
			{Name: "delete", URI: template.URL(args.Args.Get("resource")), Method: "delete"},
		},
		Resource: args.Args.Get("resource"),
		Name:     args.Args.Get("name"),
	}
	templates["delete.html"].template.ExecuteTemplate(w, "base", page)
}
Exemplo n.º 14
0
func UserPasswordPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)
	u := quimby.NewUser(args.Vars["username"])
	page := editUserPage{
		userPage: userPage{
			User:  args.User.Username,
			Admin: handlers.Admin(args),
			Links: []link{
				{"quimby", "/"},
				{"admin", "/admin.html"},
				{u.Username, fmt.Sprintf("/admin/users/%s", u.Username)},
			},
		},
		EditUser: u,
		Actions: []action{
			{Name: "cancel", URI: template.URL(fmt.Sprintf("/admin/users/%s", u.Username)), Method: "get"},
		},
	}
	templates["password.html"].template.ExecuteTemplate(w, "base", page)
}
Exemplo n.º 15
0
func AdminPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)
	gadgets, err := quimby.GetGadgets(args.DB)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	links := make([]link, len(gadgets))
	for i, g := range gadgets {
		links[i] = link{Name: g.Name, Path: fmt.Sprintf("/admin/gadgets/%s", g.Id)}
	}

	users, err := quimby.GetUsers(args.DB)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	userLinks := make([]link, len(users))
	for i, u := range users {
		userLinks[i] = link{Name: u.Username, Path: fmt.Sprintf("/admin/users/%s", u.Username)}
	}

	i := adminPage{
		userPage: userPage{
			User:  args.User.Username,
			Admin: handlers.Admin(args),
			Links: []link{
				{"quimby", "/"},
				{"admin", "/admin.html"},
			},
		},
		Gadgets: links,
		Users:   userLinks,
	}
	templates["admin.html"].template.ExecuteTemplate(w, "base", i)
}
Exemplo n.º 16
0
func ChartInputPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)
	name := args.Vars["name"]
	p := chartInputPage{
		gadgetPage: gadgetPage{
			userPage: userPage{
				User:  args.User.Username,
				Admin: handlers.Admin(args),
				Links: []link{
					{"quimby", "/"},
					{args.Gadget.Name, fmt.Sprintf("/gadgets/%s", args.Gadget.Id)},
					{"chart-setup", fmt.Sprintf("/gadgets/%s/chart-setup.html", args.Gadget.Id)},
					{name, fmt.Sprintf("/gadgets/%s/chart-setup/%s", args.Gadget.Id, name)},
				},
			},
			Gadget: args.Gadget,
		},
		Name: args.Vars["name"],
		Key:  fmt.Sprintf("%s %s", args.Gadget.Id, args.Vars["name"]),
		Back: fmt.Sprintf("/gadgets/%s/chart-setup.html", args.Gadget.Id),
	}
	templates["chart-input.html"].template.ExecuteTemplate(w, "base", p)
}
Exemplo n.º 17
0
func GadgetForm(w http.ResponseWriter, req *http.Request) {
	err := req.ParseForm()
	if err != nil {
		context.Set(req, "error", err)
		return
	}
	args := handlers.GetArgs(req)
	g := &quimby.Gadget{DB: args.DB}
	if args.Vars["gadgetid"] != "new-gadget" {
		g.Id = args.Vars["gadgetid"]
	}

	g.Host = req.PostFormValue("host")
	g.Name = req.PostFormValue("name")
	g.View = req.PostFormValue("view")
	s := req.PostFormValue("disabled")

	d := s == "on"
	g.Disabled = d
	context.Set(req, "error", g.Save())
	w.Header().Set("Location", "/admin.html")
	w.WriteHeader(http.StatusFound)
}
Exemplo n.º 18
0
func GadgetEditPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)
	id := args.Vars["gadgetid"]
	g := &quimby.Gadget{Id: id, DB: args.DB}
	if id == "new-gadget" {
		g.Id = "new-gadget"
		g.Name = "new-gadget"
	} else {
		if err := g.Fetch(); err != nil {
			context.Set(req, "error", err)
			return
		}
	}

	q := url.Values{}
	q.Add("resource", fmt.Sprintf("/admin/gadgets/%s", g.Id))
	q.Add("name", g.Name)

	p := gadgetPage{
		userPage: userPage{
			User:  args.User.Username,
			Admin: handlers.Admin(args),
			Links: []link{
				{"quimby", "/"},
				{"admin", "/admin.html"},
				{g.Name, fmt.Sprintf("/admin/gadgets/%s", g.Id)},
			},
		},
		Gadget: g,
		Actions: []action{
			{Name: "cancel", URI: template.URL("/admin.html"), Method: "get"},
			{Name: "delete", URI: template.URL(fmt.Sprintf("/admin/confirmation?%s", q.Encode())), Method: "get"},
		},
		End: 2,
	}
	templates["edit-gadget.html"].template.ExecuteTemplate(w, "base", p)
}
Exemplo n.º 19
0
func GadgetPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)
	s, err := args.Gadget.Status()

	if err != nil {
		context.Set(req, "error", err)
		return
	}

	l := map[string][]gogadgets.Message{}
	for _, msg := range s {
		if msg.Sender == "method runner" || msg.Type == "method update" || strings.HasPrefix(msg.Sender, "xbee") {
			continue
		}
		msgs, ok := l[msg.Location]
		if !ok {
			msgs = []gogadgets.Message{}
		}
		displayValues(&msg)
		msgs = append(msgs, msg)
		l[msg.Location] = msgs
	}

	p := os.Getenv("QUIMBY_PORT")
	d := os.Getenv("QUIMBY_DOMAIN")
	var u string
	if p == "443" {
		u = fmt.Sprintf("wss://%s:%s", d, p)
	} else {
		u = fmt.Sprintf("ws://%s:%s", d, p)
	}

	g := gadgetPage{
		userPage: userPage{
			User:  args.User.Username,
			Admin: handlers.Admin(args),
			Links: []link{
				{"quimby", "/"},
				{args.Gadget.Name, fmt.Sprintf("/gadgets/%s", args.Gadget.Id)},
			},
			Dropdowns: []dropdown{
				{Link: fmt.Sprintf("/gadgets/%s/method.html", args.Gadget.Id), Text: "Method"},
			},
		},
		Gadget:    args.Gadget,
		Websocket: template.URL(fmt.Sprintf("%s/api/gadgets/%s/websocket", u, args.Gadget.Id)),
		Locations: l,
		URI:       fmt.Sprintf("/gadgets/%s", args.Gadget.Id),
	}
	if args.Gadget.View == "furnace" {
		var f, t gogadgets.Message
		for _, m := range g.Locations["home"] {
			if m.Name == "furnace" {
				f = m
			} else {
				t = m
			}
		}
		var setPoint = 70.0
		if f.TargetValue != nil {
			setPoint = f.TargetValue.Value.(float64)
		}
		p := furnacePage{
			gadgetPage:  g,
			States:      []string{"heat", "cool", "off"},
			Furnace:     f,
			Thermometer: t,
			SetPoint:    setPoint,
			Usage:       getUsage(args.Gadget),
		}
		templates["furnace.html"].template.ExecuteTemplate(w, "base", p)
	} else {
		templates["gadget.html"].template.ExecuteTemplate(w, "base", g)
	}
}