-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrowFactory.sol
175 lines (149 loc) · 5.52 KB
/
GrowFactory.sol
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
pragma solidity ^0.4.19;
contract BucketNet {
// Grow identifying information, environmental data, component states
struct Grow {
uint id;
string name;
string ipAddress;
uint temp;
uint humidity;
uint soilMoisture;
uint lightIntensity;
bool intakeActive;
bool exhaustActive;
bool lightActive;
}
// Grow added
event GrowAdded(uint id, string name);
// Temperature changed
event TempChange(uint id, uint temp);
// Humidity changed
event HumidityChange(uint id, uint humidity);
// Soil Moisture changed
event SoilMoistureChange(uint id, uint soilMoisture);
// Light Intensity changed
event LightIntensityChange(uint id, uint lightIntensity);
// IP Address changed
event IpChange(uint id, string ipAddress);
// Intake toggled on/off
event IntakeToggled(uint id, bool intakeActive);
// Exhaust toggled on/off
event ExhaustToggled(uint id, bool exhaustActive);
// Water toggled on/off
event WaterToggled(uint id);
// Light toggled on/off
event LightToggled(uint id, bool lightActive);
// Associate Grow with an owner address
mapping(uint => address) public growToOwner;
// Associate Grow with a bucket address
mapping(uint => address) public growToBucket;
// Associate Owner with a Grow count
mapping(address => uint) public ownerGrowCount;
// Associate Owner with growIds
mapping(address => uint[]) public ownerToGrow;
// Unique Grow identifier
uint currentGrowId = 0;
// Collection of Grows
Grow[] public grows;
// Create new Grow
// Assign to sender address
// Trigger GrowAdded event
// Increment unique Grow identifier
function createGrow(string _newName, address _newBucketAddress) public {
grows.push(Grow(currentGrowId, _newName, '', 0, 0, 0, 0, false, false, false));
growToOwner[currentGrowId] = msg.sender;
growToBucket[currentGrowId] = _newBucketAddress;
ownerGrowCount[msg.sender] += 1;
ownerToGrow[msg.sender].push(currentGrowId);
GrowAdded(currentGrowId, _newName);
currentGrowId++;
}
// Address permission helper
function isPermittedAddress(uint _growId, address _senderAddress) private constant returns (bool) {
return _senderAddress == growToOwner[_growId] || _senderAddress == growToBucket[_growId];
}
// Get number of Grows for an Owner
function getGrowCount(address _ownerAddress) public constant returns (uint) {
return ownerGrowCount[_ownerAddress];
}
// Get Grow at index for Owner
function getGrowIdByIndex(uint _index) public constant returns (uint) {
return ownerToGrow[msg.sender][_index];
}
// Setters
// Check that sender owns the Grow
// Update stored value for a Grow
// Trigger change event
function setTemp(uint _growId, uint _newTemp) public {
require(isPermittedAddress(_growId, msg.sender));
grows[_growId].temp = _newTemp;
TempChange(_growId, _newTemp);
}
function setHumidity(uint _growId, uint _newHumidity) public {
require(isPermittedAddress(_growId, msg.sender));
grows[_growId].humidity = _newHumidity;
HumidityChange(_growId, _newHumidity);
}
function setSoilMoisture(uint _growId, uint _newSoilMoisture) public {
require(isPermittedAddress(_growId, msg.sender));
grows[_growId].soilMoisture = _newSoilMoisture;
SoilMoistureChange(_growId, _newSoilMoisture);
}
function setLightIntensity(uint _growId, uint _newLightIntensity) public {
require(isPermittedAddress(_growId, msg.sender));
grows[_growId].lightIntensity = _newLightIntensity;
LightIntensityChange(_growId, _newLightIntensity);
}
function setIpAddress(uint _growId, string _newIpAddress) public {
require(isPermittedAddress(_growId, msg.sender));
grows[_growId].ipAddress = _newIpAddress;
IpChange(_growId, _newIpAddress);
}
function toggleIntake(uint _growId) public {
require(isPermittedAddress(_growId, msg.sender));
bool newIntakeActive = !grows[_growId].intakeActive;
grows[_growId].intakeActive = newIntakeActive;
IntakeToggled(_growId, newIntakeActive);
}
function toggleExhaust(uint _growId) public {
require(isPermittedAddress(_growId, msg.sender));
bool newExhaustActive = !grows[_growId].exhaustActive;
grows[_growId].exhaustActive = newExhaustActive;
ExhaustToggled(_growId, newExhaustActive);
}
function toggleWater(uint _growId) public {
require(isPermittedAddress(_growId, msg.sender));
WaterToggled(_growId);
}
function toggleLight(uint _growId) public {
require(isPermittedAddress(_growId, msg.sender));
bool newLightActive = !grows[_growId].lightActive;
grows[_growId].lightActive = newLightActive;
LightToggled(_growId, newLightActive);
}
// Getters
function getTemp(uint _growId) public constant returns (uint) {
return grows[_growId].temp;
}
function getHumidity(uint _growId) public constant returns (uint) {
return grows[_growId].humidity;
}
function getSoilMoisture(uint _growId) public constant returns (uint) {
return grows[_growId].soilMoisture;
}
function getLightIntensity(uint _growId) public constant returns (uint) {
return grows[_growId].lightIntensity;
}
function getIpAddress(uint _growId) public constant returns (string) {
return grows[_growId].ipAddress;
}
function isIntakeActive(uint _growId) public constant returns (bool) {
return grows[_growId].intakeActive;
}
function isExhaustActive(uint _growId) public constant returns (bool) {
return grows[_growId].exhaustActive;
}
function isLightActive(uint _growId) public constant returns (bool) {
return grows[_growId].lightActive;
}
}