Example #1
0
/*
LocalScriptBackend provides the possibility to execute local scripts while
publishing their results on the internal eventbus. Scripts can either be
registered as components/sensors or as services. In the first case scripts are
polled in a regular interval. The latter one enables the script to be called
vie Service Call requests.

In order to use this backend add something like the following to your
configuration:

 backends:
  - type: script
	  scripts:
		 - exec: /path/to/script
		   name: MyPrettyName
		   #type: sensor (default)
		 - exec: /path/to/other/script
		 	 name: my_pretty_name_2
			 type: service

Service scripts can latter be called by sending a Service Call request to
`script.MyPrettyName`. Note that `script` may differ if a `name` field is
specified within the backend configuration.

The result of script executions is published as a string within state_changed
events.

*/
func LocalScriptBackend(config map[string]interface{}, back *backend.Backend) error {
	var scripts []interface{}
	services := make(map[string]command)

	switch a := config["scripts"].(type) {
	case []interface{}:
		scripts = a
	default:
		logrus.Warnf("Backend: %s invalid configuration: %#v", back.Name(), config)
		return nil // CHANGE ME
	}

	for _, s := range scripts {
		script, ok := s.(map[string]interface{})
		if !ok {
			logrus.Warnf("Backend: %s script with invalid configuration: %#v", back.Name(), script)
			continue
		}
		exec, ok := script["exec"].(string)
		if !ok {
			logrus.Warnf("Backend: %s script configuration without `exec`: %s", back.Name(), script)
			continue
		}
		name, ok := script["name"].(string)
		if !ok {
			name = exec
		}
		execType, ok := script["type"].(string)
		if !ok {
			execType = "sensor"
		}
		cmd := command{
			command: exec,
		}

		comp := &backend.Component{
			StateSink: make(chan interface{}),
			Name:      name,
			Polling:   updateScriptResult,
			Internal:  cmd,
		}
		switch execType {
		case "sensor":
			back.RegisterComponent <- comp
		case "service":
			service.ServiceRegistry.Register(back, comp.Name, "Call local script",
				cmd.execute, map[string]string{}, map[string]string{})
		}
	}

	back.Internal = services

	return nil
}
Example #2
0
/*
Backend providing a JSON based component connector via Websockets.

Configuration

In order to use this websocket based connector add something like the following
to your configuration file:

 backends:
  - type: websocket
    listen: httpapi


Description

The `listen` parameter may specifiy an endpoint the websocket server should
be bound to (e.g. localhost:8080, 0.0.0.0:6600, :9000). As a special case,
"httpapi" can be specified. If so, the websocket connector backend attaches it
self the the HTTP API plugin and will therefore share it's HTTP server. In the
latter case the access URI will be "/devices" instead of "/".


API Description

First a Websocket connection must be established. During this HTTP request,
the device name and type must be provided. Consider the following Websocket URL:

  ws://example.com/device?name=<YourDeviceName>&type=<YourDeviceType>

If this module is not used in conjunction with the HTTP API plugin the request
URL should look like:

	ws://example.com/?name=<YourDeviceName>&type=<YourDeviceType>

If both parameters are provided a websocket connection can be initialized.

Now the client can start providing data by sending JSON messages following this
format:

  {
	   "topic": "state_changed",
		 "state": <YourStateOfWhatsOEver>
  }

Everything provided in `state` will be published on the internal eventbus and
thus be available for all subscribers.

Note that a WebSocket component can only send state updates and register service
calles. It is prohibited to execute other services or send other topics then
"state_changed" and "register_service".

Invalid messages will be logged and the respective connection will be closed.

In order to register new service calles within GoHome a Websocket component could
send a service registration request with the following format:

 {
   "topic": "register_service",
   "name": "<NameOfYourService>",
   "required": {
   "param1": "This is the first and only parameter required"
  },
  "optional" {
   "param2": "This is an optional parameter which defaults to: some-value"
  }
 }

The `name`, `required` and `optional` fields are required to be set during
service registration. However, `required` and `optional` may be an empty
map/object.

Whenever a service should be executed GoHome send the following request via the
Websocket connection:

{
	"topic": "service_call",
	"method": "<NameOfYourService>",
	"params": [<ListOfParameters]
}

BUG: Currently it is NOT possible for Websocket components to return a response
to the caller! All service call will immediately return nil.

*/
func WebsocketConnectorBackend(config map[string]interface{}, back *backend.Backend) error {
	logrus.Infof("Started backend %s with name %s", config["type"].(string), back.Name())

	listen, _ := utils.GetString("listen", config, "httpapi")

	back.OnShutdown = shutdownWebsockets
	back.Internal = &websocketServer{
		clients: make(map[string]peer),
		name:    back.Name(),
		back:    back,
	}

	if listen == "httpapi" {
		logrus.Infof("Backend: %s: attaching to default HTTP server (httpapi) on URI /device", back.Name())
		http.Handle("/device", back.Internal.(*websocketServer))
	} else {
		logrus.Infof("Backend: %s: listening on %s", back.Name(), listen)
		go http.ListenAndServe(listen, back.Internal.(*websocketServer))
	}
	return nil
}
Example #3
0
func NotifyMyAndroidBackend(config map[string]interface{}, back *backend.Backend) error {
	apiKey, ok := utils.GetString("api_key", config, "")
	if !ok {
		return fmt.Errorf("Backend: %s missing configuration option 'api_key'", back.Name())
	}

	comp := &notifyMyAndroid{
		nma:     nma.New(apiKey),
		backend: back,
	}
	back.Internal = comp

	service.ServiceRegistry.Register(back, "notify", "Send a notification via Notify-My-Android",
		comp.Notify, map[string]string{
			"subject": "Subject of the notification",
			"message": "Message part of the notification",
		},
		map[string]string{
			"priority": "Priority of the notification. Defaults to 0",
		})

	return nil
}