# A Cooja-Based Tool for Coverage and Lifetime Evaluation in an In-Building Sensor Network+
## 4 Implementation
### Use Cases
#### 4.2.1 Use Case I: Greedy-Maintain  
In the centralised greedy algorithm, the sink/base station has global information about the network, the layout of the building and the obstacles, such as walls and doors. At the moment, we assume a single sink scenario. However, implementing multiple sink cases would not be a big challenge. One approach is by choosing a supersink that has a connection to the other sinks. By doing this, we reduce the problem of multiple sinks to the problem of a single sink. In the greedy initial network configuration, the computation is performed centrally, room by room, starting from a room that has the closest node to the sink/base station. The algorithm identifies nodes that are required to be active in order to satisfy the coverage and connectivity requirements for each room, while the rest are deemed as redundant. The pseudocode for the greedy initial network configuration is given in Algorithm 1. It takes as input the set \(V\) of sensor nodes that are initially off  
Figure 3: WSN-Maintain runs in parallel with Cooja. and the local coverage threshold, _i.e._, a room's required coverage. In WSN-Maintain, \(V\) is implemented as a list of objects. Each \(v\in V\) stores the following information: ID, position_x, position_y, status, room_ID, power_level, sense_range, initial_energy, remaining_energy and a set of neighbours. These variables are self-explanatory. The procedure is repeated until all rooms are processed. In each iteration, to select a room to be processed, we firstly find a node \(v\) whose status is off and has the smallest distance to an active node so as to guarantee network connectivity. Initially, only the sink is active, so the first room to be processed must be the room where the sink is or the nearest room if the sink's room has no other nodes. The room that needs to be processed is the room where \(v\) is. We then need to activate nodes in that room until the room's local coverage satisfies the local coverage threshold. For these purposes, we greedily find an inactive node in that room that has the largest contribution to the room's local coverage, has the highest remaining energy and needs the smallest number of active relay nodes. The room's local coverage (\(current\_local\_coverage\)) is calculated as the percentage of area in the room that is covered by active sensor nodes. Relay nodes are sensor nodes that need to be turned on for network connectivity; otherwise, the active network is disconnected. To find how many relay nodes are needed by each inactive node, we build a spanning tree using breadth first search. The complexity of this algorithm with breadth first search (\(O(|E|)\)) is \(O(|V||E|)\), where \(|V|\) is the number of nodes and \(|E|\) is the number of links. ```
0:\(V\), local_coverage_threshold
0:\(status\) for all \(v\in V\) while all rooms are not processed do  Find \(v\in V\) that satisfies: (1) \(v.status=\) off (2) \(v\) has the smallest distance to an active node \(i\leftarrow v.room\_ID\) while\(i.current\_local\_coverage<local\_coverage\_threshold\)do  Find \(v\in V\) that satisfies: (1) \(v.room\_ID=i\) (2) \(v.status=\) off (3) \(v\) has the largest contribution to \(i.current\_local\_coverage\) (4) \(v\) has the highest \(remaining\_energy\) (5) \(v\) needs the smallest number of active relays \(v.status\leftarrow\) on  Calculate \(i.current\_local\_coverage\) endwhile  Turn on all necessary relays for connectivity endwhile
```  
**Algorithm 1** Greedy initial network configuration. The pseudocode for greedy-maintain is given in Algorithm 2. It takes as input the set \(V\) of sensor nodes, a dead node \(x\) and the local coverage threshold. \(V\) is implemented as a list of objects, where an object \(x\in V\) has ID, position_x, position_y, status, room_ID, power_level, sense_range, initial_energy, remaining_energy and a set of neighbours. When \(x\) dies, the room's local coverage may fall below the local coverage threshold, and the network may be disconnected.