This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
/
siriproxy-ipcam.rb
executable file
·63 lines (52 loc) · 1.7 KB
/
siriproxy-ipcam.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'socket'
require 'open-uri'
require 'siri_objects'
class SiriProxy::Plugin::IPCam < SiriProxy::Plugin
attr_accessor :camurls
def initialize(config = {})
@camUrl = Hash.new
@camUrl = config["camurls"]
@webIp = "http://" + UDPSocket.open {|s| s.connect("255.255.255.0", 1); s.addr.last}
end
########## Commands
listen_for /(show me|snapshot for) the (.*) camera/i do |keyword, camera|
check_camera camera.downcase.strip
request_completed
end
listen_for /camera(?: of)?(?: for)?(?: the)? (.*)/i do |camera|
check_camera camera.downcase.strip
request_completed
end
listen_for(/check cameras/i) do
@camUrl.each_key {|camera| check_camera(camera)}
request_completed
end
########## Actions
def check_camera(camera)
if @camUrl.has_key?(camera)
if @camUrl[camera].has_key?("id")
open(@camUrl[camera]["url"], :http_basic_authentication => [@camUrl[camera]["id"], @camUrl[camera]["pw"]]) do |f|
File.open("/var/www/" + camera.gsub(/\s+/, "") + ".jpg", "wb") do |file|
file.puts f.read
end
end
push_image(camera.capitalize, @webIp + "/" + camera.gsub(/\s+/, "") + ".jpg")
else
push_image(camera.capitalize, @camUrl[camera]["url"])
end
else
say "Sorry, I could not find a camera named #{camera}."
say "Here is the list of cameras."
@camUrl.each_key {|camera| say camera.capitalize}
camera = ask "Which camera would you like to view?"
check_camera(camera.downcase.strip)
end
end
def push_image(title, image)
object = SiriAddViews.new
object.make_root(last_ref_id)
answer = SiriAnswer.new(title, [SiriAnswerLine.new('logo', image)])
object.views << SiriAnswerSnippet.new([answer])
send_object object
end
end