Ada lebih banyak contoh AWS SDK yang tersedia di repo Contoh SDK AWS Doc
Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
AWS IoT Events contoh menggunakan AWS CLI
Contoh kode berikut menunjukkan cara melakukan tindakan dan mengimplementasikan skenario umum dengan menggunakan AWS Command Line Interface with AWS IoT Events.
Tindakan merupakan kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Sementara tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks dalam skenario terkait.
Setiap contoh menyertakan tautan ke kode sumber lengkap, di mana Anda dapat menemukan instruksi tentang cara mengatur dan menjalankan kode dalam konteks.
Topik
Tindakan
Contoh kode berikut menunjukkan cara menggunakanbatch-put-message.
- AWS CLI
-
Untuk mengirim pesan (input) ke AWS IoT Events
batch-put-messageContoh berikut mengirimkan satu set pesan ke sistem AWS IoT Events. Setiap payload pesan diubah menjadi input yang Anda tentukan (inputName) dan dicerna ke detektor apa pun yang memantau input tersebut. Jika beberapa pesan dikirim, urutan pemrosesan pesan tidak dijamin. Untuk menjamin pemesanan, Anda harus mengirim pesan satu per satu dan menunggu respons yang berhasil.aws iotevents-data batch-put-message \ --cli-input-jsonfile://highPressureMessage.jsonIsi dari
highPressureMessage.json:{ "messages": [ { "messageId": "00001", "inputName": "PressureInput", "payload": "{\"motorid\": \"Fulton-A32\", \"sensorData\": {\"pressure\": 80, \"temperature\": 39} }" } ] }Output:
{ "BatchPutMessageErrorEntries": [] }Untuk informasi selengkapnya, lihat BatchPutMessagedi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat BatchPutMessage
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakanbatch-update-detector.
- AWS CLI
-
Untuk memperbarui detektor (contoh)
batch-update-detectorContoh berikut memperbarui status, nilai variabel, dan pengaturan timer dari satu atau lebih detektor (instance) dari model detektor tertentu.aws iotevents-data batch-update-detector \ --cli-input-jsonfile://budFulton-A32.jsonIsi dari
budFulton-A32.json:{ "detectors": [ { "messageId": "00001", "detectorModelName": "motorDetectorModel", "keyValue": "Fulton-A32", "state": { "stateName": "Normal", "variables": [ { "name": "pressureThresholdBreached", "value": "0" } ], "timers": [ ] } } ] }Output:
{ "batchUpdateDetectorErrorEntries": [] }Untuk informasi selengkapnya, lihat BatchUpdateDetectordi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat BatchUpdateDetector
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakancreate-detector-model.
- AWS CLI
-
Untuk membuat model detektor
create-detector-modelContoh berikut membuat model detektor dengan konfigurasi yang ditentukan oleh file parameter.aws iotevents create-detector-model \ --cli-input-jsonfile://motorDetectorModel.jsonIsi dari
motorDetectorModel.json:{ "detectorModelName": "motorDetectorModel", "detectorModelDefinition": { "states": [ { "stateName": "Normal", "onEnter": { "events": [ { "eventName": "init", "condition": "true", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "0" } } ] } ] }, "onInput": { "transitionEvents": [ { "eventName": "Overpressurized", "condition": "$input.PressureInput.sensorData.pressure > 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached + 3" } } ], "nextState": "Dangerous" } ] } }, { "stateName": "Dangerous", "onEnter": { "events": [ { "eventName": "Pressure Threshold Breached", "condition": "$variable.pressureThresholdBreached > 1", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:underPressureAction" } } ] } ] }, "onInput": { "events": [ { "eventName": "Overpressurized", "condition": "$input.PressureInput.sensorData.pressure > 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "3" } } ] }, { "eventName": "Pressure Okay", "condition": "$input.PressureInput.sensorData.pressure <= 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached - 1" } } ] } ], "transitionEvents": [ { "eventName": "BackToNormal", "condition": "$input.PressureInput.sensorData.pressure <= 70 && $variable.pressureThresholdBreached <= 1", "nextState": "Normal" } ] }, "onExit": { "events": [ { "eventName": "Normal Pressure Restored", "condition": "true", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:pressureClearedAction" } } ] } ] } } ], "initialStateName": "Normal" }, "key": "motorid", "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole" }Output:
{ "detectorModelConfiguration": { "status": "ACTIVATING", "lastUpdateTime": 1560796816.077, "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "creationTime": 1560796816.077, "detectorModelArn": "arn:aws:iotevents:us-west-2:123456789012:detectorModel/motorDetectorModel", "key": "motorid", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" } }Untuk informasi selengkapnya, lihat CreateDetectorModeldi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat CreateDetectorModel
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakancreate-input.
- AWS CLI
-
Untuk membuat masukan
create-inputContoh berikut menciptakan masukan.aws iotevents create-input \ --cli-input-jsonfile://pressureInput.jsonIsi dari
pressureInput.json:{ "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor", "inputDefinition": { "attributes": [ { "jsonPath": "sensorData.pressure" }, { "jsonPath": "motorid" } ] } }Output:
{ "inputConfiguration": { "status": "ACTIVE", "inputArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "lastUpdateTime": 1560795312.542, "creationTime": 1560795312.542, "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor" } }Untuk informasi selengkapnya, lihat CreateInputdi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat CreateInput
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakandelete-detector-model.
- AWS CLI
-
Untuk menghapus model detektor
delete-detector-modelContoh berikut menghapus model detektor yang ditentukan. Setiap contoh aktif dari model detektor juga dihapus.aws iotevents delete-detector-model \ --detector-model-namemotorDetectorModelPerintah ini tidak menghasilkan output.
Untuk informasi selengkapnya, lihat DeleteDetectorModeldi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat DeleteDetectorModel
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakandelete-input.
- AWS CLI
-
Untuk menghapus masukan
delete-inputContoh berikut menghapus input yang ditentukan.aws iotevents delete-input \ --input-namePressureInputPerintah ini tidak menghasilkan output.
Untuk informasi selengkapnya, lihat DeleteInputdi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat DeleteInput
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakandescribe-detector-model.
- AWS CLI
-
Untuk mendapatkan informasi tentang model detektor
describe-detector-modelContoh berikut menampilkan rincian untuk model detektor yang ditentukan. Karenaversionparameter tidak ditentukan, informasi tentang versi terbaru dikembalikan.aws iotevents describe-detector-model \ --detector-model-namemotorDetectorModelOutput:
{ "detectorModel": { "detectorModelConfiguration": { "status": "ACTIVE", "lastUpdateTime": 1560796816.077, "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "creationTime": 1560796816.077, "detectorModelArn": "arn:aws:iotevents:us-west-2:123456789012:detectorModel/motorDetectorModel", "key": "motorid", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" }, "detectorModelDefinition": { "states": [ { "onInput": { "transitionEvents": [ { "eventName": "Overpressurized", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached + 3" } } ], "condition": "$input.PressureInput.sensorData.pressure > 70", "nextState": "Dangerous" } ], "events": [] }, "stateName": "Normal", "onEnter": { "events": [ { "eventName": "init", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "0" } } ], "condition": "true" } ] }, "onExit": { "events": [] } }, { "onInput": { "transitionEvents": [ { "eventName": "BackToNormal", "actions": [], "condition": "$input.PressureInput.sensorData.pressure <= 70 && $variable.pressureThresholdBreached <= 1", "nextState": "Normal" } ], "events": [ { "eventName": "Overpressurized", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "3" } } ], "condition": "$input.PressureInput.sensorData.pressure > 70" }, { "eventName": "Pressure Okay", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached - 1" } } ], "condition": "$input.PressureInput.sensorData.pressure <= 70" } ] }, "stateName": "Dangerous", "onEnter": { "events": [ { "eventName": "Pressure Threshold Breached", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:underPressureAction" } } ], "condition": "$variable.pressureThresholdBreached > 1" } ] }, "onExit": { "events": [ { "eventName": "Normal Pressure Restored", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:pressureClearedAction" } } ], "condition": "true" } ] } } ], "initialStateName": "Normal" } } }Untuk informasi selengkapnya, lihat DescribeDetectorModeldi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat DescribeDetectorModel
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakandescribe-detector.
- AWS CLI
-
Untuk mendapatkan informasi tentang detektor (instance).
describe-detectorContoh berikut menampilkan rincian untuk detektor tertentu (contoh).aws iotevents-data describe-detector \ --detector-model-namemotorDetectorModel\ --key-value"Fulton-A32"Output:
{ "detector": { "lastUpdateTime": 1560797852.776, "creationTime": 1560797852.775, "state": { "variables": [ { "name": "pressureThresholdBreached", "value": "3" } ], "stateName": "Dangerous", "timers": [] }, "keyValue": "Fulton-A32", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" } }Untuk informasi selengkapnya, lihat DescribeDetectordi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat DescribeDetector
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakandescribe-input.
- AWS CLI
-
Untuk mendapatkan informasi tentang masukan
describe-inputContoh berikut menampilkan rincian untuk input yang ditentukan.aws iotevents describe-input \ --input-namePressureInputOutput:
{ "input": { "inputConfiguration": { "status": "ACTIVE", "inputArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "lastUpdateTime": 1560795312.542, "creationTime": 1560795312.542, "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor" }, "inputDefinition": { "attributes": [ { "jsonPath": "sensorData.pressure" }, { "jsonPath": "motorid" } ] } } }Untuk informasi selengkapnya, lihat DescribeInputdi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat DescribeInput
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakandescribe-logging-options.
- AWS CLI
-
Untuk mendapatkan informasi tentang pengaturan logging
describe-logging-optionsContoh berikut mengambil pengaturan saat ini dari opsi logging Peristiwa AWS IoT.aws iotevents describe-logging-optionsOutput:
{ "loggingOptions": { "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "enabled": false, "level": "ERROR" } }Untuk informasi selengkapnya, lihat DescribeLoggingOptionsdi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat DescribeLoggingOptions
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakanlist-detector-model-versions.
- AWS CLI
-
Untuk mendapatkan informasi tentang versi model detektor
list-detector-model-versionsContoh berikut Daftar semua versi model detektor. Hanya metadata yang terkait dengan setiap versi model detektor yang dikembalikan.aws iotevents list-detector-model-versions \ --detector-model-namemotorDetectorModelOutput:
{ "detectorModelVersionSummaries": [ { "status": "ACTIVE", "lastUpdateTime": 1560796816.077, "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "creationTime": 1560796816.077, "detectorModelArn": "arn:aws:iotevents:us-west-2:123456789012:detectorModel/motorDetectorModel", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" } ] }Untuk informasi selengkapnya, lihat ListDetectorModelVersionsdi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat ListDetectorModelVersions
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakanlist-detector-models.
- AWS CLI
-
Untuk mendapatkan daftar model detektor Anda
list-detector-modelsContoh berikut Daftar model detektor yang telah Anda buat. Hanya metadata yang terkait dengan setiap model detektor yang dikembalikan.aws iotevents list-detector-modelsOutput:
{ "detectorModelSummaries": [ { "detectorModelName": "motorDetectorModel", "creationTime": 1552072424.212 "detectorModelDescription": "Detect overpressure in a motor." } ] }Untuk informasi selengkapnya, lihat ListDetectorModelsdi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat ListDetectorModels
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakanlist-detectors.
- AWS CLI
-
Untuk mendapatkan daftar detektor untuk model detektor
list-detectorsContoh berikut mencantumkan detektor (contoh model detektor) di akun Anda.aws iotevents-data list-detectors \ --detector-model-namemotorDetectorModelOutput:
{ "detectorSummaries": [ { "lastUpdateTime": 1558129925.2, "creationTime": 1552073155.527, "state": { "stateName": "Normal" }, "keyValue": "Fulton-A32", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" } ] }Untuk informasi selengkapnya, lihat ListDetectorsdi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat ListDetectors
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakanlist-inputs.
- AWS CLI
-
Untuk membuat daftar input
list-inputsContoh berikut mencantumkan input yang telah Anda buat di akun Anda.aws iotevents list-inputsPerintah ini tidak menghasilkan output. Output:
{ { "status": "ACTIVE", "inputArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "lastUpdateTime": 1551742986.768, "creationTime": 1551742986.768, "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor" } }Untuk informasi selengkapnya, lihat ListInputsdi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat ListInputs
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakanlist-tags-for-resource.
- AWS CLI
-
Untuk mencantumkan tag yang ditetapkan ke sumber daya.
list-tags-for-resourceContoh berikut mencantumkan nama kunci tag dan nilai yang telah Anda tetapkan ke sumber daya.aws iotevents list-tags-for-resource \ --resource-arn"arn:aws:iotevents:us-west-2:123456789012:input/PressureInput"Output:
{ "tags": [ { "value": "motor", "key": "deviceType" } ] }Untuk informasi selengkapnya, lihat ListTagsForResourcedi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat ListTagsForResource
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakanput-logging-options.
- AWS CLI
-
Untuk mengatur opsi pencatatan
put-logging-optionsContoh berikut menetapkan atau memperbarui opsi logging Peristiwa AWS IoT. Jika Anda memperbarui nilailoggingOptions` field, it can take up to one minute for the change to take effect. Also, if you change the policy attached to the role you specified in the ``roleArnbidang apa pun (misalnya, untuk memperbaiki kebijakan yang tidak valid), diperlukan waktu hingga lima menit agar perubahan tersebut diterapkan.aws iotevents put-logging-options \ --cli-input-jsonfile://logging-options.jsonIsi dari
logging-options.json:{ "loggingOptions": { "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "level": "DEBUG", "enabled": true, "detectorDebugOptions": [ { "detectorModelName": "motorDetectorModel", "keyValue": "Fulton-A32" } ] } }Perintah ini tidak menghasilkan output.
Untuk informasi selengkapnya, lihat PutLoggingOptionsdi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat PutLoggingOptions
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakantag-resource.
- AWS CLI
-
Untuk menambahkan tag ke sumber daya
tag-resourceContoh berikut menambahkan atau memodifikasi (jika kuncideviceTypesudah ada) tag yang dilampirkan sumber daya yang ditentukan.aws iotevents tag-resource \ --cli-input-jsonfile://pressureInput.tag.jsonIsi dari
pressureInput.tag.json:{ "resourceArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "tags": [ { "key": "deviceType", "value": "motor" } ] }Perintah ini tidak menghasilkan output.
Untuk informasi selengkapnya, lihat TagResourcedi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat TagResource
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakanuntag-resource.
- AWS CLI
-
Untuk menghapus tag dari sumber daya
untag-resourceContoh berikut menghapus tag dengan nama kunci yang ditentukan dari sumber daya yang ditentukan.aws iotevents untag-resource \ --resource-arnarn:aws:iotevents:us-west-2:123456789012:input/PressureInput\ --tagkeysdeviceTypePerintah ini tidak menghasilkan output.
Untuk informasi selengkapnya, lihat UntagResourcedi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat UntagResource
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakanupdate-detector-model.
- AWS CLI
-
Untuk memperbarui model detektor
update-detector-modelContoh berikut memperbarui model detektor yang ditentukan. Detektor (instance) yang dihasilkan oleh versi sebelumnya dihapus dan kemudian dibuat ulang saat input baru tiba.aws iotevents update-detector-model \ --cli-input-jsonfile://motorDetectorModel.update.jsonIsi dari
motorDetectorModel.update.json:{ "detectorModelName": "motorDetectorModel", "detectorModelDefinition": { "states": [ { "stateName": "Normal", "onEnter": { "events": [ { "eventName": "init", "condition": "true", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "0" } } ] } ] }, "onInput": { "transitionEvents": [ { "eventName": "Overpressurized", "condition": "$input.PressureInput.sensorData.pressure > 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached + 3" } } ], "nextState": "Dangerous" } ] } }, { "stateName": "Dangerous", "onEnter": { "events": [ { "eventName": "Pressure Threshold Breached", "condition": "$variable.pressureThresholdBreached > 1", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:underPressureAction" } } ] } ] }, "onInput": { "events": [ { "eventName": "Overpressurized", "condition": "$input.PressureInput.sensorData.pressure > 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "3" } } ] }, { "eventName": "Pressure Okay", "condition": "$input.PressureInput.sensorData.pressure <= 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached - 1" } } ] } ], "transitionEvents": [ { "eventName": "BackToNormal", "condition": "$input.PressureInput.sensorData.pressure <= 70 && $variable.pressureThresholdBreached <= 1", "nextState": "Normal" } ] }, "onExit": { "events": [ { "eventName": "Normal Pressure Restored", "condition": "true", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:pressureClearedAction" } } ] } ] } } ], "initialStateName": "Normal" }, "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole" }Output:
{ "detectorModelConfiguration": { "status": "ACTIVATING", "lastUpdateTime": 1560799387.719, "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "creationTime": 1560799387.719, "detectorModelArn": "arn:aws:iotevents:us-west-2:123456789012:detectorModel/motorDetectorModel", "key": "motorid", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "2" } }Untuk informasi selengkapnya, lihat UpdateDetectorModeldi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat UpdateDetectorModel
di Referensi AWS CLI Perintah.
-
Contoh kode berikut menunjukkan cara menggunakanupdate-input.
- AWS CLI
-
Untuk memperbarui masukan
update-inputContoh berikut memperbarui input yang ditentukan dengan deskripsi dan definisi baru.aws iotevents update-input \ --cli-input-jsonfile://pressureInput.jsonIsi dari
pressureInput.json:{ "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor", "inputDefinition": { "attributes": [ { "jsonPath": "sensorData.pressure" }, { "jsonPath": "motorid" } ] } }Output:
{ "inputConfiguration": { "status": "ACTIVE", "inputArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "lastUpdateTime": 1560795976.458, "creationTime": 1560795312.542, "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor" } }Untuk informasi selengkapnya, lihat UpdateInputdi Referensi API Acara AWS IoT.
-
Untuk detail API, lihat UpdateInput
di Referensi AWS CLI Perintah.
-