在先前的教程"在Ubuntu OS上创建一个dianping Scope (Qt JSON)",我们知道如何使用C++来在Ubuntu平台上开发一个Scope;我们也在文章"使用golang来设计我们的Ubuntu Scope"里展示了如何使用go语言来在Ubuntu上开发一个Scope.在今天的文章中,我们来展示如何利用Javascript语言来开发一个Scope.这对于一些网页开发的开发者来说,无疑是一个天大的好消息,因为你们不需要学习另外一种语言就可以轻松地开发一个属于你们自己的Scope.更多关于Scope开发的知识可以在网址https://developer.ubuntu.com/en/scopes/
$ sudo apt install unity-js-scopes-dev $ unity-js-scopes-tool setup
{"error":0,"status":"success","date":"2016-01-18","results":[{"currentCity":"北京","pm25":"13","index":[{"title":"穿衣","zs":"寒冷","tipt":"穿衣指数","des":"天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。"},{"title":"洗车","zs":"较适宜","tipt":"洗车指数","des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},{"title":"旅游","zs":"一般","tipt":"旅游指数","des":"天气较好,温度稍低,而且风稍大,让您感觉有些冷,会对外出有一定影响,外出注意防风保暖。"},{"title":"感冒","zs":"极易发","tipt":"感冒指数","des":"天气寒冷,昼夜温差极大且空气湿度较大,易发生感冒,请注意适当增减衣服,加强自我防护避免感冒。"},{"title":"运动","zs":"较不宜","tipt":"运动指数","des":"天气较好,但考虑天气寒冷,风力较强,推荐您进行室内运动,若在户外运动请注意保暖并做好准备活动。"},{"title":"紫外线强度","zs":"弱","tipt":"紫外线强度指数","des":"紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。"}],"weather_data":[{"date":"周一 01月18日 (实时:-8℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"北风3-4级","temperature":"-4 ~ -11℃"},{"date":"周二","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"晴转多云","wind":"微风","temperature":"-1 ~ -8℃"},{"date":"周三","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/yin.png","weather":"多云转阴","wind":"微风","temperature":"0 ~ -7℃"},{"date":"周四","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/yin.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"阴转多云","wind":"微风","temperature":"-3 ~ -6℃"}]}]}
liuxg@liuxg:~/release/chinaweatherjs$ tree . ├── chinaweatherjs.apparmor ├── CMakeLists.txt ├── CMakeLists.txt.user ├── manifest.json.in ├── po │ ├── chinaweatherjs.pot │ ├── CMakeLists.txt │ ├── Makefile.in.in │ ├── POTFILES.in │ └── POTFILES.in.in └── src ├── chinaweatherjs.js ├── CMakeLists.txt ├── data │ ├── chinaweatherjs.ini.in │ ├── chinaweatherjs-settings.ini.in │ ├── icon.png │ └── logo.png ├── etc └── node_modules ├── last-build-arch.txt └── unity-js-scopes ├── bin │ └── unity-js-scopes-launcher ├── index.js ├── lib │ └── scope-core.js └── unity_js_scopes_bindings.node 8 directories, 20 files
$ unity-js-scopes-tool install <path/to/project/src/node_modules> <npm package>
var scopes = require('unity-js-scopes') scopes.self.initialize({}, {});
Object.defineProperty( module.exports, "self", { get: function() { if (! self) { self = new Scope(); } return self; }, });
var scopes = require('unity-js-scopes') scopes.self.initialize({}, { run: function() { console.log('Running...'); }, start: function(scope_id) { console.log('Starting scope id: ' + scope_id + ', ' + scopes.self.scope_config) }, search: function(canned_query, metadata) { return null }, preview: function(result, metadata) { return null }, }});
var query_host = "api.map.baidu.com" var weather_path = "/telematics/v3/weather?output=json&ak=DdzwVcsGMoYpeg5xQlAFrXQt&location=" var URI = "http://www.weather.com.cn/html/weather/101010100.shtml";
search: function(canned_query, metadata) { return new scopes.lib.SearchQuery( canned_query, metadata, // run function(search_reply) { var qs = canned_query.query_string(); if (!qs) { qs = "北京" } console.log("query string: " + qs); var weather_cb = function(response) { var res = ''; // Another chunk of data has been recieved, so append it to res response.on('data', function(chunk) { res += chunk; }); // The whole response has been recieved response.on('end', function() { // console.log("res: " + res); r = JSON.parse(res); // Let's get the detailed info var request_date = r.date console.log("date: " + date); var city = r.results[0].currentCity; console.log("city: " + city); var pm25 = r.results[0].pm25 console.log("pm25: " + pm25) var category_renderer = new scopes.lib.CategoryRenderer(JSON.stringify(WEATHER_TEMPLATE)); var category = search_reply.register_category("Chineweather", city, "", category_renderer); try { r = JSON.parse(res); var length = r.results[0].weather_data.length console.log("length: " + length) for (var i = 0; i < length; i++) { var categorised_result = new scopes.lib.CategorisedResult(category); var date = r.results[0].weather_data[i].date console.log("date: "+ date); var dayPictureUrl = r.results[0].weather_data[i].dayPictureUrl; console.log("dayPictureUrl: " + dayPictureUrl); var nightPictureUrl = r.results[0].weather_data[i].nightPictureUrl; console.log("nightPictureUrl: " + nightPictureUrl); var weather = r.results[0].weather_data[i].weather; console.log("weather: " + weather); var wind = r.results[0].weather_data[i].wind; console.log("wind: " + wind); var temperature = r.results[0].weather_data[i].temperature; console.log("temperature: " + temperature); categorised_result.set("weather", weather); categorised_result.set("wind", wind); categorised_result.set("temperature", temperature); categorised_result.set_uri(URI); categorised_result.set_title("白天: " + date ); categorised_result.set_art(dayPictureUrl); categorised_result.set("subtitle", weather); search_reply.push(categorised_result); categorised_result.set_title("夜晚: " + date ); categorised_result.set_art(nightPictureUrl); search_reply.push(categorised_result); } // We are done, call finished() on our search_reply // search_reply.finished(); } catch(e) { // Forecast not available console.log("Forecast for '" + qs + "' is unavailable: " + e) } }); } console.log("request string: " + query_host + weather_path + qs); http.request({host: query_host, path: weather_path + encode_utf8(qs)}, weather_cb).end(); }, // cancelled function() { }); },
preview: function(result, action_metadata) { return new scopes.lib.PreviewQuery( result, action_metadata, // run function(preview_reply) { var layout1col = new scopes.lib.ColumnLayout(1); var layout2col = new scopes.lib.ColumnLayout(2); var layout3col = new scopes.lib.ColumnLayout(3); layout1col.add_column(["imageId", "headerId", "temperatureId", "windId"]); layout2col.add_column(["imageId"]); layout2col.add_column(["headerId", "temperatureId", "windId"]); layout3col.add_column(["imageId"]); layout3col.add_column(["headerId", "temperatureId", "windId"]); layout3col.add_column([]); preview_reply.register_layout([layout1col, layout2col, layout3col]); var header = new scopes.lib.PreviewWidget("headerId", "header"); header.add_attribute_mapping("title", "title"); header.add_attribute_mapping("subtitle", "subtitle"); var image = new scopes.lib.PreviewWidget("imageId", "image"); image.add_attribute_mapping("source", "art"); var temperature = new scopes.lib.PreviewWidget("temperatureId", "text"); temperature.add_attribute_mapping("text", "temperature"); var wind = new scopes.lib.PreviewWidget("windId", "text"); wind.add_attribute_mapping("text", "wind"); preview_reply.push([image, header, temperature, wind ]); preview_reply.finished(); }, // cancelled function() { }); }
$ bzr branch lp:~davidc3/+junk/github-js-scope $ liuxg@liuxg:~/scope/github-js-scope/src$ unity-js-scopes-tool install ./node_modules github