###前提・実現したいこと
milkcocoaに格納したデータ {"NGHYL":2017031750,"ZT":1527503362,"P":1}を読み込み、
変数NGHYLの10桁目(上記の例では0)を別の変数に格納し、関数を呼ぶ際の引数に用いながら、その格納した変数をページ上に表示したいです。
以下に、javascriptのソースコードを示します。
※コメント化しているのは、いろいろ試した結果ですがうまくいきませんでした。
お手数おかけいたしますが、どうかよろしくお願いいたします。
###該当のソースコード
java
1// Milkcocoa App ID 2var MILKCOCOA_APP_ID = "re*********"; 3 4// Milkcocoa Data Store 5var MILKCOCOA_DATASTORE = "Ma*********"; 6 7// Milkcocoa 8var milkcocoa = new MilkCocoa(MILKCOCOA_APP_ID + ".mlkcca.com"); 9 10// Milkcocoa Data Store 11var dataStore = milkcocoa.dataStore(MILKCOCOA_DATASTORE); 12 13// 運転中画面 14var working = document.getElementById("working"); 15 16// 黄色停止画面(unfounded) 17var yellowstopunfounded = document.getElementById("yellowstopunfounded"); 18 19// 黄色停止画面(founded) 20var yellowstopfounded = document.getElementById("yellowstopfounded"); 21 22// 赤色停止画面(founded) 23var redstopfounded = document.getElementById("redstopfounded"); 24 25// 赤色停止画面(unfounded) 26var redstopunfounded = document.getElementById("redstopunfounded"); 27 28// 無点灯画面(others) 29var others = document.getElementById("others"); 30 31// 過去1件のデータ取得 32dataStore.stream().size(1).next(function (err, data) { 33 // 取得が成功したら 34 if (!err) { 35 var obj = data[0]; 36 37 // 明るさの状態を取得 38 var NGHYL = obj.value.NGHYL; 39 //var ZT = obj.value.ZT; 40 41 var zennou = HGHYL.charAt(9); 42 //var zennou = obj.value.P; 43 44 //Light Stateの抜き出し 45 //var flg = new Array(); 46 47 //var cnt = data.length; 48 49 //for( var i = 0 ; i < cnt ; i++){ 50 // flg[i] = data.charAt(i); 51 //} 52 53 //var zennou = flg[0]; 54 55 56 // 画面の切り替え 57 changeView(zennou); 58 } 59}); 60 61// ArduinoからMilkcocoaに値がpushされたら 62dataStore.on("push", function (data) { 63 if(receivedData) receivedData.textContent = JSON.stringify(data); // この行追加 64 // 明るさの状態を取得 65 var NGHYL = data.value.NGHYL; 66 var ZT = data.value.ZT; 67 var P = data.value.P; 68 69 var zennou = HGHYL.charAt(9); 70 71 72 // 画面の切り替え 73 changeView(zennou); 74}); 75 76/** 77 * 画面を切り替えます。 78 */ 79function changeView(zennou) { 80 // 運転中 81 if (zennou == 5) { 82 Working(); 83 } 84 // 黄色停止中(unfounded) 85 if (zennou == 4) { 86 Yellowstopunfounded(); 87 } 88 // 黄色停止中(founded) 89 if (zennou == 3) { 90 Yellowstopfounded(); 91 } 92 // 赤色停止中(unfounded) 93 if (zennou == 2) { 94 Redstopunfounded(); 95 } 96 // 赤色停止中(founded) 97 if (zennou == 1) { 98 Redstopfounded(); 99 } 100 // 無点灯中(others) 101 if (zennou == 0) { 102 Others(); 103 } 104} 105/** 106 * 運転中画面に切り替えます。 107 */ 108function Working() { 109 working.style.display = "block"; 110 yellowstopfounded.style.display = "none"; 111 yellowstopunfounded.style.display = "none"; 112 redstopunfounded.style.display = "none"; 113 redstopfounded.style.display = "none"; 114 others.style.display = "none"; 115} 116/** 117 * 黄色停止中画面(unfounded)に切り替えます。 118 */ 119function Yellowstopunfounded() { 120 working.style.display = "none"; 121 yellowstopfounded.style.display = "none"; 122 yellowstopunfounded.style.display = "block"; 123 redstopunfounded.style.display = "none"; 124 redstopfounded.style.display = "none"; 125 others.style.display = "none"; 126} 127/** 128 * 黄色停止中画面(founded)に切り替えます。 129 */ 130function Yellowstopfounded() { 131 working.style.display = "none"; 132 yellowstopfounded.style.display = "block"; 133 yellowstopunfounded.style.display = "none"; 134 redstopunfounded.style.display = "none"; 135 redstopfounded.style.display = "none"; 136 others.style.display = "none"; 137} 138/** 139 * 赤色停止中画面(founded)に切り替えます。 140 */ 141function Redstopfounded() { 142 working.style.display = "none"; 143 yellowstopfounded.style.display = "none"; 144 yellowstopunfounded.style.display = "none"; 145 redstopunfounded.style.display = "none"; 146 redstopfounded.style.display = "block"; 147 others.style.display = "none"; 148} 149/** 150 * 赤色停止中画面(unfounded)に切り替えます。 151 */ 152function Redstopunfounded() { 153 working.style.display = "none"; 154 yellowstopfounded.style.display = "none"; 155 yellowstopunfounded.style.display = "none"; 156 redstopunfounded.style.display = "block"; 157 redstopfounded.style.display = "none"; 158 others.style.display = "none"; 159} 160/** 161 * 無点灯中画面(others)に切り替えます。 162 */ 163function Others() { 164 working.style.display = "none"; 165 yellowstopfounded.style.display = "none"; 166 yellowstopunfounded.style.display = "none"; 167 redstopunfounded.style.display = "none"; 168 redstopfounded.style.display = "none"; 169 others.style.display = "block"; 170}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/22 03:13