code.pfad.fr/gohmekit@v0.2.1/cmd/generate/services.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  )
     7  
     8  func writeService(m Metadata) error {
     9  	f, err := os.Create("hapip/characteristic/service/apple_service_gen.go")
    10  	if err != nil {
    11  		return err
    12  	}
    13  	defer f.Close()
    14  
    15  	_, err = f.WriteString(`// Code generated by cmd/generate/main.go; DO NOT EDIT.
    16  
    17  package service
    18  
    19  import (
    20  	"code.pfad.fr/gohmekit/hapip"
    21  	"code.pfad.fr/gohmekit/hapip/characteristic"
    22  )
    23  `)
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	type charProp struct {
    29  		variableName string
    30  		description  string
    31  	}
    32  	charMap := make(map[string]charProp)
    33  	for _, c := range m.Characteristics {
    34  		funcName := camelCase(c.Name)
    35  		funcName = strings.ToLower(funcName[:1])[:1] + funcName[1:]
    36  		charMap[c.UUID] = charProp{
    37  			variableName: funcName,
    38  			description:  c.Name + " (" + shortUUID(c.UUID) + ")",
    39  		}
    40  	}
    41  
    42  	for _, s := range m.Services {
    43  		txt := ""
    44  		funcName := camelCase(s.Name)
    45  		short := shortUUID(s.UUID)
    46  		txt += "\nconst " + funcName + "_Type = \"" + short + "\"\n"
    47  
    48  		requiredArgs := ""
    49  		charsArgs := []string{}
    50  		txt += "\n// " + funcName + " represents a " + s.Name + " service (" + shortUUID(s.UUID) + ")\n"
    51  		if len(s.RequiredCharacteristics) > 0 {
    52  			txt += "// Required characteristics:\n"
    53  			for _, c := range s.RequiredCharacteristics {
    54  				cp := charMap[c]
    55  				txt += "//   - " + cp.variableName + ": " + cp.description + "\n"
    56  
    57  				requiredArgs += cp.variableName + " hapip.Characteristic, "
    58  				charsArgs = append(charsArgs, cp.variableName)
    59  			}
    60  			txt += "//\n"
    61  		}
    62  		if len(s.OptionalCharacteristics) > 0 {
    63  			// TODO: optional characteristics
    64  			txt += "// Optional characteristics:\n"
    65  			for _, c := range s.OptionalCharacteristics {
    66  				cp := charMap[c]
    67  				txt += "//   - " + cp.description + "\n"
    68  			}
    69  			txt += "//\n"
    70  		}
    71  		txt += "// UUID: " + s.UUID + "\n"
    72  		txt += "func " + funcName + "(" + requiredArgs + "optional ...hapip.Characteristic) characteristic.Service {\n"
    73  		txt += "	chars := []hapip.Characteristic{" + strings.Join(charsArgs, ", ") + "}\n"
    74  		txt += "	return characteristic.Service{\n"
    75  		txt += "		Typ:            " + funcName + "_Type,\n"
    76  		txt += "		Characteristic: append(chars, optional...),\n"
    77  		txt += "	}\n"
    78  		txt += "}\n"
    79  		_, err = f.WriteString(txt)
    80  		if err != nil {
    81  			return err
    82  		}
    83  	}
    84  
    85  	if err = f.Close(); err != nil {
    86  		return err
    87  	}
    88  	return nil
    89  }