This repository has been archived on 2022-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
AriaNg/app/scripts/services/aria2RpcService.js

333 lines
12 KiB
JavaScript
Raw Normal View History

2016-05-13 18:09:12 +02:00
(function () {
'use strict';
2016-05-31 16:51:12 +02:00
angular.module('ariaNg').factory('aria2RpcService', ['$q', 'aria2RpcConstants', 'ariaNgCommonService', 'ariaNgSettingService', 'aria2HttpRpcService', 'aria2WebSocketRpcService', function ($q, aria2RpcConstants, ariaNgCommonService, ariaNgSettingService, aria2HttpRpcService, aria2WebSocketRpcService) {
var protocol = ariaNgSettingService.getProtocol();
2016-05-30 19:26:41 +02:00
var getAria2MethodFullName = function (methodName) {
return aria2RpcConstants.rpcServiceName + '.' + methodName;
};
2016-05-13 18:09:12 +02:00
var invoke = function (method, context) {
2016-05-31 16:51:12 +02:00
context.uniqueId = ariaNgCommonService.generateUniqueId();
2016-05-13 18:09:12 +02:00
context.requestBody = {
jsonrpc: aria2RpcConstants.rpcServiceVersion,
2016-05-31 16:51:12 +02:00
method: (method.indexOf(aria2RpcConstants.rpcSystemServiceName + '.') != 0 ? getAria2MethodFullName(method) : method),
2016-05-13 18:09:12 +02:00
id: context.uniqueId,
params: context.params
};
if (protocol == 'ws') {
return aria2WebSocketRpcService.request(context);
} else {
return aria2HttpRpcService.request(context);
}
2016-05-13 18:09:12 +02:00
};
2016-05-30 19:26:41 +02:00
var invokeMulti = function (methodFunc, contexts, keyProperty, callback) {
var promises = [];
var results = {};
for (var i = 0; i < contexts.length; i++) {
contexts[i].callback = function (result) {
var key = this[keyProperty];
results[key] = result;
};
promises.push(methodFunc(contexts[i]));
}
return $q.all(promises).then(function () {
if (callback) {
callback(results);
}
});
};
2016-05-13 18:09:12 +02:00
return {
2016-05-30 19:26:41 +02:00
getBasicTaskParams: function () {
return [
'gid',
'totalLength',
'completedLength',
'uploadSpeed',
'downloadSpeed',
'connections',
'numSeeders',
'seeder',
'status'
];
},
getFullTaskParams: function () {
var requestParams = this.getBasicTaskParams();
requestParams.push('files');
requestParams.push('bittorrent');
return requestParams;
},
// addUri: function (context) {
// return invoke('addUri', context);
// },
// addTorrent: function (context) {
// return invoke('addTorrent', context);
// },
// addMetalink: function (context) {
// return invoke('addMetalink', context);
// },
2016-05-13 18:09:12 +02:00
remove: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('remove', {
params: [context.gid],
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
forceRemove: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('forceRemove', {
params: [context.gid],
callback: context.callback
});
},
forceRemoveMulti: function (context) {
var contexts = [];
for (var i = 0; i < context.gids.length; i++) {
contexts.push({
gid: context.gids[i]
});
}
return invokeMulti(this.forceRemove, contexts, 'gid', context.callback);
2016-05-13 18:09:12 +02:00
},
pause: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('pause', {
params: [context.gid],
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
pauseAll: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('pauseAll', {
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
forcePause: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('forcePause', {
params: [context.gid],
callback: context.callback
});
},
forcePauseMulti: function (context) {
var contexts = [];
for (var i = 0; i < context.gids.length; i++) {
contexts.push({
gid: context.gids[i]
});
}
return invokeMulti(this.forcePause, contexts, 'gid', context.callback);
2016-05-13 18:09:12 +02:00
},
forcePauseAll: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('forcePauseAll', {
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
unpause: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('unpause', {
params: [context.gid],
callback: context.callback
});
},
unpauseMulti: function (context) {
var contexts = [];
for (var i = 0; i < context.gids.length; i++) {
contexts.push({
gid: context.gids[i]
});
}
return invokeMulti(this.unpause, contexts, 'gid', context.callback);
2016-05-13 18:09:12 +02:00
},
unpauseAll: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('unpauseAll', {
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
tellStatus: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('tellStatus', {
params: [context.gid],
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
getUris: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('getUris', {
params: [context.gid],
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
getFiles: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('getFiles', {
params: [context.gid],
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
getPeers: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('getPeers', {
params: [context.gid],
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
getServers: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('getServers', {
params: [context.gid],
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
tellActive: function (context) {
2016-05-30 19:26:41 +02:00
var requestContext = {
callback: context.callback
};
if (context.requestParams) {
requestContext.params = [context.requestParams];
}
return invoke('tellActive', requestContext);
2016-05-13 18:09:12 +02:00
},
tellWaiting: function (context) {
2016-05-30 19:26:41 +02:00
var requestContext = {
params: [0, 1000],
callback: context.callback
};
if (!angular.isUndefined(context.offset)) {
requestContext.params[0] = context.offset;
}
if (!angular.isUndefined(context.num)) {
requestContext.params[1] = context.num;
}
if (context.requestParams) {
requestContext.params.push(context.requestParams);
}
return invoke('tellWaiting', requestContext);
2016-05-13 18:09:12 +02:00
},
tellStopped: function (context) {
2016-05-30 19:26:41 +02:00
var requestContext = {
params: [0, 1000],
callback: context.callback
};
if (!angular.isUndefined(context.offset)) {
requestContext.params[0] = context.offset;
}
if (!angular.isUndefined(context.num)) {
requestContext.params[1] = context.num;
}
if (context.requestParams) {
requestContext.params.push(context.requestParams);
}
return invoke('tellStopped', requestContext);
},
changePosition: function (context) {
return invoke('changePosition', {
params: [context.gid, context.pos, context.how],
callback: context.callback
});
},
2016-05-30 19:26:41 +02:00
// changeUri: function (context) {
// return invoke('changeUri', context);
// },
2016-05-13 18:09:12 +02:00
getOption: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('getOption', {
params: [context.gid],
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
changeOption: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('changeOption', {
params: [context.gid, context.options],
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
getGlobalOption: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('getGlobalOption', {
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
changeGlobalOption: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('changeGlobalOption', {
params: [context.options],
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
getGlobalStat: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('getGlobalStat', {
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
purgeDownloadResult: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('purgeDownloadResult', {
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
removeDownloadResult: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('removeDownloadResult', {
params: [context.gid],
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
getVersion: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('getVersion', {
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
getSessionInfo: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('getSessionInfo', {
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
shutdown: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('shutdown', {
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
forceShutdown: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('forceShutdown', {
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
saveSession: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('saveSession', {
callback: context.callback
});
2016-05-13 18:09:12 +02:00
},
multicall: function (context) {
2016-05-30 19:26:41 +02:00
var requestContext = {
params: [],
callback: context.callback
};
if (angular.isArray(context.methods) && context.methods.length > 0) {
for (var i = 0; i < context.methods.length; i++) {
var method = context.methods[i];
requestContext.params.push([method]);
}
}
return invoke('system.multicall', requestContext);
2016-05-13 18:09:12 +02:00
},
listMethods: function (context) {
2016-05-30 19:26:41 +02:00
return invoke('system.listMethods', {
callback: context.callback
});
2016-05-13 18:09:12 +02:00
}
};
}]);
})();