You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
4.6 KiB
100 lines
4.6 KiB
6 years ago
|
import datetime
|
||
|
import http.client
|
||
|
import json
|
||
6 years ago
|
import pdb
|
||
6 years ago
|
import os
|
||
6 years ago
|
import sys
|
||
6 years ago
|
import subprocess
|
||
6 years ago
|
import win32ts
|
||
6 years ago
|
#Глобальная переменная - все глобальные значения программы
|
||
|
global mGlobalDict
|
||
6 years ago
|
|
||
|
#Включить WTS (WIN32TS)
|
||
|
mWTSServer = lWTSServer = win32ts.WTSOpenServer("localhost")
|
||
6 years ago
|
#{
|
||
|
# actionList:
|
||
|
# [
|
||
|
# {
|
||
|
# type: <RemoteMachineProcessingRun>,
|
||
|
# host: <localhost>,
|
||
|
# port: <port>,
|
||
|
# bodyObject: <object dict, int, str, list>
|
||
6 years ago
|
# },
|
||
|
# {
|
||
6 years ago
|
# type: <ActivityLogScheduleListGet>
|
||
6 years ago
|
# },
|
||
|
# {
|
||
|
# type: <ActivityCMDRun>,
|
||
|
# code: <str>
|
||
6 years ago
|
# },
|
||
|
# {
|
||
|
# type: <ActivityRestartOrchestrator>
|
||
6 years ago
|
# },
|
||
|
# {
|
||
|
# type: <ActivitySessionCheckSetActive>
|
||
6 years ago
|
# }
|
||
|
# ]
|
||
|
#
|
||
|
#}
|
||
6 years ago
|
|
||
6 years ago
|
def ProcessingRun(inConfigurationDict):
|
||
6 years ago
|
#print(mGlobalDict)
|
||
6 years ago
|
lDateTimeString=datetime.datetime.strftime(datetime.datetime.now(),"%Y.%m.%d %H:%M:%S::%f")
|
||
|
lResult={"dateTime":lDateTimeString, "state":"connected", "actionListResult":[]}
|
||
|
|
||
|
for lItem in inConfigurationDict["actionList"]:
|
||
|
#Добавить входные значения
|
||
|
lResult["actionListResult"].append({"inArgs":lItem})
|
||
|
#Обработка запроса на отправку команды на удаленную машину
|
||
|
if lItem["type"]=="RemoteMachineProcessingRun":
|
||
|
lHTTPConnection = http.client.HTTPConnection(lItem["host"], lItem["port"], timeout=5)
|
||
|
try:
|
||
|
lHTTPConnection.request("POST","/ProcessingRun",json.dumps(lItem["bodyObject"]))
|
||
|
except Exception as e:
|
||
|
#Объединение словарей
|
||
|
lResult["actionListResult"][-1] = {**lResult["actionListResult"][-1], **{"state":"disconnected","errorMessage":str(e)}}
|
||
|
#lResult["actionListResult"][-1].join({"state":"disconnected","errorMessage":str(e)})
|
||
|
else:
|
||
|
lHTTPResponse=lHTTPConnection.getresponse()
|
||
|
lHTTPResponseByteArray=lHTTPResponse.read()
|
||
|
lResult["actionListResult"][-1] = {**lResult["actionListResult"][-1], **json.loads(lHTTPResponseByteArray.decode('utf8'))}
|
||
6 years ago
|
#Обработка команды ActivityLogScheduleListGet
|
||
6 years ago
|
if lItem["type"]=="ActivityLogScheduleListGet":
|
||
6 years ago
|
#pdb.set_trace()
|
||
6 years ago
|
lResult["actionListResult"][-1] = {**lResult["actionListResult"][-1], **{"result":mGlobalDict["ActivityLogScheduleList"]}}
|
||
6 years ago
|
#Обработка команды PlanLogListGet
|
||
|
if lItem["type"]=="PlanLogListGet":
|
||
|
#pdb.set_trace()
|
||
|
lResult["actionListResult"][-1] = {**lResult["actionListResult"][-1], **{"result":mGlobalDict["JSONConfigurationDict"]["activityList"]}}
|
||
6 years ago
|
#Обработка команды ActivityCMDRun
|
||
|
if lItem["type"]=="ActivityCMDRun":
|
||
|
lCMDCode="cmd /c "+lItem["code"]
|
||
|
subprocess.Popen(lCMDCode)
|
||
|
lResultCMDRun=1#os.system(lCMDCode)
|
||
|
lResult["actionListResult"][-1] = {**lResult["actionListResult"][-1], **{"result":str(lResultCMDRun)}}
|
||
6 years ago
|
#Обработка команды ActivityRestartOrchestrator
|
||
|
if lItem["type"]=="ActivityRestartOrchestrator":
|
||
|
os.execl(sys.executable,os.path.abspath(__file__),*sys.argv)
|
||
|
sys.exit(0)
|
||
6 years ago
|
#Обработка команды ActivitySessionCheckSetActive
|
||
|
if lItem["type"]=="ActivitySessionCheckSetActive":
|
||
|
lResult["actionListResult"][-1] = {**lResult["actionListResult"][-1], **{"result":{"IsActivated":False}}}
|
||
|
lPID = os.getpid()
|
||
|
#pdb.set_trace()
|
||
|
lSessionId = win32ts.ProcessIdToSessionId(lPID)
|
||
|
lSessionList = win32ts.WTSEnumerateSessions(lWTSServer)
|
||
|
#При попытке закрыть сервер возникает ошибка
|
||
|
#win32ts.WTSCloseServer(lWTSServer)
|
||
|
#Наложить фильтр
|
||
|
lSessionFiltered = [d for d in lSessionList if d["SessionId"]==lSessionId]
|
||
|
#Выполнить переход в активное состояние , если State != 0
|
||
|
if lSessionFiltered[0]["State"] != 0:
|
||
|
lCMDCodeTSCON = "tscon " + str(lSeessionId) + " /dest:console"
|
||
|
lCMDCode="cmd /c "+lCMDCodeTSCON
|
||
|
subprocess.Popen(lCMDCode)
|
||
|
lResult["actionListResult"][-1] = {**lResult["actionListResult"][-1], **{"result":{"IsActivated":True}}}
|
||
|
#Выключить соединение
|
||
|
|
||
6 years ago
|
#Вернуть результат
|
||
6 years ago
|
return lResult
|