Sunday, July 5, 2020

Automating Startup and Shutdown

When we start imaging, we have to:
  1. Turn on the Power Supply (and the mount, though we normally don't turn it off)
  2. Check if the mount is still up, and if not, start it through the MGBox v2 pulse (this only sends a pulse that will turn on the mount if it's off and turn it off if it's on, that's why we have to check first).
  3. Start the Planewave Heater Control Program (PWI3)
  4. Make sure that Dimension4 is running (to keep computer time exact)
  5. Check if the clocksync program is running, and if not, start it (to keep mount time synchronized to the exact computer time)
And at the end, do the opposite:
  1. Close clocksync
  2. Close PWI3
  3. Shutdown mount
  4. Turn off power supply
I found a couple of useful utilities/libraries:
  • dlipower - a python library to interact with the power web switch
  • MountCMD - a Java library to interact with the mount
With these, could whip up some Python code to automate both:

startup.py
import dlipower
import time
import subprocess
import socket

POWER_SWITCH_IP = "192.168.254.22"
POWER_SWITCH_USERNAME = "<username>"
POWER_SWTICH_PASSWORD = "<password>"
MOUNT_POWER_PORT_NO = 1
POWER_SUPPLY_PORT_NO = 2

MOUNT_IP_ADDRESS = '192.168.254.21'
MOUNT_PORT = 3490

MGBOX_PATH = "C:\\Program Files (x86)\\MGBox V2\\MGBox.exe"
MGBOX_COM_PORT = "COM5"

PWI_PATH = "C:\\Program Files (x86)\\PlaneWave Instruments\\PWI3\\PWI3.exe";

DIMENSION4_EXE = "D4.exe"
DIMENSION4_PATH = "C:\\Program Files (x86)\\D4\\D4.exe"

CLOCKSYNC_EXE = "clocksync_w.exe"
CLOCKSYNC_PATH = "C:\\Program Files (x86)\\10micron\\ClockSyncW\\clocksync_w.exe"

def process_exists(process_name):
    call = 'TASKLIST', '/FI', 'imagename eq %s' % process_name
    # use buildin check_output right away
    output = subprocess.check_output(call).decode()
    # check in last line for process name
    last_line = output.strip().split('\r\n')[-1]
    # because Fail message could be translated
    return last_line.lower().startswith(process_name.lower())

# --------- Turn on Mount and Power Supply ---------

# Connection parameters to web UI of power switch
switch = dlipower.PowerSwitch(hostname=POWER_SWITCH_IP, userid=POWER_SWITCH_USERNAME, password=POWER_SWTICH_PASSWORD)

print("Turning on Mount (it should not have been off, but just in case)")
switch.on(MOUNT_POWER_PORT_NO)

print("Turning on the Power Supply for all equipment")
switch.on(POWER_SUPPLY_PORT_NO)

print("Wait a little to make sure...")
time.sleep(5)


# --------- Starting Mount ----------

# We can only send an on/off pulse, i.e. we first have to check if the mount is already powered on

print("Checking if mount is started by pinging IP address")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mount_is_running = sock.connect_ex((MOUNT_IP_ADDRESS, MOUNT_PORT))
if mount_is_running != 0:
   print("Starting mount")
   subprocess.call([MGBOX_PATH, "-P", MGBOX_COM_PORT, "-T", "1500"])
   print("Wait quite a while ...")
   time.sleep(30)
else:
   print("Mount is already started!")


# --------- Starting Planewave Heater control app ---------

# The planewave app is reentrnt, i.e. we don't have to check if it's already running
print("Starting Planewave Heater Control app")
subprocess.Popen([PWI_PATH])


# --------- Making sure that Dimension4 is up and running ---
print("Check if Dimension4 is running")
if not process_exists(DIMENSION4_EXE):
   print("Start Dimension4")
   subprocess.Popen(DIMENSION4_PATH)
   print("Wait a little time to make sure ...")
   time.sleep(5)
else:
   print("Dimension4 already running. moving along ...")


# --------- Starting mount time sync -------------------------

# First, check if it's already running
print("Check if time sync program is already running")
if not process_exists(CLOCKSYNC_EXE):
   print("Starting time sync program")
   subprocess.Popen([CLOCKSYNC_PATH])
   print("Wait a little to make sure ...")
   time.sleep(5)
else:
   print("Time Sync program is already started")

print("Done")

shutdown.py
import dlipower
import time
import subprocess
import socket

POWER_SWITCH_IP = "192.168.254.22"
POWER_SWITCH_USERNAME = "<username>"
POWER_SWTICH_PASSWORD = "<password>"
MOUNT_POWER_PORT_NO = 1
POWER_SUPPLY_PORT_NO = 2

MOUNT_IP_ADDRESS = '192.168.254.21'
MOUNT_PORT = 3490

MGBOX_PATH = "C:\\Program Files (x86)\\MGBox V2\\MGBox.exe"
MGBOX_COM_PORT = "COM5"

PWI_EXE = "PWI3.exe"
PWI_PATH = "C:\\Program Files (x86)\\PlaneWave Instruments\\PWI3\\PWI3.exe";

DIMENSION4_EXE = "D4.exe"
DIMENSION4_PATH = "C:\\Program Files (x86)\\D4\\D4.exe"

CLOCKSYNC_EXE = "clocksync_w.exe"
CLOCKSYNC_PATH = "C:\\Program Files (x86)\\10micron\\ClockSyncW\\clocksync_w.exe"

def process_exists(process_name):
    call = 'TASKLIST', '/FI', 'imagename eq %s' % process_name
    # use buildin check_output right away
    output = subprocess.check_output(call).decode()
    # check in last line for process name
    last_line = output.strip().split('\r\n')[-1]
    # because Fail message could be translated
    return last_line.lower().startswith(process_name.lower())

# --------- Stopping mount time sync -------------------------

# First, check if it's running
print("Check if time sync program is running")
if process_exists(CLOCKSYNC_EXE):
   print("Stopping time sync program")
   subprocess.call(["taskkill", "/im", CLOCKSYNC_EXE])
else:
   print("Time Sync program isn't running ...  moving on ...")


# --------- Stopping Planewave Heater control app ---------

#First, check if it's running
print("Check if Planewave Heater Control app is running")
if process_exists(PWI_EXE):
   print("Stopping Planewave Heater Control app")
   subprocess.call(["taskkill", "/im", PWI_EXE])
else:
   print("Planewave Heater Control app isn't running ...  moving on ...")

# --------- Shutdown Mount -------------------------
print("Shutdown mount")
subprocess.Popen("C:\\Users\\namid\\bin\\MountCMD\\MountCMD-shutdown.bat") 
print("Wait to make sure that the mount is really turned off")
time.sleep(30)

# --------- Turn off Power Supply ---------------------

# Connection parameters to web UI of power switch
switch = dlipower.PowerSwitch(hostname=POWER_SWITCH_IP, userid=POWER_SWITCH_USERNAME, password=POWER_SWTICH_PASSWORD)

print("Turning off the Power Supply for all equipment")
switch.off(POWER_SUPPLY_PORT_NO)

print("Done")

Works like c charm! That was a fun activity!!!

No comments:

Post a Comment