Best JavaScript code snippet using devicefarmer-stf
devices.js
Source:devices.js
...98 }99 const group = lock.group = stats.changes[0].new_val100 if (group.devices.indexOf(serial) > -1) {101 return apiutil.isOriginGroup(group.class) ?102 dbapi.removeOriginGroupDevice(group, serial) :103 dbapi.removeGroupDevices(group, [serial])104 }105 return group106 })107 .finally(function() {108 lockutil.unlockGroup(lock)109 })110 }111 function deleteDeviceInDatabase() {112 function wrappedDeleteDeviceInDatabase() {113 const result = {114 status: false115 , data: 'not deleted'116 }117 return dbapi.loadDeviceBySerial(serial).then(function(device) {118 if (device && device.group.id === device.group.origin) {119 return deleteGroupDevice(device.group.owner.email, device.group.id)120 .then(function(group) {121 if (group !== 'not found') {122 return dbapi.deleteDevice(serial).then(function() {123 result.status = true124 result.data = 'deleted'125 })126 }127 return false128 })129 }130 return false131 })132 .then(function() {133 return result134 })135 }136 return apiutil.setIntervalWrapper(137 wrappedDeleteDeviceInDatabase138 , 10139 , Math.random() * 500 + 50)140 }141 return dbapi.lockDeviceByOrigin(req.user.groups.subscribed, serial).then(function(stats) {142 if (!stats.replaced) {143 return apiutil.lightComputeStats(res, stats)144 }145 const device = lock.device = stats.changes[0].new_val146 if (!anyPresentState && device.present !== presentState ||147 !anyControllingState && (device.owner === null) === controllingState ||148 !anyNotesState &&149 (typeof device.notes !== 'undefined' && device.notes !== '') !== notesState ||150 !anyBookingState && (device.group.id !== device.group.origin && !bookingState ||151 device.group.class === apiutil.STANDARD && bookingState)) {152 return 'unchanged'153 }154 if (device.group.class === apiutil.STANDARD) {155 return deleteDeviceInDatabase()156 }157 return dbapi.getDeviceTransientGroups(serial).then(function(groups) {158 if (groups.length && !anyBookingState && !bookingState) {159 return 'unchanged'160 }161 return Promise.each(groups, function(group) {162 return deleteGroupDevice(group.owner.email, group.id)163 })164 .then(function() {165 if (!groups.length && !anyBookingState && bookingState) {166 return 'unchanged'167 }168 return deleteDeviceInDatabase()169 })170 })171 })172 .finally(function() {173 lockutil.unlockDevice(lock)174 })175}176/* ------------------------------------ PUBLIC FUNCTIONS ------------------------------- */177function getDevices(req, res) {178 const target = req.swagger.params.target.value179 switch(target) {180 case apiutil.BOOKABLE:181 getGenericDevices(req, res, dbapi.loadBookableDevices)182 break183 case apiutil.ORIGIN:184 getGenericDevices(req, res, dbapi.loadDevicesByOrigin)185 break186 case apiutil.STANDARD:187 getGenericDevices(req, res, dbapi.loadStandardDevices)188 break189 case apiutil.STANDARDIZABLE:190 getStandardizableDevices(req, res)191 break192 default:193 getGenericDevices(req, res, dbapi.loadDevices)194 }195}196function getDeviceBySerial(req, res) {197 var serial = req.swagger.params.serial.value198 var fields = req.swagger.params.fields.value199 dbapi.loadDevice(req.user.groups.subscribed, serial)200 .then(function(cursor) {201 cursor.next(function(err, device) {202 if (err) {203 return res.status(404).json({204 success: false205 , description: 'Device not found'206 })207 }208 let responseDevice = apiutil.publishDevice(device, req.user)209 if (fields) {210 responseDevice = _.pick(device, fields.split(','))211 }212 res.json({213 success: true214 , device: responseDevice215 })216 })217 })218 .catch(function(err) {219 log.error('Failed to load device "%s": ', serial, err.stack)220 res.status(500).json({221 success: false222 })223 })224}225function getDeviceGroups(req, res) {226 const serial = req.swagger.params.serial.value227 const fields = req.swagger.params.fields.value228 dbapi.loadDevice(req.user.groups.subscribed, serial).then(function(cursor) {229 return cursor.toArray()230 })231 .then(function(devices) {232 if (!devices.length) {233 apiutil.respond(res, 404, 'Not Found (device)')234 }235 else {236 getDeviceFilteredGroups(serial, fields, false)237 .then(function(groups) {238 return apiutil.respond(res, 200, 'Groups Information', {groups: groups})239 })240 }241 })242 .catch(function(err) {243 apiutil.internalError(res, 'Failed to get device groups: ', err.stack)244 })245}246function getDeviceBookings(req, res) {247 const serial = req.swagger.params.serial.value248 const fields = req.swagger.params.fields.value249 dbapi.loadDevice(req.user.groups.subscribed, serial).then(function(cursor) {250 return cursor.toArray()251 })252 .then(function(devices) {253 if (!devices.length) {254 apiutil.respond(res, 404, 'Not Found (device)')255 }256 else {257 getDeviceFilteredGroups(serial, fields, true)258 .then(function(bookings) {259 apiutil.respond(res, 200, 'Bookings Information', {bookings: bookings})260 })261 }262 })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'419 function removeDevices(serials) {420 let results = []421 return Promise.each(serials, function(serial) {422 return removeDevice(serial, req, res).then(function(result) {423 if (result === 'not deleted') {424 apiutil.respond(res, 503, 'Server too busy [code: 2], please try again later')425 return Promise.reject('busy')426 }427 return results.push(result)...
Using AI Code Generation
1var client = require('devicefarmer-stf-client');2var options = {3 auth: {4 }5};6var stf = client(options);7stf.removeOriginGroupDevice(1, 2, function(err, data) {8 if (err) {9 console.log(err);10 } else {11 console.log(data);12 }13});14{15}16removeOriginGroupDevice(originGroupId, deviceId, callback)17stf.removeOriginGroupDevice(1, 2, function(err, data) {18 if (err) {19 console.log(err);20 } else {21 console.log(data);22 }23});24stf.removeOriginGroupDevice(1, 2).then(function(data) {25 console.log(data);26}, function(err) {27 console.log(err);28});29removeOriginGroupDeviceById()30var client = require('devicefarmer-stf-client');31var options = {32 auth: {33 }34};35var stf = client(options);36stf.removeOriginGroupDeviceById(1, 2, function(err, data) {37 if (err) {38 console.log(err);39 } else {40 console.log(data);41 }42});43{44}45removeOriginGroupDeviceById(originGroupId, deviceId, callback)46stf.removeOriginGroupDeviceById(1, 2, function(err, data) {47 if (err) {48 console.log(err);49 } else {50 console.log(data);51 }52});53stf.removeOriginGroupDeviceById(1, 2).then
Using AI Code Generation
1var devicefarmer = require("devicefarmer-stf-client");2var client = new devicefarmer.Client();3var group = new devicefarmer.Group();4var device = new devicefarmer.Device();5var group_id = "group-id";6var device_id = "device-id";7client.removeOriginGroupDevice(group_id, device_id, function(err, res){8 if(err){9 console.log(err);10 }else{11 console.log(res);12 }13});14var devicefarmer = require("devicefarmer-stf-client");15var client = new devicefarmer.Client();16var group = new devicefarmer.Group();17var device = new devicefarmer.Device();18var group_id = "group-id";19var device_id = "device-id";20client.removeGroupDevice(group_id, device_id, function(err, res){21 if(err){22 console.log(err);23 }else{24 console.log(res);25 }26});27var devicefarmer = require("devicefarmer-stf-client");28var client = new devicefarmer.Client();29var group = new devicefarmer.Group();30var device = new devicefarmer.Device();31var group_id = "group-id";32client.getGroupDevices(group_id, function(err, res){33 if(err){34 console.log(err);35 }else{36 console.log(res);37 }38});39var devicefarmer = require("devicefarmer-stf-client");40var client = new devicefarmer.Client();41var group = new devicefarmer.Group();42var device = new devicefarmer.Device();43var group_id = "group-id";44client.getGroupDevices(group_id, function(err, res){45 if(err){46 console.log(err);47 }else{48 console.log(res);49 }50});51var devicefarmer = require("devicefarmer-stf-client");52var client = new devicefarmer.Client();53var group = new devicefarmer.Group();54var device = new devicefarmer.Device();55var group_id = "group-id";56client.getGroupDevices(group_id, function(err
Using AI Code Generation
1var stf = require('devicefarmer-stf');2var originGroup = 'test';3var originDevice = 'testDevice';4client.removeOriginGroupDevice(originGroup, originDevice, function(err, res){5 if(err){6 console.log(err);7 }else{8 console.log(res);9 }10});11var stf = require('devicefarmer-stf');12var originGroup = 'test';13var originDevice = 'testDevice';14client.removeOriginGroupDevice(originGroup, originDevice)15 .then(function(res){16 console.log(res);17 })18 .catch(function(err){19 console.log(err);20 });21var stf = require('devicefarmer-stf');22var originGroup = 'test';23var originDevice = 'testDevice';24client.removeOriginGroupDevice(originGroup, originDevice)25 .then(function(res){26 console.log(res);27 })28 .catch(function(err){29 console.log(err);30 });31var stf = require('devicefarmer-stf');32var originGroup = 'test';33var originDevice = 'testDevice';34client.removeOriginGroupDevice(originGroup, originDevice, function(err, res){35 if(err){36 console.log(err);37 }else{38 console.log(res);39 }40});41var stf = require('devicefarmer-stf');42var originGroup = 'test';43var originDevice = 'testDevice';44client.removeOriginGroupDevice(originGroup, originDevice, function(err, res){45 if(err){46 console.log(err);47 }else{48 console.log(res);49 }50});
Using AI Code Generation
1const STF = require('devicefarmer-stf');2stf.removeOriginGroupDevice('someDeviceId');3const STF = require('devicefarmer-stf');4stf.addOriginGroupDevice('someDeviceId');5const STF = require('devicefarmer-stf');6stf.addOriginGroupDevice('someDeviceId');7const STF = require('devicefarmer-stf');8stf.addOriginGroupDevice('someDeviceId');9const STF = require('devicefarmer-stf');10stf.addOriginGroupDevice('someDeviceId');11const STF = require('devicefarmer-stf');12stf.addOriginGroupDevice('someDeviceId');13const STF = require('devicefarmer-stf');
Using AI Code Generation
1var stf = require('devicefarmer-stf-client');2var client = new stf.Client();3client.removeOriginGroupDevice('originGroup', 'serialNumber').then(function (data) {4 console.log(data);5}).catch(function (err) {6 console.log(err);7});8var stf = require('devicefarmer-stf-client');9var client = new stf.Client();10client.removeOriginGroupDevices('originGroup', 'serialNumber1', 'serialNumber2').then(function (data) {11 console.log(data);12}).catch(function (err) {13 console.log(err);14});15var stf = require('devicefarmer-stf-client');16var client = new stf.Client();17client.removeOriginGroupDevices('originGroup', ['serialNumber1', 'serialNumber2']).then(function (data) {18 console.log(data);19}).catch(function (err) {20 console.log(err);21});22var stf = require('devicefarmer-stf-client');23var client = new stf.Client();24client.removeOriginGroupDevices('originGroup', ['serialNumber1', 'serialNumber2']).then(function (data) {25 console.log(data);26}).catch(function (err) {27 console.log(err);28});29var stf = require('devicefarmer-stf-client');30var client = new stf.Client();31client.getOriginGroupDevices('originGroup').then(function (data) {32 console.log(data);33}).catch(function (err) {34 console.log(err);35});36var stf = require('devicefarmer-stf-client');37var client = new stf.Client();38client.getOriginGroupDevices('originGroup', 1, 10).then(function (data) {39 console.log(data);40}).catch(function (err) {41 console.log(err);42});
Using AI Code Generation
1var DeviceFarmer = require('devicefarmer-stf');2df.removeOriginGroupDevice('group_name', 'device_serial', function(err, res) {3 if (err) return console.error(err);4 console.log(res);5});6df.addOriginGroupDevice('group_name', 'device_serial', function(err, res) {7 if (err) return console.error(err);8 console.log(res);9});
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!!