Ejemplo n.º 1
0
func serializeGowebResponse(
	c context.Context,
	syntaxName string,
	statements chan *goraptor.Statement) error {
	var str string
	if syntaxName == "trig" {
		// real trig mode is crashing

		serializer := goraptor.NewSerializer("ntriples")
		defer serializer.Free()
		ntriples, err := serializer.Serialize(statements, "")
		if err != nil {
			panic(err)
		}
		log.Printf("got %d bytes of ntriples", len(ntriples))
		str = "<http://projects.bigasterisk.com/room/laundryDoor> { " + ntriples + "}"
		log.Printf("str now %d bytes", len(str))
	} else {
		serializer := goraptor.NewSerializer(syntaxName)
		defer serializer.Free()

		var err error
		str, err = serializer.Serialize(statements, "")
		if err != nil {
			panic(err)
		}
	}

	c.HttpResponseWriter().Header().Set("Content-Type",
		goraptor.SerializerSyntax[syntaxName].MimeType)
	return goweb.Respond.With(c, 200, []byte(str))
}
Ejemplo n.º 2
0
func main() {
	pins := SetupIo()

	goweb.MapStatic("/static", "static")

	// this one needs to fail if the hardware is broken in
	// any way that we can determine, though I'm not sure
	// what that will mean on rpi
	goweb.MapStaticFile("/", "index.html")

	goweb.Map("GET", "/status", func(c context.Context) error {
		jsonEncode := json.NewEncoder(c.HttpResponseWriter())
		jsonEncode.Encode(map[string]interface{}{
			"motion":     pins.GetMotion(),
			"switch1":    pins.GetSwitch("1"),
			"switch2":    pins.GetSwitch("2"),
			"switch3":    pins.GetSwitch("3"),
			"doorClosed": pins.GetDoor(),
			"led":        pins.LastOutLed,
			"strike":     pins.LastOutStrike,
		})
		return nil
	})

	goweb.Map("GET", "/trig", func(c context.Context) error {
		statements := make(chan *goraptor.Statement, 100)
		close(statements)
		serializer := goraptor.NewSerializer("trig")
		defer serializer.Free()

		str, err := serializer.Serialize(statements, "")
		if err != nil {
			panic(err)
		}
		return goweb.Respond.With(c, 200, []byte(str))
	})

	goweb.Map("GET", "/graph", func(c context.Context) error {
		DC := namespace("http://purl.org/dc/terms/")
		ROOM := namespace("http://projects.bigasterisk.com/room/")

		statements := make(chan *goraptor.Statement, 100)

		graph := ROOM("laundryDoor")

		_, thisFile, _, _ := runtime.Caller(0)
		statements <- &(goraptor.Statement{
			graph, DC("creator"), literal(thisFile, nil), graph})
		statements <- &(goraptor.Statement{
			graph, DC("modified"), nowLiteral(), graph})

		for subj, state := range map[*goraptor.Uri]*goraptor.Uri{
			ROOM("laundryDoorMotion"):  ROOM(pins.GetMotion()),
			ROOM("laundryDoorOpen"):    ROOM(pins.GetDoor()),
			ROOM("laundryDoorSwitch1"): ROOM(pins.GetSwitch("1")),
			ROOM("laundryDoorSwitch2"): ROOM(pins.GetSwitch("2")),
			ROOM("laundryDoorSwitch3"): ROOM(pins.GetSwitch("3")),
			ROOM("laundryDoorLed"):     ROOM(pins.GetLed()),
			ROOM("laundryDoorStrike"):  ROOM(pins.GetStrike()),
		} {
			statements <- &(goraptor.Statement{subj, ROOM("state"), state, graph})
		}

		close(statements)
		return serializeGowebResponse(c, "nquads", statements)
	})

	goweb.Map("PUT", "/led", func(c context.Context) error {
		body, err := c.RequestBody()
		if err != nil {
			panic(err)
		}

		pins.SetLed(string(body))
		return goweb.Respond.WithStatusText(c, http.StatusAccepted)
	})

	goweb.Map("PUT", "/strike", func(c context.Context) error {
		body, err := c.RequestBody()
		if err != nil {
			panic(err)
		}

		pins.SetStrike(string(body))
		return goweb.Respond.WithStatusText(c, http.StatusAccepted)
	})

	goweb.Map(
		"PUT", "/strike/temporaryUnlock",
		func(c context.Context) error {
			type TemporaryUnlockRequest struct {
				Seconds float64
			}

			var req TemporaryUnlockRequest
			err := json.NewDecoder(c.HttpRequest().Body).
				Decode(&req)
			if err != nil {
				panic(err)
			}

			// This is not correctly reentrant. There should be a
			// stack of temporary effects that unpop correctly,
			// and status should show you any running effects.
			pins.SetStrike("unlocked")
			go func() {
				time.Sleep(time.Duration(req.Seconds *
					float64(time.Second)))
				pins.SetStrike("locked")
			}()
			return goweb.Respond.WithStatusText(
				c, http.StatusAccepted)
		})

	goweb.Map("PUT", "/speaker/beep", func(c context.Context) error {
		// queue a beep
		return goweb.Respond.WithStatusText(c, http.StatusAccepted)
	})

	// start input posting loop. add nquads to reasoning2

	address := ":8081"

	s := &http.Server{
		Addr:         address,
		Handler:      goweb.DefaultHttpHandler(),
		ReadTimeout:  10 * time.Second,
		WriteTimeout: 10 * time.Second,
	}

	log.Printf("Listening on port %s", address)
	log.Printf("%s", goweb.DefaultHttpHandler())
	listener, listenErr := net.Listen("tcp", address)
	if listenErr != nil {
		panic(listenErr)
	}
	s.Serve(listener)
}