package main

import (
	"github.com/prometheus/client_golang/prometheus"
	"sparrow/pkg/rpcs"
	"sparrow/pkg/server"
)

var deviceOnlineCount prometheus.Gauge
var deviceMessageCount *prometheus.CounterVec

func main() {
	// init server
	err := server.Init(rpcs.MQTTAccessName)
	if err != nil {
		server.Log.Fatal(err)
		return
	}

	a, err := NewAccess()
	if err != nil {
		server.Log.Fatal(err)
		return
	}

	// register a rpc service
	err = server.RegisterRPCHandler(a)
	if err != nil {
		server.Log.Errorf("Register RPC service Error: %s", err)
		return
	}

	// register a tcp service for mqtt
	err = server.RegisterTCPHandler(a.MqttBroker)
	if err != nil {
		server.Log.Errorf("Register TCP service Error: %s", err)
		return
	}
	deviceOnlineMetric := &server.Metric{
		ID:          "device_online_count",
		Name:        "device_online_count",
		Description: "MQTT Device Online Count",
		Type:        "gauge",
	}
	recvMessageCount := &server.Metric{
		ID:          "device_message_total",
		Name:        "device_message_total",
		Description: "device report message total count",
		Type:        "counter_vec",
		Args:        []string{"device_code"},
	}

	server.RegisterMetric(deviceOnlineMetric, recvMessageCount)
	deviceOnlineCount = deviceOnlineMetric.MetricCollector.(prometheus.Gauge)
	deviceMessageCount = recvMessageCount.MetricCollector.(*prometheus.CounterVec)
	// start to run
	err = server.Run()
	if err != nil {
		server.Log.Fatal(err)
	}
}
