svghmi/svghmi.c
changeset 3613 7af7a23e4adb
parent 3400 c2b46d0965ca
equal deleted inserted replaced
3604:be87303d5b2d 3613:7af7a23e4adb
   101     uint32_t session_index = 0;
   101     uint32_t session_index = 0;
   102     int value_changed = 0;
   102     int value_changed = 0;
   103     void *dest_p = NULL;
   103     void *dest_p = NULL;
   104     void *value_p = NULL;
   104     void *value_p = NULL;
   105     size_t sz = 0;
   105     size_t sz = 0;
       
   106     int do_sample = 0;
   106     while(session_index < MAX_CONNECTIONS) {
   107     while(session_index < MAX_CONNECTIONS) {
   107         if(dsc->wstate[session_index] == buf_set){
   108         if(dsc->wstate[session_index] == buf_set){
   108             /* if being subscribed */
   109             /* if being subscribed */
   109             if(dsc->refresh_period_ms[session_index]){
   110             if(dsc->refresh_period_ms[session_index]){
   110                 if(dsc->age_ms[session_index] + ticktime_ms < dsc->refresh_period_ms[session_index]){
   111                 uint16_t new_age_ms = dsc->age_ms[session_index] + ticktime_ms;
   111                     dsc->age_ms[session_index] += ticktime_ms;
   112                 if(new_age_ms < dsc->refresh_period_ms[session_index]){
       
   113                     dsc->age_ms[session_index] = new_age_ms;
   112                 }else{
   114                 }else{
   113                     dsc->wstate[session_index] = buf_tosend;
   115                     dsc->wstate[session_index] = buf_tosend;
   114                     global_write_dirty = 1;
   116                     global_write_dirty = 1;
   115                 }
   117                 }
   116             }
   118             }
   117         }
   119         }
   118 
   120 
   119         /* variable is sample only if just subscribed
   121         /* variable is sampled if just subscribed (initial value)
   120            or already subscribed and having value change */
   122            or already subscribed and having value change */
   121         int do_sample = 0;
   123         int sample_session = 0;
   122         int just_subscribed = dsc->wstate[session_index] == buf_new;
   124         int just_subscribed = dsc->wstate[session_index] == buf_new;
   123         if(!just_subscribed){
   125         if(just_subscribed){
       
   126             sample_session = 1;
       
   127         } else {
   124             int already_subscribed = dsc->refresh_period_ms[session_index] > 0;
   128             int already_subscribed = dsc->refresh_period_ms[session_index] > 0;
   125             if(already_subscribed){
   129             if(already_subscribed){
   126                 if(!value_changed){
   130                 /* compute value_changed once only */
   127                     if(!value_p){
   131                 if(!value_p){
   128                         UnpackVar(dsc, &value_p, NULL, &sz);
   132                     UnpackVar(dsc, &value_p, NULL, &sz);
   129                         if(__Is_a_string(dsc)){
   133                     if(__Is_a_string(dsc)){
   130                             sz = ((STRING*)value_p)->len + 1;
   134                         sz = ((STRING*)value_p)->len + 1;
   131                         }
   135                     }
   132                         dest_p = &wbuf[dsc->buf_index];
   136                     dest_p = &wbuf[dsc->buf_index];
   133                     }
       
   134                     value_changed = memcmp(dest_p, value_p, sz) != 0;
   137                     value_changed = memcmp(dest_p, value_p, sz) != 0;
   135                     do_sample = value_changed;
   138                 }
   136                 }else{
   139                 sample_session = value_changed;
   137                     do_sample = 1;
   140             }
   138                 }
   141         }
   139             }
   142 
   140         } else {
   143 
   141             do_sample = 1;
   144         if(sample_session){
   142         }
       
   143 
       
   144 
       
   145         if(do_sample){
       
   146             if(dsc->wstate[session_index] != buf_set && dsc->wstate[session_index] != buf_tosend) {
   145             if(dsc->wstate[session_index] != buf_set && dsc->wstate[session_index] != buf_tosend) {
   147                 if(dsc->wstate[session_index] == buf_new \
   146                 if(dsc->wstate[session_index] == buf_new \
   148                    || ticktime_ms > dsc->refresh_period_ms[session_index]){
   147                    || ticktime_ms > dsc->refresh_period_ms[session_index]){
   149                     dsc->wstate[session_index] = buf_tosend;
   148                     dsc->wstate[session_index] = buf_tosend;
   150                     global_write_dirty = 1;
   149                     global_write_dirty = 1;
   151                 } else {
   150                 } else {
   152                     dsc->wstate[session_index] = buf_set;
   151                     dsc->wstate[session_index] = buf_set;
   153                 }
   152                 }
   154                 dsc->age_ms[session_index] = 0;
   153                 dsc->age_ms[session_index] = 0;
   155             }
   154             }
       
   155             do_sample = 1;
   156         }
   156         }
   157 
   157 
   158         session_index++;
   158         session_index++;
   159     }
   159     }
   160     /* copy value if changed (and subscribed) */
   160 
   161     if(value_changed)
   161     /* copy value if one at least one session did sample */
       
   162     if(do_sample){
       
   163         if(!value_p){
       
   164             UnpackVar(dsc, &value_p, NULL, &sz);
       
   165             if(__Is_a_string(dsc)){
       
   166                 sz = ((STRING*)value_p)->len + 1;
       
   167             }
       
   168             dest_p = &wbuf[dsc->buf_index];
       
   169         }
   162         memcpy(dest_p, value_p, sz);
   170         memcpy(dest_p, value_p, sz);
       
   171     }
   163     return 0;
   172     return 0;
   164 }
   173 }
   165 
   174 
   166 static int send_iterator(uint32_t index, hmi_tree_item_t *dsc, uint32_t session_index)
   175 static int send_iterator(uint32_t index, hmi_tree_item_t *dsc, uint32_t session_index)
   167 {
   176 {