This shows you the differences between two versions of the page.
sample_code:file_service [2015/12/17 17:00] stevegarman created |
sample_code:file_service [2017/08/26 05:22] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Web file service ===== | ||
- | Sample service to keep checking whether a file on a webserver has changed | ||
- | ==== The code ==== | ||
- | === User interface === | ||
- | Create a new DroidScript JavaScript project with the following code in the main .js file | ||
- | <code JavaScript mainApp.js> | ||
- | var svc; | ||
- | //Called when application is started. | ||
- | function OnStart() | ||
- | { | ||
- | //Create a layout. | ||
- | var lay = app.CreateLayout( " | ||
- | |||
- | //Create a 'Stop Service' | ||
- | var btn = app.CreateButton( "Stop Service", | ||
- | lay.AddChild( btn ); | ||
- | btn.SetOnTouch( btn_OnTouchStop ); | ||
- | |||
- | //Add layout to app. | ||
- | app.AddLayout( lay ); | ||
- | | ||
- | // | ||
- | svc = app.CreateService( " | ||
- | //uncomment next line to cause your service to start at boot. | ||
- | // | ||
- | } | ||
- | |||
- | //Called after our service has started. | ||
- | function OnServiceReady() | ||
- | { | ||
- | console.log( " | ||
- | var running=false; | ||
- | var me=app.GetPackageName()+": | ||
- | var lst = app.GetRunningServices(); | ||
- | for (var key in lst) | ||
- | { | ||
- | if(lst[key].name === me) | ||
- | { | ||
- | console.log(me + " running" | ||
- | break; | ||
- | } | ||
- | } | ||
- | } | ||
- | |||
- | function btn_OnTouchStop() | ||
- | { | ||
- | //Tell service we are quitting. | ||
- | svc.SendMessage( " | ||
- | //give the service time to clean up | ||
- | setTimeout(delayedStop, | ||
- | | ||
- | } | ||
- | |||
- | function delayedStop() | ||
- | { | ||
- | //Stop the service. | ||
- | svc.Stop(); | ||
- | // exit main app | ||
- | app.Exit(); | ||
- | } | ||
- | |||
- | function OnData( isStartUp ) | ||
- | |||
- | { | ||
- | var id=app.GetNotifyId(); | ||
- | |||
- | if(id === " | ||
- | { | ||
- | app.Alert(" | ||
- | } | ||
- | |||
- | } | ||
- | </ | ||
- | === Service.js === | ||
- | Add a second file called Service.js with the following code | ||
- | <code JavaScript Service.js> | ||
- | var g_ = {lastResponse:"" | ||
- | |||
- | function OnStart() | ||
- | { | ||
- | app.ShowPopup(" | ||
- | g_.notify=app.CreateNotification(); | ||
- | //Check file | ||
- | SendRequest(); | ||
- | //Check again every 30 seconds | ||
- | g_.interval = setInterval(SendRequest, | ||
- | } | ||
- | |||
- | //Send an http get request. | ||
- | function SendRequest() | ||
- | { | ||
- | app.HttpRequest( " | ||
- | " | ||
- | "/ | ||
- | "", | ||
- | HandleReply, | ||
- | "" | ||
- | } | ||
- | |||
- | //Handle the server' | ||
- | function HandleReply( error, response ) | ||
- | { | ||
- | if( !error ) | ||
- | { | ||
- | if(g_.lastResponse !== response) | ||
- | { | ||
- | | ||
- | | ||
- | | ||
- | } else console.log(" | ||
- | } | ||
- | else | ||
- | { | ||
- | app.ShowPopup( " | ||
- | } | ||
- | } | ||
- | |||
- | //Called when we get a service message. | ||
- | function OnMessage( msg ) | ||
- | { | ||
- | //Show debug in WiFi IDE. | ||
- | console.log( msg ); | ||
- | | ||
- | //Handle messages from main App. | ||
- | if( msg==" | ||
- | { | ||
- | clearInterval(g_.interval); | ||
- | console.log(" | ||
- | g_.notify.Cancel(" | ||
- | } | ||
- | } | ||
- | </ |