Facility Control

 View Only
  • 1.  HTTP POST og GET requests with JSON data in the request body

    Posted 06-06-2020 01:12

    Hi there! Been a while!
    So for some reason I just cant get this one to work...

    I'm trying to use a GitHub project called MidiRelay to work, and trigger it by DashBoard.
    You can find the project here: https://github.com/josephdadams/midi-relay
    Using QLAB as a relay work around to send MIDI to a DOT2 as of now, but I wanna get rid of the "man in the middle" aka, an old MAC Pro from 2013...

    Now, it has some simple commands, and a /settings site that lets me test this real simple, and by the looks of it, it works!

    Now, creating an applet in a website or similar that lets me send an MSC message is real simple actually!
    But I cannot for the LIFE of me figure out how to make the HTTP POST or GET in DashBoard to trigger it.

    Sending MIDI Relay Messages
    - Make an HTTP POST request with JSON data in the request body to /sendmidi on the server running midi-relay to relay a MIDI Message.
    - You can also make an HTTP GET request with the parameters passed as querystrings.
    - Requests can be sent from any computer or device reachable to the midi-relay server.

    MSC (MIDI Show Control):

    {
    midiport: 'MIDI Port Name',
    midicommand: 'msc',
    deviceid: 0,
    commandformat: 'lighting.general',
    command: 'go',
    cue: '10',
    cuelist: '13',
    cuepath: ''
    }

    The service runs locally, as in 127.0.0.1 and on port 4000

    That about sums it up what I need to do to send an MSC from it, and the API...
    But I cannot find out how to do it... Do I use the HTTP Post? The HTTP Async Message? DO I set it to POST or GET? I just cant get it to work, and I've tried just about every version of it I can imagine at the moment...

    Anyone that could clue me in, that would be downright fantastic!



  • 2.  RE: HTTP POST og GET requests with JSON data in the request body

    Posted 06-06-2020 13:35

    So, keep banging my head at this one...
    I've got the MIDI-Relay server responding, but not reacting as I would have hoped...

    I have the server responding just fine to a /refresh call, that was no problem with a regular HTTP Post as soon as I put a NULL in the datafield...

    However, when trying to send a JSON command in the DataField with the asyncHTTP, I get a syntax error from the server.
    I've recently contacted the developer of the MIDI-Relay software, so it might as well just be something on that end, but then again, he has some DashBoard examples on his site, so maybe someone might point me in the right direction here as well...

    Here is the DashBoard used to send messages to the MIDI_Relay software:

    <abs contexttype="opengear" dblinqport="2222" gridsize="20" id="_top" keepalive="true">
    <meta>
    <params>
    <param access="1" maxlength="0" name="JSON CMD" oid="JSON_CMD" type="STRING" value="{midiport:'USB Midi 4i4o',midicommand:'noteon',channel:1,note:21,velocity:100}" widget="text"/>
    </params>
    </meta>
    <button buttontype="push" height="120" left="40" name="&lt;html&gt;&lt;center&gt;SEND TO&lt;br/&gt;MIDI RELAY&lt;/center&gt;&lt;/html&gt;" top="40" width="140">
    <task tasktype="ogscript">/*! block id=1002,1005 !*/
    ogscript.asyncHTTP("http://127.0.0.1:4000/sendmidi", "POST", "application/json", params.getValue('JSON_CMD', 0), null);
    /*!!
    &lt;block id="1002" type="ogscript_async" x="329" y="52" w="318" URL="http://127.0.0.1:4000/sendmidi" METHOD="POST" TYPE="application/json" DATA="ID:1005" CALLBACK="null" /&gt;
    &lt;block id="1005" type="param__top&amp;amp;JSON CMD (JSON_CMD)[0]" x="32" y="89" w="243" SET="" /&gt;
    !!*/
    /*!!&lt;checksum&gt;a9542906b7bca962416de27520cabeea&lt;/checksum&gt;!!*/</task>
    </button>
    <button buttontype="push" height="120" left="480" name="&lt;html&gt;&lt;center&gt;REFRESH&lt;br/&gt;MIDI RELAY&lt;/center&gt;&lt;/html&gt;" top="40" width="200">
    <task tasktype="ogscript">/*! block id=1004 !*/
    ogscript.asyncPost("http://127.0.0.1:4000/refresh",null, null);
    /*!!
    &lt;block id="1004" type="ogscript_httppost" x="495" y="160" w="318" URL="http://127.0.0.1:4000/refresh" DATA="null" CALLBACK="null" /&gt;
    !!*/
    /*!!&lt;checksum&gt;72715f77abcaa8a60211fd1101a79a09&lt;/checksum&gt;!!*/</task>
    </button>
    <param expand="true" height="40" left="40" oid="JSON_CMD" top="180" width="640"/>
    <label height="40" left="40" name="Enter JSON command above and send to MIDI-Relay server" style="txt-align:west" top="220" width="640"/>
    </abs>

    And here is the response I get from the server when trying to send the message that is in the message field atm:

    SyntaxError: Unexpected token m in JSON at position 1
    at JSON.parse (<anonymous>)
    at parse (C:\snapshot\midi-relay\node_modules\body-parser\lib\types\json.js:89:19)
    at C:\snapshot\midi-relay\node_modules\body-parser\lib\read.js:121:18
    at invokeCallback (C:\snapshot\midi-relay\node_modules\raw-body\index.js:224:16)
    at done (C:\snapshot\midi-relay\node_modules\raw-body\index.js:213:7)
    at IncomingMessage.onEnd (C:\snapshot\midi-relay\node_modules\raw-body\index.js:273:7)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)

    I've tried JSON.encode on the string. I've tried encodeURI, with either no response or same response.
    I've tried setting it to something other than "application/JSON", no response at all.
    I've tried setting server IP to 10.0.0.10 as well... Same results...

    Does the function asyncHTTP do something under the hood that could mess this up?
    Is this a character encoding issue I might have here?


    #DashBoard


  • 3.  RE: HTTP POST og GET requests with JSON data in the request body

    Posted 06-06-2020 16:08

    So, after a quick response from the creator of MIDI-Relay... He put me on the track for a sollution, and turns out I've been really close in the neighbourhood all along.

    The one step I kept forgetting, before sending the JSON data through the data field in the asyncHTTP, that was to stringify it...
    Of course...

    Well, it's been too long since I've been working on this appereantly, but when he said it, if course it made total sense...

    So now I finally have a MIDI output on the same machine running DashBoard without the need for QLAB to "interpret" in between... Finally! Next, set it up and test it with the DOT2 mixer.

    Here's the "final" dashboard btw...
    Made a function out of the command, so I can easily just punch in the cue, cuelist and cuepath for this to work.

    <abs contexttype="opengear" dblinqport="2222" gridsize="20" id="_top" keepalive="true">
    <meta>
    <api immediate="true">function sendMidiMSC (cue, cuelist, cuepath) {
    var cue = cue.toString();
    var cuelist = cuelist.toString();
    var cuepath = cuepath.toString();

    var obj = { midiport: 'USB Midi 4i4o', midicommand: 'msc', deviceid: 0, commandformat: 'lighting.general', command: 'go', cue: cue, cuelist: cuelist, cuepath: cuepath };
    var jsonstring = JSON.stringify(obj);
    ogscript.debug('JSON String: ' + jsonstring);

    ogscript.asyncHTTP("http://127.0.0.1:4000/sendmidi", "POST", "application/json", jsonstring, null);
    }
    </api>
    </meta>
    <button buttontype="push" height="120" left="200" name="&lt;html&gt;&lt;center&gt;REFRESH&lt;br/&gt;MIDI RELAY&lt;/center&gt;&lt;/html&gt;" top="40" width="200">
    <task tasktype="ogscript">/*! block id=1004 !*/
    ogscript.asyncPost("http://10.0.0.10:4000/refresh",null, null);
    /*!!
    &lt;block id="1004" type="ogscript_httppost" x="495" y="160" w="318" URL="http://10.0.0.10:4000/refresh" DATA="null" CALLBACK="null" /&gt;
    !!*/
    /*!!&lt;checksum&gt;fa697f9ab11b6b06e7e21ee2d8f86381&lt;/checksum&gt;!!*/</task>
    </button>
    <button buttontype="push" height="120" left="40" name="&lt;html&gt;&lt;center&gt;SEND TO&lt;br/&gt;MIDI RELAY&lt;/center&gt;&lt;/html&gt;" top="40" width="140">
    <task tasktype="ogscript">


    /*! block id=1007 !*/
    sendMidiMSC(1, 2, 3)
    /*!!
    &lt;block id="1007" type="function_sendMidiMSC" x="30" y="26" w="243" cue="1" cuelist="2" cuepath="3" /&gt;
    !!*/
    /*!!&lt;checksum&gt;969c1e4be4122811d0f5b411b9fbfb0c&lt;/checksum&gt;!!*/</task>
    </button>
    </abs>

    #DashBoard


  • 4.  RE: HTTP POST og GET requests with JSON data in the request body

    Posted 06-08-2020 13:33

    Hi Aleksander

    Glad you were able to get it sorted out. To add some clarity to your earlier questions:

    ogscript.asyncPost will automatically do an HTTP POST message if where the request body will be whatever you put in the data field. If the data field is null, this function will do an HTTP GET instead of a post. 

    ogscript.asyncHTTP gives you more control over GET vs POST, PUT, DELETE, etc. and allows you to specify your content type (or leave it null for DashBoard to try to guess).

    When called outside of visual logic, either function will take care of converting JSON objects to their string representations. Visual Logic will only handle strings so they would need to be pre-converted.

    Cheers

    James

     


    #DashBoard