From 82e4b7a50645dfac9e32c37481e35fdf901e1c32 Mon Sep 17 00:00:00 2001 From: sujune Date: Tue, 5 Mar 2024 17:44:46 +0900 Subject: [PATCH] first commit --- README.md | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ config.json | 4 ++++ framework.yaml | 12 +++++++++++ main.py | 28 +++++++++++++++++++++++++ requirement.txt | 6 ++++++ text.txt | 1 + 6 files changed, 105 insertions(+) create mode 100644 README.md create mode 100644 config.json create mode 100644 framework.yaml create mode 100644 main.py create mode 100644 requirement.txt create mode 100644 text.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..c59c405 --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +# 이 코드 템플릿은 SDT Cloud 환경에서 s3에 파일을 업로드하고 MQTT 메세지를 발행하는 코드입니다. + +# 패키지 설치 +- 코드는 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..828b3fc --- /dev/null +++ b/framework.yaml @@ -0,0 +1,12 @@ +version: bwc/v2 # bwc 버전 정보입니다. +spec: + appName: mqtt-s3-sdt-sdk-template # 앱의 이름입니다. + runFile: main.py # 앱의 실행 파일입니다. + env: + bin: python3 # 앱을 실행할 바이너라 파일 종류입니다.(장비에 따라 다르므로 확인 후 정의해야 합니다.) + virtualEnv: mqtt-s3-env # 사용할 가상환경 이름입니다. + package: requirement.txt # 설치할 Python 패키지 정보 파일입니다.(기본 값은 requirement.txt 입니다.) +stackbase: + userName: sujune@sdt.inc # Stackbase(gitea) ID 입니다. + tagName: v1.0.0 # Stackbase(gitea)에 릴리즈 태그명 입니다. + repoName: mqtt-s3-sdt-sdk-template # Stackbase(gitea)에 저장될 저장소 이릅니다. diff --git a/main.py b/main.py new file mode 100644 index 0000000..8792d52 --- /dev/null +++ b/main.py @@ -0,0 +1,28 @@ +import sdtcloudpubsub +import sdtclouds3 +import time +import uuid + +# call sdtcloudpubsub +sdtcloudMqttClient = sdtcloudpubsub.sdtcloudpubsub() +mqttClient = sdtcloudMqttClient.setClient(f"device-app-{uuid.uuid1()}") # parameter is client ID(string) + +# call sdtclouds3 +sdtcloudS3Client = sdtclouds3.sdtclouds3() + + + +def runAction(): + while True: + uploadFile = "./text.txt" + result = sdtcloudS3Client.uploadData(uploadFile, "") + msg = { + "message": "Hello World", + "uploadFile": result + } + sdtcloudMqttClient.pubMessage(mqttClient, msg) + 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 diff --git a/text.txt b/text.txt new file mode 100644 index 0000000..557db03 --- /dev/null +++ b/text.txt @@ -0,0 +1 @@ +Hello World