|
1 <?xml version='1.0' encoding='utf-8'?> |
|
2 <PyFile xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
|
3 <variables> |
|
4 <variable name="FileNotify" type="HMI_INT"/> |
|
5 <variable name="CurrentPath" type="HMI_STRING" initial="..."/> |
|
6 <variable name="FileName" type="HMI_STRING"/> |
|
7 </variables> |
|
8 <globals> |
|
9 <xhtml:p><![CDATA[ |
|
10 from twisted.web.resource import Resource |
|
11 from os import getcwd, listdir |
|
12 from os.path import dirname, isfile, join |
|
13 import collections, json |
|
14 |
|
15 |
|
16 class FilesJsonResource(Resource): |
|
17 image_cache = {} |
|
18 |
|
19 def render_GET(self, request): |
|
20 request.setHeader('content-type', 'image/png') |
|
21 img_name = request.args[b'name'][0].decode('utf-8') |
|
22 p = getcwd() + '/' + img_name |
|
23 if p not in self.image_cache: |
|
24 with open(p, 'rb') as image_file: |
|
25 img_bytes = image_file.read() |
|
26 self.image_cache[p] = img_bytes |
|
27 else: |
|
28 img_bytes = self.image_cache[p] |
|
29 return img_bytes |
|
30 |
|
31 |
|
32 def render_POST(self, request): |
|
33 newstr = request.content.getvalue() |
|
34 newdata = json.loads(newstr) |
|
35 args = newdata['args'] |
|
36 range_feedback = newdata['range'] |
|
37 slider_position = newdata['position'] |
|
38 visible = newdata['visible'] |
|
39 extra = newdata['extra'] |
|
40 options = newdata['options'] |
|
41 |
|
42 path = PLCGlobals.CurrentPath |
|
43 if path == '...': |
|
44 PLCGlobals.CurrentPath = path = getcwd() |
|
45 |
|
46 if len(options) == 1 : |
|
47 action, = options |
|
48 if action == 'action_reset': |
|
49 PLCGlobals.CurrentPath = path = getcwd() |
|
50 elif len(options) == 2 : |
|
51 action, sent_path = options |
|
52 if action == 'onClick[acknowledge]': |
|
53 if sent_path.endswith('.csv'): |
|
54 PLCGlobals.FileName = sent_path |
|
55 else: |
|
56 PLCGlobals.CurrentPath = path = sent_path |
|
57 |
|
58 ld = listdir(path) |
|
59 ld.sort() |
|
60 |
|
61 if path != '/': |
|
62 FileList = [ |
|
63 { |
|
64 'name': '..', |
|
65 'path': dirname(path), |
|
66 'type': 'folder', |
|
67 'status': 'active', |
|
68 'thumbnail': '/files?name=folder.png' |
|
69 } |
|
70 ] |
|
71 else: |
|
72 FileList = [] |
|
73 |
|
74 FileList.extend([ |
|
75 { |
|
76 'name': f, |
|
77 'path': join(path, f), |
|
78 'type': 'folder', |
|
79 'status': 'active', |
|
80 'thumbnail': '/files?name=folder.png' |
|
81 } |
|
82 for f in ld |
|
83 if not (isfile(join(path, f)) |
|
84 or f.startswith(".")) |
|
85 ]) |
|
86 FileList.extend([ |
|
87 { |
|
88 'name': f, |
|
89 'path': join(path, f), |
|
90 'type': 'file', |
|
91 'status': 'active', |
|
92 'thumbnail': '/files?name=file.png' |
|
93 } |
|
94 for f in ld |
|
95 if isfile(join(path, f)) |
|
96 and f.endswith(".csv") |
|
97 ]) |
|
98 |
|
99 answer = self.renderTable( |
|
100 FileList, range_feedback, slider_position, visible, extra |
|
101 ) |
|
102 janswer = json.dumps(answer) |
|
103 return janswer.encode() |
|
104 |
|
105 def renderTable(self, FileList, old_range, old_position, visible, extra): |
|
106 if len(extra) > 0 and extra[0] != "": |
|
107 fFiles = [fl for fl in FileList if extra[0] in fl] |
|
108 else: |
|
109 fFiles = FileList[:] |
|
110 new_range = len(fFiles) |
|
111 delta = new_range - visible |
|
112 new_position = 0 if delta <= 0 else delta if old_position > delta else old_position |
|
113 new_visible = new_range if delta <= 0 else visible |
|
114 |
|
115 visible_files = [] |
|
116 for desc in fFiles[new_position:new_position + new_visible]: |
|
117 visible_files.append(desc) |
|
118 |
|
119 return new_range, new_position, visible_files |
|
120 |
|
121 |
|
122 ]]></xhtml:p> |
|
123 </globals> |
|
124 <init> |
|
125 <xhtml:p><![CDATA[ |
|
126 ]]></xhtml:p> |
|
127 </init> |
|
128 <cleanup> |
|
129 <xhtml:p><![CDATA[ |
|
130 ]]></xhtml:p> |
|
131 </cleanup> |
|
132 <start> |
|
133 <xhtml:p><![CDATA[ |
|
134 |
|
135 AddPathToSVGHMIServers(b"files", FilesJsonResource) |
|
136 |
|
137 |
|
138 ]]></xhtml:p> |
|
139 </start> |
|
140 <stop> |
|
141 <xhtml:p><![CDATA[ |
|
142 ]]></xhtml:p> |
|
143 </stop> |
|
144 </PyFile> |