Compare commits
26 Commits
Author | SHA1 | Date |
---|---|---|
support.sampyo | 61e7c3d4b7 | |
support.sampyo | 5297824869 | |
support.sampyo | 50a6bf2373 | |
support.sampyo | 5c1d08ee98 | |
support.sampyo | 95921c49b8 | |
support.sampyo | 44c6a224c2 | |
support.sampyo | 797816a6f5 | |
support.sampyo | 5c41e0664d | |
support.sampyo | f54f9e7fb5 | |
support.sampyo | ad62a2f89e | |
support.sampyo | c566b4070e | |
support.sampyo | 4239597f8a | |
support.sampyo | a47d7c2bd9 | |
support.sampyo | 1a6344fccf | |
support.sampyo | a493257c17 | |
support.sampyo | 696cc6f0e8 | |
support.sampyo | 02a44e8478 | |
support.sampyo | 3fdb92ae4d | |
support.sampyo | ddc05f15ee | |
support.sampyo | 944013c835 | |
support.sampyo | 8efbc66164 | |
support.sampyo | d18653ef71 | |
support.sampyo | 4328027519 | |
support.sampyo | e5d745c4e1 | |
support.sampyo | d1a2dd1027 | |
support.sampyo | c1f26f9340 |
Binary file not shown.
56
control.json
56
control.json
|
@ -1,33 +1,49 @@
|
||||||
{
|
{
|
||||||
"device": {
|
"device": {
|
||||||
"setzero": {
|
"enter": {
|
||||||
"action": "Off"
|
"action": "Off"
|
||||||
},
|
},
|
||||||
|
"main": {
|
||||||
|
"action": "Off",
|
||||||
|
"duration": 4
|
||||||
|
},
|
||||||
"measure": {
|
"measure": {
|
||||||
"action": "On",
|
"action": "On"
|
||||||
"duration": 20
|
|
||||||
},
|
},
|
||||||
"mixed": {
|
"mixed": {
|
||||||
"action": "Off",
|
"action": "Off"
|
||||||
"duration": 7
|
|
||||||
},
|
|
||||||
"pure": {
|
|
||||||
"action": "Off",
|
|
||||||
"duration": 10,
|
|
||||||
"holding": 5
|
|
||||||
},
|
|
||||||
"enter":{
|
|
||||||
"action": "Off",
|
|
||||||
"duration": 10
|
|
||||||
},
|
|
||||||
"vent": {
|
|
||||||
"action": "Off",
|
|
||||||
"duration": 30,
|
|
||||||
"holding": 2
|
|
||||||
},
|
},
|
||||||
"motor": {
|
"motor": {
|
||||||
"action": "Off"
|
"action": "Off"
|
||||||
|
},
|
||||||
|
"pure": {
|
||||||
|
"action": "Off",
|
||||||
|
"duration": 4
|
||||||
|
},
|
||||||
|
"setzero": {
|
||||||
|
"action": "Off"
|
||||||
|
},
|
||||||
|
"vent": {
|
||||||
|
"action": "Off"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"type": "manual"
|
"maintenance": {
|
||||||
|
"clean": {
|
||||||
|
"duration": 20,
|
||||||
|
"time": 25
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"type": "manual",
|
||||||
|
"working-time": {
|
||||||
|
"step0": 5,
|
||||||
|
"step1": 10,
|
||||||
|
"step2": 0.5,
|
||||||
|
"step3": 4,
|
||||||
|
"step4": 20,
|
||||||
|
"step5": 20,
|
||||||
|
"step6": 0.5,
|
||||||
|
"step7": 15,
|
||||||
|
"step8": 10,
|
||||||
|
"step9": 140
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -7,5 +7,5 @@ spec:
|
||||||
virtualEnv: base # 사용할 가상환경 이름입니다.
|
virtualEnv: base # 사용할 가상환경 이름입니다.
|
||||||
package: requirements.txt # 설치할 Python 패키지 정보 파일입니다.(기본 값은 requirement.txt 입니다.)
|
package: requirements.txt # 설치할 Python 패키지 정보 파일입니다.(기본 값은 requirement.txt 입니다.)
|
||||||
stackbase:
|
stackbase:
|
||||||
tagName: v0.0.25 # Stackbase(gitea)에 릴리즈 태그명 입니다.
|
tagName: v0.0.51 # Stackbase(gitea)에 릴리즈 태그명 입니다.
|
||||||
repoName: sampyo-dio # Stackbase(gitea)에 저장될 저장소 이릅니다.
|
repoName: sampyo-dio # Stackbase(gitea)에 저장될 저장소 이릅니다.
|
||||||
|
|
379
main.py
379
main.py
|
@ -1,6 +1,5 @@
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
import argparse
|
|
||||||
import sys, signal
|
import sys, signal
|
||||||
import gpiod
|
import gpiod
|
||||||
from pymodbus.client import ModbusTcpClient
|
from pymodbus.client import ModbusTcpClient
|
||||||
|
@ -10,6 +9,8 @@ import pytz
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import threading, socket
|
import threading, socket
|
||||||
import uuid
|
import uuid
|
||||||
|
import logging
|
||||||
|
from logging.handlers import RotatingFileHandler
|
||||||
|
|
||||||
def Motor(chip, status, action):
|
def Motor(chip, status, action):
|
||||||
if action == 'On':
|
if action == 'On':
|
||||||
|
@ -35,15 +36,101 @@ def Valve_MixedWater(chip, status, action):
|
||||||
|
|
||||||
chip.set_values(status)
|
chip.set_values(status)
|
||||||
|
|
||||||
def Valve_PureWater(chip, status, action):
|
def Valve_PureWater(chip, status, action, duration=7):
|
||||||
if action == 'On':
|
global pure_valve_status
|
||||||
status[3] = 1
|
|
||||||
else: # action == 'Off'
|
status[2] = 0
|
||||||
status[3] = 0
|
status[3] = 0
|
||||||
|
|
||||||
chip.set_values(status)
|
chip.set_values(status)
|
||||||
|
time.sleep(0.05)
|
||||||
|
|
||||||
|
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':
|
||||||
|
status[2] = 1
|
||||||
|
status[3] = 0
|
||||||
|
chip.set_values(status)
|
||||||
|
time.sleep(duration)
|
||||||
|
if duration >= 7:
|
||||||
|
pure_valve_status = 2
|
||||||
|
elif duration < 7:
|
||||||
|
pure_valve_status = 1
|
||||||
|
|
||||||
|
status[2] = 0
|
||||||
|
status[3] = 0
|
||||||
|
chip.set_values(status)
|
||||||
|
time.sleep(0.05)
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# chip.set_values(status)
|
||||||
|
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)
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
|
||||||
def Valve_EnterWater(chip, status, action):
|
|
||||||
if action == 'On':
|
if action == 'On':
|
||||||
status[4] = 1
|
status[4] = 1
|
||||||
else: # action == 'Off'
|
else: # action == 'Off'
|
||||||
|
@ -56,14 +143,15 @@ def Measure_Weight(client):
|
||||||
val = 0
|
val = 0
|
||||||
try:
|
try:
|
||||||
result = client.read_holding_registers(1, 1)
|
result = client.read_holding_registers(1, 1)
|
||||||
if result.iserror():
|
if not result:
|
||||||
print(f'Error: {result}')
|
print(f'Error: {result}')
|
||||||
else:
|
else:
|
||||||
val = result.registers[0]
|
val = result.registers[0]
|
||||||
val -= 1000
|
val -= 1000
|
||||||
val /= 1000
|
val /= 1000
|
||||||
#print(f'value: {val}')
|
# print(f'value: {val}')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
print(f'Measure_Weight Error: {e}')
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return float(val)
|
return float(val)
|
||||||
|
@ -74,115 +162,143 @@ def Calculate_Concentration(weight):
|
||||||
result = (float(weight) * volume_water * 128.5) - 126.11 # 1000 / 531 = 1.883239171
|
result = (float(weight) * volume_water * 128.5) - 126.11 # 1000 / 531 = 1.883239171
|
||||||
data['data']['concentration'] = result
|
data['data']['concentration'] = result
|
||||||
# print(f'{weight}, {result}')
|
# print(f'{weight}, {result}')
|
||||||
|
return result
|
||||||
|
|
||||||
def Set_Zero(client):
|
def Set_Zero(client):
|
||||||
client.write_coil(1, 1)
|
client.write_coil(1, 1)
|
||||||
|
|
||||||
def Command_Read():
|
def Command_Read():
|
||||||
global client
|
global client, main_valve_status, logger
|
||||||
|
|
||||||
with open('./control.json', 'r') as f:
|
with open('./control.json', 'r') as f:
|
||||||
cmd = json.load(f)
|
cmd = json.load(f)
|
||||||
|
|
||||||
if cmd['type'] == 'auto':
|
if cmd['type'] == 'auto':
|
||||||
|
main_duration = float(cmd['device']['main']['duration'])
|
||||||
|
pure_duration = float(cmd['device']['pure']['duration'])
|
||||||
|
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'])
|
||||||
|
|
||||||
Valve_Vent(chip=output_lines, status=status, action='Off')
|
|
||||||
Motor(chip=output_lines, status=status, action='Off')
|
|
||||||
|
|
||||||
mixed_duration = int(cmd['device']['mixed']['duration'])
|
# Step 0. Mesure init weight before starting the sequence
|
||||||
pure_duration = int(cmd['device']['pure']['duration'])
|
time.sleep(step0_duration)
|
||||||
pure_holding = int(cmd['device']['pure']['holding'])
|
|
||||||
vent_duration = int(cmd['device']['vent']['duration'])
|
|
||||||
vent_holding = int(cmd['device']['vent']['holding'])
|
|
||||||
measure_duration = int(cmd['device']['measure']['duration'])
|
|
||||||
enter_duration = int(cmd['device']['enter']['duration'])
|
|
||||||
|
|
||||||
time.sleep(7)
|
|
||||||
start = Measure_Weight(client=client)
|
start = Measure_Weight(client=client)
|
||||||
time.sleep(5)
|
|
||||||
|
|
||||||
# input mixed water
|
# Step 1. Vent pured water before input mixed water
|
||||||
Valve_MixedWater(chip=output_lines, status=status, action='On')
|
# Target valve status: [Motor: Off, Vent: On, Pure: Off, Enter: On, Main: On]
|
||||||
time.sleep(0.5)
|
# Valve_EnterWater(chip=output_lines, status=status, action='On')
|
||||||
Valve_EnterWater(chip=output_lines, status=status, action='On')
|
Valve_MainWater(chip=output_lines, status=status, action='On')
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
Valve_Vent(chip=output_lines, status=status, action='On')
|
Valve_Vent(chip=output_lines, status=status, action='On')
|
||||||
time.sleep(vent_holding)
|
time.sleep(0.5)
|
||||||
|
# Valve_MainWater(chip=output_lines, status=status, action='On', duration=main_duration)
|
||||||
|
Valve_EnterWater(chip=output_lines, status=status, action='On', duration=main_duration)
|
||||||
|
time.sleep(step1_duration)
|
||||||
|
|
||||||
|
# Step 2. Empty the remaining pure water
|
||||||
|
# Target valve status: [Motor: Off, Vent: On, Pure: Off, Enter: Off, Main: On]
|
||||||
|
Valve_EnterWater(chip=output_lines, status=status, action='Off')
|
||||||
|
time.sleep(step2_duration)
|
||||||
|
|
||||||
|
# Step 3. Input the mixed water
|
||||||
|
# Target valve status: [Motor: Off, Vent: Off, Pure: Off, Enter: On, Main: On]
|
||||||
Valve_Vent(chip=output_lines, status=status, action='Off')
|
Valve_Vent(chip=output_lines, status=status, action='Off')
|
||||||
time.sleep(mixed_duration)
|
time.sleep(0.5)
|
||||||
Valve_MixedWater(chip=output_lines, status=status, action='Off')
|
# Valve_EnterWater(chip=output_lines, status=status, action='On')
|
||||||
|
Valve_EnterWater(chip=output_lines, status=status, action='On', duration=main_duration)
|
||||||
|
time.sleep(step3_duration)
|
||||||
|
|
||||||
|
# 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')
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
Valve_EnterWater(chip=output_lines, status=status, action='Off')
|
Valve_EnterWater(chip=output_lines, status=status, action='Off')
|
||||||
time.sleep(measure_duration)
|
time.sleep(step4_duration)
|
||||||
|
|
||||||
|
|
||||||
# measure weight
|
|
||||||
end = Measure_Weight(client=client)
|
end = Measure_Weight(client=client)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
Calculate_Concentration(weight=(float(end)-float(start)))
|
res = Calculate_Concentration(weight=(float(end)-float(start)))
|
||||||
|
|
||||||
# # vent mixed water
|
logger.debug(f'[auto] weight: {end - start} concentration: {res:.3f}')
|
||||||
# Valve_Vent(chip=output_lines, status=status, action='On')
|
|
||||||
# time.sleep(0.5)
|
|
||||||
# Motor(chip=output_lines, status=status, action='On')
|
|
||||||
# time.sleep(vent_duration)
|
|
||||||
# Motor(chip=output_lines, status=status, action='Off')
|
|
||||||
# time.sleep(0.5)
|
|
||||||
# Valve_Vent(chip=output_lines, status=status, action='Off')
|
|
||||||
# time.sleep(0.5)
|
|
||||||
|
|
||||||
# # input pure water
|
# Step 5. Drain the mixed water and add pure water.
|
||||||
# Valve_PureWater(chip=output_lines, status=status, action='On')
|
# Target valve status: [Motor: Off, Vent: On, Pure: On, Enter: On, Main: Off]
|
||||||
# time.sleep(pure_duration)
|
Valve_Vent(chip=output_lines, status=status, action='On')
|
||||||
# Valve_PureWater(chip=output_lines, status=status, action='Off')
|
|
||||||
# time.sleep(0.5)
|
|
||||||
|
|
||||||
# # vent pure water
|
|
||||||
# Valve_Vent(chip=output_lines, status=status, action='On')
|
|
||||||
# time.sleep(0.5)
|
|
||||||
# Motor(chip=output_lines, status=status, action='On')
|
|
||||||
# time.sleep(vent_duration)
|
|
||||||
# Motor(chip=output_lines, status=status, action='Off')
|
|
||||||
# time.sleep(0.5)
|
|
||||||
# Valve_Vent(chip=output_lines, status=status, action='Off')
|
|
||||||
# time.sleep(1)
|
|
||||||
|
|
||||||
# Vent and Clear sequence
|
|
||||||
# 1) Motor On
|
|
||||||
Motor(chip=output_lines, status=status, action='On')
|
|
||||||
time.sleep(0.5)
|
|
||||||
|
|
||||||
# 2) Input Pure Water
|
|
||||||
Valve_PureWater(chip=output_lines, status=status, action='On')
|
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
Valve_EnterWater(chip=output_lines, status=status, action='On')
|
Valve_EnterWater(chip=output_lines, status=status, action='On')
|
||||||
time.sleep(vent_holding)
|
|
||||||
|
|
||||||
# 3) Open Vent
|
|
||||||
Valve_Vent(chip=output_lines, status=status, action='On')
|
|
||||||
time.sleep(pure_duration)
|
|
||||||
|
|
||||||
Valve_PureWater(chip=output_lines, status=status, action='Off')
|
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
|
# Valve_PureWater(chip=output_lines, status=status, action='On', duration=pure_duration)
|
||||||
|
Valve_PureWater(chip=output_lines, status=status, action='On')
|
||||||
|
time.sleep(step5_duration)
|
||||||
|
|
||||||
|
# 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')
|
Valve_EnterWater(chip=output_lines, status=status, action='Off')
|
||||||
|
time.sleep(step6_duration)
|
||||||
|
|
||||||
# 4) Wait until empty
|
# Step 7. Input pure water and clean
|
||||||
time.sleep(vent_duration)
|
# Target valve status: [Motor: On, Vent: Off, Pure: On, Enter: On, Main: Off]
|
||||||
|
|
||||||
# 5) Motor Off and Vent close
|
|
||||||
Motor(chip=output_lines, status=status, action='Off')
|
|
||||||
time.sleep(0.5)
|
|
||||||
Valve_Vent(chip=output_lines, status=status, action='Off')
|
Valve_Vent(chip=output_lines, status=status, action='Off')
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
|
Valve_EnterWater(chip=output_lines, status=status, action='On')
|
||||||
|
time.sleep(0.5)
|
||||||
|
Motor(chip=output_lines, status=status, action='On')
|
||||||
|
time.sleep(step7_duration)
|
||||||
|
|
||||||
|
# Step 8. Drain pure Water
|
||||||
|
# Target valve status: [Motor: On, Vent: On, Pure: Off, Enter: Off, Main: Off]
|
||||||
|
Valve_Vent(chip=output_lines, status=status, action='On')
|
||||||
|
time.sleep(0.5)
|
||||||
|
Valve_EnterWater(chip=output_lines, status=status, action='Off')
|
||||||
|
time.sleep(0.5)
|
||||||
|
Valve_PureWater(chip=output_lines, status=status, action='Off')
|
||||||
|
time.sleep(step8_duration)
|
||||||
|
|
||||||
|
# Step 9. Stop moter
|
||||||
|
# Target valve status: [Motor: Off, Vent: On, Pure: Off, Enter: Off, Main: Off]
|
||||||
|
Motor(chip=output_lines, status=status, action='Off')
|
||||||
|
time.sleep(step9_duration)
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
elif cmd['type'] == 'clean':
|
||||||
|
logger.debug(f"[clean] duration: {int(cmd['maintenance']['clean']['duration'])}")
|
||||||
|
clean_system()
|
||||||
|
time.sleep(3)
|
||||||
|
|
||||||
else: # cmd['type'] == 'manual'
|
else: # cmd['type'] == 'manual'
|
||||||
Motor(chip=output_lines, status=status, action=cmd['device']['motor']['action'])
|
Motor(chip=output_lines, status=status, action=cmd['device']['motor']['action'])
|
||||||
Valve_Vent(chip=output_lines, status=status, action=cmd['device']['vent']['action'])
|
Valve_Vent(chip=output_lines, status=status, action=cmd['device']['vent']['action'])
|
||||||
Valve_MixedWater(chip=output_lines, status=status, action=cmd['device']['mixed']['action'])
|
# Valve_MixedWater(chip=output_lines, status=status, action=cmd['device']['mixed']['action'])
|
||||||
|
# Valve_PureWater(chip=output_lines, status=status, action=cmd['device']['pure']['action'])
|
||||||
|
# Valve_EnterWater(chip=output_lines, status=status, action=cmd['device']['enter']['action'])
|
||||||
|
Valve_MainWater(chip=output_lines, status=status, action=cmd['device']['main']['action'])
|
||||||
|
|
||||||
|
if cmd['device']['pure']['duration'] == 0:
|
||||||
Valve_PureWater(chip=output_lines, status=status, action=cmd['device']['pure']['action'])
|
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'])
|
||||||
|
|
||||||
|
# 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'])
|
||||||
|
|
||||||
|
if cmd['device']['main']['duration'] == 0:
|
||||||
Valve_EnterWater(chip=output_lines, status=status, action=cmd['device']['enter']['action'])
|
Valve_EnterWater(chip=output_lines, status=status, action=cmd['device']['enter']['action'])
|
||||||
|
else:
|
||||||
|
Valve_EnterWater(chip=output_lines, status=status, action=cmd['device']['enter']['action'], duration=cmd['device']['main']['duration'])
|
||||||
|
|
||||||
|
|
||||||
if cmd['device']['measure']['action'] == 'On':
|
if cmd['device']['measure']['action'] == 'On':
|
||||||
result = Measure_Weight(client=client)
|
result = Measure_Weight(client=client)
|
||||||
|
@ -194,6 +310,37 @@ def Command_Read():
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
def clean_system():
|
||||||
|
global main_valve_status
|
||||||
|
with open('./control.json', 'r') as f:
|
||||||
|
cmd = json.load(f)
|
||||||
|
|
||||||
|
clean_duration = int(cmd['maintenance']['clean']['duration'])
|
||||||
|
|
||||||
|
if cmd['type'] == 'clean':
|
||||||
|
Valve_EnterWater(chip=output_lines, status=status, action='Off')
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
Valve_MainWater(chip=output_lines, status=status, action='On')
|
||||||
|
time.sleep(0.5)
|
||||||
|
Valve_PureWater(chip=output_lines, status=status, action='On')
|
||||||
|
time.sleep(clean_duration)
|
||||||
|
|
||||||
|
Valve_MainWater(chip=output_lines, status=status, action='Off')
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
Valve_EnterWater(chip=output_lines, status=status, action='On')
|
||||||
|
time.sleep(0.5)
|
||||||
|
Valve_Vent(chip=output_lines, status=status, action='On')
|
||||||
|
time.sleep(clean_duration)
|
||||||
|
|
||||||
|
Valve_PureWater(chip=output_lines, status=status, action='Off')
|
||||||
|
time.sleep(0.5)
|
||||||
|
Valve_Vent(chip=output_lines, status=status, action='Off')
|
||||||
|
time.sleep(0.5)
|
||||||
|
Valve_EnterWater(chip=output_lines, status=status, action='Off')
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
def runAction():
|
def runAction():
|
||||||
# Write the app's actions in the "runAction" function.
|
# Write the app's actions in the "runAction" function.
|
||||||
|
|
||||||
|
@ -204,7 +351,7 @@ def runAction():
|
||||||
# 1. device-app-test -> Good
|
# 1. device-app-test -> Good
|
||||||
# 2. device-app-light-app -> Good
|
# 2. device-app-light-app -> Good
|
||||||
# 3. device-test-app -> Bad
|
# 3. device-test-app -> Bad
|
||||||
global data, client
|
global data
|
||||||
sdtcloud = sdtcloudnodeqmqtt.sdtcloudnodeqmqtt()
|
sdtcloud = sdtcloudnodeqmqtt.sdtcloudnodeqmqtt()
|
||||||
mqttClient1 = sdtcloud.setClient(f"device-app-1{uuid.uuid1()}") # parameter is client ID(string)
|
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)
|
mqttClient2 = sdtcloud.setClient(f"device-app-2{uuid.uuid1()}") # parameter is client ID(string)
|
||||||
|
@ -218,8 +365,8 @@ def runAction():
|
||||||
# - Asset Code's variable: assetCode(string)
|
# - Asset Code's variable: assetCode(string)
|
||||||
# - You may need it to create a topic.
|
# - You may need it to create a topic.
|
||||||
|
|
||||||
|
|
||||||
cnt = 0
|
cnt = 0
|
||||||
|
clean_flag = 0
|
||||||
while True:
|
while True:
|
||||||
start = time.time()
|
start = time.time()
|
||||||
result = Command_Read()
|
result = Command_Read()
|
||||||
|
@ -233,12 +380,29 @@ def runAction():
|
||||||
|
|
||||||
end = time.time()
|
end = time.time()
|
||||||
|
|
||||||
|
try:
|
||||||
|
now = datetime.now(pytz.timezone('Asia/Seoul'))
|
||||||
|
time_str = now.strftime('%H')
|
||||||
|
time_int = int(time_str)
|
||||||
|
|
||||||
|
with open('./control.json', 'r') as f:
|
||||||
|
cmd = json.load(f)
|
||||||
|
|
||||||
|
if time_int == int(cmd['maintenance']['clean']['time']):
|
||||||
|
if clean_flag < 3:
|
||||||
|
clean_flag += 1
|
||||||
|
clean_system()
|
||||||
|
else:
|
||||||
|
clean_flag = 0
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
diff = end - start
|
diff = end - start
|
||||||
if diff < 3:
|
if diff < 3:
|
||||||
time.sleep(3 - diff)
|
time.sleep(3 - diff)
|
||||||
|
|
||||||
def handle_client(conn, ip, port):
|
def handle_client(conn, ip, port):
|
||||||
global data
|
global data, logger
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
recv = conn.recv(100)
|
recv = conn.recv(100)
|
||||||
|
@ -261,6 +425,8 @@ def handle_client(conn, ip, port):
|
||||||
data_weight = '{:.3f}'.format(h_weight)
|
data_weight = '{:.3f}'.format(h_weight)
|
||||||
data_concent = '{:.3f}'.format(h_concentration)
|
data_concent = '{:.3f}'.format(h_concentration)
|
||||||
|
|
||||||
|
logger.debug(f'TCP [R:Transfer data] weight: {data_weight}, concent: {data_concent}')
|
||||||
|
|
||||||
send_msg = 'STX' + time_str + '|' + data_weight + '|' + data_concent + 'ETX'
|
send_msg = 'STX' + time_str + '|' + data_weight + '|' + data_concent + 'ETX'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -278,6 +444,7 @@ def handle_client(conn, ip, port):
|
||||||
conn.sendall(err_msg.encode("utf8"))
|
conn.sendall(err_msg.encode("utf8"))
|
||||||
|
|
||||||
elif message[3] == 'S': # Start measurement
|
elif message[3] == 'S': # Start measurement
|
||||||
|
logger.debug(f'TCP [S:Start measurement]')
|
||||||
try:
|
try:
|
||||||
with open('./control.json', 'r') as f:
|
with open('./control.json', 'r') as f:
|
||||||
cmd = json.load(f)
|
cmd = json.load(f)
|
||||||
|
@ -293,7 +460,25 @@ def handle_client(conn, ip, port):
|
||||||
err_msg = 'STXERRORETX'
|
err_msg = 'STXERRORETX'
|
||||||
conn.sendall(err_msg.encode("utf8"))
|
conn.sendall(err_msg.encode("utf8"))
|
||||||
|
|
||||||
|
elif message[3] == 'C': # Clean sequence
|
||||||
|
logger.debug(f'TCP [C:Clean sequence]')
|
||||||
|
try:
|
||||||
|
with open('./control.json', 'r') as f:
|
||||||
|
cmd = json.load(f)
|
||||||
|
|
||||||
|
cmd['type'] = 'clean'
|
||||||
|
|
||||||
|
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"))
|
||||||
|
|
||||||
elif message[3] == 'T': # Stop measurement
|
elif message[3] == 'T': # Stop measurement
|
||||||
|
logger.debug(f'TCP [T:Stop measurement]')
|
||||||
try:
|
try:
|
||||||
with open('./control.json', 'r') as f:
|
with open('./control.json', 'r') as f:
|
||||||
cmd = json.load(f)
|
cmd = json.load(f)
|
||||||
|
@ -343,17 +528,25 @@ def start_server(addr, port):
|
||||||
|
|
||||||
soc.close()
|
soc.close()
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
def exit_handler(signum, frame):
|
def exit_handler(signum, frame):
|
||||||
Motor(chip=output_lines, status=status, action='Off')
|
Motor(chip=output_lines, status=status, action='Off')
|
||||||
Valve_Vent(chip=output_lines, status=status, action='Off')
|
Valve_Vent(chip=output_lines, status=status, action='Off')
|
||||||
Valve_MixedWater(chip=output_lines, status=status, action='Off')
|
# Valve_MixedWater(chip=output_lines, status=status, action='Off')
|
||||||
Valve_PureWater(chip=output_lines, status=status, action='Off')
|
Valve_PureWater(chip=output_lines, status=status, action='Off')
|
||||||
|
Valve_EnterWater(chip=output_lines, status=status, action='Off')
|
||||||
|
Valve_MainWater(chip=output_lines, status=status, action='Off')
|
||||||
|
|
||||||
client.close()
|
client.close()
|
||||||
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
# Set GPIO
|
||||||
output_chip = gpiod.chip('gpiochip11')
|
output_chip = gpiod.chip('gpiochip11')
|
||||||
config = gpiod.line_request()
|
config = gpiod.line_request()
|
||||||
config.consumer = 'output'
|
config.consumer = 'output'
|
||||||
|
@ -364,18 +557,40 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
status = [0, 0, 0, 0, 0, 0, 0, 0]
|
status = [0, 0, 0, 0, 0, 0, 0, 0]
|
||||||
|
|
||||||
|
# When forced to terminate
|
||||||
signal.signal(signal.SIGINT, exit_handler)
|
signal.signal(signal.SIGINT, exit_handler)
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
|
||||||
|
# Initialized valves
|
||||||
|
main_valve_status = 0
|
||||||
|
pure_valve_status = 0
|
||||||
|
|
||||||
|
Valve_MainWater(chip=output_lines, status=status, action='Off')
|
||||||
|
Valve_PureWater(chip=output_lines, status=status, action='Off')
|
||||||
|
|
||||||
|
# Read config file
|
||||||
with open('./config.json', encoding='UTF-8') as f:
|
with open('./config.json', encoding='UTF-8') as f:
|
||||||
jsonData = json.load(f)
|
jsonData = json.load(f)
|
||||||
|
|
||||||
|
# Set the weight of water in the chamber
|
||||||
volume_water = 1000.0 / float(jsonData['volume-water'])
|
volume_water = 1000.0 / float(jsonData['volume-water'])
|
||||||
|
|
||||||
|
# Set the IP address and port of NodeQ RS-232 module and activate TCP Client
|
||||||
modbus_addr = jsonData['modbus-server']['address']
|
modbus_addr = jsonData['modbus-server']['address']
|
||||||
modbus_port = jsonData['modbus-server']['port']
|
modbus_port = jsonData['modbus-server']['port']
|
||||||
|
|
||||||
client = ModbusTcpClient(modbus_addr, modbus_port)
|
client = ModbusTcpClient(modbus_addr, modbus_port)
|
||||||
|
|
||||||
|
# Define the default data format
|
||||||
data = {
|
data = {
|
||||||
"timestamp": 0,
|
"timestamp": 0,
|
||||||
"data":{
|
"data":{
|
||||||
|
|
Loading…
Reference in New Issue