[490] | 1 | #if 0
|
---|
| 2 | Inter-driver communications tutorial - Dome Driver
|
---|
| 3 | Copyright (C) 2007 Jasem Mutlaq (mutlaqja@ikarustech.com)
|
---|
| 4 |
|
---|
| 5 | This library is free software; you can redistribute it and/or
|
---|
| 6 | modify it under the terms of the GNU Lesser General Public
|
---|
| 7 | License as published by the Free Software Foundation; either
|
---|
| 8 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 9 |
|
---|
| 10 | This library is distributed in the hope that it will be useful,
|
---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 13 | Lesser General Public License for more details.
|
---|
| 14 |
|
---|
| 15 | You should have received a copy of the GNU Lesser General Public
|
---|
| 16 | License along with this library; if not, write to the Free Software
|
---|
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 18 |
|
---|
| 19 | #endif
|
---|
| 20 |
|
---|
| 21 | #include <stdio.h>
|
---|
| 22 | #include <stdlib.h>
|
---|
| 23 | #include <string.h>
|
---|
| 24 | #include <stdarg.h>
|
---|
| 25 | #include <math.h>
|
---|
| 26 | #include <unistd.h>
|
---|
| 27 | #include <time.h>
|
---|
| 28 | #include <fcntl.h>
|
---|
| 29 | #include <errno.h>
|
---|
| 30 | #include <sys/stat.h>
|
---|
| 31 | #include <sys/time.h>
|
---|
| 32 | #include <sys/types.h>
|
---|
| 33 | #include <sys/socket.h>
|
---|
| 34 | #include <netinet/in.h>
|
---|
| 35 | #include <netdb.h>
|
---|
| 36 | #include <zlib.h>
|
---|
| 37 |
|
---|
| 38 | #include "indidevapi.h"
|
---|
| 39 | #include "eventloop.h"
|
---|
| 40 | #include "indicom.h"
|
---|
| 41 |
|
---|
| 42 | #define mydev "Dome"
|
---|
| 43 |
|
---|
| 44 | #define MAIN_GROUP "Main"
|
---|
| 45 | #define SNOOP_GROUP "Snooped"
|
---|
| 46 |
|
---|
| 47 | /* Connect/Disconnect */
|
---|
| 48 | static ISwitch PowerS[] = {{"CONNECT" , "Connect" , ISS_OFF, 0, 0},{"DISCONNECT", "Disconnect", ISS_ON, 0, 0}};
|
---|
| 49 | static ISwitchVectorProperty PowerSP = { mydev, "CONNECTION" , "Connection", MAIN_GROUP, IP_RW, ISR_1OFMANY, 60, IPS_IDLE, PowerS, NARRAY(PowerS), "", 0};
|
---|
| 50 |
|
---|
| 51 | /* Dome Open/Close */
|
---|
| 52 | static ISwitch DomeS[] = {{"Open", "", ISS_ON, 0, 0}, {"Close", "", ISS_OFF, 0, 0}};
|
---|
| 53 | static ISwitchVectorProperty DomeSP = { mydev, "Dome Status", "", MAIN_GROUP, IP_RW, ISR_1OFMANY, 0, IPS_IDLE, DomeS , NARRAY(DomeS), "", 0};
|
---|
| 54 |
|
---|
| 55 | /* The property we're trying to fetch from the rain driver. It's a carbon copy of the property define in rain.c, we initilize its structure here like any
|
---|
| 56 | other local property, but we don't define it to client, it's only used internally within the driver.
|
---|
| 57 |
|
---|
| 58 | MAKE SURE to set the property's device name to the device we're snooping on! If you copy/paste the property definition from the snooped driver
|
---|
| 59 | don't forget to remove "mydev" and replace it with the driver name.
|
---|
| 60 | */
|
---|
| 61 | static ILight RainL[] = {{"Status", "", IPS_IDLE, 0, 0}};
|
---|
| 62 | static ILightVectorProperty RainLP = { "Rain", "Rain Alert", "", SNOOP_GROUP, IPS_IDLE, RainL , NARRAY(RainL), "", 0};
|
---|
| 63 |
|
---|
| 64 | void ISGetProperties (const char *dev)
|
---|
| 65 | {
|
---|
| 66 | /* MAIN_GROUP */
|
---|
| 67 | IDDefSwitch(&PowerSP, NULL);
|
---|
| 68 | IDDefSwitch(&DomeSP, NULL);
|
---|
| 69 |
|
---|
| 70 | /* Let's listen for Rain Alert property in the device Rain */
|
---|
| 71 | IDSnoopDevice("Rain", "Rain Alert");
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | void ISNewBLOB (const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n) {}
|
---|
| 75 |
|
---|
| 76 | void ISSnoopDevice (XMLEle *root)
|
---|
| 77 | {
|
---|
| 78 |
|
---|
| 79 | IPState old_state = RainL[0].s;
|
---|
| 80 |
|
---|
| 81 | /* If the "Rain Alert" property gets updated in the Rain device, we will receive a notification. We need to process the new values of Rain Alert and update the local version
|
---|
| 82 | of the property.*/
|
---|
| 83 | if (IUSnoopLight(root, &RainLP) == 0)
|
---|
| 84 | {
|
---|
| 85 |
|
---|
| 86 | // If the dome is connected and rain is Alert */
|
---|
| 87 | if (PowerSP.s == IPS_OK && RainL[0].s == IPS_ALERT)
|
---|
| 88 | {
|
---|
| 89 | // If dome is open, then close it */
|
---|
| 90 | if (DomeS[0].s == ISS_ON)
|
---|
| 91 | closeDome();
|
---|
| 92 | else
|
---|
| 93 | IDMessage(mydev, "Rain Alert Detected! Dome is already closed.");
|
---|
| 94 | }
|
---|
| 95 | else if (old_state == IPS_ALERT && RainL[0].s != IPS_ALERT)
|
---|
| 96 | IDMessage(mydev, "Rain threat passed. Opening the dome is now safe.");
|
---|
| 97 |
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | void ISNewSwitch (const char *dev, const char *name, ISState *states, char *names[], int n)
|
---|
| 103 | {
|
---|
| 104 | /* ignore if not ours */
|
---|
| 105 | if (dev && strcmp (dev, mydev))
|
---|
| 106 | return;
|
---|
| 107 |
|
---|
| 108 | /* Connection */
|
---|
| 109 | if (!strcmp (name, PowerSP.name))
|
---|
| 110 | {
|
---|
| 111 | if (IUUpdateSwitch(&PowerSP, states, names, n) < 0)
|
---|
| 112 | return;
|
---|
| 113 |
|
---|
| 114 | PowerSP.s = IPS_OK;
|
---|
| 115 |
|
---|
| 116 | if (PowerS[0].s == ISS_ON)
|
---|
| 117 | IDSetSwitch(&PowerSP, "Dome is online.");
|
---|
| 118 | else
|
---|
| 119 | {
|
---|
| 120 | PowerSP.s = IPS_IDLE;
|
---|
| 121 | IDSetSwitch(&PowerSP, "Dome is offline.");
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | return;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /* Dome */
|
---|
| 128 | if (!strcmp (name, DomeSP.name))
|
---|
| 129 | {
|
---|
| 130 |
|
---|
| 131 | if (PowerSP.s != IPS_OK)
|
---|
| 132 | {
|
---|
| 133 | IDMessage(mydev, "Dome is offline!");
|
---|
| 134 | return;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | if (IUUpdateSwitch(&DomeSP, states, names, n) < 0)
|
---|
| 138 | return;
|
---|
| 139 |
|
---|
| 140 | DomeSP.s = IPS_BUSY;
|
---|
| 141 |
|
---|
| 142 | if (DomeS[0].s == ISS_ON)
|
---|
| 143 | {
|
---|
| 144 | if (RainL[0].s == IPS_ALERT)
|
---|
| 145 | {
|
---|
| 146 | DomeSP.s = IPS_ALERT;
|
---|
| 147 | DomeS[0].s = ISS_OFF;
|
---|
| 148 | DomeS[1].s = ISS_ON;
|
---|
| 149 | IDSetSwitch(&DomeSP, "It is raining, cannot open dome.");
|
---|
| 150 | return;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | IDSetSwitch(&DomeSP, "Dome is opening.");
|
---|
| 154 | }
|
---|
| 155 | else
|
---|
| 156 | IDSetSwitch(&DomeSP, "Dome is closing.");
|
---|
| 157 |
|
---|
| 158 | sleep(5);
|
---|
| 159 |
|
---|
| 160 | DomeSP.s = IPS_OK;
|
---|
| 161 |
|
---|
| 162 | if (DomeS[0].s == ISS_ON)
|
---|
| 163 | IDSetSwitch(&DomeSP, "Dome is open.");
|
---|
| 164 | else
|
---|
| 165 | IDSetSwitch(&DomeSP, "Dome is closed.");
|
---|
| 166 |
|
---|
| 167 | return;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | void ISNewText (const char *dev, const char *name, char *texts[], char *names[], int n)
|
---|
| 173 | {
|
---|
| 174 | /* ignore if not ours */
|
---|
| 175 | if (dev && strcmp (mydev, dev))
|
---|
| 176 | return;
|
---|
| 177 |
|
---|
| 178 | /* suppress warning */
|
---|
| 179 | n=n; dev=dev; name=name; names=names; texts=texts;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 |
|
---|
| 183 | void ISNewNumber (const char *dev, const char *name, double values[], char *names[], int n) {}
|
---|
| 184 |
|
---|
| 185 | void closeDome()
|
---|
| 186 | {
|
---|
| 187 | DomeSP.s = IPS_BUSY;
|
---|
| 188 |
|
---|
| 189 | IDSetSwitch(&DomeSP, "Rain Alert! Dome is closing...");
|
---|
| 190 |
|
---|
| 191 | sleep(5);
|
---|
| 192 |
|
---|
| 193 | DomeS[0].s = ISS_OFF;
|
---|
| 194 | DomeS[1].s = ISS_ON;
|
---|
| 195 |
|
---|
| 196 | DomeSP.s = IPS_OK;
|
---|
| 197 |
|
---|
| 198 | IDSetSwitch(&DomeSP, "Dome is closed.");
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 | }
|
---|
| 202 |
|
---|