|
| 1 | +// Copyright 2016 Intel Corporation |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// The file names for client and server are reversed in this test, because the test suite uses the |
| 16 | +// file name to decide which to run first ("server.js"). In the case of presence, however, the |
| 17 | +// client must run first, because it must catch the announcement from the server when it comes up. |
| 18 | + |
| 19 | +var client = require( process.argv[ 3 ] ).client; |
| 20 | + |
| 21 | +console.log( JSON.stringify( { assertionCount: 1 } ) ); |
| 22 | + |
| 23 | +var interesting = {}; |
| 24 | + |
| 25 | +var devices = []; |
| 26 | +var resources = []; |
| 27 | + |
| 28 | +// Collect the information we need for setting up the "delete" listener |
| 29 | +function testSetUp() { |
| 30 | + var device, resource; |
| 31 | + |
| 32 | + while ( devices.length > 0 && !interesting.device ) { |
| 33 | + device = devices.shift(); |
| 34 | + if ( device.name === "test-device-" + process.argv[ 2 ] ) { |
| 35 | + interesting.device = device; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + while ( resources.length > 0 && interesting.device && |
| 40 | + !( interesting.object && interesting.control ) ) { |
| 41 | + resource = resources.shift(); |
| 42 | + if ( resource.deviceId === interesting.device.uuid ) { |
| 43 | + if ( resource.resourcePath === "/a/" + process.argv[ 2 ] ) { |
| 44 | + interesting.object = resource; |
| 45 | + } else if ( resource.resourcePath === "/target-resource" ) { |
| 46 | + interesting.control = resource; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if ( interesting.device && interesting.control && interesting.object ) { |
| 52 | + client |
| 53 | + .removeListener( "resourcefound", resourcefound ) |
| 54 | + .removeListener( "devicefound", devicefound ); |
| 55 | + interesting.object.on( "delete", function() { |
| 56 | + console.log( JSON.stringify( { assertion: "ok", arguments: [ |
| 57 | + true, "Client: Received 'delete' event" |
| 58 | + ] } ) ); |
| 59 | + console.log( JSON.stringify( { finished: 0 } ) ); |
| 60 | + } ); |
| 61 | + interesting.control.properties.uuid = process.argv[ 2 ]; |
| 62 | + client.update( interesting.control ).catch( function( error ) { |
| 63 | + console.log( JSON.stringify( { assertion: "ok", arguments: [ |
| 64 | + false, "Client: Failed to update() control resource: " + ( "" + error ) + "\n" + |
| 65 | + JSON.stringify( error, null, 4 ) |
| 66 | + ] } ) ); |
| 67 | + } ); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +function resourcefound( resource ) { |
| 72 | + resources.push( resource ); |
| 73 | + testSetUp(); |
| 74 | +} |
| 75 | + |
| 76 | +function devicefound( device ) { |
| 77 | + devices.push( device ); |
| 78 | + testSetUp(); |
| 79 | +} |
| 80 | + |
| 81 | +client |
| 82 | + .on( "resourcefound", resourcefound ) |
| 83 | + .on( "devicefound", devicefound ); |
| 84 | + |
| 85 | +console.log( JSON.stringify( { ready: true } ) ); |
0 commit comments