usingSystem;usingSystem.Collections.Generic;usingPollutometerWebApi.Singletons;namespacePollutometerWebApi{publicclassEmailService{publicEmailService(){}publicstaticvoidStart(){varstartTimeSpan=TimeSpan.Zero;varperiodTimeSpan=TimeSpan.FromMinutes(60);vartimer=newSystem.Threading.Timer((e)=>{varcommand="SELECT * FROM Readings "+"WHERE TimeStamp IN(SELECT MAX(TimeStamp) FROM Readings)";varreading=SqlOperator.GetReadings(command)[0];Dictionary<string,double>results=newDictionary<string,double>();results["CO"]=CalculateAqi(reading.Co,"CO");results["NO"]=CalculateAqi(reading.No,"NO");results["SO"]=CalculateAqi(reading.So,"SO");doublemax=0;stringgasName="";foreach(varresultinresults){if(result.Value>max){max=result.Value;gasName=result.Key;}}if(max>=151)EmailSender.SendEmail(gasName,max);},null,startTimeSpan,periodTimeSpan);}staticdoubleCalculateAqi(doublec,stringt){double[,,]breakpoints={{{0,4.4},{4.5,9.4},{9.5,12.4},{12.5,15.4},{15.5,30.4},{30.5,40.4},{40.5,50.4}},{{0.000,0.034},{0.035,0.144},{0.145,0.224},{0.225,0.304},{0.305,0.604},{0.605,0.804},{0.805,1.004}},{{0,0.05},{0.08,0.10},{0.15,0.20},{0.25,0.31},{0.65,1.24},{1.25,1.64},{1.65,2.04}},{{0,50},{51,100},{101,150},{151,200},{201,300},{301,400},{401,500}}};doublei,cLow=0,cHigh=0,iLow=0,iHigh=0;intg=0;switch(t){case"CO":g=0;break;case"SO":g=1;break;case"NO":g=2;break;}for(intj=0;j<7;j++){if(c>=breakpoints[g,j,0]&&c<=breakpoints[g,j,1]){cLow=breakpoints[g,j,0];cHigh=breakpoints[g,j,1];iLow=breakpoints[3,j,0];iHigh=breakpoints[3,j,1];break;}}i=(iHigh-iLow)/(cHigh-cLow)*(c-cLow)+iLow;returni;}}}