Skip to content

Instantly share code, notes, and snippets.

@vivahiraj
Created December 28, 2017 05:15
Show Gist options
  • Save vivahiraj/efbb9573d479f9ca7fb7fb81147a8e97 to your computer and use it in GitHub Desktop.
Save vivahiraj/efbb9573d479f9ca7fb7fb81147a8e97 to your computer and use it in GitHub Desktop.
ダイソーのリモートシャッターを押すとLINEへメッセージを送るサンプル。rootで実行する必要あり
require 'device_input'
require 'rest-client'
require 'json'
class LineBot
TOKEN = "Channel Access Token"
TO = "送信先のID"
def self.send(msg)
headers = {
"Content-Type" => "application/json; charser=UTF-8",
"Authorization" => "Bearer #{TOKEN}",
}
params = {
to: TO,
messages: [
{
type: "text",
text: msg,
}
]
}
RestClient.post "https://api.line.me/v2/bot/message/push", params.to_json, headers
end
end
class Bluebutton
attr_accessor :device
def initialize name
finded = Dir.glob("/sys/**/name").select do |file|
File.read(file).downcase[name.downcase]
end.first
raise "Can't find device info '#{name}' in /sys/**/*" if finded.nil?
@device = "/dev/" + Dir.glob("#{File.dirname(finded)}/**/uevent").map do |file|
File.read(file).split("\n").select{|s| s['DEVNAME']}.compact.first
end.compact.flatten.first.split("=")[1] rescue nil
@msg = {"VolumeUp" => "iOSボタンを押したよ",
"Enter" => "androidボタンを押したよ"}
@long_msg = {"VolumeUp" => "iOSボタンを長く押したよ",
"Enter" => "androidボタンを長く押したよ"}
puts "Device #{name} find at #{@device}"
end
def run
File.open(@device, 'rb' ) do |input|
puts "Connected"
DeviceInput.read_loop(input) do |event|
if event.type == 'EV_KEY'
if event.value > 0
key_down event
else
key_up event
end
end
end
end
end
def key_down event
if @pressed.nil?
@pressed = Time.now
@code = event.code
LineBot.send(@msg[@code])
puts "push #{@code}"
elsif @long.nil? && @pressed < (Time.now - 1)
LineBot.send(@long_msg[@code])
puts "long push #{@code}"
@long = true
end
end
def key_up event
@long = nil
@pressed = nil
@code = nil
end
end
device = "AB Shutter3"
puts "Try to find device #{device}..."
button = Bluebutton.new(device)
puts "Reading events from #{button.device}..."
button.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment