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.
160 lines
7.8 KiB
160 lines
7.8 KiB
6 years ago
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||
|
import pywinauto
|
||
|
import json
|
||
|
import subprocess
|
||
|
import time
|
||
|
import zlib
|
||
|
import os
|
||
6 years ago
|
import PIL
|
||
|
from PIL import ImageGrab
|
||
6 years ago
|
from threading import Thread
|
||
5 years ago
|
import Processor
|
||
5 years ago
|
import importlib
|
||
5 years ago
|
import pdb
|
||
|
import imp
|
||
5 years ago
|
from desktopmagic.screengrab_win32 import (
|
||
|
getDisplayRects, saveScreenToBmp, saveRectToBmp, getScreenAsImage,
|
||
|
getRectAsImage, getDisplaysAsImages)
|
||
|
global mGlobalDict
|
||
6 years ago
|
|
||
6 years ago
|
def SaveScreenshot(inFilePath):
|
||
|
# grab fullscreen
|
||
5 years ago
|
# Save the entire virtual screen as a PNG
|
||
|
lScreenshot = getScreenAsImage()
|
||
|
lScreenshot.save('screenshot.png', format='png')
|
||
|
#lScreenshot = ScreenshotSecondScreen.grab_screen()
|
||
6 years ago
|
# save image file
|
||
5 years ago
|
#lScreenshot.save('screenshot.png')
|
||
6 years ago
|
|
||
6 years ago
|
#inGlobalDict
|
||
|
# "JSONConfigurationDict":<JSON>
|
||
6 years ago
|
class RobotDaemonServer(Thread):
|
||
6 years ago
|
def __init__(self,name,inGlobalDict):
|
||
6 years ago
|
Thread.__init__(self)
|
||
|
self.name = name
|
||
6 years ago
|
def run(self):
|
||
|
inServerAddress="";
|
||
5 years ago
|
inPort = mGlobalDict["Server"]["ListenPort"];
|
||
6 years ago
|
print('starting server..., port:'+str(inPort)+" inAddress:"+inServerAddress)
|
||
6 years ago
|
# Server settings
|
||
|
# Choose port 8080, for port 80, which is normally used for a http server, you need root access
|
||
6 years ago
|
server_address = (inServerAddress, inPort)
|
||
6 years ago
|
httpd = HTTPServer(server_address, testHTTPServer_RequestHandler)
|
||
|
print('running server...')
|
||
|
httpd.serve_forever()
|
||
|
|
||
|
|
||
|
# HTTPRequestHandler class
|
||
|
class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
|
||
|
#ResponseContentTypeFile
|
||
|
def SendResponseContentTypeFile(self,inContentType,inFilePath):
|
||
|
# Send response status code
|
||
|
self.send_response(200)
|
||
|
# Send headers
|
||
|
self.send_header('Content-type',inContentType)
|
||
|
self.end_headers()
|
||
|
lFileObject = open(inFilePath, "rb")
|
||
|
# Write content as utf-8 data
|
||
|
self.wfile.write(lFileObject.read())
|
||
|
#Закрыть файловый объект
|
||
|
lFileObject.close()
|
||
|
# GET
|
||
|
def do_GET(self):
|
||
|
#Мост между файлом и http запросом (новый формат)
|
||
|
if self.path == "/":
|
||
6 years ago
|
self.SendResponseContentTypeFile('text/html',"Web\\Index.xhtml")
|
||
6 years ago
|
#Мост между файлом и http запросом (новый формат)
|
||
|
if self.path == '/3rdParty/Semantic-UI-CSS-master/semantic.min.css':
|
||
6 years ago
|
self.SendResponseContentTypeFile('text/css',"..\\Resources\\Web\\Semantic-UI-CSS-master\\semantic.min.css")
|
||
6 years ago
|
#Мост между файлом и http запросом (новый формат)
|
||
|
if self.path == '/3rdParty/Semantic-UI-CSS-master/semantic.min.js':
|
||
6 years ago
|
self.SendResponseContentTypeFile('application/javascript',"..\\Resources\\Web\\Semantic-UI-CSS-master\\semantic.min.js")
|
||
6 years ago
|
#Мост между файлом и http запросом (новый формат)
|
||
|
if self.path == '/3rdParty/jQuery/jquery-3.1.1.min.js':
|
||
6 years ago
|
self.SendResponseContentTypeFile('application/javascript',"..\\Resources\\Web\\jQuery\\jquery-3.1.1.min.js")
|
||
6 years ago
|
#Мост между файлом и http запросом (новый формат)
|
||
|
if self.path == '/3rdParty/Google/LatoItalic.css':
|
||
6 years ago
|
self.SendResponseContentTypeFile('font/css',"..\\Resources\\Web\\Google\\LatoItalic.css")
|
||
6 years ago
|
#Мост между файлом и http запросом (новый формат)
|
||
|
if self.path == '/3rdParty/Semantic-UI-CSS-master/themes/default/assets/fonts/icons.woff2':
|
||
6 years ago
|
self.SendResponseContentTypeFile('font/woff2',"..\\Resources\\Web\\Semantic-UI-CSS-master\\themes\\default\\assets\\fonts\\icons.woff2")
|
||
6 years ago
|
#Мост между файлом и http запросом (новый формат)
|
||
|
if self.path == '/favicon.ico':
|
||
6 years ago
|
self.SendResponseContentTypeFile('image/x-icon',"Web\\favicon.ico")
|
||
6 years ago
|
#Мост между файлом и http запросом (новый формат)
|
||
|
if self.path == '/3rdParty/Handlebars/handlebars-v4.1.2.js':
|
||
6 years ago
|
self.SendResponseContentTypeFile('application/javascript',"..\\Resources\\Web\\Handlebars\\handlebars-v4.1.2.js")
|
||
6 years ago
|
#Получить скриншот
|
||
6 years ago
|
if self.path.split("?")[0] == '/GetScreenshot':
|
||
6 years ago
|
#Сохранить файл на диск
|
||
|
SaveScreenshot("Screenshot.png")
|
||
|
self.SendResponseContentTypeFile('image/png',"Screenshot.png")
|
||
6 years ago
|
#Monitor
|
||
|
if self.path == '/Monitor/JSONDaemonListGet':
|
||
6 years ago
|
# Send response status code
|
||
|
self.send_response(200)
|
||
|
# Send headers
|
||
|
self.send_header('Content-type','application/json')
|
||
|
self.end_headers()
|
||
|
# Send message back to client
|
||
5 years ago
|
message = json.dumps(mGlobalDict)
|
||
6 years ago
|
# Write content as utf-8 data
|
||
6 years ago
|
self.wfile.write(bytes(message, "utf8"))
|
||
5 years ago
|
if self.path == '/Monitor/ControlPanelDictGet':
|
||
|
# Send response status code
|
||
|
self.send_response(200)
|
||
|
# Send headers
|
||
|
self.send_header('Content-type','application/json')
|
||
|
self.end_headers()
|
||
|
#Create result JSON
|
||
|
lResultJSON={"RenderRobotList":[]}
|
||
5 years ago
|
lRenderFunctionsRobotList=mGlobalDict["ControlPanelDict"]["RobotList"]
|
||
5 years ago
|
for lItem in lRenderFunctionsRobotList:
|
||
|
#Подключить модуль для вызова
|
||
5 years ago
|
#print(globals())
|
||
|
lModuleImportName = lItem.get("RenderFunctionModuleSubfolderName","")
|
||
|
if lModuleImportName!="":
|
||
|
lModuleImportName = f'{lItem["RenderFunctionModuleSubfolderName"]}.{lItem["RenderFunctionModuleName"]}'
|
||
|
else:
|
||
|
lModuleImportName = lItem["RenderFunctionModuleName"]
|
||
|
lModule=importlib.import_module(lModuleImportName)
|
||
5 years ago
|
#Найти функцию
|
||
|
lFunction=getattr(lModule,lItem["RenderFunctionName"])
|
||
|
#Выполнить вызов и записать результат
|
||
5 years ago
|
lItemResultDict=lFunction(mGlobalDict)
|
||
5 years ago
|
#RunFunction
|
||
|
lResultJSON["RenderRobotList"].append(lItemResultDict)
|
||
|
# Send message back to client
|
||
|
message = json.dumps(lResultJSON)
|
||
|
# Write content as utf-8 data
|
||
|
self.wfile.write(bytes(message, "utf8"))
|
||
5 years ago
|
#Filemanager function
|
||
|
if self.path.lower().startswith('/filemanager/'):
|
||
|
lFileURL=self.path[13:]
|
||
|
# check if file in FileURL - File Path Mapping Dict
|
||
5 years ago
|
if lFileURL.lower() in mGlobalDict["FileManager"]["FileURLFilePathDict"]:
|
||
|
self.SendResponseContentTypeFile('application/octet-stream',mGlobalDict["FileManager"]["FileURLFilePathDict"][lFileURL])
|
||
6 years ago
|
# POST
|
||
|
def do_POST(self):
|
||
6 years ago
|
#Централизованная функция получения запросов/отправки
|
||
5 years ago
|
if self.path == '/Utils/Processor':
|
||
6 years ago
|
#ReadRequest
|
||
6 years ago
|
lInputObject={}
|
||
|
if self.headers.get('Content-Length') is not None:
|
||
|
lInputByteArrayLength = int(self.headers.get('Content-Length'))
|
||
|
lInputByteArray=self.rfile.read(lInputByteArrayLength)
|
||
|
#Превращение массива байт в объект
|
||
|
lInputObject=json.loads(lInputByteArray.decode('utf8'))
|
||
6 years ago
|
# Send response status code
|
||
|
self.send_response(200)
|
||
|
# Send headers
|
||
|
self.send_header('Content-type','application/json')
|
||
|
self.end_headers()
|
||
|
# Send message back to client
|
||
5 years ago
|
message = json.dumps(Processor.ActivityListOrDict(lInputObject))
|
||
6 years ago
|
# Write content as utf-8 data
|
||
|
self.wfile.write(bytes(message, "utf8"))
|
||
|
return
|
||
|
|
||
|
#print(ChildProcessReadWaitString(p))
|