Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
fix(ble): add test and refactor ble device connect
Browse files Browse the repository at this point in the history
  • Loading branch information
guangbochen authored and Frank Mai committed Jun 22, 2020
1 parent 15b3079 commit 68b774b
Show file tree
Hide file tree
Showing 19 changed files with 530 additions and 160 deletions.
19 changes: 13 additions & 6 deletions adaptors/ble/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# BLE Adaptor

This is BLE(Bluetooth Low Energy) adaptor is used for bluetooth devices connection.
BLE stands for Bluetooth Low Energy (marketed as Bluetooth Smart). BLE is a form of wireless communication designed for short-range communications.

BLE adaptor implements the Bluetooth protocol and helps to define the the attributes of the connected BLE device.

## Registration Information

Expand Down Expand Up @@ -43,10 +45,14 @@ Grant permissions to Octopus as below:

Parameter | Description | Scheme | Required
--- | --- | --- | ---
name | Device name | string | either device name or macAddress is required
macAddress | Device access mac address | string | either name or macAddress is required
properties | Device properties | []*[DeviceProperty](#deviceproperty) | false
protocol | Device protocol config | [DeviceProtocol](#deviceprotocol) | true
properties | Device properties | []*[DeviceProperty](#deviceproperty) | false

### DeviceProtocol
Parameter | Description | Scheme | Required
--- | --- | --- | ---
name | Device name | string | NOT required when the device macAddress is provided
macAddress | Device access mac address | string | NOT required when the device name is provided

### DeviceProperty

Expand Down Expand Up @@ -116,8 +122,9 @@ spec:
labels:
device: xiaomi-temp-rs2200
spec:
name: "MJ_HT_V1"
# macAddress: ""
protocol:
name: "MJ_HT_V1"
macAddress: ""
properties:
- name: data
description: XiaoMi temp sensor with temperature and humidity data
Expand Down
49 changes: 37 additions & 12 deletions adaptors/ble/api/v1alpha1/bluetoothdevice_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,46 @@ package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// BluetoothDeviceSpec defines the desired state of BluetoothDevice
type BluetoothDeviceSpec struct {
// Parameter of the modbus device.
// +optional
Parameters *Parameters `json:"parameters,omitempty"`

// Protocol for accessing the BLE device.
// +kubebuilder:validation:Required
Protocol DeviceProtocol `json:"protocol"`

// Specifies the properties of the BLE device.
// +optional
Properties []DeviceProperty `json:"properties,omitempty"`
Name string `json:"name,omitempty"`
MacAddress string `json:"macAddress,omitempty"`
}

type Parameters struct {
// Specifies default device sync interval
// +kubebuilder:default:15s
SyncInterval v1.Duration `json:"syncInterval,omitempty"`

// Specifies default device connection timeout
// +kubebuilder:default:10s
Timeout v1.Duration `json:"timeout,omitempty"`
}

// DeviceProperty defines an individual ble device property
type DeviceProperty struct {
Name string `json:"name,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
AccessMode PropertyAccessMode `json:"accessMode,omitempty"`
Visitor PropertyVisitor `json:"visitor,omitempty"`
AccessMode PropertyAccessMode `json:"accessMode"`
Visitor PropertyVisitor `json:"visitor"`
}

// DeviceProtocol defines how to connect the BLE device
type DeviceProtocol struct {
Name string `json:"name,omitempty"`
MacAddress string `json:"macAddress,omitempty"`
}

// The access mode for a device property.
Expand All @@ -31,7 +56,7 @@ const (

// PropertyVisitor defines the specifics of accessing a particular device property
type PropertyVisitor struct {
CharacteristicUUID string `json:"characteristicUUID,omitempty"`
CharacteristicUUID string `json:"characteristicUUID"`
DefaultValue string `json:"defaultValue,omitempty"`
DataWriteTo map[string][]byte `json:"dataWrite,omitempty"`
BluetoothDataConverter BluetoothDataConverter `json:"dataConverter,omitempty"`
Expand Down Expand Up @@ -66,18 +91,18 @@ type BluetoothDeviceStatus struct {
}

type StatusProperties struct {
Name string `json:"name,omitempty"`
Desired string `json:"desired,omitempty"`
Reported string `json:"reported,omitempty"`
UpdatedAt metav1.Time `json:"updatedAt,omitempty"`
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
AccessMode PropertyAccessMode `json:"accessMode,omitempty"`
UpdatedAt metav1.Time `json:"updatedAt,omitempty"`
}

// +kubebuilder:object:root=true
// +k8s:openapi-gen=true
// +kubebuilder:resource:shortName=ble
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Name",type=string,JSONPath=`.spec.name`
// +kubebuilder:printcolumn:name="MacAddress",type=integer,JSONPath=`.spec.macAddress`
// +kubebuilder:printcolumn:name="Name",type=string,JSONPath=`.spec.protocol.name`
// +kubebuilder:printcolumn:name="MacAddress",type=integer,JSONPath=`.spec.protocol.macAddress`
// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp"
// BluetoothDevice is the Schema for the ble device API
type BluetoothDevice struct {
Expand Down
38 changes: 38 additions & 0 deletions adaptors/ble/api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion adaptors/ble/cmd/ble/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

const (
name = "ble"
description = ``
description = "The BLE adaptor defines the device configuration and the attributes of connected BLE device"
)

func newCommand() *cobra.Command {
Expand Down
50 changes: 37 additions & 13 deletions adaptors/ble/deploy/e2e/all_in_one.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
devices.edge.cattle.io/description: BLE stands for Bluetooth Low Energy (Bluetooth
LE, , and marketed as Bluetooth Smart). BLE is a form of wireless communication
designed for short-range communications. The BLE adaptor defines the device
configuration and the attributes of connected BLE device.
devices.edge.cattle.io/description: BLE stands for Bluetooth Low Energy (marketed
as Bluetooth Smart). BLE is a form of wireless communication designed for short-range
communications. The BLE adaptor defines the device configuration and the attributes
of connected BLE device.
devices.edge.cattle.io/device-property: '{"name":"string","dataType":"string","value":"string","updatedAt":"date"}'
devices.edge.cattle.io/enable: "true"
devices.edge.cattle.io/icon: https://octopus-assets.oss-cn-beijing.aliyuncs.com/adaptor-icons/ble-logo.svg
Expand All @@ -26,10 +26,10 @@ spec:
scope: Namespaced
versions:
- additionalPrinterColumns:
- jsonPath: .spec.name
- jsonPath: .spec.protocol.name
name: Name
type: string
- jsonPath: .spec.macAddress
- jsonPath: .spec.protocol.macAddress
name: MacAddress
type: integer
- jsonPath: .metadata.creationTimestamp
Expand All @@ -55,11 +55,18 @@ spec:
spec:
description: BluetoothDeviceSpec defines the desired state of BluetoothDevice
properties:
macAddress:
type: string
name:
type: string
parameters:
description: Parameter of the modbus device.
properties:
syncInterval:
description: Specifies default device sync interval
type: string
timeout:
description: Specifies default device connection timeout
type: string
type: object
properties:
description: Specifies the properties of the BLE device.
items:
description: DeviceProperty defines an individual ble device property
properties:
Expand Down Expand Up @@ -105,25 +112,42 @@ spec:
type: object
defaultValue:
type: string
required:
- characteristicUUID
type: object
required:
- accessMode
- name
- visitor
type: object
type: array
protocol:
description: Protocol for accessing the BLE device.
properties:
macAddress:
type: string
name:
type: string
type: object
required:
- protocol
type: object
status:
description: BluetoothDeviceStatus defines the observed state of BluetoothDevice
properties:
properties:
items:
properties:
desired:
accessMode:
description: The access mode for a device property.
type: string
name:
type: string
reported:
type: string
updatedAt:
format: date-time
type: string
value:
type: string
type: object
type: array
type: object
Expand Down
5 changes: 3 additions & 2 deletions adaptors/ble/deploy/e2e/dl-all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ spec:
labels:
device: xiaomi-temp-rs2200
spec:
name: "MJ_HT_V1"
# macAddress: ""
protocol:
name: "MJ_HT_V1"
macAddress: ""
properties:
- name: data
description: XiaoMi temp sensor with temperature and humidity data
Expand Down
13 changes: 7 additions & 6 deletions adaptors/ble/deploy/e2e/dl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ metadata:
name: xiaomi-temp-rs2200
spec:
adaptor:
node: ubuntu
node: edge-worker
name: adaptors.edge.cattle.io/ble
parameters:
syncInterval: 30
timeout: 60
model:
apiVersion: "devices.edge.cattle.io/v1alpha1"
kind: "BluetoothDevice"
Expand All @@ -17,8 +14,12 @@ spec:
labels:
device: xiaomi-temp-rs2200
spec:
name: "MJ_HT_V1"
# macAddress: ""
parameters:
syncInterval: 15s
timeout: 10s
protocol:
name: "MJ_HT_V1"
macAddress: ""
properties:
- name: data
description: XiaoMi temp sensor with temperature and humidity data
Expand Down
Loading

0 comments on commit 68b774b

Please sign in to comment.