github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/mappers/bluetooth_mapper/data_converter/data_converter.go (about)

     1  /*
     2  Copyright 2019 The KubeEdge Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8     http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package dataconverter
    18  
    19  import (
    20  	"strconv"
    21  	"strings"
    22  )
    23  
    24  // Bluetooth Protocol Operation type
    25  const (
    26  	BluetoothAdd      string = "Add"
    27  	BluetoothSubtract string = "Subtract"
    28  	BluetoothMultiply string = "Multiply"
    29  	BluetoothDivide   string = "Divide"
    30  )
    31  
    32  //Converter is the structure that contains data conversion specific configuration
    33  type Converter struct {
    34  	DataWrite DataWrite `yaml:"write"`
    35  	DataRead  DataRead  `yaml:"read"`
    36  }
    37  
    38  //dataWrite structure contains configuration information specific to data-writes
    39  type DataWrite struct {
    40  	Attributes []WriteAttribute `yaml:"attributes"`
    41  }
    42  
    43  //WriteAttribute structure contains the name of the attribute as well as a data-map of values to be written
    44  type WriteAttribute struct {
    45  	Name       string             `yaml:"name"`
    46  	Operations map[string]DataMap `yaml:"operations"`
    47  }
    48  
    49  //DataMap structure contains a mapping between the value that arrives from the platform (expected value) and
    50  // the byte value to be written into the device
    51  type DataMap struct {
    52  	DataMapping map[string][]byte `yaml:"data-map"`
    53  }
    54  
    55  //dataRead structure contains configuration information specific to data-read
    56  type DataRead struct {
    57  	Actions []ReadAction `yaml:"actions"`
    58  }
    59  
    60  //ReadAction specifies the name of the action along with the conversion operations to be performed in case of data-read
    61  type ReadAction struct {
    62  	ActionName          string        `yaml:"action-name"`
    63  	ConversionOperation ReadOperation `yaml:"conversion-operation"`
    64  }
    65  
    66  //ReadOperation specifies how to convert the data received from the device into meaningful data
    67  type ReadOperation struct {
    68  	StartIndex       int      `yaml:"start-index"`
    69  	EndIndex         int      `yaml:"end-index"`
    70  	ShiftLeft        uint     `yaml:"shift-left"`
    71  	ShiftRight       uint     `yaml:"shift-right"`
    72  	Multiply         float64  `yaml:"multiply"`
    73  	Divide           float64  `yaml:"divide"`
    74  	Add              float64  `yaml:"add"`
    75  	Subtract         float64  `yaml:"subtract"`
    76  	OrderOfExecution []string `yaml:"order-of-execution"`
    77  }
    78  
    79  //ConvertReadData is the function responsible to convert the data read from the device into meaningful data
    80  func (operation *ReadOperation) ConvertReadData(data []byte) float64 {
    81  	var intermediateResult uint64
    82  	var initialValue []byte
    83  	var initialStringValue = ""
    84  	if operation.StartIndex <= operation.EndIndex {
    85  		for index := operation.StartIndex; index <= operation.EndIndex; index++ {
    86  			initialValue = append(initialValue, data[index])
    87  		}
    88  	} else {
    89  		for index := operation.StartIndex; index >= operation.EndIndex; index-- {
    90  			initialValue = append(initialValue, data[index])
    91  		}
    92  	}
    93  	for _, value := range initialValue {
    94  		initialStringValue = initialStringValue + strconv.Itoa(int(value))
    95  	}
    96  	initialByteValue, _ := strconv.ParseUint(initialStringValue, 16, 16)
    97  
    98  	if operation.ShiftLeft != 0 {
    99  		intermediateResult = initialByteValue << operation.ShiftLeft
   100  	} else if operation.ShiftRight != 0 {
   101  		intermediateResult = initialByteValue >> operation.ShiftRight
   102  	}
   103  	finalResult := float64(intermediateResult)
   104  	for _, executeOperation := range operation.OrderOfExecution {
   105  		switch strings.ToUpper(executeOperation) {
   106  		case strings.ToUpper(BluetoothAdd):
   107  			finalResult = finalResult + operation.Add
   108  		case strings.ToUpper(BluetoothSubtract):
   109  			finalResult = finalResult - operation.Subtract
   110  		case strings.ToUpper(BluetoothMultiply):
   111  			finalResult = finalResult * operation.Multiply
   112  		case strings.ToUpper(BluetoothDivide):
   113  			finalResult = finalResult / operation.Divide
   114  		}
   115  	}
   116  	return finalResult
   117  }