gosenml

package module
v0.0.0-...-58b3dba Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2016 License: MIT Imports: 2 Imported by: 1

README

GoSenML

Go (golang) library for SenML

TODO

  • XML encoder
  • Binary (exi/cbor) encoder
  • Message 'compaction'
  • Proper message validation

Example

package main

import (
	"fmt"
	"github.com/krylovsk/gosenml"
)

func main() {
	e := gosenml.Entry{
		Name:  "sensor1",
		Units: "degC",
	}
	v := 42.0
	e.Value = &v

	m1 := gosenml.NewMessage(e)
	m1.BaseName = "http://example.com/"

	encoder := gosenml.NewJSONEncoder()
	decoder := gosenml.NewJSONDecoder()

	b, _ := encoder.EncodeMessage(m1)
	fmt.Println(string(b))

	m2, _ := decoder.DecodeMessage(b)

	m3 := m2.Expand()
	b, _ = encoder.EncodeMessage(&m3)
	fmt.Println(string(b))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder interface {
	DecodeEntry([]byte) (Entry, error)
	DecodeMessage([]byte) (Message, error)
}

Decoder interface

type Encoder

type Encoder interface {
	EncodeMessage(*Message) ([]byte, error)
	EncodeEntry(*Entry) ([]byte, error)
}

Encoder interface

type Entry

type Entry struct {
	Name         string   `json:"n,omitempty"`
	Units        string   `json:"u,omitempty"`
	Value        *float64 `json:"v,omitempty"`
	StringValue  *string  `json:"sv,omitempty"`
	BooleanValue *bool    `json:"bv,omitempty"`
	Sum          *float64 `json:"s,omitempty"`
	Time         int64    `json:"t,omitempty"`
	UpdateTime   int64    `json:"ut,omitempty"`
}

Entry is a measurement of Parameter Entry

type JSONDecoder

type JSONDecoder struct{}

JSONDecoder decodes SenML messages from JSON

func NewJSONDecoder

func NewJSONDecoder() *JSONDecoder

NewJSONDecoder returns a new JSONDecoder

func (*JSONDecoder) DecodeEntry

func (jd *JSONDecoder) DecodeEntry(data []byte) (Entry, error)

DecodeEntry decodes a SenML entry from JSON

func (*JSONDecoder) DecodeMessage

func (jd *JSONDecoder) DecodeMessage(data []byte) (Message, error)

DecodeMessage decodes a SenML messages from JSON

type JSONEncoder

type JSONEncoder struct{}

JSONEncoder encodes SenML messages to JSON

func NewJSONEncoder

func NewJSONEncoder() *JSONEncoder

NewJSONEncoder returns a new JSONEncoder

func (*JSONEncoder) EncodeEntry

func (je *JSONEncoder) EncodeEntry(e *Entry) ([]byte, error)

EncodeEntry encodes a SenML entry to JSON

func (*JSONEncoder) EncodeMessage

func (je *JSONEncoder) EncodeMessage(m *Message) ([]byte, error)

EncodeMessage encodes a SenML message to JSON

type Message

type Message struct {
	BaseName  string  `json:"bn,omitempty"`
	BaseTime  int64   `json:"bt,omitempty"`
	BaseUnits string  `json:"bu,omitempty"`
	Version   int     `json:"ver"`
	Entries   []Entry `json:"e"`
}

Message is the root SenML variable

func NewMessage

func NewMessage(entries ...Entry) *Message

NewMessage creates a SenML message from a number of entries

func (*Message) Compact

func (m *Message) Compact() Message

Compact returns a copy of the message with all Entries compacted (common data put into Message)

func (*Message) Expand

func (m *Message) Expand() Message

Expand returns a copy of the message with all Entries expanded ("self-contained")

func (*Message) Validate

func (m *Message) Validate() error

Validate validates a message

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL