forked from mattgwwalker/hof3-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.py
156 lines (96 loc) · 2.84 KB
/
Server.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
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
from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.static import File
import json
from PLCPrimitives import *
from PLCObjects import *
from HOF3 import HOF3Client
from EventSource import EventSource
from Logger import Logger
from Read import Read
from Write import Write
from Membrane import Membrane
from Product import Product
from Chemical import Chemical
from BagFilter import BagFilter
# Create connection to PLC
plc = HOF3Client()
#print "Electing not to connect to the PLC"
reactor.connectTCP("192.168.1.91", 10001, plc, 5)
# Get PLC time
plcTime = PLCSystemTime(plc)
d = plcTime.get()
def onResult(data):
print "PLC Time is",data
return data
d.addCallback(onResult)
#plcTime.set(None) # Set PLC to current server time
# Start listening as HTTP server
root = File("www")
#root.putChild("membrane_insert", Membrane(plc))
root.putChild("events", EventSource(plc))
root.putChild("write", Write(plc))
root.putChild("read", Read(plc))
root.putChild("logger", Logger(plc))
root.putChild("membrane", Membrane())
root.putChild("product", Product())
root.putChild("chemical", Chemical())
root.putChild("bag-filter", BagFilter())
factory = Site(root)
reactor.listenTCP(8000, factory)
reactor.run()
exit()
# /////////////////////////
# What follows is test code
# Read log value
log1 = PLCInt(plc, 493)
d = log1.get()
def onResult(data):
print "Log1:",data
return data
d.addCallback(onResult)
iv08 = PLCEnergisable( plc, PLCUserMemory(550) )
group = PLCObject(plc)
group.addChild("iv08", iv08)
d = group.setChild("iv08", "manual")
iv08 = PLCEnergisable( plc, PLCUserMemory(550) )
cpuUsage = PLCInt(plc, 8434)
pc01 = PLCPIDController( plc, PLCUserMemory(670) )
group = PLCObject(plc)
group.addChild("cpu_usage", cpuUsage)
#d = group.get()
#d = pc01.get()
d = plc.get()
def onResult(data):
print json.dumps(data)
d.addCallback(onResult)
# ///////////////////
cpuUsage = PLCInt(plc, 8434)
macroSize = PLCInt(plc, 4433)
ft03 = PLCAnalogueInput(plc, PLCUserMemory(11), 100)
inputs = PLCBitSet(plc, [249,251], [["ci1","ci2","ci3"],["di1","di2","di3"]])
iv08 = PLCEnergisable( plc, PLCUserMemory(550) )
pc01 = PLCPIDController( plc, PLCUserMemory(670) )
group = PLCObject(plc)
group.addChild("cpu_usage", cpuUsage)
group.addChild("macro_size", macroSize)
group.addChild("ft03", ft03)
group.addChild("inputs", inputs)
group.addChild("iv08", iv08)
group.addChild("pc01", pc01)
d1 = group.getChild("cpu_usage")
d2 = group.getChild("iv08")
#d = group.get()
def onResult(data):
print "*** group:",data
print "*** json(group):",json.dumps(data)
from twisted.internet import defer
d = defer.gatherResults( [d1,d2] )
def onResults(data):
d1, d2 = data
print "d1:",d1
print "d2:",d2
merge(d1,d2)
print "new d1:",d1
print "json(d1):",json.dumps(d1)
d.addCallback(onResults)