-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPostInventory.lua
71 lines (64 loc) · 1.85 KB
/
PostInventory.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
function PrintTable(table)
for key, value in pairs(table) do
print(key .. " = " .. tostring(value))
end
end
function SerializeInventory(side)
local chest = peripheral.wrap(side)
local stacks = chest.getAllStacks()
local totals = SumInventory(stacks)
PrintTable(totals)
local result = ''
for key, value in pairs(totals) do
if string.len(result) > 0 then result = result .. '&' end
result = result .. key .. '=' .. value
end
return result
end
function SumInventory(stacks)
local totals = {}
for key, value in pairs(stacks) do
if totals[value.name] == nil then
totals[value.name] = value.qty
else
totals[value.name] = totals[value.name] + value.qty
end
end
return totals
end
function PostChestContents(value)
local content = SerializeInventory(value)
print(content)
local headers = { ["content-type"] = "application/x-www-form-urlencoded" }
local response = http.request ('http://yavorg-droid.azure-mobile.net/api/updateinventory', content, headers)
print(response)
end
function FindAdjacentTrappedChest()
local peripheralNames = peripheral.getNames()
for key, value in pairs(peripheralNames) do
local type = peripheral.getType(value)
local match = string.match(type, "container_chest")
if match then
return value
end
end
end
chestDirection = FindAdjacentTrappedChest()
print("Starting to monitor chest...")
while true do
local event = os.pullEvent("redstone")
if (redstone.getInput(chestDirection)) then
--Got a signal from the chest
print("Chest opened at " .. os.time())
chestOpen = true
while chestOpen do
local event = os.pullEvent("redstone")
if (not redstone.getInput(chestDirection)) then
print("Chest closed at " .. os.time())
chestOpen = false
end
end
-- Send the final chest contents to the Mobile Service
PostChestContents(chestDirection)
end
end