From a57b86b6f35d235a3011c9352454e18d0e555c8b Mon Sep 17 00:00:00 2001 From: "sdt.cloud" Date: Thu, 14 Mar 2024 22:06:35 +0900 Subject: [PATCH] Commit message --- README.md | 55 ++++++++++++++++++++++++++++++++++-- config.json | 4 +++ framework.yaml | 11 ++++++++ main.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ requirement.txt | 6 ++++ 5 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 config.json create mode 100644 framework.yaml create mode 100644 main.py create mode 100644 requirement.txt diff --git a/README.md b/README.md index 2bb51d0..554ee67 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,54 @@ -# sdt-mqtt-app-version1 +# 이 코드 템플릿은 SDT Cloud 환경에서 s3에 파일을 업로드하는 MQTT 메세지를 발행하는 코드입니다. -Your repository description \ No newline at end of file +# 패키지 설치 +- 코드는 sdtclouds3, sdtcloudpubsub 패키지를 사용합니다. 아래 명령어로 패키지를 다운로드 해야합니다. +```bash +$ pip install sdtclouds3 sdtcloudpubsub +``` + +# 환경 셋팅 +- 코드를 실행하기 전에, 장비에 sdtcloud 로그인 작업을 수행해야 합니다. +```bash +sudo bwc-cli login +``` + +# 코드 작성 +## s3 코드 작성 +- 코드는 runAction 함수에서 동작하고자 하는 기능을 작성합니다. +- uploadFile 변수는 s3에 업로드할 파일의 위치입니다. 반드시 파일의 위치와 파일명을 함께 작성해야 합니다. +```bash +uploadFile = "filepath/text.txt" +``` +- 업로드가 완료되면 파일의 URL 값이 반환됩니다. +```bash +result = sdtcloudClient.uploadData(uploadFile) +print(result) +---------- + https:///path/text.txt +``` +- 만약 업로드하는 파일이 PNG 파일이며, URL를 클릭했을 때 이미지가 바로 보이도록 설정하고 싶다면 다음과 같이 수정합니다. +```bash +result = sdtcloudClient.uploadData(uploadFile, {“ContentType”: “image/png”}) +``` +- 이외에 여러 옵션이 있으므로 [참고 페이지](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html)를 클릭하세요. + + +## mqtt 코드 작성 +- 다음 변수로 메세지를 발행하는 코드를 작성하면... +```bash +msg = { + "message": "Hello World", + "uploadFile": result +} +``` +- 실제로 발행되는 메세지은 다음과 같습니다. +```bash +msg = { + "data": { + "message": "Hello World", + "uploadFile": "https:///path/text.txt" + }, + "timestamp": 12312311... +} + +``` \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..13eb577 --- /dev/null +++ b/config.json @@ -0,0 +1,4 @@ +{ + "KEY1": "VALUE1", + "KEY2": "VALUE2" +} \ No newline at end of file diff --git a/framework.yaml b/framework.yaml new file mode 100644 index 0000000..f03fd43 --- /dev/null +++ b/framework.yaml @@ -0,0 +1,11 @@ +version: bwc/v2 # bwc 버전 정보입니다. +spec: + appName: sdt-mqtt-app # 앱의 이름입니다. + runFile: main.py # 앱의 실행 파일입니다. + env: + bin: python3 # 앱을 실행할 바이너라 파일 종류입니다.(장비에 따라 다르므로 확인 후 정의해야 합니다.) + virtualEnv: cloud-venv # 사용할 가상환경 이름입니다. + package: requirement.txt # 설치할 Python 패키지 정보 파일입니다.(기본 값은 requirement.txt 입니다.) +stackbase: + tagName: v1.0.0 # Stackbase(gitea)에 릴리즈 태그명 입니다. + repoName: sdt-mqtt-app-version1 # Stackbase(gitea)에 저장될 저장소 이릅니다. diff --git a/main.py b/main.py new file mode 100644 index 0000000..85099d2 --- /dev/null +++ b/main.py @@ -0,0 +1,75 @@ +import sdtcloudpubsub +import time +import uuid +import random + +# call sdtcloudpubsub +sdtcloudMqttClient = sdtcloudpubsub.sdtcloudpubsub() +mqttClient = sdtcloudMqttClient.setClient(f"device-app-{uuid.uuid1()}") # parameter is client ID(string) + +def runAction(): + while True: + index = random.randint(0, 10) # float + if index < 5: + openValue = "ture" + else: + openValue = "false" + + timedata = int(time.time()*1000) + data = { + "rssi": { + "value": random.uniform(0, 30), # float, + "timestamp": timedata + }, + "gas_o2": { + "value": random.uniform(0, 30), # float, + "timestamp": timedata + }, + "gas_h2s": { + "value": random.uniform(0, 30), # float, + "timestamp": timedata + }, + "rsrp": { + "value": random.uniform(0, 30), # float, + "timestamp": timedata + }, + "battery": { + "value": random.uniform(0, 30), # float, + "timestamp": timedata + }, + "sinr": { + "value": random.uniform(0, 30), # float, + "timestamp": timedata + }, + "rsrq": { + "value": random.uniform(0, 30), # float, + "timestamp": timedata + }, + "temperature": { + "value": random.uniform(0, 30), # float, + "timestamp": timedata + }, + "ph": { + "value": random.uniform(0, 30), # float, + "timestamp": timedata + }, + "humidity": { + "value": random.uniform(0, 30), # float, + "timestamp": timedata + }, + "water_level": { + "value": random.uniform(0, 30), # float, + "timestamp": timedata + }, + "open": { + "value": openValue, + "timestamp": timedata + } + } + + sdtcloudMqttClient.pubMessage(mqttClient, data) + time.sleep(10) + +if __name__ == "__main__": + runAction() + diff --git a/requirement.txt b/requirement.txt new file mode 100644 index 0000000..5d5d04b --- /dev/null +++ b/requirement.txt @@ -0,0 +1,6 @@ +# Write package's name that need your app. +boto3 +pyyaml +awsiotsdk +#sdtcloudpubsub +#sdtcloudps3