#!/bin/sh

#Verify that AccessTechnology is set to NR5G-NSA. For units used for SA-testing we will not change any settings
AccessTechnology=`ztr69cli get Device.Cellular.Interface.1.PreferredAccessTechnology | awk '{print  $2}'`
SN=`ztr69cli get Device.DeviceInfo.SerialNumber | awk '{print  $2}'`

#if [ "$AccessTechnology" == "\"NR5G-NSA\"" ] || [ "$SN" == "S230Z05015117" ];then
if [ "$AccessTechnology" == "\"NR5G-NSA\"" ];then
	echo "Access Technology is ok, parameter-value is $AccessTechnology on $SN, continue"
else
	echo "Access Technology not ok, setting it to NR5G-NSA"
	ztr69cli set Device.Cellular.Interface.1.PreferredAccessTechnology=NR5G-NSA
	#sleep a while to let WAN re-establish before the next running of the script
        sleep 60
        echo "Done sleeping, will exit now"
        exi
fi

#Verify that ultbsize is 0 to avoid memory-leak
ulbtsize=`atcmd at+qnwcfg=\"nr5g_ultbsize\" | awk '-F' "," '{print $2}' | sed 's/ //g' | sed 's/\r$//'`

if [ "$ulbtsize" == "0" ];then                                                      
        echo "ulbtsize is ok, parameter-value is $ulbtsize, continue"                                                       
else                                                                                                         
	echo "ulbtsize is not ok, parameter-value is $ulbtsize, will change and continue"
	/usr/bin/atcmd at+qnwcfg=\"nr5g_ultbsize\",0
fi 


#Verify that mosquitto_pub is running
pid=`pidof mosquitto_pub`
#echo $pid
if [ -z "$pid" ];then
	echo "mosquitto_pub is not running, will restart it"
	cfg mqtt_client edit --Index 1 --Enable 0
	cfg mqtt_client edit --Index 1 --Enable 1
	sleep 3
	pid=`pidof mosquitto_pub`
	echo "mosquitto_pub is restarted and now running with pid $pid"
else
	echo "mosquitto_pub is running with pid $pid, all ok, will continue"
fi

LinkRate=`ztr69cli get Device.Ethernet.Interface.1.MaxBitRate | awk '{print  $2}'`
LastChangeAPN1=`ztr69cli get Device.IP.Interface.2.LastChange | awk '{print  $2}'`
LastChangeAPN2=`ztr69cli get Device.IP.Interface.3.LastChange | awk '{print  $2}'`
LastChangeAPN3=`ztr69cli get Device.IP.Interface.4.LastChange | awk '{print  $2}'`
IMSI=`ztr69cli get Device.Cellular.Interface.1.USIM.IMSI | awk '{print  $2}'`
ModelName=`ztr69cli get Device.DeviceInfo.ModelName | awk '{print  $2}'`
#SN=`ztr69cli get Device.DeviceInfo.SerialNumber | awk '{print  $2}'`
#SN=`/bin/sys atsh | grep Serial | awk '{print $4}'`
#SoftwareVersion=`/bin/sys atsh | grep Firmware | awk '{ print $4 }'`
Uptime=`cat /proc/uptime | awk '{print $1}' | sed 's/...$//'`
Load_1min=`uptime | awk '{ print $(NF-2) }' | tr -d ','`
Load_5min=`uptime | awk '{ print $(NF-1) }' | tr -d ','`
Load_15min=`uptime | awk '{ print $NF }'`
MemFree=`cat /proc/meminfo | grep MemFree | awk '{ print $2 }'`
Temp=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_TEMPERATURE_AMBIENT | awk '{print  $2}'`
DATE=`date +%Y-%m-%d-%T`
PCC_UplinkBandwidth_4G=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_UplinkBandwidth | awk '{print  $2}'`
PCC_DownlinkBandwidth_4G=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_DownlinkBandwidth | awk '{print  $2}'`
PCC_BandNum_4G=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_CurrentBandNum | awk '{print  $2}'`
RSRP_4G=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_RSRP | awk '{print  $2}'`
CellID_4G=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_CellID | awk '{print  $2}'`
SiteID_4G=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_SiteID | awk '{print  $2}'`
PhyCellID_4G=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_PhyCellID | awk '{print  $2}'`
MGMT_IP=`ztr69cli get Device.IP.Interface.2.IPv4Address.1.IPAddress | awk '{print  $2}'`

PCC_BandNum_5G=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_NewRadioNonStandAlone.BandNum | awk '{print  $2}'`                                                                                                                                                      
PCC_DownlinkBandwidth_5G=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_NewRadioNonStandAlone.DL_BW | awk '{print  $2}'`                                                                                                                                              
RSRP_5G=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_NewRadioNonStandAlone.RSRP | awk '{print  $2}'`

SCC1_CA_STATE=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_SecondaryComponentCarrier.1.Enable | awk '{print  $2}'`                               
SCC2_CA_STATE=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_SecondaryComponentCarrier.2.Enable | awk '{print  $2}'`                               
SCC3_CA_STATE=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_SecondaryComponentCarrier.3.Enable | awk '{print  $2}'`

if [ "$SCC1_CA_STATE" == "1" ];then
        SCC1_BandNum_4G=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_SecondaryComponentCarrier.1.BandNum | awk '{print  $2}'`
        SCC1_DownlinkBandwidth_4G=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_SecondaryComponentCarrier.1.DownlinkBandwidth | awk '{print  $2}'`
else                                                                                                                                                  
        SCC1_BandNum_4G=0                                                                                                                             
        SCC1_DownlinkBandwidth_4G=0                                                                                                                   
fi

if [ "$SCC2_CA_STATE" == "1" ];then                                                                                                                   
        SCC2_BandNum_4G=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_SecondaryComponentCarrier.2.BandNum | awk '{print  $2}'`                    
	SCC2_DownlinkBandwidth_4G=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_SecondaryComponentCarrier.2.DownlinkBandwidth | awk '{print  $2}'`
else                                                                                                                                                  
	SCC2_BandNum_4G=0                                                                                                                             
	SCC2_DownlinkBandwidth_4G=0                                                                                                                   
fi

if [ "$SCC3_CA_STATE" == "1" ];then
	SCC3_BandNum_4G=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_SecondaryComponentCarrier.3.BandNum | awk '{print  $2}'` 
	SCC3_DownlinkBandwidth_4G=`ztr69cli get Device.Cellular.Interface.1.X_ZYXEL_SecondaryComponentCarrier.3.DownlinkBandwidth | awk '{print  $2}'`
else
	SCC3_BandNum_4G=0
	SCC3_DownlinkBandwidth_4G=0
fi	




#OUTPUT_MSG="{\\\"Temp\\\":$Temp,\\\"Uptime\\\":$Uptime,\\\"Load_1min\\\":$Load_1min,\\\"Load_5min\\\":$Load_5min,\\\"Load_15min\\\":$Load_15min,\\\"MemFree\\\":$MemFree,\\\"Timestamp\\\":\\\"$DATE\\\"}"
OUTPUT_MSG="{\\\"SN\\\":\\\"$SN\\\",\\\"IMSI\\\":$IMSI,\\\"MGMT_IP\\\":\\\"$MGMT_IP\\\",\\\"ulbtsize\\\":$ulbtsize,\\\"LinkRate\\\":$LinkRate,\\\"LastChangeAPN1\\\":$LastChangeAPN1,\\\"LastChangeAPN2\\\":$LastChangeAPN2,\\\"LastChangeAPN3\\\":$LastChangeAPN3,\\\"PCC_BandNum_5G\\\":$PCC_BandNum_5G,\\\"PCC_DownlinkBandwidth_5G\\\":$PCC_DownlinkBandwidth_5G,\\\"RSRP_5G\\\":$RSRP_5G,\\\"SCC3_DownlinkBandwidth_4G\\\":$SCC3_DownlinkBandwidth_4G,\\\"SCC2_DownlinkBandwidth_4G\\\":$SCC2_DownlinkBandwidth_4G,\\\"SCC1_DownlinkBandwidth_4G\\\":$SCC1_DownlinkBandwidth_4G,\\\"SCC3_BandNum_4G\\\":$SCC3_BandNum_4G,\\\"SCC2_BandNum_4G\\\":$SCC2_BandNum_4G,\\\"SCC1_BandNum_4G\\\":$SCC1_BandNum_4G,\\\"PCC_UplinkBandwidth_4G\\\":$PCC_UplinkBandwidth_4G,\\\"PCC_DownlinkBandwidth_4G\\\":$PCC_DownlinkBandwidth_4G,\\\"PCC_BandNum_4G\\\":$PCC_BandNum_4G,\\\"PhyCellID_4G\\\":$PhyCellID_4G,\\\"RSRP_4G\\\":$RSRP_4G,\\\"CellID_4G\\\":$CellID_4G,\\\"SiteID_4G\\\":$SiteID_4G,\\\"Timestamp\\\":\\\"$DATE\\\",\\\"Temp\\\":$Temp,\\\"Uptime\\\":$Uptime,\\\"Load_1min\\\":$Load_1min,\\\"Load_5min\\\":$Load_5min,\\\"Load_15min\\\":$Load_15min,\\\"MemFree\\\":$MemFree}"


cfg mqtt_client publish --Index 1 --Topic $ModelName/$SN --Message $OUTPUT_MSG
