Virtual button press - API


#22

I’m not seeing any snippet here. Did you lose it while pasting?


#23

I pasted it … for some reason it does not show unless you click on to edit on …

That is the reason I pasted it twice


#24

Thanks, I see it now. Two questions …

  1. Are you getting a debug message when the workflow fires? Is the issue only that device state is not being set?
  2. Can you paste the code where you are actually calling this method?

#25
  1. I do not get debug message when the workflow fires from the api. Same problem with another device I have, same type of workflow - virtual button linked to device state

  2. the code is simple … in an html page, I have a button click to call that function having passed the parameter values, say 20,30,40,50

For testing, I am running the html page in my localhost


#26

Your code snippet is working for me … What browser are you using?

Here is a version of the snippet with the variables isolated at the top - your application ID, flow ID and virtual button key. You’ll need to paste in your application token. Are you able to server this HTML document locally and click the button? Does the virtual button in your workflow fire?

<!DOCTYPE html>
<html lang="en">
  <head>
      <meta charset="utf-8">
      <title>Virtual button press - API - Bug Report - Losant Forums</title>
      <script>
          var host = 'api.losant.com';
          var applicationId = '5b7895c3e8e3fa0007ca7759';
          var flowId = '5b79fe8a1a83b0000773232d';
          var key = '55b79fe8a1a83b0000773232d-fxtzMDVGWy~2_Ytaz7ECa';
          var token = 'APPLICATION_TOKEN_HERE';

          function PrinterWorkflow(bTarget, bActual, tTarget, tActual)
          {
            var urlwrkflow = 'https://' + host + '/applications/' + applicationId + '/flows/' + flowId + '/virtualButton';
            var data1 = {};
            data1.key = key;
            data1.payload = {};
            data1.payload.BedTempTarget = bTarget;
            data1.payload.BedTempActual = bActual;
            data1.payload.ToolTempTarget = tTarget;
            data1.payload.ToolTempActual = tActual;

            var wrkFlowData = JSON.stringify(data1); alert(wrkFlowData);

            var xhrwf = new XMLHttpRequest();
            xhrwf.open("POST", urlwrkflow, false);
            xhrwf.setRequestHeader ('Authorization', 'Bearer ' + token); // catual token is placed there as Bearer ...
            xhrwf.setRequestHeader('Content-Type','application/json; charset=utf-8');
            xhrwf.setRequestHeader('Accept','application/json');
            xhrwf.onload = function () {
              if (xhrwf.readyState == 4 && xhrwf.status == "200") {
                  alert("success");
              } else {
              //console.error(xhrwf.responseText);
              }
            }
            xhrwf.send(wrkFlowData);
        }
          </script>
    </head>
  <body><button onclick="PrinterWorkflow(4, 3, 2, 1)">click to run</button></body>
</html>  

#27

frustrating , I ran your html code locally … did not fire the workflow, application log shows no data

I tried under both chrome and IE browsers
chrome version Version 68.0.3440.106 (Official Build) (64-bit)
IE version 11.611.16299.0


#28

I’ve just sent you an invitation to an application I created that replicates your functionality. There’s a device with the attributes you describe, a workflow built similarly to yours, and an experience URL that contains the markup I sent you earlier within a page. Try opening the workflow in one tab, and in another, open the experience URL and click the button. Does the workflow fire?


#29

Your setup works within the experience view. But, it did not work calling it from my localhost html page with the same script. The same issue is with my setup. As I said, when I make the POST request from a postman, the device state in the dashboard changes OK. But from a localhost, it does not.


#30

Hi Dylan,

I have tried this … Is there a way you can help me taking a look at my setup. Can I add you to my organization so that you can take a look at. If you do not mind, will you be able to call me?


#31

Thanks. I’ll follow up with you via email.


#32

Thanks Dylan, let me know when you can help me out asap. Is there a number I can call?


#33

Sreyams, not sure if you have a contract with our sales team but any advanced support / solutions work you’re looking for should be funneled through them.


#34

Understood.

My question is why it works if I place the code in the experience view and not work when I have that piece of code in an html page running in localhost iis? It works fine when I issue from a postman script?

It should not make any difference. Is it not?


#35

Your copy/paste error is in the original snippet you posted …

var key = '55b79fe8a1a83b0000773232d-fxtzMDVGWy~2_Ytaz7ECa';

Are you sure that’s not the issue locally? You said it is indeed making the request and you’re getting a success reply.


#36

Yes, that is the correct key used both in local html page and in postman. Device state gets updated via postman and NOT from localhost page invoke


#37

If you want to paste an updated copy of your local code - all of the HTML document minus the authorization token - I can take another look.


#38

sure , attached … please place this html code in localhost running iis. There is only one function call that is made with the button click. You can ignore the other functions that are defined …

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<script>
function PrinterWorkflow(bTarget, bActual, tTarget, tActual)
{//alert(bTarget);alert(bActual); alert(tTarget); alert(tActual);
	var urlwrkflow = "https://api.losant.com/applications/5b7895c3e8e3fa0007ca7759/flows/5b79fe8a1a83b0000773232d/virtualButton";
	var data1 = {};
	data1.key = "55b79fe8a1a83b0000773232d-fxtzMDVGWy~2_Ytaz7ECa";
	data1.payload = {};
	data1.payload.BedTempTarget = bTarget;
	data1.payload.BedTempActual = bActual;
	data1.payload.ToolTempTarget = tTarget;
	data1.payload.ToolTempActual = tActual;

	var wrkFlowData = JSON.stringify(data1); alert(wrkFlowData);

    var xhrwf = new XMLHttpRequest();
    xhrwf.open("POST", urlwrkflow, false);
	xhrwf.setRequestHeader ('Authorization', 'Bearer '); // HAVE REMOVED THE TOKEN purposely just you know
    xhrwf.setRequestHeader('Content-Type','application/json; charset=utf-8');
    xhrwf.setRequestHeader('Accept','application/json');
    xhrwf.onload = function () {
      if (xhrwf.readyState == 4 && xhrwf.status == "200") {
			alert("success");alert(xhrwf.responseText);
      } else {
        console.error(xhrwf.responseText);
      }
    }
    xhrwf.send(wrkFlowData);
}


function getPrinter()
{
    var urlPrinter = "http://172.17.66.225:5000/api/printer";
    var Printer = "";

    var xhrPrinter = new XMLHttpRequest();
    xhrPrinter.open("GET", urlPrinter, false);
    xhrPrinter.setRequestHeader('x-api-key', '2CA19CEE585045CEBF48F077CC49F494');
    xhrPrinter.onload = function () {
      if (xhrPrinter.readyState == 4 && xhrPrinter.status == "200") {
		Printer = xhrPrinter.responseText;
      } else {
        ;
      }
    }
    xhrPrinter.send();
    return Printer;
}
function Print()
{
	var dataPrint;
	var print;
	var response;
	var temperature;
	var printer;
	var bed;
	var tool;
	var BedTempTarget;
	var BedTempActual;
	var ToolTempTarget;
	var ToolTempActual;


		// issue print command for file
		var urlPrint = "http://172.17.66.225:5000/api/files/local/standing.gcode";


		dataPrint = {};
		dataPrint.command = "select";
		dataPrint.print = true;

		print = JSON.stringify(dataPrint);

		var xhr = new XMLHttpRequest();
		xhr.open("POST", urlPrint, false);
		xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
		xhr.setRequestHeader('x-api-key', '2CA19CEE585045CEBF48F077CC49F494');
		xhr.onload = function () {
			// monitor the print job
			//setTimeout(function(){

				printer = getPrinter();
				response = JSON.parse(printer);
				temperature = response.temperature;
				bed = temperature.bed;
				BedTempTarget = bed.target;
				BedTempActual = bed.actual;
				tool = temperature.tool0;
				ToolTempTarget = tool.target;
				ToolTempActual = tool.actual;
				// update these readings to Printer device on Losant
				PrinterWorkflow(BedTempTarget, BedTempActual, ToolTempTarget, ToolTempActual);
			//}, 30000);
		}
		xhr.send(print);


}




</script>


<body>
	<center>
		<div class="container">
			<div class="jumbotron">
				<input type="button" id="buttonPrint" value="Print Start" onclick="PrinterWorkflow(20,30,40,50)" />
			</div>
		</div>
	</center>
</body>

#39

You still have this mistake. Remove the very first character. The virtual button’s key is actually ‘5b79fe8a1a83b0000773232d-fxtzMDVGWy~2_Ytaz7ECa’; you can see for yourself when you select your virtual button in the workflow editor and find the triggerId in the example payload.


#40

I did that correction … but no success as you see below


#41

Per your device’s state log, a state report was made about 10 minutes before you posted this. That wasn’t you?