Kodi (XBMC) control

Discussion in 'Pascal Logic Code Examples' started by djaggar, Aug 27, 2015.

  1. djaggar

    djaggar

    Joined:
    Jul 18, 2009
    Messages:
    66
    Likes Received:
    0
    Location:
    New Zealand
    Here is some code to control one or more Kodi (was XBMC) media players.

    The code runs on a Color CTouch as it uses HTTP requests to talk to the JSON RPC interface on Kodi (which you'll need to enable in Kodi settings)

    The code is actually pretty simple, but I found the learning curve quite steep (mainly because of clunkiness & case sensivity in the Kodi interface), so maybe it is a leg up for others. To use the code just set the Group Address "AV Action" to the values in the case statement ... uncomment the LogMessages to see what's being sent over the interface ...

    For what it's worth ... I've rebuilt my entire home around RaspberryPi 2, running Kodi 15, with CEC and IR controlled amps/TVs ... so I mostly use this code to shut everything off and play/pause Kodi's party mode. However in my pool, I use this as the primary interface to fire up the system, control the interface etc, so you can control the AV for the whole room from a touch screen if you wish. The Kodi control apps on phones & smart watches make the whole system work pretty well ... simple control from a DLT (basically play/pause) and full control from your smart device ...

    Before using the code, in the constant section do something like

    Code:
    AVRoomIPAddress = '192.168.0.';
    to set the base address of your network. A couple of lines into the code there are two ways to set the last byte of the IP address ... either directly, or from a group (so you can control different Kodi instances this way).

    And put these in your global variables section

    Code:
    TempInt, TempInt2: integer;
    TempString, TempString2: array[0..256] of char;
    The strings have to be defined this way or they'll overrun the default size in the logic engine.

    Then stick this lot in a Module ...

    Code:
    TempInt := GetLightingLevel("AV Action");
    
    if TempInt > 0 then begin
    
      //TempInt2 := GetLightingLevel("AV Select Room");
      TempInt2 := 215; // Pool Media Player IP Address
    
      case TempInt of
        1 : TempString := 'back';
        2 : TempString := 'up';
        3 : TempString := 'left';
        4 : TempString := 'select';
        5 : TempString := 'right';
        6 : TempString := 'down';
        7 : TempString := 'contextmenu';
        8 : TempString := 'home';
        
        10 : TempString := 'skipprevious';
        11 : TempString := 'skipnext';
        12 : TempString := 'rewind';
        13 : TempString := 'fastforward';
        14 : TempString := 'play';
        15 : TempString := 'pause';
        16 : TempString := 'stop';
        17 : TempString := 'record';
    
        20 : TempString := 'volumedown';
        21 : TempString := 'volumeup';
        22 : TempString := 'mute';
        end ;
        
      //LogMessage('AV action is ',TempString);
      if (TempInt < 10) then // ditect Input command for these
        format(TempString2,'http://',AVRoomIPAddress,TempInt2:1,
                           '/jsonrpc?request={"jsonrpc":"2.0",',
                           '"method":"Input.',TempString,
                           '","id":1}')
      else if (TempInt < 100) then // ExecuteAction for these
        format(TempString2,'http://',AVRoomIPAddress,TempInt2:1,
                           '/jsonrpc?request={"jsonrpc":"2.0",',
                           '"method":"Input.ExecuteAction",',
                           '"params":{"action":"',TempString,
                           '"},"id":1}')
      else // start party mode
        format(TempString2,'http://',AVRoomIPAddress,TempInt2:1,
                           '/jsonrpc?request={"jsonrpc":"2.0",',
                           '"method":"Player.Open",',
                           '"params":{"item":{"partymode":"music"}},',
                           '"id":1}');
    
      //LogMessage('Sending ',TempString2);
      GetHTTPData(TempString2);
      SetLightingLevel("AV Action",0,0);
    
      delay(0.2);
      ReadHTTPData(TempString2);
      //LogMessage('Returned ',TempString2);
      end;
    
     
    djaggar, Aug 27, 2015
    #1
  2. djaggar

    Damaxx

    Joined:
    May 12, 2008
    Messages:
    228
    Likes Received:
    47
    Thanks for sharing that one djaggar. Will use this to make my door bell pause my movie :)
     
    Damaxx, Sep 11, 2015
    #2
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.