Exemplo n.º 1
0
func TestData(t *testing.T) {
	payloadHead := DataHead{
		Flag:      0,
		Timestamp: uint64(time.Now().Unix() * 1000),
	}
	param1 := []interface{}{uint32(3), float32(1.2), int64(10)}
	params1, err := tlv.MakeTLVs(param1)
	if err != nil {
		t.Fatal(err)
	}
	sub1 := SubData{
		Head: SubDataHead{
			SubDeviceid: uint16(1),
			PropertyNum: uint16(1),
			ParamsCount: uint16(len(params1)),
		},
		Params: params1,
	}
	param2 := []interface{}{uint32(4), int64(11)}
	params2, err := tlv.MakeTLVs(param2)
	if err != nil {
		t.Fatal(err)
	}
	sub2 := SubData{
		Head: SubDataHead{
			SubDeviceid: uint16(1),
			PropertyNum: uint16(2),
			ParamsCount: uint16(len(params2)),
		},
		Params: params2,
	}

	payload := &Data{
		Head:    payloadHead,
		SubData: []SubData{},
	}
	payload.SubData = append(payload.SubData, sub1)
	payload.SubData = append(payload.SubData, sub2)

	buf, err := payload.Marshal()
	if err != nil {
		if err != nil {
			t.Error(err)
		}
	}

	payload2 := &Data{}

	err = payload2.UnMarshal(buf)
	if err != nil {
		if err != nil {
			t.Error(err)
		}
	}

	if !reflect.DeepEqual(payload, payload2) {
		t.Errorf("test data payload failed, want %v, got %v", payload, payload2)
	}
}
Exemplo n.º 2
0
func testEvent(c *ProductConfig, t *testing.T) {
	want := `{"alarm":["test"]}`

	testev := &protocol.Event{}
	testev.Head.No = 1
	testev.Head.SubDeviceid = 1
	params, err := tlv.MakeTLVs([]interface{}{"test"})
	if err != nil {
		t.Error(err)
	}
	testev.Params = params

	m, err := c.EventToMap(testev)
	if err != nil {
		t.Error(err)
	}

	result, err := json.Marshal(m)
	if err != nil {
		t.Error(err)
	}

	got := string(result)

	if got != want {
		t.Errorf("event to map error: want: %v, got : %v", want, got)
	}

}
Exemplo n.º 3
0
func (d *Device) reportEvent(client *MQTT.Client) {
	for {
		time.Sleep(3 * time.Second)

		event := protocol.Event{}

		params, err := tlv.MakeTLVs([]interface{}{"hello event."})
		if err != nil {
			fmt.Println(err)
			return
		}

		event.Params = params
		event.Head.No = 1
		event.Head.SubDeviceid = 1
		event.Head.ParamsCount = uint16(len(params))

		payload, err := event.Marshal()
		if err != nil {
			fmt.Println(err)
			return
		}

		client.Publish("e", 1, false, payload)

	}
}
Exemplo n.º 4
0
func (config *ProductConfig) MapToStatus(data map[string]interface{}) ([]protocol.SubData, error) {
	result := []protocol.SubData{}

	for label, one := range data {
		params, ok := one.([]interface{})
		if !ok {
			return nil, fmt.Errorf("status format error: %v", one)
		}
		obj, realParams, err := config.ValidateStatus(label, params)
		if err != nil {
			return nil, err
		}

		tlvs, err := tlv.MakeTLVs(realParams)
		if err != nil {
			return nil, err
		}

		result = append(result, protocol.SubData{
			Head: protocol.SubDataHead{
				SubDeviceid: uint16(obj.Part),
				PropertyNum: uint16(obj.No),
				ParamsCount: uint16(len(realParams)),
			},
			Params: tlvs,
		})
	}

	return result, nil
}
Exemplo n.º 5
0
func (config *ProductConfig) MapToCommand(cmd map[string]interface{}) (*protocol.Command, error) {
	result := &protocol.Command{}

	for name, one := range cmd {
		params, ok := one.([]interface{})
		if !ok {
			return nil, fmt.Errorf("command format error: %v", one)
		}

		c, realParams, err := config.ValidateCommandOrEvent(name, params, "command")
		if err != nil {
			return nil, err
		}

		tlvs, err := tlv.MakeTLVs(realParams)
		if err != nil {
			return nil, err
		}

		result.Head.No = uint16(c.No)
		result.Head.Priority = uint16(c.Priority)
		result.Head.SubDeviceid = uint16(c.Part)
		result.Head.ParamsCount = uint16(len(realParams))
		result.Params = tlvs

	}

	return result, nil
}
Exemplo n.º 6
0
func TestRecorder(t *testing.T) {
	r, err := NewRecorder("localhost", "pandocloud", "commands")
	if err != nil {
		t.Fatal(err)
	}

	tlvs, err := tlv.MakeTLVs([]interface{}{float64(0.1), int64(100), uint64(200)})
	if err != nil {
		t.Error(err)
	}

	deviceid := uint64(12345)
	timestamp := uint64(time.Now().Unix() * 1000)

	subdata := protocol.SubData{
		Head:   protocol.SubDataHead{1, 2, 3},
		Params: tlvs,
	}

	subdatas := []protocol.SubData{}

	subdatas = append(subdatas, subdata)

	data := rpcs.ArgsPutData{
		DeviceId:  deviceid,
		Timestamp: timestamp,
		Subdata:   subdatas,
	}

	err = r.Insert(data)
	if err != nil {
		t.Error(err)
	}

	readData := rpcs.ArgsPutData{}
	err = r.FindLatest(deviceid, &readData)

	if err != nil {
		t.Error(err)
	}

	if !reflect.DeepEqual(data, readData) {
		t.Errorf("read data want %v, but got %v", data, readData)
	}

	readDatas := []rpcs.ArgsPutData{}
	err = r.FindByTimestamp(deviceid, timestamp, timestamp, &readDatas)
	t.Log(readDatas)
	if !reflect.DeepEqual(data, readDatas[0]) {
		t.Errorf("read data by timestamp want %v, but got %v", data, readDatas[0])
	}
}
Exemplo n.º 7
0
func testCommand(c *ProductConfig, t *testing.T) {
	input := `{"switch":[1,2]}`

	v := make(map[string]interface{})
	err := json.Unmarshal([]byte(input), &v)
	if err != nil {
		t.Fatal(err)
	}
	params, err := tlv.MakeTLVs([]interface{}{uint8(1), uint8(2)})
	want := &protocol.Command{}
	want.Head.No = 1
	want.Head.SubDeviceid = 1
	want.Head.ParamsCount = 2
	want.Params = params

	got, err := c.MapToCommand(v)

	if !reflect.DeepEqual(want, got) {
		t.Errorf("map to command error: want: %v, got %v", want, got)
	}
}
Exemplo n.º 8
0
func TestCommand(t *testing.T) {
	param := []interface{}{uint32(1), float32(3.2), []byte{'1', '2'}}
	params, err := tlv.MakeTLVs(param)
	if err != nil {
		t.Fatal(err)
	}

	payloadHead := CommandEventHead{
		Flag:        0,
		Timestamp:   uint64(time.Now().Unix()) * 1000,
		SubDeviceid: uint16(2),
		No:          uint16(12),
		Priority:    uint16(1),
		ParamsCount: uint16(len(param)),
	}
	payload := &Command{
		Head:   payloadHead,
		Params: params,
	}

	buf, err := payload.Marshal()
	if err != nil {
		if err != nil {
			t.Error(err)
		}
	}

	payload2 := &Command{}

	err = payload2.UnMarshal(buf)
	if err != nil {
		if err != nil {
			t.Error(err)
		}
	}

	if !reflect.DeepEqual(payload, payload2) {
		t.Errorf("test command payload failed, want %v, got %v", payload, payload2)
	}
}
Exemplo n.º 9
0
func (d *Device) reportStatus(client *MQTT.Client) {

	payloadHead := protocol.DataHead{
		Flag:      0,
		Timestamp: uint64(time.Now().Unix() * 1000),
	}
	param := []interface{}{uint8(1)}
	params, err := tlv.MakeTLVs(param)
	if err != nil {
		fmt.Println(err)
		return
	}
	sub := protocol.SubData{
		Head: protocol.SubDataHead{
			SubDeviceid: uint16(1),
			PropertyNum: uint16(1),
			ParamsCount: uint16(len(params)),
		},
		Params: params,
	}

	status := protocol.Data{
		Head:    payloadHead,
		SubData: []protocol.SubData{},
	}

	status.SubData = append(status.SubData, sub)

	payload, err := status.Marshal()
	if err != nil {
		fmt.Println(err)
		return
	}

	client.Publish("s", 1, false, payload)

}