sampyo-dio/main.py

612 lines
22 KiB
Python
Raw Normal View History

2024-05-23 01:32:45 +00:00
import json
import time
import sys, signal
import gpiod
from pymodbus.client import ModbusTcpClient
# import AWSIoTPythonSDK.MQTTLib as AWSIoTPyMQTT
2024-07-30 05:22:35 +00:00
import sdtcloudnodeqmqtt
2024-05-23 01:32:45 +00:00
import pytz
from datetime import datetime
import threading, socket
import uuid
2024-10-10 07:00:26 +00:00
import logging
from logging.handlers import RotatingFileHandler
2024-05-23 01:32:45 +00:00
def Motor(chip, status, action):
if action == 'On':
status[0] = 1
else: # action == 'Off'
status[0] = 0
chip.set_values(status)
def Valve_Vent(chip, status, action):
if action == 'On':
status[1] = 1
else: # action == 'Off'
status[1] = 0
chip.set_values(status)
def Valve_MixedWater(chip, status, action):
if action == 'On':
status[2] = 1
else: # action == 'Off'
status[2] = 0
chip.set_values(status)
2024-07-04 05:12:08 +00:00
def Valve_PureWater(chip, status, action, duration=7):
global pure_valve_status
status[2] = 0
status[3] = 0
chip.set_values(status)
time.sleep(0.05)
2024-07-04 06:14:16 +00:00
if pure_valve_status != 0 and action == 'Off':
status[2] = 0
status[3] = 1
chip.set_values(status)
time.sleep(7)
pure_valve_status = 0
elif pure_valve_status == 0 and action == 'On':
2024-07-04 05:12:08 +00:00
status[2] = 1
2024-05-23 01:32:45 +00:00
status[3] = 0
2024-07-04 05:12:08 +00:00
chip.set_values(status)
time.sleep(duration)
2024-07-04 06:14:16 +00:00
if duration >= 7:
pure_valve_status = 2
elif duration < 7:
pure_valve_status = 1
2024-07-04 05:12:08 +00:00
status[2] = 0
status[3] = 0
2024-05-23 01:32:45 +00:00
chip.set_values(status)
2024-07-04 05:12:08 +00:00
time.sleep(0.05)
2024-10-10 07:00:26 +00:00
# def Valve_EnterWater(chip, status, action):
def Valve_EnterWater(chip, status, action, duration=7):
# if action == 'On':
# status[4] = 1
# else: # action == 'Off'
# status[4] = 0
2024-05-23 05:42:56 +00:00
2024-10-10 07:00:26 +00:00
# chip.set_values(status)
2024-06-12 05:16:08 +00:00
global main_valve_status
2024-10-10 07:00:26 +00:00
2024-06-12 05:16:08 +00:00
status[5] = 0
status[6] = 0
chip.set_values(status)
time.sleep(0.05)
2024-07-04 06:14:16 +00:00
if main_valve_status != 0 and action == 'Off':
status[5] = 0
status[6] = 1
chip.set_values(status)
time.sleep(7)
main_valve_status = 0
elif main_valve_status == 0 and action == 'On':
2024-06-12 05:16:08 +00:00
status[5] = 1
status[6] = 0
chip.set_values(status)
time.sleep(duration)
2024-07-04 06:14:16 +00:00
if duration >= 7:
main_valve_status = 2
elif duration < 7:
main_valve_status = 1
2024-06-12 05:16:08 +00:00
status[5] = 0
status[6] = 0
chip.set_values(status)
time.sleep(0.05)
2024-10-10 07:00:26 +00:00
# def Valve_MainWater(chip, status, action, duration=7):
def Valve_MainWater(chip, status, action):
global main_valve_status
# status[5] = 0
# status[6] = 0
# chip.set_values(status)
# time.sleep(0.05)
# if main_valve_status != 0 and action == 'Off':
# status[5] = 0
# status[6] = 1
# chip.set_values(status)
# time.sleep(7)
# main_valve_status = 0
# elif main_valve_status == 0 and action == 'On':
# status[5] = 1
# status[6] = 0
# chip.set_values(status)
# time.sleep(duration)
# if duration >= 7:
# main_valve_status = 2
# elif duration < 7:
# main_valve_status = 1
# status[5] = 0
# status[6] = 0
# chip.set_values(status)
# time.sleep(0.05)
if action == 'On':
status[4] = 1
else: # action == 'Off'
status[4] = 0
chip.set_values(status)
2024-05-23 05:50:59 +00:00
def Measure_Weight(client):
2024-05-23 05:33:27 +00:00
# print('in')
2024-05-23 01:32:45 +00:00
val = 0
try:
result = client.read_holding_registers(1, 1)
2024-05-24 06:04:38 +00:00
if not result:
2024-05-23 01:32:45 +00:00
print(f'Error: {result}')
else:
val = result.registers[0]
val -= 1000
val /= 1000
2024-05-24 06:04:38 +00:00
# print(f'value: {val}')
2024-05-23 01:32:45 +00:00
except Exception as e:
2024-05-24 06:04:38 +00:00
print(f'Measure_Weight Error: {e}')
2024-05-23 01:32:45 +00:00
pass
2024-05-23 07:03:51 +00:00
return float(val)
2024-05-23 01:32:45 +00:00
def Calculate_Concentration(weight):
global data, volume_water
data['data']['weight'] = weight
result = (float(weight) * volume_water * 128.5) - 126.11 # 1000 / 531 = 1.883239171
data['data']['concentration'] = result
# print(f'{weight}, {result}')
2024-10-10 07:00:26 +00:00
return result
2024-05-23 01:32:45 +00:00
def Set_Zero(client):
client.write_coil(1, 1)
def Command_Read():
2024-10-10 07:00:26 +00:00
global client, main_valve_status, logger
2024-05-24 05:17:25 +00:00
2024-05-23 01:32:45 +00:00
with open('./control.json', 'r') as f:
cmd = json.load(f)
if cmd['type'] == 'auto':
2024-06-12 05:16:08 +00:00
main_duration = float(cmd['device']['main']['duration'])
2024-07-04 05:12:08 +00:00
pure_duration = float(cmd['device']['pure']['duration'])
2024-07-04 05:50:25 +00:00
step0_duration = float(cmd['working-time']['step0'])
step1_duration = float(cmd['working-time']['step1'])
step2_duration = float(cmd['working-time']['step2'])
step3_duration = float(cmd['working-time']['step3'])
step4_duration = float(cmd['working-time']['step4'])
step5_duration = float(cmd['working-time']['step5'])
step6_duration = float(cmd['working-time']['step6'])
step7_duration = float(cmd['working-time']['step7'])
step8_duration = float(cmd['working-time']['step8'])
step9_duration = float(cmd['working-time']['step9'])
2024-05-23 01:32:45 +00:00
2024-07-04 05:12:08 +00:00
# Step 0. Mesure init weight before starting the sequence
2024-07-04 05:50:25 +00:00
time.sleep(step0_duration)
2024-07-04 05:12:08 +00:00
start = Measure_Weight(client=client)
# Step 1. Vent pured water before input mixed water
# Target valve status: [Motor: Off, Vent: On, Pure: Off, Enter: On, Main: On]
2024-10-10 07:00:26 +00:00
# Valve_EnterWater(chip=output_lines, status=status, action='On')
Valve_MainWater(chip=output_lines, status=status, action='On')
2024-05-23 06:12:49 +00:00
time.sleep(0.5)
Valve_Vent(chip=output_lines, status=status, action='On')
2024-07-04 05:12:08 +00:00
time.sleep(0.5)
2024-10-10 07:00:26 +00:00
# Valve_MainWater(chip=output_lines, status=status, action='On', duration=main_duration)
Valve_EnterWater(chip=output_lines, status=status, action='On', duration=main_duration)
2024-07-04 05:50:25 +00:00
time.sleep(step1_duration)
2024-07-04 05:12:08 +00:00
# Step 2. Empty the remaining pure water
# Target valve status: [Motor: Off, Vent: On, Pure: Off, Enter: Off, Main: On]
2024-05-29 06:30:40 +00:00
Valve_EnterWater(chip=output_lines, status=status, action='Off')
2024-07-04 05:50:25 +00:00
time.sleep(step2_duration)
2024-05-29 06:30:40 +00:00
2024-07-04 05:12:08 +00:00
# Step 3. Input the mixed water
# Target valve status: [Motor: Off, Vent: Off, Pure: Off, Enter: On, Main: On]
2024-05-23 06:12:49 +00:00
Valve_Vent(chip=output_lines, status=status, action='Off')
2024-05-29 06:30:40 +00:00
time.sleep(0.5)
2024-10-10 07:00:26 +00:00
# Valve_EnterWater(chip=output_lines, status=status, action='On')
Valve_EnterWater(chip=output_lines, status=status, action='On', duration=main_duration)
2024-07-04 05:50:25 +00:00
time.sleep(step3_duration)
2024-05-29 06:30:40 +00:00
2024-07-04 05:12:08 +00:00
# Step 4. Mesure the weight
# Target valve status: [Motor: Off, Vent: Off, Pure: Off, Enter: Off, Main: Off]
Valve_MainWater(chip=output_lines, status=status, action='Off')
2024-10-10 07:53:25 +00:00
time.sleep(0.5)
Valve_EnterWater(chip=output_lines, status=status, action='Off')
2024-07-04 05:50:25 +00:00
time.sleep(step4_duration)
2024-10-10 07:53:25 +00:00
2024-05-23 01:32:45 +00:00
end = Measure_Weight(client=client)
time.sleep(1)
2024-10-10 07:00:26 +00:00
res = Calculate_Concentration(weight=(float(end)-float(start)))
logger.debug(f'[auto] weight: {end - start} concentration: {res:.3f}')
2024-05-23 01:32:45 +00:00
2024-07-04 05:12:08 +00:00
# Step 5. Drain the mixed water and add pure water.
# Target valve status: [Motor: Off, Vent: On, Pure: On, Enter: On, Main: Off]
Valve_Vent(chip=output_lines, status=status, action='On')
2024-05-23 05:33:27 +00:00
time.sleep(0.5)
2024-10-10 07:53:25 +00:00
Valve_EnterWater(chip=output_lines, status=status, action='On')
time.sleep(0.5)
2024-10-10 07:00:26 +00:00
# Valve_PureWater(chip=output_lines, status=status, action='On', duration=pure_duration)
Valve_PureWater(chip=output_lines, status=status, action='On')
2024-07-04 05:50:25 +00:00
time.sleep(step5_duration)
2024-07-04 05:12:08 +00:00
# Step 6. Drain mixed water
# Target valve status: [Motor: Off, Vent: On, Pure: On, Enter: Off, Main: Off]
Valve_EnterWater(chip=output_lines, status=status, action='Off')
2024-07-04 05:50:25 +00:00
time.sleep(step6_duration)
2024-05-29 06:15:49 +00:00
2024-07-04 05:12:08 +00:00
# Step 7. Input pure water and clean
# Target valve status: [Motor: On, Vent: Off, Pure: On, Enter: On, Main: Off]
2024-05-29 05:51:51 +00:00
Valve_Vent(chip=output_lines, status=status, action='Off')
2024-05-29 06:15:49 +00:00
time.sleep(0.5)
2024-10-10 07:53:25 +00:00
Valve_EnterWater(chip=output_lines, status=status, action='On')
time.sleep(0.5)
2024-07-04 05:12:08 +00:00
Motor(chip=output_lines, status=status, action='On')
2024-07-04 05:50:25 +00:00
time.sleep(step7_duration)
2024-07-04 05:12:08 +00:00
# Step 8. Drain pure Water
# Target valve status: [Motor: On, Vent: On, Pure: Off, Enter: Off, Main: Off]
2024-05-29 05:51:51 +00:00
Valve_Vent(chip=output_lines, status=status, action='On')
time.sleep(0.5)
2024-10-10 07:53:25 +00:00
Valve_EnterWater(chip=output_lines, status=status, action='Off')
time.sleep(0.5)
2024-07-04 05:12:08 +00:00
Valve_PureWater(chip=output_lines, status=status, action='Off')
2024-07-04 05:50:25 +00:00
time.sleep(step8_duration)
2024-05-23 01:32:45 +00:00
2024-07-04 05:12:08 +00:00
# Step 9. Stop moter
# Target valve status: [Motor: Off, Vent: On, Pure: Off, Enter: Off, Main: Off]
2024-05-23 01:32:45 +00:00
Motor(chip=output_lines, status=status, action='Off')
2024-07-04 05:50:25 +00:00
time.sleep(step9_duration)
2024-07-04 05:12:08 +00:00
2024-05-23 01:32:45 +00:00
return 1
2024-05-29 05:51:51 +00:00
elif cmd['type'] == 'clean':
2024-10-10 07:00:26 +00:00
logger.debug(f"[clean] duration: {int(cmd['maintenance']['clean']['duration'])}")
2024-05-29 05:51:51 +00:00
clean_system()
2024-06-12 05:16:08 +00:00
time.sleep(3)
2024-05-29 05:51:51 +00:00
2024-05-23 01:32:45 +00:00
else: # cmd['type'] == 'manual'
Motor(chip=output_lines, status=status, action=cmd['device']['motor']['action'])
Valve_Vent(chip=output_lines, status=status, action=cmd['device']['vent']['action'])
2024-07-04 05:26:04 +00:00
# Valve_MixedWater(chip=output_lines, status=status, action=cmd['device']['mixed']['action'])
# Valve_PureWater(chip=output_lines, status=status, action=cmd['device']['pure']['action'])
2024-10-10 07:00:26 +00:00
# Valve_EnterWater(chip=output_lines, status=status, action=cmd['device']['enter']['action'])
Valve_MainWater(chip=output_lines, status=status, action=cmd['device']['main']['action'])
2024-05-23 05:33:27 +00:00
2024-07-04 05:26:04 +00:00
if cmd['device']['pure']['duration'] == 0:
Valve_PureWater(chip=output_lines, status=status, action=cmd['device']['pure']['action'])
else:
Valve_PureWater(chip=output_lines, status=status, action=cmd['device']['pure']['action'], duration=cmd['device']['pure']['duration'])
2024-10-10 07:00:26 +00:00
# if cmd['device']['main']['duration'] == 0:
# Valve_MainWater(chip=output_lines, status=status, action=cmd['device']['main']['action'])
# else:
# Valve_MainWater(chip=output_lines, status=status, action=cmd['device']['main']['action'], duration=cmd['device']['main']['duration'])
2024-06-12 05:16:08 +00:00
if cmd['device']['main']['duration'] == 0:
2024-10-10 07:00:26 +00:00
Valve_EnterWater(chip=output_lines, status=status, action=cmd['device']['enter']['action'])
2024-06-12 05:16:08 +00:00
else:
2024-10-10 07:00:26 +00:00
Valve_EnterWater(chip=output_lines, status=status, action=cmd['device']['enter']['action'], duration=cmd['device']['main']['duration'])
2024-06-12 05:16:08 +00:00
2024-10-10 07:00:26 +00:00
2024-05-23 01:32:45 +00:00
if cmd['device']['measure']['action'] == 'On':
result = Measure_Weight(client=client)
Calculate_Concentration(result)
return 1
if cmd['device']['setzero']['action'] == 'On':
Set_Zero(client=client)
return 0
2024-05-29 05:51:51 +00:00
def clean_system():
2024-06-12 05:16:08 +00:00
global main_valve_status
2024-05-29 05:51:51 +00:00
with open('./control.json', 'r') as f:
cmd = json.load(f)
2024-05-29 08:09:07 +00:00
clean_duration = int(cmd['maintenance']['clean']['duration'])
2024-05-29 05:51:51 +00:00
if cmd['type'] == 'clean':
Valve_EnterWater(chip=output_lines, status=status, action='Off')
time.sleep(0.5)
2024-06-12 05:16:08 +00:00
Valve_MainWater(chip=output_lines, status=status, action='On')
time.sleep(0.5)
2024-05-29 08:11:32 +00:00
Valve_PureWater(chip=output_lines, status=status, action='On')
2024-05-29 05:51:51 +00:00
time.sleep(clean_duration)
2024-06-12 05:16:08 +00:00
Valve_MainWater(chip=output_lines, status=status, action='Off')
time.sleep(0.5)
2024-07-30 04:50:07 +00:00
2024-07-30 04:55:55 +00:00
Valve_EnterWater(chip=output_lines, status=status, action='On')
2024-07-30 04:50:07 +00:00
time.sleep(0.5)
2024-07-30 04:55:55 +00:00
Valve_Vent(chip=output_lines, status=status, action='On')
2024-07-30 04:50:07 +00:00
time.sleep(clean_duration)
2024-05-29 08:11:32 +00:00
Valve_PureWater(chip=output_lines, status=status, action='Off')
2024-05-29 05:51:51 +00:00
time.sleep(0.5)
2024-07-30 04:55:55 +00:00
Valve_Vent(chip=output_lines, status=status, action='Off')
2024-07-30 04:50:07 +00:00
time.sleep(0.5)
2024-07-30 04:55:55 +00:00
Valve_EnterWater(chip=output_lines, status=status, action='Off')
2024-07-30 04:50:07 +00:00
time.sleep(0.5)
2024-05-29 05:51:51 +00:00
2024-05-23 01:32:45 +00:00
def runAction():
# Write the app's actions in the "runAction" function.
# Connect MQTT Broker
# You have to rename client id. There are special rules.
# Client Name: "device-app-*"
# For Example
# 1. device-app-test -> Good
# 2. device-app-light-app -> Good
# 3. device-test-app -> Bad
2024-05-24 06:04:38 +00:00
global data
2024-07-30 05:22:35 +00:00
sdtcloud = sdtcloudnodeqmqtt.sdtcloudnodeqmqtt()
mqttClient1 = sdtcloud.setClient(f"device-app-1{uuid.uuid1()}") # parameter is client ID(string)
mqttClient2 = sdtcloud.setClient(f"device-app-2{uuid.uuid1()}") # parameter is client ID(string)
mqttClient3 = sdtcloud.setClient(f"device-app-3{uuid.uuid1()}") # parameter is client ID(string)
mqttClient4 = sdtcloud.setClient(f"device-app-4{uuid.uuid1()}") # parameter is client ID(string)
mqttClient5 = sdtcloud.setClient(f"device-app-5{uuid.uuid1()}") # parameter is client ID(string)
mqttlist = [mqttClient1, mqttClient2, mqttClient3, mqttClient4, mqttClient5]
2024-05-23 01:32:45 +00:00
# If you have config's value, please make config.json file.
# - Project Code's variable: projectCode(string)
# - Asset Code's variable: assetCode(string)
# - You may need it to create a topic.
cnt = 0
2024-05-29 05:51:51 +00:00
clean_flag = 0
2024-05-23 01:32:45 +00:00
while True:
start = time.time()
result = Command_Read()
if result:
data['timestamp'] = int(time.time() * 1000)
2024-07-30 05:22:35 +00:00
sdtcloud.pubMessage(mqttlist[cnt], data)
2024-05-23 01:32:45 +00:00
cnt += 1
if cnt == 5:
cnt = 0
end = time.time()
2024-05-29 05:51:51 +00:00
try:
now = datetime.now(pytz.timezone('Asia/Seoul'))
time_str = now.strftime('%H')
time_int = int(time_str)
2024-05-30 06:37:40 +00:00
with open('./control.json', 'r') as f:
cmd = json.load(f)
2024-05-29 05:51:51 +00:00
if time_int == int(cmd['maintenance']['clean']['time']):
if clean_flag < 3:
clean_flag += 1
clean_system()
else:
clean_flag = 0
except:
pass
2024-05-23 01:32:45 +00:00
diff = end - start
if diff < 3:
time.sleep(3 - diff)
def handle_client(conn, ip, port):
2024-10-10 07:00:26 +00:00
global data, logger
2024-05-23 01:32:45 +00:00
while True:
try:
recv = conn.recv(100)
if not recv:
# print(f"Connection with {addr} was reset. Waiting for new connection.")
break
message = recv.decode().strip()
if message[:3] != 'STX' or message[-3:] != 'ETX':
err_msg = 'STXERRORETX'
conn.sendall(err_msg.encode("utf8"))
else:
if message[3] == 'R': # Transfer data from SDT to Sampyo
now = datetime.now(pytz.timezone('Asia/Seoul'))
time_str = now.strftime('%Y%m%d%H%M%S')
h_weight = float(data['data']['weight'])
h_concentration = float(data['data']['concentration'])
data_weight = '{:.3f}'.format(h_weight)
data_concent = '{:.3f}'.format(h_concentration)
2024-10-10 07:00:26 +00:00
logger.debug(f'TCP [R:Transfer data] weight: {data_weight}, concent: {data_concent}')
2024-05-23 01:32:45 +00:00
send_msg = 'STX' + time_str + '|' + data_weight + '|' + data_concent + 'ETX'
try:
with open('./control.json', 'r') as f:
cmd = json.load(f)
cmd['device']['measure']['action'] = 'On'
with open('./control.json', 'w') as f:
json.dump(cmd, f, indent=4)
conn.sendall(send_msg.encode("utf8"))
except Exception as e:
err_msg = 'STXERRORETX'
conn.sendall(err_msg.encode("utf8"))
elif message[3] == 'S': # Start measurement
2024-10-10 07:00:26 +00:00
logger.debug(f'TCP [S:Start measurement]')
2024-05-23 01:32:45 +00:00
try:
with open('./control.json', 'r') as f:
cmd = json.load(f)
cmd['type'] = 'auto'
with open('./control.json', 'w') as f:
json.dump(cmd, f, indent=4)
send_msg = 'STXOKETX'
conn.sendall(send_msg.encode("utf8"))
except Exception as e:
err_msg = 'STXERRORETX'
conn.sendall(err_msg.encode("utf8"))
2024-05-29 05:51:51 +00:00
elif message[3] == 'C': # Clean sequence
2024-10-10 07:00:26 +00:00
logger.debug(f'TCP [C:Clean sequence]')
2024-05-29 05:51:51 +00:00
try:
with open('./control.json', 'r') as f:
cmd = json.load(f)
cmd['type'] = 'clean'
with open('./control.json', 'w') as f:
2024-05-31 01:41:55 +00:00
json.dump(cmd, f, indent=4)
send_msg = 'STXOKETX'
conn.sendall(send_msg.encode("utf8"))
2024-05-29 05:51:51 +00:00
except Exception as e:
err_msg = 'STXERRORETX'
conn.sendall(err_msg.encode("utf8"))
2024-05-23 01:32:45 +00:00
elif message[3] == 'T': # Stop measurement
2024-10-10 07:00:26 +00:00
logger.debug(f'TCP [T:Stop measurement]')
2024-05-23 01:32:45 +00:00
try:
with open('./control.json', 'r') as f:
cmd = json.load(f)
cmd['type'] = 'manual'
cmd['device']['measure']['action'] = 'Off'
with open('./control.json', 'w') as f:
json.dump(cmd, f, indent=4)
send_msg = 'STXOKETX'
conn.sendall(send_msg.encode("utf8"))
except Exception as e:
err_msg = 'STXERRORETX'
conn.sendall(err_msg.encode("utf8"))
else:
err_msg = 'STXERRORETX'
conn.sendall(err_msg.encode("utf8"))
except ConnectionResetError:
# print("Connection with " + ip + ":" + port + " was reset. Waiting for new connection.")
break
# print("Closing the connection")
def start_server(addr, port):
2024-05-24 05:17:25 +00:00
host = addr # "172.17.16.201"
2024-05-23 01:32:45 +00:00
port = port # 5000
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
soc.bind((host, port))
except:
sys.exit()
soc.listen(1) # Only one connection at a time.
while True:
conn, addr = soc.accept()
ip, port = str(addr[0]), str(addr[1])
print("Connected with " + ip + ":" + port)
client_handler = threading.Thread(target=handle_client, args=(conn, ip, port))
client_handler.start()
soc.close()
2024-10-10 07:00:26 +00:00
def seoul_time(*args):
utc_dt = datetime.now()
seoul_tz = pytz.timezone('Asia/Seoul')
return utc_dt.replace(tzinfo=pytz.utc).astimezone(seoul_tz).timetuple()
2024-05-23 01:32:45 +00:00
def exit_handler(signum, frame):
Motor(chip=output_lines, status=status, action='Off')
Valve_Vent(chip=output_lines, status=status, action='Off')
2024-07-04 05:45:11 +00:00
# Valve_MixedWater(chip=output_lines, status=status, action='Off')
2024-05-23 01:32:45 +00:00
Valve_PureWater(chip=output_lines, status=status, action='Off')
2024-05-24 06:04:38 +00:00
Valve_EnterWater(chip=output_lines, status=status, action='Off')
2024-06-12 05:16:08 +00:00
Valve_MainWater(chip=output_lines, status=status, action='Off')
2024-05-23 01:32:45 +00:00
client.close()
sys.exit(0)
if __name__ == "__main__":
2024-10-10 07:00:26 +00:00
# Set GPIO
2024-05-23 01:32:45 +00:00
output_chip = gpiod.chip('gpiochip11')
config = gpiod.line_request()
config.consumer = 'output'
config.request_type = gpiod.line_request.DIRECTION_OUTPUT
output_lines = output_chip.get_lines([0, 1, 2, 3, 4, 5, 6, 7])
output_lines.request(config, default_vals=[0, 0, 0, 0, 0, 0, 0, 0])
status = [0, 0, 0, 0, 0, 0, 0, 0]
2024-10-10 07:00:26 +00:00
# When forced to terminate
2024-05-23 01:32:45 +00:00
signal.signal(signal.SIGINT, exit_handler)
2024-10-10 07:00:26 +00:00
# Set the logger
logger = logging.getLogger('sampyo_dio')
logger.setLevel(logging.DEBUG)
handler = RotatingFileHandler('/home/root/working.log', maxBytes=1024*1024, backupCount=3)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
formatter.converter = seoul_time
handler.setFormatter(formatter)
logger.addHandler(handler)
2024-06-12 05:16:08 +00:00
2024-10-10 07:00:26 +00:00
# Initialized valves
2024-06-12 05:16:08 +00:00
main_valve_status = 0
2024-07-04 06:19:55 +00:00
pure_valve_status = 0
2024-07-04 05:12:08 +00:00
2024-06-12 05:16:08 +00:00
Valve_MainWater(chip=output_lines, status=status, action='Off')
2024-07-04 05:12:08 +00:00
Valve_PureWater(chip=output_lines, status=status, action='Off')
2024-06-12 05:16:08 +00:00
2024-10-10 07:00:26 +00:00
# Read config file
2024-05-23 01:32:45 +00:00
with open('./config.json', encoding='UTF-8') as f:
jsonData = json.load(f)
2024-10-10 07:00:26 +00:00
# Set the weight of water in the chamber
2024-05-23 01:32:45 +00:00
volume_water = 1000.0 / float(jsonData['volume-water'])
2024-10-10 07:00:26 +00:00
# Set the IP address and port of NodeQ RS-232 module and activate TCP Client
2024-05-23 01:32:45 +00:00
modbus_addr = jsonData['modbus-server']['address']
modbus_port = jsonData['modbus-server']['port']
client = ModbusTcpClient(modbus_addr, modbus_port)
2024-10-10 07:00:26 +00:00
# Define the default data format
2024-05-23 01:32:45 +00:00
data = {
"timestamp": 0,
"data":{
"weight": 0,
"concentration": 0
}
}
## Get ProjectCode and AssetCode
## Execution main funcion
operation_thread = threading.Thread(target=runAction, args=())
operation_thread.start()
tcp_addr = jsonData['tcp-server']['address']
tcp_port = jsonData['tcp-server']['port']
## Execution TCP/IP server
start_server(addr=tcp_addr, port=tcp_port)