AWSTemplateFormatVersion: 2010-09-09
Parameters:
  DeviceID:
    AllowedPattern: "[A-Z0-9]{12}"
    Description: Device ID(Serial Number)
    Type: String
Resources:
  MyTopicRule:
    Type: 'AWS::IoT::TopicRule'
    Properties:
      RuleName: !Sub armadillo_iot_a6e_${DeviceID}_ENV_sensor_cloudwatch_metrics
      TopicRulePayload:
        Sql: !Sub 'SELECT * FROM "$aws/things/${DeviceID}/shadow/update/accepted"'
        RuleDisabled: false
        Actions:
          - CloudwatchMetric:
              RoleArn: !GetAtt IoTCoreRuleExecutionRole.Arn
              MetricNamespace: armadillo_iot_a6e
              MetricName: !Sub ${DeviceID}_ENV_sensor_temperature
              MetricValue: ${state.reported.ENV_sensor.temperature}
              MetricTimestamp: ${state.reported.timestamp}
              MetricUnit: None
          - CloudwatchMetric:
              RoleArn: !GetAtt IoTCoreRuleExecutionRole.Arn
              MetricNamespace: armadillo_iot_a6e
              MetricName: !Sub ${DeviceID}_ENV_sensor_humidity
              MetricValue: ${state.reported.ENV_sensor.humidity}
              MetricTimestamp: ${state.reported.timestamp}
              MetricUnit: None
          - CloudwatchMetric:
              RoleArn: !GetAtt IoTCoreRuleExecutionRole.Arn
              MetricNamespace: armadillo_iot_a6e
              MetricName: !Sub ${DeviceID}_ENV_sensor_illuminance
              MetricValue: ${state.reported.ENV_sensor.illuminance}
              MetricTimestamp: ${state.reported.timestamp}
              MetricUnit: None
          - CloudwatchMetric:
              RoleArn: !GetAtt IoTCoreRuleExecutionRole.Arn
              MetricNamespace: armadillo_iot_a6e
              MetricName: !Sub ${DeviceID}_ENV_sensor_air_pressure
              MetricValue: ${state.reported.ENV_sensor.air_pressure}
              MetricTimestamp: ${state.reported.timestamp}
              MetricUnit: None
          - CloudwatchMetric:
              RoleArn: !GetAtt IoTCoreRuleExecutionRole.Arn
              MetricNamespace: armadillo_iot_a6e
              MetricName: !Sub ${DeviceID}_ENV_sensor_sound_noise
              MetricValue: ${state.reported.ENV_sensor.sound_noise}
              MetricTimestamp: ${state.reported.timestamp}
              MetricUnit: None
          - CloudwatchMetric:
              RoleArn: !GetAtt IoTCoreRuleExecutionRole.Arn
              MetricNamespace: armadillo_iot_a6e
              MetricName: !Sub ${DeviceID}_ENV_sensor_e_tvoc
              MetricValue: ${state.reported.ENV_sensor.e_tvoc}
              MetricTimestamp: ${state.reported.timestamp}
              MetricUnit: None
          - CloudwatchMetric:
              RoleArn: !GetAtt IoTCoreRuleExecutionRole.Arn
              MetricNamespace: armadillo_iot_a6e
              MetricName: !Sub ${DeviceID}_ENV_sensor_e_co2
              MetricValue: ${state.reported.ENV_sensor.e_co2}
              MetricTimestamp: ${state.reported.timestamp}
              MetricUnit: None
          - CloudwatchMetric:
              RoleArn: !GetAtt IoTCoreRuleExecutionRole.Arn
              MetricNamespace: armadillo_iot_a6e
              MetricName: !Sub ${DeviceID}_ENV_sensor_discomfort_index
              MetricValue: ${state.reported.ENV_sensor.discomfort_index}
              MetricTimestamp: ${state.reported.timestamp}
              MetricUnit: None
          - CloudwatchMetric:
              RoleArn: !GetAtt IoTCoreRuleExecutionRole.Arn
              MetricNamespace: armadillo_iot_a6e
              MetricName: !Sub ${DeviceID}_ENV_sensor_heat_stroke_alarmness
              MetricValue: ${state.reported.ENV_sensor.heat_stroke_alarmness}
              MetricTimestamp: ${state.reported.timestamp}
              MetricUnit: None
          - CloudwatchMetric:
              RoleArn: !GetAtt IoTCoreRuleExecutionRole.Arn
              MetricNamespace: armadillo_iot_a6e
              MetricName: !Sub ${DeviceID}_ENV_sensor_vibr_info
              MetricValue: ${state.reported.ENV_sensor.vibr_info}
              MetricTimestamp: ${state.reported.timestamp}
              MetricUnit: None
  IoTCoreRuleExecutionRole:
    Type: 'AWS::IAM::Role'
    Properties:
      RoleName: !Sub A6E_ENV_sensor_CloudWatch_Metrics_Role_${DeviceID}
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Action:
              - 'sts:AssumeRole'
            Principal:
              Service:
                - "iot.amazonaws.com"
      Policies:
        - PolicyName: !Sub A6E_ENV_sensor_CloudWatch_Metrics_Policy_${DeviceID}
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action: 'cloudwatch:PutMetricData'
                Resource: '*'
  Dashboard:
    Type: 'AWS::CloudWatch::Dashboard'
    Properties:
      DashboardName: !Sub armadillo_iot_a6e_${DeviceID}_ENV_sensor
      DashboardBody: !Sub |
        {
          "widgets": [
            {
              "height": 15,
              "width": 24,
              "y": 0,
              "x": 0,
              "type": "metric",
              "properties": {
                "view": "timeSeries",
                "title": "ENV_sensor",
                "period": 60,
                "region": "${AWS::Region}",
                "stat": "Maximum",
                "stacked": false,
                "metrics": [
                    [ "armadillo_iot_a6e", "${DeviceID}_ENV_sensor_temperature" ],
                    [ "armadillo_iot_a6e", "${DeviceID}_ENV_sensor_humidity" ],
                    [ "armadillo_iot_a6e", "${DeviceID}_ENV_sensor_illuminance" ],
                    [ "armadillo_iot_a6e", "${DeviceID}_ENV_sensor_air_pressure" ],
                    [ "armadillo_iot_a6e", "${DeviceID}_ENV_sensor_sound_noise" ],
                    [ "armadillo_iot_a6e", "${DeviceID}_ENV_sensor_e_tvoc" ],
                    [ "armadillo_iot_a6e", "${DeviceID}_ENV_sensor_e_co2" ],
                    [ "armadillo_iot_a6e", "${DeviceID}_ENV_sensor_discomfort_index" ],
                    [ "armadillo_iot_a6e", "${DeviceID}_ENV_sensor_heat_stroke_alarmness" ],
                    [ "armadillo_iot_a6e", "${DeviceID}_ENV_sensor_vibr_info" ]
                ],
                "yAxis": {
                  "left": {
                    "showUnits": false,
                    "label": "value",
                    "min": 0,
                    "max": 800
                  }
                }
              }
            }
          ]
        }
