-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
94 lines (75 loc) · 3.42 KB
/
backend.py
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
import requests
import copy
import threading
import logging
import sys
from state import State
from nicegui import ui
class Backend:
def __init__(self, config):
self.conf = config
self.logger = logging.getLogger("Backend")
def saveModel(self, current_model, simple):
self.logger.info('saving model...')
to_save = copy.copy(current_model)
if (simple):
to_save = State.simplifyModel(to_save)
if self.conf.multithread:
threading.Thread(target=self.saveJSONState, args=(to_save,)).start()
else:
self.saveJSONState(to_save)
self.logger.info('saved')
def reduceGamesToOne(self):
self.saveJSONState({State.T1SET5_INT:'0', State.T2SET5_INT:'0'})
self.saveJSONState({State.T1SET4_INT:'0', State.T2SET4_INT:'0'})
self.saveJSONState({State.T1SET3_INT:'0', State.T2SET3_INT:'0'})
self.saveJSONState({State.T1SET2_INT:'0', State.T2SET2_INT:'0'})
def saveJSONState(self, to_save):
self.logger.info('saving JSON state...')
return self.sendCommandWithIdAndContent("SetOverlayContent", to_save)
def saveJSONCustomization(self, to_save):
self.logger.info('saving JSON customization...')
return self.sendCommandWithValue("SetCustomization", to_save)
def changeOverlayVisibility(self, show):
self.logger.info('changing overlay visibility, show: %s', show)
command = "HideOverlay"
if show:
command = "ShowOverlay"
return self.sendCommandWithIdAndContent(command)
def sendCommandWithValue(self, command, value=""):
jsonin = {"command": command, "value":value}
return self.process_response(requests.put(f'https://app.overlays.uno/apiv2/controlapps/{self.conf.oid}/api', json=jsonin))
def sendCommandWithIdAndContent(self, command, content=""):
jsonin = {"command": command, "id": self.conf.id, "content":content}
return self.process_response(requests.put(f'https://app.overlays.uno/apiv2/controlapps/{self.conf.oid}/api', json=jsonin))
def getCurrentStateModel(self):
self.logger.info('getting state')
response = self.sendCommandWithIdAndContent("GetOverlayContent")
if 200 == response.status_code:
return response.json()['payload']
return None
def getCurrentCustomizationStateModel(self):
self.logger.info('getting customization')
response = self.sendCommandWithIdAndContent("GetCustomization")
if 200 == response.status_code:
return response.json()['payload']
return None
def isVisible(self):
response = self.sendCommandWithIdAndContent("GetOverlayVisibility")
if 200 == response.status_code:
return response.json()['payload']
else:
return False;
def reset(self, state):
self.saveModel(state.getResetModel(), False)
def save(self, state, simple):
self.saveModel(state.getCurrentModel(), simple)
def saveCustomization(self, customization):
return self
def process_response(self, response):
if response.status_code >= 300 :
self.logger.warning("response %s: '%s'", response.status_code, response.text)
else:
self.logger.info("response status: %s", response.status_code)
self.logger.debug("response message: '%s'", response.text)
return response