forked from SharpAI/DeepCamera
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncThing.py
More file actions
171 lines (147 loc) · 5.33 KB
/
Copy pathsyncThing.py
File metadata and controls
171 lines (147 loc) · 5.33 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# -*- coding: utf-8 -*-
import xml.sax
from syncthing import Syncthing
import time
class xmlHandler( xml.sax.ContentHandler ):
def __init__(self):
self.currentkeyName = ""
self.apikey = ""
def startElement(self, tag, attributes):
if tag == 'apikey':
self.currentkeyName = "apikey"
#print("begain to parse %s"%(tag))
def endElement(self, tag):
if tag == 'apikey':
self.currentkeyName = ""
#print("finised parsing %s"%(tag))
def characters(self, content):
if len(self.currentkeyName) > 1 and self.currentkeyName == "apikey":
#print("%s=%s"%(self.currentkeyName, content))
self.apikey = str(content)
def get_Current_Syncthing_apikey(cfg_file):
parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
Handler = xmlHandler()
parser.setContentHandler( Handler )
parser.parse(cfg_file)
if len(Handler.apikey) < 1:
print('apikey not found !')
return Handler.apikey
class workaiSynctingClass:
def __init__(self):
self.KEY = ''
self.HOST = '127.0.0.1'
self.PORT = 8384
self.IS_HTTPS = False
self.SSL_CERT_FILE = None
self.syncthing = None
self.syncthing_myID = None
def _get_status(self):
if self.syncthing is None:
return None
status = self.syncthing.system.status()
if type(status) == str:
status = json.loads(status)
myId = status.get("myID", "")
if len(myId) > 1:
self.syncthing_myID = myId
def _get_discovery_IDs(self):
IDs = []
if self.syncthing is None:
return None
discovery = self.syncthing.system.discovery()
for key in discovery:
IDs.append(key)
print('discovery:')
print(IDs)
def _get_connections_IDs(self):
IDs = []
if self.syncthing is None:
return None
connections = self.syncthing.system.connections()
for key in connections['connections']:
IDs.append(key)
print('connections:')
print(IDs)
def _get_config(self):
if self.syncthing is None:
return None
config = self.syncthing.system.config()
print(config)
def add_one_device_to_syncthing(self, deviceId):
if self.syncthing is None or len(deviceId) < 1:
return None
config = self.syncthing.system.config()
if config is None:
return None
devices = config['devices']
if config is None:
return None
for device in devices:
if device['deviceID'] == deviceId:
print("%s: this device already added !" %(device['deviceID']))
return None
new_device = {
u'compression': u'metadata',
u'skipIntroductionRemovals': False,
u'allowedNetworks': [],
u'certName': u'',
u'introducer': False,
u'name': u'',
u'paused': False,
u'deviceID': deviceId,
u'introducedBy': u'',
u'addresses': [u'dynamic']
}
devices.append(new_device)
config['devices'] = devices
#把添加过设备的配置文件发回syncthing
self.syncthing.system.set_config(config, and_restart=False)
print("%s device added !" %(deviceId))
return new_device
def remove_one_device_from_syncthing(self, deviceId):
hasRemoved = True
devices_new = []
if self.syncthing is None or len(deviceId) < 1:
return None
config = self.syncthing.system.config()
if config is None:
return None
devices = config['devices']
if config is None:
return None
for device in devices:
if device['deviceID'] != deviceId:
devices_new.append(device)
else:
hasRemoved = False
if hasRemoved is True:
print("%s: this device already removed !" %(device['deviceID']))
return None
config['devices'] = devices_new
#把添加过设备的配置文件发回syncthing
self.syncthing.system.set_config(config, and_restart=False)
print("%s device removed !" %(deviceId))
return devices_new
def initialize(self, configFile):
self.KEY = get_Current_Syncthing_apikey(configFile)
self.syncthing = Syncthing(self.KEY, self.HOST, self.PORT, 10.0, self.IS_HTTPS, self.SSL_CERT_FILE)
# name spaced by API endpoints
self.syncthing.system.connections()
# suports GET/POST semantics
sync_errors = self.syncthing.system.errors()
self.syncthing.system.clear()
if sync_errors:
for e in sync_errors:
print(e)
#self.syncthing.system.disable_debug()
#self._get_status()
#self._get_discovery_IDs()
#self._get_connections_IDs()
#self._get_config()
#if __name__ == '__main__':
# sync = workaiSynctingClass()
# sync.initialize('./config.xml')
# sync.add_one_device_to_syncthing('OUDMQHI-WWYFDIB-FYBLKTX-K2VZDMH-BLAPIO3-MLAUPFS-QYI3RIC-IH6KUQG')
# time.sleep(10)
# sync.remove_one_device_from_syncthing('OUDMQHI-WWYFDIB-FYBLKTX-K2VZDMH-BLAPIO3-MLAUPFS-QYI3RIC-IH6KUQG')