Commit dc833bc8 authored by M. Rehan Abbasi's avatar M. Rehan Abbasi
Browse files

redefine opening of grid_map.yaml file using viper

parent 2e8d6c81
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -5,7 +5,7 @@ go 1.16
require (
require (
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger v0.0.0
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger v0.0.0
	github.com/lib/pq v1.5.2
	github.com/lib/pq v1.5.2
	gopkg.in/yaml.v2 v2.4.0
	github.com/spf13/viper v1.11.0
)
)


replace github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger => ../../go-packages/meep-logger
replace github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger => ../../go-packages/meep-logger
+796 −4

File changed.

Preview size limit exceeded, changes collapsed.

+24 −17
Original line number Original line Diff line number Diff line
@@ -24,10 +24,9 @@ import (
	"strings"
	"strings"
	"time"
	"time"


	"gopkg.in/yaml.v2"

	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
	_ "github.com/lib/pq"
	_ "github.com/lib/pq"
	"github.com/spf13/viper"
)
)


// DB Config
// DB Config
@@ -65,12 +64,8 @@ const (
	TrafficTable  = "traffic_patterns"
	TrafficTable  = "traffic_patterns"
)
)


// Grid Map file name
// Grid Map data
// const gridFile = "grid_map.yaml"
var gridMapData map[string]map[string][]string

// Open and embed grid_map.yaml file
//go:embed grid_map.yaml
var yamlFile []byte


// Category-wise Traffic Loads
// Category-wise Traffic Loads
var categoriesLoads = map[string]map[string]int32{
var categoriesLoads = map[string]map[string]int32{
@@ -210,7 +205,8 @@ func NewTrafficMgr(name, namespace, user, pwd, host, port string) (tm *TrafficMg
	}
	}


	// Open grid map file
	// Open grid map file
	if yamlFile == nil {
	gridMapData, err = getGridMapConfig()
	if err != nil {
		log.Error("Failed to open grid map file with err: ", err.Error())
		log.Error("Failed to open grid map file with err: ", err.Error())
		return nil, err
		return nil, err
	}
	}
@@ -314,6 +310,24 @@ func (tm *TrafficMgr) DestroyDb(name string) (err error) {
	return nil
	return nil
}
}


func getGridMapConfig() (gridData map[string]map[string][]string, err error) {
	// Read grid map from grid map file
	gridMapFile := "/grid_map.yaml"
	gridMap := viper.New()
	gridMap.SetConfigFile(gridMapFile)
	err = gridMap.ReadInConfig()
	if err != nil {
		return nil, err
	}

	var config map[string]map[string][]string
	err = gridMap.Unmarshal(config)
	if err != nil {
		return nil, err
	}
	return config, nil
}

func (tm *TrafficMgr) CreateTables() (err error) {
func (tm *TrafficMgr) CreateTables() (err error) {
	_, err = tm.db.Exec("CREATE EXTENSION IF NOT EXISTS postgis")
	_, err = tm.db.Exec("CREATE EXTENSION IF NOT EXISTS postgis")
	if err != nil {
	if err != nil {
@@ -926,14 +940,7 @@ func (tm *TrafficMgr) PopulateGridMapTable() (err error) {
	}
	}


	// Get grid map from YAML file
	// Get grid map from YAML file
	var yamlGrid map[string]map[string][]string
	for category, areas := range gridMapData {
	err = yaml.Unmarshal(yamlFile, &yamlGrid)
	if err != nil {
		log.Error(err.Error())
		return err
	}

	for category, areas := range yamlGrid {
		for area, points := range areas {
		for area, points := range areas {
			polygonStr := "("
			polygonStr := "("
			for i, pointStr := range points {
			for i, pointStr := range points {