github.com/midokura/kubeedge@v1.2.0-mido.0/tests/stubs/devices/services/temperature.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 services 18 19 import ( 20 "fmt" 21 22 "github.com/paypal/gatt" 23 ) 24 25 const ( 26 // serviceUUID is characteristic UUID for creating a new temperature service. 27 serviceUUID = "09fc95c0-c111-11e3-9904-0002a5d5c51b" 28 // readCharacteristicUUID is characteristic UUID for reading temperature. 29 readCharacteristicUUID = "11fac9e0-c111-11e3-9246-0002a5d5c51b" 30 // writeCharacteristicUUID is characteristic UUID for writing temperature. 31 writeCharacteristicUUID = "16fe0d80-c111-11e3-b8c8-0002a5d5c51b" 32 // readWrittenDataCharacteristicUUID is characteristic UUID for reading data written to connected device. 33 readWrittenDataCharacteristicUUID = "1c927b50-c116-11e3-8a33-0800200c9a66" 34 // dataConverterUUID is characteristic UUID for data conversion. 35 dataConverterUUID = "2d816a41-d335-44f5-7b55-9000200c8a77" 36 // twinStateUUID is characteristic UUID for changing twin state of device. 37 twinStateUUID = "3d816a41-e336-55e5-7c66-8000100d8a44" 38 ) 39 40 var dataWrite string 41 var write bool 42 var state string 43 44 func NewTemperatureService() *gatt.Service { 45 temp := 36 46 s := gatt.NewService(gatt.MustParseUUID(serviceUUID)) 47 48 s.AddCharacteristic(gatt.MustParseUUID(readCharacteristicUUID)).HandleReadFunc( 49 func(rsp gatt.ResponseWriter, req *gatt.ReadRequest) { 50 fmt.Fprintf(rsp, "%d", temp) 51 }) 52 53 s.AddCharacteristic(gatt.MustParseUUID(writeCharacteristicUUID)).HandleWriteFunc( 54 func(r gatt.Request, data []byte) (status byte) { 55 fmt.Println("Wrote:", string(data)) 56 write = true 57 dataWrite = string(data) 58 return gatt.StatusSuccess 59 }) 60 61 s.AddCharacteristic(gatt.MustParseUUID(readWrittenDataCharacteristicUUID)).HandleReadFunc( 62 func(rsp gatt.ResponseWriter, req *gatt.ReadRequest) { 63 if write { 64 fmt.Fprintf(rsp, "%s", dataWrite) 65 } 66 }) 67 68 s.AddCharacteristic(gatt.MustParseUUID(dataConverterUUID)).HandleReadFunc( 69 func(rsp gatt.ResponseWriter, req *gatt.ReadRequest) { 70 data := []uint8{32, 10, 248, 12} 71 fmt.Fprintf(rsp, "%s", data) 72 }) 73 74 s.AddCharacteristic(gatt.MustParseUUID(twinStateUUID)).HandleWriteFunc( 75 func(r gatt.Request, data []byte) (status byte) { 76 state = "Red" 77 return gatt.StatusSuccess 78 }) 79 80 return s 81 }