|
| 1 | +#include "stdafx.h" |
| 2 | +#ifdef WITH_TELLDUSCORE |
| 3 | +#include "Tellstick.h" |
| 4 | + |
| 5 | +#include "../main/Helper.h" |
| 6 | +#include "../main/Logger.h" |
| 7 | +#include "../main/SQLHelper.h" |
| 8 | +#include "../main/mainworker.h" |
| 9 | +#include "../main/WebServer.h" |
| 10 | +#include "../webserver/cWebem.h" |
| 11 | +#include "../json/json.h" |
| 12 | +#include "hardwaretypes.h" |
| 13 | +#include <telldus-core.h> |
| 14 | + |
| 15 | +CTellstick::CTellstick(const int ID) |
| 16 | +{ |
| 17 | + m_HwdID=ID; |
| 18 | + m_bSkipReceiveCheck = true; |
| 19 | +} |
| 20 | + |
| 21 | +CTellstick::~CTellstick(void) |
| 22 | +{ |
| 23 | + m_bIsStarted=false; |
| 24 | +} |
| 25 | + |
| 26 | +bool CTellstick::AddSwitchIfNotExits(const int id, const char* devname, bool isDimmer) |
| 27 | +{ |
| 28 | + char sid[16]; |
| 29 | + sprintf(sid, "%08X", id); |
| 30 | + |
| 31 | + std::vector<std::vector<std::string> > result; |
| 32 | + result = m_sql.safe_query("SELECT ID FROM DeviceStatus WHERE (HardwareID==%d) AND (DeviceID=='%q') AND (Type==%d) AND (SubType==%d)", m_HwdID, sid, pTypeGeneralSwitch, sSwitchTypeAC); |
| 33 | + if (result.size()<1) |
| 34 | + { |
| 35 | + _log.Log(LOG_NORM, "Tellstick: device %d %s: %s", id, sid ,devname); |
| 36 | + m_sql.safe_query( |
| 37 | + "INSERT INTO DeviceStatus (HardwareID, DeviceID, Unit, Type, SubType, SwitchType, SignalLevel, BatteryLevel, Name, nValue, sValue) " |
| 38 | + "VALUES (%d,'%q',%d,%d,%d,%d,12,255,'%q',0,' ')", |
| 39 | + m_HwdID, sid, 3, pTypeGeneralSwitch, sSwitchTypeAC, isDimmer ? STYPE_Dimmer : STYPE_OnOff, devname); |
| 40 | + return true; |
| 41 | + } |
| 42 | + return false; |
| 43 | +} |
| 44 | + |
| 45 | +void CTellstick::deviceEvent(int deviceId, int method, const char *data) { |
| 46 | + _log.Log(LOG_NORM, "Tellstick: deviceEvent %d %d: %s", deviceId, method, data); |
| 47 | + |
| 48 | + char sid[16]; |
| 49 | + sprintf(sid, "%08d", deviceId); |
| 50 | + |
| 51 | + _tGeneralSwitch gswitch; |
| 52 | + // TODO pTypeGeneralSwitch |
| 53 | + gswitch.subtype = sSwitchTypeAC; |
| 54 | + gswitch.id = deviceId; |
| 55 | + gswitch.unitcode = 3; |
| 56 | + gswitch.level = 0; |
| 57 | + gswitch.battery_level = 255; |
| 58 | + gswitch.rssi = 12; |
| 59 | + gswitch.seqnbr = 0; |
| 60 | + |
| 61 | + if (method == TELLSTICK_TURNON) { |
| 62 | + gswitch.cmnd = gswitch_sOn; |
| 63 | + sDecodeRXMessage(this, (const unsigned char *)&gswitch); |
| 64 | + } else if (method == TELLSTICK_TURNOFF) { |
| 65 | + gswitch.cmnd = gswitch_sOff; |
| 66 | + sDecodeRXMessage(this, (const unsigned char *)&gswitch); |
| 67 | + } else if (method == TELLSTICK_DIM) { |
| 68 | + gswitch.cmnd = gswitch_sSetLevel; |
| 69 | + gswitch.level = atoi(data)*99/255; |
| 70 | + sDecodeRXMessage(this, (const unsigned char *)&gswitch); |
| 71 | + } else { |
| 72 | + _log.Log(LOG_NORM, "Unknown event from device %i\n", deviceId); |
| 73 | + } |
| 74 | +} |
| 75 | +void CTellstick::deviceEventCallback(int deviceId, int method, const char *data, int callbackId, void *context) { |
| 76 | + CTellstick *t = reinterpret_cast<CTellstick *>(context); |
| 77 | + if (t) { |
| 78 | + /** Please note! |
| 79 | + * We are here in another thread than the main. Some measures to syncronize |
| 80 | + * this must be taken! |
| 81 | + **/ |
| 82 | + t->deviceEvent(deviceId, method, data); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void CTellstick::Init() |
| 87 | +{ |
| 88 | + tdInit(); |
| 89 | + tdRegisterDeviceEvent( reinterpret_cast<TDDeviceEvent>(&CTellstick::deviceEventCallback), this ); |
| 90 | + int intNumberOfDevices = tdGetNumberOfDevices(); |
| 91 | + for (int i = 0; i < intNumberOfDevices; i++) { |
| 92 | + int id = tdGetDeviceId(i); |
| 93 | + char *name = tdGetName(id); |
| 94 | + _log.Log(LOG_NORM, "Tellstick: %s method %d", name, tdMethods(id, TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_DIM) & TELLSTICK_DIM); |
| 95 | + bool isDimmer = tdMethods(id, TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_DIM) & TELLSTICK_DIM; |
| 96 | + AddSwitchIfNotExits(id, name, isDimmer); |
| 97 | + tdReleaseString(name); |
| 98 | + } |
| 99 | + |
| 100 | +} |
| 101 | + |
| 102 | +bool CTellstick::StartHardware() |
| 103 | +{ |
| 104 | + Init(); |
| 105 | + m_bIsStarted=true; |
| 106 | + sOnConnected(this); |
| 107 | + _log.Log(LOG_NORM, "Tellstick: StartHardware"); |
| 108 | + return true; |
| 109 | +} |
| 110 | + |
| 111 | +bool CTellstick::StopHardware() |
| 112 | +{ |
| 113 | + tdClose(); |
| 114 | + m_bIsStarted=false; |
| 115 | + return true; |
| 116 | +} |
| 117 | + |
| 118 | +bool CTellstick::WriteToHardware(const char *pdata, const unsigned char length) |
| 119 | +{ |
| 120 | + _tGeneralSwitch *pSwitch = (_tGeneralSwitch*)pdata; |
| 121 | + //tRBUF *pCmd=(tRBUF*) pdata; |
| 122 | + _log.Log(LOG_NORM, "Tellstick: WriteToHardware %d id: %d cmd: %d", pSwitch->type, pSwitch->id, pSwitch->cmnd); |
| 123 | + |
| 124 | + if (pSwitch->type != pTypeGeneralSwitch) |
| 125 | + return false; //only allowed to control switches |
| 126 | + |
| 127 | + if (pSwitch->cmnd == gswitch_sOn) |
| 128 | + { |
| 129 | + _log.Log(LOG_NORM, "Tellstick: Switch ON"); |
| 130 | + tdTurnOn(pSwitch->id); |
| 131 | + } |
| 132 | + else if(pSwitch->cmnd == gswitch_sOff) |
| 133 | + { |
| 134 | + _log.Log(LOG_NORM, "Tellstick: Switch OFF"); |
| 135 | + tdTurnOff(pSwitch->id); |
| 136 | + } |
| 137 | + else if(pSwitch->cmnd == gswitch_sSetLevel) |
| 138 | + { |
| 139 | + _log.Log(LOG_NORM, "Tellstick: Dim level %d %d", pSwitch->level, pSwitch->level * 255 / 99); |
| 140 | + tdDim(pSwitch->id, pSwitch->level * 255 / 99); |
| 141 | + } |
| 142 | + |
| 143 | + return true; |
| 144 | +} |
| 145 | +void CTellstick::SendCommand(const int ID, const std::string &command) |
| 146 | +{ |
| 147 | + _log.Log(LOG_NORM, "Tellstick: SendCommand"); |
| 148 | +} |
| 149 | +#endif //WITH_TELLDUSCORE |
0 commit comments