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



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

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



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

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



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

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
	begin
		@gsm.send_sms!(msg.recipient.phone_number, msg.text)
		
	rescue => err
		log_exception err, "Message sending FAILED"
	end
end

#startObject



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
# File 'lib/rubysms/backend/gsm.rb', line 55

def start
	begin
		@gsm = ::Gsm::Modem.new(@port)
		@gsm.use_pin(@pin) unless @pin.nil?
		@gsm.receive method(:incoming)
		str = @gsm.wait_for_network
		
		#bands = @gsm.bands_available.join(", ")
		#log "Modem supports: #{bands}"
		
	# couldn't open the port. this usually means
	# that the modem isn't plugged in to it...
	rescue Errno::ENOENT, ArgumentError => err
		log_exception err,\
			"Couldn't connect to " +\
			"modem on port: #{@port}"
		
	# something else went wrong
	# while initializing the modem
	rescue ::Gsm::Modem::Error => err
		log_exception err,\
			"Couldn't initialize the " +\
			"modem on port: #{@port}"
	end
	
	# nothing went wrong this time
	# so dump some useful info
	log [
		"Started #{label} Backend",
		"  Signal strength: #{str}",
		"  Port: #{@gsm.port}"
	], :init
end