generated from wuhan005/go-template
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Connect to Mi Band and get heart rate (#3)
- Loading branch information
Showing
10 changed files
with
422 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,6 @@ jobs: | |
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
# Create frontend `dist` folder. | ||
# | ||
# - name: Create frontend dist folder | ||
# run: mkdir frontend/dist/ && touch frontend/dist/1 | ||
- name: Run golangci-lint | ||
uses: golangci/[email protected] | ||
with: | ||
|
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,69 @@ | ||
// Copyright 2021 E99p1ant. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package cryptoutil | ||
|
||
import ( | ||
"crypto/cipher" | ||
) | ||
|
||
type ecb struct { | ||
b cipher.Block | ||
blockSize int | ||
} | ||
|
||
func newECB(b cipher.Block) *ecb { | ||
return &ecb{ | ||
b: b, | ||
blockSize: b.BlockSize(), | ||
} | ||
} | ||
|
||
type ecbEncrypter ecb | ||
|
||
// NewECBEncrypter returns a BlockMode which encrypts in electronic code book | ||
// mode, using the given Block. | ||
func NewECBEncrypter(b cipher.Block) cipher.BlockMode { | ||
return (*ecbEncrypter)(newECB(b)) | ||
} | ||
|
||
func (x *ecbEncrypter) BlockSize() int { return x.blockSize } | ||
|
||
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) { | ||
if len(src)%x.blockSize != 0 { | ||
panic("crypto/cipher: input not full blocks") | ||
} | ||
if len(dst) < len(src) { | ||
panic("crypto/cipher: output smaller than input") | ||
} | ||
for len(src) > 0 { | ||
x.b.Encrypt(dst, src[:x.blockSize]) | ||
src = src[x.blockSize:] | ||
dst = dst[x.blockSize:] | ||
} | ||
} | ||
|
||
type ecbDecrypter ecb | ||
|
||
// NewECBDecrypter returns a BlockMode which decrypts in electronic code book | ||
// mode, using the given Block.s | ||
func NewECBDecrypter(b cipher.Block) cipher.BlockMode { | ||
return (*ecbDecrypter)(newECB(b)) | ||
} | ||
|
||
func (x *ecbDecrypter) BlockSize() int { return x.blockSize } | ||
|
||
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) { | ||
if len(src)%x.blockSize != 0 { | ||
panic("crypto/cipher: input not full blocks") | ||
} | ||
if len(dst) < len(src) { | ||
panic("crypto/cipher: output smaller than input") | ||
} | ||
for len(src) > 0 { | ||
x.b.Decrypt(dst, src[:x.blockSize]) | ||
src = src[x.blockSize:] | ||
dst = dst[x.blockSize:] | ||
} | ||
} |
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
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,17 @@ | ||
// Copyright 2021 E99p1ant. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package miband | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func (m *MiBand) GetBatteryPercent() (int, error) { | ||
data, err := m.client.ReadCharacteristic(m.batteryCharacteristic) | ||
if err != nil { | ||
return 0, errors.Wrap(err, "read characteristic") | ||
} | ||
return int(data[0]), nil | ||
} |
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,38 @@ | ||
// Copyright 2021 E99p1ant. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package miband | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func (m *MiBand) GetHeartRateOneTime() error { | ||
<-m.authed | ||
|
||
// Subscribe to the heart rate characteristic | ||
err := m.client.Subscribe(m.heartRateMeasureCharacteristic, false, m.handleHeartRateNotification) | ||
if err != nil { | ||
return errors.Wrap(err, "subscribe heart rate characteristic") | ||
} | ||
|
||
// Stop continuous. | ||
err = m.client.WriteCharacteristic(m.heartRateControlCharacteristic, []byte("\x15\x02\x00"), false) | ||
if err != nil { | ||
return errors.Wrap(err, "stop continuous") | ||
} | ||
|
||
// Stop manual. | ||
err = m.client.WriteCharacteristic(m.heartRateControlCharacteristic, []byte("\x15\x01\x00"), false) | ||
if err != nil { | ||
return errors.Wrap(err, "stop manual") | ||
} | ||
|
||
// Start manual. | ||
err = m.client.WriteCharacteristic(m.heartRateControlCharacteristic, []byte("\x15\x01\x01"), false) | ||
if err != nil { | ||
return errors.Wrap(err, "start manual") | ||
} | ||
return nil | ||
} |
Oops, something went wrong.