Public/Convert-CsvToMessages.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<#
    .SYNOPSIS
    Converts a CSV file to message objects for posting to salesforce APIs

    .DESCRIPTION
    The CSV file must contain the following columns:

        "Type" - either "Measurement" or "Survey"
        "MinuteOffset" - the offset in minutes from StartDate
        "Name" - Either the name of the measurement type or the name of the survey

        The additional columns required are determined by the value of each row in the Type and Name columns

        For Type="Survey":

            The "AnswerFile" column is required. It must specify the name of the file that contains the survey responses.
            See help on Get-SurveyResponsesFromCsv for a description of the format of this file.

            Example:
            "MinuteOffset","Type","Name","AnswerFile"
            "0","Survey","Blood Pressure Change Survey"

        For Type="Measurement":

            The Measurment name must match a measurement name eCC phecc__Measurement_Type__c table in the org
            A column can be added for each Metric name from the 'phecc__Metric__c' table in the org
            If the column is present the value in this column is used for the message
            And additional column is required to specify the units for the metric. The column must be named '<metric name>:units"
            The value must must be a valid unit for the metric from the 'phecc__Unit__c' table in the org

            Example:
            "MinuteOffset","Type","Name","Pulse (Average) - Wearable","Pulse (Average) - Wearable:units"
            "0","Measurement","Wearable","71","Bpm"

    .INPUTS
    None. You cannot pipe objects to Get-CdrPatients.

    .OUTPUTS
    None.

    .PARAMETER CsvFile
    The CSV file to read sample data. Defaults to "data.csv"

    .PARAMETER Patient
    The patient object

    .PARAMETER StartDate
    The StartDate to start inserting the messages using the minute offset of each message

    .PARAMETER DeviceType
    The device type to include in all the messages

    .PARAMETER DeviceId
    The device id to include in all the messages. Defaults to "1217427502"

    .PARAMETER Gateway
    The Gateway to include in all the messages. Defaults to "BioHub"

    .PARAMETER GatewayDeviceId
    The Gateway device id to include in all the messages. Defaults to "BioHub:1217427502"

    .EXAMPLE
    C:\PS>
    .LINK
    .NOTES
#>

function Convert-CsvToMessages {
    param([String]$CsvFile = "data.csv", [PsObject]$Patient, [System.DateTime]$StartDate, $DeviceType, $DeviceId = "1217427502", $Gateway = "BioHub", $GatewayDeviceId = "BioHub:1217427502")

    $def = Get-MetricDefinitions
    $surveyDefs = Get-SurveyDefinitions

    if (-not $PSBoundParameters.ContainsKey('StartDate')) {
        $StartDate = Get-Date
    }

    $Start = Get-Date -Year $StartDate.year -Month $StartDate.month -Day $StartDate.Day -Hour $StartDate.Hour -Minute $StartDate.Minute -Second 0 -Millisecond 0

    $Rows = Import-Csv -Path $CsvFile
    foreach($Row in $Rows) {

        $DateTime = $Start.AddMinutes($Row.MinuteOffset)

        switch ($Row.Type)
        {
            "Measurement" {
                $measurement = $def.measurements | Where-Object { $_.Name -eq $Row.Name}
                if (-not $measurement) {
                    throw "Measurement name $($Row.Name) not found. File $($CsvFile) is invalid."
                }
                $p = @{
                    PatientId = $Patient.sfPatient.Id
                    DateTime = $DateTime
                    DeviceType = $measurement.phecc__Device_Type_Code__c
                    DeviceId = (New-Guid).Guid
                    Gateway = (New-Guid).Guid
                    GatewayDeviceId =(New-Guid).Guid
                    ExternalObsId = (New-Guid).Guid
                }
                $observation = New-Observation @p
                $scalars = @()
                $measurementMetrics = $def.measurementMetrics | Where-Object { $_.phecc__Measurement_Type__c -eq $measurement.Id }
                if (-not $measurementMetrics) {
                    throw "Metrics not found for measurement $($measurement.Name). File $($CsvFile) is invalid."
                }
                foreach ($measurementMetric in $measurementMetrics) {
                    $metrics = $def.metrics | Where-Object { $_.Id -eq $measurementMetric.phecc__Metric__c }
                    foreach ($metric in $metrics) {
                        $valueToUse = $Row."$($metric.Name)"
                        if ($valueToUse) {
                            $metricUnits = $def.metricUnits | Where-Object { $_.phecc__Metric__c -eq $metric.Id }
                            foreach ($metricUnit in $metricUnits) {
                                $unitToUse = $Row."$($metric.Name):units"
                                $unit = $def.units | Where-Object { $_.Id -eq $metricUnit.phecc__Unit__c -and $_.Name -eq $unitToUse}
                                if ($unit) {
                                    $params = @{
                                        MetricTypeCode = $metric.phecc__Metric_Type_Code__c
                                        UnitTypeCode = $unit.phecc__Unit_Type_Code__c
                                        Value = $valueToUse
                                        DateTime = $DateTime
                                    }
                                    $scalars += (New-Scalar @params)
                                }
                            }
                        }
                    }
                }
                $Body = New-ObservationScalars -Observation $observation -Scalars $scalars
                Write-Information ($Body | ConvertTo-Json -Depth 100)
                Write-Output @{
                    body = $Body
                    endpoint = "/services/apexrest/phecc/scalarObservation/postObservationWithScalars"
                }
            }
            "Survey" {
                $p = @{
                    PatientId = $Patient.sfPatient.id
                    DateTime = $DateTime
                    DeviceType = (New-Guid).Guid
                    DeviceId = (New-Guid).Guid
                    Gateway = (New-Guid).Guid
                    GatewayDeviceId = (New-Guid).Guid
                    ExternalObsId = (New-Guid).Guid
                }
                $observation = New-Observation @p
                $Body = Get-SurveyResponsesFromCsv -SurveyName $Row.Name -AnswerFile $Row.AnswerFile -SurveyDefinitions $surveyDefs
                $Body.body.observation = $observation
                @{
                    body = $Body
                    endpoint = "/services/apexrest/phecc/surveyResponseObservation/postObservationWithSurvey"
                }
            }
        }
    }
}