Best JavaScript code snippet using devicefarmer-stf
devices.js
Source:devices.js
...263 .catch(function(err) {264 apiutil.internalError(res, 'Failed to get device bookings: ', err.stack)265 })266}267function addOriginGroupDevices(req, res) {268 const serials = apiutil.getBodyParameter(req.body, 'serials')269 const fields = apiutil.getQueryParameter(req.swagger.params.fields)270 const target = apiutil.getQueryParameter(req.swagger.params.redirected) ? 'device' : 'devices'271 const lock = {}272 function askUpdateDeviceOriginGroup(group, serial) {273 return new Promise(function(resolve, reject) {274 const signature = util.format('%s', uuid.v4()).replace(/-/g, '')275 let messageListener276 const responseTimer = setTimeout(function() {277 req.options.channelRouter.removeListener(wireutil.global, messageListener)278 apiutil.respond(res, 504, 'Gateway Time-out')279 reject('timeout')280 }, 5000)281 messageListener = wirerouter()282 .on(wire.DeviceOriginGroupMessage, function(channel, message) {283 if (message.signature === signature) {284 clearTimeout(responseTimer)285 req.options.channelRouter.removeListener(wireutil.global, messageListener)286 dbapi.loadDeviceBySerial(serial).then(function(device) {287 if (fields) {288 resolve(_.pick(apiutil.publishDevice(device, req.user), fields.split(',')))289 }290 else {291 resolve(apiutil.publishDevice(device, req.user))292 }293 })294 }295 })296 .handler()297 req.options.channelRouter.on(wireutil.global, messageListener)298 return dbapi.askUpdateDeviceOriginGroup(serial, group, signature)299 })300 }301 function updateDeviceOriginGroup(group, serial) {302 const lock = {}303 return dbapi.lockDeviceByOrigin(req.user.groups.subscribed, serial).then(function(stats) {304 if (!stats.replaced) {305 return apiutil.lightComputeStats(res, stats)306 }307 lock.device = stats.changes[0].new_val308 return dbapi.isUpdateDeviceOriginGroupAllowed(serial, group)309 .then(function(updatingAllowed) {310 if (!updatingAllowed) {311 apiutil.respond(res, 403, 'Forbidden (device is currently booked)')312 return Promise.reject('booked')313 }314 return askUpdateDeviceOriginGroup(group, serial)315 })316 })317 .finally(function() {318 lockutil.unlockDevice(lock)319 })320 }321 function updateDevicesOriginGroup(group, serials) {322 let results = []323 return Promise.each(serials, function(serial) {324 return updateDeviceOriginGroup(group, serial).then(function(result) {325 results.push(result)326 })327 })328 .then(function() {329 const result = target === 'device' ? {device: {}} : {devices: []}330 results = _.without(results, 'unchanged')331 if (!results.length) {332 return apiutil.respond(res, 200, `Unchanged (${target})`, result)333 }334 results = _.without(results, 'not found')335 if (!results.length) {336 return apiutil.respond(res, 404, `Not Found (${target})`)337 }338 if (target === 'device') {339 result.device = results[0]340 }341 else {342 result.devices = results343 }344 return apiutil.respond(res, 200, `Updated (${target})`, result)345 })346 .catch(function(err) {347 if (err !== 'booked' && err !== 'timeout' && err !== 'busy') {348 throw err349 }350 })351 }352 return lockutil.lockGroup(req, res, lock).then(function(lockingSuccessed) {353 if (lockingSuccessed) {354 const group = lock.group355 if (!apiutil.isOriginGroup(group.class)) {356 return apiutil.respond(res, 400, 'Bad Request (this group cannot act as an origin one)')357 }358 if (typeof serials !== 'undefined') {359 return updateDevicesOriginGroup(360 group361 , _.without(serials.split(','), '').filter(function(serial) {362 return group.devices.indexOf(serial) < 0363 })364 )365 }366 return dbapi.loadDevicesByOrigin(req.user.groups.subscribed).then(function(devices) {367 if (group.class === apiutil.BOOKABLE) {368 return devices369 }370 return extractStandardizableDevices(devices)371 })372 .then(function(devices) {373 const serials = []374 devices.forEach(function(device) {375 if (group.devices.indexOf(device.serial) < 0) {376 serials.push(device.serial)377 }378 })379 return updateDevicesOriginGroup(group, serials)380 })381 }382 return false383 })384 .catch(function(err) {385 apiutil.internalError(res, `Failed to update ${target} origin group: `, err.stack)386 })387 .finally(function() {388 lockutil.unlockGroup(lock)389 })390}391function addOriginGroupDevice(req, res) {392 apiutil.redirectApiWrapper('serial', addOriginGroupDevices, req, res)393}394function removeOriginGroupDevices(req, res) {395 const lock = {}396 return lockutil.lockGroup(req, res, lock).then(function(lockingSuccessed) {397 if (lockingSuccessed) {398 const group = lock.group399 if (!apiutil.checkBodyParameter(req.body, 'serials')) {400 req.body = {serials: group.devices.join()}401 }402 return dbapi.getRootGroup().then(function(group) {403 req.swagger.params.id = {value: group.id}404 return addOriginGroupDevices(req, res)405 })406 }407 return false408 })409 .finally(function() {410 lockutil.unlockGroup(lock)411 })412}413function removeOriginGroupDevice(req, res) {414 apiutil.redirectApiWrapper('serial', removeOriginGroupDevices, req, res)415}416function deleteDevices(req, res) {417 const serials = apiutil.getBodyParameter(req.body, 'serials')418 const target = apiutil.getQueryParameter(req.swagger.params.redirected) ? 'device' : 'devices'...
Using AI Code Generation
1var http = require('http');2var request = require('request');3var fs = require('fs');4var options = {5 headers: {6 },7 body: JSON.stringify({8 })9};10request(options, function (error, response, body) {11 if (!error && response.statusCode == 200) {12 console.log(body);13 } else {14 console.log(error);15 }16});17{18}19{20}21var http = require('http');22var request = require('request');23var fs = require('fs');24var options = {25 headers: {26 },27 body: JSON.stringify({28 })29};30request(options, function (error, response, body) {31 if (!error && response.statusCode == 200) {32 console.log(body);33 } else {34 console.log(error);35 }36});
Using AI Code Generation
1var stf = require('devicefarmer-stf');2var deviceGroup = new stf.DeviceGroup();3var devices = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];4deviceGroup.addOriginGroupDevices('origin_group_name', devices, function (err, data) {5 if (err) {6 console.log(err);7 } else {8 console.log(data);9 }10});11var stf = require('devicefarmer-stf');12var deviceGroup = new stf.DeviceGroup();13deviceGroup.getOriginGroupDevices('origin_group_name', function (err, data) {14 if (err) {15 console.log(err);16 } else {17 console.log(data);18 }19});20var stf = require('devicefarmer-stf');21var deviceGroup = new stf.DeviceGroup();22var devices = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];23deviceGroup.deleteOriginGroupDevices('origin_group_name', devices, function (err, data) {24 if (err) {25 console.log(err);26 } else {27 console.log(data);28 }29});30var stf = require('devicefarmer-stf');31var deviceGroup = new stf.DeviceGroup();32deviceGroup.getDeviceGroupDevices('device_group_name', function (err, data) {33 if (err) {34 console.log(err);35 } else {36 console.log(data);37 }38});39var stf = require('devicefarmer-stf');40var deviceGroup = new stf.DeviceGroup();41var devices = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];42deviceGroup.deleteDeviceGroupDevices('device_group_name', devices, function (err, data) {43 if (err) {
Using AI Code Generation
1var devicefarmer = require('devicefarmer-stf');2stf.addOriginGroupDevices('origin', ['device1', 'device2', 'device3'])3 .then(function(response) {4 console.log(response);5 })6 .catch(function(error) {7 console.log(error);8 });9var devicefarmer = require('devicefarmer-stf');10stf.addOriginGroupDevices('origin', ['device1', 'device2', 'device3'])11 .then(function(response) {12 console.log(response);13 })14 .catch(function(error) {15 console.log(error);16 });17var devicefarmer = require('devicefarmer-stf');18stf.addOriginGroupDevices('origin', ['device1', 'device2', 'device3'])19 .then(function(response) {20 console.log(response);21 })22 .catch(function(error) {23 console.log(error);24 });25var devicefarmer = require('devicefarmer-stf');26stf.addOriginGroupDevices('origin', ['device1', 'device2', 'device3'])27 .then(function(response) {28 console.log(response);29 })30 .catch(function(error) {31 console.log(error);32 });33var devicefarmer = require('devicefarmer-stf');34stf.addOriginGroupDevices('origin', ['device1', 'device2', 'device3'])
Using AI Code Generation
1var devicefarmer = require('devicefarmer-stf');2stf.addOriginGroupDevices('group-name', 'origin-name', 'device-serial', function(err, result) {3 if (err) {4 console.log(err);5 } else {6 console.log(result);7 }8});9var devicefarmer = require('devicefarmer-stf');10stf.addOriginGroupDevices('group-name', 'origin-name', 'device-serial', function(err, result) {11 if (err) {12 console.log(err);13 } else {14 console.log(result);15 }16});
Using AI Code Generation
1const DeviceFarmer = require('devicefarmer-stf-client');2client.addOriginGroupDevices('origin_group_name', 'device_serial').then((data) => {3 console.log('Data:', data);4}).catch((err) => {5 console.error('Error:', err);6});7const DeviceFarmer = require('devicefarmer-stf-client');8client.removeOriginGroupDevices('origin_group_name', 'device_serial').then((data) => {9 console.log('Data:', data);10}).catch((err) => {11 console.error('Error:', err);12});13const DeviceFarmer = require('devicefarmer-stf-client');14client.getOriginGroupDevices('origin_group_name').then((data) => {15 console.log('Data:', data);16}).catch((err) => {17 console.error('Error:', err);18});19const DeviceFarmer = require('devicefarmer-stf-client');20client.getOriginGroupDevices('origin_group_name').then((data) => {21 console.log('Data:', data);22}).catch((err) => {23 console.error('Error:', err);24});25const DeviceFarmer = require('devicefarmer-stf-client');26client.getOriginGroupDevices('origin_group_name').then((data) => {27 console.log('Data:', data);28}).catch((err) => {29 console.error('Error:', err);30});31const DeviceFarmer = require('devicefarmer-stf-client');32const client = new DeviceFarmer.Client('http
Using AI Code Generation
1const stf = require('devicefarmer-stf-client');2const groupId = process.argv[2];3const deviceId = process.argv[3];4client.addOriginGroupDevices(groupId, deviceId)5.then(function(result) {6 console.log(result);7})8.catch(function(err) {9 console.error(err);10});11const stf = require('devicefarmer-stf-client');12const groupId = process.argv[2];13const deviceId = process.argv[3];14client.removeOriginGroupDevices(groupId, deviceId)15.then(function(result) {16 console.log(result);17})18.catch(function(err) {19 console.error(err);20});21const stf = require('devicefarmer-stf-client');22const groupId = process.argv[2];23const userId = process.argv[3];24client.addOriginGroupUsers(groupId, userId)25.then(function(result) {26 console.log(result);27})28.catch(function(err) {29 console.error(err);30});31const stf = require('devicefarmer-stf-client');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!