forked from miguelalarcos/pySimulatorSCADA
/
main_twisted.py
71 lines (50 loc) · 1.5 KB
/
main_twisted.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
from simple_json import dumps
from twisted.web import resource
from twisted.internet import reactor, task
from twisted.application import service
from twisted.web import server
#from temperature import read_temp
from time import time
from random import random
sets = {}
sets['balbula_1'] = True
sets['control'] = False
input1 = 50.0
class DataResource(resource.Resource):
isLeaf = True
def render_GET(self, request):
request.setHeader("content-type", "application/json")
return dumps({'time': time(), 'control': sets['control'], 'balbula_1': sets['balbula_1'], 'input1': input1})
def setUnset(variable):
sets[variable] = not sets[variable]
class Set(resource.Resource):
isLeaf = True
def render_POST(self, request):
variable = request.args['var'][0]
setUnset(variable)
return ''
def simulacion():
global input1
input1 -= 1
input1 += sets['balbula_1']*2
#input1 = read_temp() or input1
print input1
t = task.LoopingCall(simulacion)
def f2():
global input1
if not sets['control']:
if input1 <= 50:
sets['balbula_1'] = True
else:
sets['balbula_1'] = False
t2 = task.LoopingCall(f2)
root = resource.Resource()
root.putChild('data', DataResource())
root.putChild('set', Set())
application = service.Application('SCADA')
service = reactor.listenTCP(8080, server.Site(root))
application.addComponent(service)
if __name__ == '__main__':
t.start(0.5)
t2.start(1.0)
reactor.run()