Skip to content

Commit

Permalink
Show light
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Mar 21, 2019
1 parent cd26c8d commit c7ab06b
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 53 deletions.
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Release Notes

## 1.3.0 - 2019-03-21
* Show light

## 1.2.1 - 2019-03-19
* Update .NET Core to 2.2.3

Expand Down
7 changes: 5 additions & 2 deletions src/Client/ReleaseNotes.fs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
module internal ReleaseNotes

let Version = "1.2.0"
let Version = "1.3.0"

let IsPrerelease = false

let Notes = """
# Release Notes
## 1.2.0 - 2019-03-19
## 1.3.0 - 2019-03-21
* Show light
## 1.2.1 - 2019-03-19
* Update .NET Core to 2.2.3
## 1.1.16 - 2019-03-1
Expand Down
66 changes: 66 additions & 0 deletions src/PiServer/FirmwareUpdate.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module FirmwareUpdate

open System.Diagnostics
open System
open FSharp.Control.Tasks.ContextInsensitive
open Thoth.Json.Net
open ServerCore.Domain
open System.Threading.Tasks
open Paket

let firmwareTarget = System.IO.Path.GetFullPath "/home/pi/firmware"

let runFirmwareUpdate() =
let p = new Process()
let startInfo = ProcessStartInfo()
startInfo.WorkingDirectory <- "/home/pi/firmware/"
startInfo.FileName <- "sudo"
startInfo.Arguments <- "sh update.sh"
startInfo.RedirectStandardOutput <- true
startInfo.UseShellExecute <- false
startInfo.CreateNoWindow <- true
p.StartInfo <- startInfo
p.Start() |> ignore

let checkFirmware (log:log4net.ILog,tagServer) = task {
use webClient = new System.Net.WebClient()
System.Net.ServicePointManager.SecurityProtocol <-
System.Net.ServicePointManager.SecurityProtocol |||
System.Net.SecurityProtocolType.Tls11 |||
System.Net.SecurityProtocolType.Tls12

let url = sprintf @"%s/api/firmware" tagServer
let! result = webClient.DownloadStringTaskAsync(System.Uri url)

match Decode.fromString Firmware.Decoder result with
| Error msg ->
log.ErrorFormat("Decoder error: {0}", msg)
return failwith msg
| Ok firmware ->
try
log.InfoFormat("Latest firmware on server: {0}", firmware.Version)
let serverVersion = SemVer.Parse firmware.Version
let localVersion = SemVer.Parse ReleaseNotes.Version
if serverVersion > localVersion then
let localFileName = System.IO.Path.GetTempFileName().Replace(".tmp", ".zip")
log.InfoFormat("Starting download of {0}", firmware.Url)
do! webClient.DownloadFileTaskAsync(firmware.Url,localFileName)
log.Info "Download done."

if System.IO.Directory.Exists firmwareTarget then
System.IO.Directory.Delete(firmwareTarget,true)
System.IO.Directory.CreateDirectory(firmwareTarget) |> ignore
System.IO.Compression.ZipFile.ExtractToDirectory(localFileName, firmwareTarget)
System.IO.File.Delete localFileName
runFirmwareUpdate()
while true do
log.Info "Running firmware update."
do! Task.Delay 3000
()
else
if System.IO.Directory.Exists firmwareTarget then
System.IO.Directory.Delete(firmwareTarget,true)
with
| exn ->
log.ErrorFormat("Upgrade error: {0}", exn.Message)
}
54 changes: 5 additions & 49 deletions src/PiServer/PiServer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ open System.IO
open Microsoft.AspNetCore.NodeServices
open Thoth.Json.Net
open System.Threading.Tasks
open System.Diagnostics
open System.Xml
open System.Reflection
open GeneralIO
Expand Down Expand Up @@ -78,7 +77,11 @@ let rfidLoop (dispatch,nodeServices:INodeServices) = task {
use _previousButton = new Button(Unosquare.RaspberryIO.Pi.Gpio.[BcmPin.Gpio18], fun () -> dispatch PreviousMediaFile)
use _volumeDownButton = new Button(Unosquare.RaspberryIO.Pi.Gpio.[BcmPin.Gpio26], fun () -> dispatch VolumeDown)
use _volumeUpButton = new Button(Unosquare.RaspberryIO.Pi.Gpio.[BcmPin.Gpio12], fun () -> dispatch VolumeUp)
let blueLight = GeneralIO.LED(Unosquare.RaspberryIO.Pi.Gpio.[BcmPin.Gpio20])
let yellowLight = GeneralIO.LED(Unosquare.RaspberryIO.Pi.Gpio.[BcmPin.Gpio21])
let allLights = [| blueLight; yellowLight|]

let! _ = allLights |> Array.map (fun l -> l.Blink(10)) |> Task.WhenAll
log.InfoFormat("Waiting for RFID cards or NFC tags...")
while true do
let! token = nodeServices.InvokeExportAsync<string>("./read-tag", "read", "tag")
Expand Down Expand Up @@ -133,53 +136,6 @@ let previousFile (model:Model,token:string) = task {
| Ok tag -> return tag
}

let mutable nextFirmwareCheck = DateTimeOffset.MinValue


let checkFirmware (model:Model) = task {
use webClient = new System.Net.WebClient()
System.Net.ServicePointManager.SecurityProtocol <-
System.Net.ServicePointManager.SecurityProtocol |||
System.Net.SecurityProtocolType.Tls11 |||
System.Net.SecurityProtocolType.Tls12

let url = sprintf @"%s/api/firmware" model.TagServer
let! result = webClient.DownloadStringTaskAsync(System.Uri url)

match Decode.fromString Firmware.Decoder result with
| Error msg ->
log.ErrorFormat("Decoder error: {0}", msg)
return failwith msg
| Ok firmware ->
try
nextFirmwareCheck <- DateTimeOffset.UtcNow.Add model.FirmwareUpdateInterval
log.InfoFormat("Latest firmware on server: {0}", firmware.Version)
let serverVersion = Paket.SemVer.Parse firmware.Version
let localVersion = Paket.SemVer.Parse ReleaseNotes.Version
if serverVersion > localVersion then
let localFileName = System.IO.Path.GetTempFileName().Replace(".tmp", ".zip")
log.InfoFormat("Starting download of {0}", firmware.Url)
do! webClient.DownloadFileTaskAsync(firmware.Url,localFileName)
log.Info "Download done."

if System.IO.Directory.Exists FirmwareUpdate.firmwareTarget then
System.IO.Directory.Delete(FirmwareUpdate.firmwareTarget,true)
System.IO.Directory.CreateDirectory(FirmwareUpdate.firmwareTarget) |> ignore
System.IO.Compression.ZipFile.ExtractToDirectory(localFileName, FirmwareUpdate.firmwareTarget)
System.IO.File.Delete localFileName
FirmwareUpdate.runFirmwareUpdate()
while true do
log.Info "Running firmware update."
do! Task.Delay 3000
()
else
if System.IO.Directory.Exists FirmwareUpdate.firmwareTarget then
System.IO.Directory.Delete(FirmwareUpdate.firmwareTarget,true)
with
| exn ->
log.ErrorFormat("Upgrade error: {0}", exn.Message)
}

let getStartupAction (model:Model) = task {
use webClient = new System.Net.WebClient()
let url = sprintf @"%s/api/startup" model.TagServer
Expand Down Expand Up @@ -246,7 +202,7 @@ let update (msg:Msg) (model:Model) =
{ model with Playing = None }, Cmd.none

| CheckFirmware ->
model, Cmd.ofTask checkFirmware model FirmwareUpToDate Err
model, Cmd.ofTask FirmwareUpdate.checkFirmware (log,model.TagServer) FirmwareUpToDate Err

| Noop _ ->
model, Cmd.none
Expand Down
2 changes: 1 addition & 1 deletion src/PiServer/PiServer.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
<Compile Include="..\Client\ReleaseNotes.fs" />
<Compile Include="..\Shared\Shared.fs" />
<Compile Include="Utils.fs" />
<Compile Include="SemVer.fs" />
<Compile Include="FirmwareUpdate.fs" />
<Compile Include="Elmish.Audio.fs" />
<Compile Include="GeneralIO.fs" />
<Compile Include="SemVer.fs" />
<Compile Include="PiServer.fs" />
</ItemGroup>
<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/PiServer/Utils.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Utils
open System.Net.NetworkInformation


let getMACAddress() =
NetworkInterface.GetAllNetworkInterfaces()
|> Seq.filter (fun nic ->
Expand Down

0 comments on commit c7ab06b

Please sign in to comment.