package Ġde . and rena . sensor app . validation Ċ Ċ import Ġcom . microsoft . azure . functions . ExecutionContext Ċ import Ġcom . microsoft . azure . functions . Output Binding Ċ import Ġcom . microsoft . azure . functions . annotation . FunctionName Ċ import Ġcom . microsoft . azure . functions . annotation . Queue Output Ċ import Ġcom . microsoft . azure . functions . annotation . Queue Trigger Ċ import Ġde . and rena . sensor app . sensor . Sensor Repository Ċ import Ġde . and rena . util . json . decode Json Ċ import Ġjava . time . Offset DateTime Ċ Ċ type alias ĠValidate Output Ġ= ĠOutput Binding < String > Ċ Ċ @ FunctionName (" Sensor Validation ") Ċ fun Ġvalidate ( ĊĠĠĠ Ġ@ Queue Trigger ( name Ġ= Ġ" ag gregated Sensor Data ", Ġqueue Name Ġ= Ġ" ag gregated - sensor - data ", Ġconnection Ġ= Ġ" Azure Web Jobs Storage ") ĊĠĠĠ Ġaggregated Sensor Data : ĠString , ĊĠĠĠ Ġ@ Queue Output ( name Ġ= Ġ" output ", Ġqueue Name Ġ= Ġ" validated - sensor - data ", Ġconnection Ġ= Ġ" Azure Web Jobs Storage ") ĊĠĠĠ Ġoutput : ĠValidate Output , ĊĠĠĠ Ġcontext : ĠExecutionContext , Ċ ) Ġ{ ĊĠĠĠ Ġval Ġdecoded Ag gregated Sensor Data Ġ= Ġtry Ġ{ ĊĠĠĠĠĠĠĠ Ġaggregated Sensor Data . decode Json < Ag gregated Sensor Data >() ĊĠĠĠ Ġ} Ġcatch Ġ( ex : ĠIllegalArgumentException ) Ġ{ ĊĠĠĠĠĠĠĠ Ġcontext . logger . se vere (" Unable Ġto Ġdecode ĠAg gregated Sensor Data =${ ag gregated Sensor Data }: Ġ${ ex . message }") ĊĠĠĠĠĠĠĠ Ġreturn ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġwhen Ġ( val Ġvalidation Result Ġ= Ġvalidate AndUpdate Last Seen ( decoded Ag gregated Sensor Data , Ġcontext )) Ġ{ ĊĠĠĠĠĠĠĠ Ġis ĠValidation Result . Error Ġ-> Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcontext . logger . se vere (" Validation Ġfailed Ġfor ĠAg gregated Sensor Data =${ ag gregated Sensor Data } Ġ${ validation Result . message }") ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ ĠValidation Result . Ok Ġ-> Ġcontext . logger . fine (" Successfully Ġvalidated Ġaggregated Sensor Data . Ġ$ ag gregated Sensor Data ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoutput . value Ġ= Ġaggregated Sensor Data Ċ } Ċ Ċ private Ġfun Ġvalidate AndUpdate Last Seen ( ag gregated Sensor Data : ĠAg gregated Sensor Data , Ġcontext : ĠExecutionContext ): ĠValidation Result Ġ{ ĊĠĠĠ Ġcontext . logger . fine (" Valid ating Ġaggregated Sensor Data . Ġ$ ag gregated Sensor Data ") ĊĊĠĠĠ Ġval Ġsensor Ġ= ĠSensor Repository . getById And Type ( ag gregated Sensor Data . sensor Box Id , Ġaggregated Sensor Data . sensor Type ) ĊĠĠĠĠĠĠĠ Ġ?: Ġreturn ĠValidation Result . Error (" No Ġsensor Ġfound Ġfor Ġsensor Ġbox Ġid =${ ag gregated Sensor Data . sensor Box Id } Ġand Ġtype Ġ${ ag gregated Sensor Data . sensor Type }") ĊĊĠĠĠ Ġsensor . last Seen DateTime Ġ= ĠOffset DateTime . now () ĊĠĠĠ ĠSensor Repository . update ( sensor ) ĊĊĠĠĠ Ġif Ġ( ag gregated Sensor Data . isValid For ( sensor )) ĊĠĠĠĠĠĠĠ Ġreturn ĠValidation Result . Ok ĊĊĠĠĠ ĠSensor Alarm Repository . insert ( Sensor Alarm . invalid Data ( sensor )) ĊĠĠĠ Ġreturn ĠValidation Result . Error (" Invalid Ġdata ") Ċ } Ċ Ċ sealed Ġclass ĠValidation Result Ġ{ ĊĠĠĠ Ġobject ĠOk Ġ: ĠValidation Result () ĊĠĠĠ Ġclass ĠError ( val Ġmessage : ĠString ) Ġ: ĠValidation Result () Ċ } Ċ