0

I have a file dmreboot_service.js in my /js folder. When I run this file using node /js/dmreboot_service.js it successfully invokes a direct method in Azure.

I need to be able to execute this function or file on a button click from my web app.

I tried loading the script into the head of my html using :

<script src="js/dmreboot_service.js"></script>

I put an alert in the external file. If I put this alert at the top of the file it works, but at the bottom it fails, so the contents of the file are not loading.

The content of dmreboot_service.js is :

'use strict';

var Registry = require('azure-iothub').Registry;
var Client = require('azure-iothub').Client;


var connectionString ="HostName=XxxxxxxX.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=XxxxxxxxxxxxxxxxxxxxxxxxxxxxxxX=";
var registry = Registry.fromConnectionString(connectionString);
var client = Client.fromConnectionString(connectionString);
var deviceToReboot = 'Runner';

var startRebootDevice = function (twin) {

  var methodName = "reboot";

  var methodParams = {
      methodName: methodName,
      payload: null,
      timeoutInSeconds: 30
  };

  client.invokeDeviceMethod(deviceToReboot, methodParams, function(err, result) {
      if (err) {
          console.error("Direct method error: "+err.message);
      } else {
          console.log("Successfully invoked the device to reboot.");  
      }
  });
};

var queryTwinLastReboot = function() {

  registry.getTwin(deviceToReboot, function(err, twin){

      if (twin.properties.reported.iothubDM != null)
      {
          if (err) {
              console.error('Could not query twins: ' + err.varructor.name + ': ' + err.message);
          } else {
              var lastRebootTime = twin.properties.reported.iothubDM.reboot.lastReboot;
              console.log('Last reboot time: ' + JSON.stringify(lastRebootTime, null, 2));
          }
      } else 
          console.log('Waiting for device to report last reboot time.');
  });
};

startRebootDevice();
setInterval(queryTwinLastReboot, 2000);

alert('dmreboot included!');

I have also tried creating a function in the head of my html that includes the entire contents of dmreboot_service.js, but although the function is called successfully the code does not execute.

This is the last part of a project that I need to get working. I'm fairly new to this, and this is driving me nuts!! Any advice much appreciated.

Martin
  • 1
  • 1
  • Update : I've tracked the problem down to the two lines of code that use Require. Require is not supported in the browser. Will look into browserify or requirejs which apparently enable this. – Martin Nov 21 '19 at 14:36

1 Answers1

0

I usually handle click in HTML with javascript like so;

const doSomething = document.querySelector('.whateverclassnameyouchoose').addEventListener('click',onClick);

function onClick(e){
what ever you want the function to do
}

Hope it helps :)

Tijani
  • 133
  • 2
  • 3
  • 16
  • Hi, Many thanks for your response, but I think you may have misunderstood my question. I only have a problem running this particular function from button click This is because the code in the file fails to be included in page head when I use . The code also fails to execute If I put it in a function in my html, even though the function is called successfully on button click. Many thanks for your help – Martin Nov 21 '19 at 12:18