meshblu-connector-skype
Version:
178 lines (143 loc) • 6.51 kB
text/coffeescript
async = require 'async'
{EventEmitter} = require 'events'
_ = require 'lodash'
debug = require('debug')('meshblu-connector-skype:index')
LyncEventEmitter = require './lync-event-emitter'
LyncLauncher = require './lync-launcher.coffee'
LyncDisableFeedback = require './lync-disable-feedback'
class Connector extends EventEmitter
constructor: ({}) ->
?= require './lync-manager'
= new LyncEventEmitter()
start: (device, callback) =>
.on 'config',
.on 'config', _.throttle (=> ), 500
LyncDisableFeedback.disable (error) =>
return callback error if error?
# .on 'config', (config) => console.log JSON.stringify config, null, 2
{ } = device
device, (error) =>
return callback error if error
callback
close: (callback) =>
return callback()
onConfig: ({desiredState, autoLaunchSkype}={}, callback) =>
callback()
.emitEvents .handle
return if _.isEmpty desiredState
= desiredState
{}
if autoLaunchSkype == true
LyncLauncher.autoCheck()
else
LyncLauncher.stopAutoCheck()
startMeeting: ({audioEnabled, videoEnabled}, callback) =>
finishStartMeetingHandler = (conversations) =>
currentState = _.first _.values conversations
conversationUrl = _.get currentState, 'properties.conferenceAccessInformation.ExternalUrl'
if conversationUrl
.off 'config', finishStartMeetingHandler
callback null, meeting: url: conversationUrl
.stopMeetings null, (error) =>
.on 'config', finishStartMeetingHandler
{audioEnabled, videoEnabled, meeting: {}}
truthAndReconcilliation: =>
currentState = _.first _.values .conversations
debug "truthAndReconcilliation", {currentState, }
return unless ?
currentState, (error) =>
delete .meeting
currentState
currentState
updateDesiredState: (desiredState) =>
'update', {desiredState}
_refreshCurrentState: (callback=->) =>
(error, state) =>
return callback error if error?
{state}, callback
_emitNoClient: ({state}, callback) =>
'error', new Error('Cannot find running Lync Process')
{state}, callback
_emitUpdate: (update, callback) =>
return callback() if _.isEqual update,
'update', update
= update
callback()
_computeState: (callback) =>
debug '_computeState'
currentState = _.first _.values .conversations
return callback null, {meeting: null} unless currentState?
conversationUrl = _.get currentState, 'properties.conferenceAccessInformation.ExternalUrl'
conversationUrl = null if _.isEmpty conversationUrl
self = currentState.participants?[currentState.self]
videoState = _.get currentState, 'video.state'
ourKindaState =
meeting:
url: conversationUrl
subject: _.get currentState, 'subject'
participants: _.get currentState, 'participants'
conversationId: _.get currentState, 'properties.id'
videoState: _.get currentState, 'video.state'
videoEnabled: videoState == 'Send' || videoState == 'SendReceive'
videoActions: _.get currentState, 'video.actions'
audioEnabled: !self?.isMuted
callback null, ourKindaState
_handleAudioEnabled: (currentState, callback=->) =>
debug '_handleAudioEnabled', {currentState, }
return callback() unless _.has , 'audioEnabled'
return callback() unless _.has currentState, 'self'
return callback() unless _.lowerCase(currentState?.state) == 'active'
self = _.get currentState, "participants.#{currentState.self}"
debug .audioEnabled, self.isMuted
if .audioEnabled
debug 'unmuting'
return .unmute null, (error) =>
debug 'unmuted', error
return callback error if error?
delete .audioEnabled
callback()
debug 'muting'
return .mute null, (error) =>
debug 'muted', error
return callback error if error?
delete .audioEnabled
callback()
_handleMeeting: (currentState, callback=->) =>
debug '_handleMeeting', {, currentState}
{meeting} =
delete .meeting
return callback() if meeting == undefined
return .stopMeetings null, callback if meeting == null
conversationUrl = _.get currentState, 'properties.conferenceAccessInformation.ExternalUrl'
return callback() if conversationUrl && meeting.url == conversationUrl
debug 'stopping meetings'
.stopMeetings null, (error) =>
return callback error if error?
return .createMeeting null, callback if _.isEmpty meeting.url
.joinMeeting meeting.url, callback
_handleVideoEnabled: (currentState, callback=->) =>
debug '_handleVideoEnabled',
return callback() unless _.has , 'videoEnabled'
return callback() unless _.lowerCase(currentState?.state) == 'active'
return .stopVideo null, callback unless .videoEnabled
return currentState, callback
_startVideo: (currentState, callback) =>
debug "trying to _startVideo"
videoState = _.get currentState, 'video.state'
if videoState == 'Send' || videoState == 'SendReceive'
debug "videoState was #{videoState}. We're done!"
delete .videoEnabled
return callback()
unless _.get(currentState, 'modality.state') == 'Connected'
debug 'not connected. waiting till next time'
return callback()
unless _.get(currentState, 'video.actions.Start') || _.get(currentState, 'video.actions.Resume')
debug "I can't resume or start the video. waiting until next time"
return callback()
debug "Starting video"
.startVideo null, (error) =>
return callback error if error?
delete .videoEnabled
callback()
module.exports = Connector