示例#1
0
文件: event.go 项目: tjbx/X10Bridge
// Body returns the json body for an event response as bytes.
func Body(a *accessory.Accessory, c *characteristic.Characteristic) (*bytes.Buffer, error) {
	chars := data.NewCharacteristics()
	char := data.Characteristic{AccessoryID: a.GetID(), ID: c.GetID(), Value: c.GetValue()}
	chars.AddCharacteristic(char)

	result, err := json.Marshal(chars)
	if err != nil {
		return nil, err
	}

	var b bytes.Buffer
	b.Write(result)
	return &b, err
}
// HandleGetCharacteristics handles a get characteristic request.
func (ctr *CharacteristicController) HandleGetCharacteristics(form url.Values) (io.Reader, error) {
	var b bytes.Buffer
	aid, cid, err := ParseAccessoryAndCharacterID(form.Get("id"))
	containerChar := ctr.GetCharacteristic(aid, cid)
	if containerChar == nil {
		log.Printf("[WARN] No characteristic found with aid %d and iid %d\n", aid, cid)
		return &b, nil
	}

	chars := data.NewCharacteristics()
	char := data.Characteristic{AccessoryID: aid, ID: cid, Value: containerChar.GetValue(), Events: containerChar.EventsEnabled()}
	chars.AddCharacteristic(char)

	result, err := json.Marshal(chars)
	if err != nil {
		log.Println("[ERRO]", err)
	}

	b.Write(result)
	return &b, err
}