Home AMX User Forum AMX Technical Discussion

Parse part of host name from controller

Hi,
Still sorta new to coding so bear with me,

I'm trying to parse part of the hostname from the controller as a variable to use it to define settings for specific rooms later in code. I can get the full hostname by using hostname = ipaddress.hostname, and then from that I only need the last 6 characters. our host names are set up as BLDG-FLOOR-ROOM.xyz.com i just need the 'room' part of the name. i've tried using right_string or remove_string but they all come up as empty values in debug. any thoughts?

Comments

  • I'd do a while through the string removing at the "-".

    hostname = ipaddress.hostname
    amx_log(AMX_ERROR,"'BEFORE hostname = ', hostname");
    
    // Remove 'BLD-' and 'FLOOR-' prefix
    while(find_string(hostname,'-',1))
    {
    remove_string(hostname,'-',1);
    }
    
    // Remove '.com' suffix
    if(find_string(hostname,'.com',1))
    {
    set_length_string(hostname, length_string(hostname) - length_string('.com'));
    }
    
    amx_log(AMX_ERROR,"'AFTER hostname = ', hostname");
    
  • emdx71emdx71 Posts: 42

    Ian,

    Where abouts would you put that in code? I tried calling it as a function, setting up an online event but it still comes up blank in debug.

    Thanks

  • HARMAN_ChrisHARMAN_Chris Posts: 597

    You mentioned a function - can you write a button event to call the function for testing purposes at runtime?

  • HARMAN_icraigieHARMAN_icraigie Posts: 660
    edited June 2020

    @emdx71 said:
    Ian,

    Where abouts would you put that in code? I tried calling it as a function, setting up an online event but it still comes up blank in debug.

    Thanks

    Question - where are you setting your ipaddress.hostname variable from?

    And yes it would best be found in a function

    define_function char[255] parseHostName(hostName) 
    {
      stack_var char tempString[255];
    
      tempString = hostName;
    
      amx_log(AMX_ERROR,"'BEFORE = ', hostName");
    
      // Remove 'BLD-' and 'FLOOR-' prefix
      while(find_string(tempString ,'-',1))
      {
        remove_string(tempString ,'-',1);
      }
    
      // Remove '.com' suffix
      if(find_string(tempString ,'.com',1))
      {
        set_length_string(tempString , length_string(tempString ) - length_string('.com'));
      }
    
      amx_log(AMX_ERROR,"'AFTER =  ', tempString ");
    
      return tempString;
    }
    
  • emdx71emdx71 Posts: 42

    I just made a simple program with the example you posted, but its giving an illegal assignment statment at tempstring = hostname.

    If i comment that out it will compile.

    PROGRAM_NAME='Hostname Program'
    (***********************************************************)
    (* FILE CREATED ON: 06/04/2020 AT: 09:33:14 )
    (***********************************************************)
    (***********************************************************)
    (***********************************************************)
    (
    FILE_LAST_MODIFIED_ON: 06/04/2020 AT: 10:22:51 )
    (***********************************************************)
    (
    System Type : NetLinx )
    (***********************************************************)
    (
    REV HISTORY: )
    (***********************************************************)
    (

    $History: $
    )
    (***********************************************************)
    (
    DEVICE NUMBER DEFINITIONS GO BELOW *)
    (***********************************************************)
    DEFINE_DEVICE
    dvMaster = 0:1:0

    (***********************************************************)
    (* CONSTANT DEFINITIONS GO BELOW *)
    (***********************************************************)
    DEFINE_CONSTANT

    (***********************************************************)
    (* DATA TYPE DEFINITIONS GO BELOW *)
    (***********************************************************)
    DEFINE_TYPE

    (***********************************************************)
    (* VARIABLE DEFINITIONS GO BELOW *)
    (***********************************************************)
    DEFINE_VARIABLE
    IP_ADDRESS_STRUCT IPAddress
    DNS_STRUCT DNSName
    CHAR HOSTNAME[50] = 'IPAddress.HOSTNAME'
    CHAR SUBNET[50] = 'DNSName.DomainName'

    (***********************************************************)
    (* LATCHING DEFINITIONS GO BELOW *)
    (***********************************************************)
    DEFINE_LATCHING

    (***********************************************************)
    (* MUTUALLY EXCLUSIVE DEFINITIONS GO BELOW *)
    (***********************************************************)
    DEFINE_MUTUALLY_EXCLUSIVE

    (***********************************************************)
    (* SUBROUTINE/FUNCTION DEFINITIONS GO BELOW )
    (***********************************************************)
    (
    EXAMPLE: DEFINE_FUNCTION () )
    (
    EXAMPLE: DEFINE_CALL '' () *)

    define_function char[255] parseHostName(hostName)
    {
    stack_var char tempString[254];

    tempString = hostName

    amx_log(AMX_ERROR,"'BEFORE = ', hostName");

    // Remove 'BLD-' and 'FLOOR-' prefix
    while(find_string(tempString ,'-',1))
    {
    remove_string(tempString ,'-',1);
    }

    // Remove '.com' suffix
    if(find_string(tempString ,'.com',1))
    {
    set_length_string(tempString , length_string(tempString ) - length_string('.com'));
    }

    amx_log(AMX_ERROR,"'AFTER = ', tempString ");

    return tempString;
    }
    (***********************************************************)
    (* STARTUP CODE GOES BELOW *)
    (***********************************************************)
    DEFINE_START

    (***********************************************************)
    (* THE EVENTS GO BELOW *)
    (***********************************************************)
    DEFINE_EVENT

    DATA_EVENT [dvMASTER]
    {
    ONLINE:
    {
    GET_IP_ADDRESS(dvMaster,IPAddress);
    GET_DNS_LIST(dvMaster,DNSName);
    parseHostName(hostname);
    SUBNET = DNSName.DomainName;
    HOSTNAME = IPAddress.HOSTNAME;
    }

    }

    DEFINE_PROGRAM

  • My bad - missed the data type for the parameter
    parseHostName(char hostName[])

Sign In or Register to comment.