Play time

Finally got Raspberry Pi 2 set up with windows 10 and connected to bread board to play with electronics :slight_smile:

This is actually monitoring a build server.

Behind it all, a process in the cloud is scanning Jenkins jobs and updating a SQL database. An ASP.net website is then offering a WebApi 2 view of the build job data which this program uses.

And whats more all the code is in VB (Finally fully supported in Universal apps :slight_smile: )

' The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
Imports Newtonsoft.Json
Imports Windows.Devices.Gpio
Imports Windows.Web.Http

''' <summary>
''' An empty page that can be used on its own or navigated to within a Frame.
''' </summary>
Public NotInheritable Class MainPage
    Inherits Page

    ' Private Class Declrations
    Private LEDStatus As Integer = 0
    Private Const RED_LED_PIN As Integer = 5
    Private Const YELLOW_LED_PIN As Integer = 6
    Private Const GREEN_LED_PIN As Integer = 13
    Private red_pin As GpioPin
    Private yellow_pin As GpioPin
    Private green_pin As GpioPin
    Private redBrush As SolidColorBrush = New SolidColorBrush(Windows.UI.Colors.Red)
    Private yellowBrush As SolidColorBrush = New SolidColorBrush(Windows.UI.Colors.Yellow)
    Private greenBrush As SolidColorBrush = New SolidColorBrush(Windows.UI.Colors.Green)

    Private _timer As DispatcherTimer
    Private _flasher As DispatcherTimer

    Private _currentFlashStatus As Boolean = False

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        InitGPIO()

        AddHandler Me.Unloaded, AddressOf MainPage_Unloaded

        ' Add any initialization after the InitializeComponent() call.

        _timer = New DispatcherTimer
        _timer.Interval = TimeSpan.FromSeconds(10)
        AddHandler _timer.Tick, AddressOf Timer_Tick
        _timer.Start()
        _flasher = New DispatcherTimer
        _flasher.Interval = TimeSpan.FromMilliseconds(250)
        AddHandler _flasher.Tick, AddressOf Flasher_Tick

        GetBuildStatus()

    End Sub

    Private Sub Flasher_Tick(sender As Object, e As Object)
        If _currentFlashStatus = False Then
            AllOn()
            _currentFlashStatus = True
        Else
            AllOff()
            _currentFlashStatus = False
        End If
    End Sub

    Private Sub Timer_Tick(sender As Object, e As Object)
        GetBuildStatus()
    End Sub

    Private Sub MainPage_Unloaded(sender As Object, e As RoutedEventArgs)
        red_pin.Dispose()
        yellow_pin.Dispose()
        green_pin.Dispose()
    End Sub

    Private Sub InitGPIO()
        Dim gpio = GpioController.GetDefault

        ' Show error if there is no GPIO Controller
        If gpio Is Nothing Then
            red_pin = Nothing
            yellow_pin = Nothing
            green_pin = Nothing
            GpioStatus.Text = "There is no GPIO Controller on this device."
            Exit Sub
        End If

        red_pin = gpio.OpenPin(RED_LED_PIN)
        yellow_pin = gpio.OpenPin(YELLOW_LED_PIN)
        green_pin = gpio.OpenPin(GREEN_LED_PIN)

        ' Show error is the pin wasn't initialised properly
        If red_pin Is Nothing Or yellow_pin Is Nothing Or green_pin Is Nothing Then
            GpioStatus.Text = "There were problems initalising the GPIO pins."
            Exit Sub
        End If

        red_pin.Write(GpioPinValue.High)
        red_pin.SetDriveMode(GpioPinDriveMode.Output)
        yellow_pin.Write(GpioPinValue.High)
        yellow_pin.SetDriveMode(GpioPinDriveMode.Output)
        green_pin.Write(GpioPinValue.High)
        green_pin.SetDriveMode(GpioPinDriveMode.Output)

        GpioStatus.Text = "GPIO Pins initialised correctly."

    End Sub

    Public Sub SetMode(status As BuildStatus)
        Dim pin As GpioPin
        Dim brush As SolidColorBrush
        Select Case status
            Case BuildStatus.Failed
                pin = red_pin
                brush = redBrush
            Case BuildStatus.Passed
                pin = green_pin
                brush = greenBrush
            Case Else
                pin = yellow_pin
                brush = yellowBrush
        End Select
        If status = BuildStatus.Running Then
            _flasher.Start()
        Else
            _flasher.Stop()
            AllOff()
            If pin IsNot Nothing Then
                pin.Write(GpioPinValue.Low)
            End If
            LED.Fill = brush
        End If

    End Sub

    Public Sub AllOff()
        red_pin.Write(GpioPinValue.High)
        yellow_pin.Write(GpioPinValue.High)
        green_pin.Write(GpioPinValue.High)
    End Sub

    Public Sub AllOn()
        red_pin.Write(GpioPinValue.Low)
        yellow_pin.Write(GpioPinValue.Low)
        green_pin.Write(GpioPinValue.Low)
    End Sub

    Public Async Sub GetBuildStatus()
        Dim client As New HttpClient
        Dim jobs As New List(Of BuildJob)

        Try
            Dim result = Await client.GetStringAsync(New Uri("http://mybuildserver.com/api/BuildStatus?ModuleName=Master Build"))
            'GpioStatus.Text = result
            jobs = JsonConvert.DeserializeObject(Of List(Of BuildJob))(result)
        Catch ex As Exception
            GpioStatus.Text = ex.Message
            Exit Sub
        End Try

        If jobs(0).JobIsRunning = True Then
            jobs(0).CurrentStatus = "running"
        End If
        Dim status As BuildStatus

        Select Case jobs(0).CurrentStatus
            Case "running"
                status = BuildStatus.Running
            Case "success"
                status = BuildStatus.Passed
            Case "failure"
                status = BuildStatus.Failed
            Case Else
                status = BuildStatus.Error
        End Select
        SetMode(status)
    End Sub

End Class

Can’t wait till I can plug in Gadgetter devices.

4 Likes