node_aimp
Version:
use aimp remote use module web remote aimp
128 lines (126 loc) • 6.39 kB
JavaScript
const axios = require("axios");
let Methods = [
"Play",
"Pause",
"Stop",
"PlayPrevious",
"PlayNext",
"ShufflePlaybackMode",
"RepeatPlaybackMode",
"GetPlayerControlPanelState",
"VolumeLevel",
"Mute",
"RadioCaptureMode",
"Status",
"EnqueueTrack",
"RemoveTrackFromPlayQueue",
"GetPlaylists",
"CreatePlaylist",
"GetPlaylistEntries",
"GetEntryPositionInDataTable",
"GetPlaylistEntriesCount",
"GetFormattedEntryTitle",
"GetPlaylistEntryInfo",
"SetTrackRating",
"GetCover",
"DownloadTrack",
"SubscribeOnAIMPStateUpdateEvent",
"PluginCapabilities",
"AddURLToPlaylist"
];
class aimp{
active = false;
constructor({port,await_time}){
this.j = 0;
this.port = port?port:3333;
this.await = (getActive,f,time_await)=>new Promise((res,rej)=>{
let i = 0;
time_await = typeof f === "number"?f:typeof time_await === "number"?time_await:typeof getActive === "number"?getActive:false;
getActive = typeof getActive === "boolean"?getActive:false;
let time = setInterval(()=>{
let t = (a,b=false,c)=>{
i = 0;this.active = b;clearInterval(a);
let result = b?{OK:"active"}:{error:"not active"};
return typeof c === "function"?c(result):result;
}
if(time_await){
if(i>=time_await){
rej(f?t(time,this.active,f):t(time,this.active,()=>({error:"exeded time await"})))
}
}
if(typeof getActive === "boolean"){
if(getActive===true){
if(this.active===false){
axios.get(this.getURL())
.then(_=>f?t(time,true,f):res(t(time,true)))
.catch(_=>f?t(time,false,f):rej(t(time,false)))
}
}else if(this.active===true){
t(time,true,f?f:res)
}
}
i++;
})
})
this.await(true,await_time).catch(e=>console.log({error:e}))
}
getURL = ()=>"http://localhost:"+(this.port?this.port:3333)+"/RPC_JSON";
call = (method,params={},f)=>new Promise((res,rej)=>{
this.await().then(_=>axios.post(this.getURL(),{method:method,params:params}).then(e=>e.data?e.data:e).then(e=>e))
.then(e=>e)
.catch(e=>({error:e,run:rej({error:"disconected"})}))
.then(e=>f?(f(e.result?e.result:e.error?e.error:{error:"disconected"},this.method,this.j)):e)
.then(e=>res({response:e,methods:this.method,id:this.j}))
})
setMethod = (array=Methods)=>{
if(typeof array === "object"){
if(array.length){
let result = {};
array.map(e=>{switch(e){
case "PlayPrevious":result["Back"] = (params,f)=>f?this.call(e,params,f):this.call(e,params);break;
case "PlayNext":result["Next"] = (params,f)=>f?this.call(e,params,f):this.call(e,params);break;
case "ShufflePlaybackMode":result["RandomSong"] = (params,f)=>f?this.call(e,params,f):this.call(e,params);break;
case "RepeatPlaybackMode":result["RepeatMode"] = (params,f)=>f?this.call(e,params,f):this.call(e,params);break;
case "GetPlayerControlPanelState":result["GetControl"] = (params,f)=>f?this.call(e,params,f):this.call(e,params);break;
case "VolumeLevel":result["Vol"] = (params,f)=>f?this.call(e,params,f):this.call(e,params);break;
case "Status":result[e] = (params,f)=>f?this.call(e,params,f):this.call(e,params);result["trackPosicion"] = (params,f)=>f?this.call(e,params.hasOwnProperty("position")?{status_id: 31,value: params.position}:{},f):this.call(e,params.hasOwnProperty("position")?{status_id: 31,value: params.position}:{});break;
case "RemoveTrackFromPlayQueue":result["RemoveTrack"] = (params,f)=>f?this.call(e,params,f):this.call(e,params);break;
case "GetFormattedEntryTitle":result["GetFormatSong"] = (params,f)=>f?this.call(e,params,f):this.call(e,params);break;
case "GetPlaylistEntryInfo":result["getSongInfo"] = (params,f)=>f?this.call(e,params,f):this.call(e,params);break;
case "SubscribeOnAIMPStateUpdateEvent":result["getEvent"] = (params,f)=>f?this.call(e,params,f):this.call(e,params);break;
case "PluginCapabilities":result[e] = (params,f)=>f?this.call(e,params,f):this.call(e,params);break;
default:result[e] = (params,f)=>f?this.call(e,params,f):this.call(e,params);
}})
this.method = result;
return result
}else{throw "require object list"}
}else{throw "require object"}
}
on = (event,eventAction)=>{
event = event==="control"?"control_panel_state_change":
(event=="playback"?"play_state_change":
(event=="playlist"?"playlists_content_change":
(event=="track"?'current_track_change':false)));
let {method} = this;
if(method){
let { getEvent } = method;
let {OK,complete} = eventAction;
return new Promise((res,rej)=>{
if(event){
if(getEvent){
let loop=async(event,eventAction)=>await getEvent({event})
.then(a=>complete?
({complete,f:(OK?OK:eventAction?eventAction:(a)=>console.log(a,"default response"))(a)}):
({f:(OK?OK:eventAction?eventAction:(a)=>console.log(a,"default response"))(a)}))
.then(({complete,f})=>complete?complete(f):f)
.catch(e=>(eventAction.error?eventAction.error:rej)(e)).then(_=>loop(event,eventAction))
res(loop(event,eventAction));
}else{rej({error:"not exist getEvent"});}
}
})
}else{
throw "require methods"
}
}
}
module.exports = aimp;