Updated on 2024-11-15 GMT+08:00

Building a Program

Editing a Function Program

Open function iotdemo, copy the following coordinate conversion code to the function. This code is for testing purposes only and can be modified if needed.

 # -*- coding:utf-8 -*-
import json
import math
from math import pi
 
def handler(event, context):
    data = event["notify_data"]["body"]
    lat = data["lat"]
    lng = data["lng"]
    print(f" WGS84: ({lng},{lat})")
    gcj_lng, gcj_lat = transform(lng, lat)
    print(f" GCJ02: ({gcj_lng},{gcj_lat})")
    body = {
        "gcj_lng": gcj_lng,
        "gcj_lat": gcj_lat
    }
    return {
        "statusCode": 200,
        "isBase64Encoded": False,
        "body": json.dumps(body),
        "headers": {
            "Content-Type": "application/json"
        }
    }
 
def transform(lon, lat):
    a = 6378245.0
    ee = 0.00669342162296594323
 
    dlat = transform_lat(lon - 105.0, lat - 35.0)
    dlon = transform_lon(lon - 105.0, lat - 35.0)
 
    rad_lat = lat / 180.0 * pi
    magic = math.sin(rad_lat)
    magic = 1 - ee * magic * magic
    sqrt_magic = math.sqrt(magic)
 
    dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrt_magic) * pi)
    dlon = (dlon * 180.0) / (a / sqrt_magic * math.cos(rad_lat) * pi)
 
    mg_lon = lon + dlon
    mg_lat = lat + dlat
 
    return mg_lon, mg_lat
 
def transform_lon(x, y):
    ret = 300.0 + x + 2.0 * y + 0.1 * x * x + \
        0.1 * x * y + 0.1 * math.sqrt(math.fabs(x))
    ret += (20.0 * math.sin(6.0 * pi * x) +
            20.0 * math.sin(2.0 * pi * x)) * 2.0 / 3.0
    ret += (20.0 * math.sin(pi * x) +
            40.0 * math.sin(pi / 3.0 * x)) * 2.0 / 3.0
    ret += (150.0 * math.sin(pi / 12.0 * x) +
            300.0 * math.sin(pi / 30.0 * x)) * 2.0 / 3.0
return ret
 
def transform_lat(x, y):
    ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + \
        0.1 * x * y + 0.2 * math.sqrt(math.fabs(x))
    ret += (20.0 * math.sin(6.0 * pi * x) +
            20.0 * math.sin(2.0 * pi * x)) * 2.0 / 3.0
    ret += (20.0 * math.sin(pi * y) +
            40.0 * math.sin(pi / 3.0 * y)) * 2.0 / 3.0
    ret += (160.0 * math.sin(pi / 12.0 * y) +
            320 * math.sin(pi / 30.0 * y)) * 2.0 / 3.0
    return ret

Online Joint Commissioning with IoTDA

  1. Log in to the IoTDA console and click an instance name. In the navigation pane, choose Rules & > Data Forwarding. In the Rule List, click View on the right of the target rule name. The Data Forwarding Rule Details page is displayed.
  2. Select Set Forwarding Target and click Test on the right of the forwarding target to edit the test data.

    Figure 1 Testing the forwarding rule

  3. Enter the test data and click Connectivity Test.

    {
        "resource": "device.message",
        "event": "report",
        "event_time": "string",
        "notify_data": {
            "header": {
                "app_id": "d4922d8a-6c8e-4396-852c-164aefa6638f",
                "device_id": "d4922d8a-6c8e-4396-852c-164aefa6638f",
                "node_id": "ABC123456789",
                "product_id": "ABC123456789",
                "gateway_id": "d4922d8a-6c8e-4396-852c-164aefa6638f",
                "tags": [
                    {
                        "tag_key": "testTagName",
                        "tag_value": "testTagValue"
                    }
                ]
            },
            "body": {
                "lat": 92.64763932844794,
                "lng": 35.25202546134364
            }
        }
    }
    Figure 2 Connectivity test result

  4. Go to the FunctionGraph console, choose Monitoring > Logs, and click the request ID in blue to view logs.

    Figure 3 Viewing logs
    Figure 4 Viewing request details

    To invoke other systems, persist data in OBS, or achieve other purposes, modify the program.