UNPKG

artik-sdk

Version:

SDK for developing applications for Samsung's ARTIK platform

104 lines (92 loc) 3.11 kB
var Zigbee = require('../src/zigbee').Zigbee var DimmableLight = require('../src/zigbee').ZigbeeDevices.DIMMABLE_LIGHT var options = { withReset: false, mode: 'default' } process.argv.forEach(function (opt) { if (opt === '--reset') { options.withReset = true } else if (opt === 'ezmode') { options.mode = 'ezmode' } }) var znode = new Zigbee() var devDimmableLight = new DimmableLight(znode, 20) znode.on('network_notification', function (event) { switch (event.status) { case 'find_form': /* Coordinator: Form success. */ /* Allow to join this network during 3 minutes (60secs * 3) */ console.log('form success. permitjoin(3 minutes)') znode.network_permitjoin(60 * 3) break case 'find_form_failed': /* Coordinator: Form failed. */ /* Leave from current network and retry */ console.log('form failed') znode.network_leave() znode.network_form() break case 'join': /* Coordinator: Client joined */ console.log('new router/end-node joined') znode.device_discover() break case 'find_join': /* Router: Join success */ console.log('join success') znode.device_discover() break case 'find_join_failed': /* Router: Join failed. */ /* Change role to Coordinator */ console.log('join failed') znode.network_form() break default: break } }) znode.on('device_discover', function (event) { /* Device discovered */ if (event.status === 'found') { console.log('device found:', event.device) } else { console.log('device discover status:', event.status) } }) znode.on('attribute_change', function (event) { if (devDimmableLight.endpoint_id === event.endpoint_id) { /* Light status changed */ console.log('light status =', devDimmableLight.onoff_get_value()) console.log('light level =', devDimmableLight.level_control_get_value()) } }) znode.on('commissioning_status', function (event) { console.log('ezmode commissioning_status:', event.status) }) znode.initialize() znode.network_start() if (options.withReset) { /* Reset network status (leave from network & cleanup bind table) */ console.log('reset') znode.reset_local() } var status = znode.network_request_my_network_status() console.log('- current network_status =', status) console.log('- current device_node_type =', znode.device_request_my_node_type()) if (options.mode === 'ezmode') { /* EZMode commissioning - automatic join/form */ devDimmableLight.ezmode_commissioning_target_start() } else if (options.mode === 'default') { if (status === 'joined_network' || status === 'joining_network') { /* Network is already configured - Get dimmable light status */ console.log('- current light status =', devDimmableLight.onoff_get_value()) console.log('- current light level =', devDimmableLight.level_control_get_value()) } else { /* Try to join the existing network (Router role) */ /* Coordinator should permit(network_permitjoin(N-secs)) this joining. */ znode.network_join() } }