Class: Growatt::Client

Inherits:
API
  • Object
show all
Defined in:
lib/growatt/client.rb

Overview

Wrapper for the Growatt REST API

See Also:

  • API documentation, reverse engineered

Instance Method Summary collapse

Methods inherited from API

#config

Methods included from Authentication

#login

Methods included from Connection

#connection

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



11
12
13
# File 'lib/growatt/client.rb', line 11

def initialize(options = {})
  super(options)
end

Instance Method Details

#device_list(plant_id) ⇒ Object



38
39
40
# File 'lib/growatt/client.rb', line 38

def device_list(plant_id)
  plant_info(plant_id).deviceList
end

#export_limit(serial_number, enable, value = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/growatt/client.rb', line 82

def export_limit(serial_number,enable,value=nil)
  if ExportLimit::DISABLE.eql? enable
    params = [0]
  else
    raise ArgumentError, "exportlimitation enable should be ExportLimit::WATT or ExportLimit::PERCENTAGE" unless [ExportLimit::WATT,ExportLimit::PERCENTAGE].include? enable
    raise ArgumentError, "Value should be set for export limitation" unless value
    params = [1, value, enable]
  end
  update_inverter_setting(serial_number,'maxSetApi','backflow_setting',params)
end

#inverter_control_data(inverter_id) ⇒ Object

get data for invertor control



48
49
50
51
52
53
# File 'lib/growatt/client.rb', line 48

def inverter_control_data(inverter_id)
  _inverter_api({
    'op': 'getMaxSetData',
    'serialNum': inverter_id
  }).obj.maxSetBean
end

#inverter_data(inverter_id, type = Timespan::DAY, date = Time.now) ⇒ Object

functions below are copied from python example code but not sure if these work with MOD9000 inverters



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/growatt/client.rb', line 107

def inverter_data(inverter_id,type=Timespan::DAY,date=Time.now)
  if Timespan::DAY.eql? type
    operator = 'getInverterData_max'
  elsif Timespan::MONTH.eql? type
    operator = 'getMaxMonthPac'
  elsif Timespan::YEAR.eql? type
    operator = 'getMaxYearPac'
  end
  _inverter_api({
    'op': operator,
    'id': inverter_id,
    'type': 1,
    'date': timespan_date(type,date)
  })
end

#inverter_list(plant_id) ⇒ Object



42
43
44
45
# File 'lib/growatt/client.rb', line 42

def inverter_list(plant_id)
  devices = device_list(plant_id)
  devices.select { |device| 'inverter'.eql? device.deviceType  }
end

#inverter_on?(serial_number) ⇒ Boolean

check if invertor is turned on

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/growatt/client.rb', line 77

def inverter_on?(serial_number)
  status = inverter_control_data(serial_number)
  Inverter::ON.eql? status.max_cmd_on_off
end

#login_infoObject

access data returned from login



16
17
18
# File 'lib/growatt/client.rb', line 16

def 
  @login_data
end

#plant_detail(plant_id, type = Timespan::DAY, date = Time.now) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/growatt/client.rb', line 23

def plant_detail(plant_id,type=Timespan::DAY,date=Time.now)
  _plant_detail( {
    'plantId': plant_id,
    'type': type,
    'date': timespan_date(type,date)
  })
end

#plant_info(plant_id) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/growatt/client.rb', line 30

def plant_info(plant_id)
  _plant_info({
    'op': 'getAllDeviceList',
    'plantId': plant_id,
    'pageNum': 1,
    'pageSize': 1
  })
end

#plant_list(user_id = nil) ⇒ Object



19
20
21
22
# File 'lib/growatt/client.rb', line 19

def plant_list(user_id=nil)
  user_id = ['user']['id'] unless user_id
  _plant_list({'userId':user_id})
end

#timespan_date(timespan = Timespan::DAY, date = Time.now) ⇒ Object

utility function to get date accordign timespan month/day



94
95
96
97
98
99
100
101
102
# File 'lib/growatt/client.rb', line 94

def timespan_date(timespan=Timespan::DAY,date=Time.now)
  if Timespan::YEAR.eql? timespan
    date.strftime("%Y")
  elsif Timespan::MONTH.eql? timespan
    date.strftime("%Y-%m")
  elsif Timespan::DAY.eql? timespan
    date.strftime("%Y-%m-%d")
  end
end

#turn_inverter(serial_number, on = true) ⇒ Object

turn invertor on of off



71
72
73
74
# File 'lib/growatt/client.rb', line 71

def turn_inverter(serial_number,on=true)
  onoff = (on ? Inverter::ON : Inverter::OFF )
  update_inverter_setting(serial_number,'maxSetApi','max_cmd_on_off',[onoff])
end

#update_ac_inverter_setting(serial_number, setting_type, parameters) ⇒ Object



139
140
141
# File 'lib/growatt/client.rb', line 139

def update_ac_inverter_setting(serial_number, setting_type, parameters)
  update_inverter_setting(serial_number,'spaSetApi',setting_type,parameters)
end

#update_inverter_setting(serial_number, command, param_id, parameters) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/growatt/client.rb', line 55

def update_inverter_setting(serial_number,command,param_id,parameters)
  command_parameters = {
    'op': command,
    'serialNum': serial_number,
    'paramId': param_id
  }
  # repeated values to hash { param1: value1 }

  parameters = parameters.map.with_index { |value, index| ["param#{index + 1}", value] }.to_h if parameters.is_a? Array
  self.format = 'x-www-form-urlencoded'
  data = JSON.parse(post('newTcpsetAPI.do',command_parameters.merge(parameters)).body)
  self.format = :json
  data['success']
end

#update_mix_inverter_setting(serial_number, setting_type, parameters) ⇒ Object

def inverter_detail(inverter_id)

  _inverter_api({
    'op': 'getInverterDetailData',
    'inverterId': inverter_id
  })
end
def inverter_detail_two(inverter_id)
  _inverter_api({
    'op': 'getInverterDetailData_two',
    'inverterId': inverter_id
  })
end


136
137
138
# File 'lib/growatt/client.rb', line 136

def update_mix_inverter_setting(serial_number, setting_type, parameters)
  update_inverter_setting(serial_number,'mixSetApiNew',setting_type,parameters)
end