Skip to content

Commit fc12440

Browse files
committed
Add missing data package
1 parent 2c5506f commit fc12440

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
.DS_Store
2-
data
2+
/data

netio/data/characteristic.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package data
2+
3+
// Implements json of format
4+
//
5+
// {
6+
// "aid": 0, "iid": 1, "value": 10 [, "ev": true ]
7+
// }
8+
type Characteristic struct {
9+
AccessoryId int64 `json:"aid"`
10+
Id int64 `json:"iid"`
11+
Value interface{} `json:"value"`
12+
13+
// Events property is true or false
14+
Events interface{} `json:"ev,omitempty"`
15+
}

netio/data/characteristics.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package data
2+
3+
// Implements json of format
4+
//
5+
// {
6+
// "characteristics": [
7+
// ...
8+
// ]
9+
// }
10+
type Characteristics struct {
11+
Characteristics []Characteristic `json:"characteristics"`
12+
}
13+
14+
func NewCharacteristics() *Characteristics {
15+
return &Characteristics{
16+
Characteristics: make([]Characteristic, 0),
17+
}
18+
}
19+
20+
func (r *Characteristics) AddCharacteristic(c Characteristic) {
21+
r.Characteristics = append(r.Characteristics, c)
22+
}

netio/data/characteristics_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package data
2+
3+
import (
4+
"encoding/json"
5+
"github.com/stretchr/testify/assert"
6+
"testing"
7+
)
8+
9+
func TestEventCharacteristicFromJSON(t *testing.T) {
10+
var b = []byte(`{"characteristics":[{"aid":2,"iid":13,"ev":true}]}`)
11+
var chars Characteristics
12+
err := json.Unmarshal(b, &chars)
13+
assert.Nil(t, err)
14+
assert.Equal(t, len(chars.Characteristics), 1)
15+
var char = chars.Characteristics[0]
16+
assert.Equal(t, char.AccessoryId, 2)
17+
assert.Equal(t, char.Id, 13)
18+
assert.Equal(t, char.Events, true)
19+
assert.Nil(t, char.Value)
20+
}

netio/data/doc.go

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package data provides structs to map json to objects.
2+
package data

0 commit comments

Comments
 (0)