diff --git a/README.md b/README.md index 2a01c6e..554ee67 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,54 @@ -# gseps-app-mqtt-s3 +# 이 코드 템플릿은 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..e6b98cd --- /dev/null +++ b/framework.yaml @@ -0,0 +1,11 @@ +version: bwc/v2 # bwc 버전 정보입니다. +spec: + appName: gseps-app # 앱의 이름입니다. + runFile: main.py # 앱의 실행 파일입니다. + env: + bin: python3 # 앱을 실행할 바이너라 파일 종류입니다.(장비에 따라 다르므로 확인 후 정의해야 합니다.) + virtualEnv: mqtt-s3-env # 사용할 가상환경 이름입니다. + package: requirement.txt # 설치할 Python 패키지 정보 파일입니다.(기본 값은 requirement.txt 입니다.) +stackbase: + tagName: v1.0.1 # Stackbase(gitea)에 릴리즈 태그명 입니다. + repoName: gseps-app-mqtt-s3 # Stackbase(gitea)에 저장될 저장소 이릅니다. diff --git a/main.py b/main.py new file mode 100644 index 0000000..0c6bf32 --- /dev/null +++ b/main.py @@ -0,0 +1,45 @@ +import sdtcloudpubsub +import sdtclouds3 +import time +import uuid +import random + +# 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: + index = random.randint(0, 10) # float + if index < 5: + openValue = "open" + else: + openValue = "close" + + pngIndex = random.randint(0,2) + pngList = ["./test1.png", "./test2.png", "./test3.png"] + result = sdtcloudS3Client.uploadData(pngList[pngIndex], {"ContentType": "image/png"}) + + data = { + "temperature": random.uniform(0, 30), # float + "water_level": random.randint(0, 30), # int, + "open": openValue, # string + "image": result, + "data": { + "temperature": random.uniform(0, 30), # float + "water_level": random.randint(0, 30), # int + "open": openValue + "image": result + } + } + 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 diff --git a/test1.png b/test1.png new file mode 100644 index 0000000..5d1b9c8 Binary files /dev/null and b/test1.png differ diff --git a/test2.png b/test2.png new file mode 100644 index 0000000..d6effb7 Binary files /dev/null and b/test2.png differ diff --git a/test3.png b/test3.png new file mode 100644 index 0000000..efe7c4e Binary files /dev/null and b/test3.png differ 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