Class: Buttplug::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/buttplugrb.rb

Overview

Our Client for a buttplug.io server

Instance Method Summary collapse

Constructor Details

#initialize(serverLocation, clientName = "buttplugrb") ⇒ Client

Creates a new client for buttplug.io

Arguments:

  • serverLocation (string) - Where our buttplug.io server is hosted. this will tend to be: "wss://localhost:12345/buttplug"

Returns:

  • A shiney new buttplug client ready for some action



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/buttplugrb.rb', line 34

def initialize(serverLocation, clientName="buttplugrb")
  @messageID=1
  @location=serverLocation
  #Ok Explanation time!
  # * @EventQueue - The events we are triggering on the server, Expected to be an array, with the first element being the message Id, and the second being the message itself!
  @eventQueue=EM::Queue.new
  @logLevel=Buttplug::LogLevel::Off
  @scanning=false
  @currentDevices=[];
  startEventMachine()
  @eventMachine.run
end

Instance Method Details

#currentDevicesObject



122
123
124
# File 'lib/buttplugrb.rb', line 122

def currentDevices()
  return @currentDevices
end

#deviceSusbscribe(id, &code) ⇒ Object



125
126
127
# File 'lib/buttplugrb.rb', line 125

def deviceSusbscribe(id,&code)
  #TODO: Add Code here to allow a class like Buttplug::Device to subscribe to events, annnnd realize that the device has disconnected when that does happen (like the hush has a tendeancy to do ... )
end

#generateIDObject

Does exactly what it says on the tin, generates a random id for our messages

Returns:

  • a number between 2 and 4294967295



118
119
120
121
# File 'lib/buttplugrb.rb', line 118

def generateID()
  @messageID+=1
  return @messageID
end

#isScanning?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/buttplugrb.rb', line 66

def isScanning?()
  return @scanning
end

#listDevicesObject

Lists all devices available to the server

Returns:

  • An array of available devices from the server

Example:

client.listDevices()
   [{"DeviceName"=>"XBox Compatible Gamepad (XInput)", "DeviceIndex"=>1, "DeviceMessages"=>{"SingleMotorVibrateCmd"=>{}, "VibrateCmd"=>{"FeatureCount"=>2}, "StopDeviceCmd"=>{}}}]


79
80
81
82
83
84
85
86
87
# File 'lib/buttplugrb.rb', line 79

def listDevices()
  id=generateID()
  deviceRequest=[id,"[{\"RequestDeviceList\": {\"Id\":#{id}}}]"]
  @eventQueue.push(deviceRequest)
  while(deviceRequest.length<3) do
    sleep 0.01#Just so we arn't occupying all the time on the system while we are waiting for our device list to come back.
  end
  return deviceRequest[2]["DeviceList"]["Devices"]
end

#sendMessage(message) ⇒ Object

Sends a message to our buttplug server

Arguments:

  • message (JSON formatted string) - The message we are sending to our server

Returns:

  • the Response from our server



105
106
107
108
109
110
111
# File 'lib/buttplugrb.rb', line 105

def sendMessage(message)
  @eventQueue.push(message)
  while(message.length<3) do
    sleep 0.01
  end
  return message[3]
end

#setLogLevel(logLevel) ⇒ Object



46
47
48
# File 'lib/buttplugrb.rb', line 46

def setLogLevel(logLevel)
  @logLevel=logLevel
end

#startScanningObject

Tells our server to start scanning for new devices



52
53
54
55
56
57
58
# File 'lib/buttplugrb.rb', line 52

def startScanning()
  id=generateID()
  response=sendMessage([id,"[{\"StartScanning\":{\"Id\":#{id}}}]"])
  if(response[0].keys.include? "Error")
    #TODO: Add Error Handling code
  end
end

#stopAllDevicesObject

Stops all devices currently controlled by the server



91
92
93
94
95
# File 'lib/buttplugrb.rb', line 91

def stopAllDevices()
  id=generateID()
  deviceRequest=[id,"[{\"StopAllDevices\": {\"ID\":#{id}}}]"]
  @eventQueue.push(deviceRequest)
end

#stopScanningObject

Tells our server to stop scanning for new devices



62
63
64
65
# File 'lib/buttplugrb.rb', line 62

def stopScanning()
  id=generateID()
  @eventQueue.push([id,"[{\"StopScanning\":{\"Id\":#{id}}}]"])
end