sim_card 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,50 @@
1
+ class SimCard
2
+ class ReceivedSmsMessage
3
+
4
+ # parse raw output from SIM card and return list of ReceivedSmsMessage instances.
5
+ # see SimCardTest for examples of raw SIM output.
6
+ def self.to_messages raw_sim_output
7
+ messages = []
8
+ raw_input2 = raw_sim_output[14..-1] # remove initial AT+CMGL="ALL"\n
9
+ raw_input3 = (raw_input2 || '').split('+CMGL: ')[1..-1]
10
+
11
+ (raw_input3 || []).each do |raw_message|
12
+ header, *text_lines = raw_message.split("\n")
13
+
14
+ # +CMGL: 2,"REC READ","+421918987987","","13/08/20,19:00:44+08"
15
+ message_id, _, sender_number, _, date, time = header.gsub("\"", '').split(',')
16
+
17
+ timestamp = DateTime.strptime (date + ' ' + time), '%y/%m/%d %H:%M:%S'
18
+ message_text = text_lines.join "\n"
19
+
20
+ sms_message = ReceivedSmsMessage.new message_id, sender_number, timestamp, message_text
21
+ messages << sms_message
22
+ end
23
+
24
+ return messages.reverse
25
+ end
26
+
27
+ attr_reader :message_id, :sender_number, :timestamp, :text
28
+
29
+ # * message_id : ID of the sms message as provided by the SIM card
30
+ # * sender_number : who sent the message
31
+ # * timestamp : time of message arrival
32
+ # * text : message text
33
+ def initialize message_id, sender_number, timestamp, text
34
+ @message_id = message_id
35
+ @sender_number = sender_number
36
+ @timestamp = timestamp
37
+ @text = text
38
+ end
39
+
40
+ def to_s
41
+ <<STRING
42
+ message id: #{@message_id}
43
+ sender: #{@sender_number}
44
+ timestamp: #{@timestamp}
45
+ #{@text}
46
+ STRING
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,68 @@
1
+ class SimCard
2
+
3
+ # code below is based on:
4
+ # http://www.dzone.com/snippets/send-and-receive-sms-text
5
+
6
+ class Sim
7
+
8
+ # connect to SIM card. user_options may contain:
9
+ # * port : device where the gsm modem is connected, default is '/dev/ttyUSB0'
10
+ # * speed : connection speed in bauds, default is 9600
11
+ # * pin : pin to your sim card, default is '0000'
12
+ # * sms_center_no : SMS center of you provider. required only if you want to send SMS messages
13
+ def initialize(user_options = {})
14
+ default_options = {:port => '/dev/ttyUSB0', :speed => 9600, :pin => '0000'}
15
+ options = default_options.merge user_options
16
+
17
+ @port = SerialPort.new(options[:port], options[:speed])
18
+ cmd("AT")
19
+ cmd("AT+CPIN=\"#{options[:pin]}\"")
20
+ # Set to text mode
21
+ cmd("AT+CMGF=1")
22
+ # Set SMSC number
23
+ cmd("AT+CSCA=\"#{options[:sms_center_no]}\"") if options[:sms_center_no]
24
+ end
25
+
26
+ # correctly disconnect from SIM card
27
+ def close
28
+ @port.close
29
+ end
30
+
31
+ def send_sms number, message_text
32
+ cmd("AT+CMGS=\"#{number}\"")
33
+ cmd("#{message_text[0..140]}#{26.chr}\r\r")
34
+ sleep 3
35
+ wait
36
+ cmd("AT")
37
+ end
38
+
39
+ # list SMS messages in SIM memory
40
+ def sms_messages
41
+ raw_sim_output = cmd("AT+CMGL=\"ALL\"")
42
+ ReceivedSmsMessage.to_messages raw_sim_output
43
+ end
44
+
45
+ # remove SMS message from SIM card memory
46
+ # * sms_message: instance of SimCard::SmsMessage to be deleted
47
+ def delete_sms_message sms_message
48
+ cmd("AT+CMGD=#{sms_message.message_id}")
49
+ end
50
+
51
+ private
52
+ def cmd(cmd)
53
+ # puts "SIM CMD IN:#{cmd}"
54
+ @port.write(cmd + "\r")
55
+ wait
56
+ end
57
+
58
+ def wait
59
+ buffer = ''
60
+ while IO.select([@port], [], [], 0.25)
61
+ chr = @port.getc.chr;
62
+ buffer += chr
63
+ end
64
+ # puts "SIM OUT:#{buffer}"
65
+ buffer
66
+ end
67
+ end
68
+ end
data/lib/sim_card.rb ADDED
@@ -0,0 +1,7 @@
1
+ # see README
2
+ class SimCard
3
+ end
4
+
5
+ require 'date'
6
+ require 'sim_card/received_sms_message'
7
+ require 'sim_card/sim'
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sim_card
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Peter Vojtek
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-21 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/sim_card.rb
21
+ - lib/sim_card/sim.rb
22
+ - lib/sim_card/received_sms_message.rb
23
+ homepage:
24
+ licenses:
25
+ - MIT
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.11
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Access and control cellphone SIM card functionality via AT commands
48
+ test_files: []