#!/bin/bash
# console-config/src/lib-scripts/serial-msmodbus.sh
#
#  Copyright: ©2014, Güralp Systems Ltd.
#  Author: Kelly Dunlop <kdunlop@guralp.com>
#  License: GPLv3
#
# Setup the parameters for a serial port configured for Morningstar Modbus.



IOLINE_NAMES="$CONFIGDIR/ioline/name_map.local"



# display_msmodbus_ioline_extension
# ioline_extension (default _portname)
# $1 is the title text to display
# $2 is the current value
# $3 is the default value
display_msmodbus_ioline_extension() {
    local default

    if [ -z "$2" ]
    then
        default="$3"
    else
        default="$2"
    fi

    display_inputbox "$1" "Distinguishing extension added to Digital I/O identifiers." \
        0 0 "$default"
}



# display_msmodbus_slave_type
# slave_type (default Sunsaver_MPPT)
# $1 is the title text to display
# $2 is the current value
# $3 is the default value
display_msmodbus_slave_type() {
    local default

    if [ -z "$2" ]
    then
        default="$3"
    else
        default="$2"
    fi

    MENU=( "Sunsaver_MPPT" "Morningstar Sunsaver MPPT" )

    display_menu "$1" "Select the type of attached device.  (Only Morningstar Sunsaver MPPT is supported at present)" \
        0 0 "$default" 
}



# display_msmodbus_slave_id
# slave_id (default 0x01)
# $1 is the title text to display
# $2 is the current value
# $3 is the default value
display_msmodbus_slave_id() {
    local default

    if [ -z "$2" ]
    then
        default="$3"
    else
        default="$2"
    fi

    display_inputbox "$1" "Enter Modbus slave address used by the device.  (default is 0x01)" \
        0 0 "$default"
}



# update_ioline_entries
# Add ioline names to the map file.
# $1 is the ioline_extension value
update_ioline_entries() {
    local ext="$1"

    if [ -n "$ext" ]
    then
        if grep "^ss_charge${ext} " $IOLINE_NAMES > /dev/null 2>&1
        then
            : Nothing to do, names are in place
        else
            echo "ss_charge${ext} =" \
                "SunSaver (${ext#_}) charging"              >> $IOLINE_NAMES
            echo "ss_load${ext} =" \
                "SunSaver (${ext#_}) load"                  >> $IOLINE_NAMES
            echo "ss_misc${ext} =" \
                "Other SunSaver (${ext#_}) parameters"      >> $IOLINE_NAMES
        fi
    fi
}



# Go through the list of things that the user can change in the msmodbus config
# file.
#   $1 - portname
# If something is Cancelled or there is an error then this can exit.
serial_msmodbus_parameters() {
    local port=$1
    local titletext

    MSM_CFG_FILE="${SERIAL_CFDIR}/msmodbus/${port}"

    # Set default values
    DEF_MSM_SLAVE_TYPE="Sunsaver_MPPT"
    DEF_MSM_SLAVE_ID=0x01
    DEF_MSM_IOLINE_EXTENSION="_${port}"

    # Get current values or set defaults if file doesn't already exist
    if [ -r "${MSM_CFG_FILE}" ]
    then
        # get the current values for each item
        MSM_SLAVE_TYPE="`cfget ${MSM_CFG_FILE} slave_type`"
        MSM_SLAVE_ID="`cfget ${MSM_CFG_FILE} slave_id`"
        MSM_IOLINE_EXTENSION="`cfget ${MSM_CFG_FILE} ioline_extension`"
    else
        # set empty values for each item
        MSM_SLAVE_TYPE=""
        MSM_SLAVE_ID=""
        MSM_IOLINE_EXTENSION=""
    fi

    # Now go through the values giving user a choice
    titletext="${port} Modbus device config"

    display_msmodbus_ioline_extension "${titletext}" "${MSM_IOLINE_EXTENSION}" "${DEF_MSM_IOLINE_EXTENSION}"
    NEW_MSM_IOLINE_EXTENSION="`cat ${DIALOG_RESULTFILE}`"

    display_msmodbus_slave_type "${titletext}" "${MSM_SLAVE_TYPE}" "${DEF_MSM_SLAVE_TYPE}"
    NEW_MSM_SLAVE_TYPE="`cat ${DIALOG_RESULTFILE}`"

    display_msmodbus_slave_id "${titletext}" "${MSM_SLAVE_ID}" "${DEF_MSM_SLAVE_ID}"
    NEW_MSM_SLAVE_ID="`cat ${DIALOG_RESULTFILE}`"

    cconf_create_config_file "${MSM_CFG_FILE}"

    # Now we need to set the values in the config file.
    cfset ${MSM_CFG_FILE} ioline_extension="${NEW_MSM_IOLINE_EXTENSION}"
    cfset ${MSM_CFG_FILE} slave_type="${NEW_MSM_SLAVE_TYPE}"
    cfset ${MSM_CFG_FILE} slave_id="${NEW_MSM_SLAVE_ID}"

    update_ioline_entries ${NEW_MSM_IOLINE_EXTENSION}

    # Now we've finished making changes reset the permission of the config file
    cconf_set_perms_and_group ${MSM_CFG_FILE} 664 "conf"
}



# vim: ts=4:sw=4:expandtab:syntax=sh
