1
0
Fork 0
This commit is contained in:
YujinChu 2023-09-08 06:33:21 +00:00
commit 34d005656f
6 changed files with 361 additions and 0 deletions

336
AWX_collector.py Normal file
View File

@ -0,0 +1,336 @@
import threading
import time
import logging
import serial
import minimalmodbus
import serial.rs485
import subprocess
import json
import queue
import paho.mqtt.client as mqtt
'''
전달해야할 데이터
mes code | 상태 | 전압 | 전류 | 4pin (DO) | ER1 | ER2
tube 번호| 상태 | volt | curr | do | cnt | cnt
tube 번호: 1 - 128 , chamber / slot
- slot(n) - tube(n)
- 1-1 ~ 8-16 : 128개다
상태: 정상이냐, 초기화, 불량
ER1: 1초동안 판별, 판별할 동안 counting 예정 2V 이하라면 CNT UP
if) er: 10 되었음 (1 유지) -> 0으로 변환, ER2 cnt up
if) 2v 이상인 경우가 발생했다 -> 0으로 변환
ER2: OFF 횟수
'''
###############################################################################
# JSON FILE READ #
###############################################################################
with open("./connect_info.json","r") as f:
info = json.load(f)
###############################################################################
# MQTT Broker Setting #
###############################################################################
MQTT_TOPIC = info['mqtt']['topic']
MQTT_ID = info['mqtt']['id']
MQTT_PW = info['mqtt']['pw']
MQTT_HOST_IP = info['mqtt']['host_ip']
MQTT_PORT = info['mqtt']['port']
def publish_json_message(client, data):
topic = MQTT_TOPIC
client.publish(topic, payload=data)
client = mqtt.Client("python_pub")
client.username_pw_set(MQTT_ID, MQTT_PW)
client.connect(host=MQTT_HOST_IP,port=MQTT_PORT)
client.loop(2) # timeout = 2초
serial_dev1 = ""
serial_dev2 = ""
# chip0_lock = threading.Lock()
# chip1_lock = threading.Lock()
_serial_port1 = serial.Serial("/dev/ttyMAX1", 115200, parity= serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout = 1, rtscts=True)
_serial_port1.rs485_mode = serial.rs485.RS485Settings()
_serial_port2 = serial.Serial("/dev/ttyMAX0", 115200, parity= serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout = 1, rtscts=True)
_serial_port2.rs485_mode = serial.rs485.RS485Settings()
#_serial_port3 = serial.Serial("/dev/ttyMAX2", 115200, parity= serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout = 1, rtscts=True)
#_serial_port3.rs485_mode = serial.rs485.RS485Settings()
#_serial_port4 = serial.Serial("/dev/ttyMAX3", 115200, parity= serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout = 1, rtscts=True)
#_serial_port4.rs485_mode = serial.rs485.RS485Settings()
#data_queue = queue.Queue()
######################################### thread 함수 작성 ################################################
dict_1={}
def read_cihp0(e,port_num1,port_num2,id1,id2,id3,id4,id5,id6):
serial_dev1 = "/dev/ttyMAX1"
serial_dev2 = "/dev/ttyMAX0"
# lock = chip1_lock
#_serial_port3 = serial.Serial("/dev/ttyMAX2", 115200, parity= serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout = 1, rtscts=True)
#_serial_port3.rs485_mode = serial.rs485.RS485Settings()
#_serial_port4 = serial.Serial("/dev/ttyMAX3", 115200, parity= serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout = 1, rtscts=True)
#_serial_port4.rs485_mode = serial.rs485.RS485Settings()
port1_AI1 = minimalmodbus.Instrument(serial_dev1, id1, debug=False, close_port_after_each_call=False) # port name, slave address (in decimal)
port1_AI1.serial.baudrate = 115200 # baudrate
port1_DO = minimalmodbus.Instrument(serial_dev1, id2, debug=False, close_port_after_each_call=False) # port name, slave address (in decimal)
port1_DO.serial.baudrate = 115200 # baudrate
port1_AI2 = minimalmodbus.Instrument(serial_dev1, id3, debug=False, close_port_after_each_call=False) # port name, slave address (in decimal)
port1_AI2.serial.baudrate = 115200 # baudrate
port2_AI1 = minimalmodbus.Instrument(serial_dev2, id4, debug=False, close_port_after_each_call=False) # port name, slave address (in decimal)
port2_AI1.serial.baudrate = 115200 # baudrate
port2_DO = minimalmodbus.Instrument(serial_dev2, id5, debug=False, close_port_after_each_call=False) # port name, slave address (in decimal)
port2_DO.serial.baudrate = 115200 # baudrate
port2_AI2 = minimalmodbus.Instrument(serial_dev2, id6, debug=False, close_port_after_each_call=False) # port name, slave address (in decimal)
port2_AI2.serial.baudrate = 115200 # baudrate
global read_cnt_ch0
read_cnt_ch0 = 0
global suc_cnt_ai
global suc_cnt_do
global suc_cnt_ai2
global suc_cnt_do2
suc_cnt_ai = 0
suc_cnt_do = 0
suc_cnt_ai2 = 0
suc_cnt_do2 = 0
global lists
lists = []
while True:
e.wait()
#print("read thread get even")
e.clear()
read_cnt_ch0 = read_cnt_ch0 +1
#value list on -> do control data
#추후에 do power on 할 리스트는 어디선가 읽어서 와야 해요
#do1 do2 나눠야 해요
value_list_ON = [1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0]
# lock.acquire()
timenow = time.time()
'''
| AI1 | AI2 | DO1 | DO2 |
Success | 0 | 0 | x | x |
Success | 0 | 1 | x | x |
Success | 1 | 0 | x | x |
Success | 1 | 1 | x | x | => 유효한 데이터
최소 한개 이상 실패 했을
-> retry 해볼 것이냐
-> 데이터를 어떻게 것이냐
-> 이전 데이터가 있다면, 이전 데이터를 가지고 더미 데이터로 사용하는 방법 => 안하기로 했었음
'''
try:
ai1=port1_AI1.read_registers(0x32, 32)
suc_cnt_ai = suc_cnt_ai +1
except:
# pass
None
# print("Port3 AI1 Fail")
try:
#print("try")
port1_DO.write_bits(0, value_list_ON)
suc_cnt_do = suc_cnt_do +1
#print("do1")
except:
None
# print("Port3 DO Fail")
# try:
# ai2 = port3_AI2.read_registers(0x32, 32)
# except:
# print("Port3 AI2 Fail")
#print(ai1)
#print(ai2)
try:
global ai2
ai2=port2_AI1.read_registers(0x32, 32)
suc_cnt_ai2 = suc_cnt_ai2 +1
except:
pass
# print("Port4 AI1 Fail")
try:
do2 = port2_DO.write_bits(0, value_list_ON)
suc_cnt_do2 = suc_cnt_do2 +1
except:
pass
# print("Port4 DO Fail, ")
# try:
# ai2 = port4_AI2.read_registers(0x32, 32)
# except:
# print("Port4 AI2 Fail")
#print(ai1)
#print(ai2)
# lock.release()
'''
리스트를 생성, 10개를 잘라서 어떻게 해야 할ㅈ지 모르겠음
'''
##데이터 생성 할 때
# ai1, ai2
# -> ai1 [32] -> [curr, volt, curr, volt ...]
data_for_list = {
"slot" : 1,
"timestamp": int(timenow*1000),
"data":{
"current": ai1,
"volatge": ai2,
"do1": value_list_ON,
"err1": value_list_ON,
"err2": value_list_ON
#err1, 2
}
}
lists.append(data_for_list)
### 여기서 보냄
# 10번 시그널 받으면
#
# list mqtt send
# list clear
if read_cnt_ch0 % 10 == 0:
data = {
"modelCode":"test4",
"assetCode":"NQ-R04-TEST-003",
"dataType":"DATA",
"data": {
"chamber":1,
"list": lists
}
}
global dict_1
data_json_str = json.dumps(data, indent=0)
publish_json_message(client, data_json_str)
#print(data_json_str)
# time.sleep(1)
# PRINT("CHip1 read count:", read_cnt)
######################################### thread 함수 작성 ################################################
def event_trigger_thread():
print("inpuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuut\r\n")
if input("Enter to exit(): "):
e.set()
#exit()
return
def timer_event_loop(e):
cnt = 0
# global read_cnt
while True:
# print("event set, meanning : periodic sensing")
e.set()
time.sleep(0.1)
#cnt = cnt +1
#if cnt > 6000:
# print("chip0 read count:", read_cnt_ch0)
#print("\r\nchip1 read count:", read_cnt_ch0)
#print("\r\nai1:", suc_cnt_ai, "\r\nai2:", suc_cnt_ai2)
#print(ai2)
#break
#print(cnt)
# print(read_cnt)
exit()
# timer = threading.Timer(3, lambda: print("test event"))
# timer.start()
# timer.join()
# # timer = threading.Timer(3, lambda: e.set())
################################# 작성 중 #####################################
# def publish_json_message(client, data):
# topic = "devicedata/axr/nodeq1"
# client.publish(topic, payload=data)
# client = mqtt.Client("python_pub")
# client.username_pw_set("sdt", "251327")
# client.connect(host="13.209.39.139",port=32259)
# # get_value()
# data = json.dumps(dict_1)
# #publish_json_message(client, data)
# client.loop(2) # timeout = 2초
################################# 작성 중 ######################################
if __name__ == '__main__':
e = threading.Event()
read_data_chip0 = threading.Thread(target=read_cihp0,args=(e,1,2,1,2,3,5,6,7))
# read_data_chip1 = threading.Thread(target=read_cihp1,args=(e,2,3,9,10,11,13,14,15))
# read_data_ch3 = threading.Thread(target=read_thread,args=(e,3,9,10,11))
# read_data_ch4 = threading.Thread(target=read_thread,args=(e,4,13,14,15))
event_trigger = threading.Thread(target=event_trigger_thread)
event_thread = threading.Thread(target=timer_event_loop, args=(e,))
# timer = threading.Timer(3,lambda: e.set()) # 1 time
# read_data_ch1.start()
# read_data_ch2.start()
# read_data_chip1.start()
read_data_chip0.start()
# timer.start()
event_trigger.start()
event_thread.start()
# wait_and_timer.start()
# while True:
# input("Enter to trigger the event: ")
# exit():
# e.set()
# time.sleep(600

10
AWX_collector.service Normal file
View File

@ -0,0 +1,10 @@
[Unit]
Description=awx_collector
[Service]
ExecStart=/usr/bin/python3 /usr/local/sdt/app/appName/AWX_collector.py -config appName
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target

1
config.json Normal file
View File

@ -0,0 +1 @@
ss

7
install.sh Normal file
View File

@ -0,0 +1,7 @@
#!/bin/bash
pip install -r requirements.txt
sudo sed -i "s/appName/$1/g" /usr/local/sdt/app/$1/AWX_collector.service
sudo cp /usr/local/sdt/app/$1/AWX_collector.service /etc/systemd/system/$1.service
sudo systemctl start $1
sudo systemctl enable $1

6
install.sh~ Normal file
View File

@ -0,0 +1,6 @@
#!/bin/bash
sudo sed -i "s/appName/$1/g" /usr/local/sdt/app/$1/AWX_collector.service
sudo cp /usr/local/sdt/app/$1/AWX_collector.service /etc/systemd/system/$1.service
sudo systemctl start $1
sudo systemctl enable $1

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
### package