Skip to content

Commit f8068f1

Browse files
committed
Merge branch 'staging'
2 parents f9e54d7 + 0d8aaf3 commit f8068f1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+49893
-1148
lines changed

scripts/metalsmith.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var fs = require('fs');
4646
var sitemap = require('./sitemap.js');
4747
var buildZip = require('./buildZip.js');
4848
var carriersUpdate = require('./carriers-update/carriers-update.js');
49+
var trackerSchema = require('./tracker-schema.js');
4950

5051
var handlebars = require('handlebars');
5152
var prettify = require('prettify');
@@ -90,9 +91,22 @@ exports.metalsmith = function () {
9091
'content/languages/**/*',
9192
'assets/images/**/*.ai'
9293
]))
93-
.use(buildZip({
94-
dir: '../src/assets/files/app-notes/'
95-
}))
94+
.use(msIf(
95+
environment === 'development',
96+
buildZip({
97+
dir: '../src/assets/files/app-notes/'
98+
})))
99+
.use(msIf(
100+
environment === 'development',
101+
trackerSchema({
102+
dir: '../src/assets/files/tracker/',
103+
officialSchema: 'tracker-edge.json',
104+
defaultSchema: 'default-schema.json',
105+
fragments: [
106+
'engine-schema',
107+
'test-schema'
108+
]
109+
})))
96110
// Minify CSS
97111
.use(cleanCSS({
98112
files: '**/*.css'
@@ -298,10 +312,12 @@ exports.metalsmith = function () {
298312
omitExtensions: ['.md'],
299313
omitTrailingSlashes: false
300314
}))
301-
.use(function(files, metalsmith, done) {
315+
.use(msIf(
316+
environment === 'development',
317+
function(files, metalsmith, done) {
302318
carriersUpdate.doUpdate(__dirname);
303319
done();
304-
})
320+
}))
305321
// Replace the {{handlebar}} markers inside Markdown files before they are rendered into HTML and
306322
// any other files with a .hbs extension in the src folder
307323
.use(inPlace({
@@ -397,6 +413,7 @@ exports.server = function (callback) {
397413
'../templates/layouts/workshops.hbs': 'content/workshops/**/*.md',
398414
'../templates/layouts/landing.hbs': 'content/*.md',
399415
'../templates/layouts/main.hbs': 'content/index.md',
416+
'../templates/helpers/**/*.hbs': 'content/**/*.md',
400417
'../templates/partials/**/*.hbs': 'content/**/*.md',
401418
'${source}/assets/js/*.js*': true,
402419
'${source}/assets/files/**/*': true,

scripts/tracker-schema.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
const indentLevel = 2;
7+
8+
function updateFileIfChanged(file, newContents) {
9+
let oldContents = '';
10+
if (fs.existsSync(file)) {
11+
oldContents = fs.readFileSync(file);
12+
}
13+
if (oldContents != newContents) {
14+
fs.writeFileSync(file, newContents);
15+
}
16+
}
17+
18+
function updateConfigurationSchemas(dir, options) {
19+
// console.log('updating configuration schemas ' + dir);
20+
21+
const originalSchemaObj = JSON.parse(fs.readFileSync(path.join(dir, options.officialSchema), 'utf8'));
22+
23+
// Reformat to 2 spaces per indent to make it easier to read in the code box
24+
const newDefaultSchema = JSON.stringify(originalSchemaObj, null, indentLevel);
25+
updateFileIfChanged(path.join(dir, options.defaultSchema), newDefaultSchema);
26+
27+
options.fragments.forEach(function(frag) {
28+
const fragmentPath = path.join(dir, frag + '-fragment.json');
29+
const fragmentObj = JSON.parse(fs.readFileSync(fragmentPath, 'utf8'));
30+
const dstPath = path.join(dir, frag + '.json');
31+
32+
// Clean up fragment source if necessary
33+
updateFileIfChanged(fragmentPath, JSON.stringify(fragmentObj, null, indentLevel));
34+
35+
// Duplicate original
36+
let newSchema = JSON.parse(JSON.stringify(originalSchemaObj));
37+
38+
// Copy in new
39+
Object.keys(fragmentObj).forEach(function(key) {
40+
newSchema.properties[key] = fragmentObj[key];
41+
});
42+
43+
const newSchemaString = JSON.stringify(newSchema, null, indentLevel);
44+
updateFileIfChanged(dstPath, newSchemaString);
45+
});
46+
}
47+
48+
49+
module.exports = function(options) {
50+
51+
return function(files, metalsmith, done) {
52+
const dir = metalsmith.path(options.dir);
53+
updateConfigurationSchemas(dir, options);
54+
done();
55+
};
56+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "Particle.h"
2+
3+
// The following line is optional, but it allows your code to run
4+
// even when not cloud connected
5+
SYSTEM_THREAD(ENABLED);
6+
7+
// This allows for USB serial debug logs
8+
SerialLogHandler logHandler;
9+
10+
unsigned long colorSetTime = 0;
11+
std::chrono::milliseconds colorExpirationTime = 10s;
12+
13+
// Forward declarations (functions used before they're implemented)
14+
int setColor(String cmd);
15+
16+
void setup()
17+
{
18+
Particle.function("setColor", setColor);
19+
}
20+
21+
void loop()
22+
{
23+
if (colorSetTime != 0 && millis() - colorSetTime >= colorExpirationTime.count())
24+
{
25+
// Revert back to system color scheme
26+
RGB.control(false);
27+
colorSetTime = 0;
28+
Log.info("reverted to normal color scheme");
29+
}
30+
}
31+
32+
int setColor(String cmd)
33+
{
34+
int result = 0;
35+
36+
Log.info("setColor %s", cmd.c_str());
37+
38+
int red, green, blue;
39+
if (sscanf(cmd, "%d,%d,%d", &red, &green, &blue) == 3)
40+
{
41+
// Override the status LED color temporarily
42+
RGB.control(true);
43+
RGB.color(red, green, blue);
44+
colorSetTime = millis();
45+
46+
Log.info("red=%d green=%d blue=%d", red, green, blue);
47+
result = 1;
48+
}
49+
else {
50+
Log.info("not red,green,blue");
51+
}
52+
return result;
53+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "Particle.h"
2+
3+
// The following line is optional, but it allows your code to run
4+
// even when not cloud connected
5+
SYSTEM_THREAD(ENABLED);
6+
7+
// This allows for USB serial debug logs
8+
SerialLogHandler logHandler;
9+
10+
// Forward declarations (functions used before they're implemented)
11+
int getSensor();
12+
13+
// This is the event name to publish
14+
const char *eventName = "testEvent";
15+
16+
// This is how often to publish (30s = every 30 seconds)
17+
// Other useful units include min for minutes and h for hours.
18+
std::chrono::milliseconds publishPeriod = 30s;
19+
20+
// This keeps track of the last time we published
21+
unsigned long lastPublishMs;
22+
23+
void setup()
24+
{
25+
}
26+
27+
void loop()
28+
{
29+
// Check to see if it's time to publish
30+
if (millis() - lastPublishMs >= publishPeriod.count())
31+
{
32+
lastPublishMs = millis();
33+
34+
// The event data is string but we just send our value as
35+
// a ASCII formatted decimal number
36+
String eventData = String::format("%d", getSensor());
37+
38+
// Make sure we're cloud connected before publishing
39+
if (Particle.connected())
40+
{
41+
Particle.publish(eventName, eventData);
42+
43+
Log.info("published %s", eventData.c_str());
44+
}
45+
else
46+
{
47+
Log.info("not cloud connected %s", eventData.c_str());
48+
}
49+
}
50+
}
51+
52+
int getSensor()
53+
{
54+
// To make this tutorial function without actually
55+
// requiring a sensor, we just return a random
56+
// value from 0 - 4095 like you'd get from an
57+
// analogRead() call
58+
return rand() % 4096;
59+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "Particle.h"
2+
3+
// The following line is optional, but it allows your code to run
4+
// even when not cloud connected
5+
SYSTEM_THREAD(ENABLED);
6+
7+
// This allows for USB serial debug logs
8+
SerialLogHandler logHandler;
9+
10+
// Forward declarations (functions used before they're implemented)
11+
void getSensor(float &temperatureC, float &humidity);
12+
13+
// This is the event name to publish
14+
const char *eventName = "testEvent";
15+
16+
// This is how often to publish (30s = every 30 seconds)
17+
// Other useful units include min for minutes and h for hours.
18+
std::chrono::milliseconds publishPeriod = 30s;
19+
20+
// This keeps track of the last time we published
21+
unsigned long lastPublishMs;
22+
23+
void setup()
24+
{
25+
}
26+
27+
void loop()
28+
{
29+
// Check to see if it's time to publish
30+
if (millis() - lastPublishMs >= publishPeriod.count())
31+
{
32+
lastPublishMs = millis();
33+
34+
// Get temperature and humidity values
35+
float temperatureC, humidity;
36+
getSensor(temperatureC, humidity);
37+
38+
// Make a comma-separated output of both values. Each
39+
// value is a floating point number with 1 decimal place
40+
// %f is a floating point number and the .1 is one
41+
// decimal place in the float.
42+
String eventData = String::format("%.1f,%.1f",
43+
temperatureC, humidity);
44+
45+
// Make sure we're cloud connected before publishing
46+
if (Particle.connected())
47+
{
48+
Particle.publish(eventName, eventData);
49+
50+
Log.info("published %s", eventData.c_str());
51+
}
52+
else
53+
{
54+
Log.info("not cloud connected %s", eventData.c_str());
55+
}
56+
}
57+
}
58+
59+
void getSensor(float &temperatureC, float &humidity)
60+
{
61+
// Return a random temperature from -20 to +40 C
62+
// and a humidity from 0 to 100 just for testing
63+
// purposes. You would normally get it from your
64+
// sensor hardware here.
65+
temperatureC = (float)(rand() % 600) / 10 - 20;
66+
humidity = (float)(rand() % 1010) / 10;
67+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "Particle.h"
2+
3+
// The following line is optional, but it allows your code to run
4+
// even when not cloud connected
5+
SYSTEM_THREAD(ENABLED);
6+
7+
// This allows for USB serial debug logs
8+
SerialLogHandler logHandler;
9+
10+
// This is the event name to publish
11+
const char *eventName = "testEvent";
12+
13+
// This is how often to publish (30s = every 30 seconds)
14+
// Other useful units include min for minutes and h for hours.
15+
std::chrono::milliseconds publishPeriod = 15s;
16+
17+
// This keeps track of the last time we published
18+
unsigned long lastPublishMs;
19+
20+
void setup()
21+
{
22+
}
23+
24+
void loop()
25+
{
26+
// Check to see if it's time to publish
27+
if (millis() - lastPublishMs >= publishPeriod.count())
28+
{
29+
lastPublishMs = millis();
30+
31+
// Make sure we're cloud connected before publishing
32+
if (Particle.connected())
33+
{
34+
int a = rand() % 10000;
35+
float b = ((float)rand()) / 100.0;
36+
bool c = (bool)(rand() % 2);
37+
String d(String::format("testing %d", rand() % 1000));
38+
39+
char buf[256];
40+
snprintf(buf, sizeof(buf),
41+
"{\"a\":%d,\"b\":%.3f,\"c\":%s,\"d\":\"%s\"}",
42+
a, b, (c ? "true" : "false"), d.c_str());
43+
Particle.publish(eventName, buf);
44+
45+
Log.info("published %s", buf);
46+
}
47+
}
48+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "Particle.h"
2+
3+
// The following line is optional, but it allows your code to run
4+
// even when not cloud connected
5+
SYSTEM_THREAD(ENABLED);
6+
7+
// This allows for USB serial debug logs
8+
SerialLogHandler logHandler;
9+
10+
// This is the event name to publish
11+
const char *eventName = "testEvent";
12+
13+
// This is how often to publish (30s = every 30 seconds)
14+
// Other useful units include min for minutes and h for hours.
15+
std::chrono::milliseconds publishPeriod = 15s;
16+
17+
// This keeps track of the last time we published
18+
unsigned long lastPublishMs;
19+
20+
void setup()
21+
{
22+
}
23+
24+
void loop()
25+
{
26+
// Check to see if it's time to publish
27+
if (millis() - lastPublishMs >= publishPeriod.count())
28+
{
29+
lastPublishMs = millis();
30+
31+
// Make sure we're cloud connected before publishing
32+
if (Particle.connected())
33+
{
34+
int a = rand() % 10000;
35+
float b = ((float)rand()) / 100.0;
36+
bool c = (bool)(rand() % 2);
37+
String d(String::format("testing %d", rand() % 1000));
38+
39+
char buf[256];
40+
JSONBufferWriter writer(buf, sizeof(buf));
41+
writer.beginObject();
42+
writer.name("a").value(a);
43+
writer.name("b").value(b, 3);
44+
writer.name("c").value(c);
45+
writer.name("d").value(d);
46+
writer.endObject();
47+
writer.buffer()[std::min(writer.bufferSize(), writer.dataSize())] = 0;
48+
49+
Particle.publish(eventName, buf);
50+
51+
Log.info("published %s", buf);
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)