GetHTTPData command from wiser2

Discussion in 'C-Bus Wiser 2 Controller' started by Benroe, Sep 9, 2018.

  1. Benroe

    Benroe

    Joined:
    Mar 29, 2015
    Messages:
    3
    Likes Received:
    0
    Location:
    Huddersfield, Yorkshire
    Hi
    I Wonder if someone could help with this......
    I would like to send a GetHttpdata Command from wiser2 to an API Running on a raspberry pi. I can get everything to work
    but only when I have PICED connected to my c-bus network. I had a similar problem with the postHTTPdata command but resolved this by following it with a readposthttpdata so..

    Begin
    PostHTTPData('http://192.168.1.3/Port/Set/0/0,Data', '');;
    Delay("0:00:01");
    ReadHTTPPostData(DataString);
    Delay("0:00:05");
    ReadHTTPPostData(DataString);
    SetLightingState("CCTV lounge", OFF);
    end;

    However when using the GetHTTPdata command nothing happens unless connected to cbus through PICED . Ive used Wireshark and again there's going on unless connected to PICED. my code runs as follows:-

    Begin
    GetHTTPData('http://192.168.1.5:5005/Living Room/playpause');
    Delay("0:00:01");

    SetLightingState("Sonos master bed p/p", OFF);
    end;
    Help !!!
     
    Benroe, Sep 9, 2018
    #1
  2. Benroe

    SgrAystar

    Joined:
    Oct 4, 2018
    Messages:
    65
    Likes Received:
    5
    Location:
    Melbourne, Australia
    Perhaps you would be interested in the Sonos script below that also makes use of calls to node-sonos-http-api

    The switch button is configured as a dimmer for a dummy GA; on=play, off=pause, dimmer level = volume (within a limited range).

    In my case this is an bathroom so I don't have to keep the switch synchronised all the time, hence the code to update the switch to match current sonos status when the lights change, controlled by a PIR.

    Cheers

    Code:
    Constants
    
    { sonos constants }
    VOLUME_MAX_SONOS = 70;
    VOLUME_MIN_SONOS = 40;
    VOLUME_DEF_SONOS = 50;
    
    HTTP_RESPONSE_TIME = 0.3;
    
    API_MBR_PLAY = 'http://localhost:5005/master%20bed/play';
    API_MBR_PAUSE = 'http://localhost:5005/master%20bed/pause';
    API_MBR_STATE = 'http://localhost:5005/master%20bed/state';
    API_MBR_VOLUME = 'http://localhost:5005/master%20bed/volume/';
    
    API_LIV_MEAL_VOLUME = 'http://localhost:5005/living%20room/volume/23';
    HDINNER = 6;
    WDINNER = 12;
    
    Variables
    
    {Sonos variables}
    dataString : array[0..1200] of char;
    playbackState : string;
    volumeStr : string;
    volumeURL : string;
    volumeSonos : integer;
    volumeLevel : integer;
    dataStart : integer;
    dataEnd : integer;
    retryCount : integer;
    
    Procedures
    Functions
    function SonosToLevel (integer2 : integer): integer;
    var
      real2 : real;
    begin
      if integer2 > VOLUME_MAX_SONOS then
        begin
        integer2 := VOLUME_MAX_SONOS
        end
      else if integer2 < VOLUME_MIN_SONOS then
        begin
        integer2 := VOLUME_MIN_SONOS;
        end;
        
      real2 := (integer2 - VOLUME_MIN_SONOS) / (VOLUME_MAX_SONOS - VOLUME_MIN_SONOS) * 254;
      integer2 := round(real2);
    
      if (integer2 > 0) then
        begin
        SonosToLevel := integer2;
        end
      else
        begin
        SonosToLevel := 1;
        end;
        
    end;
    
    function LevelToSonos (integer3 : integer): integer;
    var
      real3 : real;
    begin
      real3 := ((integer3 / 255) * (VOLUME_MAX_SONOS - VOLUME_MIN_SONOS)) + VOLUME_MIN_SONOS;
      integer3 := round(real3);
    
      if integer3 > VOLUME_MAX_SONOS then
        begin
        integer3 := VOLUME_MAX_SONOS
        end
      else if integer3 < VOLUME_MIN_SONOS then
        begin
        integer3 := VOLUME_MIN_SONOS;
        end;
    
      LevelToSonos := integer3;
      
    end;
    
    Module "Sonos"
    {update the ensuite sonos button whenever the ensuite lights change}
    
    if HasChanged(GetLightingState("bed1 ensuite lvs")) then
      begin
      GetHTTPData(API_MBR_STATE);
      retryCount := 0;
      dataString :='';
      repeat
        Delay(HTTP_RESPONSE_TIME);
        ReadHTTPData(dataString);
        retryCount := retryCount + 1;
      until (dataString <> '') or (retryCount >= 3);
        
      if (dataString <> '') then
        begin
        playbackState := '';
        dataStart := pos('playbackState', dataString) + 16;
        dataEnd := pos('playMode', dataString) - 3;
        Copy(playbackState, dataString, dataStart, dataEnd - dataStart);
    
        if playbackState = 'PLAYING' then
          begin
          volumeStr := '';
          dataStart := pos('volume', dataString) + 8;
          dataEnd := pos('mute', dataString) - 2;
          Copy(volumeStr, dataString, dataStart, dataEnd - dataStart);
          volumeSonos := StringToIntDef(volumeStr, VOLUME_DEF_SONOS);
          
          SetLightingLevel("flag bed1 sonos", SonosToLevel(volumeSonos), 0);
          end
        else
          SetLightingState("flag bed1 sonos", OFF);
          
        end;
      end;
    
    {update sonos in response to switch action, play/pause/volume}
    if HasChanged(GetLightingLevel("flag bed1 sonos")) then
    
      begin
      volumeLevel := GetLightingLevel("flag bed1 sonos");
    
      case volumeLevel of
        0 : {short press toggle OFF = sonos pause}
          begin
          GetHTTPData(API_MBR_PAUSE);
          retryCount := 0;
          dataString :='';
          repeat
            Delay(HTTP_RESPONSE_TIME);
            ReadHTTPData(dataString);
            retryCount := retryCount + 1;
          until (dataString <> '') or (retryCount >= 3); 
          end;
    
        255 : {short press toggle ON = sonos play}
          begin
          GetHTTPData(API_MBR_PLAY);
          retryCount := 0;
          dataString :='';
          repeat
            Delay(HTTP_RESPONSE_TIME);
            ReadHTTPData(dataString);
            retryCount := retryCount + 1;
          until (dataString <> '') or (retryCount >= 3);
    
          {set the phantom lighting level = current sonos volume}
          GetHTTPData(API_MBR_STATE);
          retryCount := 0;
          dataString :='';
          repeat
            Delay(HTTP_RESPONSE_TIME);
            ReadHTTPData(dataString);
            retryCount := retryCount + 1;
          until (dataString <> '') or (retryCount >= 3);
    
          volumeSonos := VOLUME_DEF_SONOS;
        
          if (dataString <> '') then
            begin
            volumeStr := '';
            dataStart := pos('volume', dataString) + 8;
            dataEnd := pos('mute', dataString) - 2;
            Copy(volumeStr, dataString, dataStart, dataEnd - dataStart);
            volumeSonos := StringToIntDef(volumeStr, VOLUME_DEF_SONOS);
            end;
    
          SetLightingLevel("flag bed1 sonos", SonosToLevel(volumeSonos), 0);
          end;
    
        else {long press dimmer = sonos volume}
          begin
          volumeSonos := LevelToSonos(volumeLevel);
          Format(volumeStr, volumeSonos:2);
    
          volumeURL := API_MBR_VOLUME;
          Append(volumeURL, volumeStr);
    
          GetHTTPData(volumeURL);
          retryCount := 0;
          dataString :='';
          repeat
            Delay(HTTP_RESPONSE_TIME);
            ReadHTTPData(dataString);
            retryCount := retryCount + 1;
          until (dataString <> '') or (retryCount >= 3);
          end;
    
        end; {end of case volumeLevel}
      
      end;
    
    { Set the Sonos volume for meals }
    
    once ( (GetTriggerLevel("general") = HDINNER) or (GetTriggerLevel("general") = WDINNER) ) then
      begin
      volumeURL := API_LIV_MEAL_VOLUME;
      GetHTTPData(volumeURL);
      retryCount := 0;
      dataString :='';
      repeat
        Delay(HTTP_RESPONSE_TIME);
        ReadHTTPData(dataString);
        retryCount := retryCount + 1;
      until (dataString <> '') or (retryCount >= 3);
      end;
    
     
    SgrAystar, Oct 4, 2018
    #2
    BenR likes this.
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.