## 4 Functional Model of the Framework
### Level 1--Traffic Congestion at Intersections  
The problem of traffic congestion at intersections is the seeding level of a traffic jam, especially at peak times. The solution is to rely on intelligent, dynamic traffic signals adapted to the behaviour of traffic on each side of the intersection instead of static traffic lights that give a fixed time slot to traffic on each street. An alternative solution is building bridges or tunnels and this is always the best solution if available, but it is very expensive and may not be possible in all situations.  
To create a smart traffic light, we need to know the number of vehicles in each street. We can acquire this information from three different types of sources. The first option is to rely on control cameras (either a camera in each street at the intersection or a moving camera that takes successive images). This is considered the easiest option in terms of its application, cost, and maintenance. This does not affect traffic during its installation or maintenance. However, the accuracy of the images may be influenced by luminance factors such as weather and lighting. Nevertheless, the threshold for tolerating the noise can be customized as we are interested in estimating congestion based on the size and number of vehicles. The second option is to rely on sensors on the roadside and the center of each road, separated by different distances, to measure the number of vehicles passing through. This ensures accuracy but may disturb traffic flow during installation and maintenance. The third option is to rely on assigning a unique radio ID to each vehicle, so that each vehicle connects to the nearest receiving unit (may be a traffic light). The location of each vehicle and the number of vehicles on each route can therefore be ascertained. Although this is effective, it may not be available in many countries for privacy reasons.  
It is also possible to use more than one of these sources together to increase the level of availability and reliability, knowing that all the data will reach the fog node responsible for the intersection (which may be a traffic signal light) in order to process the data such as the images captured by cameras, calculate the congestion level, the number of vehicles on each route, and so on. Accordingly, the timing of the signals can be controlled so that a street with a huge number of waiting vehicles is given additional time at the expense of a side where there are no or few waiting vehicles. Indeed, starvation of each street is avoided by limiting the waiting time for any route to a specific amount. Moreover, all data pertaining to the congestion in each route and at each intersection are sent to the cloud for later use in the future planning of the city network and the flow of vehicles within it.  
In our implementation of the framework, we used cameras at traffic lights, WSNs, and RFID to collect the data about traffic flow from the streets, i.e., the number of cars. This data are aggregated and fed to our proposed algorithm for a smart traffic light (STL), discussed below in Listing 1.  
```
1 Input:Repeat_Period=X//Scanperiod,say10s
2 Output:ResultsR[]//Numberofvehiclesinteachstreet
3 Initialization:Max_Time=120;//Timallowedforegreensignal
4 Street1.WaitingTime=0;
5 Street2.WaitingTime=0;
6 Street3.WaitingTime=0;
7 Street4.WaitingTime=0;
8
9 Flag_MaxWait=0;
10
11 Start:RepeatexecutioneachXseconds//say10s
12 Step1:Takenewimagesofallstreets
13 Step2:ForeachstreetinStreets
14R[Street]=Get_Objects_Num(street)//counthenumberofvehicles/street
15 If(Street.WaitingTime>=Max_Time)
16 Street.isGreen=True;//Openstreet
17 Street.WaitingTime=0;
18 Plax_MaxWait=1;
19 Else
20 Street.WaitingTime=Street.waitingTime+X;
21 Flax_MaxWait=0;
22 EndFor;
23
24 Step3:If(!Flag_MaxWait)
25 MaxStreet=Find_Max_Congestion(R);
26 MaxSteet.isGreen=True;//Openstreet
27 MaxSteet.WaitingTime=0;
28 END
29 END
30 Step4:GootStart
```  
**Listing 1** Proposed algorithm for a smart traffic light (STL).