GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — SDP_SCADA/SCADA.py at master · rancorjoy/SDP_SCADA

Expand file tree

/

SCADA.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
118 lines (96 loc) · 8.19 KB
/

SCADA.py

Copy path

File metadata and controls

118 lines (96 loc) · 8.19 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Import Needed Libraries
import os # Operating System Management
import time # Time Keeping and Sleep
import pathlib # Object-Oriented File Path Management
import json # JSON File Management
import shutil # Shell Utilities (High Level File Operations)
import threading # Multithreading for Commands Outside of Loop
import flask # Lightweight web server
import requests # For Command Line Interface Window
import serial # Serial Communications over USB<->USB
import serial.tools.list_ports # Serial communication tools for port detection
import queue # Adds queues that can be pushed or popped to from a thread
from dataclasses import dataclass # Allows current state to be passed as a struct to FLASK thread
# Import the scripts folder - scripts and utility methods
from scripts import data_path_utils
from scripts import dcs_dict_utils
from scripts import dcs_flash_utils
from scripts import flask_thread
from scripts import flash_thread
from scripts import serial_thread
from scripts import create_block_lib
from scripts import worker_thread_utils
from scripts import print_log
from scripts import sql_utils
from scripts.current_state import CurrentState
def main(): # Main Method - Program Entry Point
print_log.pL("System", "Event", "Starting SCADA", "System", True, None)
# Initialize Data Path and Sub-Paths
is_path = data_path_utils.init_data_path() # Initialize Data Path
path = data_path_utils.get_data_path() # Get Current Data Path
is_info = dcs_dict_utils.init_dcs_path(path) # Initialize DCS Info Path
is_sp = dcs_flash_utils.init_code_path(path) # Initialize DCS Code Path
is_bl = create_block_lib.initialize_block_lib() # Initialize Code Block Library in main path (persistent)
blk_lib = create_block_lib.open_block_lib() # Open a copy of the block library
serial_queue = queue.Queue() # Create an event queue for the serial monitoring thread
flash_queue = queue.Queue() # Create an event queue for flashing DCS controllers
flash_lock = threading.Lock() # Create a lock for the flash thread
current_dcs = {} # Dictionary for current dcs connections
worker_threads = {} # Dictionary for current worker threads (monitor connections)
sql_queue = queue.Queue() # Event queue to add to SQL database
current_dict = {} # Dictionary for DCS details of each controller (current/unsaved versions)
current_dict = dcs_dict_utils.init_current_dict(path) # Get the (starting) current dictionary from previously learned devices
current_dict_lock = threading.Lock() # Prevents errors from read/write collisions in the FLASK server
dcs_dict_utils.load_progs(current_dict, current_dict_lock, path) # Load all saved programs (empty controllers)
devices = {} # Array for current devices (USB connections)
previous_devices = {} # Array for previous devices (USB connections)
if is_path and is_bl: # If the data path is initialized and code blocks are initialized (if not, nothing can/should run)
# Create a thread that runs serial_thread.serial_loop
# Pass the thread the event queue so results can be used in main thread
monitor_thread = threading.Thread(target=serial_thread.serial_loop, args=(serial_queue,is_info))
monitor_thread.daemon = True # Dies when main program dies
monitor_thread.start() # Start the new thread
# Data structure being passed to the FLASK server with current state of system (pass by reference)
thisState = CurrentState(current_dcs,current_dict, current_dict_lock, flash_queue, flash_lock, blk_lib, worker_threads)
# Create a thread that runs flask_thread.flask_loop
server_thread = threading.Thread(target=flask_thread.flask_loop, args=(thisState,))
server_thread.daemon = True # Dies when main program dies
server_thread.start() # Start the new thread
# Create a thread that runs flash_thread.flash_loop
# Pass the thread the event flash so results can be used in main thread
write_thread = threading.Thread(target=flash_thread.flash_loop, args=(flash_queue,flash_lock, path, is_sp, worker_threads))
write_thread.daemon = True # Dies when main program dies
write_thread.start() # Start the new thread
# SQL Operations moved to the worker threads themselves!
# Create a thread that runs the SQL database
# Pass the thread the SQL event queue so it can be scheduled
#db_thread = threading.Thread(target=sql_thread.sql_worker, args=(path, sql_queue))
#db_thread.daemon = True # Dies when main program dies
#db_thread.start() # Start the new thread
init_sql = sql_utils.init_sql # Make sure the sql server is initialized
while True: # Main Loop
# Non-blocking check for new serial events
while not serial_queue.empty(): # Whenever the event queue is not empty...
event = serial_queue.get() # Get the current state of the event queue from the serial thread
# Print detection of connection or disconnection
print_log.pL("Serial", "Event", f"Controller {event["event"]}, {event["port"]}", "System", True, None)
# If a serial event has been detected... update device list:
previous_devices = dict(devices)
with serial_thread.devices_lock: # Lock while reading
devices = dict(serial_thread.connected_devices) # Get list of all devices from the serial thread dictionary
# Newly connected devices
for device in devices.values():
if device not in previous_devices.values():
is_saved = dcs_dict_utils.init_dcs(path, device, current_dict, current_dict_lock)
if is_saved:
is_loaded = dcs_dict_utils.load_dcs(path, device["port"], current_dcs, current_dict, current_dict_lock)
port = device["port"]
worker_thread_utils.add_worker(port, worker_threads, sql_queue, current_dict, path)
# Disconnected devices
for device in previous_devices.values():
if device not in devices.values():
is_removed = dcs_dict_utils.unload_dcs(path, device["port"], current_dcs)
worker_thread_utils.remove_worker(device["port"], worker_threads)
else: # If the data path is not initialized
print_log.pL("System", "Error", "Data Path has failed to initialize", "System", True)
main() # Run Main Loop
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.