Class: Connection

Inherits:
Object
  • Object
show all
Includes:
Sms
Defined in:
lib/smsruby/connection.rb

Overview

The Connection class represent the connection layer. Contains all values that describe an available connection that is associated with an attach phone. Implements methods for comunicating with the phone for send/receive messages and for obtain information about associated phone

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, port) ⇒ Connection

Initialize the connection reprensented by name through businit function provided by the sms shared object. In the same way initialize all class attributes.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/smsruby/connection.rb', line 36

def initialize(name,port)
  begin
    if RUBY_PLATFORM =~ /(win|w)32$/
     path = ENV['userprofile']+'/_gnokiirc'
    elsif RUBY_PLATFORM =~ /linux/
     path = ENV['HOME']+'/.gnokiirc'
    end
    error=businit(name, path)
    error <= 0  ? (error==-500 ? (raise "The maximum number of connection has been open") : @id_connection=error.to_i*-1) : exception(error)
    @port = port
    @phone_model=phoneModel(@id_connection)
    @phone_manufacter=phoneManufacter(@id_connection)
    @phone_revsoft=phoneRevSoft(@id_connection)
    @phone_imei=(phoneImei(@id_connection)).gsub(/[\s\D]/, "")
    @typec='none'
    @status='available'
  end
end

Instance Attribute Details

#id_connectionObject (readonly)

Represent the unique id of the current connection



21
22
23
# File 'lib/smsruby/connection.rb', line 21

def id_connection
  @id_connection
end

#phone_imeiObject (readonly)

Reference imei of the phone attached to the current connection



29
30
31
# File 'lib/smsruby/connection.rb', line 29

def phone_imei
  @phone_imei
end

#phone_manufacterObject (readonly)

Reference manufacter of the phone attached to the current connection



25
26
27
# File 'lib/smsruby/connection.rb', line 25

def phone_manufacter
  @phone_manufacter
end

#phone_modelObject (readonly)

Reference model of the phone attached to the current connection



23
24
25
# File 'lib/smsruby/connection.rb', line 23

def phone_model
  @phone_model
end

#phone_revsoftObject (readonly)

Reference the installed software revition in the phone attached to the current connection



27
28
29
# File 'lib/smsruby/connection.rb', line 27

def phone_revsoft
  @phone_revsoft
end

#portObject (readonly)

Reference the associated port for the current connection



19
20
21
# File 'lib/smsruby/connection.rb', line 19

def port
  @port
end

#statusObject

Reference the status of the current connection



15
16
17
# File 'lib/smsruby/connection.rb', line 15

def status
  @status
end

#typecObject

Reference if the current connection it’s allow to send, receive or both



17
18
19
# File 'lib/smsruby/connection.rb', line 17

def typec
  @typec
end

Instance Method Details

#batterylevelObject

Return the battery level of the phone asociatted with the current connection



73
74
75
# File 'lib/smsruby/connection.rb', line 73

def batterylevel
  return bat_level(@id_connection)
end

#closeObject

Terminate the current connection



80
81
82
# File 'lib/smsruby/connection.rb', line 80

def close
  busterminate(@id_connection)
end

#exception(error) ⇒ Object

Throws a diferent exception depending of specified error code



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/smsruby/connection.rb', line 129

def exception (error)
  case error
    when 1..9 then raise ErrorHandler::GeneralError.new(error)
    when 10..15 then raise ErrorHandler::StatemachineError.new(error)
    when 16..18 then raise ErrorHandler::LocationError.new(error)
    when 19..21 then raise ErrorHandler::FormatError.new(error)
    when 22..25 then raise ErrorHandler::CallError.new(error)
    when 26..29 then raise ErrorHandler::OtherError.new(error)
    when 30..35 then raise ErrorHandler::ConfigError.new(error)
  end
end

#execute(hsh) ⇒ Object

Execute a specific function from the shared object depending of type option value specified in hsh. If an error occurs an exception will be thrown



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/smsruby/connection.rb', line 88

def execute(hsh)
  cmd = hsh[:type]

  case cmd
  when /send/
    @status = 'sending'
    error = 0
    puts ":: Manufacter sending the message: "+@phone_manufacter.to_s+' in port: '+@port.to_s+' with id: '+@id_connection.to_s+"\n"
    error= send_sms(hsh[:dst], hsh[:msj], hsh[:smsc], hsh[:report], hsh[:validity], @id_connection)
    #puts ':: The return error in execute send function is: '+error.to_s+"\n"
    sleep(7)
  when /receive/
    @status = 'receiving'
    msj=[]
    nmsj=0
    error = get_sms(@id_connection)
    error <= 0 ? number=error.to_i*-1 : exception(error)
    number.times { |i|
        m = get_msj(i,@id_connection)
        if(m.type_sms.eql?("Inbox Message"))
          msj[nmsj] = m  if (m.error == 0)
          nmsj+=1 if m.error ==0
        end
      }
    return msj
  end
  exception(error) unless error==0
end

#signallevelObject

Return the signal level of the phone asociatted with the current connection



66
67
68
# File 'lib/smsruby/connection.rb', line 66

def signallevel
  return rf_level(@id_connection)
end

#testObject

Allow to test and existing connection to verify if is still available. Return 0 if is still available, diferent from 0 otherwise.



59
60
61
# File 'lib/smsruby/connection.rb', line 59

def test
  return testconn(@id_connection)
end

#type(status) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/smsruby/connection.rb', line 117

def type(status)
  case status
    when 0 then return "unread"
    when 1 then return "read"
    when 2 then return "sent"
    when 3 then return "unsent"
  end
end