0.0.67 • Published 4 years ago

alexa-simple-smart-home-controller v0.0.67

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

Imporant Notice:

This library is deprecated.

This library currently maintenance on below repository.

https://github.com/secual/alexa-smart-home-controller

alexa-simple-smart-home-controller (beta)

This package make your smart home skill code simpler.

** Important: This package is now Work in progress.

Concept

  • Be Readable
  • Can be more focus to implement a logic of device cloud
  • Implement simplify

install

npm install alexa-simple-smart-home-controller

or

yarn add alexa-simple-smart-home-controller

example

import SmartHomeController from 'alexa-simple-smart-home-controller'

// Yous have to implement a logic of your device cloud
import * as DeviceCloud from './deviceCloud'

// @ts-ignore
export const handler = async (event, context) => {
  console.log(event, context)
  console.log(JSON.stringify(event))

  const controller = new SmartHomeController(
    event,
    DeviceCloud.discoverFunc,
    DeviceCloud.searchFunc
  )

  const response = await controller.run()
  return response
};

How to implement your Smart Home Skill with alexa-simple-smart-home-controller

Step1: implement device discovery function

Smart home skill has to discover user devices from device cloud. You can implement discovery logic as DeviceSearchFunction object. This object needs for creating SmartHomeController instance.

 export type DeviceSearchFunction = (
  event: Device.ControllerRequestEvent
) => Promise<Device.IUserDevice>;

Step2: implement device object

In this package, Requesting to the device cloud is used through the UserDevice object. For example, when your skill request to the light device, You will implement a device object for the light device.

class LightDevice extends Device.UserDevice {
  public async sendSignal(): Promise<Device.Response | {}> {
    const header = this.event.directive.header

    switch (header.name) {
      case 'SetBrightness':
          // Requesting for your light device to the device cloud here.
          break;
      case 'AdjustBrightness':
          // Requesting for your light device to the device cloud here.
        break;
      default:
        break;
    }
    const r: Device.Response = {
      event: {
        header: {
          namespace: 'Alexa',
          name: 'Response',
          messageId: this.event.directive.header.messageId + '-R',
          payloadVersion: '3',
          correlationToken: 'token1'
        },
        endpoint: this.event.directive.endpoint,
        payload: 'hoge' 
      },
      context: {
        properties: [
          {
            namespace: header.namespace,
            name: header.name,
            value: 'ON',
            timeOfSample: 'test',
            uncertaintyInMilliseconds: 50
          }
        ]
      }
    }
    return r
  }
}

Step3: implement search device function

This package is made for skill can respond utterances for several types of devices. So You have to find target device from user utterance. DeviceSearchFunction object is to do so.

Easiest way is check device category. (below)

export const searchFunc: Device.DeviceSearchFunction = async (event) => {
  // @ts-ignore
  return DeviceMap[event.directive.header.namespace](event)
}

export const DeviceMap = {
  'Alexa.BrightnessController': (event: Device.ControllerRequestEvent) => {
    return new LightDevice(event)
  },
  'Alexa.ChannelController': (event: Device.ControllerRequestEvent) => {
    return new TvDevice(event)
  }
}
0.0.67

4 years ago

0.0.66

4 years ago

0.0.65

4 years ago

0.0.63

4 years ago

0.0.64

4 years ago

0.0.62

4 years ago

0.0.61

4 years ago

0.0.60

4 years ago

0.0.59

4 years ago

0.0.58

4 years ago

0.0.57

4 years ago

0.0.56

4 years ago

0.0.55

4 years ago

0.0.52

4 years ago

0.0.48

4 years ago

0.0.46

4 years ago

0.0.47

4 years ago

0.0.44

4 years ago

0.0.45

4 years ago

0.0.43

4 years ago

0.0.42

4 years ago

0.0.40

4 years ago

0.0.41

4 years ago

0.0.38

4 years ago

0.0.39

4 years ago

0.0.37

4 years ago

0.0.35

4 years ago

0.0.34

4 years ago

0.0.33

4 years ago

0.0.31

4 years ago

0.0.32

4 years ago

0.0.30

4 years ago

0.0.29

4 years ago

0.0.28

4 years ago

0.0.27

4 years ago

0.0.26

4 years ago

0.0.24

4 years ago

0.0.21

4 years ago

0.0.22

4 years ago

0.0.20

4 years ago

0.0.16

4 years ago

0.0.17

4 years ago

0.0.19

4 years ago

0.0.15

4 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago