github.com/stampzilla/stampzilla-go@v2.0.0-rc9+incompatible/nodes/stampzilla-example/main.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/sirupsen/logrus"
     8  	"github.com/stampzilla/stampzilla-go/nodes/stampzilla-server/models/devices"
     9  	"github.com/stampzilla/stampzilla-go/pkg/node"
    10  )
    11  
    12  func main() {
    13  	node := node.New("example")
    14  
    15  	node.OnConfig(updatedConfig)
    16  
    17  	dev1 := &devices.Device{
    18  		Name:   "Device1",
    19  		Type:   "light",
    20  		ID:     devices.ID{ID: "1"},
    21  		Online: true,
    22  		Traits: []string{"OnOff"},
    23  		State: devices.State{
    24  			"on": false,
    25  		},
    26  	}
    27  	dev2 := &devices.Device{
    28  		Name:   "Device2",
    29  		Type:   "light",
    30  		ID:     devices.ID{ID: "2"},
    31  		Online: true,
    32  		Traits: []string{"OnOff", "Brightness", "ColorSetting"},
    33  		State: devices.State{
    34  			"on": false,
    35  		},
    36  	}
    37  	dev3 := &devices.Device{
    38  		Name:   "Device3",
    39  		Type:   "light",
    40  		ID:     devices.ID{ID: "3"},
    41  		Online: true,
    42  		State: devices.State{
    43  			"on": false,
    44  		},
    45  	}
    46  	dev4 := &devices.Device{
    47  		Name:   "Device4 that requires a node config",
    48  		Type:   "light",
    49  		ID:     devices.ID{ID: "4"},
    50  		Online: true,
    51  		Traits: []string{"OnOff"},
    52  		State: devices.State{
    53  			"on": false,
    54  		},
    55  	}
    56  
    57  	node.OnRequestStateChange(func(state devices.State, device *devices.Device) error {
    58  		logrus.Info("OnRequestStateChange:", state, device.ID)
    59  
    60  		// Make device 3 follow the value of device 1
    61  		if device.ID.ID == "1" {
    62  			dev3.State["on"] = state["on"]
    63  			node.AddOrUpdate(dev3)
    64  		}
    65  
    66  		// Load device config from the config struct
    67  		devConfig, ok := config.Devices[device.ID.ID]
    68  
    69  		// Require a device config for node 4 only
    70  		if !ok && device.ID.ID == "4" {
    71  			return fmt.Errorf("Foudn no config for device %s", device.ID)
    72  		}
    73  
    74  		state.Bool("on", func(on bool) {
    75  			if on {
    76  				fmt.Printf("turning on %s with senderid %s\n", device.ID.String(), devConfig.SenderID)
    77  				return
    78  			}
    79  			fmt.Printf("turning off %s with senderid %s\n", device.ID.String(), devConfig.SenderID)
    80  		})
    81  
    82  		state.Float("brightness", func(lvl float64) {
    83  			fmt.Printf("dimming to %f on device %s\n", lvl, device.ID.String())
    84  		})
    85  
    86  		return nil
    87  	})
    88  
    89  	err := node.Connect()
    90  
    91  	if err != nil {
    92  		logrus.Error(err)
    93  		return
    94  	}
    95  
    96  	node.AddOrUpdate(dev1)
    97  	node.AddOrUpdate(dev2)
    98  	node.AddOrUpdate(dev3)
    99  	node.AddOrUpdate(dev4)
   100  
   101  	node.Wait()
   102  }
   103  
   104  var config = &Config{}
   105  
   106  func updatedConfig(data json.RawMessage) error {
   107  	logrus.Info("Received config from server:", string(data))
   108  
   109  	newConf := &Config{}
   110  	err := json.Unmarshal(data, newConf)
   111  	if err != nil {
   112  		return err
   113  	}
   114  
   115  	// example when we change "global" config
   116  	if newConf.GatewayIP != config.GatewayIP {
   117  		fmt.Println("ip changed. lets connect to that instead")
   118  	}
   119  
   120  	config = newConf
   121  	logrus.Info("Config is now: ", config)
   122  
   123  	return nil
   124  }
   125  
   126  type Config struct {
   127  	Devices map[string]struct {
   128  		SenderID string
   129  		RecvEEPs []string // example config taken from enocean node
   130  	}
   131  	GatewayIP string
   132  }
   133  
   134  /*
   135  Config to put into gui:
   136  {
   137  	"devices":{
   138  		"1":{
   139  			"senderid":"senderid1",
   140  			"recveeps":[
   141  				"asdf1",
   142  				"asdf2"
   143  			]
   144  		},
   145  		"2":{
   146  			"senderid":"senderid1",
   147  			"recveeps":[
   148  				"asdf1",
   149  				"asdf2"
   150  			]
   151  		}
   152  	}
   153  }
   154  
   155  */