Languages

Menu
Sites
Language
[Wearable] Using tizen API in web worker's script

Hello, guys.

I am Tizen newbie and JS newbie as well.

I tried to use tizen API in worker.js not app.js. 

Why I use Tizen API in worker.js is that I need to write a data from sensors to specific text file continuously.

It was success to run in app.js that the very first script when I made this project.

 

However, in worker.js, I cannot use tizen API and tizen studio gave me below error message.

Can't find variable: tizen.

 

Question 1 : Is it possible using tizen API in other javascript file?

Question 2 -1 : If it is possible, do I have to specify something for worker.js?

Question 2-2 : If it is impossible, what should I do for above situation? Could you suggest ?

Edited by: Jungmo Ahn on 28 Jul, 2017

Responses

3 Replies
Slawek Kowalski

No, you can't use tizen object in workers becuase tizen object is not visible in worker process.

You can change data between your main proces and worker via messages system.

More about it you find here https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers

 

Generally, workers should be used to do something in background. It works like Thread() from Java.

Workers are only way to run sth in separate thread. Functions setTimeout()/setInterval() work in main thread.

Armaan-Ul- Islam

Question 1 : Is it possible using tizen API in other javascript file?

Answer: Yes, Of course. You can use tizen API in any javascrpit file. That don't need to be default 'app.js', it could be anything 'app2.js', 'myApp.js', etc. (Include the JS file in index.html)

 

But not from Web workers, as workers run in another global context, which is already mentioned by Slawek Kowalski.

 

Question 2 -1 : If it is possible, do I have to specify something for worker.js?

Answer: I don't think so.

 

Question 2-2 : If it is impossible, what should I do for above situation? Could you suggest ?

Suggestion:

You can use Tizen Sensor APIs from any JS file, for example we say 'sensorData.js' . Now the new methodology would be to read sensor data using tizen API in 'sensorData.js' and send the data as message to 'worker.js'. The web worker would write the sensor data on text file for you on the background optimizing your app's performance. Don't know which api you are using for writing to text file, I hope it would work. Otherwise, you have to skip web workers.

 

sensorData.js

var data = -1;
var worker1 = new Worker("js/worker.js");

function readSensorData() {
    data = tizen.sensor.data();             // don't write this line, it's just example pseudo code
    console.log("log on script: "+ data);	
    worker1.postMessage(data);              // send data to web worker
	
    setTimeout("readSensorData()",9000);   // recursively call the function to read sensor data 
}

readSensorData();

 

worker.js

onmessage=function(event){
    console.log("log on worker: "+ event.data);
    writeToFile(event.data);    //example line of code
}