Type123.fs 2.96 KB
Newer Older
Dag Brattli's avatar
Dag Brattli committed
1
namespace NAisParser
2 3 4 5

open System
open FParsec

Dag Brattli's avatar
Dag Brattli committed
6
open NAisParser.Core
Dag Brattli's avatar
Dag Brattli committed
7

Dag Brattli's avatar
Dag Brattli committed
8

9
module Type123 =
10 11
    let messageType123 type' repeat mmsi status turn speed accuracy lon
        lat course heading second maneuver raim radio: MessageType123=
Dag Brattli's avatar
Dag Brattli committed
12
        {
13
            Type = type'
Dag Brattli's avatar
Dag Brattli committed
14 15 16 17 18 19
            Repeat = repeat;
            Mmsi = mmsi;
            Status = status;
            RateOfTurn = turn;
            SpeedOverGround = speed;
            PositionAccuracy = accuracy;
Dag Brattli's avatar
Dag Brattli committed
20 21
            Longitude = lon;
            Latitude = lat;
Dag Brattli's avatar
Dag Brattli committed
22
            CourseOverGround = course;
23 24 25
            TrueHeading = heading;
            TimeStamp = second;
            ManeuverIndicator = maneuver;
26 27
            RaimFlag = raim;
            RadioStatus = radio;
Dag Brattli's avatar
Dag Brattli committed
28 29
        }

30
    let defaultMessageType123: MessageType123= {
31
        Type = 0uy;
Dag Brattli's avatar
Dag Brattli committed
32
        Repeat = 0uy;
33
        Mmsi = 0;
Dag Brattli's avatar
Dag Brattli committed
34
        Status = NavigationStatus.NotDefined;
35
        RateOfTurn = 128.0;
36
        SpeedOverGround = 0;
37
        PositionAccuracy = false;
38 39
        Longitude = 181.0;
        Latitude = 91.0;
Dag Brattli's avatar
Dag Brattli committed
40
        CourseOverGround = 0.0;
41 42 43
        TrueHeading = 511;
        TimeStamp = 0;
        ManeuverIndicator = ManeuverIndicator.NoSpecialManeuver;
44 45
        RaimFlag = false;
        RadioStatus = 0;
46 47 48
    }

    let parseRateOfTurn =
49
        let squareSigned x = x * x * float(Math.Sign(float x))
Dag Brattli's avatar
Dag Brattli committed
50
        Core.parseSByte
Dag Brattli's avatar
Dag Brattli committed
51
        |>> fun x -> squareSigned((float x) / 4.733)
52 53 54

    let parseSpeedOverGround =
        Core.parseBits 10
Dag Brattli's avatar
Dag Brattli committed
55
        |>> fun x -> Convert.ToInt32(x, 2)
56 57

    let parsePositionAccuracy =
58
        Core.parseBool
59

Dag Brattli's avatar
Dag Brattli committed
60 61 62
    let parseCourseOverGround =
        Core.parseBits 12
        |>> fun x -> Convert.ToInt32(x, 2)
63
        |>> fun x -> float(x) / 10.0
Dag Brattli's avatar
Dag Brattli committed
64

65 66 67 68
    let parseStatus =
        Core.parseBits 4
        |>> (fun x ->
            let value = Convert.ToInt32(x, 2)
Dag Brattli's avatar
Dag Brattli committed
69 70
            enum<NavigationStatus>(value)
        )
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85
    let parseTrueHeading =
        Core.parseBits 9
        |>> fun x -> Convert.ToInt32(x, 2)

    let parseTimeStamp =
        Core.parseBits 6
        |>> fun x -> Convert.ToInt32(x, 2)

    let parseManeuverIndicator =
        Core.parseBits 2
        |>> fun x ->
            let value = Convert.ToInt32(x, 2)
            enum<ManeuverIndicator>(value)

86
    let parseType123  : Parser<_> =
87 88
        // A little repetitive, but better to do it here at declaration time
        Common.parseType3 (Common.toPaddedBinary 1)  (Common.toPaddedBinary 2) (Common.toPaddedBinary  3)
89

90 91
    let parseMessageType123: Parser<_> =
        preturn messageType123
92
        <*> parseType123
Dag Brattli's avatar
Dag Brattli committed
93 94
        <*> Common.parseRepeat
        <*> Common.parseMmsi
Dag Brattli's avatar
Dag Brattli committed
95 96 97 98
        <*> parseStatus
        <*> parseRateOfTurn
        <*> parseSpeedOverGround
        <*> parsePositionAccuracy
99 100
        <*> Common.parseLongitude
        <*> Common.parseLatitude
Dag Brattli's avatar
Dag Brattli committed
101
        <*> parseCourseOverGround
102 103 104
        <*> parseTrueHeading
        <*> parseTimeStamp
        <*> parseManeuverIndicator
105 106 107
        <*  Common.parseSpare
        <*> Common.parseRaimFlag
        <*> Common.parseRadioStatus
Dag Brattli's avatar
Dag Brattli committed
108
        |>> Type123