github.com/midokura/kubeedge@v1.2.0-mido.0/tests/stubs/devices/mockserver.go (about) 1 package devices 2 3 import ( 4 "context" 5 "flag" 6 "fmt" 7 "os" 8 "time" 9 10 "github.com/paypal/gatt" 11 "github.com/paypal/gatt/examples/option" 12 "github.com/paypal/gatt/examples/service" 13 "k8s.io/klog" 14 15 "github.com/kubeedge/kubeedge/tests/stubs/devices/services" 16 ) 17 18 const openBeaconUUID = "AA6062F098CA42118EC4193EB73CCEB6" 19 20 var timeDuration *int 21 22 func createServiceAndAdvertise(d gatt.Device, s gatt.State) { 23 // Setup GAP and GATT services for Linux implementation. 24 d.AddService(service.NewGapService("SensorTagMock")) 25 d.AddService(service.NewGattService()) 26 27 // Creating a temperature reading service 28 temperatureSvc := services.NewTemperatureService() 29 d.AddService(temperatureSvc) 30 31 // Advertise device name and service's UUIDs. 32 klog.Info("Advertising device name and service UUID") 33 d.AdvertiseNameAndServices("mock temp sensor model", []gatt.UUID{temperatureSvc.UUID()}) 34 35 // Advertise as an OpenBeacon iBeacon 36 klog.Info("Advertise as an OpenBeacon iBeacon") 37 d.AdvertiseIBeacon(gatt.MustParseUUID(openBeaconUUID), 1, 2, -59) 38 } 39 40 //usage is responsible for setting up the default settings of all defined command-line flags for klog. 41 func usage() { 42 flag.PrintDefaults() 43 os.Exit(2) 44 } 45 46 //init for getting command line arguments 47 func init() { 48 flag.Usage = usage 49 timeDuration = flag.Int("duration", 5, "time duration for which server should be run") 50 flag.Parse() 51 } 52 53 func main() { 54 d, err := gatt.NewDevice(option.DefaultServerOptions...) 55 if err != nil { 56 klog.Fatalf("Failed to open device, err: %s", err) 57 } 58 59 // Register optional handlers. 60 d.Handle( 61 gatt.CentralConnected(func(c gatt.Central) { fmt.Println("Connect: ", c.ID()) }), 62 gatt.CentralDisconnected(func(c gatt.Central) { fmt.Println("Disconnect: ", c.ID()) }), 63 ) 64 65 duration := time.Duration(*timeDuration) * time.Minute 66 ctx, cancel := context.WithTimeout(context.Background(), duration) 67 defer cancel() 68 d.Init(createServiceAndAdvertise) 69 70 select { 71 case <-ctx.Done(): 72 klog.Info("Stopping server and cleaning up") 73 d.StopAdvertising() 74 d.RemoveAllServices() 75 klog.Info("Stopped advertising and removed all services!!!!") 76 } 77 }