Example #1
0
// POST /api/sim/event/{prot}
func (app *WebApplication) simulateProtocol(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	protocol := vars["prot"]
	body, err := ioutil.ReadAll(req.Body)
	if err != nil {
		log.Println(err)
	}

	content := string(body)
	handler := app.container.ProtocolHandler(protocol)

	data := api.NewReadRequest(content)

	handler.OnRead(data)
}
Example #2
0
func (ser *SerialChannel) Start() error {
	log.Println("[INFO] Starting Serial")

	cfg := ser.GetConfiguration()
	port := cfg.Port
	baud := cfg.Baud

	c := &serial.Config{Name: port, Baud: int(baud)}
	s, err := serial.OpenPort(c)
	ser.serial = s
	if err == nil {
		buf := ""

		for {
			serialContent := make([]byte, 256)
			_, err := s.Read(serialContent)
			if err != nil {
				log.Fatal(err)
			}

			c := string(serialContent)
			if strings.Index(c, "\n") != -1 {
				str := strings.Split(c, "\n")
				buf += str[0]
				buf = strings.Replace(buf, "\x00", "", -1)

				// Iterate all protocols and call Handle for Buf
				payload := api.NewReadRequest(buf)
				for _, prot := range ser.GetProtocols() {
					go prot.OnRead(payload)
				}

				buf = str[1]
			} else {
				buf += c
			}
		}
	} else {
		log.Print("[ERROR] Unable to start Serial -- ", err)
	}
	return nil
}