Developing lightweight computation at the DSG edge

Commit ab6b87a9 authored by Roger Pueyo Centelles's avatar Roger Pueyo Centelles
Browse files

[assign] Linter party


Signed-off-by: default avatarRoger Pueyo Centelles <roger.pueyo@guifi.net>
parent fc754f45
......@@ -121,3 +121,22 @@ type DeviceAddressesServer struct {
Addresses []string
GraphServer int
}
// The DeviceAddresses lists the IPv4 addresses a GraphServer is assigned
type DeviceAddresses struct {
ID int `json:"Id"`
Addresses []string `json:"Addresses"`
GraphServer int `json:"GraphServer"`
}
// The DeviceMonitorAssignation holds...
type DeviceMonitorAssignation struct {
DeviceID int
MonitorID []string
}
// The AssignChecksum holds...
type AssignChecksum struct {
MonitorID string
Checksum string
}
......@@ -14,59 +14,70 @@ import (
"time"
"github.com/golang/glog"
"lightkone.guifi.net/uc-monitor-go-test/cnml"
)
type DeviceAddresses struct {
Id int `json:"Id"`
Addresses []string `json:"Addresses"`
GraphServer int `json:"GraphServer"`
}
// Error levels
type DeviceMonitorAssignation struct {
DeviceId int
MonitorId []string
}
// FATAL output code
var FATAL = 1
type AssignChecksum struct {
MonitorId string
Checksum string
}
// ERROR output code
var ERROR = 2
// INFO output code
var INFO = 3
// WARNING output code
var WARNING = 4
//Error levels
var FATAL int = 1
var ERROR int = 2
var INFO int = 3
var WARNING int = 4
var DEFAULT_FILEMODE os.FileMode = 0755
// DEFAULTFILEMODE for created files
var DEFAULTFILEMODE os.FileMode = 0755
//Default settings and descriptions
var id_num int
var id string = "0"
var id_help string = "Numeric id of this monitoring instance"
var max_devs int = 1000
var max_devs_help string = "Maximum number of devices to monitor"
var tmp_dir string = "/tmp/gmonitor2/"
var tmp_dir_help string = "Temporary directory where to read and write files"
var devs_file string = "devs.json"
var devs_file_help string = "Filename of the JSON file containing the list of devices to monitor and their IPv4 addresses"
var devs_checksum_file string = "devs.checksum"
var devs_checksum_file_help string = "Filename of the text file containing the checksum of the list of devices to monitor"
var assign_dir string = fmt.Sprintf("%sassign/", tmp_dir)
var assign_dir_help string = "Temporary directory where to write local and read foreign device<=>monitors assignation files"
var devs_check_interval int = 5 //60
var devs_check_interval_help string = "Interval (in seconds) in which to check for changes in the devices list"
var reassign_interval int = 5 //60
var reassign_interval_help string = "Interval (in seconds) in which to update the local devices assignation list"
var global_assign_check_interval int = 5 //60
var global_assign_check_interval_help string = "Interval (in seconds) in which to check for changes in the global devices assignation list"
// idNum holds the monitor id, in numberic form
var idNum int
// ID represents the ID of the monitor, in alfanumeric form
var ID = "0"
// idHelp usage description help
var idHelp = "Numeric ID of this monitoring instance"
var maxDevs = 1000
var maxDevsHelp = "Maximum number of devices to monitor"
var tmpDir = "/tmp/gmonitor2/"
// tmpDirHelp usage description help for tmpDir
var tmpDirHelp = "Temporary directory where to read and write files"
var devsFile = "devs.json"
// devsFileHelp usage description help for devsFile
var devsFileHelp = "Filename of the JSON file containing the list of devices to monitor and their IPv4 addresses"
var devsChecksumFile = "devs.checksum"
// idHelp usage description help
var devsChecksumFileHelp = "Filename of the text file containing the checksum of the list of devices to monitor"
var assignDir = fmt.Sprintf("%sassign/", tmpDir)
// idHelp usage description help
var assignDirHelp = "Temporary directory where to write local and read foreign device<=>monitors assignation files"
var devsCheckInterval = 5 //60
// idHelp usage description help
var devsCheckIntervalHelp = "Interval (in seconds) in which to check for changes in the devices list"
var reassignInterval = 5 //60
// idHelp usage description help
var reassignIntervalHelp = "Interval (in seconds) in which to update the local devices assignation list"
var globalAssignCheckInterval = 5 //60
// idHelp usage description help
var globalAssignCheckIntervalHelp = "Interval (in seconds) in which to check for changes in the global devices assignation list"
//Global variables
var all_devs []DeviceAddresses //The global list with all the fetched devices and their IPv4 addresses
var all_devs_checksum string //The checksum of the last global devices list and IPv4 addresses fetched
var local_assign []DeviceAddresses //The list of locally assigned devices and their IPv4 addresses
var local_assign_checksum string //The checksum of the local assigned devices list
var global_assign []DeviceMonitorAssignation //The global assignation list of devices<=>monitors
var global_assign_checksums []AssignChecksum //The checksum of the foreign assigned devices list
var allDevs []cnml.DeviceAddresses //The global list with all the fetched devices and their IPv4 addresses
var allDevsChecksum string //The checksum of the last global devices list and IPv4 addresses fetched
var localAssign []cnml.DeviceAddresses //The list of locally assigned devices and their IPv4 addresses
var localAssignChecksum string //The checksum of the local assigned devices list
var globalAssign []cnml.DeviceMonitorAssignation //The global assignation list of devices<=>monitors
var globalAssignChecksums []cnml.AssignChecksum //The checksum of the foreign assigned devices list
func main() {
......@@ -78,97 +89,97 @@ func main() {
func initialize() {
//Define and parse command line flags
id_int, _ := strconv.ParseInt(id, 10, 32)
id_ptr := flag.Int("id", int(id_int), id_help)
max_devs_ptr := flag.Int("max_devs", max_devs, max_devs_help)
tmp_dir_ptr := flag.String("tmp_dir", tmp_dir, tmp_dir_help)
devs_file_ptr := flag.String("devs_file", devs_file, devs_file_help)
devs_checksum_file_ptr := flag.String("devs_checksum_file", devs_checksum_file, devs_checksum_file_help)
assign_dir_ptr := flag.String("assign_dir", assign_dir, assign_dir_help)
devs_check_interval_ptr := flag.Int("devs_check_interval", devs_check_interval, devs_check_interval_help)
reassign_interval_ptr := flag.Int("reassign_interval", reassign_interval, reassign_interval_help)
global_assign_check_interval_ptr := flag.Int("global_assign_check_interval", global_assign_check_interval, global_assign_check_interval_help)
idInt, _ := strconv.ParseInt(ID, 10, 32)
idPtr := flag.Int("id", int(idInt), idHelp)
maxDevsPtr := flag.Int("maxDevs", maxDevs, maxDevsHelp)
tmpDirPtr := flag.String("tmpDir", tmpDir, tmpDirHelp)
devsFilePtr := flag.String("devsFile", devsFile, devsFileHelp)
devsChecksumFilePtr := flag.String("devsChecksumFile", devsChecksumFile, devsChecksumFileHelp)
assignDirPtr := flag.String("assignDir", assignDir, assignDirHelp)
devsCheckIntervalPtr := flag.Int("devsCheckInterval", devsCheckInterval, devsCheckIntervalHelp)
reassignIntervalPtr := flag.Int("reassignInterval", reassignInterval, reassignIntervalHelp)
globalAssignCheckIntervalPtr := flag.Int("globalAssignCheckInterval", globalAssignCheckInterval, globalAssignCheckIntervalHelp)
flag.Parse()
//Service id
if *id_ptr > 0 {
id_num = *id_ptr
id = fmt.Sprint(*id_ptr)
fmt.Println("Using id", id)
if *idPtr > 0 {
idNum = *idPtr
ID = fmt.Sprint(*idPtr)
fmt.Println("Using ID", ID)
} else {
randsource := rand.New(rand.NewSource(time.Now().UnixNano()))
id = fmt.Sprint("a", 100000+randsource.Intn(900000))
fmt.Println("Using anonymous id", id)
ID = fmt.Sprint("a", 100000+randsource.Intn(900000))
fmt.Println("Using anonymous id", ID)
}
//Maximum devices to monitor
if *max_devs_ptr > 0 {
max_devs = *max_devs_ptr
if *maxDevsPtr > 0 {
maxDevs = *maxDevsPtr
}
//Directories
tmp_dir = *tmp_dir_ptr
assign_dir = *assign_dir_ptr
initialize_dirs()
tmpDir = *tmpDirPtr
assignDir = *assignDirPtr
initializeDirs()
//Devices and addresses list related files
devs_file = *devs_file_ptr
devs_checksum_file = *devs_checksum_file_ptr
devsFile = *devsFilePtr
devsChecksumFile = *devsChecksumFilePtr
//Timers for tickers (intervals)
if *devs_check_interval_ptr > 0 {
devs_check_interval = *devs_check_interval_ptr
if *devsCheckIntervalPtr > 0 {
devsCheckInterval = *devsCheckIntervalPtr
}
if *reassign_interval_ptr > 0 {
reassign_interval = *reassign_interval_ptr
if *reassignIntervalPtr > 0 {
reassignInterval = *reassignIntervalPtr
}
if *global_assign_check_interval_ptr > 0 {
global_assign_check_interval = *global_assign_check_interval_ptr
if *globalAssignCheckIntervalPtr > 0 {
globalAssignCheckInterval = *globalAssignCheckIntervalPtr
}
//Import the devices and IPv4 addresses list from the JSON file, and the checksum
all_devs = read_devsadds_file(fmt.Sprintf("%s/%s", tmp_dir, devs_file), ERROR)
set_current_all_devs_checksum()
allDevs = readDevsaddsFile(fmt.Sprintf("%s/%s", tmpDir, devsFile), ERROR)
setCurrentAllDevsChecksum()
//Import the global devices assignation lists
update_global_import_all_assign_files()
updateGlobalImportAllAssignFiles()
initialize_timers()
initializeTimers()
//Block forever
fmt.Println("Entering infinite loop...")
select {}
}
func initialize_dirs() {
func initializeDirs() {
//Create the temporary dir, along with any necessary parents
err := os.MkdirAll(tmp_dir, DEFAULT_FILEMODE)
err_check(err, FATAL, fmt.Sprintf("Unable to create temporary directory %s", tmp_dir))
err := os.MkdirAll(tmpDir, DEFAULTFILEMODE)
errCheck(err, FATAL, fmt.Sprintf("Unable to create temporary directory %s", tmpDir))
//Create the assignation dir, along with any necessary parents
err = os.MkdirAll(assign_dir, DEFAULT_FILEMODE)
err_check(err, FATAL, fmt.Sprintf("Unable to create temporary directory %s", assign_dir))
err = os.MkdirAll(assignDir, DEFAULTFILEMODE)
errCheck(err, FATAL, fmt.Sprintf("Unable to create temporary directory %s", assignDir))
}
func initialize_timers() {
//Initialize all_devs checksum changes check timer
start_all_devs_checksum_timer()
func initializeTimers() {
//Initialize allDevs checksum changes check timer
startAllDevsChecksumTimer()
//Initialize the local assignation check timer
start_global_assign_timer()
startGlobalAssignTimer()
//Initialize all_devs checksum changes check timer
start_local_assign_timer()
//Initialize allDevs checksum changes check timer
startLocalAssignTimer()
}
//Read a devices and IPv4 addresses list from a JSON file
func read_devsadds_file(filename string, err_level int) []DeviceAddresses {
var newdevs []DeviceAddresses
func readDevsaddsFile(filename string, errLevel int) []cnml.DeviceAddresses {
var newdevs []cnml.DeviceAddresses
jdevsaddrsFile, err := os.Open(filename)
err_check(err, err_level, fmt.Sprintf("Error opening file %s", filename))
errCheck(err, errLevel, fmt.Sprintf("Error opening file %s", filename))
if err != nil {
return newdevs
}
......@@ -183,9 +194,9 @@ func read_devsadds_file(filename string, err_level int) []DeviceAddresses {
//Unmarshal the JSON objects to DeviceAddresses objects
for _, v := range lines {
var devaddr DeviceAddresses
var devaddr cnml.DeviceAddresses
err := json.Unmarshal([]byte(v), &devaddr)
err_check(err, INFO, "TODO")
errCheck(err, INFO, "TODO")
newdevs = append(newdevs, devaddr)
}
......@@ -193,12 +204,12 @@ func read_devsadds_file(filename string, err_level int) []DeviceAddresses {
return newdevs
}
func update_devsadds_file(filename string, currdevsadds []DeviceAddresses) []DeviceAddresses {
func updateDevsaddsFile(filename string, currdevsadds []cnml.DeviceAddresses) []cnml.DeviceAddresses {
fdevs := read_devsadds_file(filename, ERROR)
fdevs := readDevsaddsFile(filename, ERROR)
for _, v := range fdevs {
if !is_device_in_slice(v, currdevsadds) {
if !isDeviceInSlice(v, currdevsadds) {
currdevsadds = append(currdevsadds, v)
}
}
......@@ -206,17 +217,17 @@ func update_devsadds_file(filename string, currdevsadds []DeviceAddresses) []Dev
}
//Export the assigned devices and IPv4 addresses list to a file
func export_devsaddrs_file(filename string) {
func exportDevsaddrsFile(filename string) {
//afile, err := os.Create(fmt.Sprintf("%s%s.json", assign_dir, id))
//afile, err := os.Create(fmt.Sprintf("%s%s.json", assignDir, id))
exportfile, err := os.Create(filename)
err_check(err, INFO, "TODO")
errCheck(err, INFO, "TODO")
defer exportfile.Close()
for _, v := range local_assign {
for _, v := range localAssign {
jda, err := json.Marshal(v)
err_check(err, INFO, "TODO")
errCheck(err, INFO, "TODO")
exportfile.WriteString(string(jda))
exportfile.WriteString("\n")
......@@ -224,10 +235,10 @@ func export_devsaddrs_file(filename string) {
}
//Read a text file containing a SHA256 checksum on its first line
func read_checksum_file(filename string, err_level int) string {
func readChecksumFile(filename string, errLevel int) string {
cfile, err := os.Open(filename)
err_check(err, err_level, fmt.Sprintf("Error opening file %s", filename))
errCheck(err, errLevel, fmt.Sprintf("Error opening file %s", filename))
defer cfile.Close()
var sha256 string
......@@ -239,16 +250,16 @@ func read_checksum_file(filename string, err_level int) string {
}
//Read the last SHA256 checksum of the devices and IPv4 addresses GO struct
func set_current_all_devs_checksum() {
all_devs_checksum = read_checksum_file(fmt.Sprintf("%s/%s", tmp_dir, devs_checksum_file), ERROR)
fmt.Println("Current all devices list checksum set to", all_devs_checksum)
func setCurrentAllDevsChecksum() {
allDevsChecksum = readChecksumFile(fmt.Sprintf("%s/%s", tmpDir, devsChecksumFile), ERROR)
fmt.Println("Current all devices list checksum set to", allDevsChecksum)
}
func check_all_devs_checksum() {
if all_devs_checksum != read_checksum_file(fmt.Sprintf("%s/%s", tmp_dir, devs_checksum_file), ERROR) {
func checkAllDevsChecksum() {
if allDevsChecksum != readChecksumFile(fmt.Sprintf("%s/%s", tmpDir, devsChecksumFile), ERROR) {
fmt.Println("SHA256 has changed since last check")
set_current_all_devs_checksum()
all_devs = read_devsadds_file(fmt.Sprintf("%s/%s", tmp_dir, devs_file), ERROR)
setCurrentAllDevsChecksum()
allDevs = readDevsaddsFile(fmt.Sprintf("%s/%s", tmpDir, devsFile), ERROR)
return
}
fmt.Println("SHA256 has not changed since last check")
......@@ -258,72 +269,72 @@ func check_all_devs_checksum() {
// func import_all_assign_files() {
//
// //List all the JSON files in the assignations dir
// json_files, err := ioutil.ReadDir(fmt.Sprintf("%s", assign_dir))
// err_check(err, INFO, "TODO")
// json_files, err := ioutil.ReadDir(fmt.Sprintf("%s", assignDir))
// errCheck(err, INFO, "TODO")
//
// for _, v := range json_files {
// if v.Name() != fmt.Sprintf("%s.json", id) {
// fmt.Println("Importing assignation file", v.Name())
// }
//
// monitor_id := strings.Replace(v.Name(), ".json", "", -1)
// monitorID := strings.Replace(v.Name(), ".json", "", -1)
//
// //Import all json files except the current instance's one
// if monitor_id != id {
// fmt.Println("Adding assignation list from monitor", monitor_id, "to global assignation list")
// adevsadds := read_devsadds_file(fmt.Sprintf("%s/%s", assign_dir, v.Name()), ERROR)
// if monitorID != ID {
// fmt.Println("Adding assignation list from monitor", monitorID, "to global assignation list")
// adevsadds := readDevsaddsFile(fmt.Sprintf("%s/%s", assignDir, v.Name()), ERROR)
// for _, w := range adevsadds {
// global_assign = assign_device_monitor(global_assign, w.Id, monitor_id)
// globalAssign = assignDeviceMonitor(globalAssign, w.Id, monitorID)
// }
// }
// }
// }
//Import all the monitor=>device assignations
func update_global_import_all_assign_files() {
func updateGlobalImportAllAssignFiles() {
//List all the assignation and checksum files in the assignations dir
assign_dir_files, err := ioutil.ReadDir(fmt.Sprintf("%s", assign_dir))
err_check(err, WARNING, fmt.Sprintf("The global assignation dir %s can not be scanned for files", assign_dir))
assignDirFiles, err := ioutil.ReadDir(fmt.Sprintf("%s", assignDir))
errCheck(err, WARNING, fmt.Sprintf("The global assignation dir %s can not be scanned for files", assignDir))
//Iterate all the files to get all the monitors' info
for _, jfile := range assign_dir_files {
//Get the monitor id from the filename
monitor_id := strings.Replace(jfile.Name(), ".json", "", -1)
for _, jfile := range assignDirFiles {
//Get the monitor ID from the filename
monitorID := strings.Replace(jfile.Name(), ".json", "", -1)
//But only pick only .json files, not .checksum, and exclude the local instance
if monitor_id != id && !jfile.IsDir() && jfile.Name() != monitor_id {
if monitorID != ID && !jfile.IsDir() && jfile.Name() != monitorID {
//Find the corresponding .checksum file
for _, cfile := range assign_dir_files {
for _, cfile := range assignDirFiles {
//We have the json file and the checksum file in jfile and cfile
if !cfile.IsDir() && cfile.Name() == fmt.Sprintf("%s.checksum", monitor_id) {
if !cfile.IsDir() && cfile.Name() == fmt.Sprintf("%s.checksum", monitorID) {
//Read the checksum from the file
monitor_checksum := read_checksum_file(fmt.Sprintf("%s/%s", assign_dir, cfile.Name()), ERROR)
monitorChecksum := readChecksumFile(fmt.Sprintf("%s/%s", assignDir, cfile.Name()), ERROR)
//If we got something as a checksum (TODO: check it is an actual checksum)
if monitor_checksum != "" {
if monitorChecksum != "" {
//Check if the monitor is already known
if is_monitor_in_slice(monitor_id, global_assign_checksums) {
if isMonitorInSlice(monitorID, globalAssignChecksums) {
//Check if it must be updated
if monitor_checksum != global_assign_checksums[pos_monitor_in_slice(monitor_id, global_assign_checksums)].Checksum {
if monitorChecksum != globalAssignChecksums[posMonitorInSlice(monitorID, globalAssignChecksums)].Checksum {
//Update it
fmt.Println("Updating assignation list from monitor", monitor_id)
monitor_devs_assign := read_devsadds_file(fmt.Sprintf("%s/%s", assign_dir, jfile.Name()), ERROR)
fmt.Println("Updating assignation list from monitor", monitorID)
monitorDevsAssign := readDevsaddsFile(fmt.Sprintf("%s/%s", assignDir, jfile.Name()), ERROR)
//Iterate all the assigned devices
for _, v := range monitor_devs_assign {
for _, v := range monitorDevsAssign {
//Add them to the global list
global_assign = assign_device_monitor(global_assign, v.Id, monitor_id)
globalAssign = assignDeviceMonitor(globalAssign, v.ID, monitorID)
}
//Save the new checksum
global_assign_checksums[pos_monitor_in_slice(monitor_id, global_assign_checksums)].Checksum = monitor_checksum
globalAssignChecksums[posMonitorInSlice(monitorID, globalAssignChecksums)].Checksum = monitorChecksum
}
} else {
//Add new monitor to the list
fmt.Println("Importing assignation list from monitor", monitor_id)
this_checksum := AssignChecksum{MonitorId: monitor_id, Checksum: monitor_checksum}
global_assign_checksums = append(global_assign_checksums, this_checksum)
monitor_devs_assign := read_devsadds_file(fmt.Sprintf("%s/%s", assign_dir, jfile.Name()), ERROR)
fmt.Println("Importing assignation list from monitor", monitorID)
thisChecksum := cnml.AssignChecksum{MonitorID: monitorID, Checksum: monitorChecksum}
globalAssignChecksums = append(globalAssignChecksums, thisChecksum)
monitorDevsAssign := readDevsaddsFile(fmt.Sprintf("%s/%s", assignDir, jfile.Name()), ERROR)
//Iterate all the assigned devices
for _, v := range monitor_devs_assign {
for _, v := range monitorDevsAssign {
//Add them to the global list
global_assign = assign_device_monitor(global_assign, v.Id, monitor_id)
globalAssign = assignDeviceMonitor(globalAssign, v.ID, monitorID)
}
}
}
......@@ -331,94 +342,94 @@ func update_global_import_all_assign_files() {
}
}
// monitor_id := strings.Replace(v.Name(), ".json", "", -1)
// monitorID := strings.Replace(v.Name(), ".json", "", -1)
// if v.Name() != fmt.Sprintf("%s.json", id) {
// fmt.Println("Importing assignation file", v.Name())
// }
//
// monitor_id := strings.Replace(v.Name(), ".json", "", -1)
// monitorID := strings.Replace(v.Name(), ".json", "", -1)
//
// //Import all json files except the current instance's one
// if monitor_id != id {
// fmt.Println("Adding assignation list from monitor", monitor_id, "to global assignation list")
// adevsadds := read_devsadds_file(fmt.Sprintf("%s/%s", assign_dir, v.Name()), ERROR)
// if monitorID != ID {
// fmt.Println("Adding assignation list from monitor", monitorID, "to global assignation list")
// adevsadds := readDevsaddsFile(fmt.Sprintf("%s/%s", assignDir, v.Name()), ERROR)
// for _, w := range adevsadds {
// global_assign = assign_device_monitor(global_assign, w.Id, monitor_id)
// globalAssign = assignDeviceMonitor(globalAssign, w.Id, monitorID)
// }
// }
}
}
func assign_device_monitor(assgnlist []DeviceMonitorAssignation, device_id int, monitor_id string) []DeviceMonitorAssignation {
if !is_device_in_assignation_slice(device_id, assgnlist) {
var nassgn DeviceMonitorAssignation
nassgn.DeviceId = device_id
nassgn.MonitorId = []string{monitor_id}
func assignDeviceMonitor(assgnlist []cnml.DeviceMonitorAssignation, deviceID int, monitorID string) []cnml.DeviceMonitorAssignation {
if !isDeviceInAssignationSlice(deviceID, assgnlist) {
var nassgn cnml.DeviceMonitorAssignation
nassgn.DeviceID = deviceID
nassgn.MonitorID = []string{monitorID}
assgnlist = append(assgnlist, nassgn)
} else {
var tassgn = assgnlist[pos_device_in_assignation_slice(device_id, assgnlist)]
var tassgn = assgnlist[posDeviceInAssignationSlice(deviceID, assgnlist)]
in_mon_list := false
for _, v := range tassgn.MonitorId {
if v == monitor_id {
in_mon_list = true
inMonList := false
for _, v := range tassgn.MonitorID {
if v == monitorID {
inMonList = true
}
}
if !in_mon_list {
if !inMonList {
//Fixme: Use pointers here
assgnlist[pos_device_in_assignation_slice(device_id, assgnlist)].MonitorId = append(assgnlist[pos_device_in_assignation_slice(device_id, assgnlist)].MonitorId, monitor_id)
assgnlist[posDeviceInAssignationSlice(deviceID, assgnlist)].MonitorID = append(assgnlist[posDeviceInAssignationSlice(deviceID, assgnlist)].MonitorID, monitorID)
}
}
return assgnlist
}
func reassign_devs() {
func reassignDevs() {
fmt.Println("\nReassignation of devices")
fmt.Println(len(local_assign), "devices currently assigned (maximum:", max_devs, "devices)")
fmt.Println(len(localAssign), "devices currently assigned (maximum:", maxDevs, "devices)")
assgnchanges := false
var deldevs []DeviceAddresses
newassigndevs := local_assign
var deldevs []cnml.DeviceAddresses
newassigndevs := localAssign
// Check if all the devices assigned are still in the global list
for _, v := range local_assign {
if !is_device_in_slice(v, all_devs) {
for _, v := range localAssign {
if !isDeviceInSlice(v, allDevs) {
//TODO check this loop
fmt.Println("Removing old device ", v.Id, " from the assigned devices list")
fmt.Println("Removing old device ", v.ID, " from the assigned devices list")
deldevs = append(deldevs, v)
newassigndevs = rm_device_from_slice(v, newassigndevs)
newassigndevs = rmDeviceFromSlice(v, newassigndevs)
assgnchanges = true
}
}
if assgnchanges {
local_assign = newassigndevs
localAssign = newassigndevs
}
// If less than maximum devices are assigned and more can be assigned,
// pick some at random, at a maximum rate so that the maximum is reached
// in 1 hour, until the maximum is reached
if (len(local_assign) < max_devs) && (len(local_assign) < len(all_devs)) {
if (len(localAssign) < maxDevs) && (len(localAssign) < len(allDevs)) {
//Assign the following new nodes
toassign := int(math.Min(float64(int(math.Min(math.Min(float64(len(all_devs)), float64(max_devs))*float64(devs_check_interval)/3600+1, float64(len(all_devs)-len(local_assign))))), float64(max_devs-len(local_assign))))
// toassign := int(math.Min(float64(int(math.Min(math.Min(float64(len(allDevs)), float64(maxDevs))*float64(devsCheckInterval)/3600+1, float64(len(allDevs)-len(localAssign))))), float64(maxDevs-len(localAssign))))
//Be quick, do it in 1 min
toassign = int(math.Min(float64(int(math.Min(math.Min(float64(len(all_devs)), float64(max_devs))*float64(devs_check_interval)/60+1, float64(len(all_devs)-len(local_assign))))), float64(max_devs-len(local_assign))))
toassign := int(math.Min(float64(int(math.Min(math.Min(float64(len(allDevs)), float64(maxDevs))*float64(devsCheckInterval)/60+1, float64(len(allDevs)-len(localAssign))))), float64(maxDevs-len(localAssign))))
fmt.Println("Assigning", toassign, "new devices")
//Create a list with all the unassigned devices
var unassigned []DeviceAddresses
for _, v := range all_devs {
if !is_device_in_slice(v, local_assign) {
var unassigned []cnml.DeviceAddresses
for _, v := range allDevs {
if !isDeviceInSlice(v, localAssign) {
unassigned = append(unassigned, v)