From f9842fc05d59de23e4d9f514af512db1820122d9 Mon Sep 17 00:00:00 2001 From: ssung Date: Mon, 20 Nov 2023 11:32:48 +0900 Subject: [PATCH] first commit --- README.md | 75 ++++++++++++++++++++++++++++++++++ app-setting.sh | 14 +++++++ config1.json | 9 +++++ config2.json | 6 +++ install.sh | 9 +++++ main.py | 90 +++++++++++++++++++++++++++++++++++++++++ nodeq-port-test.service | 10 +++++ 7 files changed, 213 insertions(+) create mode 100644 README.md create mode 100755 app-setting.sh create mode 100644 config1.json create mode 100644 config2.json create mode 100755 install.sh create mode 100644 main.py create mode 100644 nodeq-port-test.service diff --git a/README.md b/README.md new file mode 100644 index 0000000..4c37f71 --- /dev/null +++ b/README.md @@ -0,0 +1,75 @@ +# SDTcloud App Format + +## SDTcloud 앱을 만들기 위한 앱 포맷입니다. +### 코드 구성 +```bash +sdtcloud-app-format +├── app-setting.sh +├── config1.json +├── config2.json +├── install.sh +├── main.py +├── README.md +└── sdt-cloud-app.service +``` +- 배포될 앱은 실행 파일, Config 파일, 앱 설치 shell script와 같이 필수 파일 있어야 합니다. +- app-setting.sh 파일은 앱 생성 기본 양식을 저장하고자 하는 이름의 앱으로 변경 시켜주는 스크립트 입니다. +- SDT cloud에서 배포 되는 앱은 리눅스의 Systemd에 의해서 관리됩니다. +- 앱을 배포하기 위해서는 실행 파일 이외에 .service 파일이 반드시 필요합니다. + +### 살행 코드 내용 +- SDT Cloud는 현재 Python과 Java 언어에 대해서 앱 배포가 가능합니다. +- 두 언어를 작성할 때, Config 파일을 읽을 위치를 코드 내에서 지정해야 합니다. +- SDT Cloud는 device에 아래 위치에 앱을 설치하고 관리합니다. +```bash +/usr/local/sdt/app +``` +- 즉, SDTcloud의 앱은 "/usr/local/sdt/app" 디렉토리에 보관되고 관리 되므로, "/usr/local/sdt/app/앱이름/config.json" 경로와 같이 config 파일을 읽도록 코드를 작성해야 합니다. +- 또한, 앱이름은 사용자가 배포할 때마다 앱의 이름이 변경될 수 있으므로 프로세스가 매개변수를 입력받아 동적으로 변경가능하도록 설계 해야 합니다. +```bash +## Python의 경우 +parser = argparse.ArgumentParser() +parser.add_argument('-app',help='') +args = parser.parse_args() + +with open(f'/usr/local/sdt/app/{args.app}/config.json', encoding='UTF-8') as f: + jsonData = json.load(f) +``` +- 따라서 python의 경우 앱을 실행할 때, 앱이름을 매개 변수로 입력 받아서 실행하도록 설계 되어야 합니다. +```bash +python3 main.py -app example-app +``` + +### 앱 생성 +- 배포하고자 하는 앱이 Python이며, 실행 파일은 main.py이고 config 파일이 config.json 이며, 이 앱의 이름을 example-app으로 앱을 저장할 것이라면, +- 앱을 저장하기 위해 다음 명령어로 셋팅해야 합니다. +```bash +$ mv config1.json config.json +$ vi config.json -> config 값 수정 +$ ./app-setting.sh example-app /usr/bin/python3 main.py +``` +- 3번 코드 라인 설명은 다음과 같습니다. + - example-app: 앱 이름 + - /usr/bin/python3: python3 bin 파일 위치 → 이 같은 경우는 정확한 bin 파일 경로로 작성해야 합니다. + - main.py: python3 실행 파일 +- 위 작업을 완료하면 앱이 SDT cloud를 통해 배포하기 위한 기본적인 앱 파일 구조가 완성됩니다. +- service 파일을 확인 하면 다음과 같습니다. + +```bash +$ vi example-app.service +[Unit] +Description=example-app + +[Service] +ExecStart=/usr/bin/python3 /usr/local/sdt/app/appName/main.py -app appName +Restart=always +RestartSec=10 + +[Install] +WantedBy=multi-user.target +``` +- 이후, StackBase(gitea)에 앱을 업로드 합니다. +- 업로드 하기전 아래 작업을 수행 후, gitea에 새 저장소로 추가합니다. +```bash +$ rm -rf .git +``` \ No newline at end of file diff --git a/app-setting.sh b/app-setting.sh new file mode 100755 index 0000000..d880b91 --- /dev/null +++ b/app-setting.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +if [ -z $1 ] || [ -z $2 ] || [ -z $3 ] ; then + echo "Please fill in app's name, bin path, file." + exit +fi + + + +mv sdt-cloud-app.service $1.service +sed -i "s/sdt-cloud-app/$1/g" ./install.sh +sed -i "s/sdt-cloud-app/$1/g" ./$1.service +sed -i "s%exePath%$2%g" ./$1.service +sed -i "s/exeFile/$3/g" ./$1.service \ No newline at end of file diff --git a/config1.json b/config1.json new file mode 100644 index 0000000..f77c7a2 --- /dev/null +++ b/config1.json @@ -0,0 +1,9 @@ +{ + "topic": "performance", + "id": "sdt", + "password": "11223344", + "host": { + "address": "192.168.100.200", + "hostname": "sdt" + } +} \ No newline at end of file diff --git a/config2.json b/config2.json new file mode 100644 index 0000000..4a478a0 --- /dev/null +++ b/config2.json @@ -0,0 +1,6 @@ +{ + "hw": "intel", + "cpu": 12, + "memory": 32, + "gpu": "2080TI" +} diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..5a8fe9c --- /dev/null +++ b/install.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +set -e + +sudo sed -i "s/appName/$1/g" /usr/local/sdt/app/$1/nodeq-port-test.service +sudo cp /usr/local/sdt/app/$1/nodeq-port-test.service /etc/systemd/system/$1.service +sudo systemctl start $1 +sudo systemctl enable $1 + diff --git a/main.py b/main.py new file mode 100644 index 0000000..7483272 --- /dev/null +++ b/main.py @@ -0,0 +1,90 @@ +import serial +import json +import pika +import time +import struct,fcntl + + +ser = serial.Serial("/dev/ttyMAX0", baudrate=9600, stopbits=1, bytesize=8, parity="N", timeout=1,rtscts=True) + +fd=ser.fileno() +serial_rs485=struct.pack('hhhhhhhh', 1, 1, 0, 0, 0, 0, 0, 0) +fcntl.ioctl(fd,0x542F,serial_rs485) + +address = 0x01 +function_code = 0x03 +up_reg_address = 0x00 +down_reg_addree = 0x00 +multi_reg = 0x00 +prime_reg = 0x02 +down_crc_code = 0xC4 +up_crc_code = 0x0B + +def circle(): + request = [address, function_code, up_reg_address, down_reg_addree, multi_reg, prime_reg, down_crc_code, up_crc_code] + packet = bytearray() + + for data in request: + packet.append(data) + + # print(packet) + response = ser.write(packet) + # print(response) + + result = ser.read(9) + # print(result) + # print(hex(result[0]),hex(result[1]),hex(result[2]),hex(result[3]),hex(result[4]),hex(result[5]),hex(result[6]),hex(result[7]),hex(result[8])) + + hum = (result[3]*256 +result[4]) + temp = (result[5]*256 + result[6]) + + f = open ('/sys/devices/virtual/thermal/thermal_zone0/temp', 'r') + cputemp = f.readline() + + f.close() + + Data = { + "NodeQ":{ + "Interface": "RS-485", + "CPU Temperature": int(cputemp)/1000, + }, + "componentId": "CP-SDT-btftm01", + "timestamp": int(time.time()*1000), + "data": { + "Temperature": int(temp)/10, + "Humidity": int(hum)/10 + } + } + + dict_1 = json.dumps(Data, indent=2) + print(" [x] Sent 'Data!' Datas are", dict_1) + + + + # connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.0.20')) + # channel = connection.channel() + + # channel.basic_publish(exchange='lab', + # routing_key='node.q', + # body = dict_1) + + + + # connection.close() + +while True: + circle() + print("\nStop : cntl + c\n") + time.sleep(5) + + + + # Data = { + # "componentId": "CP-SDT-btftm01", + # "timestamp": int(time.time()*1000), + # "data": { + # "Temperature": int(temp)/10, + # "Humidity": int(hum)/10, + # "CPU Temperature": int(cputemp)/1000 + # } + # } diff --git a/nodeq-port-test.service b/nodeq-port-test.service new file mode 100644 index 0000000..361785c --- /dev/null +++ b/nodeq-port-test.service @@ -0,0 +1,10 @@ +[Unit] +Description=nodeq-port-test + +[Service] +ExecStart=/usr/bin/python3 /usr/local/sdt/app/appName/main.py -app appName +Restart=always +RestartSec=10 + +[Install] +WantedBy=multi-user.target