playback-device-server/www/src/services/remotes-service.js

84 lines
1.8 KiB
JavaScript

import Command from "../data/command";
import Serializer from "../data/serializer";
function RemotesService() {
let commands = [
new Command({
id: 1,
protocol: "samsung",
commandNumber: 1,
device: 7,
commandType: "power",
title: "Power Samsung",
}),
new Command({
id: 2,
protocol: "samsung",
commandNumber: 2,
device: 7,
commandType: "input",
title: "Input Samsung",
}),
new Command({
id: 3,
protocol: "samsung",
commandNumber: 3,
device: 7,
commandType: "volume_up",
title: "Volume Up Samsung",
}),
];
let remotes = [];
async function getRemotes() {
return [].concat(remotes);
}
async function createRemote(remoteObject) {
let remote = Serializer.deserializeRemote(remoteObject);
let id = Math.random().toString(36).substr(2, 9);
remote.setId(id);
remotes.push(remote);
return remote;
}
async function deleteRemote(remoteId) {
let index = remotes.findIndex((remote) => remote.getId() === remoteId);
if (index >= 0) {
remotes.splice(index, 1);
}
}
async function getCommands() {
return [].concat(commands);
}
async function createCommand(commandObject) {
let command = Serializer.deserializeCommand(commandObject);
let id = Math.random().toString(36).substr(2, 9);
command.setId(id);
commands.push(command);
return command;
}
async function deleteCommand(commandId) {
let index = commands.findIndex((command) => command.getId() === commandId);
if (index >= 0) {
commands.splice(index, 1);
}
}
return {
getRemotes,
createRemote,
deleteRemote,
getCommands,
createCommand,
deleteCommand,
};
}
RemotesService = new RemotesService();
export default RemotesService;