Class: FortumReader

Inherits:
Object
  • Object
show all
Defined in:
lib/fortum_reader.rb

Constant Summary collapse

START_URL =
'https://www.fortum.com/countries/fi/yksityisasiakkaat/omat-palvelut/kulutuksen-seuranta/lukemahistoria/pages/default.aspx'
AGREEMENTS_URL =
'https://www.fortum.com/countries/fi/yksityisasiakkaat/omat-palvelut/sopimukset/pages/default.aspx'

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ FortumReader

Returns a new instance of FortumReader.



10
11
12
13
14
15
16
# File 'lib/fortum_reader.rb', line 10

def initialize(username, password)
  @rds=[]
  @username=username
  @password=password
  raise "Username and password must exist!" unless @username && @password
  @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
end

Instance Method Details

#agentObject



107
108
109
110
# File 'lib/fortum_reader.rb', line 107

def agent
  new_agent unless @agent
  @agent
end

#basic_informationObject

Reads basic information for customer targets. Example address. Returns hash.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fortum_reader.rb', line 68

def basic_information
  vals={}

  new_agent
  welcome_page=(AGREEMENTS_URL)
  bodypage=welcome_page.iframe.click
  ps=bodypage.search("//p")
  ps.each do |node|
    if node.text.include?("Käyttöpaikka:")
      parts=node.text.split("\n")
      vals[:street_address]=parts[1].chomp.strip
      vals[:postno]=parts[2].chomp.strip
      vals[:city]=parts[3].chomp.strip.capitalize
    end
  end

  vals
end

#collect_readings(page, usage_point_id) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/fortum_reader.rb', line 143

def collect_readings(page, usage_point_id)
  readings=page.search("//tr[@name='NormalRow']") + page.search("//tr[@name='HighLightRow']")
  @logger.debug "Got #{readings.size} possible elements for readings..."
  readings.each do |reading|
    if reading.children.size >= 4
      comment=reading.children[1].children.text.chomp
      #@logger.debug "comment is #{comment}"
      reading_hash={read_at: reading.children[0].children.text.chomp,
                    reading: reading.children[3].children.text.chomp,
                    usage_point_id: usage_point_id,
                    comment: reading.children[1].children.text.chomp}
      @logger.info "Reading hash: #{reading_hash}"
      if comment.downcase.include?("siirto") || comment.downcase.include?("kauko")
        @rds<<reading_hash
      else
        @logger.error "Got reading, with unknown comment: #{comment} cannot process."
      end
    end
  end
end

#login(url = START_URL) ⇒ Object

Logins user. Return page after login.



113
114
115
116
117
118
119
120
121
# File 'lib/fortum_reader.rb', line 113

def (url= START_URL)
  page=agent.get(url)
  =page.form
  .username=@username
  .password=@password
  page=agent.submit()
  @logger.info "After login, result page url is #{page.uri}"
  page
end

#login_ok?Boolean

Check that login works with given params.

Returns:

  • (Boolean)


19
20
21
22
23
# File 'lib/fortum_reader.rb', line 19

def 
  new_agent
  welcome_page=
  welcome_page.uri.to_s.include?("login") ? false : true
end

#logoutObject



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

def logout
  @agent=nil
end

Nagigagtes from welcome page to select cup page.



95
96
97
98
99
# File 'lib/fortum_reader.rb', line 95

def navigate_to_select_cup_page(welcome_page)
  lukemahistoria_page=welcome_page.link_with(text: "Lukemahistoria").click
  select_cup_page=lukemahistoria_page.iframe.click
  select_cup_page
end


133
134
135
136
137
138
139
140
141
# File 'lib/fortum_reader.rb', line 133

def navigate_to_usages_page(select_cup_form, no, usage_point_id)
  @logger.info "Reading usage point #{usage_point_id} readings..."
  select_cup_form.radiobuttons[no-1].check
  #select_cup_form["cmdSelectCP"]="Jatka"
  usages_page=select_cup_form.click_button
  raise "error: submit select did not work." if usages_page.content.include?("Valitse k")
  raise "error: Did not get given usage_point_id #{usage_point_id} page." unless usages_page.content.include?(usage_point_id)
  usages_page
end

#new_agentObject



101
102
103
104
105
# File 'lib/fortum_reader.rb', line 101

def new_agent
  @agent=Mechanize.new
  @agent.user_agent_alias="Linux Firefox"
  @agent
end

#readObject

Read readings! Returns array of readings.



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
# File 'lib/fortum_reader.rb', line 26

def read
  new_agent
  @cups=[]
  welcome_page=
  if welcome_page.uri.to_s.include?("login")
    raise "Cannot read readings: login was not OK. Is there problem with username(#{@username}) and password?"
  end
  select_cup_form=navigate_to_select_cup_page(welcome_page).form
  # read available cups
  read_cups(select_cup_form)
  # Iterate each cup.
  @cups.each_with_index do |cup, index|
    @logger.info "Reading usage point #{index+1} #{cup[:usage_point_id]}..."
    usages_page=navigate_to_usages_page(select_cup_form, index+1, cup[:usage_point_id])
    # Find each page and load readings
    usages_page.links.each do |link|
      if link.href.include?("ReadingReport")
        reading_page=link.click
        @logger.info "Reading from page with title '#{link.text.chomp}'"
        collect_readings(reading_page, cup[:usage_point_id])
      end
    end

    if index < @cups.size-1
      logout
      welcome_page=
      select_cup_form=navigate_to_select_cup_page(welcome_page).form
    end
  end
  @logger.info "Ready! Got #{@rds.size} readings."
  @rds
end

#read_cups(select_cup_form) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/fortum_reader.rb', line 123

def read_cups(select_cup_form)
  @cups=[]
  @logger.info "Reading usage point information..."
  select_cup_form.page.search("//table//tr").each_with_index do |cup, index|
    infos=cup.children[1].text.gsub(/\r\n/m, "\n").split("\n")
    @cups<<{usage_point_id: infos[1].strip, street_address: infos[2].strip, postno: infos[3].strip, city: infos[4].strip}
  end
  @logger.info "Found #{@cups.size} usage points."
end

#readingsObject



63
64
65
# File 'lib/fortum_reader.rb', line 63

def readings
  @rds
end

#usage_point_informationObject

Reads usage point information. Returns list of usage point information hashes.



88
89
90
91
92
# File 'lib/fortum_reader.rb', line 88

def usage_point_information
  list=[]
  # TODO: Implement
  list
end