#!/bin/sh

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}'`

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="{\\\"IMSI\\\":$IMSI,\\\"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
