1import * as netmgr from 'netmgr'; 2import * as iot from 'iot'; 3import * as appota from 'appota' 4 5//此脚本包含了wifi连网功能,仅适合haas100、haaseduk1 6 7var productKey = ''; /* your productKey */ 8var deviceName = ''; /* your deviceName */ 9var deviceSecret = ''; /* your deviceSecret */ 10var device; 11var module_name = 'default'; 12var default_ver = '0.0.1'; 13 14var ota; 15var status; 16/* download info */ 17var info = { 18 url: '', 19 store_path: '/data/jsamp/pack.bin', 20 install_path: '/data/jsamp/', 21 length: 0, 22 hashType: '', 23 hash: '' 24} 25 26function createDevice() { 27 device = iot.device({ 28 productKey: productKey, 29 deviceName: deviceName, 30 deviceSecret: deviceSecret, 31 region: 'cn-shanghai', 32 }); 33 34device.on('connect', function () { 35 console.log('(re)connected'); 36 var iotDeviceHandle = device.getDeviceHandle(); 37 console.log('get device handle module'); 38 ota = appota.open(iotDeviceHandle); 39 console.log('report default module ver'); 40 ota.report({ 41 device_handle: iotDeviceHandle, 42 product_key: productKey, 43 device_name: deviceName, 44 module_name: module_name, 45 version: default_ver 46 }); 47 ota.on('new', function(res) { 48 console.log('length is ' + res.length); 49 console.log('module_name is ' + res.module_name); 50 console.log('version is ' + res.version); 51 console.log('url is ' + res.url); 52 console.log('hash is ' + res.hash); 53 console.log('hash_type is ' + res.hash_type); 54 55 info.url = res.url; 56 info.length = res.length; 57 info.module_name = res.module_name; 58 info.version = res.version; 59 info.hash = res.hash; 60 info.hashType = res.hash_type; 61 62 ota.download({ 63 url: info.url, 64 store_path: info.store_path 65 }, function(res) { 66 if (res >= 0) { 67 console.log('download success'); 68 console.log('verify start'); 69 console.log(info.hashType); 70 ota.verify({ 71 length: info.length, 72 hash_type: info.hashType, 73 hash: info.hash, 74 store_path: info.store_path 75 }, function(res) { 76 if (res >= 0) { 77 console.log('verify success'); 78 console.log('upgrade start'); 79 ota.upgrade({ 80 length: info.length, 81 store_path: info.store_path, 82 install_path: info.install_path 83 }, function(res) { 84 if (res >= 0) { 85 console.log('upgrade success') 86 } else { 87 console.log('upgrade failed') 88 } 89 }) 90 } else { 91 console.log('verify failed'); 92 } 93 }) 94 } else { 95 console.log('download failed'); 96 } 97 }); 98 }); 99}); 100} 101 102var network = netmgr.openNetMgrClient({ 103 name: '/dev/wifi0' 104}); 105 106var status; 107status = network.getState(); 108console.log('status is ' + status); 109network.connect({ 110 ssid: '', //请替换为自己的热点ssid 111 password: '' //请替换为自己热点的密码 112}); 113 114network.on('error', function () { 115 console.log('error ...'); 116}); 117 118network.on('connect', function () { 119 createDevice(); 120}); 121 122