javascript - How to use this js script in Angular: have I got to create a service? -


i'm trying create small iot project external bluetooth sensor. i'm using sensortag ti , have couple of js library manage it.

at moment i'm using simple html page js code, want move project ionic framework using angular.

<script>   // globals.   var sensortag   var values;   var compensationx = 0;   var compensationy = 0;    function initialise()   {     initialisesensortag()   }    function initialisesensortag()   {     // create sensortag cc2650 instance.     sensortag = evothings.tisensortag.createinstance(       evothings.tisensortag.cc2650_bluetooth_smart)      // set callbacks , sensors.     sensortag       .statuscallback(statushandler)       .errorcallback(errorhandler)       .keypresscallback(keypresshandler)       .accelerometercallback(accelerometerhandler, 100)   }    function connect()   {     sensortag.connecttonearestdevice()   }    function disconnect()   {     sensortag.disconnectdevice()     displaystatus('disconnected')   }    function statushandler(status)   {     displaystatus(status)      if (evothings.tisensortag.ble.status.sensortag_online == status)     {       // displaysprite()     }   }    function errorhandler(error)   {     if (evothings.easyble.error.disconnected == error)     {       displaystatus('disconnected')       // sprite.hide()     }     else     {       displaystatus('error: ' + error)     }   }    function keypresshandler(data)   {     compensationx = values.x;     compensationy = values.y;    }    function accelerometerhandler(data)   {     values = sensortag.getaccelerometervalues(data);     var rawx = values.x;      var rawy = values.y;     var degreex = rawx - compensationx;     var degreey = rawy - compensationy;    }     document.addeventlistener(     'deviceready',     function() { evothings.scriptsloaded(initialise) },     false)   </script> 

my idea create service, cannot figure how manage addeventlistener , initialization. begin whit code:

app.factory('sensorfactory', function(){      var sensortag     var values;     var compensationx = 0;     var compensationy = 0;      return {         initialisesensortag: function(){             sensortag = evothings.tisensortag.createinstance(           evothings.tisensortag.cc2650_bluetooth_smart)          // set callbacks , sensors.         sensortag           .statuscallback(statushandler)           .errorcallback(errorhandler)           .keypresscallback(keypresshandler)           .accelerometercallback(accelerometerhandler, 100)         }            connect: function(){         sensortag.connecttonearestdevice()       }     }                }); 

but have 2 question:

1) how can manage part of js:

document.addeventlistener(     'deviceready',     function() { evothings.scriptsloaded(initialise) },     false) 

2) can keep sensor values (values) app scope? need them app.

for deviceready piece use scope.on($viewcontentloaded, $statechangesuccess) or other built in listener fits needs instead.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -