lib

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2022 License: MIT Imports: 11 Imported by: 0

README

Saves TIC events to TimescaleDB

Testing

podman-compose up -d
go run cli/main.go process
declare -a fields=(IINST IINST1 IINST2 IINST3 PAPP BASE HCHP HCHC)
while sleep 1; do
    value=$((1 + RANDOM % 100))
    field=${fields[1 + $((RANDOM % ${#fields[@]}))]}
    echo "{\"ts\":$EPOCHSECONDS,\"val\":\"$(printf %03d $value)\"}" | pub -broker mqtt://localhost:1883 -topic esp-tic/status/tic/$field -username dev -password secret -qos 1
done

Documentation

Overview

Copyright © 2022 Nicolas MASSE

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

View Source
const (
	MQTT_QOS_0 = 0 // QoS 1
	MQTT_QOS_1 = 1 // QoS 2
	MQTT_QOS_2 = 2 // QoS 3
)

Those flags define the MQTT Quality of Service (QoS) levels

View Source
const (
	// How many in-flight MQTT messages to buffer
	MESSAGE_CHANNEL_LENGTH = 10

	// SQL Query to store current data
	UpsertCurrentQuery string = `
	INSERT INTO current VALUES ($1, $2, $3)
	ON CONFLICT (timestamp, phase) DO UPDATE
    SET current = excluded.current`

	// SQL Query to store power data
	UpsertPowerQuery string = `
	INSERT INTO power VALUES ($1, $2)
	ON CONFLICT (timestamp) DO UPDATE
    SET power = excluded.power`

	// SQL Query to store energy data
	UpsertEnergyQuery string = `
	INSERT INTO energy VALUES ($1, $2, $3)
	ON CONFLICT (timestamp, tariff) DO UPDATE
    SET reading = excluded.reading`
)

Variables

View Source
var SqlMigrationFS embed.FS

SqlMigrationFS stores a list of database schema migration scripts

Functions

func MigrateDb

func MigrateDb(db *sql.DB) error

MigrateDb migrates the provided database to the most recent schema

func NewMqttClient

func NewMqttClient(config MqttConfig) (mqtt.Client, error)

NewMqttClient creates a new MQTT client and connects to the broker

func SetMqttLogger

func SetMqttLogger(logger *log.Logger)

SetMqttLogger sets the logger to be used by the underlying MQTT library

Types

type MqttConfig

type MqttConfig struct {
	BrokerURL   string        // broker url (tcp://hostname:port or ssl://hostname:port)
	Username    string        // username (optional)
	Password    string        // password (optional)
	ClientID    string        // MQTT ClientID
	Timeout     time.Duration // how much time to wait for connect and subscribe operations to complete
	GracePeriod time.Duration // how much time to wait for the disconnect operation to complete
}

An MqttConfig represents the required information to connect to an MQTT broker.

type Processor

type Processor struct {
	Config ProcessorConfig // the configuration
	// contains filtered or unexported fields
}

A Processor receives events from the MQTT broker and saves data to the database

func NewProcessor

func NewProcessor(c ProcessorConfig) *Processor

NewProcessor creates a new processor from its configuration

func (*Processor) Process

func (processor *Processor) Process() error

Process receives MQTT messages and saves data to the SQL database

type ProcessorConfig

type ProcessorConfig struct {
	Sql    SqlConfig
	Mqtt   MqttConfig
	Logger *log.Logger
}

A ProcessorConfig stores the configuration of a processor

type SqlConfig

type SqlConfig struct {
	Url string // Database URL (driver://user:password@hostname:port/db?opts)
}

An SqlConfig stores connection details to the database

type TicMessage

type TicMessage struct {
	Timestamp UnixEpoch `json:"ts"`
	Field     string    `json:"-"`
	Value     string    `json:"val"`
}

A TicMessage represents data received from the TIC (Tele Information Client)

type UnixEpoch

type UnixEpoch time.Time

A UnixEpoch is a time.Time that serializes / deserializes as Unix epoch

func (UnixEpoch) MarshalJSON

func (t UnixEpoch) MarshalJSON() ([]byte, error)

MarshalJSON returns the current value as JSON

func (*UnixEpoch) UnmarshalJSON

func (t *UnixEpoch) UnmarshalJSON(b []byte) error

UnmarshalJSON initialises the current object from its JSON representation

Directories

Path Synopsis
cli
cmd

Jump to

Keyboard shortcuts

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