-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.lua
179 lines (155 loc) · 5.26 KB
/
main.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
172
173
174
175
176
177
178
179
local computer = require("computer")
local http = require("http-method")
local component = require("component")
local json = require("json")
local me = component.me_interface
local meCpu = require("cpu")
local config = require("config")
local tasks = {}
local monitors = {}
function tasks.refreshStorage(data)
-- data 是物品过滤表
local item = me.getItemsInNetwork(data)
local result = {}
for _, j in pairs(item) do
table.insert(result, {
name = j.name,
label = j.label,
isCraftable = j.isCraftable,
damage = j.damage,
size = j.size,
aspect = j.aspect
})
end
http.put(config.path.items, {}, {result = result})
end
function tasks.refreshFluidStorage(_)
-- 刷新流体库存,因为无法筛选,有内存溢出情况
local fluids = me.getFluidsInNetwork()
local result = {}
for _, j in pairs(fluids) do
table.insert(result, {
name = j.name,
label = j.label,
isCraftable = j.isCraftable,
amount = j.amount
})
end
http.put(config.path.fluids, {}, {result = result})
end
function tasks.refreshEssentiaStorage(_)
-- 刷新原质库存(因为无法筛选,有内存溢出情况)
local fluids = me.getEssentiaInNetwork()
local result = {}
for _, j in pairs(fluids) do
table.insert(result, {
name = j.name,
label = j.label,
amount = j.amount,
aspect = j.aspect
})
end
http.put(config.path.essentia, {}, {result = result})
end
function tasks.requestItem(data)
-- 请求制作物品,参数格式:
-- data = {
-- filter = {},
-- amount = 1,
-- prioritizePower = true,
-- cpuName = "cpu1"
-- }
if data.filter == nil then data.filter = {} end
local craftable = me.getCraftables(data.filter)[1]
if craftable == nil then return "没有指定的物品: " .. json.encode(data.filter) end
if data.amount == nil then data.amount = 1 end
local result
if data.cpuName ~= nil then
if data.prioritizePower == nil then data.prioritizePower = true end
result = craftable.request(data.amount, data.prioritizePower, data.cpuName)
elseif data.prioritizePower ~= nil then
result = craftable.request(data.amount, data.prioritizePower)
else
result = craftable.request(data.amount)
end
if result == nil then return "请求制造物品失败!", nil end
local res = {
item = craftable.getItemStack(),
failed = result.hasFailed(),
computing = result.isComputing(),
done = {result = true, why = nil},
canceled = {result = true, why = nil}
}
res.done.result, res.done.why = result.isDone()
res.canceled.result, res.canceled.why = result.isCanceled()
return "请求制造物品完成", res
end
function tasks.simpleCpusInfo(_)
-- 获取所有cpu的简单信息
local list = meCpu.getCpuList(false)
for _, cpu in pairs(list) do
if cpu.id ~= nil and cpu.id ~= "" then
local _, code = http.put(config.path.cpu .. "/" .. cpu.id, {}, cpu)
if code ~= 200 then
http.post(config.path.cpu, {}, cpu)
end
end
end
end
function tasks.allCpusInfo(_)
-- 获取所有cpu的详细信息
local list = meCpu.getCpuList(true)
for _, cpu in pairs(list) do
if cpu.id ~= nil and cpu.id ~= "" then
local _, code = http.put(config.path.cpu .. "/" .. cpu.id, {}, cpu)
if code ~= 200 then
http.post(config.path.cpu, {}, cpu)
end
end
end
end
function tasks.cpuDetail(data)
-- 获取cpu的详细信息
-- data.id: cpu 的ID
if data.id == nil then return "没有提供 CPU 名称" end
local details = meCpu.getCpuDetail(data.id)
if details == nil then return "获取 " .. data.id .. " 的信息失败!" end
http.put(config.path.cpu .. "/" .. data.id, {}, details)
end
function tasks.cancelMonitor(data)
-- data.id 要取消的监控的id
monitors[data.id] = nil
end
function tasks.monitors(_)
-- data.id 要取消的监控的id
local m = {}
for key in pairs(monitors) do table.insert(m, key) end
return "current monitors", {monitors = m}
end
function tasks.cpuMonitor(_)
-- 添加cpu监控器,直到无cpu运行时移除
monitors.cpuMonitor = {
data = {},
func = function(data)
local list = meCpu.getCpuList(true)
local busy = false
for _, cpu in pairs(list) do
local flag = cpu.busy or data[cpu.id] == nil
if cpu.id ~= nil and cpu.id ~= "" and flag then
http.put(config.path.cpu .. "/" .. cpu.id, {}, cpu)
busy = true
if not cpu.busy then data[cpu.id] = true end
end
end
if not busy then monitors.cpuMonitor = nil end
end
}
end
http.init(config.baseUrl, tasks)
while true do
for _, monitor in pairs(monitors) do monitor.func(monitor.data) end
local result, message = http.executeNextTask(config.path.task)
print(result, message)
os.sleep(config.sleep)
computer.beep(500)
end