{"id":21834,"date":"2018-12-24T14:00:17","date_gmt":"2018-12-24T05:00:17","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=21834"},"modified":"2018-12-24T14:34:56","modified_gmt":"2018-12-24T05:34:56","slug":"co2-logging","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2018\/12\/24\/co2-logging\/","title":{"rendered":"\u304a\u305d\u3068\u3067\u304a\u3046\u3061\u306e\u4e8c\u9178\u5316\u70ad\u7d20\u3092\u898b\u308b"},"content":{"rendered":"
\u3053\u308c\u306f \ud83d\ude3aTECHSCORE Advent Calendar 2018\ud83d\ude3a<\/a> \u306e24\u65e5\u76ee\u306e\u8a18\u4e8b\u3067\u3059\u3002 \u524d\u56de\u306e\u8a18\u4e8b<\/a>\u3067\u3001\u5c0f\u3055\u306a\u30c7\u30a3\u30b9\u30d7\u30ec\u30a4\u4e0a\u3067\u4e8c\u9178\u5316\u70ad\u7d20\u6fc3\u5ea6\u3092\u898b\u3089\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002 \u306a\u304a\u3001\u5916\u51fa\u5148\u3067\u898b\u308b\u3053\u3068\u306b\u610f\u5473\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n \u307e\u305a\u3001\u30ed\u30b0\u306f\u30b0\u30e9\u30d5\u3092\u7c21\u5358\u306b\u898b\u3089\u308c\u308b AWS \u306e Cloudwatch \u306e\u30ab\u30b9\u30bf\u30e0\u30e1\u30c8\u30ea\u30af\u30b9\u3092\u4f7f\u3044\u307e\u3059\u3002 \u3057\u304b\u3057\u3001\u3053\u308c\u3060\u3051\u3067\u306f\u3001AWS \u306b\u3044\u3061\u3044\u3061\u30ed\u30b0\u30a4\u30f3\u3057\u306a\u3051\u308c\u3070\u898b\u3089\u308c\u307e\u305b\u3093\u3002 \u5148\u306e\u30c7\u30d0\u30a4\u30b9\u306f\u3001\u30b7\u30ea\u30a2\u30eb\u30dd\u30fc\u30c8\u304b\u3089\u5e38\u306b\u4e8c\u9178\u5316\u70ad\u7d20\u6fc3\u5ea6\u3068\u6c17\u6e29\u3092\u51fa\u529b\u3057\u3066\u3044\u307e\u3057\u305f\u3002 Ruby \u306a\u3069\u3067\u30e9\u30a4\u30d6\u30e9\u30ea\u304b\u3089 API \u3092\u53e9\u304f\u3068\u3044\u3046\u624b\u3082\u3042\u308a\u307e\u3059\u304c\u3001\u3053\u3046\u3044\u3046\u7c21\u5358\u306a\u3082\u306e\u3067\u3042\u308c\u3070 AWS CLI + \u30b7\u30a7\u30eb\u30b9\u30af\u30ea\u30d7\u30c8\u306e\u307b\u3046\u304c\u9762\u5012\u304c\u5c11\u306a\u304f\u3066\u826f\u3044\u3067\u3059\u306d\u3002<\/p>\n \u307b\u307c\u81ea\u660e\u306a\u3082\u306e\u306a\u306e\u3067\u3059\u304c\u3001\u53c2\u8003\u307e\u3067\u306b\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u4ee5\u4e0b\u306b\u793a\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n picocom \u3067\u30c7\u30d0\u30a4\u30b9\u306b\u30b7\u30ea\u30a2\u30eb\u63a5\u7d9a\u3057\u3066\u5024\u3092\u8aad\u3093\u3067\u3044\u307e\u3059\u3002 \u3053\u308c\u306f\u3001Python \u3067\u66f8\u304d\u307e\u3059\u3002 \u306a\u304a\u3001 CloudWatch Events \u3067 10 \u5206\u6bce\u306b\u5148\u7a0b\u306e Lambda \u95a2\u6570\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n \u307e\u305f\u3001\u66f8\u304d\u51fa\u3057\u305f\u753b\u50cf\u3092\u57cb\u3081\u3053\u3093\u3060 index.html \u3082\u7528\u610f\u3057\u3066\u3084\u308a\u307e\u3059\u3002 \u3053\u308c\u3067\u3001\u5916\u51fa\u5148\u304b\u3089 S3 \u7d4c\u7531\u3067(\u307b\u307c)\u6700\u65b0\u306e\u30b0\u30e9\u30d5\u304c\u898b\u3089\u308c\u307e\u3059\u3002 \u8ab0\u3067\u3082\u898b\u3089\u308c\u308b\u3088\u3046\u306a\u72b6\u614b\u3067 URL \u3092\u6559\u3048\u3066\u3057\u307e\u3046\u3068\u3001\u5bb6\u306b\u4eba\u304c\u3044\u306a\u3044\u3053\u3068\u304c\u306a\u3093\u3068\u306a\u304f\u5206\u304b\u3063\u3066\u3057\u307e\u3046\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002 \u3053\u308c\u306f \ud83d\ude3aTECHSCORE Advent Calendar 2018\ud83d\ude3a \u306e24\u65e5\u76ee\u306e\u8a18\u4e8b\u3067\u3059\u3002
\n\u307e\u305f\u30011 \u65e5\u76ee\u306e \u304a\u3046\u3061\u306e\u4e8c\u9178\u5316\u70ad\u7d20\u6fc3\u5ea6\u3092\u8a08\u3063\u3066\u307f\u308b<\/a> \u306e\u7d9a\u304d\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n\u304a\u305d\u3068\u3067\u304a\u3046\u3061\u306e\u4e8c\u9178\u5316\u70ad\u7d20\u3092\u898b\u308b<\/h1>\n
\n\u3057\u304b\u3057\u3001\u3053\u3046\u306a\u308b\u3068\u30ed\u30b0\u3092\u53d6\u3063\u3066\u5909\u5316\u3092\u898b\u305f\u304f\u306a\u308b\u3068\u3053\u308d\u3067\u3059\u3002
\n\u305d\u3053\u3067\u3001\u30ed\u30b0\u3092\u53d6\u308a\u3064\u3064\u3001\u5916\u51fa\u5148\u3067\u76f4\u8fd1\u306e\u5909\u5316\u3092\u898b\u3089\u308c\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n\u4f5c\u6226<\/h1>\n
\n\u3053\u308c\u3067\u3001\u4e8c\u9178\u5316\u70ad\u7d20\u6fc3\u5ea6\u306e\u30ed\u30b0\u3092\u6b8b\u305b\u307e\u3059\u3002<\/p>\n
\n\u305d\u3053\u3067\u3001S3 \u306b\u30b0\u30e9\u30d5\u753b\u50cf\u3092\u5b9a\u671f\u7684\u306b\u751f\u6210\u3057\u3066\u3084\u308b\u3053\u3068\u306b\u3057\u307e\u3059\u3002
\n\u3053\u306e S3 \u306e\u8a2d\u5b9a\u3067\u3001\u4e00\u822c\u516c\u958b\u72b6\u614b\u306b\u3057\u3066\u3057\u307e\u3048\u3070\u3001\u5916\u51fa\u5148\u3067\u3082\u898b\u3089\u308c\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<\/p>\n\u307e\u305a\u30e1\u30c8\u30ea\u30af\u30b9\u306b\u5024\u3092\u6295\u5165\u3059\u308b<\/h1>\n
\n\u305d\u308c\u3092\u8aad\u307f\u53d6\u308a\u3001AWS \u306e API \u3092\u547c\u3076\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u30b5\u30fc\u30d3\u30b9\u3068\u3057\u3066\u5e38\u99d0\u3055\u305b\u308b\u3053\u3068\u306b\u3057\u307e\u3059\u3002
\n\u5358\u7d14\u306a\u30b7\u30a7\u30eb\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u3001AWS CLI \u3067 API \u3092\u53e9\u3044\u3066\u3044\u307e\u3059\u3002<\/p>\nppm.service (systemd \u7528\u30b5\u30fc\u30d3\u30b9\u5b9a\u7fa9\u30d5\u30a1\u30a4\u30eb)<\/h2>\n
[Unit]\nDescription=PPM\n\n[Service]\nType=simple\nExecStart=\/home\/app\/app\/ppm\/reporter\n\n[Install]\nWantedBy=multi-user.target\n<\/code><\/pre>\n
\u30b5\u30fc\u30d3\u30b9\u3068\u3057\u3066\u52d5\u304b\u3059\u30b7\u30a7\u30eb\u30b9\u30af\u30ea\u30d7\u30c8<\/h2>\n
\n\u30c7\u30d0\u30a4\u30b9\u306e\u51fa\u529b\u983b\u5ea6\u306b\u5408\u305b\u3066\u3044\u308b\u3068\u3001\u304a\u91d1\u304c\u304b\u304b\u308a\u307e\u3059\u306e\u3067\u3001\u9069\u5f53\u306b\u9593\u9694\u3092\u3042\u3051\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n#!\/bin\/bash\n\ndev=\"${1:-\/dev\/ttyUSB0}\"\nlast=0\n\nwhile read -r line\ndo\n now=\"$( date '+%s' )\"\n d=\"$(( now - last ))\"\n\n if [ \"$d\" -ge 180 ]\n then\n date\n ppm=\"${line% *}\"\n temp=\"${line#* }\"\n aws --region ap-northeast-1 cloudwatch put-metric-data --namespace myhome --metric-name ppm --value \"$ppm\"\n aws --region ap-northeast-1 cloudwatch put-metric-data --namespace myhome --metric-name temp --value \"$temp\"\n last=\"$now\"\n fi\ndone < <(picocom --quiet -b 115200 \"$dev\")<\/pre>\n
Lambda \u95a2\u6570\u3092\u7528\u610f\u3059\u308b<\/h1>\n
\n\u30b0\u30e9\u30d5\u306e\u5b9a\u7fa9\u3092 JSON \u3067\u6307\u5b9a\u3057\u3001\u30e1\u30c8\u30ea\u30af\u30b9\u306e\u753b\u50cf\u3092\u8fd4\u3057\u3066\u304f\u308c\u308b\u4fbf\u5229 API \u3092\u547c\u3093\u3067\u3044\u307e\u3059\u3002
\n\u305d\u306e\u753b\u50cf\u3092\u305d\u306e\u307e\u307e S3 \u306b\u66f8\u304d\u51fa\u3057\u3066\u3044\u307e\u3059\u3002
\n\u3061\u306a\u307f\u306b\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u306b\u610f\u5473\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\nimport json\nimport boto3\nimport datetime\n\nTEMP_JSON = '''\n{\n \"view\": \"timeSeries\",\n \"stacked\": false,\n \"metrics\": [\n [ \"myhome\", \"temp\" ]\n ],\n \"yAxis\": {\n \"left\": {\n \"min\": 5,\n \"max\": 35,\n \"showUnits\": false\n }\n },\n \"period\": 300,\n \"width\": 800,\n \"height\": 250,\n \"start\": \"-PT12H\",\n \"end\": \"P0D\",\n \"timezone\": \"+0900\"\n}'''\n\nPPM_JSON = '''\n{\n \"view\": \"timeSeries\",\n \"stacked\": false,\n \"metrics\": [\n [ \"myhome\", \"ppm\" ]\n ],\n \"yAxis\": {\n \"left\": {\n \"min\": 400,\n \"max\": 1500\n }\n },\n \"annotations\": {\n \"horizontal\": [\n {\n \"value\": 1000,\n \"fill\": \"above\"\n }\n ]\n },\n \"period\": 300,\n \"width\": 800,\n \"height\": 250,\n \"start\": \"-PT12H\",\n \"end\": \"P0D\",\n \"timezone\": \"+0900\"\n}\n'''\n\ndef lambda_handler(event, context):\n def upload(key_name, widget):\n image = cloudwatch.get_metric_widget_image(MetricWidget = widget)\n obj = s3.Object(bucket_name, key_name)\n r = obj.put(Body = image['MetricWidgetImage'], ContentType = 'image\/png')\n\n def get_value(name):\n now = datetime.datetime.utcnow()\n r = cloudwatch.get_metric_statistics(\n Period = 300,\n StartTime = now - datetime.timedelta(seconds = 600),\n EndTime = now,\n Namespace = 'myhome',\n MetricName = name,\n Statistics=['Average'])\n return r['Datapoints'][-1]['Average']\n\n bucket_name = 'ky.example.com'\n s3 = boto3.resource('s3')\n cloudwatch = boto3.client('cloudwatch', region_name = 'ap-northeast-1')\n\n upload('ppm.png', PPM_JSON)\n upload('temp.png', TEMP_JSON)\n\n ppm = get_value('ppm')\n temp = get_value('temp')\n obj = s3.Object(bucket_name, 'values.json')\n r = obj.put(Body = json.dumps({'ppm': ppm, 'temp': temp}), ContentType = 'application\/javascript')\n\n return {\n \"statusCode\": 200,\n \"body\": 'OK'\n }\n<\/code><\/pre>\n
TEMP_JSON<\/code> \u3084
PPM_JSON<\/code> \u306f\u81ea\u5206\u3067\u66f8\u304f\u306e\u306f\u9762\u5012\u3067\u3059\u3002
\nCloudWatch \u306e\u30b3\u30f3\u30bd\u30fc\u30eb\u3067\u5b9f\u969b\u306b\u30b0\u30e9\u30d5\u3092\u4f5c\u3063\u3066\u307f\u3066\u3001\u30bd\u30fc\u30b9<\/code>\u30bf\u30d6\u3067
\u30a4\u30e1\u30fc\u30b8API<\/code>\u3092\u9078\u3073\u3001\u8868\u793a\u3055\u308c\u305f\u3082\u306e\u3092\u30b3\u30d4\u30da\u3059\u308b\u3068\u697d\u3067\u3059\u3002
\n\u2193\u306e\u3088\u3046\u306a\u753b\u9762\u304c\u3042\u308b\u306f\u305a\u3067\u3059\u3002<\/p>\n<\/a><\/p>\n
\u5b9a\u671f\u7684\u306b\u66f8\u304d\u3060\u3059<\/h1>\n
<\/a><\/p>\n
\n\u3053\u3061\u3089\u306f\u52ff\u8ad6\u9759\u7684\u306a\u3082\u306e\u3067\u304b\u307e\u308f\u306a\u3044\u306e\u3067\u3001\u9069\u5f53\u306b\u66f8\u3044\u3066\u540c\u3058 S3 \u306e\u30d0\u30b1\u30c3\u30c8\u306b\u653e\u308a\u3053\u307f\u307e\u3059\u3002<\/p>\n\u5b8c\u6210!<\/h1>\n
\n\u2193\u304c\u5b9f\u969b\u306e\u30b0\u30e9\u30d5\u3067\u3059\u3002\u3046\u3063\u304b\u308a\u30c9\u30a2\u3092\u9589\u3081\u3066\u3044\u305f\u306e\u3067\u6fc3\u5ea6\u304c\u9ad8\u304f\u306a\u3063\u3066\u3044\u307e\u3059\u306d\u2026\u3002<\/p>\n<\/a><\/p>\n
\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3<\/h1>\n
\n\u305f\u307e\u306b\u6fc0\u3057\u3044\u904b\u52d5\u3092\u3057\u3066\u4e8c\u9178\u5316\u70ad\u7d20\u3092\u751f\u6210\u3059\u308b\u751f\u7269\u304c\u5c45\u308b\u3068\u826f\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"
\n\u307e\u305f\u30011 \u65e5\u76ee\u306e \u304a\u3046\u3061\u306e\u4e8c\u9178\u5316\u70ad\u7d20\u6fc3\u5ea6\u3092\u8a08\u3063\u3066\u307f\u308b \u306e\u7d9a\u304d\u3068\u306a\u308a\u307e\u3059\u3002
\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":43,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[308,254,18],"tags":[196,119,87,319,248,318],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/21834"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=21834"}],"version-history":[{"count":9,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/21834\/revisions"}],"predecessor-version":[{"id":21908,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/21834\/revisions\/21908"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=21834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=21834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=21834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}