github.com/stampzilla/stampzilla-go@v2.0.0-rc9+incompatible/nodes/stampzilla-google-assistant/googleassistant/request.go (about)

     1  package googleassistant
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/sirupsen/logrus"
     7  )
     8  
     9  // Intent of the action
    10  type Intent string
    11  
    12  const (
    13  	// SyncIntent is used when syncing devices
    14  	SyncIntent = "action.devices.SYNC"
    15  	// QueryIntent is used when querying for status
    16  	QueryIntent = "action.devices.QUERY"
    17  	// ExecuteIntent is used when controling devices
    18  	ExecuteIntent = "action.devices.EXECUTE"
    19  
    20  	CommandBrightnessAbsolute = "action.devices.commands.BrightnessAbsolute"
    21  	CommandOnOff              = "action.devices.commands.OnOff"
    22  )
    23  
    24  // Inputs from google
    25  type Inputs []map[string]json.RawMessage
    26  
    27  func (i Inputs) Intent() Intent {
    28  	for _, v := range i {
    29  		if v, ok := v["intent"]; ok {
    30  			in := ""
    31  			err := json.Unmarshal(v, &in)
    32  			if err != nil {
    33  				logrus.Error(err)
    34  				return ""
    35  			}
    36  
    37  			return Intent(in)
    38  		}
    39  	}
    40  	return ""
    41  }
    42  func (i Inputs) Payload() Payload {
    43  	for _, v := range i {
    44  		if v, ok := v["payload"]; ok {
    45  			pl := Payload{}
    46  			err := json.Unmarshal(v, &pl)
    47  			if err != nil {
    48  				logrus.Error(err)
    49  				return Payload{}
    50  			}
    51  			return pl
    52  		}
    53  	}
    54  	return Payload{}
    55  }
    56  
    57  type Payload struct {
    58  	Commands []struct {
    59  		Devices []struct {
    60  			ID string `json:"id"`
    61  			//CustomData struct {
    62  			//FooValue int    `json:"fooValue"`
    63  			//BarValue bool   `json:"barValue"`
    64  			//BazValue string `json:"bazValue"`
    65  			//} `json:"customData"`
    66  		} `json:"devices"`
    67  		Execution []struct {
    68  			Command string `json:"command"`
    69  			Params  struct {
    70  				On         bool `json:"on"`
    71  				Brightness int  `json:"brightness"`
    72  			} `json:"params"`
    73  		} `json:"execution"`
    74  	} `json:"commands"`
    75  }
    76  
    77  type Request struct {
    78  	RequestID string
    79  	Inputs    Inputs
    80  }