示例#1
0
app.get('/senduserpin/:userToken/:minutesToAdd?', function (request, response){

	var userToken = request.params.userToken;
	var minutesToAdd = request.params.minutesToAdd || 0;

	console.log('Got a request from ' + userToken + ' to send in a pin in ' + minutesToAdd + ' min');

	// generate random id
	var id = randomstring.generate(64);

	var now = new Date();

	// create the pin object
	var pin = new Timeline.Pin({
		id: id,
		time: new Date(now.getTime() + (minutesToAdd*60*1000)),
		layout: {
			type: Timeline.Pin.LayoutType.GENERIC_PIN,
			tinyIcon: Timeline.Pin.Icon.PIN,
			title: 'This is a generic pin!',
			body: 'Pin ID: ' + id
		}
	});

	// add actions to the pin
	[10, 20, 30].forEach(function (min){
		pin.addAction(new Timeline.Pin.Action({
			type: Timeline.Pin.ActionType.OPEN_WATCH_APP,
			title: min + ' min',
			launchCode: min
		}));
	});

	// send the pin
	timeline.sendUserPin(userToken, pin, function (error, body, resp){
		if (error){
			return console.log(error);
		}

		response.send('Status Code: ' + resp.statusCode);
	});
});
示例#2
0
function pushPin(place, res) {
  var pin;
  // var pinID = parseInt(place._id.toHexString(), 16);
  var pinID = place._id.toString();
  var pinTime = place.pin.time || place.time || null;
  if (place.pin) {
    place.pin.id = pinID;
    place.pin.time = new Date(pinTime);
    if (place.pin.reminders) {
      for (var i=0; i<place.pin.reminders.length; i++) {
        place.pin.reminders[i].time = new Date(place.pin.reminders[i].time);
      }
      console.log(place.pin);
    }
    try {
     pin = new Timeline.Pin(place.pin);
    }
    catch(e) {
      console.warn('Failed to create pin: ' + e);
      res.status(500);
      res.send('Failed to create pin: ' + e);
      return;
    }
  }
  else {
    pin = new Timeline.Pin({
      id: pinID,
      time: new Date(pinTime),
      layout: new Timeline.Pin.Layout({
        type: Timeline.Pin.LayoutType.GENERIC_PIN,
        tinyIcon: Timeline.Pin.Icon.PIN,
        title: 'Get Back target',
        body: 'Set from watch'
      })
    });
    console.log(JSON.stringify(pin));
  }
  pin.addAction(new Timeline.Pin.Action({
    title: 'Get Back',
    type: Timeline.Pin.ActionType.OPEN_WATCH_APP,
    launchCode: parseInt(place._id)
  }));
  if (!place.pin || !place.pin.createNotification) {
    pin.createNotification = new Timeline.Pin.Notification({
      "layout": {
        "type": "genericNotification",
        "title": pin.layout.title || "Get Back target",
        "tinyIcon": "system://images/NOTIFICATION_FLAG",
        "body": pin.layout.body || "New target added."
      }
    });
  }
  place.pin = pin;
  console.log(place);

  console.log('Sending pin for ' + place.timelineToken + ': ' + JSON.stringify(pin));
  timeline.sendUserPin(place.timelineToken, pin, function (err, body, resp) {
    if(err) {
      console.warn('Failed to push pin to timeline: ' + err);
      res.status(500);
      res.send('Failed to push pin to timeline: ' + err);
    }
    else {
      console.log('Pin successfully pushed!');
      res.json(place);
    }
  });
}