forked from yitsushi/midea-air-condition
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command structure + packet builder + status command
- Loading branch information
Balazs Nadasdi
committed
Apr 22, 2018
1 parent
2df19c3
commit f5f991a
Showing
9 changed files
with
149 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module MideaAirCondition | ||
module Command | ||
# Base Command class | ||
class BaseCommand | ||
# Default device type: 0xAC | ||
def initialize(device_type: 0xAC) | ||
@data = [0xaa, 0x23, device_type, 0x00, 0x00, 0x00, 0x00, 0x00] | ||
|
||
@data += [ | ||
0x03, 0x02, 0xff, 0x81, 0x00, 0xff, 0x03, 0xff, | ||
0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00 | ||
] | ||
|
||
fill | ||
end | ||
|
||
def finalize(security) | ||
# Add command sequence number | ||
# Can't be lower than 3 | ||
@data << 0x03 | ||
@data << security.crc8(@data[0x10..(@data.length - 1)]) | ||
@data[0x01] = @data.length | ||
|
||
@data | ||
end | ||
|
||
private | ||
|
||
def fill; end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module MideaAirCondition | ||
module Command | ||
# Request status of a device | ||
class RequestStatus < BaseCommand | ||
def fill | ||
@data[0x0a] = 0x40 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module MideaAirCondition | ||
module Command | ||
# Request status of a device | ||
class Set < BaseCommand | ||
def turn_on | ||
@data[0x3] |= 0x1 | ||
end | ||
|
||
def turn_off | ||
@data[0x3] &= 0x1 | ||
end | ||
|
||
def temperature=(celsius, mode: 2) | ||
c = ((mode << 5) & 0xe0) | (celsius & 0xf) | ((celsius << 4) & 0x10) | ||
@data[0x04] = c | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
require_relative 'client' | ||
require_relative 'device' | ||
require_relative 'packet_builder' | ||
require_relative 'security' | ||
require_relative 'version' | ||
|
||
# MideaAirCondition namespace | ||
module MideaAirCondition | ||
# Module to deparate command classes | ||
module Command | ||
end | ||
end | ||
|
||
# path = File.join(File.dirname(__FILE__), 'commands', '*.rb') | ||
# Dir.glob(path).each do |file| | ||
# require file | ||
# end | ||
path = File.join(File.dirname(__FILE__), 'commands', 'command.rb') | ||
require path | ||
path = File.join(File.dirname(__FILE__), 'commands', '*.rb') | ||
Dir.glob(path).each do |file| | ||
require file | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module MideaAirCondition | ||
# This is where we build our packets | ||
class PacketBuilder | ||
def initialize(security) | ||
@security = security | ||
@command = [] | ||
|
||
populate_header_data | ||
|
||
# Maybe this one is the client id | ||
# In a response it's the device id | ||
# and the first six bytes are the same | ||
@packet += [0xc6, 0x79, 0x00, 0x00, 0x00, 0x05, 0x0a, 0x00] | ||
|
||
add_unknown_section | ||
end | ||
|
||
def add_command(command) | ||
raise Exception, 'Invalid argument' if command.is_a?(Command) | ||
@command += command.finalize(@security) | ||
end | ||
|
||
def finalize | ||
@packet += @command | ||
@packet << @security.checksum(@command[1..(@command.length - 1)]) | ||
@packet << 0x00 | ||
|
||
# Add padding + update packet length | ||
@packet += [0] * (47 - @command.length) | ||
@packet[0x04] = @packet.length | ||
|
||
p @packet | ||
|
||
@packet | ||
end | ||
|
||
def populate_header_data | ||
# was always fix for me except the length byte | ||
@packet = [0x5a, 0x5a, 0x01, 0x11, 0x5c, 0x00, 0x20, 0x00] | ||
# was different for status and power | ||
# @packet += [0x12, 0x00, 0x00, 0x00, 0x6f, 0x33, 0x0c, 0x00] | ||
@packet += [0x01, 0x00, 0x00, 0x00, 0x8d, 0x0f, 0x17, 0x02] | ||
# was always fix for me | ||
@packet += [0x0e, 0x03, 0x12, 0x14] | ||
end | ||
|
||
def add_unknown_section | ||
@packet += [ | ||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
0x02, 0x00, 0x00, 0x00 | ||
] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters