Windows Virtual Shields for Arduino
Sensor.h
Go to the documentation of this file.
1 /*
2  Copyright(c) Microsoft Open Technologies, Inc. All rights reserved.
3 
4  The MIT License(MIT)
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files(the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions :
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  THE SOFTWARE.
23 */
24 
25 #ifndef Sensor_h
26 #define Sensor_h
27 
28 #include "Arduino.h"
29 
30 #include "SensorModels.h"
31 #include "VirtualShield.h"
32 #include "ShieldEvent.h"
33 #include <ArduinoJson.h>
34 #include "Attr.h"
35 
36 class VirtualShield;
37 
38 const PROGMEM char MESSAGE[] = "Message";
39 const PROGMEM char MS[] = "Ms";
40 const PROGMEM char TO[] = "To";
41 const PROGMEM char ATTACHMENT[] = "Attachment";
42 const PROGMEM char ENABLE[] = "ENABLE";
43 const PROGMEM char DISABLE[] = "DISABLE";
44 const PROGMEM char ACTION[] = "Action";
45 const PROGMEM char TAG[] = "Tag";
46 const PROGMEM char IMAGE[] = "IMAGE";
47 const PROGMEM char AUDIO[] = "Audio";
48 const PROGMEM char URL[] = "Url";
49 const PROGMEM char STOP[] = "STOP";
50 
51 class Sensor {
52 public:
53  void(*onEvent)(ShieldEvent* shieldEvent);
54 
57 
58  const char sensorType;
59  bool isRunning = false;
60 
61  Sensor(const VirtualShield &shield, const char sensorType);
62 
63  int start(double delta = 0, long interval = 0);
64  virtual int stop();
65  int get();
66  int getOnChange(double delta = 0);
67 
68  bool isUpdated();
69 
70  int writeAll(const char* serviceName, EPtr values[], int count, Attr extraAttributes[] = 0, int extraAttributeCount = 0);
71  int sensorAction(SensorAction sensorAction, double delta = 0, long interval = 0) const;
72 
73  virtual bool isEvent(const char* tag, const char* action, ShieldEvent* shieldEvent);
74  virtual bool isEvent(int id, const char* action, ShieldEvent* shieldEvent);
75 
76  void setOnEvent(void(*onEvent)(ShieldEvent* shieldEvent))
77  {
78  this->onEvent = onEvent;
79  }
80 
81  int sendStop(const char* serviceName);
82 
83  virtual void onJsonReceived(JsonObject& root, ShieldEvent* shieldEvent);
84 
85 protected:
86  bool _isUpdated = false;
87 };
88 
90  SensorEvent();
91 
93 };
94 
95 #endif
const PROGMEM char ACTION[]
Definition: Sensor.h:44
void(* onEvent)(ShieldEvent *shieldEvent)
Definition: Sensor.h:53
virtual int stop()
Stops this sensor.
Definition: Sensor.cpp:61
Definition: Sensor.h:51
int getOnChange(double delta=0)
Requests a single reading when more than the delta beyond the current reading.
Definition: Sensor.cpp:81
const PROGMEM char TO[]
Definition: Sensor.h:40
Definition: VirtualShield.h:50
const PROGMEM char IMAGE[]
Definition: Sensor.h:46
const char sensorType
Definition: Sensor.h:58
SensorAction
Definition: SensorModels.h:30
virtual bool isEvent(const char *tag, const char *action, ShieldEvent *shieldEvent)
Determines whether the specified shieldEvent matches the tag and action.
Definition: Sensor.cpp:128
const PROGMEM char ATTACHMENT[]
Definition: Sensor.h:41
VirtualShield & shield
Definition: Sensor.h:55
const PROGMEM char URL[]
Definition: Sensor.h:48
int start(double delta=0, long interval=0)
Starts the sensor, triggered when longer than the interval and larger than the delta.
Definition: Sensor.cpp:51
SensorEvent()
Initializes a new instance of the SensorEvent struct.
Definition: Sensor.cpp:204
const PROGMEM char ENABLE[]
Definition: Sensor.h:42
Sensor * sensor
Definition: Sensor.h:92
const PROGMEM char MS[]
Definition: Sensor.h:39
const PROGMEM char AUDIO[]
Definition: Sensor.h:47
ShieldEvent recentEvent
Definition: Sensor.h:56
bool isUpdated()
Determines whether this sensor has an updated value. Resets to false after this call.
Definition: Sensor.cpp:112
Sensor(const VirtualShield &shield, const char sensorType)
Initializes a new instance of the Sensor class.
Definition: Sensor.cpp:42
Definition: SensorModels.h:101
int sendStop(const char *serviceName)
Definition: Sensor.cpp:195
Definition: Sensor.h:89
const PROGMEM char TAG[]
Definition: Sensor.h:45
Definition: Attr.h:30
const PROGMEM char STOP[]
Definition: Sensor.h:49
Definition: ShieldEvent.h:35
bool _isUpdated
Definition: Sensor.h:86
int sensorAction(SensorAction sensorAction, double delta=0, long interval=0) const
Sends the specific action to start/stop/get/onChange the sensor using a delta and interval...
Definition: Sensor.cpp:161
const PROGMEM char MESSAGE[]
Definition: Sensor.h:38
void setOnEvent(void(*onEvent)(ShieldEvent *shieldEvent))
Definition: Sensor.h:76
int writeAll(const char *serviceName, EPtr values[], int count, Attr extraAttributes[]=0, int extraAttributeCount=0)
Writes all EPtr values to the communication channel.
Definition: Sensor.cpp:150
bool isRunning
Definition: Sensor.h:59
virtual void onJsonReceived(JsonObject &root, ShieldEvent *shieldEvent)
Event called when a valid json message was received. Consumes the proper values for this sensor...
Definition: Sensor.cpp:92
const PROGMEM char DISABLE[]
Definition: Sensor.h:43