Commit message

This commit is contained in:
sdt.cloud 2024-03-14 22:06:35 +09:00
parent f8195729b8
commit a57b86b6f3
5 changed files with 149 additions and 2 deletions

View File

@ -1,3 +1,54 @@
# sdt-mqtt-app-version1 # 이 코드 템플릿은 SDT Cloud 환경에서 s3에 파일을 업로드하는 MQTT 메세지를 발행하는 코드입니다.
Your repository description # 패키지 설치
- 코드는 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://<s3_bucket>/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://<s3_bucket>/path/text.txt"
},
"timestamp": 12312311...
}
```

4
config.json Normal file
View File

@ -0,0 +1,4 @@
{
"KEY1": "VALUE1",
"KEY2": "VALUE2"
}

11
framework.yaml Normal file
View File

@ -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)에 저장될 저장소 이릅니다.

75
main.py Normal file
View File

@ -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()

6
requirement.txt Normal file
View File

@ -0,0 +1,6 @@
# Write package's name that need your app.
boto3
pyyaml
awsiotsdk
#sdtcloudpubsub
#sdtcloudps3