# Fog-Based Two-Phase Event Monitoring and Data Gathering in Vehicular Sensor Networks
## 4 TPEG Framework
### Algorithm Descriptions  
Algorithms 1 and 2 are the pseudocodes of the message handling at ordinary nodes, RSUs and the cloud, which illustrate the procedures of the TPEG scheme. For an event-monitoring and detection application, each ordinary node works in LCS mode and generates data (Algorithm 1, Line 1). The weight of data \(d\) on event \(e\) is \(w(d,e)\), and \(d\) will be reported to the RSU if its weight is larger than a predefined threshold \(\tau_{0}\) (Algorithm 1, Lines 2-3): When the RSU receives the LCS data from ordinary nodes, it stores the data and calculates the weight of the event based on the accumulated dataset (Algorithm 2, Lines 2-3). If the weight of the event exceeds the threshold \(\tau_{1}\), the RSU initiates an event-checking procedure (Algorithm 2: Lines 4-5). It broadcasts the event-checking command within its coverage and waits to receive the HCS messages (Algorithm 2, Lines 6-7). The command is then received by ordinary nodes. An ordinary node would create a timer \(tm(\beta)\) when it receives an event-checking command from an RSU for the first time. When the timer is fired, the node transfers to HCS mode, and more detailed data would be uploaded to the RSU (Algorithm 1, Lines 4-8). When the HCS data are received by the RSU, they would be forwarded to the cloud (Algorithm 2, Line 9).  
When the cloud receives the sensing data from the RSUs, it would store the data, check whether an event has happened by its backend event analysis system and send the final result, i.e., the EVENT message, back to the RSU (Algorithm 2, Lines 20-23). When an RSU receives the EVENT message, if the event is verified to be false, it would adjust its threshold \(\tau_{1}\) for the event through Equation (14) (Algorithm 2, Lines 14-16). \(\tau_{1}\) might be adjusted to a larger value, which makes it harder to trigger the event-checking procedure. Threshold \(\tau_{1}\) is also updated by Equation (15) as time passes by (Algorithm 2, Lines 17-18).  
```
1 work in LCS mode;
2 ifweight of data \(w(d,e)\) is greater than \(\tau_{0}\)then
3 report data to RSU;
4 ifreceive event_checking command from RSUthen
5 set a timer \(tm(\beta)\);
6 iftimer \(tm(\beta)\) firesthen
7 transfer \(self\) to HCS mode;
8 adaptively upload HCS data to RSU;
9
10
11 ifreceive threshold-setting messagethen
12 update threshold \(\tau_{0}\);
13
14 forevery update time unit passesdo
15 generate sketch of sensed data;
16 send sketch to RSU;
```  
**Algorithm 1**Handling messages at ordinary nodes.  
```
1 at RSU
2 ifreceive LCS data from ordinary nodesthen
3 accumulate data to a dataset \(D\);
4 ifweight of dataset \(w(D)\) exceeds threshold \(\tau_{1}\)then
5 initiate an event_checking procedure;
6 broadcasting event_checking command;
7 wait for receiving HCS messages;
8
9 ifreceive HCS data from ordinary nodesthen
10 forward data to the cloud;
11
12 ifreceive sketch messages from ordinary nodesthen
13 accumulate sketch data sets;
14 re-calculate threshold of \(\tau_{0}\) through (13);
15 broadcast update message of \(\tau_{0}\);
16
17 ifreceive EVENT messages from cloudthen
18 iffalse EVENTthen
19\(\tau_{1}=\tau_{1}*(1+\Delta)\);
20
21 forevery time unit (TU) passes bydo
22\(\tau_{1}=\tau_{1}*(1-\Delta*\frac{tu}{TU})\);
23
24 at cloud
25 ifreceive HCS data from RSUthen
26 accumulates and gathers the data;
27 check if an event happens;
28 send EVENT message to RSU;
```  
**Algorithm 2**Handling messages at RSUs and the cloud.  
The threshold at the ordinary nodes, denoted by \(\tau_{0}\), is calculated and updated through sketches. For every unit of update time, a _sketch_is generated at ordinary nodes and sent to the RSU (Algorithm 1, Lines 12-13), where the threshold \(\tau_{0}\) is recalculated through Equation (13) (Algorithm 2, Lines 14-12). The new threshold is then broadcast through a update message within its coverage (Algorithm 2, Line 13). The node receiving the update message from the RSU would just reset the threshold \(\tau_{0}\) (Algorithm 1, Line 10).