API 사용

액세스 토큰이 확보되고 초기 기기 목록이 호출되면 이제 SDM API를 사용하여 기기에 액세스하고 기기를 제어할 수 있습니다.

구조 및 기기 나열

curl를 사용하여 structures 엔드포인트에 간단한 GET 호출을 실행합니다.

curl -X GET 'https://smartdevicemanagement.googleapis.com/v1/enterprises/project-id/structures' \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer access-token'

호출이 성공하면Device Access 프로젝트에 연결된 계정의 구조 목록이 반환됩니다.

{
  "structures": [
    {
      "name": "enterprises/project-id/structures/structure-id",
      "traits": {
        "sdm.structures.traits.Info": {
          "customName": "structure-name"
        }
      }
    }
  ]
}

아직 하지 않은 경우 devices 엔드포인트에 GET 호출을 실행하여 기기 목록을 가져옵니다.

curl -X GET 'https://smartdevicemanagement.googleapis.com/v1/enterprises/project-id/devices' \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer access-token'

호출이 성공하면 Device Access프로젝트에 연결된 기기 목록이 반환됩니다. 기기마다 사용 가능한 특성의 고유한 목록이 있습니다.

{
  "devices": [
    {
      "name": "enterprises/project-id/devices/device-id",
      "type": "sdm.devices.types.device-type",
      "traits": { ... },
      "parentRelations": [
        {
          "parent": "enterprises/project-id/structures/structure-id/rooms/room-id",
          "displayName": "device-room-name"
        }
      ]
    }
  ]
}

다른 API 호출에도 필요하므로 각 기기의 device-id를 복사합니다.

기기 정보 가져오기

특정 기기에 관한 정보를 가져오려면 device-id 엔드포인트에 GET 호출을 실행합니다.

curl -X GET 'https://smartdevicemanagement.googleapis.com/v1/enterprises/project-id/devices/device-id' \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer access-token'

응답은 이전과 비슷하지만 특정 기기에만 적용됩니다.

{
  "name": "enterprises/project-id/devices/device-id",
  "type": "sdm.devices.types.device-type",
  "traits": { ... },
  "parentRelations": [
    {
      "parent": "enterprises/project-id/structures/structure-id/rooms/room-id",
      "displayName": "device-room-name"
    }
  ]
}

명령어 실행

GET 호출을 사용하여 액세스의 유효성을 검사한 후 승인한 기기 유형에 따라 명령어를 실행해 보세요.

온도 조절기

curl -X POST \
  'https://smartdevicemanagement.googleapis.com/v1/enterprises/project-id/devices/device-id:executeCommand' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer access-token' \
  --data-raw '{
    "command" : "sdm.devices.commands.ThermostatMode.SetMode",
    "params" : {
      "mode" : "HEAT"
    }
  }'

카메라

curl -X POST \
  'https://smartdevicemanagement.googleapis.com/v1/enterprises/project-id/devices/device-id:executeCommand' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer access-token' \
  --data-raw '{
    "command" : "sdm.devices.commands.CameraLiveStream.GenerateRtspStream",
    "params" : {}
  }'

호출이 성공하면 다음과 같은 결과가 발생합니다.

온도 조절기

빈 응답이 수신되고 실제 온도 조절기가 현재 모드를 명령어 매개변수에 지정된 모드로 변경합니다.

{}

카메라

실시간 스트림 URL과 관련 토큰을 받게 됩니다.

{
  "results" : {
    "streamUrls" : {
      "rtspUrl" : "rtsps://someurl.com/CjY5Y3VKaTZwR3o4Y19YbTVfMF...?auth=g.0.streamingToken"
    },
    "streamExtensionToken" : "CjY5Y3VKaTZwR3o4Y19YbTVfMF...",
    "streamToken" : "g.0.streamingToken",
    "expiresAt" : "2018-01-04T18:30:00.000Z"
  }
}

문제 해결

인증되지 않음

SDM API용 액세스 토큰은 1시간 동안만 유효합니다. UNAUTHENTICATED 응답을 받으면 토큰이 만료된 것일 수 있습니다. 갱신 토큰을 사용하여 새 액세스 토큰을 가져오세요.

기타 오류

전체 오류 코드 목록은 오류 코드 참조를 확인하세요. Device Access