-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulator.lua
176 lines (129 loc) · 2.98 KB
/
Simulator.lua
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
module(..., package.seeall)
require "knx"
require "sys"
require "socket"
function intiSocket()
local host, port = "localhost", 3663
local ip = socket.dns.toip(host)
local master, error = socket.tcp()
if master == nil then
print("faild to make socket: " .. error)
end
local done, error = master:connect(ip, port)
if done == nil then
print("faild to make socket: " .. error)
else
_G._debugClientTcpSocket = master
end
_G._debugClientTcpSocket:settimeout(0) -- make socket non blocking
end
function split(str, pat)
r = {}
last = 1
for i=1,str:len() do
if string.sub(str, i,i) == pat then
if last ~= i then
table.insert(r,string.sub(str, last,i-1))
else
table.insert(r,"")
end
last = i+1
end
end
table.insert(r,string.sub(str, last,str:len()))
return r
end
-- overwrite default output
function out(...)
local st=""
for index, val in ipairs(arg) do
if type(val) == "boolean" then
if val == true then
st = st .. "true" .. " "
else
st = st .. "false" .. " "
end
else
st = st .. val .. " "
end
end
if _G._debugClientTcpSocket ~= -1 then
_G._debugClientTcpSocket:send("DebugOut;D;".. st .. "\n")
else
_defPrint(st)
end
end
_G.print = out
-- main system
--function dispatcher ()
-- while true do
-- local n = table.getn(threads)
-- if n == 0 then break end -- no more threads to run
-- for i=1,n do
-- if coroutine.status (threads[i]) ~= "dead" then
-- local status, errorMsg = coroutine.resume(threads[i])
-- if not status then
-- print(errorMsg)
-- end
-- else
-- table.remove(threads, i)
-- end
-- end
-- end
--end
-- handel socket receive
function receive()
local val
local status
val,status = _G._debugClientTcpSocket:receive('*l')
if val ~= nil then
args = split(val,";")
if args[1] == "SetObjVal" then
knx.set_float(tonumber(args[2]),tonumber(args[3]))
end
--_G._debugClientTcpSocket:send("DebugOut;D;" .. val .. "\n")
end
end
-- for timer callback
function timerCallback()
if _timerIsRunning ~= 0 then
makeCall = 0
if (_lastTimeCalled == -1)
or ((socket.gettime()*1000 - _lastTimeCalled ) >= _actualTimeout) then
if _CallBackTimeout ~= -1 then
_lastTimeCalled = socket.gettime()*1000
local retVal
if _actualTimerHandover ~= -1 then
retVal = _CallBackTimeout(_actualTimerHandover)
else
retVal = _CallBackTimeout()
end
-- stop if 1 returned
if retVal == 1 then
_timerIsRunning = 0
end
else
print("Timer: No Callback Function Defined")
end
end
end
end
function init()
intiSocket()
_G._debugClientTcpSocket:send("START " .. os.date("%X") .. "\n")
end
function main()
while true do
receive()
timerCallback()
end
--local co = coroutine.create(function()
-- receive()
--end)
--table.insert(threads,co)
--co = coroutine.create(function()
-- timerCallback()
--end)
--table.insert(threads,co)
--dispatcher()
end