#!/bin/sh

# Load common functions
. /usr/share/fan-control/common.sh

NAME="Fibocom FM160"
MANUFACTURER="Fibocom"
DESCRIPTION="Fibocom 5G modem FM160"
AT_PORT_PRIORITY="ttyUSB1,ttyUSB2"

get_modem_temperature() {
    local device="$1"
    local temp=0
    
    if [ ! -c "$device" ]; then
        echo "0"
        return 1
    fi
    
    # Try GTSENRDTEMP command
    gt_response=$(sms_tool_query "AT+GTSENRDTEMP=1" "$device")
    temp=$(echo "$gt_response" | awk -F'[ ,:]+' '/\+GTSENRDTEMP:/{ value=$3; result=value/1000; printf "%.0f", result }')
    
    if [ -n "$temp" ] && [ "$temp" -gt 0 ] && [ "$temp" -lt 120 ]; then
        echo "$temp"
        return 0
    fi
    
    # Fallback
    echo "0"
    return 1
}

test_port() {
    local device="$1"
    
    if [ ! -c "$device" ]; then
        return 1
    fi
    
    response=$(sms_tool_query "ATI" "$device")
    echo "$response" | grep -qv "ERROR" && echo "$response" | grep -q "."
    return $?
}