Skip to content

Instantly share code, notes, and snippets.

@jiangzhuo
Created December 5, 2015 15:40
Show Gist options
  • Save jiangzhuo/3ec18b9d066d36d5dff7 to your computer and use it in GitHub Desktop.
Save jiangzhuo/3ec18b9d066d36d5dff7 to your computer and use it in GitHub Desktop.
myhomevod through qq
//var list=
var Browser = require('nodecast-js');
var DeviceWatcher = require('upnp-device-client');
var util = require('util');
var browser = new Browser();
var deviceList = [];
var currentDevice = '';
var playList = [];
var currentSong;
var gid = 2178625976;
log = new (require('log'))('debug');
exports.init = function (robot) {
browser.onDevice(function (device) {
device.onError(function (err) {
log.error(err);
});
if (currentDevice === '') {
currentDevice = device.host;
}
device.deviceWatcher = new DeviceWatcher(device.xml);
device.deviceWatcherInterval = setTimeout(watcherFunc.bind(device, robot), 1000);
log.debug('new device discovered: ' + device.name + ' (' + device.type + ')');
deviceList[device.host] = device;
});
browser.start();
};
var watcherFunc = function (robot) {
clearTimeout(this.deviceWatcherInterval);
var self = this;
if (this.host != currentDevice)
return;
this.deviceWatcher.callAction('AVTransport', 'GetTransportInfo', {InstanceID: 0}, function (err, result) {
var state = result.CurrentTransportState;
log.debug(state);
if (state == 'STOPPED' || state == 'NO_MEDIA_PRESENT') {
currentSong = playList.pop();
if (currentSong != null) {
var device = deviceList[currentDevice];
if (device) {
var url = currentSong.url;
var fromUser = currentSong.fromUser;
device.play(url, 0);
var group = robot.get_group({
gid: gid
});
robot.send_message_to_group(group, util.format("开始播放%s点播的%s", fromUser, url), function (ret, e) {
});
self.deviceWatcher.callAction('AVTransport', 'GetPositionInfo', {InstanceID: 0}, function (err, result) {
if (result != null) {
var TrackDuration = result.TrackDuration;
var RelTime = result.RelTime;
log.debug('RelTime', RelTime, 'TrackDuration', TrackDuration);
var realTimeSplit = RelTime.split(':');
var trackDurationSplit = TrackDuration.split(':');
var timeout = ((parseInt(trackDurationSplit[0]) * 60 * 60 + parseInt(trackDurationSplit[1]) * 60 + parseInt(trackDurationSplit[2])) -
(parseInt(realTimeSplit[0]) * 60 * 60 + parseInt(realTimeSplit[1]) * 60 + parseInt(realTimeSplit[2]))) * 1000;
//log.debug(timeout);
self.deviceWatcherInterval = setTimeout(watcherFunc.bind(self, robot), timeout + 2000);
}
});
//return send(util.format("开始播放%s点播的%s", fromUser, url));
} else {
//return send("当前播放设备不可用,选择其他设备");
}
} else {
return;
}
} else if (state == 'PAUSED_PLAYBACK') {
// do nothing
} else if (state == 'PLAYING') {
// do nothing
self.deviceWatcher.callAction('AVTransport', 'GetPositionInfo', {InstanceID: 0}, function (err, result) {
if (result != null) {
var TrackDuration = result.TrackDuration;
var RelTime = result.RelTime;
log.debug('RelTime', RelTime, 'TrackDuration', TrackDuration);
if (RelTime == '0:00:00') {
self.deviceWatcherInterval = setTimeout(watcherFunc.bind(self, robot), 2000);
} else {
var realTimeSplit = RelTime.split(':');
var trackDurationSplit = TrackDuration.split(':');
var timeout = ((parseInt(trackDurationSplit[0]) * 60 * 60 + parseInt(trackDurationSplit[1]) * 60 + parseInt(trackDurationSplit[2])) -
(parseInt(realTimeSplit[0]) * 60 * 60 + parseInt(realTimeSplit[1]) * 60 + parseInt(realTimeSplit[2]))) * 1000;
//log.debug(timeout);
self.deviceWatcherInterval = setTimeout(watcherFunc.bind(self, robot), timeout + 2000);
}
}
});
}
});
};
exports.stop = function (robot) {
for (var k in deviceList) {
var device = deviceList[k];
clearTimeout(device.deviceWatcherInterval);
}
browser.destroy();
};
exports.received = function (content, send, robot, message) {
if (message.type != 'group_message' || message.from_gid != gid) {
// this is from 东云研究所点歌台
return;
}
var fromUser = message.from_user.nick;
if (content.match(/^信息$/i)) {
var device = deviceList[currentDevice];
var ret = "";
if (currentSong != null)
ret += util.format("正在播放%s点播的%s\n", currentSong.fromUser, currentSong.url);
ret += "播放列表:\n";
if (device) {
for (var k in playList) {
var song = playList[k];
ret += util.format("%s - %s\n", song.fromUser, song.url);
}
ret += util.format("当前播放设备是:%s - %s|%s", device.host, device.name, device.type)
return send(ret);
} else {
return send("当前没有播放设备")
}
}
if (content.match(/^设备$/i)) {
var ret = "设备列表:\n";
for (var k in deviceList) {
var device = deviceList[k];
ret += util.format("%s - %s|%s\n", device.host, device.name, device.type);
}
ret += '输入"选择设备 ip"进行选择';
return send(ret);
}
var ret = content.match(/^选择设备\s+(.*)/i);
if (ret) {
if (deviceList[ret[1]]) {
currentDevice = ret[1];
return send("选择了设备" + deviceList[currentDevice].name);
} else {
return send("所选设备不存在");
}
}
var ret = content.match(/^添加\s+(.*)/i);
if (ret) {
var url = ret[1];
playList.push({url: url, fromUser: fromUser});
send(util.format("添加%s点播的%s", fromUser, url));
var device = deviceList[currentDevice];
if (device) {
setTimeout(watcherFunc.bind(device, robot), 500);
} else {
return send("当前播放设备不可用,选择其他设备");
}
}
if (content.match(/^下一首$/i)) {
if (currentSong != null) {
send(util.format("%s立即结束了%s点播的当前曲目%s", fromUser, currentSong.fromUser, currentSong.url));
}
var device = deviceList[currentDevice];
if (device) {
if (device._player != null) {
device.stop();
} else {
device.deviceWatcher.callAction('AVTransport', 'Stop', {InstanceID: 0}, function () {
});
}
setTimeout(watcherFunc.bind(device, robot), 500);
} else {
return send("当前播放设备不可用,选择其他设备");
}
}
if (content.match(/^暂停$/i)) {
send(util.format("%s暂停了播放", fromUser));
var device = deviceList[currentDevice];
if (device != null) {
if (device._player != null) {
device._player.pause();
} else {
device.deviceWatcher.callAction('AVTransport', 'Pause', {InstanceID: 0}, function () {
});
}
} else {
return send("当前播放设备不可用,选择其他设备");
}
}
if (content.match(/^播放$/i)) {
send(util.format("%s继续了播放", fromUser));
var device = deviceList[currentDevice];
if (device != null) {
if (device._player != null) {
device._player.play();
} else {
device.deviceWatcher.callAction('AVTransport', 'Play', {InstanceID: 0}, function () {
});
}
} else {
return send("当前播放设备不可用,选择其他设备");
}
}
var ret = content.match(/^播放JZ\s+(.*)/i);
if (ret) {
var url = ret[1];
var device = deviceList[currentDevice];
if (device) {
device.play(url, 0);
return send(util.format("开始播放%s点播的%s", fromUser, url));
} else {
return send("当前播放设备不可用,选择其他设备");
}
}
var ret = content.match(/^下一首JZ\s+(.*)/i);
if (ret) {
var url = ret[1];
var device = deviceList[currentDevice];
if (device) {
if (device._player != null) {
device._player.load(url, {autoplay: false}, function (err) {
device._player.play();
});
} else {
device.play(url, 0);
}
//console.log(util.inspect(device._player,{depth:5}));
//device._player.subscribe('AVTransport', function (e) {
// //Will receive events like { InstanceID: 0, TransportState: 'PLAYING' } when playing media
// console.info('AVTransport', e);
//});
return send(util.format("开始播放下一首%s点播的%s", fromUser, url));
} else {
return send("当前播放设备不可用,选择其他设备");
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment