Class: SMS::Backend::GSM

Inherits:
Base show all
Defined in:
lib/rubysms/backend/gsm.rb

Overview

Provides an interface between RubyGSM and RubySMS, which allows RubySMS to send real SMS in an abstract fashion, which can be replicated by other backends. This backend is probably the thinnest layer between applications and the network, since the backend API (first implemented here) was based on RubyGSM.

Instance Attribute Summary

Attributes inherited from Thing

#label, #router

Instance Method Summary collapse

Methods inherited from Thing

#outgoing, #stop

Constructor Details

#initialize(port = :auto, pin = nil) ⇒ GSM

just store the arguments until the backend is ready to be started



49
50
51
52
# File 'lib/rubysms/backend/gsm.rb', line 49

def initialize(port=:auto, pin=nil)
	@port = port
	@pin = nil
end

Instance Method Details

#incoming(msg) ⇒ Object

called back by rubygsm when an incoming message arrives, which we will pass on to rubysms to dispatch to applications



105
106
107
108
109
110
111
112
113
114
# File 'lib/rubysms/backend/gsm.rb', line 105

def incoming(msg)
	
	# NOTE: the msg argument is a GSM::Incoming
	# object from RubyGSM, NOT the more useful
	# SMS::Incoming object from RubySMS
	
	router.incoming(
		SMS::Incoming.new(
			self, msg.sender, msg.sent, msg.text))
end

#send_sms(msg) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/rubysms/backend/gsm.rb', line 91

def send_sms(msg)
	super
	
	# send the message to the modem via rubygsm, and log
	# if it failed. TODO: needs moar info from rubygsm
	# on *why* sending failed
	unless @gsm.send_sms(msg.recipient.phone_number, msg.text)
		log "Message sending FAILED", :warn
	end
end

#startObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rubysms/backend/gsm.rb', line 54

def start
	
	# lock the threads during modem initialization,
	# simply to avoid the screen log being mixed up
	Thread.exclusive do
		begin
			@gsm = ::Gsm::Modem.new(@port)
			@gsm.use_pin(@pin) unless @pin.nil?
			@gsm.receive method(:incoming)
			
			#bands = @gsm.bands_available.join(", ")
			#log "Using GSM Band: #{@gsm.band}MHz"
			#log "Modem supports: #{bands}"
			
			log "Waiting for GSM network..."
			str = @gsm.wait_for_network
			log "Signal strength is: #{str}"
			
		# couldn't open the port. this usually means
		# that the modem isn't plugged in to it...
		rescue Errno::ENOENT, ArgumentError
			log "Couldn't open #{@port}", :err
			raise IOError
			
		# something else went wrong
		# while initializing the modem
		rescue ::Gsm::Modem::Error => err
			log ["Couldn't initialize the modem",
				   "RubyGSM Says: #{err.desc}"], :err
			raise RuntimeError
		end
		
		# rubygsm didn't blow up?!
		log "Started GSM Backend", :init
	end
end