# A Cooja-Based Tool for Coverage and Lifetime Evaluation in an In-Building Sensor Network+
## 4 Implementation
### Use Cases
#### 4.2.2 Use Case II: Local-Maintain  
In the localised algorithm, each node only knows the locations of its direct neighbours, their sensing range and whether they are in the same room or not. Nodes do not have knowledge regarding the layout of the building and obstacles (walls and doors). Each node decides locally to be active or redundant based on its priority. The complexity of the localised algorithm is \(O(|V|)\). The local computation starts from a node that has the highest priority. Priority is computed locally. In the local initial network configuration (Algorithm 3), it is based on the node's remaining energy and the distance to a sink, while in local-maintain (Algorithm 4), it is based on the node's remaining energy and the distance to a dead node. In the priority calculation, \(\alpha\) and \(\beta\) are weights that are assigned to the residual energy and the distance. In this paper, we set \(\alpha=0.5\) and \(\beta=0.5\). ```
0:\(V\), local_coverage_threshold
0:\(status\) for all \(v\in V\)do \(v\).priority \(\leftarrow\)1/ \(\left[1-\left(\alpha\times\frac{v.remaining\_energy}{v.initial\_energy} \right)-\left(\beta\times\frac{1}{d(sink,\alpha)}\right)\right]\) endfor for all\(v\in V\) in decreasing order of \(priority\)do  Calculate \(v\).overlap_coverage \(\leftarrow\) percentage of \(v\)'s coverage area that is overlap with every \(w\)'s coverage area for all \(w\in v\).neighbour that satisfies: (1) \(w\).room_1D = \(v\).room_1D (2) \(d(v,w)<v\).sense_range + \(w\).sense_range if\(v\).overlap_coverage \(<\) local_coverage_threshold then \(v\).status \(\leftarrow\) on endif endfor
```  
**Algorithm 3** Local initial network configuration. A node \(v\) locally decides to be active if the overlap coverage is below the local coverage threshold. \(v\)'s overlap coverage is calculated as the percentage of \(v\)'s coverage area that is overlapped with its neighbours' coverage area. To preserve connectivity, a node also needs to check if it is connected to any other active nodes or not. To compute connectivity locally, we utilise the Span method [18]. Span is a decentralised algorithm to activate extra nodes for one-connectivity. With Span, a node decides to become active if two of its neighbours cannot reach each other, either directly or via one or two active nodes. In both Algorithms 3 and 4, \(V\) is implemented as a list of objects, where an object \(v\in V\) or \(x\in V\) has ID, position_x, position_y, status, room_ID, power_level, sense_range, initial_energy, remaining_energy, a set of neighbours, priority and overlap_coverage. The set of neighbours is implemented the same as \(V\). ```
0:\(V\), dead_node x, local_coverage_threshold
0:\(status\) for all \(v\in x\).neighbour \(A\leftarrow\emptyset\) for all\(v\in x\).neighbour that satisfies: (1)\(v\).room_ID = \(x\).room_ID (2)\(d(x,v)<x\).sense_range + \(v\).sense_range do \(v\).priority \(\leftarrow\)\(1/[1-(\alpha\times\frac{v\).remaining_energy}{v\).initial_energy})-\) \((\beta\times\frac{x\).sense_range + \(v\).sense_range - \(d(x,v)}{2\times x\).sense_range })]\) \(A\gets A\cup\{v\}\) endfor for all\(v\in A\) in decreasing order of prioritydo  Calculate \(v\).overlap_coverage \(\leftarrow\) percentage of \(v\)'s coverage area that is overlap with every \(w\)'s coverage area for all \(w\in v\).neighbour that satisfies: (1)\(w\).room_ID = \(v\).room_ID (2)\(d(x,w)<x\).sense_range + \(w\).sense_range if\(v\).overlap_coverage \(<\)local_coverage_threshold then \(v\).status \(\leftarrow\) on endif endfor
```  
**Algorithm 4** Local-maintain. The four algorithms try to achieve the desired coverage by turning on some nodes in the network. If no nodes satisfy the coverage and connectivity requirements when the network operates, the quality of coverage decreases over time, as will be shown in the simulation results in Section 6. WSN-Maintain then notifies users by reporting failures that require physical maintenance.