Change WWAN Hotlink according to the Service Type

The following script can be used to check the service type (LTE, UMTS and so on) of several wwan connections and automatically use the best service type as hotlink. This however does not take signal strength and other factors into account. The script currently only runs once when triggered. If you would like to change that please incorporate a loop on the bottom of the script, but use some longer sleep periods, to prevent unwanted changing of your wwan links all the time.

wwan_switcher.are
/* DESC: This script can be used to prioritise the best 
 * WWAN Service Type (LTE over UMTS and so on). Only service
 * types are taken into account. Not signal strength.
 *
 * NOTE:
 * Please set Link priorities on the router according to best practise for Link Manager
 * for the script to work properly.
 * LAN -> Permanent, WLAN --> Permanent, WWAN usw. 
 * Copyright (C) 2018 NetModule AG, Switzerland
 */
 
 
template wanswitch{
	// Set total amount of max WAN Links
	max_wanlinks = 8;
	network_list = mkarray("CDMA", "CSD", "GPRS", "EDGE", "UMTS", "HSDPA", "HSUPA", "HSPA", "HSPA+", "LTE");
	status = nb_status("wan");
 
	int getWanlinkService(){
 
		x=0;
		max=0;
		mob_prio = mkarray();	
 
		currentHotLink = struct_get(this.status, "WAN_HOTLINK");
		currentHotLinkServiceType = struct_get(this.status, sprintf("%s_SERVICE_TYPE", currentHotLink));
 
		nb_syslog("Current Hotlink: %s, Service Type: %s\n", currentHotLink, currentHotLinkServiceType);
 
 
		for(x=0; x<this.max_wanlinks; x++){
			if(is_void(mob_services[x] = struct_get(this.status, sprintf("WANLINK%i_SERVICE_TYPE", x+1)))){
				continue;
			}else{
				mob_services[x] = struct_get(this.status, sprintf("WANLINK%i_SERVICE_TYPE", x+1));
			}
			if(mob_services[x] == "n/a"){
				nb_syslog("WANLINK%i currently has no service \n", x+1);
			}else{
				mob_prio[x] = array_search(this.network_list, mob_services[x]);
			}
 
			if(mob_prio[x] > max){	
				max = mob_prio[x];
				pos = x; 
			}
			nb_syslog("WANLINK%i: %s Prio: %i\n", x+1, mob_services[x], mob_prio[x]);
		}
 
		if(mob_services[pos] == "n/a"){
			nb_syslog("No available service found for your mobile connections");
			return -1;  
		}else{
			nb_syslog("Highest Prio is %i, %s from Mobile %i\n", mob_prio[pos], mob_services[pos], pos+1);		
 
			if(struct_get(this.status, "WAN_HOTLINK") == sprintf("WANLINK%i", pos+1)){
				nb_syslog("High prio wanlink is already the hotlink\n");
			}else{
				nb_syslog("high prio wanlink is not hot yet ... activate\n");
				if(currentHotLinkServiceType == mob_services[pos]){
					nb_syslog("ServiceType of current Hotlink is the same, doing nothing\n");
				}else{
					nb_syslog("Activating Hotlink\n");
					nb_wanlink_priorize(sprintf("WANLINK%i", pos+1),0);	
				}
			}
			return 0;
		}
	}
}
 
ws = new wanswitch();
 
// Check if WAN Hotlink is neither WLAN nor Ethernet  
if(!ws.status.WAN_HOTLINK == "n/a" ){
 
	wanhotlink = struct_get(ws.status, "WAN_HOTLINK");
	if(struct_get(ws.status, sprintf("%s_TYPE", wanhotlink)) == "eth" || struct_get(ws.status, sprintf("%s_TYPE", wanhotlink)) == "wlan"){
		nb_syslog("No active WWAN Hotlinks...exiting\n");
		exit(0);
	}else{
		nb_syslog("Updating WAN Link Servicetype\n");
		ws.getWanlinkService();		
	}
 
}else{
	nb_syslog("Currently no active WAN hotlink");
	exit(0);
}