-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
2,941 additions
and
12 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
{ | ||
"ExpandedNodes": [ | ||
"", | ||
"\\hapweb" | ||
"" | ||
], | ||
"SelectedNode": "\\homeintegration.c", | ||
"SelectedNode": "\\json.c", | ||
"PreviewInSolutionExplorer": false | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "elgato.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#ifndef __ELGATO_H__ | ||
#define __ELGATO_H__ | ||
|
||
#define ELGATO_EPOCH_OFFSET 978307200 | ||
|
||
#define HOMEKIT_SERVICE_ELGATO_HISTORY "E863F007-079E-48FF-8F27-9C2605A29F52" | ||
|
||
#define HOMEKIT_CHARACTERISTIC_ELGATO_SET_TIME "E863F121-079E-48FF-8F27-9C2605A29F52" // 121 => S2W2 | ||
#define HOMEKIT_CHARACTERISTIC_ELGATO_HISTORY_REQUEST "E863F11C-079E-48FF-8F27-9C2605A29F52" // 11C => S2W1 | ||
#define HOMEKIT_CHARACTERISTIC_ELGATO_HISTORY_STATUS "E863F116-079E-48FF-8F27-9C2605A29F52" // 116 => S2R1 | ||
#define HOMEKIT_CHARACTERISTIC_ELGATO_HISTORY_ENTRIES "E863F117-079E-48FF-8F27-9C2605A29F52" // 117 => S2R2 | ||
|
||
|
||
#define HOMEKIT_DECLARE_CHARACTERISTIC_ELGATO_SET_TIME(_value, ...) \ | ||
.type = HOMEKIT_CHARACTERISTIC_ELGATO_SET_TIME, \ | ||
.description = "SetTime", \ | ||
.format = homekit_format_data, \ | ||
.permissions = homekit_permissions_paired_read \ | ||
| homekit_permissions_notify, \ | ||
.value = HOMEKIT_DATA_(NULL,128), \ | ||
##__VA_ARGS__ | ||
|
||
|
||
#define HOMEKIT_DECLARE_CHARACTERISTIC_ELGATO_HISTORY_REQUEST(_value, ...) \ | ||
.type = HOMEKIT_CHARACTERISTIC_ELGATO_HISTORY_REQUEST, \ | ||
.description = "History", \ | ||
.format = homekit_format_data, \ | ||
.permissions = homekit_permissions_paired_read \ | ||
| homekit_permissions_notify, \ | ||
.value = HOMEKIT_DATA_(NULL,128), \ | ||
##__VA_ARGS__ | ||
|
||
#define HOMEKIT_DECLARE_CHARACTERISTIC_ELGATO_HISTORY_STATUS(_value, ...) \ | ||
.type = HOMEKIT_CHARACTERISTIC_ELGATO_HISTORY_STATUS, \ | ||
.description = "History status", \ | ||
.format = homekit_format_data, \ | ||
.permissions = homekit_permissions_paired_read \ | ||
| homekit_permissions_notify, \ | ||
.value = HOMEKIT_DATA_(NULL,128), \ | ||
##__VA_ARGS__ | ||
|
||
#define HOMEKIT_DECLARE_CHARACTERISTIC_ELGATO_HISTORY_ENTRIES(_value, ...) \ | ||
.type = HOMEKIT_CHARACTERISTIC_ELGATO_HISTORY_ENTRIES, \ | ||
.description = "History entries", \ | ||
.format = homekit_format_data, \ | ||
.permissions = homekit_permissions_paired_read \ | ||
| homekit_permissions_notify, \ | ||
.value = HOMEKIT_DATA_(NULL,128), \ | ||
##__VA_ARGS__ | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
#ifndef Array_h | ||
#define Array_h | ||
|
||
#ifndef STORAGE_SIZE | ||
#define STORAGE_SIZE 10 | ||
#endif | ||
#include "Arduino.h" | ||
template<class T> | ||
struct CSimpleArraySleepStorage | ||
{ | ||
typedef int size_type; | ||
T data_storage[STORAGE_SIZE]; | ||
size_type data_size; | ||
size_type elements; | ||
}; | ||
template<class T> | ||
class CSimpleArray | ||
{ | ||
typedef int size_type; | ||
|
||
public: | ||
CSimpleArray() : data_storage(NULL), data_size(0), elements(0) { | ||
dummy = (T*)new char[sizeof(T)]; | ||
} | ||
~CSimpleArray() { | ||
// RemoveAll(); | ||
if (data_storage) | ||
delete[] (char*)data_storage; | ||
if(dummy) | ||
delete[] (char*)dummy; | ||
} | ||
void RemoveAll() { | ||
if (data_storage) | ||
delete[](char*)data_storage; | ||
data_storage = NULL; | ||
data_size = 0; | ||
elements = 0; | ||
} | ||
//void SerializeToStorage(){ | ||
// SerializeToStorage(ArrayStorage); | ||
//} | ||
void SerializeToStorage(CSimpleArraySleepStorage<T> &storage){ | ||
size_type count=data_size; | ||
count=count>STORAGE_SIZE?STORAGE_SIZE:count; | ||
storage.data_size=count; | ||
storage.elements=elements>STORAGE_SIZE?STORAGE_SIZE:elements; | ||
memcpy(storage.data_storage,data_storage,sizeof(T)*count); | ||
} | ||
//void RestoreFromStorage(){ | ||
// RestoreFromStorage(ArrayStorage); | ||
//} | ||
void RestoreFromStorage(CSimpleArraySleepStorage<T> &storage){ | ||
if(data_storage) | ||
delete[] data_storage; | ||
data_size=storage.data_size; | ||
elements=storage.elements; | ||
data_storage= new T[data_size]; | ||
memcpy(data_storage,storage.data_storage,sizeof(T)*data_size); | ||
} | ||
size_type GetSize() const { | ||
return elements; | ||
} | ||
void Add(T & t) { | ||
if (data_size <= elements) { | ||
Realloc(data_size + 10); | ||
} | ||
elements++; | ||
SetAt(elements - 1, t); | ||
|
||
} | ||
void AddWithShiftLeft(T & t) { | ||
if (data_size == 0) | ||
return; | ||
if (data_size == elements) { | ||
memcpy(data_storage, data_storage + 1, sizeof(T) * (data_size - 1)); | ||
data_storage[data_size - 1] = t; | ||
} | ||
else { | ||
Add(t); | ||
} | ||
} | ||
bool Dequeue(T* element) { | ||
if (elements == 0) | ||
return false; | ||
*element = GetAt(0); | ||
memcpy(data_storage, data_storage + 1, sizeof(T) * (data_size - 1)); | ||
elements--; | ||
return true; | ||
} | ||
bool SetAt(size_type index, T & t) { | ||
if (index < 0 || index >= data_size) | ||
return false; | ||
data_storage[index] = t; | ||
return true; | ||
} | ||
//Just reset without deleting buffer | ||
size_type SetSize(size_type size){ | ||
if(size>elements) | ||
return -1; | ||
elements=size; | ||
return elements; | ||
} | ||
T Sum(){ | ||
T sum=0; | ||
for (int i = 0; i<elements; i++){ | ||
sum+=data_storage[i]; | ||
} | ||
return sum; | ||
} | ||
T& Max() { | ||
return Select([](const T & t1, const T & t2)-> bool{return t1 > t2;}); | ||
/* | ||
if (elements == 0) | ||
return dummy[0]; | ||
size_type idx_cur = 0; | ||
for (int i = 1; i<elements; i++){ | ||
idx_cur = data_storage[idx_cur]>data_storage[i] ? idx_cur : i; | ||
} | ||
return data_storage[idx_cur]; | ||
*/ | ||
} | ||
T& Min() { | ||
return Select([](const T & t1, const T & t2)-> bool {return t1 < t2;}); | ||
/* | ||
if (elements == 0) | ||
return dummy[0]; | ||
size_type idx_cur = 0; | ||
for (int i = 1; i<elements; i++){ | ||
idx_cur = data_storage[idx_cur]<data_storage[i] ? idx_cur : i; | ||
} | ||
return data_storage[idx_cur]; | ||
*/ | ||
} | ||
T& GetAt(size_type index) const { | ||
if (index < 0 || index >= data_size) | ||
return dummy[0]; | ||
return data_storage[index]; | ||
} | ||
T& operator[] (size_type index) const { | ||
return GetAt(index); | ||
} | ||
T& Select(bool(*fcompare)(const T&, const T&)) { | ||
if (elements == 0) | ||
return dummy[0]; | ||
size_type idx_cur = 0; | ||
for (int i = 1; i < elements; i++) { | ||
idx_cur = fcompare(data_storage[idx_cur], data_storage[i]) ? idx_cur : i; | ||
} | ||
return data_storage[idx_cur]; | ||
} | ||
String toJsonArray(T min, T max) { | ||
String res = "["; | ||
|
||
for (int i = 0; i < elements; i++) { | ||
res += String(constrain(data_storage[i], min, max)); | ||
if (i != (elements - 1)) | ||
res += ","; | ||
} | ||
res += "]"; | ||
return res; | ||
} | ||
private: | ||
void Realloc(size_type newsize) { | ||
T* olddata = data_storage; | ||
data_storage =(T*) new char[sizeof(T)*newsize]; //new T[newsize]; | ||
memset((char*)data_storage, 0, sizeof(T)*newsize); | ||
if (data_size != 0) { | ||
memcpy(data_storage, olddata, sizeof(T)*data_size); | ||
// delete[] (char*)olddata; | ||
} | ||
if(olddata) | ||
delete[](char*)olddata; | ||
data_size = newsize; | ||
} | ||
|
||
|
||
protected: | ||
T* data_storage; | ||
size_type data_size; | ||
size_type elements; | ||
T* dummy; | ||
//RTC_DATA_ATTR CSimpleArraySleepStorage<T> ArrayStorage; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "CHistory.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#pragma once | ||
#include "Arduino.h" | ||
|
||
#include "Array.h" | ||
#include <FS.h> | ||
|
||
#if !defined(ESP8266) | ||
#include <SPIFFS.h> | ||
#endif | ||
|
||
template<class T> | ||
class CHistory :public CSimpleArray <T>{ | ||
public: | ||
typedef void(*WriteElementFN)(Stream* ps,T elem); | ||
typedef String(*GetStringElement)( T elem); | ||
typedef T(*ExtractFromJsonString)(String json); | ||
|
||
void WriteToStream(Stream* ps) { | ||
for (int i = 0; i < this->GetSize(); i++) { | ||
pfn_write_element(ps,this->GetAt(i)); | ||
} | ||
} | ||
bool WriteToFile(String fileName) { | ||
if(!pfn_get_string_element) | ||
return false; | ||
SPIFFS.remove(fileName); | ||
|
||
File file = SPIFFS.open(fileName, "w+"); | ||
if (!file){ | ||
Serial.println("Failed to open file"); | ||
return false; | ||
} | ||
file.print("[\n"); | ||
for (int i = 0; i < this->GetSize(); i++) { | ||
|
||
String s = pfn_get_string_element(this->GetAt(i)); | ||
file.print(s); | ||
if (i == (this->GetSize() - 1)) { | ||
file.print("\n"); | ||
} | ||
else { | ||
file.print(",\n"); | ||
} | ||
|
||
} | ||
file.print("]"); | ||
file.close(); | ||
return true; | ||
}; | ||
bool LoadFromFile(String fileName){ | ||
File file = SPIFFS.open(fileName, "r"); | ||
if (!file){ | ||
Serial.println("Failed to open file"); | ||
return false; | ||
} | ||
int line=0; | ||
String buffer; | ||
while (file.available()) { | ||
buffer = file.readStringUntil('\n'); | ||
//Serial.println(buffer); //Printing for debugging purpose | ||
line++; | ||
if(line==1){ | ||
continue; | ||
} | ||
if(buffer=="]"){ | ||
break; | ||
} | ||
if(buffer.endsWith(",")){ | ||
buffer.remove(buffer.length()-1,1); | ||
} | ||
if(pfn_extract_from_jsonstring){ | ||
T elem=pfn_extract_from_jsonstring(buffer); | ||
this->Add(elem); | ||
} | ||
} | ||
}; | ||
void SetWriteFn(WriteElementFN fn) { pfn_write_element = fn; }; | ||
void SetGetStringFn(GetStringElement fn) { pfn_get_string_element = fn; }; | ||
void SetExtractFn(ExtractFromJsonString fn) { pfn_extract_from_jsonstring = fn; }; | ||
private: | ||
WriteElementFN pfn_write_element; | ||
GetStringElement pfn_get_string_element; | ||
ExtractFromJsonString pfn_extract_from_jsonstring; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#pragma once | ||
const char* ntpServer = "pool.ntp.org"; | ||
const long gmtOffset_sec = 7200; | ||
const int daylightOffset_sec = -3600; | ||
#include <time.h> | ||
#if defined(ESP8266) | ||
#include <WiFiUdp.h> | ||
WiFiUDP ntpUDP; | ||
NTPClient* pTimeClient=null; | ||
#endif | ||
|
||
void startTime(){ | ||
#if defined(ESP8266) | ||
ptimeClient = new NTPClient(ntpUDP, ntpServer.c_str(), gmtOffset_sec); | ||
ptimeClient->begin(); | ||
ptimeClient->update(); | ||
#else | ||
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); | ||
#endif | ||
}; | ||
|
||
unsigned long getLocalTime_ms(){ | ||
|
||
unsigned long tm=0; | ||
#if defined(ESP8266) | ||
if(ptimeClient){ | ||
ptimeClient->update(); | ||
tm= ptimeClient->getEpochTime(); | ||
} | ||
#else | ||
struct tm timeinfo; | ||
if (getLocalTime(&timeinfo)) { | ||
tm = mktime(&timeinfo); | ||
} | ||
#endif | ||
return tm; | ||
}; |
Oops, something went wrong.