Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 raymond 1
#pragma once
2
#include <Arduino.h>
3
 
4
 
5
inline const char thingsboard_htm[] PROGMEM = R"EOF(
6
<div>
7
  <br>If you don't have a valid <b>device token</b> press button "Device Provisioning" to start procedure in order to get a new token from ThingsBoard server.<br>
8
  <br>To perform <a href="https://thingsboard.io/docs/user-guide/device-provisioning/">device provisioning</a>, this functionality must be enabled in the ThingsBoard profile of your devices.
9
  <div class='btn-bar'>
10
    <a id=device-provisioning class='btn'>
11
        <div class=svg>
12
          <svg class='icon' viewBox='0 0 24 24'>
13
            <path fill='currentColor' d='M21.41 11.58L12.41 2.58C12.04 2.21 11.53 2 11 2H4C2.9 2 2 2.9 2 4V11C2 11.53 2.21 12.04 2.59 12.41L3 12.81C3.9 12.27 4.94 12 6 12C9.31 12 12 14.69 12 18C12 19.06 11.72 20.09 11.18 21L11.58 21.4C11.95 21.78 12.47 22 13 22S14.04 21.79 14.41 21.41L21.41 14.41C21.79 14.04 22 13.53 22 13S21.79 11.96 21.41 11.58M5.5 7C4.67 7 4 6.33 4 5.5S4.67 4 5.5 4 7 4.67 7 5.5 6.33 7 5.5 7M8.63 14.27L4.76 18.17L3.41 16.8L2 18.22L4.75 21L10.03 15.68L8.63 14.27' />
14
          </svg>
15
        </div>
16
        <span> Device provisioning</span>
17
    </a>
18
  </div>
19
</div>
20
)EOF";
21
 
22
 
23
inline const char thingsboard_script[] PROGMEM = R"EOF(
24
const TB_DEVICE_NAME = 'Device Name';
25
const TB_DEVICE_LAT = 'Device Latitude';
26
const TB_DEVICE_LON = 'Device Longitude';
27
const TB_SERVER = 'ThingsBoard server address';
28
const TB_PORT = 'ThingsBoard server port';
29
const TB_DEVICE_TOKEN = 'ThingsBoard device token';
30
const TB_DEVICE_KEY = 'Provisioning device key';
31
const TB_SECRET_KEY = 'Provisioning secret key';
32
 
33
function getUrl(host, port) {
34
  var url;
35
  if (port === '80')
36
    url = 'http://' + host + '/api/v1/';
37
  else if (port === '443')
38
    url = 'https://' + host + '/api/v1/'
39
  else
40
    url = 'http://' + host + ':'+ port + '/api/v1/'
41
  return url;
42
}
43
 
44
function deviceProvisioning() {
45
  console.log('Device provisioning');
46
  var server = $(TB_SERVER).value;
47
  var port = $(TB_PORT).value;
48
  var token = $(TB_DEVICE_TOKEN).value;
49
  if (token === '')
50
    token = 'xxxxx';
51
  const url = 'https://corsproxy.io/?' + encodeURIComponent(getUrl(server, port) + token + '/attributes');
52
  fetch(url, {
53
    method: "GET"
54
  })
55
  .then((response) => {
56
    if (response.ok) {
57
      openModalMessage('Device provisioning', 'Device already provisioned. Do you want update device client attributes?', setDeviceClientAttribute);
58
      return;
59
    }
60
    throw new Error('Device token not present. Provisioning new device');
61
  })
62
  .catch((error) => {
63
    openModalMessage('New device', 'A new device will be provisioned on ThingsBoard server', createNewDevice);
64
  });
65
}
66
 
67
function createNewDevice() {
68
  var server = $(TB_SERVER).value;
69
  var port = $(TB_PORT).value;
70
  var key = $(TB_DEVICE_KEY).value;
71
  var secret = $(TB_SECRET_KEY).value;
72
  var name = $(TB_DEVICE_NAME).value;
73
  const url = 'https://corsproxy.io/?' + encodeURIComponent(getUrl(server, port) + 'provision');
74
  var payload = {
75
    'deviceName': name,
76
    'provisionDeviceKey': key,
77
    'provisionDeviceSecret': secret
78
  };
79
  fetch(url, {
80
    headers: {
81
      'Accept': 'application/json',
82
      'Content-Type': 'application/json'
83
    },
84
    method: 'POST',
85
    body: JSON.stringify(payload)
86
  })
87
  .then(response => response.json())
88
  .then(obj => {
89
    var token = $(TB_DEVICE_TOKEN);
90
    token.focus();
91
    token.value = obj.credentialsValue;
92
    options[token.id] = token.value;     // Manual update, because, it doesn't fire "change" event...
93
    openModal('Write device attributes', 'Device provisioned correctly.<br>Do you want to set client attributes on ThingsBoard server?', setDeviceClientAttribute);
94
  });
95
}
96
 
97
 
98
function setDeviceClientAttribute(){
99
  var server = $(TB_SERVER).value;
100
  var port = $(TB_PORT).value;
101
  var token = $(TB_DEVICE_TOKEN).value;
102
  var name = $(TB_DEVICE_NAME).value;
103
  var latitude = $(TB_DEVICE_LAT).value;
104
  var longitude = $(TB_DEVICE_LON).value;
105
  const url = 'https://corsproxy.io/?' + encodeURIComponent(getUrl(server, port) + token + '/attributes');
106
  var payload = {
107
    'DeviceName': name,
108
    'latitude': latitude,
109
    'longitude': longitude,
110
  };
111
  fetch(url, {
112
    method: 'POST',
113
    body: JSON.stringify(payload),
114
  })
115
  .then(response => response.text())
116
  .then(() => {
117
    openModalMessage('Device properties', 'Device client properties updated!');
118
  });
119
}
120
$('device-provisioning').addEventListener('click', deviceProvisioning);
121
)EOF";